1
2
3
4
5
6
7
8
9
10
11 package org.mule.transaction.lookup;
12
13 import org.mule.api.lifecycle.Initialisable;
14 import org.mule.api.lifecycle.InitialisationException;
15 import org.mule.api.transaction.TransactionManagerFactory;
16 import org.mule.config.i18n.CoreMessages;
17 import org.mule.util.JndiContextHelper;
18 import org.mule.util.StringUtils;
19
20 import java.util.Map;
21
22 import javax.naming.Context;
23 import javax.naming.NamingException;
24 import javax.transaction.TransactionManager;
25
26 import org.apache.commons.logging.Log;
27 import org.apache.commons.logging.LogFactory;
28
29
30
31
32
33
34
35
36
37 public class GenericTransactionManagerLookupFactory implements TransactionManagerFactory, Initialisable
38 {
39 protected final Log logger = LogFactory.getLog(getClass());
40
41 protected Context context;
42
43 private Map environment;
44
45 private TransactionManager txManager;
46
47 private String jndiName;
48
49 public String getJndiName()
50 {
51 return jndiName;
52 }
53
54 public void setJndiName(final String jndiName)
55 {
56 this.jndiName = jndiName;
57 }
58
59 public TransactionManager getTxManager()
60 {
61 return txManager;
62 }
63
64 public void setTxManager(final TransactionManager txManager)
65 {
66 this.txManager = txManager;
67 }
68
69 public Map getEnvironment()
70 {
71 return environment;
72 }
73
74 public void setEnvironment(final Map environment)
75 {
76 this.environment = environment;
77 }
78
79 public Context getContext()
80 {
81 return context;
82 }
83
84 public void setContext(final Context context)
85 {
86 this.context = context;
87 }
88
89
90 public TransactionManager create() throws Exception
91 {
92
93 initialise();
94 if (txManager == null)
95 {
96 txManager = (TransactionManager) context.lookup(jndiName);
97 }
98
99 return txManager;
100 }
101
102
103
104
105
106
107
108
109
110
111
112
113
114 public void initialise() throws InitialisationException
115 {
116 if (txManager == null && StringUtils.isEmpty(StringUtils.trim(jndiName)))
117 {
118 throw new InitialisationException(CoreMessages.propertiesNotSet("jndiName"), this);
119 }
120
121 try
122 {
123 if (context == null)
124 {
125 context = JndiContextHelper.initialise(getEnvironment());
126 }
127 }
128 catch (NamingException e)
129 {
130 throw new InitialisationException(CoreMessages.failedToCreate("Jndi context"),
131 e, this);
132 }
133 }
134 }