Source Code Cross Referenced for CustomServiceImpl.java in  » Web-Services » wsif » customfactory » client » Java Source Code / Java DocumentationJava Source Code and Java Documentation

Java Source Code / Java Documentation
1. 6.0 JDK Core
2. 6.0 JDK Modules
3. 6.0 JDK Modules com.sun
4. 6.0 JDK Modules com.sun.java
5. 6.0 JDK Modules sun
6. 6.0 JDK Platform
7. Ajax
8. Apache Harmony Java SE
9. Aspect oriented
10. Authentication Authorization
11. Blogger System
12. Build
13. Byte Code
14. Cache
15. Chart
16. Chat
17. Code Analyzer
18. Collaboration
19. Content Management System
20. Database Client
21. Database DBMS
22. Database JDBC Connection Pool
23. Database ORM
24. Development
25. EJB Server geronimo
26. EJB Server GlassFish
27. EJB Server JBoss 4.2.1
28. EJB Server resin 3.1.5
29. ERP CRM Financial
30. ESB
31. Forum
32. GIS
33. Graphic Library
34. Groupware
35. HTML Parser
36. IDE
37. IDE Eclipse
38. IDE Netbeans
39. Installer
40. Internationalization Localization
41. Inversion of Control
42. Issue Tracking
43. J2EE
44. JBoss
45. JMS
46. JMX
47. Library
48. Mail Clients
49. Net
50. Parser
51. PDF
52. Portal
53. Profiler
54. Project Management
55. Report
56. RSS RDF
57. Rule Engine
58. Science
59. Scripting
60. Search Engine
61. Security
62. Sevlet Container
63. Source Control
64. Swing Library
65. Template Engine
66. Test Coverage
67. Testing
68. UML
69. Web Crawler
70. Web Framework
71. Web Mail
72. Web Server
73. Web Services
74. Web Services apache cxf 2.0.1
75. Web Services AXIS2
76. Wiki Engine
77. Workflow Engines
78. XML
79. XML UI
Java
Java Tutorial
Java Open Source
Jar File Download
Java Articles
Java Products
Java by API
Photoshop Tutorials
Maya Tutorials
Flash Tutorials
3ds-Max Tutorials
Illustrator Tutorials
GIMP Tutorials
C# / C Sharp
C# / CSharp Tutorial
C# / CSharp Open Source
ASP.Net
ASP.NET Tutorial
JavaScript DHTML
JavaScript Tutorial
JavaScript Reference
HTML / CSS
HTML CSS Reference
C / ANSI-C
C Tutorial
C++
C++ Tutorial
Ruby
PHP
Python
Python Tutorial
Python Open Source
SQL Server / T-SQL
SQL Server / T-SQL Tutorial
Oracle PL / SQL
Oracle PL/SQL Tutorial
PostgreSQL
SQL / MySQL
MySQL Tutorial
VB.Net
VB.Net Tutorial
Flash / Flex / ActionScript
VBA / Excel / Access / Word
XML
XML Tutorial
Microsoft Office PowerPoint 2007 Tutorial
Microsoft Office Excel 2007 Tutorial
Microsoft Office Word 2007 Tutorial
Java Source Code / Java Documentation » Web Services » wsif » customfactory.client 
Source Cross Referenced  Class Diagram Java Document (Java Doc) 


