1
2
3
4
5
6
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
46
47
48
49 public final class EJBProxyUtils {
50
51
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
59
60 private EJBProxyUtils() {}
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
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
95
96 File tempWSDLFile = Jbi4EjbSOAPExtensionsUtils.addSoapElements(wsdlFile);
97
98
99
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
120 tempWSDLFile.delete();
121
122
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
135 if (remoteInterfaceClassName == null) {
136 remoteInterfaceClassName
137 = EJBProxyUtils.findRemoteInterfaceCompleteClassName(classesDir.getAbsolutePath(), portTypeName);
138 }
139
140
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
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
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
186 EJBUtils.createStub(classesDir.getAbsolutePath(), remoteInterfaceClassName, jarFilesName);
187
188 return new EJBClasses(classesDir.getAbsolutePath(), remoteInterfaceClassName);
189 }
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211 public static String createEJBClasses(String wsdlPath, String remoteInterfaceClassName, String portTypeName,
212 Properties classesId, List<String> jarFilesName) throws EJBDeployException {
213
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
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
240
241
242
243
244
245
246
247
248
249
250
251
252
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
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
272 Object remoteObjectBean = getEJBFromCorbaHomeObject(home, orb, remoteInterfaceName, ejbClassLoader);
273
274
275 Thread.currentThread().setContextClassLoader(previousClassLoader);
276
277 return remoteObjectBean;
278 }
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296 @SuppressWarnings("unchecked")
297 public static Object createStatelessEJBFromJNDI(final String jndiName, Properties jndiParams, final String remoteInterfaceName, ClassLoader ejbClassLoader)
298 throws EJBDeployException {
299
300
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
331 ORB orb = home._orb();
332
333 LOG.info("EJB000911_ORB_found", new Object[]{orb});
334
335 Object remoteObjectBean = getEJBFromCorbaHomeObject(home, orb, remoteInterfaceName, ejbClassLoader);
336
337
338 Thread.currentThread().setContextClassLoader(previousClassLoader);
339
340 return remoteObjectBean;
341 }
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
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
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412 @SuppressWarnings("unchecked")
413 public static Object getEJBFromCorbaHomeObject(org.omg.CORBA.Object home, ORB orb,
414 String remoteInterfaceName, ClassLoader ejbClassLoader) throws EJBDeployException {
415
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
441 Object remoteObjectBean = PortableRemoteObject.narrow(reference, remoteClass);
442 return remoteObjectBean;
443 }
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462 @SuppressWarnings("unchecked")
463 public static Class getInterfaceClass(String remoteInterfaceClassName, String wsdlPath, List<String> jarFilesName) throws EJBDeployException {
464
465
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
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
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
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
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
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558 @SuppressWarnings("unchecked")
559 public static Object createStatelessEJBUsingRMIClassLoader(final String corbaname, final String remoteInterfaceName, Class myRemoteInterfaceClass, ORB orb)
560 throws EJBDeployException {
561
562
563
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
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
592 Object remoteObjectBean = PortableRemoteObject.narrow(reference, myRemoteInterfaceClass);
593 LOG.debug("remoteObjectBean found: " + remoteObjectBean);
594
595
596
597
598 return remoteObjectBean;
599 }
600
601
602
603
604
605
606
607
608
609
610 public static String getInterfaceId(String completeClassName) {
611 String id = "RMI:" + completeClassName + ":0000000000000000";
612 return id;
613 }
614
615
616
617
618
619
620
621
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
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651 public static Object invokeMethod(Object remoteBean, Method method, Object[] params, ClassLoader ejbClassLoader, ORB orb)
652 throws EJBInvokeException, IllegalAccessException, InvocationTargetException {
653
654
655 LOG.info("Invoking using ORB class: " + orb.getClass().getName());
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
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
692 ClassLoader previousClassLoader = Thread.currentThread().getContextClassLoader();
693 Thread.currentThread().setContextClassLoader(ejbClassLoader);
694
695 Object returnObj = method.invoke(remoteBean, params);
696
697
698 Thread.currentThread().setContextClassLoader(previousClassLoader);
699 return returnObj;
700 }
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
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
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
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
800
801
802
803
804
805
806
807
808
809
810
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
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 }