1 |
| package org.jboss.cache.transaction; |
2 |
| |
3 |
| import org.apache.commons.logging.Log; |
4 |
| import org.apache.commons.logging.LogFactory; |
5 |
| |
6 |
| import javax.naming.InitialContext; |
7 |
| import javax.naming.NamingException; |
8 |
| import javax.transaction.TransactionManager; |
9 |
| import java.lang.reflect.Method; |
10 |
| |
11 |
| |
12 |
| |
13 |
| |
14 |
| |
15 |
| |
16 |
| |
17 |
| |
18 |
| |
19 |
| |
20 |
| |
21 |
| |
22 |
| |
23 |
| |
24 |
| |
25 |
| |
26 |
| |
27 |
| |
28 |
| |
29 |
| |
30 |
| public class GenericTransactionManagerLookup implements TransactionManagerLookup |
31 |
| { |
32 |
| |
33 |
| private static Log log = LogFactory.getLog(GenericTransactionManagerLookup.class); |
34 |
| |
35 |
| |
36 |
| |
37 |
| |
38 |
| private static boolean lookupDone = false; |
39 |
| |
40 |
| |
41 |
| |
42 |
| |
43 |
| private static boolean lookupFailed = false; |
44 |
| |
45 |
| |
46 |
| |
47 |
| |
48 |
| private static TransactionManager tm = null; |
49 |
| |
50 |
| |
51 |
| |
52 |
| |
53 |
| private static String[][] knownJNDIManagers = |
54 |
| { |
55 |
| {"java:/TransactionManager", "JBoss, JRun4"}, |
56 |
| {"java:comp/TransactionManager", "Resin 3.x"}, |
57 |
| {"java:pm/TransactionManager", "Borland, Sun"}, |
58 |
| {"java:appserver/TransactionManager", "Sun Glassfish"}, |
59 |
| {"javax.transaction.TransactionManager", "BEA WebLogic"}, |
60 |
| {"java:comp/UserTransaction", "Resin, Orion, JOnAS (JOTM)"}, |
61 |
| }; |
62 |
| |
63 |
| |
64 |
| |
65 |
| |
66 |
| private static final String WS_FACTORY_CLASS_5_1 = "com.ibm.ws.Transaction.TransactionManagerFactory"; |
67 |
| |
68 |
| |
69 |
| |
70 |
| |
71 |
| private static final String WS_FACTORY_CLASS_5_0 = "com.ibm.ejs.jts.jta.TransactionManagerFactory"; |
72 |
| |
73 |
| |
74 |
| |
75 |
| |
76 |
| private static final String WS_FACTORY_CLASS_4 = "com.ibm.ejs.jts.jta.JTSXA"; |
77 |
| |
78 |
| |
79 |
| |
80 |
| |
81 |
| |
82 |
| |
83 |
538
| public TransactionManager getTransactionManager()
|
84 |
| { |
85 |
538
| if (!lookupDone)
|
86 |
538
| doLookups();
|
87 |
538
| if (tm != null)
|
88 |
474
| return tm;
|
89 |
64
| if (lookupFailed)
|
90 |
| { |
91 |
| |
92 |
64
| tm = DummyTransactionManager.getInstance();
|
93 |
64
| log.warn("Falling back to DummyTransactionManager from JBossCache");
|
94 |
| } |
95 |
64
| return tm;
|
96 |
| } |
97 |
| |
98 |
| |
99 |
| |
100 |
| |
101 |
538
| private static void doLookups()
|
102 |
| { |
103 |
538
| if (lookupFailed)
|
104 |
473
| return;
|
105 |
65
| InitialContext ctx;
|
106 |
65
| try
|
107 |
| { |
108 |
65
| ctx = new InitialContext();
|
109 |
| } |
110 |
| catch (NamingException e) |
111 |
| { |
112 |
0
| log.error("Failed creating initial JNDI context", e);
|
113 |
0
| lookupFailed = true;
|
114 |
0
| return;
|
115 |
| } |
116 |
| |
117 |
65
| for (String[] knownJNDIManager : knownJNDIManagers)
|
118 |
| { |
119 |
385
| Object jndiObject;
|
120 |
385
| try
|
121 |
| { |
122 |
385
| if (log.isDebugEnabled())
|
123 |
0
| log.debug("Trying to lookup TransactionManager for " + knownJNDIManager[1]);
|
124 |
385
| jndiObject = ctx.lookup(knownJNDIManager[0]);
|
125 |
| } |
126 |
| catch (NamingException e) |
127 |
| { |
128 |
384
| log.debug("Failed to perform a lookup for [" + knownJNDIManager[0] + " (" + knownJNDIManager[1]
|
129 |
| + ")]"); |
130 |
384
| continue;
|
131 |
| } |
132 |
1
| if (jndiObject instanceof TransactionManager)
|
133 |
| { |
134 |
1
| tm = (TransactionManager) jndiObject;
|
135 |
1
| log.debug("Found TransactionManager for " + knownJNDIManager[1]);
|
136 |
1
| return;
|
137 |
| } |
138 |
| } |
139 |
| |
140 |
64
| Class clazz;
|
141 |
64
| try
|
142 |
| { |
143 |
64
| log.debug("Trying WebSphere 5.1: " + WS_FACTORY_CLASS_5_1);
|
144 |
64
| clazz = Class.forName(WS_FACTORY_CLASS_5_1);
|
145 |
0
| log.debug("Found WebSphere 5.1: " + WS_FACTORY_CLASS_5_1);
|
146 |
| } |
147 |
| catch (ClassNotFoundException ex) |
148 |
| { |
149 |
64
| try
|
150 |
| { |
151 |
64
| log.debug("Trying WebSphere 5.0: " + WS_FACTORY_CLASS_5_0);
|
152 |
64
| clazz = Class.forName(WS_FACTORY_CLASS_5_0);
|
153 |
0
| log.debug("Found WebSphere 5.0: " + WS_FACTORY_CLASS_5_0);
|
154 |
| } |
155 |
| catch (ClassNotFoundException ex2) |
156 |
| { |
157 |
64
| try
|
158 |
| { |
159 |
64
| log.debug("Trying WebSphere 4: " + WS_FACTORY_CLASS_4);
|
160 |
64
| clazz = Class.forName(WS_FACTORY_CLASS_4);
|
161 |
0
| log.debug("Found WebSphere 4: " + WS_FACTORY_CLASS_4);
|
162 |
| } |
163 |
| catch (ClassNotFoundException ex3) |
164 |
| { |
165 |
64
| log.debug("Couldn't find any WebSphere TransactionManager factory class, neither for WebSphere version 5.1 nor 5.0 nor 4");
|
166 |
64
| lookupFailed = true;
|
167 |
64
| return;
|
168 |
| } |
169 |
| } |
170 |
| } |
171 |
0
| try
|
172 |
| { |
173 |
0
| Class[] signature = null;
|
174 |
0
| Object[] args = null;
|
175 |
0
| Method method = clazz.getMethod("getTransactionManager", signature);
|
176 |
0
| tm = (TransactionManager) method.invoke(null, args);
|
177 |
| } |
178 |
| catch (Exception ex) |
179 |
| { |
180 |
0
| log.error("Found WebSphere TransactionManager factory class [" + clazz.getName()
|
181 |
| + "], but couldn't invoke its static 'getTransactionManager' method", ex); |
182 |
| } |
183 |
| } |
184 |
| |
185 |
| } |