View Javadoc

1   /*******************************************************************************
2    *  Copyright (c) 2005, 2006, 2007 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.jbi4ejb.jbi.endpoint;
9   
10  import it.imolinfo.jbi4ejb.Logger;
11  import it.imolinfo.jbi4ejb.LoggerFactory;
12  import it.imolinfo.jbi4ejb.descriptor.ProviderServiceDescriptor;
13  import it.imolinfo.jbi4ejb.exception.Jbi4EjbException;
14  import it.imolinfo.jbi4ejb.jbi.Messages;
15  import it.imolinfo.jbi4ejb.jbi.component.runtime.AbstractComponentLifeCycle;
16  import it.imolinfo.jbi4ejb.jbi.component.runtime.RuntimeHelper;
17  import it.imolinfo.jbi4ejb.processor.ProviderExchangeProcessor;
18  import it.imolinfo.jbi4ejb.runtime.ProviderServiceCreator;
19  import it.imolinfo.jbi4ejb.runtime.ejbproxy.StatelessEJBProxy;
20  import it.imolinfo.jbi4ejb.runtime.ejbproxy.StatelessEJBProxyFactory;
21  import it.imolinfo.jbi4ejb.webservice.generator.Util;
22  
23  import java.io.ByteArrayOutputStream;
24  import java.io.File;
25  import java.util.List;
26  
27  import javax.xml.namespace.QName;
28  
29  import org.codehaus.xfire.XFire;
30  import org.codehaus.xfire.service.Service;
31  
32  /**
33   * The Class Jbi4EjbProviderEndpoint.
34   * 
35   * @author <a href="mailto:mpiraccini@imolinfo.it">Marco Piraccini</a> 
36   */
37  public class Jbi4EjbProviderEndpoint extends Jbi4EjbEndpoint {
38      
39      private static final long serialVersionUID = -7829120677780408268L;
40  
41      /** The Logger. */
42      private static final Logger LOG
43      = LoggerFactory.getLogger(Jbi4EjbProviderEndpoint.class);
44      final Messages MESSAGES = Messages.getMessages(Jbi4EjbProviderEndpoint.class);
45  
46      /** The service descriptor. */
47      private ProviderServiceDescriptor serviceDescriptor;
48  
49      /** The xfire service. */
50      private Service xfireService;
51      
52      /**
53       * Instantiates a new jbi4 ejb provider endpoint.
54       * 
55       * @param serviceName the service name 
56       * @param endpointName the endpoint name
57       * 
58       * @throws Jbi4EjbException
59       *             if some problem occurs
60       */
61      public Jbi4EjbProviderEndpoint(QName serviceName, String endpointName) throws Jbi4EjbException {
62          super(serviceName, endpointName);
63          this.setExchangeProcessor(new ProviderExchangeProcessor(this));
64      }    
65      
66  
67      /* (non-Javadoc)
68       * @see it.imolinfo.jbi4ejb.jbi.endpoint.Jbi4EjbEndpoint#registerService()
69       */
70      
71      /**
72       * Creates the EJB proxy and registers the xfire service.
73       * @throws Jbi4EjbException if some problem occurs
74       */
75      public void registerService() throws Jbi4EjbException {
76          LOG.debug("Registering xfire service for: " + this.getServiceName());
77          
78          XFire xfire = this.getSuManager().getLifeCycle().getXfire();
79  
80          File wsdl = this.getEndpointWSDL();
81  
82          // Creates the deploy directory
83          String deployDirectoryName = serviceDescriptor.getComponentRootPath() + "/" + serviceDescriptor.hashCode();
84          LOG.debug("Starting deploy using root path: " + deployDirectoryName);
85          File rootDir = new File(deployDirectoryName);
86          if (!rootDir.exists()) {
87              boolean result = rootDir.mkdir();
88              if (!result) {
89              	String msg=MESSAGES.getString("EJB000301_Error_creating_working_directory", new Object[] {rootDir.getAbsolutePath()});
90                  LOG.error(msg);
91                  throw new Jbi4EjbException(msg);
92  
93              } else {
94                  LOG.debug("created working directory: " + rootDir.getAbsolutePath());
95              }
96          }
97  
98          // Creates the jar list to compile the sources                
99          List<String> jarFilesName = Util.prepareClassPath(RuntimeHelper.getComponentContext().getInstallRoot());
100 
101         // Creates the EJB proxy class        
102         StatelessEJBProxy ejbProxy = StatelessEJBProxyFactory.createEJBProxy(wsdl, serviceDescriptor, rootDir, jarFilesName);
103 
104         // Sets the ejbProxy on the service descriptor
105         serviceDescriptor.setEjbProxy(ejbProxy);        
106 
107         // Generates the xfire service
108         ProviderServiceCreator serviceCreator = new ProviderServiceCreator();
109 
110         // Generate the WSDL using xfire. The xfire service MUST be created
111         xfireService = serviceCreator.createJbiService(this.serviceDescriptor,
112                 xfire);
113         xfire.getServiceRegistry().register(xfireService);
114                 
115         // Logs out the xfire service WSDL
116         ByteArrayOutputStream baos = new ByteArrayOutputStream();
117         LOG.debug("xfireService:" + xfireService);
118         xfire.generateWSDL(xfireService.getSimpleName(), baos);
119         if (LOG.isDebugEnabled()) {
120           LOG.debug("WSDL:\n------------------\n"
121                   + baos.toString()
122                   + "\n------------------");
123         }
124 
125     }
126 
127 
128     /**
129      * Unregisters the service.
130      * @see it.imolinfo.jbi4ejb.jbi.endpoint.Jbi4EjbEndpoint#unregisterService()
131      * @throws Jbi4EjbException if some problem occurs
132      */
133     public void unregisterService() throws Jbi4EjbException {
134         // DO nothing
135     	LOG.info("EJB000302_unregister_Service", new Object[]{this.getServiceName()}, new Object[]{this.getEndpointName()});
136     }      
137 
138     /**
139      * Validate the endpoint.
140      * (now do noting)
141      * @throws Jbi4EjbException if some problem occurs
142      */
143     public void validate() throws Jbi4EjbException {
144         // Do nothing
145     }
146 
147     /**
148      * Gets the service descriptor.
149      * 
150      * @return the service descriptor
151      */
152     public ProviderServiceDescriptor getServiceDescriptor() {
153         return serviceDescriptor;
154     }
155 
156     /**
157      * Sets the service descriptor.
158      * 
159      * @param serviceDescriptor
160      *            the new service descriptor
161      */
162     public void setServiceDescriptor(ProviderServiceDescriptor serviceDescriptor) {
163         this.serviceDescriptor = serviceDescriptor;
164     }   
165 
166     /**
167      * Gets the xfire service.
168      * 
169      * @return the xfire service
170      */
171     public Service getXfireService() {
172         return xfireService;
173     }
174 
175 }