View Javadoc

1    /****************************************************************************
2    * Copyright (c) 2005, 2006, 2007, 2008, 2009 Imola Informatica.
3    * All rights reserved. This program and the accompanying materials
4    * are made available under the terms of the LGPL License v2.1
5    * which accompanies this distribution, and is available at
6    * http://www.gnu.org/licenses/lgpl.html
7    ****************************************************************************/
8   package it.imolinfo.jbi4corba.webservice.runtime;
9   
10  import it.imolinfo.jbi4corba.Logger;
11  import it.imolinfo.jbi4corba.LoggerFactory;
12  import it.imolinfo.jbi4corba.exception.Jbi4CorbaException;
13  import it.imolinfo.jbi4corba.jbi.cxf.CXFUtils;
14  import it.imolinfo.jbi4corba.webservice.descriptor.ProviderServiceDescriptor;
15  
16  import java.io.ByteArrayOutputStream;
17  import java.io.IOException;
18  
19  import javax.wsdl.WSDLException;
20  import javax.xml.namespace.QName;
21  
22  import org.apache.cxf.jaxws.JaxWsClientFactoryBean;
23  import org.apache.cxf.service.Service;
24  
25  /**
26   * The Class ProviderCXFServiceCreator.
27   */
28  public class ProviderServiceCreator {
29  
30      /** Logger. */
31      private static final Logger LOG
32      = LoggerFactory.getLogger(ProviderServiceCreator.class);  
33      /**
34       * Empty Constructor.
35       */
36      public ProviderServiceCreator() {
37          // NOP
38      }
39  
40      /**
41       * Creates the CXF service. 
42       * If the interface name is null, get it from the created service.
43       * 
44       * @param serviceDescriptor
45       * @param interfaceName
46       * 
47       * @return the service
48       * 
49       * @throws IOException
50       * @throws WSDLException
51       * @throws Jbi4CorbaException
52       */
53      public Service createService(ProviderServiceDescriptor serviceDescriptor, QName interfaceName) throws IOException, WSDLException, 
54      Jbi4CorbaException {
55          
56          LOG.debug(">>>>> createService - begin");  
57          LOG.debug("InterfaceName: " + interfaceName);
58          LOG.debug("Service Interface: " + serviceDescriptor.getServiceInterface());       
59  
60          // Creates the endpoints
61          JaxWsClientFactoryBean endpointFactory = CXFUtils.getJaxWsClientFactoryBean();                  
62          
63          // The wrapper must be anonymous to avoid name collisions in jaxb-jaxws, for example:
64          // Foo foo();
65          // With CXF 2.1.3 does not work!!!!!!!!
66          // See https://issues.apache.org/jira/browse/CXF-1930        
67          endpointFactory.getServiceFactory().setAnonymousWrapperTypes(true);
68          // To qualify schema elements
69          endpointFactory.getServiceFactory().setQualifyWrapperSchema(true);     
70          endpointFactory.getServiceFactory().setWrapped(true);        
71                  
72          // Sets the service class    
73          endpointFactory.setServiceClass(serviceDescriptor.getServiceInterface());
74          endpointFactory.setServiceName(interfaceName);
75                         
76          // Creates the endpoint
77          endpointFactory.create();              
78         
79          // Gets the service model
80          Service service = endpointFactory.getServiceFactory().getService();
81  
82          LOG.debug("service: " + service);
83          LOG.debug("service.getName(): " + service.getName());       
84  
85          LOG.debug("Found services: " + service.getServiceInfos().size());
86          if (LOG.isDebugEnabled()) {
87              LOG.debug("Service=" + service);
88          }    
89  
90          // Sets the service invoker
91          service.setInvoker(new ProviderServiceInvoker(serviceDescriptor));    
92  
93          if (LOG.isDebugEnabled()) {
94              ByteArrayOutputStream baos = new ByteArrayOutputStream();        
95              CXFUtils.writeDefinitionOnOutputStream(service, baos);        
96              LOG.debug(baos.toString());        
97          }
98  
99          LOG.debug("<<<<< createService - end");
100         return service;
101     }
102     
103     /**
104      * Creates the CXF service no specifying the interface name (gets it from the created service). 
105      * 
106      * @param serviceDescriptor
107      * 
108      * @return the service
109      * 
110      * @throws IOException
111      * @throws WSDLException
112      * @throws Jbi4CorbaException
113      */
114     public Service createService(ProviderServiceDescriptor serviceDescriptor) throws IOException, 
115         WSDLException, Jbi4CorbaException {        
116         return createService(serviceDescriptor, null);
117     }   
118 
119 }