|
1 |
| |
|
2 |
| |
|
3 |
| |
|
4 |
| |
|
5 |
| |
|
6 |
| |
|
7 |
| package jtr.enterprise; |
|
8 |
| |
|
9 |
| import java.lang.reflect.Method; |
|
10 |
| import java.lang.reflect.InvocationTargetException; |
|
11 |
| import java.util.Hashtable; |
|
12 |
| |
|
13 |
| import javax.naming.Context; |
|
14 |
| import javax.naming.NamingException; |
|
15 |
| import javax.naming.InitialContext; |
|
16 |
| import javax.rmi.PortableRemoteObject; |
|
17 |
| |
|
18 |
| import jtr.config.enterprise.EnterpriseConfig; |
|
19 |
| import org.apache.log4j.Logger; |
|
20 |
| |
|
21 |
| |
|
22 |
| |
|
23 |
| |
|
24 |
| |
|
25 |
| |
|
26 |
| |
|
27 |
| |
|
28 |
| public class ServiceLocator { |
|
29 |
| |
|
30 |
| |
|
31 |
| |
|
32 |
| |
|
33 |
| |
|
34 |
| |
|
35 |
| |
|
36 |
| |
|
37 |
| |
|
38 |
| |
|
39 |
| |
|
40 |
| |
|
41 |
| |
|
42 |
| |
|
43 |
| |
|
44 |
| |
|
45 |
| |
|
46 |
0
| @Deprecated
|
|
47 |
| public static <T> T getEJBObject(String ejbName, Class clazz, Hashtable environment) throws LocatorException { |
|
48 |
0
| Object ref = null;
|
|
49 |
0
| try {
|
|
50 |
0
| logger.debug("Getting remote object " + ejbName + "...");
|
|
51 |
0
| ref = getInitialContext(environment).lookup(ejbName);
|
|
52 |
0
| ref = PortableRemoteObject.narrow(ref, clazz);
|
|
53 |
0
| T newRef = (T) invokeCreate(ref);
|
|
54 |
0
| logger.debug("... done");
|
|
55 |
0
| if(newRef==null)
|
|
56 |
0
| return (T)ref;
|
|
57 |
| else |
|
58 |
0
| return newRef;
|
|
59 |
| } catch (Exception e) { |
|
60 |
0
| String msg = "Remote object location failed for " + ejbName;
|
|
61 |
0
| logger.fatal(msg, e);
|
|
62 |
0
| throw new LocatorException(msg, e);
|
|
63 |
| } |
|
64 |
| } |
|
65 |
| |
|
66 |
| |
|
67 |
| |
|
68 |
| |
|
69 |
| |
|
70 |
| |
|
71 |
| |
|
72 |
| |
|
73 |
| |
|
74 |
| |
|
75 |
| |
|
76 |
| |
|
77 |
| |
|
78 |
| |
|
79 |
| |
|
80 |
| |
|
81 |
| |
|
82 |
19994
| public static <T> T getEJBObject(String ejbName, Class clazz, EnterpriseConfig environment) throws LocatorException {
|
|
83 |
19994
| Object ref = null;
|
|
84 |
19998
| try {
|
|
85 |
19998
| logger.debug("Getting remote object " + ejbName + "...");
|
|
86 |
19998
| ref = getInitialContext(environment.toHashtable()).lookup(ejbName);
|
|
87 |
19994
| ref = PortableRemoteObject.narrow(ref, clazz);
|
|
88 |
19992
| T newRef = (T) invokeCreate(ref);
|
|
89 |
19996
| logger.debug("... done");
|
|
90 |
19995
| if(newRef==null)
|
|
91 |
19995
| return (T)ref;
|
|
92 |
| else |
|
93 |
0
| return newRef;
|
|
94 |
| } catch (Exception e) { |
|
95 |
4
| String msg = "Remote object location failed for " + ejbName;
|
|
96 |
4
| logger.fatal(msg, e);
|
|
97 |
4
| throw new LocatorException(msg, e);
|
|
98 |
| } |
|
99 |
| } |
|
100 |
| |
|
101 |
19999
| private static Context getInitialContext(Hashtable env) throws NamingException {
|
|
102 |
20000
| return new InitialContext(env);
|
|
103 |
| } |
|
104 |
| |
|
105 |
19993
| private static <T> T invokeCreate(Object obj) throws SecurityException, InvocationTargetException, IllegalArgumentException, IllegalAccessException {
|
|
106 |
19994
| try {
|
|
107 |
19994
| Method createMethod = obj.getClass().getDeclaredMethod(CREATE_METHOD_SIGNATURE, new Class[0]);
|
|
108 |
0
| return (T) createMethod.invoke(obj, new Object[0]);
|
|
109 |
| } catch(NoSuchMethodException e) { |
|
110 |
19984
| String msg = "The provided instance of type "+obj.getClass()+", does not have a "+CREATE_METHOD_SIGNATURE+" method.\n"+
|
|
111 |
| "Maybe this is because it is already a remote interface obtained looking up an EJB3 bean."+ |
|
112 |
| "Follows the caugth exception just for your convenience:\n"; |
|
113 |
19992
| logger.warn(msg,e);
|
|
114 |
19996
| return null;
|
|
115 |
| } |
|
116 |
| } |
|
117 |
| |
|
118 |
| private static String CREATE_METHOD_SIGNATURE = "create"; |
|
119 |
| private static Logger logger = Logger.getLogger(ServiceLocator.class); |
|
120 |
| } |