1 | |
|
2 | |
|
3 | |
|
4 | |
|
5 | |
|
6 | |
|
7 | |
package org.mule.object; |
8 | |
|
9 | |
import org.mule.api.MuleContext; |
10 | |
import org.mule.api.lifecycle.InitialisationCallback; |
11 | |
import org.mule.api.lifecycle.InitialisationException; |
12 | |
import org.mule.api.object.ObjectFactory; |
13 | |
import org.mule.config.i18n.CoreMessages; |
14 | |
|
15 | |
import java.util.Hashtable; |
16 | |
import java.util.Map; |
17 | |
|
18 | |
import javax.naming.Context; |
19 | |
import javax.naming.InitialContext; |
20 | |
import javax.naming.NamingException; |
21 | |
|
22 | |
import org.apache.commons.logging.Log; |
23 | |
import org.apache.commons.logging.LogFactory; |
24 | |
|
25 | |
public class JndiObjectFactory implements ObjectFactory |
26 | |
{ |
27 | |
|
28 | |
|
29 | |
|
30 | |
|
31 | 0 | private boolean lookupOnEachCall = false; |
32 | |
|
33 | |
private String objectName; |
34 | |
|
35 | |
private String initialFactory; |
36 | |
|
37 | |
private String url; |
38 | |
|
39 | |
private Map properties; |
40 | |
|
41 | |
private Context _context; |
42 | |
|
43 | |
private Object _object; |
44 | |
|
45 | 0 | protected final Log logger = LogFactory.getLog(getClass()); |
46 | |
|
47 | |
public JndiObjectFactory() |
48 | 0 | { |
49 | |
|
50 | 0 | } |
51 | |
|
52 | |
public JndiObjectFactory(String objectName, String initialFactory, String url) |
53 | |
{ |
54 | 0 | this(objectName, initialFactory, url, null); |
55 | 0 | } |
56 | |
|
57 | |
public JndiObjectFactory(String objectName, String initialFactory, String url, Map properties) |
58 | 0 | { |
59 | 0 | this.objectName = objectName; |
60 | 0 | this.initialFactory = initialFactory; |
61 | 0 | this.url = url; |
62 | 0 | this.properties = properties; |
63 | 0 | } |
64 | |
|
65 | |
public void initialise() throws InitialisationException |
66 | |
{ |
67 | 0 | if (_context == null) |
68 | |
{ |
69 | 0 | Hashtable props = new Hashtable(); |
70 | |
|
71 | 0 | if (initialFactory != null) |
72 | |
{ |
73 | 0 | props.put(Context.INITIAL_CONTEXT_FACTORY, initialFactory); |
74 | |
} |
75 | 0 | else if (properties == null |
76 | |
|| !properties.containsKey(Context.INITIAL_CONTEXT_FACTORY)) |
77 | |
{ |
78 | 0 | throw new InitialisationException(CoreMessages.objectIsNull("jndiInitialFactory"), this); |
79 | |
} |
80 | |
|
81 | 0 | if (url != null) |
82 | |
{ |
83 | 0 | props.put(Context.PROVIDER_URL, url); |
84 | |
} |
85 | |
|
86 | 0 | if (properties != null) |
87 | |
{ |
88 | 0 | props.putAll(properties); |
89 | |
} |
90 | |
|
91 | |
try |
92 | |
{ |
93 | 0 | _context = new InitialContext(props); |
94 | |
} |
95 | 0 | catch (NamingException e) |
96 | |
{ |
97 | 0 | throw new InitialisationException(e, this); |
98 | 0 | } |
99 | |
} |
100 | 0 | } |
101 | |
|
102 | |
public void dispose() |
103 | |
{ |
104 | 0 | if (_context != null) |
105 | |
{ |
106 | |
try |
107 | |
{ |
108 | 0 | _context.close(); |
109 | |
} |
110 | 0 | catch (NamingException e) |
111 | |
{ |
112 | 0 | logger.error("JNDI Context failed to dispose properly: ", e); |
113 | |
} |
114 | |
finally |
115 | |
{ |
116 | 0 | _context = null; |
117 | 0 | } |
118 | |
} |
119 | 0 | } |
120 | |
|
121 | |
public Object getInstance(MuleContext muleContext) throws Exception |
122 | |
{ |
123 | 0 | if (_object == null || lookupOnEachCall == true) |
124 | |
{ |
125 | 0 | _object = _context.lookup(objectName); |
126 | |
} |
127 | 0 | return _object; |
128 | |
} |
129 | |
|
130 | |
|
131 | |
public Class<?> getObjectClass() |
132 | |
{ |
133 | 0 | throw new UnsupportedOperationException(); |
134 | |
} |
135 | |
|
136 | |
|
137 | |
|
138 | |
|
139 | |
|
140 | |
public String getInitialFactory() |
141 | |
{ |
142 | 0 | return initialFactory; |
143 | |
} |
144 | |
|
145 | |
public void setInitialFactory(String initialFactory) |
146 | |
{ |
147 | 0 | this.initialFactory = initialFactory; |
148 | 0 | } |
149 | |
|
150 | |
public boolean isLookupOnEachCall() |
151 | |
{ |
152 | 0 | return lookupOnEachCall; |
153 | |
} |
154 | |
|
155 | |
public void setLookupOnEachCall(boolean lookupOnEachCall) |
156 | |
{ |
157 | 0 | this.lookupOnEachCall = lookupOnEachCall; |
158 | 0 | } |
159 | |
|
160 | |
public String getObjectName() |
161 | |
{ |
162 | 0 | return objectName; |
163 | |
} |
164 | |
|
165 | |
public void setObjectName(String objectName) |
166 | |
{ |
167 | 0 | this.objectName = objectName; |
168 | 0 | } |
169 | |
|
170 | |
public Map getProperties() |
171 | |
{ |
172 | 0 | return properties; |
173 | |
} |
174 | |
|
175 | |
public void setProperties(Map properties) |
176 | |
{ |
177 | 0 | this.properties = properties; |
178 | 0 | } |
179 | |
|
180 | |
public String getUrl() |
181 | |
{ |
182 | 0 | return url; |
183 | |
} |
184 | |
|
185 | |
public void setUrl(String url) |
186 | |
{ |
187 | 0 | this.url = url; |
188 | 0 | } |
189 | |
|
190 | |
public Context getContext() |
191 | |
{ |
192 | 0 | return _context; |
193 | |
} |
194 | |
|
195 | |
protected void setContext(Context context) |
196 | |
{ |
197 | 0 | this._context = context; |
198 | 0 | } |
199 | |
|
200 | |
public void addObjectInitialisationCallback(InitialisationCallback callback) |
201 | |
{ |
202 | 0 | throw new UnsupportedOperationException(); |
203 | |
} |
204 | |
|
205 | |
public boolean isSingleton() |
206 | |
{ |
207 | 0 | return false; |
208 | |
} |
209 | |
|
210 | |
public boolean isExternallyManagedLifecycle() |
211 | |
{ |
212 | 0 | return false; |
213 | |
} |
214 | |
|
215 | |
public boolean isAutoWireObject() |
216 | |
{ |
217 | 0 | return true; |
218 | |
} |
219 | |
} |