KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > objectweb > celtix > wsdl > EndpointReferenceUtils


1 package org.objectweb.celtix.wsdl;
2
3 import java.net.URL JavaDoc;
4 import java.util.ArrayList JavaDoc;
5 import java.util.List JavaDoc;
6 import java.util.Map JavaDoc;
7 import java.util.WeakHashMap JavaDoc;
8 import java.util.logging.Level JavaDoc;
9 import java.util.logging.Logger JavaDoc;
10
11 import javax.jws.WebService;
12 import javax.wsdl.Definition;
13 import javax.wsdl.Import;
14 import javax.wsdl.Port;
15 import javax.wsdl.Service;
16 import javax.wsdl.Types;
17 import javax.wsdl.WSDLException;
18 import javax.xml.XMLConstants JavaDoc;
19 import javax.xml.bind.JAXBElement;
20 import javax.xml.namespace.QName JavaDoc;
21 import javax.xml.transform.Source JavaDoc;
22 import javax.xml.transform.Transformer JavaDoc;
23 import javax.xml.transform.TransformerConfigurationException JavaDoc;
24 import javax.xml.transform.TransformerException JavaDoc;
25 import javax.xml.transform.TransformerFactory JavaDoc;
26 import javax.xml.transform.dom.DOMResult JavaDoc;
27 import javax.xml.transform.dom.DOMSource JavaDoc;
28 import javax.xml.transform.stream.StreamSource JavaDoc;
29 import javax.xml.validation.Schema JavaDoc;
30 import javax.xml.validation.SchemaFactory JavaDoc;
31 import javax.xml.ws.WebServiceException;
32 import javax.xml.ws.WebServiceProvider;
33
34 import org.w3c.dom.Document JavaDoc;
35 import org.w3c.dom.Element JavaDoc;
36 import org.w3c.dom.Node JavaDoc;
37 import org.xml.sax.SAXException JavaDoc;
38
39 import org.objectweb.celtix.common.logging.LogUtils;
40 import org.objectweb.celtix.jaxb.JAXBUtils;
41 import org.objectweb.celtix.ws.addressing.AttributedURIType;
42 import org.objectweb.celtix.ws.addressing.EndpointReferenceType;
43 import org.objectweb.celtix.ws.addressing.MetadataType;
44 import org.objectweb.celtix.ws.addressing.ObjectFactory;
45 import org.objectweb.celtix.ws.addressing.wsdl.AttributedQNameType;
46 import org.objectweb.celtix.ws.addressing.wsdl.ServiceNameType;
47
48 /**
49  * Provides utility methods for obtaining endpoint references, wsdl definitions, etc.
50  */

