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.configuration.InterfaceExtractorUtil;
13  import it.imolinfo.jbi4ejb.exception.ClassGenerationException;
14  import it.imolinfo.jbi4ejb.exception.EJBDeployException;
15  import it.imolinfo.jbi4ejb.exception.EJBInvokeException;
16  import it.imolinfo.jbi4ejb.jbi.Messages;
17  import it.imolinfo.jbi4ejb.jbi.wsdl.Jbi4EjbSOAPExtensionsUtils;
18  import it.imolinfo.jbi4ejb.webservice.generator.EJBUtils;
19  import it.imolinfo.jbi4ejb.webservice.generator.Util;
20  import it.imolinfo.jbi4ejb.webservice.generator.WSDLGenerator;
21  
22  import java.io.File;
23  import java.io.FileNotFoundException;
24  import java.io.IOException;
25  import java.lang.reflect.InvocationTargetException;
26  import java.lang.reflect.Method;
27  import java.net.MalformedURLException;
28  import java.rmi.RMISecurityManager;
29  import java.util.Enumeration;
30  import java.util.List;
31  import java.util.Properties;
32  
33  import javax.naming.InitialContext;
34  import javax.naming.NamingException;
35  import javax.rmi.PortableRemoteObject;
36  
37  import org.codehaus.xfire.gen.Wsdl11Generator;
38  import org.omg.CORBA.Any;
39  import org.omg.CORBA.NamedValue;
40  import org.omg.CORBA.ORB;
41  import org.omg.CORBA.Request;
42  import org.omg.CORBA.TypeCode;
43  
44  /**
45   * EJBProxy helper methods.
46   * 
47   * @author <a href="mailto:mpiraccini@imolinfo.it">Marco Piraccini</a>
48   */
49  public final class EJBProxyUtils {
50  
51      /** The logger. */
52      private static final Logger LOG
53      = LoggerFactory.getLogger(EJBProxyUtils.class);  
54      private static final Messages MESSAGES
55      = Messages.getMessages(EJBProxyUtils.class);
56      
57      /**
58       * Instantiates a new EJB proxy utils.
59       */
60      private EJBProxyUtils() {}
61                    
62      /**
63       * Creates the EJB classes.
64       * 
65       * @param classesId
66       *            The <code>Hashtable</code> with the classes uids
67       * @param jarFilesName
68       *            the jar list to compile the sources The jar list to compile
69       *            the generated sources
70       * @param wsdlFile
71       *            the wsdl File
72       * @param tempDir
73       *            the temporary directory to use
74       * @param remoteInterfaceName
75       *            the remote interface class name if null, retrives-it using the portTypeName
76       * @param portTypeName
77       *            the poert-tpye name used to get the remote interface name (if not specified). 
78       * @return the classes dir absolute path
79       * 
80       * @throws EJBDeployException
81       *             If some problem occurs
82       */
83      @SuppressWarnings("unchecked")
84      public static EJBClasses createEJBClasses(File wsdlFile, 
85              Properties classesId, File tempDir, List<String> jarFilesName, String remoteInterfaceName, String portTypeName) throws EJBDeployException {
86                 
87          if (!wsdlFile.exists()) {
88          	String msg=MESSAGES.getString("EJB000901_No_WSDL_exists", new Object[]{wsdlFile});
89              LOG.error(msg);
90              throw new EJBDeployException(msg);   
91  
92          }
93          
94          // Adds the SOAP extensions
95          // Creates a new file containing the SOAP extensions (see EJB -17).
96          File tempWSDLFile = Jbi4EjbSOAPExtensionsUtils.addSoapElements(wsdlFile);
97                
98          
99          // From the WSDL, creates the classes
100         File sourceDir = new File(tempDir, "src");
101         sourceDir.mkdir();
102         
103         LOG.debug("Generating classes from WSDL: " + tempWSDLFile.getAbsolutePath() + " in directory:" +  sourceDir.getAbsolutePath());
104         
105         Wsdl11Generator gen = new Wsdl11Generator();        
106         gen.setWsdl(tempWSDLFile.getAbsolutePath());        
107         gen.setBinding(Wsdl11Generator.JAXB);
108         gen.setOverwrite(true);
109         gen.setOutputDirectory(sourceDir.getAbsolutePath());        
110         try {            
111             gen.setExplicitAnnotation(true);
112             gen.generate();
113         } catch (Exception e) {
114         	String msg=MESSAGES.getString("EJB000902_Exception_generating_class_from_WSDL", new Object[]{e.getMessage()});
115             LOG.error(msg,e);
116             throw new EJBDeployException(msg,e);   
117 
118         } 
119         // deletes the temporary file
120         tempWSDLFile.delete();
121 
122         // Compiles the generated source
123         File classesDir = new File(tempDir, "classes");        
124         List<String> sources =  Util.findJavaSources(sourceDir.getAbsolutePath(), null);
125         try {
126             Util.compileJavaClasses(sourceDir.getAbsolutePath(), classesDir.getAbsolutePath(), sources, jarFilesName, null);
127         } catch (ClassGenerationException ex) {
128         	String msg=MESSAGES.getString("EJB000903_Exception_compiling_generated_source", new Object[]{ex.getMessage()});
129             LOG.error(msg,ex);
130             throw new EJBDeployException(msg,ex);
131         }
132        
133         String remoteInterfaceClassName  = remoteInterfaceName;
134         // Gets the remote interface class name (from the WSDL PortType), if not defined
135         if (remoteInterfaceClassName == null) {
136          remoteInterfaceClassName
137             = EJBProxyUtils.findRemoteInterfaceCompleteClassName(classesDir.getAbsolutePath(), portTypeName);
138         }
139 
140         // Adds the SUIDs to the classes
141         Enumeration classesToSerialize = classesId.keys();
142         while (classesToSerialize.hasMoreElements()) {
143             LOG.debug("Adding uid in class: " + classesToSerialize);
144             String classToSer = (String)classesToSerialize.nextElement();
145             Object serNumber = classesId.get(classToSer);
146             Long uid = null;
147             if (serNumber instanceof String) {
148                 uid = new Long((String)serNumber);
149             } else {
150                 uid = (Long)serNumber;
151             }            
152 
153             String classFileName 
154             = classesDir.getAbsolutePath() + File.separatorChar + classToSer.replace('.', File.separatorChar) + ".class";
155 
156             try {
157                 LOG.debug("Insert uid: " + uid + "in class: " + classFileName);
158                 Util.tweakSerializableDecoration(classFileName, uid);
159             } catch (ClassGenerationException e) {
160             	String msg=MESSAGES.getString("EJB000904_Exception_adding_SUIDs_to_classes", new Object[]{e.getMessage()});
161                 LOG.error(msg,e);
162                 throw new EJBDeployException(msg,e);
163             }            
164         }      
165         
166         // Adds correct the throws clause of the remote interface (adding the application exception).
167         try {
168             EJBUtils.tweakRemoteInterfaceGeneratedFromWSDL(remoteInterfaceClassName, classesDir.getAbsolutePath());
169         } catch (ClassGenerationException e) {
170         	String msg=MESSAGES.getString("EJB000905_Exception_adding_the_application_exception", new Object[]{e.getMessage()});
171             LOG.error(msg,e);
172             throw new EJBDeployException(msg,e);
173         }          
174         
175         // Adds javax.rmi.Remote to the interface and the throws RemoteException clause
176         try {
177             Util.tweakInterfaceClasses(remoteInterfaceClassName, classesDir.getAbsolutePath());
178         } catch (ClassGenerationException e) {
179         	String msg=MESSAGES.getString("EJB000906_Exception_adding_javaxRmiRemote_to_interface", new Object[]{e.getMessage()});
180             LOG.error(msg,e);
181             throw new EJBDeployException(msg,e);
182         }
183          
184         
185         // Generates the stub for the remote interface
186         EJBUtils.createStub(classesDir.getAbsolutePath(), remoteInterfaceClassName, jarFilesName);    
187         
188         return new EJBClasses(classesDir.getAbsolutePath(), remoteInterfaceClassName);
189     }   
190     
191     /**
192      * Creates the EJB classes, creating an external temp dir (for testing
193      * pourpouse).
194      * 
195      * @param wsdlPath
196      *            The complete path to the wsdl
197      * @param classesId
198      *            The <code>Hashtable</code> with the classes uids
199      * @param jarFilesName
200      *            the jar list to compile the sources The jar list to compile
201      *            the generated sources
202      * @param portTypeName
203      *            The port type name (to identify the remote interface)
204      * @param remoteInterfaceClassName
205      *            The remote interface class name
206      * @return the classes dir absolute path
207      * 
208      * @throws EJBDeployException
209      *             If some problem occurs
210      */
211         public static String createEJBClasses(String wsdlPath, String remoteInterfaceClassName, String portTypeName, 
212                 Properties classesId, List<String> jarFilesName) throws EJBDeployException {
213             // Creates the working temp dir
214             File tempDir;
215             try {
216                 tempDir = EJBUtils.createTempDir();
217             } catch (IOException e) {
218             	String msg=MESSAGES.getString("EJB000907_Exception_creating_working_temp_dir", new Object[]{e.getMessage()});
219                 LOG.error(msg,e);
220                 throw new EJBDeployException(msg,e);
221             }
222             
223             // Tests if the WSDL exists
224             File wsdlFile = new File(wsdlPath);
225 
226             if (!wsdlFile.exists()) {
227             	String msg=MESSAGES.getString("EJB000901_No_WSDL_exists", new Object[]{wsdlFile});
228                 LOG.error(msg);
229                 throw new EJBDeployException(msg);  
230             }                                  
231                           
232             EJBClasses ejbClasses =  createEJBClasses(wsdlFile, classesId, tempDir, jarFilesName, remoteInterfaceClassName, portTypeName);
233             
234             return ejbClasses.getEjbClassesPath();    
235         }    
236         
237 
238     /**
239      * Creates the stateless EJB and the stub.
240      * 
241      * @param corbaname
242      *            The corbaname to locate the <code>EJBHome</code>
243      * @param remoteInterfaceName
244      *            The remote interface class name
245      * @param ejbClassLoader
246      *            The classloader to use
247      * @param orb
248      *            The ORB
249      * @return the object The stateless EJB remote stub
250      * 
251      * @throws EJBDeployException
252      *             If some problem occurs
253      */
254     @SuppressWarnings("unchecked")
255     public static Object createStatelessEJBFromCorbaName(final String corbaname, final String remoteInterfaceName,
256                 final ClassLoader ejbClassLoader, ORB orb)
257     throws EJBDeployException {
258 
259         // Saves the previous classloader
260         ClassLoader previousClassLoader = Thread.currentThread().getContextClassLoader(); 
261         Thread.currentThread().setContextClassLoader(ejbClassLoader);                    
262                              
263         org.omg.CORBA.Object home = orb.string_to_object(corbaname);
264         
265         if (home == null) {
266             String msg=MESSAGES.getString("EJB000908_Null_object_retrieved_with_corbaname", new Object[]{corbaname});
267             LOG.error(msg);
268             throw new EJBDeployException(msg); 
269         }
270                        
271         // Retrieve the EJB reference
272         Object remoteObjectBean = getEJBFromCorbaHomeObject(home, orb, remoteInterfaceName, ejbClassLoader);
273 
274         // Sets back the prevoius classloader
275         Thread.currentThread().setContextClassLoader(previousClassLoader);
276 
277         return remoteObjectBean;
278     }
279     
280     /**
281      * Creates the stateless EJB, using JNDI to lookup the home object.
282      * 
283      * @param remoteInterfaceName
284      *            The remote interface class name
285      * @param jndiName
286      *            The Jndi name
287      * @param jndiParams
288      *            The Jndi params
289      * @param ejbClassLoader
290      *            The ejb class loader
291      * @return the object The stateless EJB remote stub
292      * 
293      * @throws EJBDeployException
294      *             If some problem occurs
295      */
296     @SuppressWarnings("unchecked")
297     public static Object createStatelessEJBFromJNDI(final String jndiName, Properties jndiParams, final String remoteInterfaceName, ClassLoader ejbClassLoader)
298     throws EJBDeployException {
299         
300         // Saves the previuos classloader
301         ClassLoader previousClassLoader = Thread.currentThread().getContextClassLoader(); 
302         Thread.currentThread().setContextClassLoader(ejbClassLoader);
303 
304         InitialContext jndiContext = null;
305         try {
306             if (jndiParams == null) {            
307                 jndiContext = new InitialContext();            
308             } else {            
309                 jndiContext = new InitialContext(jndiParams);            
310             }
311         } catch (NamingException e) {
312         	String msg=MESSAGES.getString("EJB000909_createStatelessEJBFromJNDI", new Object[]{e.getMessage()});
313             LOG.error(msg,e);
314             throw new EJBDeployException(msg,e);
315         }
316         
317         LOG.debug("jndiContext retrieved:" + jndiContext);
318 
319         org.omg.CORBA.portable.ObjectImpl home = null;
320         try {
321             home = (org.omg.CORBA.portable.ObjectImpl)jndiContext.lookup(jndiName);
322         } catch (NamingException e) {
323         	String msg=MESSAGES.getString("EJB000909_createStatelessEJBFromJNDI", new Object[]{e.getMessage()});
324             LOG.error(msg,e);
325             throw new EJBDeployException(msg,e);
326         }
327        
328         LOG.info("EJB000910_Home_object_found", new Object[]{home});
329 
330         // Gets the ORB from the org.omg.CORBA.portable.ObjectImpl object
331         ORB orb = home._orb();               
332 
333         LOG.info("EJB000911_ORB_found", new Object[]{orb});
334         // Retrieve the EJB reference
335         Object remoteObjectBean = getEJBFromCorbaHomeObject(home, orb, remoteInterfaceName, ejbClassLoader);        
336 
337         // Sets back the prevoius classloader
338         Thread.currentThread().setContextClassLoader(previousClassLoader);
339         
340         return remoteObjectBean;
341     }    
342     
343     /**
344      * Creates the stateless EJB, using JNDI to lookup the home object.
345      * 
346      * @param remoteInterfaceName
347      *            The remote interface class name
348      * @param jndiName
349      *            The jndi name
350      * @param jndiParams
351      *            The jndi params
352      * @param ejbClassLoader
353      *            The classloader to use
354      * @return the object The stateless EJB remote stub
355      * 
356      * @throws EJBDeployException
357      *             If some problem occurs
358      */
359     @SuppressWarnings("unchecked")
360     public static org.omg.CORBA.portable.ObjectImpl createStatelessHomeFromJNDI(final String jndiName, Properties jndiParams, final String remoteInterfaceName, ClassLoader ejbClassLoader)
361     throws EJBDeployException {
362         
363 
364 
365         InitialContext jndiContext = null;
366         try {
367             if (jndiParams == null) {            
368                 jndiContext = new InitialContext();            
369             } else {            
370                 jndiContext = new InitialContext(jndiParams);            
371             }
372         } catch (NamingException e) {
373         	String msg=MESSAGES.getString("EJB000912_createStatelessHomeFromJNDI", new Object[]{e.getMessage()});
374             LOG.error(msg,e);
375             throw new EJBDeployException(msg,e);
376         }
377         
378         LOG.debug("jndiContext retrieved:" + jndiContext);
379 
380         org.omg.CORBA.portable.ObjectImpl home = null;
381         try {
382             home = (org.omg.CORBA.portable.ObjectImpl)jndiContext.lookup(jndiName);
383         } catch (NamingException e) {
384         	String msg=MESSAGES.getString("EJB000912_createStatelessHomeFromJNDI", new Object[]{e.getMessage()});
385             LOG.error(msg,e);
386             throw new EJBDeployException(msg,e);
387         }
388         
389         LOG.info("EJB000910_Home_object_found", new Object[]{home});
390         
391         return home;
392     }     
393     
394     
395     /**
396      * Gets the EJB from corba home object.
397      * 
398      * @param home
399      *            The CORBA home remote object
400      * @param orb
401      *            The ORB
402      * @param remoteInterfaceName
403      *            The remote interface name
404      * @param ejbClassLoader
405      *            The classloader to use to get the remomte interface class
406      * 
407      * @return the EJB from corba home object
408      * 
409      * @throws EJBDeployException
410      *              if some problem occurs 
411      */
412     @SuppressWarnings("unchecked")
413     public static Object getEJBFromCorbaHomeObject(org.omg.CORBA.Object home, ORB orb, 
414                 String remoteInterfaceName, ClassLoader ejbClassLoader) throws EJBDeployException {
415         // call create method and obtain a pointer to real stateless bean        
416         Any result = orb.create_any();
417         
418         NamedValue resultVal = orb.create_named_value("result", result, org.omg.CORBA.ARG_OUT.value);
419         Request createRequest = home._create_request(null, "create", orb.create_list(0), resultVal);
420 
421         String interfaceName = EJBProxyUtils.getInterfaceName(remoteInterfaceName);
422         String interfaceId = EJBProxyUtils.getInterfaceId(remoteInterfaceName);
423         TypeCode remoteInterfaceType = orb.create_interface_tc(interfaceId, interfaceName);
424         
425         createRequest.set_return_type(remoteInterfaceType);
426         createRequest.invoke();
427         result = createRequest.return_value();
428         org.omg.CORBA.Object reference=result.extract_Object();
429 
430         Class remoteClass = null;
431         try {
432             remoteClass = ejbClassLoader.loadClass(remoteInterfaceName);
433         } catch (ClassNotFoundException e) {
434         	String msg=MESSAGES.getString("EJB000913_getEJBFromCorbaHomeObject", new Object[]{e.getMessage()});
435             LOG.error(msg,e);
436             throw new EJBDeployException(msg,e);
437         }
438         LOG.debug("Remote class: " + remoteClass);
439 
440         // Narrow
441         Object remoteObjectBean = PortableRemoteObject.narrow(reference, remoteClass);        
442         return remoteObjectBean;         
443     }
444 
445     /**
446      * Creates the Class interface (to narrow in the dynamic case). NO STUB IS
447      * CREATED
448      * 
449      * @param wsdlPath
450      *            The complete path to the wsdl
451      * @param jarFilesName
452      *            the jar list to compile the sources The jar list to compile
453      *            the generated sources
454      * @param remoteInterfaceClassName
455      *            the remote interface class name
456      * 
457      * @return the string
458      * 
459      * @throws EJBDeployException
460      *             If some problem occurs
461      */
462     @SuppressWarnings("unchecked")
463     public static Class getInterfaceClass(String remoteInterfaceClassName, String wsdlPath, List<String> jarFilesName) throws EJBDeployException {
464 
465         // Creates the working temp dir
466         File tempDir;
467         try {
468             tempDir = EJBUtils.createTempDir();
469         } catch (IOException e) {
470         	String msg=MESSAGES.getString("EJB000914_getInterfaceClass", new Object[]{e.getMessage()});
471             LOG.error(msg,e);
472             throw new EJBDeployException(msg,e);
473         }
474 
475         // Tests if the WSDL exists
476         File wsdlFile = new File(wsdlPath);
477 
478         if (!wsdlFile.exists()) {
479         	String msg=MESSAGES.getString("EJB000901_No_WSDL_exists", new Object[]{wsdlFile});
480             LOG.error(msg);
481             throw new EJBDeployException(msg); 
482         }
483 
484         // From the WSDL, creates the classes
485         File sourceDir = new File(tempDir, "src");
486         sourceDir.mkdir();
487         Wsdl11Generator gen = new Wsdl11Generator();        
488         gen.setWsdl(wsdlFile.getAbsolutePath());
489 
490         gen.setOutputDirectory(sourceDir.getAbsolutePath());        
491         try {
492             gen.generate();
493         } catch (Exception e) {
494         	String msg=MESSAGES.getString("EJB000914_getInterfaceClass", new Object[]{e.getMessage()});
495             LOG.error(msg,e);
496             throw new EJBDeployException(msg,e);
497         }                        
498 
499         // Compiles the generated source
500         File classesDir = new File(tempDir, "classes");        
501 
502         List<String> sources =  Util.findJavaSources(sourceDir.getAbsolutePath(), null);
503 
504         try {
505             Util.compileJavaClasses(sourceDir.getAbsolutePath(), classesDir.getAbsolutePath(), sources, jarFilesName, null);
506         } catch (ClassGenerationException ex) {
507         	String msg=MESSAGES.getString("EJB000914_getInterfaceClass", new Object[]{ex.getMessage()});
508             LOG.error(msg,ex);
509             throw new EJBDeployException(msg,ex);
510         }
511 
512         // Adds javax.rmi.Remote to the interface and generates the stubs
513         try {
514             Util.tweakInterfaceClasses(remoteInterfaceClassName, classesDir.getAbsolutePath());
515         } catch (ClassGenerationException e) {
516         	String msg=MESSAGES.getString("EJB000914_getInterfaceClass", new Object[]{e.getMessage()});
517             LOG.error(msg,e);
518             throw new EJBDeployException(msg,e);
519         }               
520 
521         ClassLoader ejbClassesClassLoader = null;
522         try {
523             ejbClassesClassLoader = Util.getURLClassLoader(classesDir.getAbsolutePath());
524         } catch (MalformedURLException e) {
525         	String msg=MESSAGES.getString("EJB000914_getInterfaceClass", new Object[]{e.getMessage()});
526             LOG.error(msg,e);
527             throw new EJBDeployException(msg,e);
528         }
529         Class myRemoteClass = null;
530         try {
531             myRemoteClass = ejbClassesClassLoader.loadClass(remoteInterfaceClassName);
532         } catch (ClassNotFoundException e) {
533         	String msg=MESSAGES.getString("EJB000914_getInterfaceClass", new Object[]{e.getMessage()});
534             LOG.error(msg,e);
535             throw new EJBDeployException(msg,e);
536         }
537         return myRemoteClass;
538     }    
539 
540     /**
541      * Creates the stateless EJB and try to get a reference using the
542      * <code>RMIClassLoader</code> and trying to dinamically load the stub and
543      * the paramete/return classes.
544      * 
545      * @param corbaname
546      *            The corbaname to locate the <code>EJBHome</code>
547      * @param remoteInterfaceName
548      *            The remote interface class name
549      * @param myRemoteInterfaceClass
550      *            The remote interface class
551      * @param orb
552      *              The ORB
553      * @return the object The stateless EJB remote stub
554      * 
555      * @throws EJBDeployException
556      *             If some problem occurs
557      */
558     @SuppressWarnings("unchecked")
559     public static Object createStatelessEJBUsingRMIClassLoader(final String corbaname, final String remoteInterfaceName, Class myRemoteInterfaceClass,  ORB orb)
560     throws EJBDeployException {
561 
562         // Sets the RMISecurityManager
563         // SecurityManager previousSystemSecurityManager = System.getSecurityManager();
564         if (System.getProperty("java.security.policy") != null) {
565             LOG.debug("Using security policy: " + System.getProperty("java.security.policy"));    
566         }        
567         RMISecurityManager rmiSecurityManager = new RMISecurityManager();        
568         System.setSecurityManager(rmiSecurityManager);
569 
570         org.omg.CORBA.Object ejbHome = orb.string_to_object(corbaname);
571 
572         LOG.debug("ejbHome: " + ejbHome);
573         LOG.debug("ejbHome.class: " + ejbHome.getClass());               
574 
575         // call create method and obtain a pointer to real stateless bean
576         Any result = orb.create_any();
577         LOG.debug("result: "+result);
578         NamedValue resultVal = orb.create_named_value("result", result, org.omg.CORBA.ARG_OUT.value);
579         Request createRequest = ejbHome._create_request(null, "create", orb.create_list(0), resultVal);
580 
581         String interfaceName = EJBProxyUtils.getInterfaceName(remoteInterfaceName);
582         String interfaceId = EJBProxyUtils.getInterfaceId(remoteInterfaceName);
583         TypeCode remoteInterfaceType = orb.create_interface_tc(interfaceId, interfaceName);
584         createRequest.set_return_type(remoteInterfaceType);
585         createRequest.invoke();
586         result = createRequest.return_value();
587         org.omg.CORBA.Object reference=result.extract_Object();        
588         LOG.debug("reference.class: " + reference.getClass());              
589         LOG.debug("GetCodebase: " + javax.rmi.CORBA.Util.getCodebase(ejbHome.getClass()));
590 
591         // The stub now should be dynamically downloaded...
592         Object remoteObjectBean = PortableRemoteObject.narrow(reference, myRemoteInterfaceClass);        
593         LOG.debug("remoteObjectBean found: " + remoteObjectBean);      
594 
595         // sets the prervious security manager
596         // System.setSecurityManager(previousSystemSecurityManager);
597 
598         return remoteObjectBean;
599     }    
600 
601 
602     /**
603      * Gets the interface id.
604      * 
605      * @param completeClassName
606      *            The class name
607      * 
608      * @return the interface id
609      */
610     public static String getInterfaceId(String completeClassName) {
611         String id = "RMI:" + completeClassName + ":0000000000000000";
612         return id;
613     }
614 
615     /**
616      * Gets the interface name.
617      * 
618      * @param completeClassName
619      *            The class name
620      * 
621      * @return the interface name
622      */
623     public static String getInterfaceName(String completeClassName) {
624         int lastDot = completeClassName.lastIndexOf(".");
625         String className = completeClassName.substring(lastDot + 1, completeClassName.length());
626         return className;
627     }
628     
629     /**
630      * Invoke method on the object (the proxied EJB).
631      * 
632      * @param remoteBean
633      *          The ejb remoteobject
634      * @param method
635      *          The method to call
636      * @param params
637      *          The method params
638      * @param ejbClassLoader
639      *          The classloader to use
640      * @param orb
641      *          The orb
642      * @return the object
643      * 
644      * @throws EJBInvokeException
645      *          if some problem in the EJB invocation occurs
646      * @throws InvocationTargetException 
647      *              If an exception is thrown in the invokation
648      * @throws IllegalAccessException 
649      *              If there are access problems
650      */
651     public static Object invokeMethod(Object remoteBean, Method method, Object[] params, ClassLoader ejbClassLoader, ORB orb) 
652     throws EJBInvokeException, IllegalAccessException, InvocationTargetException {
653         // org.omg.CosTransactions.Current ts_current = null;        
654         
655         LOG.info("Invoking using ORB class: " + orb.getClass().getName());
656                 
657         // OTSManager.setORB(myORB);
658         // OTSManager.setPOA(myOA);         
659         // Current current = OTSManager.get_current();                              
660         //        try {            
661         //            org.omg.CORBA.Object test =  orb.resolve_initial_references("TransactionFactory");
662         //            org.omg.CORBA.Object test =  orb.string_to_object("TransactionFactory");
663         //            System.err.println("*************");
664         //            System.err.println(test);            
665         //            System.err.println("*************");
666         //            ts_current = 
667         //                org.omg.CosTransactions.CurrentHelper.narrow(test);
668         //        }
669         //        catch (Exception e) {
670         //            e.printStackTrace();
671         //          return null;
672         //        }
673 
674         // Begin transaction
675         //        try {
676         //            // ts_current.begin();                                              
677         //            current.begin();   
678         //            System.err.println("Transaction Name: " + current.get_transaction_name());            
679         //        }
680         //        catch (Exception e) {
681         //          e.printStackTrace();
682         //        }        
683 
684 
685         if (remoteBean == null) {
686             String msg = "The bean is null";
687             LOG.debug(msg);
688             throw new EJBInvokeException(msg);
689         }
690 
691         // Saves the previuos classloader
692         ClassLoader previousClassLoader = Thread.currentThread().getContextClassLoader(); 
693         Thread.currentThread().setContextClassLoader(ejbClassLoader);
694                 
695         Object returnObj = method.invoke(remoteBean, params);     
696         
697         // Sets back the previous classloader
698         Thread.currentThread().setContextClassLoader(previousClassLoader);        
699         return returnObj;                       
700     }
701 
702     /**
703      * Invoke method on the EJB.
704      * 
705      * @param methodName
706      *            The method to invoke
707      * @param params
708      *            The params for the method invocation
709      * @param remoteBean
710      *            The remote bean object
711      * @param ejbClassLoader
712      *            The classloader to use
713      * @param orb
714      *            The orb
715      * 
716      * @return the object
717      * 
718      * @throws EJBInvokeException
719      *             If some problems occurs in method invocation
720      * @throws InvocationTargetException 
721      *              If the call throws an exception
722      * @throws IllegalAccessException 
723      *              Problem in accesing object
724      */    
725     public static Object invokeMethod(Object remoteBean, String methodName, Object[] params, ClassLoader ejbClassLoader, ORB orb) throws EJBInvokeException, IllegalAccessException, InvocationTargetException {
726                 
727         Method method = getMethodFromName(remoteBean, methodName, params);        
728         
729         return invokeMethod(remoteBean, method, params, ejbClassLoader, orb);
730     }          
731 
732 //    /**
733 //     *  Return a string representation of the given status code.
734 //     */
735 //    private static String toString(int status) {
736 //      switch (status) {
737 //        case javax.transaction.Status.STATUS_PREPARING:
738 //          return "STATUS_PREPARING";
739 //        case javax.transaction.Status.STATUS_PREPARED:
740 //          return "STATUS_PREPARED";
741 //        case javax.transaction.Status.STATUS_ROLLING_BACK:
742 //          return "STATUS_ROLLING_BACK";
743 //        case javax.transaction.Status.STATUS_ROLLEDBACK:
744 //          return "STATUS_ROLLEDBACK";
745 //        case javax.transaction.Status.STATUS_COMMITTING:
746 //          return "STATUS_COMMITING";
747 //        case javax.transaction.Status.STATUS_COMMITTED:
748 //          return "STATUS_COMMITED";
749 //        case javax.transaction.Status.STATUS_NO_TRANSACTION:
750 //          return "STATUS_NO_TRANSACTION";
751 //        case javax.transaction.Status.STATUS_UNKNOWN:
752 //          return "STATUS_UNKNOWN";
753 //        case javax.transaction.Status.STATUS_MARKED_ROLLBACK:
754 //          return "STATUS_MARKED_ROLLBACK";
755 //        case javax.transaction.Status.STATUS_ACTIVE:
756 //          return "STATUS_ACTIVE";
757 //        default:
758 //          return "STATUS_UNKNOWN(" + status + ")";
759 //      }
760 //    }
761 
762     
763  /**
764  * Gets the <code>Method</code> from the name.
765  * 
766  * @param remoteBean
767  *          The object that defines the method
768  * @param methodName
769  *          The method name
770  * @param params
771  *          The method params
772  * @return the method from name
773  * 
774  * @throws EJBInvokeException
775  *          If some problem occurs in getting the method name
776  */
777 @SuppressWarnings("unchecked")
778 public static Method getMethodFromName(Object remoteBean, String methodName, Object[] params) 
779         throws EJBInvokeException {
780         Class[] parameterTypes = new Class[params.length];
781         for (int i = 0; i < params.length; i++) {
782             parameterTypes[i] = params[i].getClass();
783         }
784                 
785         Method method = null;                
786         try {
787             Class objClass = remoteBean.getClass();
788             method = objClass.getMethod(methodName, parameterTypes);            
789         } catch (NoSuchMethodException e) {
790         	String msg=MESSAGES.getString("EJB000915_getMethodFromName", new Object[]{e.getMessage()});
791             LOG.error(msg,e);
792             throw new EJBInvokeException(e);
793         }
794         return method;
795     }
796     
797     
798     /**
799      * Retrieve the remote interface class name from the class name (without
800      * package). The class name must be unique for the whole package.
801      * 
802      * @param sourcesDir
803      *          Directory where to look for the class name
804      * @param className
805      *          The class name
806      * @return
807      *          The class name (without the package)
808      * 
809      * @throws EJBDeployException
810      *          If some proble occurs in classes search
811      */
812     public static String findRemoteInterfaceCompleteClassName(String sourcesDir, String className) throws EJBDeployException {
813         LOG.debug("Looking for class: " + className + " in directory: " + sourcesDir);
814         List<File> files = Util.findFilesFromSourceDirectory(sourcesDir, className + ".class");
815         
816         if (files.size() != 1) {
817         	String msg=MESSAGES.getString("EJB000916_Retrieve_remote_interface_class_name", new Object[]{files.size()}, 
818         			new Object[]{className}, new Object[]{sourcesDir});
819             LOG.error(msg);
820             throw new EJBDeployException(msg);
821         } else {
822             LOG.debug("Found class: " + files.get(0).getAbsolutePath());
823             LOG.debug("The sources directory is: " + sourcesDir);
824         }
825         File myClassFile = files.get(0);
826         String myClassFileAbsolutePath = myClassFile.getAbsolutePath();                                 
827         String relativeFileNamePath = myClassFileAbsolutePath.substring(sourcesDir.length() + 1, myClassFileAbsolutePath.length());       
828         relativeFileNamePath = relativeFileNamePath.replace(File.separatorChar, '.');
829         // Removes the ".class"
830         relativeFileNamePath = relativeFileNamePath.substring(0, relativeFileNamePath.length() - ".class".length());
831         LOG.debug("The remote interface class name is: " + relativeFileNamePath);
832         return relativeFileNamePath;        
833     }
834 
835 }