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.runtime.ejbproxy;
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.EJBDeployException;
14  import it.imolinfo.jbi4ejb.exception.EJBInvokeException;
15  import it.imolinfo.jbi4ejb.exception.EJBWSDLGenerationException;
16  import it.imolinfo.jbi4ejb.jbi.Messages;
17  import it.imolinfo.jbi4ejb.webservice.generator.Util;
18  
19  import java.io.File;
20  import java.net.MalformedURLException;
21  import java.util.List;
22  import java.util.Properties;
23  
24  import org.omg.CORBA.ORB;
25  
26  /**
27   * A factory for creating StatelessEJBProxy objects.
28   * 
29   * @author <a href="mailto:mpiraccini@imolinfo.it">Marco Piraccini</a>
30   */
31  public final class StatelessEJBProxyFactory {
32  
33      /** The Constant LOG. */
34      private static final Logger LOG
35      = LoggerFactory.getLogger(StatelessEJBProxyFactory.class);
36      private static final Messages MESSAGES
37      = Messages.getMessages(StatelessEJBProxyFactory.class);
38      
39      /**
40       * Instantiates a new stateless EJB proxy factory.
41       */
42      private StatelessEJBProxyFactory() {}
43      
44      /**
45       * Creates the <code>StatelessEJBProxy</code> from the service
46       * description.
47       * 
48       * @param wsdl
49       *            the wsdl <source>File</source>
50       * @param serviceDescriptor
51       *            the service descriotor
52       * @param tempDir
53       *            the temp dir where to create the EJB proxy files
54       * @param jarFilesName
55       *            the jars list
56       * 
57       * @return the <code>StatelessEJBProxy</code>
58       * 
59       * @throws EJBDeployException if some problem occurs in the ejb proxy creation
60       */
61      @SuppressWarnings("unchecked")
62      public static StatelessEJBProxy createEJBProxy(File wsdl, ProviderServiceDescriptor serviceDescriptor, File tempDir, List<String> jarFilesName) throws EJBDeployException {       
63  
64          // Getst the portTypeName from the service descriptor
65          String portTypeName = serviceDescriptor.getPortTypeName().getLocalPart();
66          
67          // Creates the EJB classes        
68          EJBClasses ejbClasses = EJBProxyUtils.createEJBClasses(wsdl, serviceDescriptor.getSerialVersionUID(), tempDir, jarFilesName, null, portTypeName);
69          
70          // Creates the URL ClassLoader, based on the generated classes        
71          ClassLoader ejbInvokeClassLoader;
72          try {
73              ejbInvokeClassLoader = Util.getURLClassLoader(ejbClasses.getEjbClassesPath());
74          } catch (MalformedURLException e) {
75          	String msg=MESSAGES.getString("EJB000917_Exception_creating_URL_ClassLoder", new Object[]{e.getMessage()});
76              LOG.error(msg,e);
77              throw new EJBDeployException(msg,e);
78          }
79  
80          // Init the ORB
81          ORB orb = ORB.init(new String[]{}, serviceDescriptor.getOrbProperties());
82  
83          // Uses DII to create the remote bean reference
84          Object remoteBean = EJBProxyUtils.createStatelessEJBFromCorbaName(serviceDescriptor.getName(), ejbClasses.getRemoteInterfaceClassName(), ejbInvokeClassLoader, orb);
85  
86          // Creates the EJB proxy
87          StatelessEJBProxy ejbProxy = new StatelessEJBProxy(ejbClasses.getRemoteInterfaceClassName(), remoteBean, ejbInvokeClassLoader, orb);
88  
89          // Try to load the EJB remote interface and set it
90          try {
91              Class myRemoteInterface = ejbInvokeClassLoader.loadClass(ejbClasses.getRemoteInterfaceClassName());
92              ejbProxy.setRemoteInterfaceClass(myRemoteInterface);
93              
94          } catch (ClassNotFoundException e) {
95          	String msg=MESSAGES.getString("EJB000918_Exception_getting_remote_interface_class", new Object[]{e.getMessage()});
96              LOG.error(msg,e);
97              throw new EJBDeployException(msg,e);
98          }
99  
100         return ejbProxy;
101     }    
102 
103     /**
104      * Gets the EJB from corbaname.
105      * 
106      * @param wsdlPath
107      *          The path to the WSDL file
108      * @param remoteInterfaceClassName
109      *          The remote interface class name
110      * @param corbaName
111      *          The corba name
112      * @param classesId
113      *          The classes UIDs
114      * @param jarFilesName
115      *          The jar list to use to compile the classes
116      * @param orbParams
117      *          The orb parameters
118      * @return the EJB from corbaname
119      * 
120      * @throws EJBWSDLGenerationException
121      *          if some proxy ejb generation  occurs
122      */
123     @SuppressWarnings("unchecked")
124     public static StatelessEJBProxy getEJBFromCorbaname(String wsdlPath, String remoteInterfaceClassName, String corbaName
125             , Properties classesId, List<String> jarFilesName, Properties orbParams) throws EJBWSDLGenerationException {
126         return getEJBFromCorbaname(wsdlPath, remoteInterfaceClassName, corbaName, classesId, jarFilesName, orbParams, false);
127     }
128 
129     /**
130      * Gets the EJB from corbaname using RMI classloader.
131      * 
132      * @param wsdlPath
133      *      The path to the WSDL file
134      * @param remoteInterfaceClassName
135      *      The remote interface class name
136      * @param corbaName
137      *       The corba name
138      * @param classesId
139      *        The classes UIDs
140      * @param jarFilesName
141      *      The jar list to use to compile the classes
142      * @param orbParams
143      *       The orb parameters
144      * 
145      * @return the EJB from corbaname using RMI classloader
146      * 
147      * @throws EJBWSDLGenerationException
148      *          if some proxy ejb generation  occurs
149      */
150     @SuppressWarnings("unchecked")
151     public static StatelessEJBProxy getEJBFromCorbanameUsingRMIClassloader(String wsdlPath, String remoteInterfaceClassName, String corbaName
152             , Properties classesId, List<String> jarFilesName, Properties orbParams) throws EJBWSDLGenerationException {
153         return getEJBFromCorbaname(wsdlPath, remoteInterfaceClassName, corbaName, classesId, jarFilesName, orbParams, true);
154     }
155     
156     /**
157      * Instantiates a new dynamic EJB proxy.
158      * 
159      * @param wsdlPath
160      *            The complete WSDL path
161      * @param remoteInterfaceClassName
162      *            The remote interface name
163      * @param classesId
164      *            The <code>Hashtable</code> containing the clasess UIDS
165      * @param jarFilesName
166      *            The jar list to use to compile the generated classes
167      * @param jndiName
168      *            The jndi name
169      * @param jndiParams
170      *            The jndi properties
171      * @param orbProperties
172      *            The ORB properties
173      * 
174      * @return the EJB proxy
175      * 
176      * @throws EJBWSDLGenerationException
177      *             If some problem occurs
178      */
179     @SuppressWarnings("unchecked")
180     public static StatelessEJBProxy getEJBFromJNDIName(String wsdlPath, String remoteInterfaceClassName, String jndiName, Properties jndiParams,
181             Properties orbProperties, Properties classesId, List<String> jarFilesName) throws EJBWSDLGenerationException {         
182 
183         ClassLoader ejbInvokeClassLoader = null;
184         Object remoteBean = null;
185         try {               
186 
187             // Creates the EJB Classes
188             String classesDir = EJBProxyUtils.createEJBClasses(wsdlPath, remoteInterfaceClassName, null, classesId, jarFilesName);                       
189 
190             LOG.debug("The classes are in the directory:" + classesDir);
191 
192             // Creates the URL class Loader, based on the generated classes
193             ejbInvokeClassLoader = Util.getURLClassLoader(classesDir);
194 
195             
196             // Saves the previuos classloader
197             ClassLoader previousClassLoader = Thread.currentThread().getContextClassLoader(); 
198             Thread.currentThread().setContextClassLoader(ejbInvokeClassLoader);
199             
200             // Uses DII to create the remote bean reference
201             org.omg.CORBA.portable.ObjectImpl remoteHome = EJBProxyUtils.createStatelessHomeFromJNDI(jndiName, jndiParams, remoteInterfaceClassName, ejbInvokeClassLoader);
202             
203             // Gets the ORB from the org.omg.CORBA.portable.ObjectImpl object
204             ORB orb = remoteHome._orb();               
205 
206             LOG.info("EJB000911_ORB_found", new Object[]{orb});    
207             // Retrieve the EJB reference
208             remoteBean = EJBProxyUtils.getEJBFromCorbaHomeObject(remoteHome, orb, remoteInterfaceClassName, ejbInvokeClassLoader);        
209 
210             // Sets back the prevoius classloader
211             Thread.currentThread().setContextClassLoader(previousClassLoader);
212                         
213             StatelessEJBProxy ejbProxy = new StatelessEJBProxy(remoteInterfaceClassName, remoteBean, ejbInvokeClassLoader, orb);
214 
215             return ejbProxy;
216 
217         } catch (Exception ex) {
218             String msg=MESSAGES.getString("EJB000919_getEJBFromJNDIName", new Object[]{ex.getMessage()});
219             LOG.error(msg,ex);
220             throw new EJBWSDLGenerationException(msg,ex);
221         }
222     }        
223 
224 
225 /**
226  * Instantiates a new dynamic EJB client.
227  * 
228  * @param wsdlPath
229  *            The complete WSDL path
230  * @param remoteInterfaceClassName
231  *            The remote interface name
232  * @param corbaName
233  *            The corba name to use
234  * @param classesId
235  *            The <code>Hashtable</code> containing the clasess UIDS
236  * @param jarFilesName
237  *            The jar list to use to compile the generated classes
238  * @param orbParams
239  *            The ORB params
240  * @param dynamicClassLoading
241  *            If true, try to dinamically load stubs and classes using the
242  *            RMIClassLoader
243  * 
244  * @return
245  *      The EJB proxy
246  * 
247  * @throws EJBWSDLGenerationException
248  *             If some problem occurs
249  */
250 @SuppressWarnings("unchecked")
251 public static StatelessEJBProxy getEJBFromCorbaname(String wsdlPath, String remoteInterfaceClassName, String corbaName
252         , Properties classesId, List<String> jarFilesName, Properties orbParams, boolean dynamicClassLoading) throws EJBWSDLGenerationException {
253 
254     ClassLoader ejbInvokeClassLoader = null;
255     Object remoteBean = null;
256     try {            
257         
258         ORB orb = null;
259         if (orbParams != null) {
260             orb = ORB.init(new String[]{}, orbParams);
261         } else {
262             orb = ORB.init(new String[]{}, new Properties());
263         }        
264 
265         if (!dynamicClassLoading) {                                           
266 
267             // Creates the EJB Classes
268             String classesDir = EJBProxyUtils.createEJBClasses(wsdlPath, remoteInterfaceClassName, null, classesId, jarFilesName);
269 
270             LOG.debug("The ejb client classes are in the directory:" + classesDir);
271 
272             // Creates the URL class Loader, based on the generated classes
273             ejbInvokeClassLoader = Util.getURLClassLoader(classesDir);
274 
275             // Uses DII to create the remote bean reference
276             remoteBean = EJBProxyUtils.createStatelessEJBFromCorbaName(corbaName, remoteInterfaceClassName, ejbInvokeClassLoader, orb);            
277         } else {
278 
279             // Dynamic stub loading
280             LOG.debug("Dynamic invocation, classes loaded using RMI");         
281 
282             Class myRemoteInterfaceClass = EJBProxyUtils.getInterfaceClass(remoteInterfaceClassName, wsdlPath, jarFilesName);
283 
284             LOG.debug("Loaded remote interface: " + myRemoteInterfaceClass);
285 
286             remoteBean = EJBProxyUtils.createStatelessEJBUsingRMIClassLoader(corbaName, remoteInterfaceClassName, myRemoteInterfaceClass, orb);
287         }
288         StatelessEJBProxy ejbProxy = new StatelessEJBProxy(remoteInterfaceClassName, remoteBean, ejbInvokeClassLoader, orb);
289 
290         return ejbProxy;
291 
292     } catch (Exception ex) {
293         // TODO i18n
294         //LOG.error(ex.getMessage());
295         //throw new EJBWSDLGenerationException(ex);
296         String msg=MESSAGES.getString("EJB000920_getEJBFromCorbaname", new Object[]{ex.getMessage()});
297         LOG.error(msg,ex);
298         throw new EJBWSDLGenerationException(msg,ex);
299     }
300 }
301 
302 
303 }