001: /**
002: * Licensed to the Apache Software Foundation (ASF) under one
003: * or more contributor license agreements. See the NOTICE file
004: * distributed with this work for additional information
005: * regarding copyright ownership. The ASF licenses this file
006: * to you under the Apache License, Version 2.0 (the
007: * "License"); you may not use this file except in compliance
008: * with the License. You may obtain a copy of the License at
009: *
010: * http://www.apache.org/licenses/LICENSE-2.0
011: *
012: * Unless required by applicable law or agreed to in writing,
013: * software distributed under the License is distributed on an
014: * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
015: * KIND, either express or implied. See the License for the
016: * specific language governing permissions and limitations
017: * under the License.
018: */package org.apache.cxf.wsdl;
019:
020: import java.net.URL;
021: import java.util.ArrayList;
022: import java.util.List;
023: import java.util.Map;
024: import java.util.WeakHashMap;
025: import java.util.logging.Level;
026: import java.util.logging.Logger;
027:
028: import javax.wsdl.Definition;
029: import javax.wsdl.Port;
030: import javax.wsdl.Service;
031: import javax.wsdl.WSDLException;
032: import javax.xml.XMLConstants;
033: import javax.xml.bind.JAXBElement;
034: import javax.xml.namespace.QName;
035: import javax.xml.transform.Source;
036: import javax.xml.transform.Transformer;
037: import javax.xml.transform.TransformerConfigurationException;
038: import javax.xml.transform.TransformerException;
039: import javax.xml.transform.TransformerFactory;
040: import javax.xml.transform.dom.DOMResult;
041: import javax.xml.transform.dom.DOMSource;
042: import javax.xml.transform.stream.StreamSource;
043: import javax.xml.validation.Schema;
044: import javax.xml.validation.SchemaFactory;
045:
046: import org.w3c.dom.Document;
047: import org.w3c.dom.Element;
048: import org.w3c.dom.Node;
049: import org.xml.sax.SAXException;
050:
051: import org.apache.cxf.Bus;
052: import org.apache.cxf.common.i18n.Message;
053: import org.apache.cxf.common.logging.LogUtils;
054: import org.apache.cxf.common.util.StringUtils;
055: import org.apache.cxf.endpoint.EndpointResolverRegistry;
056: import org.apache.cxf.endpoint.Server;
057: import org.apache.cxf.endpoint.ServerRegistry;
058: import org.apache.cxf.service.model.SchemaInfo;
059: import org.apache.cxf.service.model.ServiceInfo;
060: import org.apache.cxf.transport.Destination;
061: import org.apache.cxf.transport.MultiplexDestination;
062: import org.apache.cxf.ws.addressing.AttributedURIType;
063: import org.apache.cxf.ws.addressing.EndpointReferenceType;
064: import org.apache.cxf.ws.addressing.MetadataType;
065: import org.apache.cxf.ws.addressing.wsdl.AttributedQNameType;
066: import org.apache.cxf.ws.addressing.wsdl.ServiceNameType;
067:
068: /**
069: * Provides utility methods for obtaining endpoint references, wsdl definitions, etc.
070: */
071: public final class EndpointReferenceUtils {
072:
073: public static final String ANONYMOUS_ADDRESS = "http://www.w3.org/2005/08/addressing/anonymous";
074:
075: static WeakHashMap<ServiceInfo, Schema> schemaMap = new WeakHashMap<ServiceInfo, Schema>();
076:
077: private static final Logger LOG = LogUtils
078: .getL7dLogger(EndpointReferenceUtils.class);
079:
080: private static final String WSDL_INSTANCE_NAMESPACE = "http://www.w3.org/2006/01/wsdl-instance";
081: private static final String WSA_WSDL_NAMESPACE = "http://www.w3.org/2005/02/addressing/wsdl";
082: private static final String WSA_WSDL_NAMESPACE_PREFIX = "wsaw";
083: private static final QName WSA_WSDL_NAMESPACE_NS = new QName(
084: "xmlns:" + WSA_WSDL_NAMESPACE_PREFIX);
085: private static final String XML_SCHEMA_NAMESPACE = "http://www.w3.org/2001/XMLSchema";
086: private static final String XML_SCHEMA_NAMESPACE_PREFIX = "xs";
087: private static final QName XML_SCHEMA_NAMESPACE_NS = new QName(
088: "xmlns:" + XML_SCHEMA_NAMESPACE_PREFIX);
089: private static final String XML_SCHEMA_INSTANCE_NAMESPACE = "http://www.w3.org/2001/XMLSchema-instance";
090: private static final QName WSDL_LOCATION = new QName(
091: WSDL_INSTANCE_NAMESPACE, "wsdlLocation");
092: private static final QName XSI_TYPE = new QName(
093: XML_SCHEMA_INSTANCE_NAMESPACE, "type", "xsi");
094:
095: private static final org.apache.cxf.ws.addressing.wsdl.ObjectFactory WSA_WSDL_OBJECT_FACTORY = new org.apache.cxf.ws.addressing.wsdl.ObjectFactory();
096: private static final org.apache.cxf.ws.addressing.ObjectFactory WSA_OBJECT_FACTORY = new org.apache.cxf.ws.addressing.ObjectFactory();
097:
098: private EndpointReferenceUtils() {
099: // Utility class - never constructed
100: }
101:
102: private static Transformer getTransformer()
103: throws EndpointUtilsException {
104: //To Support IBM JDK
105: //If use the default transformFactory ,org.apache.xalan.processor.TransformerFactoryImpl \
106: //when transform stuff will lost attributes
107: if (System.getProperty("java.vendor").indexOf("IBM") > -1) {
108: System
109: .setProperty(
110: "javax.xml.transform.TransformerFactory",
111: "org.apache.xalan.xsltc.trax.TransformerFactoryImpl");
112: }
113:
114: try {
115: return TransformerFactory.newInstance().newTransformer();
116: } catch (TransformerConfigurationException tce) {
117: throw new EndpointUtilsException(new Message(
118: "COULD_NOT_CREATE_TRANSFORMER", LOG), tce);
119: }
120:
121: }
122:
123: /**
124: * Sets the service and port name of the provided endpoint reference.
125: * @param ref the endpoint reference.
126: * @param serviceName the name of service.
127: * @param portName the port name.
128: */
129: public static void setServiceAndPortName(EndpointReferenceType ref,
130: QName serviceName, String portName) {
131: if (null != serviceName) {
132: JAXBElement<ServiceNameType> jaxbElement = getServiceNameType(
133: serviceName, portName);
134: MetadataType mt = ref.getMetadata();
135: if (null == mt) {
136: mt = new MetadataType();
137: ref.setMetadata(mt);
138: }
139:
140: mt.getAny().add(jaxbElement);
141: }
142: }
143:
144: public static JAXBElement<ServiceNameType> getServiceNameType(
145: QName serviceName, String portName) {
146: ServiceNameType serviceNameType = WSA_WSDL_OBJECT_FACTORY
147: .createServiceNameType();
148: serviceNameType.setValue(serviceName);
149: serviceNameType.setEndpointName(portName);
150: serviceNameType.getOtherAttributes().put(WSA_WSDL_NAMESPACE_NS,
151: WSA_WSDL_NAMESPACE);
152: serviceNameType.getOtherAttributes().put(
153: XSI_TYPE,
154: WSA_WSDL_NAMESPACE_PREFIX + ":"
155: + serviceNameType.getClass().getSimpleName());
156: return WSA_WSDL_OBJECT_FACTORY
157: .createServiceName(serviceNameType);
158: }
159:
160: /**
161: * Gets the service name of the provided endpoint reference.
162: * @param ref the endpoint reference.
163: * @return the service name.
164: */
165: public static QName getServiceName(EndpointReferenceType ref) {
166: MetadataType metadata = ref.getMetadata();
167: if (metadata != null) {
168: for (Object obj : metadata.getAny()) {
169: if (obj instanceof Element) {
170: Node node = (Element) obj;
171: if (node.getNamespaceURI().equals(
172: WSA_WSDL_NAMESPACE)
173: && node.getLocalName()
174: .equals("ServiceName")) {
175: String content = node.getTextContent();
176: String namespaceURI = node.getFirstChild()
177: .getNamespaceURI();
178: String service = content;
179: if (content.contains(":")) {
180: namespaceURI = getNameSpaceUri(node,
181: content, namespaceURI);
182: service = getService(content);
183: } else {
184: Node nodeAttr = node.getAttributes()
185: .getNamedItem("xmlns");
186: namespaceURI = nodeAttr.getNodeValue();
187: }
188:
189: return new QName(namespaceURI, service);
190: }
191: } else if (obj instanceof JAXBElement) {
192: Object val = ((JAXBElement) obj).getValue();
193: if (val instanceof ServiceNameType) {
194: return ((ServiceNameType) val).getValue();
195: }
196: } else if (obj instanceof ServiceNameType) {
197: return ((ServiceNameType) obj).getValue();
198: }
199: }
200: }
201: return null;
202: }
203:
204: /**
205: * Gets the port name of the provided endpoint reference.
206: * @param ref the endpoint reference.
207: * @return the port name.
208: */
209: public static String getPortName(EndpointReferenceType ref) {
210: MetadataType metadata = ref.getMetadata();
211: if (metadata != null) {
212: for (Object obj : metadata.getAny()) {
213: if (obj instanceof Element) {
214: Node node = (Element) obj;
215: if (node.getNamespaceURI().equals(
216: WSA_WSDL_NAMESPACE)
217: && node.getNodeName().contains(
218: "ServiceName")) {
219: return node.getAttributes().getNamedItem(
220: "EndpointName").getTextContent();
221: }
222: } else if (obj instanceof JAXBElement) {
223: Object val = ((JAXBElement) obj).getValue();
224: if (val instanceof ServiceNameType) {
225: return ((ServiceNameType) val)
226: .getEndpointName();
227: }
228: } else if (obj instanceof ServiceNameType) {
229: return ((ServiceNameType) obj).getEndpointName();
230: }
231: }
232: }
233: return null;
234: }
235:
236: public static void setPortName(EndpointReferenceType ref,
237: String portName) {
238: MetadataType metadata = ref.getMetadata();
239: if (metadata != null) {
240: for (Object obj : metadata.getAny()) {
241: if (obj instanceof JAXBElement) {
242: Object val = ((JAXBElement) obj).getValue();
243: if (val instanceof ServiceNameType) {
244: ((ServiceNameType) val)
245: .setEndpointName(portName);
246: }
247: } else if (obj instanceof ServiceNameType) {
248: ((ServiceNameType) obj).setEndpointName(portName);
249: }
250: }
251: }
252: }
253:
254: public static void setInterfaceName(EndpointReferenceType ref,
255: QName portTypeName) {
256: if (null != portTypeName) {
257: AttributedQNameType interfaceNameType = WSA_WSDL_OBJECT_FACTORY
258: .createAttributedQNameType();
259:
260: interfaceNameType.setValue(portTypeName);
261: interfaceNameType.getOtherAttributes().put(
262: XML_SCHEMA_NAMESPACE_NS, XML_SCHEMA_NAMESPACE);
263: interfaceNameType.getOtherAttributes().put(
264: XSI_TYPE,
265: XML_SCHEMA_NAMESPACE_PREFIX + ":"
266: + portTypeName.getClass().getSimpleName());
267:
268: JAXBElement<AttributedQNameType> jaxbElement = WSA_WSDL_OBJECT_FACTORY
269: .createInterfaceName(interfaceNameType);
270:
271: MetadataType mt = ref.getMetadata();
272: if (null == mt) {
273: mt = WSA_OBJECT_FACTORY.createMetadataType();
274: ref.setMetadata(mt);
275: }
276: mt.getAny().add(jaxbElement);
277: }
278: }
279:
280: public static QName getInterfaceName(EndpointReferenceType ref) {
281: MetadataType metadata = ref.getMetadata();
282: if (metadata != null) {
283: for (Object obj : metadata.getAny()) {
284: if (obj instanceof Element) {
285: Node node = (Element) obj;
286: System.out.println(node.getNamespaceURI() + ":"
287: + node.getNodeName());
288: if (node.getNamespaceURI().equals(
289: WSA_WSDL_NAMESPACE)
290: && node.getNodeName().contains(
291: "InterfaceName")) {
292:
293: String content = node.getTextContent();
294: String namespaceURI = node.getFirstChild()
295: .getNamespaceURI();
296: //String service = content;
297: if (content.contains(":")) {
298: namespaceURI = getNameSpaceUri(node,
299: content, namespaceURI);
300: content = getService(content);
301: } else {
302: Node nodeAttr = node.getAttributes()
303: .getNamedItem("xmlns");
304: namespaceURI = nodeAttr.getNodeValue();
305: }
306:
307: return new QName(namespaceURI, content);
308: }
309: } else if (obj instanceof JAXBElement) {
310: Object val = ((JAXBElement) obj).getValue();
311: if (val instanceof AttributedQNameType) {
312: return ((AttributedQNameType) val).getValue();
313: }
314: } else if (obj instanceof AttributedQNameType) {
315: return ((AttributedQNameType) obj).getValue();
316: }
317: }
318: }
319:
320: return null;
321: }
322:
323: public static void setWSDLLocation(EndpointReferenceType ref,
324: String... wsdlLocation) {
325:
326: MetadataType metadata = ref.getMetadata();
327: if (null == metadata) {
328: metadata = WSA_OBJECT_FACTORY.createMetadataType();
329: ref.setMetadata(metadata);
330: }
331:
332: //wsdlLocation attribute is a list of anyURI.
333: StringBuffer strBuf = new StringBuffer();
334: for (String str : wsdlLocation) {
335: strBuf.append(str);
336: strBuf.append(" ");
337: }
338:
339: metadata.getOtherAttributes().put(WSDL_LOCATION,
340: strBuf.toString().trim());
341: }
342:
343: public static String getWSDLLocation(EndpointReferenceType ref) {
344: String wsdlLocation = null;
345: MetadataType metadata = ref.getMetadata();
346:
347: if (metadata != null) {
348: wsdlLocation = metadata.getOtherAttributes().get(
349: WSDL_LOCATION);
350: }
351:
352: if (null == wsdlLocation) {
353: return null;
354: }
355:
356: //TODO The wsdlLocation inserted should be a valid URI
357: //before doing a split. So temporarily return the string
358: //return wsdlLocation.split(" ");
359: return wsdlLocation;
360: }
361:
362: /**
363: * Sets the metadata on the provided endpoint reference.
364: * @param ref the endpoint reference.
365: * @param metadata the list of metadata source.
366: */
367: public static void setMetadata(EndpointReferenceType ref,
368: List<Source> metadata) throws EndpointUtilsException {
369:
370: if (null != ref) {
371: MetadataType mt = ref.getMetadata();
372: if (null == mt) {
373: mt = new MetadataType();
374: ref.setMetadata(mt);
375: }
376: List<Object> anyList = mt.getAny();
377: try {
378: for (Source source : metadata) {
379: Node node = null;
380: boolean doTransform = true;
381: if (source instanceof StreamSource) {
382: StreamSource ss = (StreamSource) source;
383: if (null == ss.getInputStream()
384: && null == ss.getReader()) {
385: setWSDLLocation(ref, ss.getSystemId());
386: doTransform = false;
387: }
388: } else if (source instanceof DOMSource) {
389: node = ((DOMSource) node).getNode();
390: doTransform = false;
391: }
392:
393: if (doTransform) {
394: DOMResult domResult = new DOMResult();
395: domResult.setSystemId(source.getSystemId());
396:
397: getTransformer().transform(source, domResult);
398:
399: node = domResult.getNode();
400: }
401:
402: if (null != node) {
403: if (node instanceof Document) {
404: ((Document) node).setDocumentURI(source
405: .getSystemId());
406: node = node.getFirstChild();
407: }
408:
409: while (node.getNodeType() != Node.ELEMENT_NODE) {
410: node = node.getNextSibling();
411: }
412:
413: anyList.add(node);
414: }
415: }
416: } catch (TransformerException te) {
417: throw new EndpointUtilsException(new Message(
418: "COULD_NOT_POPULATE_EPR", LOG), te);
419: }
420: }
421: }
422:
423: /**
424: * Gets the WSDL definition for the provided endpoint reference.
425: * @param manager - the WSDL manager
426: * @param ref - the endpoint reference
427: * @return Definition the wsdl definition
428: * @throws WSDLException
429: */
430: public static Definition getWSDLDefinition(WSDLManager manager,
431: EndpointReferenceType ref) throws WSDLException {
432:
433: if (null == manager) {
434: return null;
435: }
436:
437: MetadataType metadata = ref.getMetadata();
438: String location = getWSDLLocation(ref);
439:
440: if (null != location) {
441: //Pick up the first url to obtain the wsdl defintion
442: return manager.getDefinition(location);
443: }
444:
445: for (Object obj : metadata.getAny()) {
446: if (obj instanceof Element) {
447: Element el = (Element) obj;
448: if (StringUtils.isEqualUri(el.getNamespaceURI(),
449: WSDLConstants.WSDL11_NAMESPACE)
450: && "definitions".equals(el.getLocalName())) {
451: return manager.getDefinition(el);
452: }
453: }
454: }
455:
456: return null;
457: }
458:
459: public static Schema getSchema(ServiceInfo serviceInfo) {
460: if (serviceInfo == null) {
461: return null;
462: }
463: synchronized (schemaMap) {
464: if (schemaMap.containsKey(serviceInfo)) {
465: return schemaMap.get(serviceInfo);
466: }
467: }
468: Schema schema = schemaMap.get(serviceInfo);
469:
470: if (schema == null) {
471: SchemaFactory factory = SchemaFactory
472: .newInstance(XMLConstants.W3C_XML_SCHEMA_NS_URI);
473: List<Source> schemaSources = new ArrayList<Source>();
474: for (SchemaInfo schemaInfo : serviceInfo.getSchemas()) {
475: Source source = new DOMSource(schemaInfo.getElement());
476: if (source != null) {
477: source.setSystemId(schemaInfo.getElement()
478: .getBaseURI());
479: schemaSources.add(source);
480: }
481: }
482: try {
483: schema = factory.newSchema(schemaSources
484: .toArray(new Source[schemaSources.size()]));
485: if (schema != null) {
486: synchronized (schemaMap) {
487: schemaMap.put(serviceInfo, schema);
488: }
489: LOG.log(Level.FINE,
490: "Obtained schema from ServiceInfo");
491: }
492: } catch (SAXException ex) {
493: // Something not right with the schema from the wsdl.
494: LOG.log(Level.WARNING, "SAXException for newSchema()",
495: ex);
496: }
497:
498: }
499: return schema;
500: }
501:
502: /**
503: * Gets the WSDL port for the provided endpoint reference.
504: * @param manager - the WSDL manager
505: * @param ref - the endpoint reference
506: * @return Port the wsdl port
507: * @throws WSDLException
508: */
509: public static Port getPort(WSDLManager manager,
510: EndpointReferenceType ref) throws WSDLException {
511:
512: Definition def = getWSDLDefinition(manager, ref);
513: if (def == null) {
514: throw new WSDLException(WSDLException.OTHER_ERROR,
515: "unable to find definition for reference");
516: }
517:
518: MetadataType metadata = ref.getMetadata();
519: for (Object obj : metadata.getAny()) {
520:
521: if (obj instanceof JAXBElement) {
522: Object jaxbVal = ((JAXBElement) obj).getValue();
523:
524: if (jaxbVal instanceof ServiceNameType) {
525: Port port = null;
526: ServiceNameType snt = (ServiceNameType) jaxbVal;
527: LOG.log(Level.FINEST, "found service name "
528: + snt.getValue().getLocalPart());
529: Service service = def.getService(snt.getValue());
530: if (service == null) {
531: LOG
532: .log(
533: Level.WARNING,
534: "can't find the service name ["
535: + snt.getValue()
536: + "], using the default service name in wsdl");
537: service = (Service) def.getServices().values()
538: .iterator().next();
539: if (service == null) {
540: return null;
541: }
542: }
543: String endpoint = snt.getEndpointName();
544: if ("".equals(endpoint)
545: && service.getPorts().size() == 1) {
546: port = (Port) service.getPorts().values()
547: .iterator().next();
548: } else {
549: port = service.getPort(endpoint);
550: }
551: // FIXME this needs to be looked at service.getPort(endpoint)
552: //should not return null when endpoint is valid
553: if (port == null) {
554: LOG
555: .log(
556: Level.WARNING,
557: "can't find the port name ["
558: + endpoint
559: + "], using the default port name in wsdl");
560: port = (Port) service.getPorts().values()
561: .iterator().next();
562: }
563: return port;
564: }
565: }
566: }
567:
568: if (def.getServices().size() == 1) {
569: Service service = (Service) def.getServices().values()
570: .iterator().next();
571: if (service.getPorts().size() == 1) {
572: return (Port) service.getPorts().values().iterator()
573: .next();
574: }
575: }
576:
577: QName serviceName = getServiceName(ref);
578: if (null != serviceName) {
579: Service service = def.getService(serviceName);
580: if (service == null) {
581: throw new WSDLException(WSDLException.OTHER_ERROR,
582: "Cannot find service for " + serviceName);
583: }
584: if (service.getPorts().size() == 1) {
585: return (Port) service.getPorts().values().iterator()
586: .next();
587: }
588: String str = getPortName(ref);
589: LOG.log(Level.FINE, "getting port " + str
590: + " from service " + service.getQName());
591: Port port = service.getPort(str);
592: if (port == null) {
593: throw new WSDLException(WSDLException.OTHER_ERROR,
594: "unable to find port " + str);
595: }
596: return port;
597: }
598: // TODO : throw exception here
599: return null;
600: }
601:
602: /**
603: * Get the address from the provided endpoint reference.
604: * @param ref - the endpoint reference
605: * @return String the address of the endpoint
606: */
607: public static String getAddress(EndpointReferenceType ref) {
608: AttributedURIType a = ref.getAddress();
609: if (null != a) {
610: return a.getValue();
611: }
612: // should wsdl be parsed for an address now?
613: return null;
614: }
615:
616: /**
617: * Set the address of the provided endpoint reference.
618: * @param ref - the endpoint reference
619: * @param address - the address
620: */
621: public static void setAddress(EndpointReferenceType ref,
622: String address) {
623: AttributedURIType a = WSA_OBJECT_FACTORY
624: .createAttributedURIType();
625: a.setValue(address);
626: ref.setAddress(a);
627: }
628:
629: /**
630: * Create an endpoint reference for the provided wsdl, service and portname.
631: * @param wsdlUrl - url of the wsdl that describes the service.
632: * @param serviceName - the <code>QName</code> of the service.
633: * @param portName - the name of the port.
634: * @return EndpointReferenceType - the endpoint reference
635: */
636: public static EndpointReferenceType getEndpointReference(
637: URL wsdlUrl, QName serviceName, String portName) {
638: EndpointReferenceType reference = WSA_OBJECT_FACTORY
639: .createEndpointReferenceType();
640: reference.setMetadata(WSA_OBJECT_FACTORY.createMetadataType());
641: setServiceAndPortName(reference, serviceName, portName);
642: //TODO To Ensure it is a valid URI syntax.
643: setWSDLLocation(reference, wsdlUrl.toString());
644:
645: return reference;
646: }
647:
648: /**
649: * Create a duplicate endpoint reference sharing all atributes
650: * @param ref the reference to duplicate
651: * @return EndpointReferenceType - the duplicate endpoint reference
652: */
653: public static EndpointReferenceType duplicate(
654: EndpointReferenceType ref) {
655:
656: EndpointReferenceType reference = WSA_OBJECT_FACTORY
657: .createEndpointReferenceType();
658: reference.setMetadata(ref.getMetadata());
659: reference.getAny().addAll(ref.getAny());
660: reference.setAddress(ref.getAddress());
661: return reference;
662: }
663:
664: /**
665: * Create an endpoint reference for the provided address.
666: * @param address - address URI
667: * @return EndpointReferenceType - the endpoint reference
668: */
669: public static EndpointReferenceType getEndpointReference(
670: String address) {
671:
672: EndpointReferenceType reference = WSA_OBJECT_FACTORY
673: .createEndpointReferenceType();
674: setAddress(reference, address);
675: return reference;
676: }
677:
678: public static EndpointReferenceType getEndpointReference(
679: AttributedURIType address) {
680:
681: EndpointReferenceType reference = WSA_OBJECT_FACTORY
682: .createEndpointReferenceType();
683: reference.setAddress(address);
684: return reference;
685: }
686:
687: /**
688: * Create an anonymous endpoint reference.
689: * @return EndpointReferenceType - the endpoint reference
690: */
691: public static EndpointReferenceType getAnonymousEndpointReference() {
692:
693: EndpointReferenceType reference = WSA_OBJECT_FACTORY
694: .createEndpointReferenceType();
695: setAddress(reference, ANONYMOUS_ADDRESS);
696: return reference;
697: }
698:
699: /**
700: * Resolve logical endpoint reference via the Bus EndpointResolverRegistry.
701: *
702: * @param logical the abstract EPR to resolve
703: * @return the resolved concrete EPR if appropriate, null otherwise
704: */
705: public static EndpointReferenceType resolve(
706: EndpointReferenceType logical, Bus bus) {
707: EndpointReferenceType physical = null;
708: if (bus != null) {
709: EndpointResolverRegistry registry = bus
710: .getExtension(EndpointResolverRegistry.class);
711: if (registry != null) {
712: physical = registry.resolve(logical);
713: }
714: }
715: return physical != null ? physical : logical;
716: }
717:
718: /**
719: * Renew logical endpoint reference via the Bus EndpointResolverRegistry.
720: *
721: * @param logical the original abstract EPR (if still available)
722: * @param physical the concrete EPR to renew
723: * @return the renewed concrete EPR if appropriate, null otherwise
724: */
725: public static EndpointReferenceType renew(
726: EndpointReferenceType logical,
727: EndpointReferenceType physical, Bus bus) {
728: EndpointReferenceType renewed = null;
729: if (bus != null) {
730: EndpointResolverRegistry registry = bus
731: .getExtension(EndpointResolverRegistry.class);
732: if (registry != null) {
733: renewed = registry.renew(logical, physical);
734: }
735: }
736: return renewed != null ? renewed : physical;
737: }
738:
739: /**
740: * Mint logical endpoint reference via the Bus EndpointResolverRegistry.
741: *
742: * @param serviceName the given serviceName
743: * @return the newly minted EPR if appropriate, null otherwise
744: */
745: public static EndpointReferenceType mint(QName serviceName, Bus bus) {
746: EndpointReferenceType logical = null;
747: if (bus != null) {
748: EndpointResolverRegistry registry = bus
749: .getExtension(EndpointResolverRegistry.class);
750: if (registry != null) {
751: logical = registry.mint(serviceName);
752: }
753: }
754: return logical;
755: }
756:
757: /**
758: * Mint logical endpoint reference via the Bus EndpointResolverRegistry.
759: *
760: * @param physical the concrete template EPR
761: * @return the newly minted EPR if appropriate, null otherwise
762: */
763: public static EndpointReferenceType mint(
764: EndpointReferenceType physical, Bus bus) {
765: EndpointReferenceType logical = null;
766: if (bus != null) {
767: EndpointResolverRegistry registry = bus
768: .getExtension(EndpointResolverRegistry.class);
769: if (registry != null) {
770: logical = registry.mint(physical);
771: }
772: }
773: return logical != null ? logical : physical;
774: }
775:
776: private static String getNameSpaceUri(Node node, String content,
777: String namespaceURI) {
778: if (namespaceURI == null) {
779: namespaceURI = node.lookupNamespaceURI(content.substring(0,
780: content.indexOf(":")));
781: }
782: return namespaceURI;
783: }
784:
785: private static String getService(String content) {
786: return content.substring(content.indexOf(":") + 1, content
787: .length());
788: }
789:
790: /**
791: * Obtain a multiplexed endpoint reference for the deployed service that contains the provided id
792: * @param serviceQName identified the target service
793: * @param portName identifies a particular port of the service, may be null
794: * @param id that must be embedded in the returned reference
795: * @param bus the current bus
796: * @return a new reference or null if the target destination does not support destination mutiplexing
797: */
798: public static EndpointReferenceType getEndpointReferenceWithId(
799: QName serviceQName, String portName, String id, Bus bus) {
800: EndpointReferenceType epr = null;
801: MultiplexDestination destination = getMatchingMultiplexDestination(
802: serviceQName, portName, bus);
803: if (null != destination) {
804: epr = destination.getAddressWithId(id);
805: }
806: return epr;
807: }
808:
809: /**
810: * Obtain the id String from the endpoint reference of the current dispatch.
811: * @param messageContext the current message context
812: * @return the id embedded in the current endpoint reference or null if not found
813: */
814: public static String getEndpointReferenceId(Map messageContext) {
815: String id = null;
816: Destination destination = (Destination) messageContext
817: .get(Destination.class.getName());
818: if (destination instanceof MultiplexDestination) {
819: id = ((MultiplexDestination) destination)
820: .getId(messageContext);
821: }
822: return id;
823: }
824:
825: private static MultiplexDestination getMatchingMultiplexDestination(
826: QName serviceQName, String portName, Bus bus) {
827: MultiplexDestination destination = null;
828: ServerRegistry serverRegistry = (ServerRegistry) bus
829: .getExtension(ServerRegistry.class);
830: if (null != serverRegistry) {
831: List<Server> servers = serverRegistry.getServers();
832: for (Server s : servers) {
833: QName targetServiceQName = s.getEndpoint()
834: .getEndpointInfo().getService().getName();
835: if (serviceQName.equals(targetServiceQName)
836: && portNameMatches(s, portName)) {
837: Destination dest = s.getDestination();
838: if (dest instanceof MultiplexDestination) {
839: destination = (MultiplexDestination) dest;
840: break;
841: }
842: }
843: }
844: } else {
845: LOG
846: .log(
847: Level.WARNING,
848: "Failed to locate service matching "
849: + serviceQName
850: + ", because the bus ServerRegistry extension provider is null");
851: }
852: return destination;
853: }
854:
855: private static boolean portNameMatches(Server s, String portName) {
856: boolean ret = false;
857: if (null == portName
858: || portName.equals(s.getEndpoint().getEndpointInfo()
859: .getName().getLocalPart())) {
860: return true;
861: }
862: return ret;
863: }
864: }
|