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