KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > customfactory > client > CustomServiceImpl


1 package customfactory.client;
2
3 import java.util.ArrayList JavaDoc;
4 import java.util.HashMap JavaDoc;
5 import java.util.Hashtable JavaDoc;
6 import java.util.Iterator JavaDoc;
7 import java.util.List JavaDoc;
8 import java.util.Map JavaDoc;
9
10 import javax.wsdl.Binding;
11 import javax.wsdl.Definition;
12 import javax.wsdl.Input;
13 import javax.wsdl.Message;
14 import javax.wsdl.Operation;
15 import javax.wsdl.OperationType;
16 import javax.wsdl.Output;
17 import javax.wsdl.Port;
18 import javax.wsdl.PortType;
19 import javax.wsdl.Service;
20 import javax.wsdl.WSDLException;
21 import javax.wsdl.extensions.ExtensibilityElement;
22 import javax.wsdl.extensions.ExtensionRegistry;
23 import javax.wsdl.extensions.UnknownExtensibilityElement;
24 import javax.xml.namespace.QName JavaDoc;
25
26 import org.apache.wsif.WSIFConstants;
27 import org.apache.wsif.WSIFException;
28 import org.apache.wsif.WSIFMessage;
29 import org.apache.wsif.WSIFPort;
30 import org.apache.wsif.WSIFService;
31 import org.apache.wsif.base.WSIFClientProxy;
32 import org.apache.wsif.base.WSIFDefaultMessage;
33 import org.apache.wsif.compiler.schema.tools.Schema2Java;
34 import org.apache.wsif.compiler.util.TypeMapping;
35 import org.apache.wsif.compiler.util.Utils;
36 import org.apache.wsif.logging.MessageLogger;
37 import org.apache.wsif.logging.Trc;
38 import org.apache.wsif.providers.WSIFDynamicTypeMap;
39 import org.apache.wsif.spi.WSIFProvider;
40 import org.apache.wsif.util.WSIFPluggableProviders;
41 import org.apache.wsif.util.WSIFUtils;
42 import org.apache.wsif.wsdl.extensions.java.JavaBinding;
43 import org.w3c.dom.Element JavaDoc;
44
45 import com.ibm.wsdl.util.xml.QNameUtils;
46
47 /**
48  * An entry point to dynamic WSDL invocations.
49  *
50  * @author Alekander Slominski
51  * @author Sanjiva Weerawarana
52  * @author Owen Burroughs <owenb@apache.org>
53  * @author Ant Elder <antelder@apache.org>
54  * @author Jeremy Hughes <hughesj@apache.org>
55  * @author Mark Whitlock <whitlock@apache.org>
56  */

