/*
* Licensed to the Apache Software Foundation (ASF) under one
* or more contributor license agreements. See the NOTICE file
* distributed with this work for additional information
* regarding copyright ownership. The ASF licenses this file
* to you under the Apache License, Version 2.0 (the
* "License"); you may not use this file except in compliance
* with the License. You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing,
* software distributed under the License is distributed on an
* "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
* KIND, either express or implied. See the License for the
* specific language governing permissions and limitations
* under the License.
*
*/
package org.apache.muse.tools.generator.util;
import java.util.HashMap;
import java.util.Iterator;
import java.util.Map;
import javax.xml.namespace.QName;
import org.apache.muse.tools.inspector.JavaMethod;
import org.apache.muse.util.xml.XmlUtils;
import org.w3c.dom.Document;
import org.w3c.dom.Element;
import org.w3c.dom.Node;
/**
* Helps the Axis2Projectizer create a services.xml. Handles
* adding multiple services to the service group for situations
* where the muse.xml descriptor that we're generating from
* has more than one resource-type.
*
* @author Andrew Eberbach (aeberbac)
*/
public class ServicesDescriptorHelper {
private static final QName SERVICE_GROUP_QNAME = new QName("serviceGroup");
private static final QName SERVICE_QNAME = new QName("service");
private static final QName OPERATION_QNAME = new QName("operation");
String AXIS2_ISOLATION_LAYER = "org.apache.muse.core.platform.axis2.AxisIsolationLayer";
private static final QName ACTION_MAPPING = new QName("actionMapping");
private static final String NAME_ATTRIBUTE = "name";
private static final String SERVICE_CLASS = "ServiceClass";
private static final QName PARAMETER_QNAME = new QName("parameter");
private static final String LOCKED_ATTRIBUTE = "locked";
private static final String HANDLE_REQUEST = "handleRequest";
private static final String MESSAGE_RECV_CLASS = "org.apache.axis2.receivers.RawXMLINOutMessageReceiver";
private static final QName MESSAGE_RECV_QNAME = new QName("messageReceiver");
private static final String CLASS_ATTRIBUTE = "class";
private Document _servicesDocument;
private Element _serviceGroupElement;
private Map _serviceOperations;
public ServicesDescriptorHelper() {
_serviceOperations = new HashMap();
_servicesDocument = XmlUtils.createDocument();
_serviceGroupElement = XmlUtils.createElement(_servicesDocument, SERVICE_GROUP_QNAME);
_servicesDocument.appendChild(_serviceGroupElement);
}
private String makeActionURI(Capability capability, JavaMethod method) {
String actionURI = method.getActionURI();
if(actionURI != null) {
return actionURI;
}
String name = method.getName().getLocalPart();
name = name.substring(0,1).toUpperCase() + name.substring(1);
return capability.getURI() + "/" + name;
}
public void addActionMappings(Capability capability, String serviceName) {
Element operationElement = (Element) _serviceOperations.get(serviceName);
for (Iterator j = capability.getOperations().iterator(); j.hasNext();) {
JavaMethod method = (JavaMethod) j.next();
Element actionMapping = XmlUtils.createElement(_servicesDocument,ACTION_MAPPING);
XmlUtils.setElementText(actionMapping, makeActionURI(
capability, method));
operationElement.appendChild(actionMapping);
}
}
public Node getDocument() {
return _servicesDocument;
}
public void createService(String serviceName) {
Element serviceElement = XmlUtils.createElement(_servicesDocument, SERVICE_QNAME);
setName(serviceElement, serviceName);
Element parameter = createParameter(SERVICE_CLASS, AXIS2_ISOLATION_LAYER,false);
serviceElement.appendChild(parameter);
Element operation = createOperation(HANDLE_REQUEST);
serviceElement.appendChild(operation);
_serviceOperations.put(serviceName, operation);
_serviceGroupElement.appendChild(serviceElement);
}
private Element createOperation(String name) {
Element operationElement = XmlUtils.createElement(_servicesDocument, OPERATION_QNAME);
operationElement.setAttribute(NAME_ATTRIBUTE, name);
Element messageReceiver = createMessageReceiver(MESSAGE_RECV_CLASS);
operationElement.appendChild(messageReceiver);
return operationElement;
}
private Element createMessageReceiver(String recvClass) {
Element messageRecvElement = XmlUtils.createElement(_servicesDocument, MESSAGE_RECV_QNAME);
messageRecvElement.setAttribute(CLASS_ATTRIBUTE, recvClass);
return messageRecvElement;
}
private Element createParameter(String name, String value, boolean locked) {
Element parameterElement = XmlUtils.createElement(_servicesDocument, PARAMETER_QNAME);
parameterElement.setAttribute(NAME_ATTRIBUTE, name);
parameterElement.setAttribute(LOCKED_ATTRIBUTE, String.valueOf(locked));
XmlUtils.setElementText(parameterElement, value);
return parameterElement;
}
private void setName(Element serviceElement, String serviceName) {
serviceElement.setAttribute(NAME_ATTRIBUTE, serviceName);
}
}