1 package org.apache.ojb.broker.transaction.tm;
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18 import javax.transaction.TransactionManager;
19
20 import org.apache.commons.lang.SystemUtils;
21 import org.apache.ojb.broker.core.NamingLocator;
22 import org.apache.ojb.broker.util.ClassHelper;
23 import org.apache.ojb.broker.util.logging.Logger;
24 import org.apache.ojb.broker.util.logging.LoggerFactory;
25
26
27
28
29
30
31
32
33
34 public abstract class AbstractTransactionManagerFactory implements TransactionManagerFactory
35 {
36 private static Logger log = LoggerFactory.getLogger(AbstractTransactionManagerFactory.class);
37
38
39
40
41 public static String TM_DEFAULT_METHOD_NAME = "getTransactionManager";
42
43
44 private static TransactionManager tm = null;
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73 public abstract String[][] getLookupInfo();
74
75
76
77
78 public synchronized TransactionManager getTransactionManager() throws TransactionManagerFactoryException
79 {
80 if (tm == null)
81 {
82 StringBuffer msg = new StringBuffer();
83 String[][] lookupInfo = getLookupInfo();
84 String EOL = SystemUtils.LINE_SEPARATOR;
85
86 for (int i = 0; i < lookupInfo.length; i++)
87 {
88 String description = lookupInfo[i][0];
89 String methodName = lookupInfo[i][1];
90 String className = lookupInfo[i][2];
91 try
92 {
93 if (className == null)
94 {
95 tm = jndiLookup(description, methodName);
96 }
97 else
98 {
99 tm = instantiateClass(description, className, methodName);
100 }
101 msg.append("Successfully requested TM for " + description + EOL);
102 }
103 catch (Exception e)
104 {
105 if (className == null)
106 msg.append("Error on TM request for " + description +
107 ", using jndi-lookup '" + methodName + "'" + EOL + e.getMessage() + EOL);
108 else
109 msg.append("Error on TM request for " + description + ", using method '" +
110 methodName + "' for class '" + className + "'" + EOL + e.getMessage() + EOL);
111 }
112 if (tm != null) break;
113 }
114
115 if (tm == null)
116 {
117 throw new TransactionManagerFactoryException("Can't lookup transaction manager:" + EOL + msg);
118 }
119 }
120 return tm;
121 }
122
123 protected TransactionManager jndiLookup(String description, String methodName)
124 {
125 log.info(description + ", lookup TransactionManager: '" + methodName + "'");
126 return (TransactionManager) NamingLocator.lookup(methodName);
127 }
128
129 protected TransactionManager instantiateClass(String description, String className, String methodName) throws Exception
130 {
131 log.info(description + ", invoke method '"
132 + methodName + "()' on class " + className);
133 Class tmClass = ClassHelper.getClass(className);
134 return (TransactionManager) tmClass.getMethod(methodName, null).invoke(null, null);
135 }
136 }