57 public class CustomServiceImpl implements WSIFService {
58     private static MyPrivateCompositeExtensionRegistry providersExtRegs =
59         new MyPrivateCompositeExtensionRegistry();
60     private Definition def = null;
61     private Service service;
62     private PortType portType;
63     private Port[] myPortsArr;
64     private Map JavaDoc myPortsMap;
65     private WSIFDynamicTypeMap typeMap = new WSIFDynamicTypeMap();
66     private boolean typeMapInitialised = false;
67     private String JavaDoc preferredPort = null;
68     private Map JavaDoc typeReg = null;
69     private Port chosenPort = null;
70     private WSIFMessage context;
71
72     /**
73      * Create a WSIF service instance from WSDL document URL.
74      * <br> If serviceName or serviceNS is null,
75      * then WSDL document must have exactly one service in it.
76      * <br> If portTypeName or portTypeNS is null,
77      * then WSDL document must have exactly one portType in it
78      * and all ports of the selected service must
79      * implement the same portType.
80      * <br>NOTE:
81      * The equivalent {@link org.apache.wsif.WSIFServiceFactory}.getService method
82      * should be used to create a WSIFService.
83      */

84     CustomServiceImpl(
85         String JavaDoc wsdlLoc,
86         String JavaDoc serviceNS,
87         String JavaDoc serviceName,
88         String JavaDoc portTypeNS,
89         String JavaDoc portTypeName)
90         throws WSIFException {
91         Trc.entry(this, wsdlLoc, serviceNS, serviceName, portTypeNS, portTypeName);
92
93         // load WSDL defintion
94
Definition def = null;
95         try {
96             def = WSIFUtils.readWSDL(null, wsdlLoc);
97             checkWSDL(def);
98         } catch (WSDLException ex) {
99             Trc.exception(ex);
100             throw new WSIFException("could not load " + wsdlLoc, ex);
101         }
102
103         // select WSDL service if given name
104
Service service = WSIFUtils.selectService(def, serviceNS, serviceName);
105
106         // select WSDL portType if given name
107
PortType portType = WSIFUtils.selectPortType(def, portTypeNS, portTypeName);
108
109         init(def, service, portType);
110         if (Trc.ON)
111             Trc.exit(deep());
112     }
113
114     /**
115       * Create a WSIF service instance from WSDL document URL
116       * using a ClassLoader to find local resources.
117       * <br> If serviceName or serviceNS is null,
118       * then WSDL document must have exactly one service in it.
119       * <br> If portTypeName or portTypeNS is null,
120       * then WSDL document must have exactly one portType in it
121       * and all ports of the selected service must
122       * implement the same portType.
123       * <br>NOTE:
124       * The equivalent {@link org.apache.wsif.WSIFServiceFactory}.getService method
125       * should be used to create a WSIFService.
126       */

127     CustomServiceImpl(
128         String JavaDoc wsdlLoc,
129         ClassLoader JavaDoc cl,
130         String JavaDoc serviceNS,
131         String JavaDoc serviceName,
132         String JavaDoc portTypeNS,
133         String JavaDoc portTypeName)
134         throws WSIFException {
135         Trc.entry(this, wsdlLoc, cl, serviceNS, serviceName, portTypeNS, portTypeName);
136
137         // load WSDL defintion
138
Definition def = null;
139         try {
140             def = WSIFUtils.readWSDL(null, wsdlLoc, cl);
141             checkWSDL(def);
142         } catch (WSDLException ex) {
143             Trc.exception(ex);
144             throw new WSIFException("could not load " + wsdlLoc, ex);
145         }
146
147         // select WSDL service if given name
148
Service service = WSIFUtils.selectService(def, serviceNS, serviceName);
149
150         // select WSDL portType if given name
151
PortType portType = WSIFUtils.selectPortType(def, portTypeNS, portTypeName);
152
153         init(def, service, portType);
154         if (Trc.ON)
155             Trc.exit(deep());
156     }
157
158     /**
159       * Create a WSIF service instance
160       * <br>NOTE:
161       * The equivalent {@link org.apache.wsif.WSIFServiceFactory}.getService method
162       * should be used to create a WSIFService.
163       */

164     CustomServiceImpl(Definition def) throws WSIFException {
165         this(def, null);
166     }
167
168     /**
169       * Create a WSIF service instance
170       * <br>NOTE:
171       * The equivalent {@link org.apache.wsif.WSIFServiceFactory}.getService method
172       * should be used to create a WSIFService.
173       */

174     CustomServiceImpl(Definition def, Service service) throws WSIFException {
175         this(def, service, null);
176     }
177
178     /**
179       * Create a WSIF service instance
180       * <br>NOTE:
181       * The equivalent {@link org.apache.wsif.WSIFServiceFactory}.getService method
182       * should be used to create a WSIFService.
183       */

184     CustomServiceImpl(Definition def, Service service, PortType portType)
185         throws WSIFException {
186         Trc.entry(this, def, service, portType);
187
188         init(def, service, portType);
189         if (Trc.ON)
190             Trc.exit(deep());
191     }
192
193     /**
194       * Create a WSIF service instance
195       * <br>NOTE:
196       * The equivalent {@link org.apache.wsif.WSIFServiceFactory}.getService method
197       * should be used to create a WSIFService.
198       */

199     CustomServiceImpl(Definition def, String JavaDoc serviceNS, String JavaDoc serviceName)
200         throws WSIFException {
201         Trc.entry(this, def, serviceNS, serviceName);
202
203         // select WSDL service if given by name or only one
204
Service service = WSIFUtils.selectService(def, serviceNS, serviceName);
205         init(def, service, null);
206         if (Trc.ON)
207             Trc.exit(deep());
208     }
209
210     /**
211       * Create a WSIF service instance
212       * <br>NOTE:
213       * The equivalent {@link org.apache.wsif.WSIFServiceFactory}.getService method
214       * should be used to create a WSIFService.
215       */

216     CustomServiceImpl(
217         Definition def,
218         String JavaDoc serviceNS,
219         String JavaDoc serviceName,
220         String JavaDoc portTypeNS,
221         String JavaDoc portTypeName)
222         throws WSIFException {
223         Trc.entry(this, def, serviceNS, serviceName, portTypeNS, portTypeName);
224
225         checkWSDLForWSIF(def);
226
227         // select WSDL service if given by name or only one
228
Service service = WSIFUtils.selectService(def, serviceNS, serviceName);
229
230         // select WSDL portType if given by name or only one portType
231
PortType portType = WSIFUtils.selectPortType(def, portTypeNS, portTypeName);
232
233         init(def, service, portType);
234         if (Trc.ON)
235             Trc.exit(deep());
236     }
237
238     /**
239       * Create a WSIF service instance from another instance.
240       */

241     CustomServiceImpl(CustomServiceImpl wsi)
242         throws WSIFException {
243         Trc.entry(this, wsi);
244         copyInitializedService(wsi);
245         if (Trc.ON)
246             Trc.exit(deep());
247     }
248
249     /**
250      * Copy the "read-only" parts of an initialized CustomServiceImpl
251      */

252     private void copyInitializedService(CustomServiceImpl svc) {
253         this.def = svc.def;
254         this.service = svc.service;
255         this.portType = svc.portType;
256         this.myPortsArr = new Port[svc.myPortsArr.length];
257         System.arraycopy(svc.myPortsArr, 0, this.myPortsArr, 0, svc.myPortsArr.length);
258         this.myPortsMap = (Map JavaDoc) ((Hashtable JavaDoc) svc.myPortsMap).clone();
259         this.typeMap = svc.typeMap.copy();
260     }
261
262     /**
263      * Set the preferred port
264      * @param portName The name of the port to use
265      */

266     public void setPreferredPort(String JavaDoc portName) throws WSIFException {
267         Trc.entry(this, portName);
268
269         if (portName == null) {
270             throw new WSIFException("Preferred port name cannot be null");
271         }
272         PortType pt = getPortTypeFromPortName(portName);
273         if (pt.getQName().equals(this.portType.getQName())) {
274             this.preferredPort = portName;
275         } else {
276             throw new WSIFException(
277                 "Preferred port "
278                     + portName
279                     + "is not available for the port type "
280                     + this.portType.getQName());
281         }
282         Trc.exit();
283     }
284
285     /**
286      * Create a PortType object from the name of a port
287      * @param portName The name of the port
288      * @return A PortType corresponding to the port type used by the
289      * specified port
290      */

291     private PortType getPortTypeFromPortName(String JavaDoc portName)
292         throws WSIFException {
293         if (portName == null) {
294             throw new WSIFException("Unable to find port type from a null port name");
295         }
296         Port port = (Port) service.getPort(portName);
297         if (port == null) {
298             throw new WSIFException(
299                 "Port '" + portName + "' cannot be found in the service");
300         }
301         Binding binding = port.getBinding();
302         if (binding == null) {
303             throw new WSIFException("No binding found for port '" + portName + "'");
304         }
305         PortType pt = binding.getPortType();
306         if (pt == null) {
307             throw new WSIFException(
308                 "No port type found for binding '" + binding.getQName() + "'");
309         }
310         checkPortTypeInformation(def, pt);
311         return pt;
312     }
313
314     /**
315      * Get the names of the available ports
316      * @return Iterator for list of available port names.
317      */

318     public Iterator JavaDoc getAvailablePortNames() throws WSIFException {
319         Trc.entry(this);
320         Iterator JavaDoc it = null;
321         try {
322             it = this.myPortsMap.keySet().iterator();
323         } catch (NullPointerException JavaDoc ne) {
324             Trc.exception(ne);
325             it = null;
326         }
327         Trc.exit();
328         return it;
329     }
330
331     /**
332      * Create dynamic port instance from WSDL model defnition and port.
333      */

334     private WSIFPort createDynamicWSIFPort(
335         Definition def,
336         Service service,
337         Port port)
338         throws WSIFException {
339         checkWSDLForWSIF(def);
340         List JavaDoc bindingExList = port.getBinding().getExtensibilityElements();
341         ExtensibilityElement bindingFirstEx =
342             (ExtensibilityElement) bindingExList.get(0);
343         String JavaDoc bindingNS = bindingFirstEx.getElementType().getNamespaceURI();
344         WSIFProvider provider = WSIFPluggableProviders.getProvider(bindingNS);
345         if (provider != null) {
346             return provider.createDynamicWSIFPort(def, service, port, typeMap);
347         } else {
348             throw new WSIFException(
349                 "could not find suitable provider for binding namespace '" + bindingNS + "'");
350         }
351     }
352
353     public WSIFPort getPort() throws WSIFException {
354     // ignore preferred port preference
355
// check if the service has a java binding; if so use that port
356
// since it will be faster than accessing a SOAP, EJB etc. implemenations of
357
// the same thing
358
Definition definition = getDefinition();
359     // get the first service (we assume there is exactly one)
360
// if we have no services, no bindings or other screwy stuff
361
// the code below will choke
362
Map JavaDoc services = definition.getServices();
363     Service service = (Service) services.values().iterator().next();
364     Iterator JavaDoc ports = service.getPorts().values().iterator();
365     while (ports.hasNext()) {
366         Port port = (Port) ports.next();
367         // check the binding
368
Binding binding = port.getBinding();
369         if (binding instanceof JavaBinding)
370         return getPort(port.getName());
371     }
372     // no java binding available, just return the first port
373
Port firstPort = (Port) service.getPorts().values().iterator().next();
374     return getPort(firstPort.getName());
375     }
376
377     /**
378      * Return dynamic port instance selected by port name.
379      */

380     public WSIFPort getPort(String JavaDoc portName) throws WSIFException {
381         Trc.entry(this, portName);
382         Port port = null;
383
384         if (portName == null) {
385             // Get first available port
386
if (myPortsArr.length > 0) {
387                 port = myPortsArr[0];
388             }
389         } else {
390             port = (Port) myPortsMap.get(portName);
391         }
392         if (port == null) {
393             if (portName == null) {
394                 throw new WSIFException("Unable to find an available port");
395             } else {
396                 throw new WSIFException(
397                     "Port '"
398                         + portName
399                         + "' is not available and "
400                         + " no alternative can be found");
401             }
402         }
403
404         portName = port.getName();
405         WSIFPort portInstance = createDynamicWSIFPort(def, service, port);
406         if (portInstance == null) {
407             throw new WSIFException(
408                 "Provider was unable to create WSIFPort for port " + portName);
409         }
410         // Store the chosen port so that we can query which was is being used
411
chosenPort = port;
412
413         Trc.exit(portInstance);
414         return portInstance;
415     }
416
417     /**
418      * Add association between XML and Java type.
419      * @param xmlType The qualified xml name
420      * @param javaType The Java class
421      */

422     public void mapType(QName JavaDoc xmlType, Class JavaDoc javaType) throws WSIFException {
423         Trc.entry(this, xmlType, javaType);
424         typeMap.mapType(xmlType, javaType);
425         Trc.exit();
426     }
427
428     /**
429      * Add an association between XML and Java type.
430      * @param xmlType The qualified xml name
431      * @param javaType The Java class
432      * @param force flag to indicate if mapping should override an existing one
433      * for the same xmlType
434      */

435     private void mapType(QName JavaDoc xmlType, Class JavaDoc javaType, boolean force)
436         throws WSIFException {
437         Trc.entry(this, xmlType, javaType, new Boolean JavaDoc(force));
438         typeMap.mapType(xmlType, javaType, force);
439         Trc.exit();
440     }
441
442     /**
443      * Add an association between a namespace URI and and a Java package.
444      * @param namespace The namespace URI
445      * @param packageName The full package name
446      */

447     public void mapPackage(String JavaDoc namespace, String JavaDoc packageName)
448         throws WSIFException {
449         Trc.entry(namespace, packageName);
450         typeMap.mapPackage(namespace, packageName);
451         Trc.exit();
452     }
453
454     /**
455      * @deprecated this method is replaced by the getProvider
456      * method in the org.apache.util.WSIFPluggableProviders class
457      */

458     public static WSIFProvider getDynamicWSIFProvider(String JavaDoc namespaceURI) {
459         Trc.entry(null, namespaceURI);
460         WSIFProvider p =
461            WSIFPluggableProviders.getProvider( namespaceURI );
462         Trc.exit( p );
463         return p;
464     }
465
466     /**
467      * @deprecated this method is replaced by the overrideDefaultProvider
468      * method in the org.apache.util.WSIFPluggableProviders class
469      */

470     public static void setDynamicWSIFProvider(
471         String JavaDoc providerNamespaceURI,
472         WSIFProvider provider) {
473         Trc.entry(null, providerNamespaceURI, provider);
474
475         WSIFPluggableProviders.overrideDefaultProvider(
476            providerNamespaceURI, provider );
477
478         Trc.exit();
479     }
480
481     /**
482      * @deprecated this method is replaced by the setAutoLoadProviders
483      * method in the org.apache.util.WSIFPluggableProviders class
484      */

485     public static void setAutoLoadProviders(boolean b) {
486         Trc.entry(null, b);
487         WSIFPluggableProviders.setAutoLoadProviders( b );
488         Trc.exit();
489     }
490
491     /**
492      * Get the dynamic proxy that will implement the interface iface
493      * for the port portName.
494      */

495     public Object JavaDoc getStub(String JavaDoc portName, Class JavaDoc iface) throws WSIFException {
496         Trc.entry(this, portName, iface);
497
498         // Initialise the type mappings here (not in the constructor) so that
499
// other products which use non-standard WSDL in their complexTypes
500
// that WSIF wouldn't understand, can use the DynamicInvoker
501
// successfully. Using the DynamicInvoker means we would never come
502
// through this code and so never try to parse the complexTypes.
503
// Obviously if the user wants to use dynamic proxies then we have to
504
// parse the complex types.
505
if (!typeMapInitialised) {
506             initialiseTypeMappings();
507             typeMapInitialised = true;
508         }
509
510         // if the port is not available, force the expection now rather
511
// rather than go through the rest of this method
512
WSIFPort wsifPort = getPort(portName);
513
514         // If we've got to this line then the port must be available
515
PortType pt = getPortTypeFromPortName(portName);
516
517         // If the user has already created a proxy for this interface before
518
// but is now asking for a proxy for the same interface but a different
519
// portName, we should cache the proxy here and just call
520
// clientProxy.setPort() instead.
521
WSIFClientProxy clientProxy =
522             WSIFClientProxy.newInstance(
523                 iface,
524                 def,
525                 service.getQName().getNamespaceURI(),
526                 service.getQName().getLocalPart(),
527                 portType.getQName().getNamespaceURI(),
528                 portType.getQName().getLocalPart(),
529                 typeMap);
530
531         clientProxy.setPort(wsifPort);
532         Object JavaDoc proxy = clientProxy.getProxy();
533
534         // Tracing the proxy causes a hang!
535
Trc.exit();
536         return proxy;
537     }
538
539     /**
540      * Get the dynamic proxy that will implement the interface iface
541      */

542     public Object JavaDoc getStub(Class JavaDoc iface) throws WSIFException {
543         Trc.entry(this, iface);
544
545         // Initialise the type mappings here (not in the constructor) so that
546
// other products which use non-standard WSDL in their complexTypes
547
// that WSIF wouldn't understand, can use the DynamicInvoker
548
// successfully. Using the DynamicInvoker means we would never come
549
// through this code and so never try to parse the complexTypes.
550
// Obviously if the user wants to use dynamic proxies then we have to
551
// parse the complex types.
552
if (!typeMapInitialised) {
553             initialiseTypeMappings();
554             typeMapInitialised = true;
555         }
556
557         // if the port is not available, force the expection now rather
558
// rather than go through the rest of this method
559
WSIFPort wsifPort = getPort();
560
561         // Chosen port has been stored so use it to find portType
562
String JavaDoc portName = chosenPort.getName();
563         PortType pt = getPortTypeFromPortName(portName);
564
565         // If the user has already created a proxy for this interface before
566
// but is now asking for a proxy for the same interface but a different
567
// portName, we should cache the proxy here and just call
568
// clientProxy.setPort() instead.
569
WSIFClientProxy clientProxy =
570             WSIFClientProxy.newInstance(
571                 iface,
572                 def,
573                 service.getQName().getNamespaceURI(),
574                 service.getQName().getLocalPart(),
575                 pt.getQName().getNamespaceURI(),
576                 pt.getQName().getLocalPart(),
577                 typeMap);
578
579         clientProxy.setPort(wsifPort);
580         Object JavaDoc proxy = clientProxy.getProxy();
581
582         // Tracing the proxy causes a hang!
583
Trc.exit();
584         return proxy;
585     }
586
587     /**
588      * Add new WSDL model extension registry that is shared by all
589      * dynamic WSIF providers.
590      */

591     public static void addExtensionRegistry(ExtensionRegistry reg) {
592         Trc.entry(null, reg);
593         providersExtRegs.addExtensionRegistry(reg);
594         Trc.exit();
595     }
596
597     /**
598      * Return extension registry that contains ALL declared extensions.
599      * This is special registry that does not allow to register serializers
600      * but only to add new extension registreis through
601      * addExtensionRegistry method.
602      *
603      * @see #addExtensionRegistry
604      */

605     public static ExtensionRegistry getCompositeExtensionRegistry() {
606         Trc.entry(null);
607         Trc.exit(providersExtRegs);
608         return providersExtRegs;
609     }
610
611     private void init(Definition def, Service service, PortType portType)
612         throws WSIFException {
613         if (def == null)
614             throw new IllegalArgumentException JavaDoc("WSDL definition can not be null");
615         checkWSDLForWSIF(def);
616
617         if (service == null) {
618             Map JavaDoc services = WSIFUtils.getAllItems(def, "Service");
619
620             service = (Service) WSIFUtils.getNamedItem(services, null, "Service");
621         }
622
623         if (portType == null) {
624             // if all ports have the same portType --> use it
625
Map JavaDoc ports = service.getPorts();
626             if (ports.size() == 0) {
627                 throw new WSIFException(
628                     "WSDL must contain at least one port in " + service.getQName());
629             }
630
631             for (Iterator JavaDoc i = ports.values().iterator(); i.hasNext();) {
632                 Port port = (Port) i.next();
633                 if (portType == null) {
634                     portType = port.getBinding().getPortType();
635                 } else {
636                     PortType pt = port.getBinding().getPortType();
637                     if (!pt.getQName().equals(portType.getQName())) {
638                         throw new WSIFException(
639                             "when no port type was specified all ports "
640                                 + "must have the same port type in WSDL service "
641                                 + service.getQName());
642                     }
643                 }
644             }
645             if (portType == null) {
646                 throw new IllegalArgumentException JavaDoc(
647                     "WSDL more than one portType in service " + service);
648
649             }
650         }
651         this.def = def;
652         this.service = service;
653         this.portType = portType;
654
655         // checkPortTypeIsRPC(Definition def, PortType portType) has been replaced by
656
// checkPortTypeInformation(Definition def, PortType portType) since "Input Only"
657
// operations are supported.
658
checkPortTypeInformation(def, portType);
659
660         // get all ports from service that has given portType
661

662         Map JavaDoc ports = service.getPorts();
663         // check that service has at least one port ...
664
if (ports.size() == 0) {
665             throw new WSIFException(
666                 "WSDL must contain at least one port in " + service.getQName());
667         }
668
669         myPortsMap = new Hashtable JavaDoc();
670         for (Iterator JavaDoc i = ports.values().iterator(); i.hasNext();) {
671             Port port = (Port) i.next();
672
673             Binding binding = port.getBinding();
674             if (binding == null)
675                 continue; // Ignore this error for the moment
676

677             try {
678                 // Ignore port if provider is not available for supporting it
679
List JavaDoc bindingExList = port.getBinding().getExtensibilityElements();
680                 ExtensibilityElement bindingFirstEx =
681                     (ExtensibilityElement) bindingExList.get(0);
682                 String JavaDoc bindingNS = bindingFirstEx.getElementType().getNamespaceURI();
683                 String JavaDoc addressNS = bindingNS;
684                 try {
685                     List JavaDoc addressExList = port.getExtensibilityElements();
686                     ExtensibilityElement addressFirstEx =
687                         (ExtensibilityElement) addressExList.get(0);
688                     addressNS = addressFirstEx.getElementType().getNamespaceURI();
689                 } catch (NullPointerException JavaDoc npe) {
690                     Trc.ignoredException(npe);
691                     // ignore
692
} catch (ArrayIndexOutOfBoundsException JavaDoc aie) {
693                     Trc.ignoredException(aie);
694                     // Extensibility element 0 does not exist
695
// Allow address namespace to be the same as binding
696
}
697                 // Check for a provider that supports the
698
if (WSIFPluggableProviders.isProviderAvailable(bindingNS, addressNS) ) {
699                    // check if port has the same port type
700
if (binding.getPortType().getQName().equals(portType.getQName())) {
701                       String JavaDoc portName = port.getName();
702                       myPortsMap.put(portName, port);
703                    }
704                 }
705             } catch (NullPointerException JavaDoc e) {
706                 Trc.ignoredException(e);
707                 // Binding or extensibility element or QName was null
708
// any of which means something's not right with
709
// the port so don't include it.
710
} catch (ArrayIndexOutOfBoundsException JavaDoc aie) {
711                 Trc.ignoredException(aie);
712                 // Extensibility element 0 does not exist
713
}
714         }
715         int size = myPortsMap.size();
716         myPortsArr = new Port[size];
717         int count = 0;
718         for (Iterator JavaDoc i = myPortsMap.values().iterator(); i.hasNext();) {
719             // NOTE: there is no order in ports (it is hash function dependent...)
720
Port port = (Port) i.next();
721             myPortsArr[count++] = port;
722         }
723
724         // Provide the WSIDDynamicTypeMap with a list of all of the custom
725
// types in the wsdl
726
typeMap.setAllTypes(getAllCustomTypes());
727     }
728
729     /**
730      * Get a list of all the custom complexTypes and simpleTypes in the wsdl
731      */

732     private ArrayList JavaDoc getAllCustomTypes() {
733         ArrayList JavaDoc types = new ArrayList JavaDoc();
734         Iterator JavaDoc typeMappingIterator = getDefaultTypeMappings();
735         if (typeMappingIterator != null) {
736             while (typeMappingIterator.hasNext()) {
737                 TypeMapping tm = (TypeMapping) typeMappingIterator.next();
738                 if (tm != null) {
739                     String JavaDoc namespaceURI = tm.elementType.getNamespaceURI();
740                     if (!namespaceURI.equals(WSIFConstants.NS_URI_1999_SCHEMA_XSD)
741                         && !namespaceURI.equals(WSIFConstants.NS_URI_2000_SCHEMA_XSD)
742                         && !namespaceURI.equals(WSIFConstants.NS_URI_2001_SCHEMA_XSD)) {
743                         QName JavaDoc element = tm.elementType;
744                         if (element != null) {
745                             types.add(element);
746                         }
747                     }
748                 }
749             }
750         }
751         return types;
752     }
753
754     /**
755      * Get a list of all the default mappings for complexTypes and simpleTypes
756      * in the wsdl
757      */

758     private Iterator JavaDoc getDefaultTypeMappings() {
759         if (typeReg != null) {
760             return typeReg.values().iterator();
761         }
762         typeReg = new HashMap JavaDoc();
763         List JavaDoc typesElList = Utils.getAllTypesElements(def);
764         if (typesElList.size() > 0) {
765             String JavaDoc schemaURI1999 = WSIFConstants.NS_URI_1999_SCHEMA_XSD;
766             Schema2Java s2j1999 = new Schema2Java(schemaURI1999);
767             QName JavaDoc qElemSchema1999 = new QName JavaDoc(schemaURI1999, "schema");
768             String JavaDoc schemaURI2000 = WSIFConstants.NS_URI_2000_SCHEMA_XSD;
769             Schema2Java s2j2000 = new Schema2Java(schemaURI2000);
770             QName JavaDoc qElemSchema2000 = new QName JavaDoc(schemaURI2000, "schema");
771             String JavaDoc schemaURI2001 = WSIFConstants.NS_URI_2001_SCHEMA_XSD;
772             Schema2Java s2j2001 = new Schema2Java(schemaURI2001);
773             QName JavaDoc qElemSchema2001 = new QName JavaDoc(schemaURI2001, "schema");
774
775             Iterator JavaDoc typesElIterator = typesElList.iterator();
776             while (typesElIterator.hasNext()) {
777                 UnknownExtensibilityElement unknExEl =
778                     (UnknownExtensibilityElement) typesElIterator.next();
779                 Element schemaEl = unknExEl.getElement();
780                 try {
781                     if (QNameUtils.matches(qElemSchema1999, schemaEl)
782                         || QNameUtils.matches(qElemSchema2000, schemaEl)
783                         || QNameUtils.matches(qElemSchema2001, schemaEl)) {
784                         //Hashtable typeReg = new Hashtable();
785
if (QNameUtils.matches(qElemSchema1999, schemaEl))
786                             s2j1999.createJavaMapping(schemaEl, typeReg);
787                         else if (QNameUtils.matches(qElemSchema2000, schemaEl))
788                             s2j2000.createJavaMapping(schemaEl, typeReg);
789                         else
790                             s2j2001.createJavaMapping(schemaEl, typeReg);
791                     }
792                 } catch (Exception JavaDoc e) {
793                     Trc.ignoredException(e);
794                     //ignored
795
}
796             }
797         }
798         return typeReg.values().iterator();
799     }
800
801     /**
802      * Initialize default mappings between custom complex types and simple types and
803      * Java classes.
804      */

805     private void initialiseTypeMappings() throws WSIFException {
806         Iterator JavaDoc typeMappingIterator = getDefaultTypeMappings();
807         if (typeMappingIterator != null) {
808             while (typeMappingIterator.hasNext()) {
809                 TypeMapping tm = (TypeMapping) typeMappingIterator.next();
810
811                 if (tm.elementType != null && tm.elementType.getNamespaceURI() != null) {
812                     String JavaDoc namespaceURI = tm.elementType.getNamespaceURI();
813                     if (namespaceURI != null
814                         && !namespaceURI.equals(WSIFConstants.NS_URI_1999_SCHEMA_XSD)
815                         && !namespaceURI.equals(WSIFConstants.NS_URI_2000_SCHEMA_XSD)
816                         && !namespaceURI.equals(WSIFConstants.NS_URI_2001_SCHEMA_XSD)
817                         && !namespaceURI.equals(WSIFConstants.NS_URI_SOAP_ENC)
818                         && tm.javaType != null) {
819                         String JavaDoc packageName = Utils.getPackageName(tm.javaType);
820                         if (packageName != null && !packageName.equals("")) {
821                             packageName += ".";
822                         }
823                         String JavaDoc className = packageName + Utils.getClassName(tm.javaType);
824                         Class JavaDoc clazz = null;
825
826                         try {
827                             clazz =
828                                 Class.forName(className, true, Thread.currentThread().getContextClassLoader());
829                         } catch (ClassNotFoundException JavaDoc e) {
830                             // Ignore error - mapping will not be added
831
Trc.ignoredException(e);
832                         }
833                         // Create a new mapping but don't override one that already exists for this element type
834
if (clazz != null) {
835                             mapType(tm.elementType, clazz, false);
836                         }
837                     }
838                 }
839             } // end while
840
}
841     }
842
843     /**
844      * Check PortType information is consistent. This method can be updated when
845      * new operation types are supported.
846      */

847     private void checkPortTypeInformation(Definition def, PortType portType)
848         throws WSIFException {
849         List JavaDoc operationList = portType.getOperations();
850
851         // process each operation to create dynamic operation instance
852
for (Iterator JavaDoc i = operationList.iterator(); i.hasNext();) {
853             Operation op = (Operation) i.next();
854             String JavaDoc name = op.getName();
855             if (op.isUndefined()) {
856                 throw new WSIFException("operation " + name + " is undefined!");
857             }
858             OperationType opType = op.getStyle();
859             if (opType == null) {
860                 throw new WSIFException("operation " + name + " has no type!");
861             }
862             if (opType.equals(OperationType.REQUEST_RESPONSE)) {
863                 Input input = op.getInput();
864                 Output output = op.getOutput();
865                 if (input == null) {
866                     throw new WSIFException("missing input message for operation " + name);
867                 }
868                 if (output == null) {
869                     throw new WSIFException("missing output message for operation " + name);
870                 }
871             } else if (opType.equals(OperationType.ONE_WAY)) {
872                 Input input = op.getInput();
873                 if (input == null) {
874                     throw new WSIFException("missing input message for operation " + name);
875                 }
876             } else {
877                 // Log message
878
MessageLogger.log(
879                     "WSIF.0004E",
880                     opType,
881                     portType.getQName().getLocalPart());
882
883                 // End message
884
throw new WSIFException(
885                     "operation type "
886                         + opType
887                         + " is not supported in port instance for "
888                         + portType.getQName());
889             }
890         }
891     }
892
893     private void checkWSDLForWSIF(Definition def) throws WSIFException {
894         try {
895             checkWSDL(def);
896         } catch (WSDLException ex) {
897             Trc.exception(ex);
898             throw new WSIFException("invalid WSDL defintion " + def.getQName(), ex);
899         }
900     }
901
902     /**
903      * Check WSDL defintion to make sure it does not contain undefined
904      * elements (typical case is referncing not defined portType).
905      * <p><b>NOTE:</b> check is done only for curent document and not
906      * recursively for imported ones (they may be invalid but this
907      * port factory may not need them...).
908      */

909     private void checkWSDL(Definition def) throws WSDLException {
910         for (Iterator JavaDoc i = def.getMessages().values().iterator(); i.hasNext();) {
911             Message v = (Message) i.next();
912             if (v.isUndefined()) {
913                 throw new WSDLException(
914                     WSDLException.INVALID_WSDL,
915                     "referencing undefined message " + v);
916             }
917         }
918         for (Iterator JavaDoc i = def.getPortTypes().values().iterator(); i.hasNext();) {
919             PortType v = (PortType) i.next();
920             if (v.isUndefined()) {
921                 throw new WSDLException(
922                     WSDLException.INVALID_WSDL,
923                     "referencing undefined portType " + v);
924             }
925         }
926         for (Iterator JavaDoc i = def.getBindings().values().iterator(); i.hasNext();) {
927             Binding v = (Binding) i.next();
928             if (v.isUndefined()) {
929                 throw new WSDLException(
930                     WSDLException.INVALID_WSDL,
931                     "referencing undefined binding " + v);
932             }
933         }
934     }
935
936     /**
937     * Get the Definition object representing the wsdl document
938     * @return The Definition object
939     */

940     public Definition getDefinition() {
941         Trc.entry(this);
942         Trc.exit(def);
943         return def;
944     }
945
946     /**
947      * Gets the context information for this WSIFService.
948      * @return context
949      */

950     public WSIFMessage getContext() throws WSIFException {
951         Trc.entry(this);
952         WSIFMessage contextCopy;
953         if (this.context == null) {
954             contextCopy = new WSIFDefaultMessage();
955         } else {
956             try {
957                 contextCopy = (WSIFMessage) this.context.clone();
958             } catch (CloneNotSupportedException JavaDoc e) {
959                 throw new WSIFException(
960                     "CloneNotSupportedException cloning context", e);
961             }
962         }
963         Trc.exit(contextCopy);
964         return contextCopy;
965     }
966
967     /**
968      * Sets the context information for this WSIFService.
969      * @param WSIFMessage the new context information
970      */

971     public void setContext(WSIFMessage context) {
972         Trc.entry(this, context);
973         if (context == null) {
974             throw new IllegalArgumentException JavaDoc("context must not be null");
975         }
976         this.context = context;
977         Trc.exit();
978     }
979
980     public String JavaDoc deep() {
981         String JavaDoc buff = "";
982         try {
983             buff = new String JavaDoc(this.toString());
984             buff += "\nprovidersExtRegs:"
985                 + (providersExtRegs == null ? "null" : providersExtRegs.toString());
986             buff += "\ndef:" + Trc.brief(def);
987             buff += "\nservice:" + Trc.brief(service);
988             buff += "\nportType:" + Trc.brief(portType);
989             buff += "\nmyPortsArr:" + (myPortsArr == null ? "null" : myPortsArr.toString());
990             buff += "\nmyPortsMap:" + Trc.brief(myPortsMap);
991             buff += "\ntypeMap:" + (typeMap == null ? "null" : typeMap.toString());
992             buff += "\ntypeMapInitialised:" + typeMapInitialised;
993             buff += "\npreferredPort:" + (preferredPort == null ? "null" : preferredPort);
994             buff += "\nchosenPort:" + Trc.brief(chosenPort);
995             buff += "\ncontext:" + context;
996         } catch (Exception JavaDoc e) {
997             Trc.exceptionInTrace(e);
998         }
999         return buff;
1000    }
1001}
1002
1003
Popular Tags