51 public final class EndpointReferenceUtils {
52
53     static WeakHashMap JavaDoc<Definition, Schema JavaDoc> schemaMap = new WeakHashMap JavaDoc<Definition, Schema JavaDoc>();
54
55     private static final Logger JavaDoc LOG = LogUtils.getL7dLogger(EndpointReferenceUtils.class);
56
57     private static final QName JavaDoc WSDL_LOCATION = new QName JavaDoc("http://www.w3.org/2006/01/wsdl-instance",
58                                                          "wsdlLocation");
59     private static final Transformer JavaDoc XML_TRANSFORMER;
60     static {
61         Transformer JavaDoc transformer = null;
62         try {
63             TransformerFactory JavaDoc tf = TransformerFactory.newInstance();
64             transformer = tf.newTransformer();
65         } catch (TransformerConfigurationException JavaDoc tce) {
66             throw new WebServiceException("Could not create transformer", tce);
67         }
68         XML_TRANSFORMER = transformer;
69     }
70     
71     private EndpointReferenceUtils() {
72         // Utility class - never constructed
73
}
74     
75     /**
76      * Sets the service and port name of the provided endpoint reference.
77      * @param ref the endpoint reference.
78      * @param serviceName the name of service.
79      * @param portName the port name.
80      */

81     public static void setServiceAndPortName(EndpointReferenceType ref,
82                                              QName JavaDoc serviceName,
83                                              String JavaDoc portName)
84         throws WebServiceException {
85         if (null != serviceName) {
86             ServiceNameType serviceNameType = new ServiceNameType();
87             serviceNameType.setValue(serviceName);
88             serviceNameType.setEndpointName(portName);
89             org.objectweb.celtix.ws.addressing.wsdl.ObjectFactory objectFactory =
90                 new org.objectweb.celtix.ws.addressing.wsdl.ObjectFactory();
91             JAXBElement<ServiceNameType> jaxbElement = objectFactory.createServiceName(serviceNameType);
92
93             MetadataType mt = ref.getMetadata();
94             if (null == mt) {
95                 mt = new MetadataType();
96                 ref.setMetadata(mt);
97             }
98
99             mt.getAny().add(jaxbElement);
100         }
101     }
102     
103     /**
104      * Gets the service name of the provided endpoint reference.
105      * @param ref the endpoint reference.
106      * @return the service name.
107      */

108     public static QName JavaDoc getServiceName(EndpointReferenceType ref) {
109         MetadataType metadata = ref.getMetadata();
110         if (metadata != null) {
111             for (Object JavaDoc obj : metadata.getAny()) {
112                 if (obj instanceof Element) {
113                     Node JavaDoc node = (Element)obj;
114                     if (node.getNamespaceURI().equals("http://www.w3.org/2005/08/addressing/wsdl")
115                         && node.getLocalName().equals("ServiceName")) {
116                         String JavaDoc content = node.getTextContent();
117                         String JavaDoc namespaceURI = node.getFirstChild().getNamespaceURI();
118                         String JavaDoc service = content;
119                         if (content.contains(":")) {
120                             namespaceURI = getNameSpaceUri(node, content, namespaceURI);
121                             service = getService(content);
122                         } else {
123                             Node JavaDoc nodeAttr = node.getAttributes().getNamedItem("xmlns");
124                             namespaceURI = nodeAttr.getNodeValue();
125                         }
126                         
127                         return new QName JavaDoc(namespaceURI, service);
128                     }
129                 } else if (obj instanceof JAXBElement) {
130                     Object JavaDoc val = ((JAXBElement)obj).getValue();
131                     if (val instanceof ServiceNameType) {
132                         return ((ServiceNameType)val).getValue();
133                     }
134                 } else if (obj instanceof ServiceNameType) {
135                     return ((ServiceNameType)obj).getValue();
136                 }
137             }
138         }
139         return null;
140     }
141     
142     /**
143      * Gets the port name of the provided endpoint reference.
144      * @param ref the endpoint reference.
145      * @return the port name.
146      */

147     public static String JavaDoc getPortName(EndpointReferenceType ref) {
148         MetadataType metadata = ref.getMetadata();
149         if (metadata != null) {
150             for (Object JavaDoc obj : metadata.getAny()) {
151                 if (obj instanceof Element) {
152                     Node JavaDoc node = (Element)obj;
153                     if (node.getNamespaceURI().equals("http://www.w3.org/2005/08/addressing/wsdl")
154                         && node.getNodeName().contains("ServiceName")) {
155                         return node.getAttributes().getNamedItem("EndpointName").getTextContent();
156                     }
157                 } else if (obj instanceof JAXBElement) {
158                     Object JavaDoc val = ((JAXBElement)obj).getValue();
159                     if (val instanceof ServiceNameType) {
160                         return ((ServiceNameType)val).getEndpointName();
161                     }
162                 } else if (obj instanceof ServiceNameType) {
163                     return ((ServiceNameType)obj).getEndpointName();
164                 }
165             }
166         }
167         return null;
168     }
169     
170     public static void setInterfaceName(EndpointReferenceType ref, QName JavaDoc portTypeName) {
171         if (null != portTypeName) {
172             AttributedQNameType interfaceNameType = new AttributedQNameType();
173             
174             interfaceNameType.setValue(portTypeName);
175             
176             org.objectweb.celtix.ws.addressing.wsdl.ObjectFactory objectFactory =
177                 new org.objectweb.celtix.ws.addressing.wsdl.ObjectFactory();
178             JAXBElement<AttributedQNameType> jaxbElement =
179                 objectFactory.createInterfaceName(interfaceNameType);
180
181             MetadataType mt = ref.getMetadata();
182             if (null == mt) {
183                 mt = new MetadataType();
184                 ref.setMetadata(mt);
185             }
186             mt.getAny().add(jaxbElement);
187         }
188     }
189   
190     public static QName JavaDoc getInterfaceName(EndpointReferenceType ref) {
191         MetadataType metadata = ref.getMetadata();
192         if (metadata != null) {
193             for (Object JavaDoc obj : metadata.getAny()) {
194                 if (obj instanceof Element) {
195                     Node JavaDoc node = (Element)obj;
196                     System.out.println(node.getNamespaceURI() + ":" + node.getNodeName());
197                     if (node.getNamespaceURI().equals("http://www.w3.org/2005/08/addressing/wsdl")
198                         && node.getNodeName().contains("InterfaceName")) {
199                         
200                         String JavaDoc content = node.getTextContent();
201                         String JavaDoc namespaceURI = node.getFirstChild().getNamespaceURI();
202                         //String service = content;
203
if (content.contains(":")) {
204                             namespaceURI = getNameSpaceUri(node, content, namespaceURI);
205                             content = getService(content);
206                         } else {
207                             Node JavaDoc nodeAttr = node.getAttributes().getNamedItem("xmlns");
208                             namespaceURI = nodeAttr.getNodeValue();
209                         }
210
211                         return new QName JavaDoc(namespaceURI, content);
212                     }
213                 } else if (obj instanceof JAXBElement) {
214                     Object JavaDoc val = ((JAXBElement)obj).getValue();
215                     if (val instanceof AttributedQNameType) {
216                         return ((AttributedQNameType)val).getValue();
217                     }
218                 } else if (obj instanceof AttributedQNameType) {
219                     return ((AttributedQNameType)obj).getValue();
220                 }
221             }
222         }
223
224         return null;
225     }
226     
227     private static void setWSDLLocation(EndpointReferenceType ref, String JavaDoc... wsdlLocation) {
228         
229         MetadataType metadata = ref.getMetadata();
230         if (null == metadata) {
231             metadata = new MetadataType();
232             ref.setMetadata(metadata);
233         }
234
235         //wsdlLocation attribute is a list of anyURI.
236
StringBuffer JavaDoc strBuf = new StringBuffer JavaDoc();
237         for (String JavaDoc str : wsdlLocation) {
238             strBuf.append(str);
239             strBuf.append(" ");
240         }
241
242         metadata.getOtherAttributes().put(WSDL_LOCATION, strBuf.toString().trim());
243     }
244     
245     public static String JavaDoc getWSDLLocation(EndpointReferenceType ref) {
246         String JavaDoc wsdlLocation = null;
247         MetadataType metadata = ref.getMetadata();
248
249         if (metadata != null) {
250             wsdlLocation = metadata.getOtherAttributes().get(WSDL_LOCATION);
251         }
252
253         if (null == wsdlLocation) {
254             return null;
255         }
256
257         //TODO The wsdlLocation inserted should be a valid URI
258
//before doing a split. So temporarily return the string
259
//return wsdlLocation.split(" ");
260
return wsdlLocation;
261     }
262
263     /**
264      * Sets the metadata on the provided endpoint reference.
265      * @param ref the endpoint reference.
266      * @param the list of metadata source.
267      */

268     public static void setMetadata(EndpointReferenceType ref, List JavaDoc<Source JavaDoc> metadata) {
269         if (null != ref) {
270             MetadataType mt = ref.getMetadata();
271             if (null == mt) {
272                 mt = new MetadataType();
273                 ref.setMetadata(mt);
274             }
275             List JavaDoc<Object JavaDoc> anyList = mt.getAny();
276             try {
277                 for (Source JavaDoc source : metadata) {
278                     Node JavaDoc node = null;
279                     boolean doTransform = true;
280                     if (source instanceof StreamSource JavaDoc) {
281                         StreamSource JavaDoc ss = (StreamSource JavaDoc)source;
282                         if (null == ss.getInputStream()
283                             && null == ss.getReader()) {
284                             setWSDLLocation(ref, ss.getSystemId());
285                             doTransform = false;
286                         }
287                     } else if (source instanceof DOMSource JavaDoc) {
288                         node = ((DOMSource JavaDoc)node).getNode();
289                         doTransform = false;
290                     }
291                     
292                     if (doTransform) {
293                         DOMResult JavaDoc domResult = new DOMResult JavaDoc();
294                         domResult.setSystemId(source.getSystemId());
295                         
296                         XML_TRANSFORMER.transform(source, domResult);
297     
298                         node = domResult.getNode();
299                     }
300                     
301                     if (null != node) {
302                         if (node instanceof Document JavaDoc) {
303                             ((Document JavaDoc)node).setDocumentURI(source.getSystemId());
304                             node = node.getFirstChild();
305                         }
306                         
307                         while (node.getNodeType() != Node.ELEMENT_NODE) {
308                             node = node.getNextSibling();
309                         }
310                         
311                         anyList.add(node);
312                     }
313                 }
314             } catch (TransformerException JavaDoc te) {
315                 throw new WebServiceException("Populating metadata in EPR failed", te);
316             }
317         }
318     }
319    
320     /**
321      * Gets the WSDL definition for the provided endpoint reference.
322      * @param manager - the WSDL manager
323      * @param ref - the endpoint reference
324      * @return Definition the wsdl definition
325      * @throws WSDLException
326      */

327     public static Definition getWSDLDefinition(WSDLManager manager, EndpointReferenceType ref)
328         throws WSDLException {
329
330         if (null == manager) {
331             return null;
332         }
333
334         MetadataType metadata = ref.getMetadata();
335         String JavaDoc location = getWSDLLocation(ref);
336
337         if (null != location) {
338             //Pick up the first url to obtain the wsdl defintion
339
return manager.getDefinition(location);
340         }
341
342         for (Object JavaDoc obj : metadata.getAny()) {
343             if (obj instanceof Element) {
344                 Element el = (Element)obj;
345                 if ("http://schemas.xmlsoap.org/wsdl/".equals(el.getNamespaceURI())
346                     && "definitions".equals(el.getLocalName())) {
347                     return manager.getDefinition(el);
348                 }
349             }
350         }
351
352         QName JavaDoc portTypeName = getInterfaceName(ref);
353         if (null != portTypeName) {
354             
355             StringBuffer JavaDoc seiName = new StringBuffer JavaDoc();
356             seiName.append(JAXBUtils.namespaceURIToPackage(portTypeName.getNamespaceURI()));
357             seiName.append(".");
358             seiName.append(JAXBUtils.nameToIdentifier(portTypeName.getLocalPart(),
359                                                       JAXBUtils.IdentifierType.INTERFACE));
360             
361             Class JavaDoc<?> sei = null;
362             try {
363                 sei = Class.forName(seiName.toString(), true,
364                                     manager.getClass().getClassLoader());
365             } catch (ClassNotFoundException JavaDoc ex) {
366                 LOG.log(Level.INFO, "SEI_LOAD_FAILURE_MSG", ex);
367                 return null;
368             }
369             Definition def = manager.getDefinition(sei);
370             if (def == null && sei.getInterfaces().length > 0) {
371                 sei = sei.getInterfaces()[0];
372                 def = manager.getDefinition(sei);
373             }
374             return def;
375         }
376         return null;
377     }
378
379     private static List JavaDoc<javax.wsdl.extensions.schema.Schema> getSchemas(Definition definition) {
380         Types types = definition.getTypes();
381         List JavaDoc<javax.wsdl.extensions.schema.Schema> schemaList =
382             new ArrayList JavaDoc<javax.wsdl.extensions.schema.Schema>();
383         if (types != null) {
384             for (Object JavaDoc o : types.getExtensibilityElements()) {
385                 if (o instanceof javax.wsdl.extensions.schema.Schema) {
386                     javax.wsdl.extensions.schema.Schema s =
387                         (javax.wsdl.extensions.schema.Schema)o;
388                     schemaList.add(s);
389                 }
390             }
391         }
392
393         Map JavaDoc wsdlImports = definition.getImports();
394         for (Object JavaDoc o : wsdlImports.values()) {
395             if (o instanceof List JavaDoc) {
396                 for (Object JavaDoc p : (List JavaDoc)o) {
397                     if (p instanceof Import) {
398                         schemaList.addAll(getSchemas(((Import)p).getDefinition()));
399                     }
400                 }
401             }
402         }
403         return schemaList;
404     }
405
406     public static Schema JavaDoc getSchema(WSDLManager manager, EndpointReferenceType ref) {
407         Definition definition;
408         try {
409             definition = getWSDLDefinition(manager, ref);
410         } catch (javax.wsdl.WSDLException wsdlEx) {
411             return null;
412         }
413         if (definition == null) {
414             return null;
415         }
416         synchronized (schemaMap) {
417             if (schemaMap.containsKey(definition)) {
418                 return schemaMap.get(definition);
419             }
420         }
421         Schema JavaDoc schema = schemaMap.get(definition);
422         if (schema == null) {
423             List JavaDoc<javax.wsdl.extensions.schema.Schema> schemas = getSchemas(definition);
424             SchemaFactory JavaDoc factory = SchemaFactory.newInstance(
425                 XMLConstants.W3C_XML_SCHEMA_NS_URI);
426             List JavaDoc<Source JavaDoc> schemaSources = new ArrayList JavaDoc<Source JavaDoc>();
427             for (javax.wsdl.extensions.schema.Schema s : schemas) {
428                 Source JavaDoc source = new DOMSource JavaDoc(s.getElement());
429                 if (source != null) {
430                     schemaSources.add(source);
431                 }
432             }
433             try {
434                 schema = factory.newSchema(schemaSources.toArray(
435                     new Source JavaDoc[schemaSources.size()]));
436                 if (schema != null) {
437                     synchronized (schemaMap) {
438                         schemaMap.put(definition, schema);
439                     }
440                     LOG.log(Level.FINE, "Obtained schema from wsdl definition");
441                 }
442             } catch (SAXException JavaDoc ex) {
443                 // Something not right with the schema from the wsdl.
444
LOG.log(Level.WARNING, "SAXException for newSchema()", ex);
445             }
446         }
447         return schema;
448     }
449     
450     /**
451      * Gets the WSDL port for the provided endpoint reference.
452      * @param manager - the WSDL manager
453      * @param ref - the endpoint reference
454      * @return Port the wsdl port
455      * @throws WSDLException
456      */

457     public static Port getPort(WSDLManager manager, EndpointReferenceType ref) throws WSDLException {
458
459         Definition def = getWSDLDefinition(manager, ref);
460         if (def == null) {
461             throw new WSDLException(WSDLException.OTHER_ERROR, "unable to find definition for reference");
462         }
463
464         MetadataType metadata = ref.getMetadata();
465         for (Object JavaDoc obj : metadata.getAny()) {
466             
467             if (obj instanceof JAXBElement) {
468                 Object JavaDoc jaxbVal = ((JAXBElement)obj).getValue();
469
470                 if (jaxbVal instanceof ServiceNameType) {
471                     Port port = null;
472                     ServiceNameType snt = (ServiceNameType)jaxbVal;
473                     LOG.log(Level.FINEST, "found service name ", snt.getEndpointName());
474                     Service service = def.getService(snt.getValue());
475                     if (service == null) {
476                         service = (Service)def.getServices().values().iterator().next();
477                         if (service == null) {
478                             return null;
479                         }
480                     }
481                     String JavaDoc endpoint = snt.getEndpointName();
482                     if ("".equals(endpoint) && service.getPorts().size() == 1) {
483                         port = (Port)service.getPorts().values().iterator().next();
484                     } else {
485                         port = service.getPort(endpoint);
486                     }
487                     // FIXME this needs to be looked at service.getPort(endpoint)
488
//should not return null when endpoint is valid
489
if (port == null) {
490                         port = (Port)service.getPorts().values().iterator().next();
491                     }
492                     return port;
493                 }
494             }
495         }
496
497         if (def.getServices().size() == 1) {
498             Service service = (Service)def.getServices().values().iterator().next();
499             if (service.getPorts().size() == 1) {
500                 return (Port)service.getPorts().values().iterator().next();
501             }
502         }
503         
504         QName JavaDoc serviceName = getServiceName(ref);
505         if (null != serviceName) {
506             Service service = def.getService(serviceName);
507             if (service == null) {
508                 throw new WSDLException(WSDLException.OTHER_ERROR, "Cannot find service for " + serviceName);
509             }
510             if (service.getPorts().size() == 1) {
511                 return (Port)service.getPorts().values().iterator().next();
512             }
513             String JavaDoc str = getPortName(ref);
514             LOG.log(Level.FINE, "getting port " + str + " from service " + service.getQName());
515             Port port = service.getPort(str);
516             if (port == null) {
517                 throw new WSDLException(WSDLException.OTHER_ERROR, "unable to find port " + str);
518             }
519             return port;
520         }
521         // TODO : throw exception here
522
return null;
523     }
524
525     /**
526      * Get the address from the provided endpoint reference.
527      * @param ref - the endpoint reference
528      * @return String the address of the endpoint
529      */

530     public static String JavaDoc getAddress(EndpointReferenceType ref) {
531         AttributedURIType a = ref.getAddress();
532         if (null != a) {
533             return a.getValue();
534         }
535         // should wsdl be parsed for an address now?
536
return null;
537     }
538
539     /**
540      * Set the address of the provided endpoint reference.
541      * @param ref - the endpoint reference
542      * @param address - the address
543      */

544     public static void setAddress(EndpointReferenceType ref, String JavaDoc address) {
545         AttributedURIType a = new ObjectFactory().createAttributedURIType();
546         a.setValue(address);
547         ref.setAddress(a);
548     }
549     /**
550      * Create an endpoint reference for the provided wsdl, service and portname.
551      * @param wsdlUrl - url of the wsdl that describes the service.
552      * @param serviceName - the <code>QName</code> of the service.
553      * @param portName - the name of the port.
554      * @return EndpointReferenceType - the endpoint reference
555      */

556     public static EndpointReferenceType getEndpointReference(URL JavaDoc wsdlUrl,
557                                                              QName JavaDoc serviceName,
558                                                              String JavaDoc portName) {
559         EndpointReferenceType reference = new EndpointReferenceType();
560         reference.setMetadata(new MetadataType());
561         setServiceAndPortName(reference, serviceName, portName);
562         //TODO To Ensure it is a valid URI syntax.
563
setWSDLLocation(reference, wsdlUrl.toString());
564
565         return reference;
566     }
567     
568     /**
569      * Create an endpoint reference for the provided .
570      * @param address - address URI
571      * @return EndpointReferenceType - the endpoint reference
572      */

573     public static EndpointReferenceType getEndpointReference(String JavaDoc address) {
574
575         EndpointReferenceType reference = new EndpointReferenceType();
576         setAddress(reference, address);
577         return reference;
578     }
579
580     /**
581      * Get the WebService for the provided class. If the class
582      * itself does not have a WebService annotation, this method
583      * is called recursively on the class's interfaces and superclass.
584      * @param cls - the Class .
585      * @return WebService - the web service
586      */

587     public static WebService getWebServiceAnnotation(Class JavaDoc<?> cls) {
588         if (cls == null) {
589             return null;
590         }
591         WebService ws = cls.getAnnotation(WebService.class);
592         if (null != ws) {
593             return ws;
594         }
595         for (Class JavaDoc<?> inf : cls.getInterfaces()) {
596             ws = getWebServiceAnnotation(inf);
597             if (null != ws) {
598                 return ws;
599             }
600         }
601
602         return getWebServiceAnnotation(cls.getSuperclass());
603     }
604
605     /**
606      * Gets an endpoint reference for the provided implementor object.
607      * @param manager - the wsdl manager.
608      * @param implementor - the service implementor.
609      * @return EndpointReferenceType - the endpoint reference
610      * @throws WSDLException
611      */

612     public static EndpointReferenceType getEndpointReference(WSDLManager manager,
613                                                                  Object JavaDoc implementor) {
614         return getEndpointReference(manager, implementor.getClass());
615     }
616
617     /**
618      * Gets an endpoint reference for the provided implementor object.
619      * @param manager - the wsdl manager.
620      * @param implementor - the service implementor.
621      * @return EndpointReferenceType - the endpoint reference
622      * @throws WSDLException
623      */

624     public static EndpointReferenceType getEndpointReference(WSDLManager manager,
625                                                                  Class JavaDoc<?> implementorClass) {
626
627         WebService ws = getWebServiceAnnotation(implementorClass);
628
629         WebServiceProvider wsp = null;
630         if (null == ws) {
631             wsp = implementorClass.getAnnotation(WebServiceProvider.class);
632             if (null == wsp) {
633                 return null;
634             }
635         }
636
637         EndpointReferenceType reference = new EndpointReferenceType();
638         reference.setMetadata(new MetadataType());
639         String JavaDoc serviceName = (null != ws) ? ws.serviceName() : wsp.serviceName();
640         String JavaDoc targetNamespace = (null != ws) ? ws.targetNamespace() : wsp.targetNamespace();
641         String JavaDoc portName = (null != ws) ? ws.portName() : wsp.portName();
642         String JavaDoc url = (null != ws) ? ws.wsdlLocation() : wsp.wsdlLocation();
643         String JavaDoc className = (null != ws) ? ws.endpointInterface() : null;
644      
645         QName JavaDoc portTypeName = null;
646         if (null != className && !"".equals(className)) {
647             Class JavaDoc<?> seiClazz = null;
648             try {
649                 seiClazz = Class.forName(className);
650             } catch (ClassNotFoundException JavaDoc cnfe) {
651                 LOG.log(Level.SEVERE, "SEI_LOAD_FAILURE_MSG", cnfe);
652                 throw new WebServiceException("endpointInterface element in WebService annotation invalid",
653                                               cnfe);
654             }
655             
656             if (!seiClazz.isInterface()) {
657                 throw new WebServiceException("endpointInterface element does not refer to a java interface");
658             }
659             
660             WebService seiws = seiClazz.getAnnotation(WebService.class);
661             if (null == seiws) {
662                 throw new WebServiceException("SEI should have a WebService Annotation");
663             }
664
665             if ("".equals(url)) {
666                 url = seiws.wsdlLocation();
667             }
668
669             //WebService.name maps to wsdl:portType name.
670
portTypeName = new QName JavaDoc(ws.targetNamespace(), seiws.name());
671
672             //ServiceName,portName,endpointInterface not allowed on the WebService annotation
673
// of a SEI, Section 3.2 JSR181.
674
// set interfaceName using WebService.targetNamespace of SEI only.
675
} else {
676
677             if (null != ws) {
678                 className = ws.name();
679             }
680             if (null == className || "".equals(className)) {
681                 className = implementorClass.getSimpleName();
682             }
683             portTypeName = new QName JavaDoc(targetNamespace, className);
684         }
685
686         setInterfaceName(reference, portTypeName);
687         // set serviceName, portName and targetNamespace
688
if (!"".equals(serviceName)) {
689             setServiceAndPortName(reference, new QName JavaDoc(targetNamespace, serviceName),
690                                   portName);
691         }
692
693         if (null != url && url.length() > 0) {
694             //REVISIT Resolve the url for all cases
695
URL JavaDoc wsdlUrl = implementorClass.getResource(url);
696             if (wsdlUrl != null) {
697                 url = wsdlUrl.toExternalForm();
698             }
699         }
700         // set wsdlLocation
701
if (!"".equals(url)) {
702             setWSDLLocation(reference, url);
703         }
704
705         if (LOG.isLoggable(Level.FINE)) {
706             LOG.fine("created endpoint reference with");
707             LOG.fine(" service name: " + getServiceName(reference));
708             LOG.fine(" wsdl location: " + getWSDLLocation(reference));
709             LOG.fine(" sei class: " + getInterfaceName(reference));
710         }
711         return reference;
712     }
713     
714     private static String JavaDoc getNameSpaceUri(Node JavaDoc node, String JavaDoc content, String JavaDoc namespaceURI) {
715         if (namespaceURI == null) {
716             namespaceURI = node.lookupNamespaceURI(content.substring(0,
717                                                                   content.indexOf(":")));
718         }
719         return namespaceURI;
720     }
721
722     private static String JavaDoc getService(String JavaDoc content) {
723         return content.substring(content.indexOf(":") + 1, content.length());
724     }
725 }
726
Popular Tags