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