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 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 protected final Log logger = LogFactory.getLog(getClass());
46
47 public JndiObjectFactory()
48 {
49
50 }
51
52 public JndiObjectFactory(String objectName, String initialFactory, String url)
53 {
54 this(objectName, initialFactory, url, null);
55 }
56
57 public JndiObjectFactory(String objectName, String initialFactory, String url, Map properties)
58 {
59 this.objectName = objectName;
60 this.initialFactory = initialFactory;
61 this.url = url;
62 this.properties = properties;
63 }
64
65 public void initialise() throws InitialisationException
66 {
67 if (_context == null)
68 {
69 Hashtable props = new Hashtable();
70
71 if (initialFactory != null)
72 {
73 props.put(Context.INITIAL_CONTEXT_FACTORY, initialFactory);
74 }
75 else if (properties == null
76 || !properties.containsKey(Context.INITIAL_CONTEXT_FACTORY))
77 {
78 throw new InitialisationException(CoreMessages.objectIsNull("jndiInitialFactory"), this);
79 }
80
81 if (url != null)
82 {
83 props.put(Context.PROVIDER_URL, url);
84 }
85
86 if (properties != null)
87 {
88 props.putAll(properties);
89 }
90
91 try
92 {
93 _context = new InitialContext(props);
94 }
95 catch (NamingException e)
96 {
97 throw new InitialisationException(e, this);
98 }
99 }
100 }
101
102 public void dispose()
103 {
104 if (_context != null)
105 {
106 try
107 {
108 _context.close();
109 }
110 catch (NamingException e)
111 {
112 logger.error("JNDI Context failed to dispose properly: ", e);
113 }
114 finally
115 {
116 _context = null;
117 }
118 }
119 }
120
121 public Object getInstance(MuleContext muleContext) throws Exception
122 {
123 if (_object == null || lookupOnEachCall == true)
124 {
125 _object = _context.lookup(objectName);
126 }
127 return _object;
128 }
129
130
131 public Class<?> getObjectClass()
132 {
133 throw new UnsupportedOperationException();
134 }
135
136
137
138
139
140 public String getInitialFactory()
141 {
142 return initialFactory;
143 }
144
145 public void setInitialFactory(String initialFactory)
146 {
147 this.initialFactory = initialFactory;
148 }
149
150 public boolean isLookupOnEachCall()
151 {
152 return lookupOnEachCall;
153 }
154
155 public void setLookupOnEachCall(boolean lookupOnEachCall)
156 {
157 this.lookupOnEachCall = lookupOnEachCall;
158 }
159
160 public String getObjectName()
161 {
162 return objectName;
163 }
164
165 public void setObjectName(String objectName)
166 {
167 this.objectName = objectName;
168 }
169
170 public Map getProperties()
171 {
172 return properties;
173 }
174
175 public void setProperties(Map properties)
176 {
177 this.properties = properties;
178 }
179
180 public String getUrl()
181 {
182 return url;
183 }
184
185 public void setUrl(String url)
186 {
187 this.url = url;
188 }
189
190 public Context getContext()
191 {
192 return _context;
193 }
194
195 protected void setContext(Context context)
196 {
197 this._context = context;
198 }
199
200 public void addObjectInitialisationCallback(InitialisationCallback callback)
201 {
202 throw new UnsupportedOperationException();
203 }
204
205 public boolean isSingleton()
206 {
207 return false;
208 }
209
210 public boolean isExternallyManagedLifecycle()
211 {
212 return false;
213 }
214
215 public boolean isAutoWireObject()
216 {
217 return true;
218 }
219 }