Added: synapse/trunk/java/modules/core/src/main/java/org/apache/synapse/config/xml/endpoints/TemplateFactory.java URL: http://svn.apache.org/viewvc/synapse/trunk/java/modules/core/src/main/java/org/apache/synapse/config/xml/endpoints/TemplateFactory.java?rev=1080161&view=auto ============================================================================== --- synapse/trunk/java/modules/core/src/main/java/org/apache/synapse/config/xml/endpoints/TemplateFactory.java (added) +++ synapse/trunk/java/modules/core/src/main/java/org/apache/synapse/config/xml/endpoints/TemplateFactory.java Thu Mar 10 09:16:32 2011 @@ -0,0 +1,87 @@ +/* + * 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.synapse.config.xml.endpoints; + +import org.apache.axiom.om.OMAttribute; +import org.apache.axiom.om.OMElement; +import org.apache.commons.logging.Log; +import org.apache.commons.logging.LogFactory; +import org.apache.synapse.SynapseConstants; +import org.apache.synapse.SynapseException; +import org.apache.synapse.config.xml.XMLConfigConstants; +import org.apache.synapse.endpoints.Template; + +import javax.xml.namespace.QName; +import java.util.Iterator; +import java.util.Properties; + +public class TemplateFactory { + public static final Log log = LogFactory.getLog(TemplateFactory.class); + + public Template createEndpointTemplate(OMElement element, Properties properties) { + Template template = new Template(); + + OMAttribute nameAttribute = element.getAttribute( + new QName(XMLConfigConstants.NULL_NAMESPACE, "name")); + + if (nameAttribute != null) { + template.setName(nameAttribute.getAttributeValue()); + } else { + handleException("Error loading the configuration from endpointTemplate, '" + + "name' attribute missing"); + } + + Iterator paramItr = element.getChildrenWithName( + new QName(SynapseConstants.SYNAPSE_NAMESPACE, "parameter")); + while (paramItr.hasNext()) { + OMElement paramElement = (OMElement) paramItr.next(); + + OMAttribute paramName = paramElement.getAttribute(new QName("name")); + OMAttribute paramValue = paramElement.getAttribute(new QName("value")); + + if (paramName == null) { + handleException("parameter name should be present"); + } + + if (paramValue == null) { + handleException("parameter value should be present"); + } + + assert paramName != null; + assert paramValue != null; + + template.addParameter(paramName.getAttributeValue(), paramValue.getAttributeValue()); + } + + OMElement endpointElement = element.getFirstChildWithName( + new QName(SynapseConstants.SYNAPSE_NAMESPACE, "endpoint")); + if (endpointElement == null) { + handleException("endpoint element is required in an endpoint template"); + } + template.setElement(endpointElement); + + return template; + } + + protected void handleException(String message) { + log.error(message); + throw new SynapseException(message); + } +}
Added: synapse/trunk/java/modules/core/src/main/java/org/apache/synapse/config/xml/endpoints/TemplateSerializer.java URL: http://svn.apache.org/viewvc/synapse/trunk/java/modules/core/src/main/java/org/apache/synapse/config/xml/endpoints/TemplateSerializer.java?rev=1080161&view=auto ============================================================================== --- synapse/trunk/java/modules/core/src/main/java/org/apache/synapse/config/xml/endpoints/TemplateSerializer.java (added) +++ synapse/trunk/java/modules/core/src/main/java/org/apache/synapse/config/xml/endpoints/TemplateSerializer.java Thu Mar 10 09:16:32 2011 @@ -0,0 +1,64 @@ +/* + * 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.synapse.config.xml.endpoints; + +import org.apache.axiom.om.OMAbstractFactory; +import org.apache.axiom.om.OMElement; +import org.apache.axiom.om.OMFactory; +import org.apache.axiom.om.OMNamespace; +import org.apache.synapse.SynapseConstants; +import org.apache.synapse.config.xml.XMLConfigConstants; +import org.apache.synapse.endpoints.Template; + +import javax.xml.namespace.QName; +import java.util.Map; + +public class TemplateSerializer { + protected static OMFactory fac = OMAbstractFactory.getOMFactory(); + + protected static final OMNamespace nullNS + = fac.createOMNamespace(XMLConfigConstants.NULL_NAMESPACE, ""); + + public OMElement serializeEndpointTemplate(Template template, OMElement parent) { + OMElement templateElement = + fac.createOMElement("template", SynapseConstants.SYNAPSE_OMNAMESPACE); + + templateElement.addAttribute(fac.createOMAttribute("name", nullNS, template.getName())); + + Map<String, String> parameters = template.getParameters(); + for (Map.Entry<String, String> entry : parameters.entrySet()) { + OMElement paramElement = fac.createOMElement( + new QName(SynapseConstants.SYNAPSE_NAMESPACE, "parameter")); + + paramElement.addAttribute(fac.createOMAttribute("name", nullNS, entry.getKey())); + paramElement.addAttribute(fac.createOMAttribute("value", nullNS, entry.getValue())); + + templateElement.addChild(paramElement); + } + + templateElement.addChild(template.getElement().cloneOMElement()); + + if (parent != null) { + parent.addChild(templateElement); + } + + return parent; + } +} Modified: synapse/trunk/java/modules/core/src/main/java/org/apache/synapse/config/xml/endpoints/WSDLEndpointFactory.java URL: http://svn.apache.org/viewvc/synapse/trunk/java/modules/core/src/main/java/org/apache/synapse/config/xml/endpoints/WSDLEndpointFactory.java?rev=1080161&r1=1080160&r2=1080161&view=diff ============================================================================== --- synapse/trunk/java/modules/core/src/main/java/org/apache/synapse/config/xml/endpoints/WSDLEndpointFactory.java (original) +++ synapse/trunk/java/modules/core/src/main/java/org/apache/synapse/config/xml/endpoints/WSDLEndpointFactory.java Thu Mar 10 09:16:32 2011 @@ -95,7 +95,13 @@ public class WSDLEndpointFactory extends (new QName(SynapseConstants.SYNAPSE_NAMESPACE, "wsdl")); if (wsdlElement != null) { - EndpointDefinition endpoint = null; + EndpointDefinitionFactory fac = new EndpointDefinitionFactory(); + EndpointDefinition endpoint = fac.createDefinition(wsdlElement); + + // for now, QOS information has to be provided explicitly. + extractSpecificEndpointProperties(endpoint, wsdlElement); + wsdlEndpoint.setDefinition(endpoint); + processAuditStatus(endpoint, wsdlEndpoint.getName(), wsdlElement); // get the service name and port name. at this point we should not worry about // the presence of those parameters. they are handled by corresponding WSDL builders. @@ -125,8 +131,8 @@ public class WSDLEndpointFactory extends String nsUri = omElement.getNamespace().getNamespaceURI(); if (org.apache.axis2.namespace.Constants.NS_URI_WSDL11.equals(nsUri)) { - endpoint = new WSDL11EndpointBuilder(). - createEndpointDefinitionFromWSDL( + new WSDL11EndpointBuilder(). + populateEndpointDefinitionFromWSDL(endpoint, wsdlURI.trim(), omElement, serviceName, portName); } else if (WSDL2Constants.WSDL_NAMESPACE.equals(nsUri)) { @@ -141,8 +147,6 @@ public class WSDLEndpointFactory extends handleException("Couldn't create endpoint from the given WSDL URI : " + e.getMessage(), e); } - } else { - endpoint = new EndpointDefinition(); } } @@ -161,7 +165,7 @@ public class WSDLEndpointFactory extends if (!baseUri.endsWith(File.separator)) { baseUri = baseUri + File.separator; } - endpoint = new WSDL11EndpointBuilder().createEndpointDefinitionFromWSDL( + new WSDL11EndpointBuilder().populateEndpointDefinitionFromWSDL(endpoint, baseUri, definitionElement, serviceName, portName); } else { endpoint = new EndpointDefinition(); @@ -174,16 +178,6 @@ public class WSDLEndpointFactory extends if (endpoint == null && descriptionElement != null) { handleException("WSDL 2.0 Endpoints are currently not supported."); } - - if (endpoint != null) { - // for now, QOS information has to be provided explicitly. - extractCommonEndpointProperties(endpoint, wsdlElement); - extractSpecificEndpointProperties(endpoint, wsdlElement); - wsdlEndpoint.setDefinition(endpoint); - processAuditStatus(endpoint, wsdlEndpoint.getName(), wsdlElement); - } else { - handleException("WSDL is not specified for WSDL endpoint."); - } } // process the parameters Modified: synapse/trunk/java/modules/core/src/main/java/org/apache/synapse/config/xml/endpoints/WSDLEndpointSerializer.java URL: http://svn.apache.org/viewvc/synapse/trunk/java/modules/core/src/main/java/org/apache/synapse/config/xml/endpoints/WSDLEndpointSerializer.java?rev=1080161&r1=1080160&r2=1080161&view=diff ============================================================================== --- synapse/trunk/java/modules/core/src/main/java/org/apache/synapse/config/xml/endpoints/WSDLEndpointSerializer.java (original) +++ synapse/trunk/java/modules/core/src/main/java/org/apache/synapse/config/xml/endpoints/WSDLEndpointSerializer.java Thu Mar 10 09:16:32 2011 @@ -76,7 +76,8 @@ public class WSDLEndpointSerializer exte // special elements under the wsdl element. in future, these information should be // extracted from the wsdl. EndpointDefinition epDefinition = wsdlEndpoint.getDefinition(); - serializeCommonEndpointProperties(epDefinition, wsdlElement); + EndpointDefinitionSerializer serializer = new EndpointDefinitionSerializer(); + serializer.serializeEndpointDefinition(epDefinition, wsdlElement); serializeSpecificEndpointProperties(epDefinition, wsdlElement); endpointElement.addChild(wsdlElement); Modified: synapse/trunk/java/modules/core/src/main/java/org/apache/synapse/config/xml/endpoints/utils/WSDL11EndpointBuilder.java URL: http://svn.apache.org/viewvc/synapse/trunk/java/modules/core/src/main/java/org/apache/synapse/config/xml/endpoints/utils/WSDL11EndpointBuilder.java?rev=1080161&r1=1080160&r2=1080161&view=diff ============================================================================== --- synapse/trunk/java/modules/core/src/main/java/org/apache/synapse/config/xml/endpoints/utils/WSDL11EndpointBuilder.java (original) +++ synapse/trunk/java/modules/core/src/main/java/org/apache/synapse/config/xml/endpoints/utils/WSDL11EndpointBuilder.java Thu Mar 10 09:16:32 2011 @@ -58,14 +58,17 @@ public class WSDL11EndpointBuilder { * Creates an EndpointDefinition for WSDL endpoint from an inline WSDL supplied in the WSDL * endpoint configuration. * + * @param endpointDefinition the endpoint definition to populate * @param baseUri base uri of the wsdl * @param wsdl OMElement representing the inline WSDL * @param service Service of the endpoint * @param port Port of the endpoint * @return EndpointDefinition containing the information retrieved from the WSDL */ - public EndpointDefinition createEndpointDefinitionFromWSDL(String baseUri, OMElement wsdl, - String service, String port) { + public EndpointDefinition populateEndpointDefinitionFromWSDL( + EndpointDefinition endpointDefinition, + String baseUri, OMElement wsdl, + String service, String port) { ByteArrayOutputStream baos = new ByteArrayOutputStream(); try { @@ -87,7 +90,8 @@ public class WSDL11EndpointBuilder { WSDLFactory fac = WSDLFactory.newInstance(); WSDLReader reader = fac.newWSDLReader(); Definition definition = reader.readWSDL(wsdlLocator, doc.getDocumentElement()); - return createEndpointDefinitionFromWSDL(definition, service, port); + return createEndpointDefinitionFromWSDL( + endpointDefinition, definition, service, port); } } catch (XMLStreamException e) { handleException("Error retrieving the WSDL definition from the inline WSDL.", e); @@ -98,8 +102,10 @@ public class WSDL11EndpointBuilder { return null; } - private EndpointDefinition createEndpointDefinitionFromWSDL(Definition definition, - String serviceName, String portName) { + private EndpointDefinition createEndpointDefinitionFromWSDL( + EndpointDefinition endpointDefinition, + Definition definition, + String serviceName, String portName) { if (definition == null) { handleException("WSDL document is not specified."); @@ -163,7 +169,6 @@ public class WSDL11EndpointBuilder { } if (serviceURL != null) { - EndpointDefinition endpointDefinition = new EndpointDefinition(); endpointDefinition.setAddress(serviceURL); if (SynapseConstants.FORMAT_SOAP11.equals(format)) { endpointDefinition.setForceSOAP11(true); Added: synapse/trunk/java/modules/core/src/main/java/org/apache/synapse/endpoints/Template.java URL: http://svn.apache.org/viewvc/synapse/trunk/java/modules/core/src/main/java/org/apache/synapse/endpoints/Template.java?rev=1080161&view=auto ============================================================================== --- synapse/trunk/java/modules/core/src/main/java/org/apache/synapse/endpoints/Template.java (added) +++ synapse/trunk/java/modules/core/src/main/java/org/apache/synapse/endpoints/Template.java Thu Mar 10 09:16:32 2011 @@ -0,0 +1,115 @@ +/* + * 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.synapse.endpoints; + +import org.apache.axiom.om.OMAttribute; +import org.apache.axiom.om.OMElement; +import org.apache.commons.logging.Log; +import org.apache.commons.logging.LogFactory; +import org.apache.synapse.config.xml.endpoints.EndpointFactory; + +import java.util.HashMap; +import java.util.Iterator; +import java.util.Map; +import java.util.Properties; + +public class Template { + private OMElement element = null; + + private String name = null; + + private Map<String, String> parameters = new HashMap<String, String>(); + + public Endpoint create(TemplateEndpoint templateEndpoint, Properties properties) { + // first go through all the elements and replace with the parameters + OMElement clonedElement = element.cloneOMElement(); + replaceElement(templateEndpoint, clonedElement); + + return EndpointFactory.getEndpointFromElement(clonedElement, false, properties); + } + + public String getName() { + return name; + } + + public void setName(String name) { + this.name = name; + } + + public Map<String, String> getParameters() { + return parameters; + } + + public void addParameter(String name, String value) { + parameters.put(name, value); + } + + public void setElement(OMElement element) { + this.element = element; + } + + public OMElement getElement() { + return element; + } + + public void setParameters(Map<String, String> parameters) { + this.parameters = parameters; + } + + private void replaceElement(TemplateEndpoint templateEndpoint, OMElement element) { + Iterator attributesItr = element.getAllAttributes(); + while (attributesItr.hasNext()) { + OMAttribute attribute = (OMAttribute) attributesItr.next(); + + String replace = replace(attribute.getAttributeValue(), templateEndpoint); + + if (replace != null) { + attribute.setAttributeValue(replace); + } + } + + if (element.getText() != null && !"".equals(element.getText())) { + String replace = replace(element.getText(), templateEndpoint); + + if (replace != null) { + element.setText(replace); + } + } + + Iterator elemItr = element.getChildElements(); + while (elemItr.hasNext()) { + OMElement childElement = (OMElement) elemItr.next(); + + replaceElement(templateEndpoint, childElement); + } + } + + private String replace(String value, TemplateEndpoint templateEndpoint) { + if (value.startsWith("$")) { + String param = value.substring(1); + + if (templateEndpoint.getParameters().containsKey(param)) { + return templateEndpoint.getParameterValue(param); + } + } + + return null; + } +} Added: synapse/trunk/java/modules/core/src/main/java/org/apache/synapse/endpoints/TemplateEndpoint.java URL: http://svn.apache.org/viewvc/synapse/trunk/java/modules/core/src/main/java/org/apache/synapse/endpoints/TemplateEndpoint.java?rev=1080161&view=auto ============================================================================== --- synapse/trunk/java/modules/core/src/main/java/org/apache/synapse/endpoints/TemplateEndpoint.java (added) +++ synapse/trunk/java/modules/core/src/main/java/org/apache/synapse/endpoints/TemplateEndpoint.java Thu Mar 10 09:16:32 2011 @@ -0,0 +1,103 @@ +/* + * 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.synapse.endpoints; + +import org.apache.commons.logging.Log; +import org.apache.commons.logging.LogFactory; +import org.apache.synapse.MessageContext; +import org.apache.synapse.SynapseConstants; +import org.apache.synapse.core.SynapseEnvironment; + +import java.util.HashMap; +import java.util.Map; + +public class TemplateEndpoint extends AbstractEndpoint { + private static final Log log = LogFactory.getLog(TemplateEndpoint.class); + + private String template = null; + + private Endpoint realEndpoint = null; + + private Map<String, String> parameters = new HashMap<String, String>(); + + private String address = null; + + @Override + public void send(MessageContext synCtx) { + if (realEndpoint != null) { + realEndpoint.send(synCtx); + } else { + informFailure(synCtx, SynapseConstants.ENDPOINT_IN_DIRECT_NOT_READY, + "Couldn't find the endpoint with the name " + getName() + + " & template : " + template); + } + } + + public Map<String, String> getParameters() { + return parameters; + } + + public String getParameterValue(String name) { + return parameters.get(name); + } + + public void addParameter(String name, String value) { + parameters.put(name, value); + } + + public String getTemplate() { + return template; + } + + public void setTemplate(String template) { + this.template = template; + } + + public String getAddress() { + return address; + } + + public void setAddress(String address) { + this.address = address; + } + + @Override + public void init(SynapseEnvironment synapseEnvironment) { + super.init(synapseEnvironment); + + Template endpointTemplate = synapseEnvironment.getSynapseConfiguration(). + getEndpointTemplates().get(template); + + if (endpointTemplate == null) { + handleException("Template " + template + + " cannot be found for the endpoint " + getName()); + } + + realEndpoint = endpointTemplate.create(this, + synapseEnvironment.getSynapseConfiguration().getProperties()); + + realEndpoint.init(synapseEnvironment); + + if (realEndpoint == null) { + handleException("Couldn't retrieve the endpoint " + getName() + + " from the template: " + endpointTemplate.getName()); + } + } +}