0001:        package customfactory.client;
0002:
0003:        import java.util.ArrayList;
0004:        import java.util.HashMap;
0005:        import java.util.Hashtable;
0006:        import java.util.Iterator;
0007:        import java.util.List;
0008:        import java.util.Map;
0009:
0010:        import javax.wsdl.Binding;
0011:        import javax.wsdl.Definition;
0012:        import javax.wsdl.Input;
0013:        import javax.wsdl.Message;
0014:        import javax.wsdl.Operation;
0015:        import javax.wsdl.OperationType;
0016:        import javax.wsdl.Output;
0017:        import javax.wsdl.Port;
0018:        import javax.wsdl.PortType;
0019:        import javax.wsdl.Service;
0020:        import javax.wsdl.WSDLException;
0021:        import javax.wsdl.extensions.ExtensibilityElement;
0022:        import javax.wsdl.extensions.ExtensionRegistry;
0023:        import javax.wsdl.extensions.UnknownExtensibilityElement;
0024:        import javax.xml.namespace.QName;
0025:
0026:        import org.apache.wsif.WSIFConstants;
0027:        import org.apache.wsif.WSIFException;
0028:        import org.apache.wsif.WSIFMessage;
0029:        import org.apache.wsif.WSIFPort;
0030:        import org.apache.wsif.WSIFService;
0031:        import org.apache.wsif.base.WSIFClientProxy;
0032:        import org.apache.wsif.base.WSIFDefaultMessage;
0033:        import org.apache.wsif.compiler.schema.tools.Schema2Java;
0034:        import org.apache.wsif.compiler.util.TypeMapping;
0035:        import org.apache.wsif.compiler.util.Utils;
0036:        import org.apache.wsif.logging.MessageLogger;
0037:        import org.apache.wsif.logging.Trc;
0038:        import org.apache.wsif.providers.WSIFDynamicTypeMap;
0039:        import org.apache.wsif.spi.WSIFProvider;
0040:        import org.apache.wsif.util.WSIFPluggableProviders;
0041:        import org.apache.wsif.util.WSIFUtils;
0042:        import org.apache.wsif.wsdl.extensions.java.JavaBinding;
0043:        import org.w3c.dom.Element;
0044:
0045:        import com.ibm.wsdl.util.xml.QNameUtils;
0046:
0047:        /**
0048:         * An entry point to dynamic WSDL invocations.
0049:         *
0050:         * @author Alekander Slominski
0051:         * @author Sanjiva Weerawarana
0052:         * @author Owen Burroughs <owenb@apache.org>
0053:         * @author Ant Elder <antelder@apache.org>
0054:         * @author Jeremy Hughes <hughesj@apache.org>
0055:         * @author Mark Whitlock <whitlock@apache.org>
0056:         */
0057:        public class CustomServiceImpl implements  WSIFService {
0058:            private static MyPrivateCompositeExtensionRegistry providersExtRegs = new MyPrivateCompositeExtensionRegistry();
0059:            private Definition def = null;
0060:            private Service service;
0061:            private PortType portType;
0062:            private Port[] myPortsArr;
0063:            private Map myPortsMap;
0064:            private WSIFDynamicTypeMap typeMap = new WSIFDynamicTypeMap();
0065:            private boolean typeMapInitialised = false;
0066:            private String preferredPort = null;
0067:            private Map typeReg = null;
0068:            private Port chosenPort = null;
0069:            private WSIFMessage context;
0070:
0071:            /**
0072:             * Create a WSIF service instance from WSDL document URL.
0073:             * <br> If serviceName or serviceNS is null,
0074:             *   then WSDL document must have exactly one service in it.
0075:             * <br> If portTypeName or portTypeNS is null,
0076:             *   then WSDL document must have exactly one portType in it
0077:             *   and all ports of the selected service must
0078:             *    implement the same portType.
0079:             * <br>NOTE: 
0080:             * The equivalent {@link org.apache.wsif.WSIFServiceFactory}.getService method
0081:             * should be used to create a WSIFService.
0082:             */
0083:            CustomServiceImpl(String wsdlLoc, String serviceNS,
0084:                    String serviceName, String portTypeNS, String portTypeName)
0085:                    throws WSIFException {
0086:                Trc.entry(this , wsdlLoc, serviceNS, serviceName, portTypeNS,
0087:                        portTypeName);
0088:
0089:                // load WSDL defintion
0090:                Definition def = null;
0091:                try {
0092:                    def = WSIFUtils.readWSDL(null, wsdlLoc);
0093:                    checkWSDL(def);
0094:                } catch (WSDLException ex) {
0095:                    Trc.exception(ex);
0096:                    throw new WSIFException("could not load " + wsdlLoc, ex);
0097:                }
0098:
0099:                // select WSDL service if given name
0100:                Service service = WSIFUtils.selectService(def, serviceNS,
0101:                        serviceName);
0102:
0103:                // select WSDL portType if given name
0104:                PortType portType = WSIFUtils.selectPortType(def, portTypeNS,
0105:                        portTypeName);
0106:
0107:                init(def, service, portType);
0108:                if (Trc.ON)
0109:                    Trc.exit(deep());
0110:            }
0111:
0112:            /**
0113:             * Create a WSIF service instance from WSDL document URL
0114:             * using a ClassLoader to find local resources.
0115:             * <br> If serviceName or serviceNS is null,
0116:             *   then WSDL document must have exactly one service in it.
0117:             * <br> If portTypeName or portTypeNS is null,
0118:             *   then WSDL document must have exactly one portType in it
0119:             *   and all ports of the selected service must
0120:             *    implement the same portType.
0121:             * <br>NOTE: 
0122:             * The equivalent {@link org.apache.wsif.WSIFServiceFactory}.getService method
0123:             * should be used to create a WSIFService.
0124:             */
0125:            CustomServiceImpl(String wsdlLoc, ClassLoader cl, String serviceNS,
0126:                    String serviceName, String portTypeNS, String portTypeName)
0127:                    throws WSIFException {
0128:                Trc.entry(this , wsdlLoc, cl, serviceNS, serviceName,
0129:                        portTypeNS, portTypeName);
0130:
0131:                // load WSDL defintion
0132:                Definition def = null;
0133:                try {
0134:                    def = WSIFUtils.readWSDL(null, wsdlLoc, cl);
0135:                    checkWSDL(def);
0136:                } catch (WSDLException ex) {
0137:                    Trc.exception(ex);
0138:                    throw new WSIFException("could not load " + wsdlLoc, ex);
0139:                }
0140:
0141:                // select WSDL service if given name
0142:                Service service = WSIFUtils.selectService(def, serviceNS,
0143:                        serviceName);
0144:
0145:                // select WSDL portType if given name
0146:                PortType portType = WSIFUtils.selectPortType(def, portTypeNS,
0147:                        portTypeName);
0148:
0149:                init(def, service, portType);
0150:                if (Trc.ON)
0151:                    Trc.exit(deep());
0152:            }
0153:
0154:            /**
0155:             * Create a WSIF service instance
0156:             * <br>NOTE:
0157:             * The equivalent {@link org.apache.wsif.WSIFServiceFactory}.getService method
0158:             * should be used to create a WSIFService.
0159:             */
0160:            CustomServiceImpl(Definition def) throws WSIFException {
0161:                this (def, null);
0162:            }
0163:
0164:            /**
0165:             * Create a WSIF service instance
0166:             * <br>NOTE: 
0167:             * The equivalent {@link org.apache.wsif.WSIFServiceFactory}.getService method
0168:             * should be used to create a WSIFService.
0169:             */
0170:            CustomServiceImpl(Definition def, Service service)
0171:                    throws WSIFException {
0172:                this (def, service, null);
0173:            }
0174:
0175:            /**
0176:             * Create a WSIF service instance
0177:             * <br>NOTE: 
0178:             * The equivalent {@link org.apache.wsif.WSIFServiceFactory}.getService method
0179:             * should be used to create a WSIFService.
0180:             */
0181:            CustomServiceImpl(Definition def, Service service, PortType portType)
0182:                    throws WSIFException {
0183:                Trc.entry(this , def, service, portType);
0184:
0185:                init(def, service, portType);
0186:                if (Trc.ON)
0187:                    Trc.exit(deep());
0188:            }
0189:
0190:            /**
0191:             * Create a WSIF service instance
0192:             * <br>NOTE: 
0193:             * The equivalent {@link org.apache.wsif.WSIFServiceFactory}.getService method
0194:             * should be used to create a WSIFService.
0195:             */
0196:            CustomServiceImpl(Definition def, String serviceNS,
0197:                    String serviceName) throws WSIFException {
0198:                Trc.entry(this , def, serviceNS, serviceName);
0199:
0200:                // select WSDL service if given by name or only one
0201:                Service service = WSIFUtils.selectService(def, serviceNS,
0202:                        serviceName);
0203:                init(def, service, null);
0204:                if (Trc.ON)
0205:                    Trc.exit(deep());
0206:            }
0207:
0208:            /**
0209:             * Create a WSIF service instance
0210:             * <br>NOTE: 
0211:             * The equivalent {@link org.apache.wsif.WSIFServiceFactory}.getService method
0212:             * should be used to create a WSIFService.
0213:             */
0214:            CustomServiceImpl(Definition def, String serviceNS,
0215:                    String serviceName, String portTypeNS, String portTypeName)
0216:                    throws WSIFException {
0217:                Trc.entry(this , def, serviceNS, serviceName, portTypeNS,
0218:                        portTypeName);
0219:
0220:                checkWSDLForWSIF(def);
0221:
0222:                // select WSDL service if given by name or only one
0223:                Service service = WSIFUtils.selectService(def, serviceNS,
0224:                        serviceName);
0225:
0226:                // select WSDL portType if given by name or only one portType
0227:                PortType portType = WSIFUtils.selectPortType(def, portTypeNS,
0228:                        portTypeName);
0229:
0230:                init(def, service, portType);
0231:                if (Trc.ON)
0232:                    Trc.exit(deep());
0233:            }
0234:
0235:            /**
0236:             * Create a WSIF service instance from another instance. 
0237:             */
0238:            CustomServiceImpl(CustomServiceImpl wsi) throws WSIFException {
0239:                Trc.entry(this , wsi);
0240:                copyInitializedService(wsi);
0241:                if (Trc.ON)
0242:                    Trc.exit(deep());
0243:            }
0244:
0245:            /**
0246:             * Copy the "read-only" parts of an initialized CustomServiceImpl
0247:             */
0248:            private void copyInitializedService(CustomServiceImpl svc) {
0249:                this .def = svc.def;
0250:                this .service = svc.service;
0251:                this .portType = svc.portType;
0252:                this .myPortsArr = new Port[svc.myPortsArr.length];
0253:                System.arraycopy(svc.myPortsArr, 0, this .myPortsArr, 0,
0254:                        svc.myPortsArr.length);
0255:                this .myPortsMap = (Map) ((Hashtable) svc.myPortsMap).clone();
0256:                this .typeMap = svc.typeMap.copy();
0257:            }
0258:
0259:            /**
0260:             * Set the preferred port
0261:             * @param portName The name of the port to use
0262:             */
0263:            public void setPreferredPort(String portName) throws WSIFException {
0264:                Trc.entry(this , portName);
0265:
0266:                if (portName == null) {
0267:                    throw new WSIFException(
0268:                            "Preferred port name cannot be null");
0269:                }
0270:                PortType pt = getPortTypeFromPortName(portName);
0271:                if (pt.getQName().equals(this .portType.getQName())) {
0272:                    this .preferredPort = portName;
0273:                } else {
0274:                    throw new WSIFException("Preferred port " + portName
0275:                            + "is not available for the port type "
0276:                            + this .portType.getQName());
0277:                }
0278:                Trc.exit();
0279:            }
0280:
0281:            /**
0282:             * Create a PortType object from the name of a port
0283:             * @param portName The name of the port
0284:             * @return A PortType corresponding to the port type used by the
0285:             * specified port
0286:             */
0287:            private PortType getPortTypeFromPortName(String portName)
0288:                    throws WSIFException {
0289:                if (portName == null) {
0290:                    throw new WSIFException(
0291:                            "Unable to find port type from a null port name");
0292:                }
0293:                Port port = (Port) service.getPort(portName);
0294:                if (port == null) {
0295:                    throw new WSIFException("Port '" + portName
0296:                            + "' cannot be found in the service");
0297:                }
0298:                Binding binding = port.getBinding();
0299:                if (binding == null) {
0300:                    throw new WSIFException("No binding found for port '"
0301:                            + portName + "'");
0302:                }
0303:                PortType pt = binding.getPortType();
0304:                if (pt == null) {
0305:                    throw new WSIFException("No port type found for binding '"
0306:                            + binding.getQName() + "'");
0307:                }
0308:                checkPortTypeInformation(def, pt);
0309:                return pt;
0310:            }
0311:
0312:            /**
0313:             * Get the names of the available ports
0314:             * @return Iterator for list of available port names.
0315:             */
0316:            public Iterator getAvailablePortNames() throws WSIFException {
0317:                Trc.entry(this );
0318:                Iterator it = null;
0319:                try {
0320:                    it = this .myPortsMap.keySet().iterator();
0321:                } catch (NullPointerException ne) {
0322:                    Trc.exception(ne);
0323:                    it = null;
0324:                }
0325:                Trc.exit();
0326:                return it;
0327:            }
0328:
0329:            /**
0330:             * Create dynamic port instance from WSDL model defnition and port.
0331:             */
0332:            private WSIFPort createDynamicWSIFPort(Definition def,
0333:                    Service service, Port port) throws WSIFException {
0334:                checkWSDLForWSIF(def);
0335:                List bindingExList = port.getBinding()
0336:                        .getExtensibilityElements();
0337:                ExtensibilityElement bindingFirstEx = (ExtensibilityElement) bindingExList
0338:                        .get(0);
0339:                String bindingNS = bindingFirstEx.getElementType()
0340:                        .getNamespaceURI();
0341:                WSIFProvider provider = WSIFPluggableProviders
0342:                        .getProvider(bindingNS);
0343:                if (provider != null) {
0344:                    return provider.createDynamicWSIFPort(def, service, port,
0345:                            typeMap);
0346:                } else {
0347:                    throw new WSIFException(
0348:                            "could not find suitable provider for binding namespace '"
0349:                                    + bindingNS + "'");
0350:                }
0351:            }
0352:
0353:            public WSIFPort getPort() throws WSIFException {
0354:                // ignore preferred port preference
0355:                // check if the service has a java binding; if so use that port
0356:                // since it will be faster than accessing a SOAP, EJB etc. implemenations of 
0357:                // the same thing
0358:                Definition definition = getDefinition();
0359:                // get the first service (we assume there is exactly one)
0360:                // if we have no services, no bindings or other screwy stuff
0361:                // the code below will choke
0362:                Map services = definition.getServices();
0363:                Service service = (Service) services.values().iterator().next();
0364:                Iterator ports = service.getPorts().values().iterator();
0365:                while (ports.hasNext()) {
0366:                    Port port = (Port) ports.next();
0367:                    // check the binding
0368:                    Binding binding = port.getBinding();
0369:                    if (binding instanceof  JavaBinding)
0370:                        return getPort(port.getName());
0371:                }
0372:                // no java binding available, just return the first port
0373:                Port firstPort = (Port) service.getPorts().values().iterator()
0374:                        .next();
0375:                return getPort(firstPort.getName());
0376:            }
0377:
0378:            /**
0379:             * Return dynamic port instance selected by port name.
0380:             */
0381:            public WSIFPort getPort(String portName) throws WSIFException {
0382:                Trc.entry(this , portName);
0383:                Port port = null;
0384:
0385:                if (portName == null) {
0386:                    // Get first available port
0387:                    if (myPortsArr.length > 0) {
0388:                        port = myPortsArr[0];
0389:                    }
0390:                } else {
0391:                    port = (Port) myPortsMap.get(portName);
0392:                }
0393:                if (port == null) {
0394:                    if (portName == null) {
0395:                        throw new WSIFException(
0396:                                "Unable to find an available port");
0397:                    } else {
0398:                        throw new WSIFException("Port '" + portName
0399:                                + "' is not available and "
0400:                                + " no alternative can be found");
0401:                    }
0402:                }
0403:
0404:                portName = port.getName();
0405:                WSIFPort portInstance = createDynamicWSIFPort(def, service,
0406:                        port);
0407:                if (portInstance == null) {
0408:                    throw new WSIFException(
0409:                            "Provider was unable to create WSIFPort for port "
0410:                                    + portName);
0411:                }
0412:                // Store the chosen port so that we can query which was is being used
0413:                chosenPort = port;
0414:
0415:                Trc.exit(portInstance);
0416:                return portInstance;
0417:            }
0418:
0419:            /**
0420:             * Add association between XML and Java type.
0421:             * @param xmlType The qualified xml name
0422:             * @param javaType The Java class 
0423:             */
0424:            public void mapType(QName xmlType, Class javaType)
0425:                    throws WSIFException {
0426:                Trc.entry(this , xmlType, javaType);
0427:                typeMap.mapType(xmlType, javaType);
0428:                Trc.exit();
0429:            }
0430:
0431:            /**
0432:             * Add an association between XML and Java type.
0433:             * @param xmlType The qualified xml name
0434:             * @param javaType The Java class
0435:             * @param force flag to indicate if mapping should override an existing one
0436:             * for the same xmlType 
0437:             */
0438:            private void mapType(QName xmlType, Class javaType, boolean force)
0439:                    throws WSIFException {
0440:                Trc.entry(this , xmlType, javaType, new Boolean(force));
0441:                typeMap.mapType(xmlType, javaType, force);
0442:                Trc.exit();
0443:            }
0444:
0445:            /**
0446:             * Add an association between a namespace URI and and a Java package.
0447:             * @param namespace The namespace URI
0448:             * @param packageName The full package name
0449:             */
0450:            public void mapPackage(String namespace, String packageName)
0451:                    throws WSIFException {
0452:                Trc.entry(namespace, packageName);
0453:                typeMap.mapPackage(namespace, packageName);
0454:                Trc.exit();
0455:            }
0456:
0457:            /**
0458:             * @deprecated this method is replaced by the getProvider
0459:             * method in the org.apache.util.WSIFPluggableProviders class
0460:             */
0461:            public static WSIFProvider getDynamicWSIFProvider(
0462:                    String namespaceURI) {
0463:                Trc.entry(null, namespaceURI);
0464:                WSIFProvider p = WSIFPluggableProviders
0465:                        .getProvider(namespaceURI);
0466:                Trc.exit(p);
0467:                return p;
0468:            }
0469:
0470:            /**
0471:             * @deprecated this method is replaced by the overrideDefaultProvider
0472:             * method in the org.apache.util.WSIFPluggableProviders class
0473:             */
0474:            public static void setDynamicWSIFProvider(
0475:                    String providerNamespaceURI, WSIFProvider provider) {
0476:                Trc.entry(null, providerNamespaceURI, provider);
0477:
0478:                WSIFPluggableProviders.overrideDefaultProvider(
0479:                        providerNamespaceURI, provider);
0480:
0481:                Trc.exit();
0482:            }
0483:
0484:            /**
0485:             * @deprecated this method is replaced by the setAutoLoadProviders
0486:             * method in the org.apache.util.WSIFPluggableProviders class
0487:             */
0488:            public static void setAutoLoadProviders(boolean b) {
0489:                Trc.entry(null, b);
0490:                WSIFPluggableProviders.setAutoLoadProviders(b);
0491:                Trc.exit();
0492:            }
0493:
0494:            /**
0495:             * Get the dynamic proxy that will implement the interface iface
0496:             * for the port portName.
0497:             */
0498:            public Object getStub(String portName, Class iface)
0499:                    throws WSIFException {
0500:                Trc.entry(this , portName, iface);
0501:
0502:                // Initialise the type mappings here (not in the constructor) so that
0503:                // other products which use non-standard WSDL in their complexTypes
0504:                // that WSIF wouldn't understand, can use the DynamicInvoker 
0505:                // successfully. Using the DynamicInvoker means we would never come
0506:                // through this code and so never try to parse the complexTypes.
0507:                // Obviously if the user wants to use dynamic proxies then we have to 
0508:                // parse the complex types.
0509:                if (!typeMapInitialised) {
0510:                    initialiseTypeMappings();
0511:                    typeMapInitialised = true;
0512:                }
0513:
0514:                // if the port is not available, force the expection now rather
0515:                // rather than go through the rest of this method		
0516:                WSIFPort wsifPort = getPort(portName);
0517:
0518:                // If we've got to this line then the port must be available
0519:                PortType pt = getPortTypeFromPortName(portName);
0520:
0521:                // If the user has already created a proxy for this interface before
0522:                // but is now asking for a proxy for the same interface but a different
0523:                // portName, we should cache the proxy here and just call 
0524:                // clientProxy.setPort() instead.
0525:                WSIFClientProxy clientProxy = WSIFClientProxy.newInstance(
0526:                        iface, def, service.getQName().getNamespaceURI(),
0527:                        service.getQName().getLocalPart(), portType.getQName()
0528:                                .getNamespaceURI(), portType.getQName()
0529:                                .getLocalPart(), typeMap);
0530:
0531:                clientProxy.setPort(wsifPort);
0532:                Object proxy = clientProxy.getProxy();
0533:
0534:                // Tracing the proxy causes a hang!
0535:                Trc.exit();
0536:                return proxy;
0537:            }
0538:
0539:            /**
0540:             * Get the dynamic proxy that will implement the interface iface
0541:             */
0542:            public Object getStub(Class iface) throws WSIFException {
0543:                Trc.entry(this , iface);
0544:
0545:                // Initialise the type mappings here (not in the constructor) so that
0546:                // other products which use non-standard WSDL in their complexTypes
0547:                // that WSIF wouldn't understand, can use the DynamicInvoker 
0548:                // successfully. Using the DynamicInvoker means we would never come
0549:                // through this code and so never try to parse the complexTypes.
0550:                // Obviously if the user wants to use dynamic proxies then we have to 
0551:                // parse the complex types.
0552:                if (!typeMapInitialised) {
0553:                    initialiseTypeMappings();
0554:                    typeMapInitialised = true;
0555:                }
0556:
0557:                // if the port is not available, force the expection now rather
0558:                // rather than go through the rest of this method		
0559:                WSIFPort wsifPort = getPort();
0560:
0561:                // Chosen port has been stored so use it to find portType
0562:                String portName = chosenPort.getName();
0563:                PortType pt = getPortTypeFromPortName(portName);
0564:
0565:                // If the user has already created a proxy for this interface before
0566:                // but is now asking for a proxy for the same interface but a different
0567:                // portName, we should cache the proxy here and just call 
0568:                // clientProxy.setPort() instead.
0569:                WSIFClientProxy clientProxy = WSIFClientProxy.newInstance(
0570:                        iface, def, service.getQName().getNamespaceURI(),
0571:                        service.getQName().getLocalPart(), pt.getQName()
0572:                                .getNamespaceURI(), pt.getQName()
0573:                                .getLocalPart(), typeMap);
0574:
0575:                clientProxy.setPort(wsifPort);
0576:                Object proxy = clientProxy.getProxy();
0577:
0578:                // Tracing the proxy causes a hang!
0579:                Trc.exit();
0580:                return proxy;
0581:            }
0582:
0583:            /**
0584:             * Add new WSDL model extension registry that is shared by all
0585:             * dynamic WSIF providers.
0586:             */
0587:            public static void addExtensionRegistry(ExtensionRegistry reg) {
0588:                Trc.entry(null, reg);
0589:                providersExtRegs.addExtensionRegistry(reg);
0590:                Trc.exit();
0591:            }
0592:
0593:            /**
0594:             * Return extension registry that contains ALL declared extensions.
0595:             * This is special registry that does not allow to register serializers
0596:             * but only to add new extension registreis through
0597:             * addExtensionRegistry method.
0598:             *
0599:             * @see #addExtensionRegistry
0600:             */
0601:            public static ExtensionRegistry getCompositeExtensionRegistry() {
0602:                Trc.entry(null);
0603:                Trc.exit(providersExtRegs);
0604:                return providersExtRegs;
0605:            }
0606:
0607:            private void init(Definition def, Service service, PortType portType)
0608:                    throws WSIFException {
0609:                if (def == null)
0610:                    throw new IllegalArgumentException(
0611:                            "WSDL definition can not be null");
0612:                checkWSDLForWSIF(def);
0613:
0614:                if (service == null) {
0615:                    Map services = WSIFUtils.getAllItems(def, "Service");
0616:
0617:                    service = (Service) WSIFUtils.getNamedItem(services, null,
0618:                            "Service");
0619:                }
0620:
0621:                if (portType == null) {
0622:                    // if all ports have the same portType --> use it
0623:                    Map ports = service.getPorts();
0624:                    if (ports.size() == 0) {
0625:                        throw new WSIFException(
0626:                                "WSDL must contain at least one port in "
0627:                                        + service.getQName());
0628:                    }
0629:
0630:                    for (Iterator i = ports.values().iterator(); i.hasNext();) {
0631:                        Port port = (Port) i.next();
0632:                        if (portType == null) {
0633:                            portType = port.getBinding().getPortType();
0634:                        } else {
0635:                            PortType pt = port.getBinding().getPortType();
0636:                            if (!pt.getQName().equals(portType.getQName())) {
0637:                                throw new WSIFException(
0638:                                        "when no port type was specified all ports "
0639:                                                + "must have the same port type in WSDL service "
0640:                                                + service.getQName());
0641:                            }
0642:                        }
0643:                    }
0644:                    if (portType == null) {
0645:                        throw new IllegalArgumentException(
0646:                                "WSDL more than one portType in service "
0647:                                        + service);
0648:
0649:                    }
0650:                }
0651:                this .def = def;
0652:                this .service = service;
0653:                this .portType = portType;
0654:
0655:                // checkPortTypeIsRPC(Definition def, PortType portType) has been replaced by 
0656:                // checkPortTypeInformation(Definition def, PortType portType) since "Input Only"
0657:                // operations are supported.
0658:                checkPortTypeInformation(def, portType);
0659:
0660:                // get all ports from service that has given portType
0661:
0662:                Map ports = service.getPorts();
0663:                // check that service has at least one port ...
0664:                if (ports.size() == 0) {
0665:                    throw new WSIFException(
0666:                            "WSDL must contain at least one port in "
0667:                                    + service.getQName());
0668:                }
0669:
0670:                myPortsMap = new Hashtable();
0671:                for (Iterator i = ports.values().iterator(); i.hasNext();) {
0672:                    Port port = (Port) i.next();
0673:
0674:                    Binding binding = port.getBinding();
0675:                    if (binding == null)
0676:                        continue; // Ignore this error for the moment
0677:
0678:                    try {
0679:                        // Ignore port if provider is not available for supporting it
0680:                        List bindingExList = port.getBinding()
0681:                                .getExtensibilityElements();
0682:                        ExtensibilityElement bindingFirstEx = (ExtensibilityElement) bindingExList
0683:                                .get(0);
0684:                        String bindingNS = bindingFirstEx.getElementType()
0685:                                .getNamespaceURI();
0686:                        String addressNS = bindingNS;
0687:                        try {
0688:                            List addressExList = port
0689:                                    .getExtensibilityElements();
0690:                            ExtensibilityElement addressFirstEx = (ExtensibilityElement) addressExList
0691:                                    .get(0);
0692:                            addressNS = addressFirstEx.getElementType()
0693:                                    .getNamespaceURI();
0694:                        } catch (NullPointerException npe) {
0695:                            Trc.ignoredException(npe);
0696:                            // ignore
0697:                        } catch (ArrayIndexOutOfBoundsException aie) {
0698:                            Trc.ignoredException(aie);
0699:                            // Extensibility element 0 does not exist
0700:                            // Allow address namespace to be the same as binding
0701:                        }
0702:                        // Check for a provider that supports the 
0703:                        if (WSIFPluggableProviders.isProviderAvailable(
0704:                                bindingNS, addressNS)) {
0705:                            // check if port has the same port type
0706:                            if (binding.getPortType().getQName().equals(
0707:                                    portType.getQName())) {
0708:                                String portName = port.getName();
0709:                                myPortsMap.put(portName, port);
0710:                            }
0711:                        }
0712:                    } catch (NullPointerException e) {
0713:                        Trc.ignoredException(e);
0714:                        // Binding or extensibility element or QName was null
0715:                        // any of which means something's not right with
0716:                        // the port so don't include it.
0717:                    } catch (ArrayIndexOutOfBoundsException aie) {
0718:                        Trc.ignoredException(aie);
0719:                        // Extensibility element 0 does not exist
0720:                    }
0721:                }
0722:                int size = myPortsMap.size();
0723:                myPortsArr = new Port[size];
0724:                int count = 0;
0725:                for (Iterator i = myPortsMap.values().iterator(); i.hasNext();) {
0726:                    // NOTE: there is no order in ports (it is hash function dependent...)
0727:                    Port port = (Port) i.next();
0728:                    myPortsArr[count++] = port;
0729:                }
0730:
0731:                // Provide the WSIDDynamicTypeMap with a list of all of the custom
0732:                // types in the wsdl
0733:                typeMap.setAllTypes(getAllCustomTypes());
0734:            }
0735:
0736:            /**
0737:             * Get a list of all the custom complexTypes and simpleTypes in the wsdl
0738:             */
0739:            private ArrayList getAllCustomTypes() {
0740:                ArrayList types = new ArrayList();
0741:                Iterator typeMappingIterator = getDefaultTypeMappings();
0742:                if (typeMappingIterator != null) {
0743:                    while (typeMappingIterator.hasNext()) {
0744:                        TypeMapping tm = (TypeMapping) typeMappingIterator
0745:                                .next();
0746:                        if (tm != null) {
0747:                            String namespaceURI = tm.elementType
0748:                                    .getNamespaceURI();
0749:                            if (!namespaceURI
0750:                                    .equals(WSIFConstants.NS_URI_1999_SCHEMA_XSD)
0751:                                    && !namespaceURI
0752:                                            .equals(WSIFConstants.NS_URI_2000_SCHEMA_XSD)
0753:                                    && !namespaceURI
0754:                                            .equals(WSIFConstants.NS_URI_2001_SCHEMA_XSD)) {
0755:                                QName element = tm.elementType;
0756:                                if (element != null) {
0757:                                    types.add(element);
0758:                                }
0759:                            }
0760:                        }
0761:                    }
0762:                }
0763:                return types;
0764:            }
0765:
0766:            /**
0767:             * Get a list of all the default mappings for complexTypes and simpleTypes 
0768:             * in the wsdl
0769:             */
0770:            private Iterator getDefaultTypeMappings() {
0771:                if (typeReg != null) {
0772:                    return typeReg.values().iterator();
0773:                }
0774:                typeReg = new HashMap();
0775:                List typesElList = Utils.getAllTypesElements(def);
0776:                if (typesElList.size() > 0) {
0777:                    String schemaURI1999 = WSIFConstants.NS_URI_1999_SCHEMA_XSD;
0778:                    Schema2Java s2j1999 = new Schema2Java(schemaURI1999);
0779:                    QName qElemSchema1999 = new QName(schemaURI1999, "schema");
0780:                    String schemaURI2000 = WSIFConstants.NS_URI_2000_SCHEMA_XSD;
0781:                    Schema2Java s2j2000 = new Schema2Java(schemaURI2000);
0782:                    QName qElemSchema2000 = new QName(schemaURI2000, "schema");
0783:                    String schemaURI2001 = WSIFConstants.NS_URI_2001_SCHEMA_XSD;
0784:                    Schema2Java s2j2001 = new Schema2Java(schemaURI2001);
0785:                    QName qElemSchema2001 = new QName(schemaURI2001, "schema");
0786:
0787:                    Iterator typesElIterator = typesElList.iterator();
0788:                    while (typesElIterator.hasNext()) {
0789:                        UnknownExtensibilityElement unknExEl = (UnknownExtensibilityElement) typesElIterator
0790:                                .next();
0791:                        Element schemaEl = unknExEl.getElement();
0792:                        try {
0793:                            if (QNameUtils.matches(qElemSchema1999, schemaEl)
0794:                                    || QNameUtils.matches(qElemSchema2000,
0795:                                            schemaEl)
0796:                                    || QNameUtils.matches(qElemSchema2001,
0797:                                            schemaEl)) {
0798:                                //Hashtable typeReg = new Hashtable();
0799:                                if (QNameUtils.matches(qElemSchema1999,
0800:                                        schemaEl))
0801:                                    s2j1999
0802:                                            .createJavaMapping(schemaEl,
0803:                                                    typeReg);
0804:                                else if (QNameUtils.matches(qElemSchema2000,
0805:                                        schemaEl))
0806:                                    s2j2000
0807:                                            .createJavaMapping(schemaEl,
0808:                                                    typeReg);
0809:                                else
0810:                                    s2j2001
0811:                                            .createJavaMapping(schemaEl,
0812:                                                    typeReg);
0813:                            }
0814:                        } catch (Exception e) {
0815:                            Trc.ignoredException(e);
0816:                            //ignored
0817:                        }
0818:                    }
0819:                }
0820:                return typeReg.values().iterator();
0821:            }
0822:
0823:            /**
0824:             * Initialize default mappings between custom complex types and simple types and
0825:             * Java classes.
0826:             */
0827:            private void initialiseTypeMappings() throws WSIFException {
0828:                Iterator typeMappingIterator = getDefaultTypeMappings();
0829:                if (typeMappingIterator != null) {
0830:                    while (typeMappingIterator.hasNext()) {
0831:                        TypeMapping tm = (TypeMapping) typeMappingIterator
0832:                                .next();
0833:
0834:                        if (tm.elementType != null
0835:                                && tm.elementType.getNamespaceURI() != null) {
0836:                            String namespaceURI = tm.elementType
0837:                                    .getNamespaceURI();
0838:                            if (namespaceURI != null
0839:                                    && !namespaceURI
0840:                                            .equals(WSIFConstants.NS_URI_1999_SCHEMA_XSD)
0841:                                    && !namespaceURI
0842:                                            .equals(WSIFConstants.NS_URI_2000_SCHEMA_XSD)
0843:                                    && !namespaceURI
0844:                                            .equals(WSIFConstants.NS_URI_2001_SCHEMA_XSD)
0845:                                    && !namespaceURI
0846:                                            .equals(WSIFConstants.NS_URI_SOAP_ENC)
0847:                                    && tm.javaType != null) {
0848:                                String packageName = Utils
0849:                                        .getPackageName(tm.javaType);
0850:                                if (packageName != null
0851:                                        && !packageName.equals("")) {
0852:                                    packageName += ".";
0853:                                }
0854:                                String className = packageName
0855:                                        + Utils.getClassName(tm.javaType);
0856:                                Class clazz = null;
0857:
0858:                                try {
0859:                                    clazz = Class.forName(className, true,
0860:                                            Thread.currentThread()
0861:                                                    .getContextClassLoader());
0862:                                } catch (ClassNotFoundException e) {
0863:                                    // Ignore error - mapping will not be added
0864:                                    Trc.ignoredException(e);
0865:                                }
0866:                                // Create a new mapping but don't override one that already exists for this element type
0867:                                if (clazz != null) {
0868:                                    mapType(tm.elementType, clazz, false);
0869:                                }
0870:                            }
0871:                        }
0872:                    } // end while
0873:                }
0874:            }
0875:
0876:            /**
0877:             * Check PortType information is consistent. This method can be updated when
0878:             * new operation types are supported.
0879:             */
0880:            private void checkPortTypeInformation(Definition def,
0881:                    PortType portType) throws WSIFException {
0882:                List operationList = portType.getOperations();
0883:
0884:                // process each operation to create dynamic operation instance
0885:                for (Iterator i = operationList.iterator(); i.hasNext();) {
0886:                    Operation op = (Operation) i.next();
0887:                    String name = op.getName();
0888:                    if (op.isUndefined()) {
0889:                        throw new WSIFException("operation " + name
0890:                                + " is undefined!");
0891:                    }
0892:                    OperationType opType = op.getStyle();
0893:                    if (opType == null) {
0894:                        throw new WSIFException("operation " + name
0895:                                + " has no type!");
0896:                    }
0897:                    if (opType.equals(OperationType.REQUEST_RESPONSE)) {
0898:                        Input input = op.getInput();
0899:                        Output output = op.getOutput();
0900:                        if (input == null) {
0901:                            throw new WSIFException(
0902:                                    "missing input message for operation "
0903:                                            + name);
0904:                        }
0905:                        if (output == null) {
0906:                            throw new WSIFException(
0907:                                    "missing output message for operation "
0908:                                            + name);
0909:                        }
0910:                    } else if (opType.equals(OperationType.ONE_WAY)) {
0911:                        Input input = op.getInput();
0912:                        if (input == null) {
0913:                            throw new WSIFException(
0914:                                    "missing input message for operation "
0915:                                            + name);
0916:                        }
0917:                    } else {
0918:                        // Log message
0919:                        MessageLogger.log("WSIF.0004E", opType, portType
0920:                                .getQName().getLocalPart());
0921:
0922:                        // End message
0923:                        throw new WSIFException("operation type " + opType
0924:                                + " is not supported in port instance for "
0925:                                + portType.getQName());
0926:                    }
0927:                }
0928:            }
0929:
0930:            private void checkWSDLForWSIF(Definition def) throws WSIFException {
0931:                try {
0932:                    checkWSDL(def);
0933:                } catch (WSDLException ex) {
0934:                    Trc.exception(ex);
0935:                    throw new WSIFException("invalid WSDL defintion "
0936:                            + def.getQName(), ex);
0937:                }
0938:            }
0939:
0940:            /**
0941:             * Check WSDL defintion to make sure it does not contain undefined
0942:             * elements (typical case is referncing not defined portType).
0943:             * <p><b>NOTE:</b> check is done only for curent document and not
0944:             *  recursively for imported ones (they may be invalid but this
0945:             *  port factory may not need them...).
0946:             */
0947:            private void checkWSDL(Definition def) throws WSDLException {
0948:                for (Iterator i = def.getMessages().values().iterator(); i
0949:                        .hasNext();) {
0950:                    Message v = (Message) i.next();
0951:                    if (v.isUndefined()) {
0952:                        throw new WSDLException(WSDLException.INVALID_WSDL,
0953:                                "referencing undefined message " + v);
0954:                    }
0955:                }
0956:                for (Iterator i = def.getPortTypes().values().iterator(); i
0957:                        .hasNext();) {
0958:                    PortType v = (PortType) i.next();
0959:                    if (v.isUndefined()) {
0960:                        throw new WSDLException(WSDLException.INVALID_WSDL,
0961:                                "referencing undefined portType " + v);
0962:                    }
0963:                }
0964:                for (Iterator i = def.getBindings().values().iterator(); i
0965:                        .hasNext();) {
0966:                    Binding v = (Binding) i.next();
0967:                    if (v.isUndefined()) {
0968:                        throw new WSDLException(WSDLException.INVALID_WSDL,
0969:                                "referencing undefined binding " + v);
0970:                    }
0971:                }
0972:            }
0973:
0974:            /**
0975:             * Get the Definition object representing the wsdl document
0976:             * @return The Definition object
0977:             */
0978:            public Definition getDefinition() {
0979:                Trc.entry(this );
0980:                Trc.exit(def);
0981:                return def;
0982:            }
0983:
0984:            /**
0985:             * Gets the context information for this WSIFService.
0986:             * @return context
0987:             */
0988:            public WSIFMessage getContext() throws WSIFException {
0989:                Trc.entry(this );
0990:                WSIFMessage contextCopy;
0991:                if (this .context == null) {
0992:                    contextCopy = new WSIFDefaultMessage();
0993:                } else {
0994:                    try {
0995:                        contextCopy = (WSIFMessage) this .context.clone();
0996:                    } catch (CloneNotSupportedException e) {
0997:                        throw new WSIFException(
0998:                                "CloneNotSupportedException cloning context", e);
0999:                    }
1000:                }
1001:                Trc.exit(contextCopy);
1002:                return contextCopy;
1003:            }
1004:
1005:            /**
1006:             * Sets the context information for this WSIFService.
1007:             * @param WSIFMessage the new context information
1008:             */
1009:            public void setContext(WSIFMessage context) {
1010:                Trc.entry(this , context);
1011:                if (context == null) {
1012:                    throw new IllegalArgumentException(
1013:                            "context must not be null");
1014:                }
1015:                this .context = context;
1016:                Trc.exit();
1017:            }
1018:
1019:            public String deep() {
1020:                String buff = "";
1021:                try {
1022:                    buff = new String(this .toString());
1023:                    buff += "\nprovidersExtRegs:"
1024:                            + (providersExtRegs == null ? "null"
1025:                                    : providersExtRegs.toString());
1026:                    buff += "\ndef:" + Trc.brief(def);
1027:                    buff += "\nservice:" + Trc.brief(service);
1028:                    buff += "\nportType:" + Trc.brief(portType);
1029:                    buff += "\nmyPortsArr:"
1030:                            + (myPortsArr == null ? "null" : myPortsArr
1031:                                    .toString());
1032:                    buff += "\nmyPortsMap:" + Trc.brief(myPortsMap);
1033:                    buff += "\ntypeMap:"
1034:                            + (typeMap == null ? "null" : typeMap.toString());
1035:                    buff += "\ntypeMapInitialised:" + typeMapInitialised;
1036:                    buff += "\npreferredPort:"
1037:                            + (preferredPort == null ? "null" : preferredPort);
1038:                    buff += "\nchosenPort:" + Trc.brief(chosenPort);
1039:                    buff += "\ncontext:" + context;
1040:                } catch (Exception e) {
1041:                    Trc.exceptionInTrace(e);
1042:                }
1043:                return buff;
1044:            }
1045:        }
www.java2java.com | Contact Us
Copyright 2009 - 12 Demo Source and Support. All rights reserved.
All other trademarks are property of their respective owners.