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 | 0 | public class GenericTransactionManagerLookupFactory implements TransactionManagerFactory, Initialisable |
35 | |
{ |
36 | 0 | 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 | 0 | return jndiName; |
49 | |
} |
50 | |
|
51 | |
public void setJndiName(final String jndiName) |
52 | |
{ |
53 | 0 | this.jndiName = jndiName; |
54 | 0 | } |
55 | |
|
56 | |
public TransactionManager getTxManager() |
57 | |
{ |
58 | 0 | return txManager; |
59 | |
} |
60 | |
|
61 | |
public void setTxManager(final TransactionManager txManager) |
62 | |
{ |
63 | 0 | this.txManager = txManager; |
64 | 0 | } |
65 | |
|
66 | |
public Map getEnvironment() |
67 | |
{ |
68 | 0 | return environment; |
69 | |
} |
70 | |
|
71 | |
public void setEnvironment(final Map environment) |
72 | |
{ |
73 | 0 | this.environment = environment; |
74 | 0 | } |
75 | |
|
76 | |
public Context getContext() |
77 | |
{ |
78 | 0 | return context; |
79 | |
} |
80 | |
|
81 | |
public void setContext(final Context context) |
82 | |
{ |
83 | 0 | this.context = context; |
84 | 0 | } |
85 | |
|
86 | |
|
87 | |
|
88 | |
public TransactionManager create(MuleConfiguration config) throws Exception |
89 | |
{ |
90 | |
|
91 | 0 | initialise(); |
92 | 0 | if (txManager == null) |
93 | |
{ |
94 | 0 | txManager = (TransactionManager) context.lookup(jndiName); |
95 | |
} |
96 | |
|
97 | 0 | return txManager; |
98 | |
} |
99 | |
|
100 | |
|
101 | |
|
102 | |
|
103 | |
|
104 | |
|
105 | |
|
106 | |
|
107 | |
|
108 | |
|
109 | |
|
110 | |
|
111 | |
|
112 | |
public void initialise() throws InitialisationException |
113 | |
{ |
114 | 0 | if (txManager == null && StringUtils.isEmpty(StringUtils.trim(jndiName))) |
115 | |
{ |
116 | 0 | throw new InitialisationException(CoreMessages.propertiesNotSet("jndiName"), this); |
117 | |
} |
118 | |
|
119 | |
try |
120 | |
{ |
121 | 0 | if (context == null) |
122 | |
{ |
123 | 0 | context = JndiContextHelper.initialise(getEnvironment()); |
124 | |
} |
125 | |
} |
126 | 0 | catch (NamingException e) |
127 | |
{ |
128 | 0 | throw new InitialisationException(CoreMessages.failedToCreate("Jndi context"), |
129 | |
e, this); |
130 | 0 | } |
131 | 0 | } |
132 | |
} |