View Javadoc

1   /*
2    * $Id: JndiObjectFactory.java 12269 2008-07-10 04:19:03Z dfeist $
3    * --------------------------------------------------------------------------------------
4    * Copyright (c) MuleSource, Inc.  All rights reserved.  http://www.mulesource.com
5    *
6    * The software in this package is published under the terms of the CPAL v1.0
7    * license, a copy of which has been included with this distribution in the
8    * LICENSE.txt file.
9    */
10  
11  package org.mule.object;
12  
13  import org.mule.api.lifecycle.InitialisationCallback;
14  import org.mule.api.lifecycle.InitialisationException;
15  import org.mule.api.object.ObjectFactory;
16  import org.mule.config.i18n.CoreMessages;
17  
18  import java.util.Hashtable;
19  import java.util.Map;
20  
21  import javax.naming.Context;
22  import javax.naming.InitialContext;
23  import javax.naming.NamingException;
24  
25  import org.apache.commons.logging.Log;
26  import org.apache.commons.logging.LogFactory;
27  
28  public class JndiObjectFactory implements ObjectFactory
29  {
30      /**
31       * If true, the object is looked up from JNDI each time create() is called, otherwise it
32       * is looked up once and stored locally.  Default value is false.
33       */
34      private boolean lookupOnEachCall = false;
35      
36      private String objectName;
37  
38      private String initialFactory;
39  
40      private String url;
41  
42      private Map properties;
43      
44      private Context _context;
45      
46      private Object _object;
47  
48      protected final Log logger = LogFactory.getLog(getClass());
49      
50      public JndiObjectFactory()
51      {
52          // for IoC only
53      }
54      
55      public JndiObjectFactory(String objectName, String initialFactory, String url)
56      {
57          this(objectName, initialFactory, url, null);
58      }
59      
60      public JndiObjectFactory(String objectName, String initialFactory, String url, Map properties)
61      {
62          this.objectName = objectName;
63          this.initialFactory = initialFactory;
64          this.url = url;
65          this.properties = properties;
66      }
67      
68      public void initialise() throws InitialisationException
69      {
70          if (_context == null)
71          {
72              Hashtable props = new Hashtable();
73  
74              if (initialFactory != null)
75              {
76                  props.put(Context.INITIAL_CONTEXT_FACTORY, initialFactory);
77              }
78              else if (properties == null
79                      || !properties.containsKey(Context.INITIAL_CONTEXT_FACTORY))
80              {
81                  throw new InitialisationException(CoreMessages.objectIsNull("jndiInitialFactory"), this);
82              }
83  
84              if (url != null)
85              {
86                  props.put(Context.PROVIDER_URL, url);
87              }
88  
89              if (properties != null)
90              {
91                  props.putAll(properties);
92              }
93              
94              try
95              {
96                  _context = new InitialContext(props);
97              }
98              catch (NamingException e)
99              {
100                 throw new InitialisationException(e, this);
101             }
102         }
103     }
104     
105     public void dispose() 
106     {
107         if (_context != null)
108         {
109             try
110             {
111                 _context.close();
112             }
113             catch (NamingException e)
114             {
115                 logger.error("JNDI Context failed to dispose properly: ", e);
116             }
117             finally
118             {
119                 _context = null;
120             }
121         }
122     }
123     
124     public Object getInstance() throws Exception
125     {
126         if (_object == null || lookupOnEachCall == true)
127         {
128             _object = _context.lookup(objectName);
129         }    
130         return _object;
131     }
132     
133     /** {@inheritDoc} */
134     public Class getObjectClass()
135     {
136         throw new UnsupportedOperationException();
137     }
138 
139     ///////////////////////////////////////////////////////////////////////////////////////////
140     // Getters and Setters
141     ///////////////////////////////////////////////////////////////////////////////////////////    
142 
143     public String getInitialFactory()
144     {
145         return initialFactory;
146     }
147 
148     public void setInitialFactory(String initialFactory)
149     {
150         this.initialFactory = initialFactory;
151     }
152 
153     public boolean isLookupOnEachCall()
154     {
155         return lookupOnEachCall;
156     }
157 
158     public void setLookupOnEachCall(boolean lookupOnEachCall)
159     {
160         this.lookupOnEachCall = lookupOnEachCall;
161     }
162 
163     public String getObjectName()
164     {
165         return objectName;
166     }
167 
168     public void setObjectName(String objectName)
169     {
170         this.objectName = objectName;
171     }
172 
173     public Map getProperties()
174     {
175         return properties;
176     }
177 
178     public void setProperties(Map properties)
179     {
180         this.properties = properties;
181     }
182 
183     public String getUrl()
184     {
185         return url;
186     }
187 
188     public void setUrl(String url)
189     {
190         this.url = url;
191     }
192 
193     public Context getContext()
194     {
195         return _context;
196     }
197 
198     protected void setContext(Context context)
199     {
200         this._context = context;
201     }
202 
203     public void addObjectInitialisationCallback(InitialisationCallback callback)
204     {
205         throw new UnsupportedOperationException();
206     }
207 
208     public boolean isSingleton()
209     {
210         return false;
211     }
212 }