View Javadoc
1   /*
2    * Copyright (c) MuleSoft, Inc.  All rights reserved.  http://www.mulesoft.com
3    * The software in this package is published under the terms of the CPAL v1.0
4    * license, a copy of which has been included with this distribution in the
5    * LICENSE.txt file.
6    */
7   package org.mule.transport.jms.jndi;
8   
9   import org.mule.api.MuleException;
10  import org.mule.api.lifecycle.InitialisationException;
11  
12  import java.util.Hashtable;
13  import java.util.Map;
14  
15  import javax.naming.Context;
16  import javax.naming.InitialContext;
17  import javax.naming.NamingException;
18  import javax.naming.spi.InitialContextFactory;
19  
20  import org.apache.commons.logging.Log;
21  import org.apache.commons.logging.LogFactory;
22  
23  public abstract class AbstractJndiNameResolver implements JndiNameResolver
24  {
25  
26      protected final Log logger = LogFactory.getLog(getClass());
27  
28      private String jndiProviderUrl;
29      private String jndiInitialFactory;
30      private Map jndiProviderProperties;
31  
32      // Default contextFactory
33      private InitialContextFactory contextFactory = new InitialContextFactory()
34      {
35          public Context getInitialContext(Hashtable<?, ?> hashtable) throws NamingException
36          {
37              return new InitialContext(hashtable);
38          }
39      };
40  
41      /**
42       * Creates a JNDI context using the current {@link #contextFactory}
43       *
44       * @return a new {@link Context} instance. Callers must provide concurrent
45       *         access control on the returned value.
46       * @throws NamingException if there is a problem during the context creation.
47       */
48      protected Context createInitialContext() throws NamingException
49      {
50          return contextFactory.getInitialContext(getContextProperties());
51      }
52  
53      protected Hashtable getContextProperties()
54      {
55          if ((jndiInitialFactory == null) && (jndiProviderProperties == null
56                                               || !jndiProviderProperties.containsKey(Context.INITIAL_CONTEXT_FACTORY)))
57          {
58              throw new IllegalArgumentException("Undefined value for jndiInitialFactory property");
59          }
60  
61          Hashtable<String, Object> props = new Hashtable<String, Object>();
62  
63          if (jndiInitialFactory != null)
64          {
65              props.put(Context.INITIAL_CONTEXT_FACTORY, jndiInitialFactory);
66          }
67  
68          if (jndiProviderUrl != null)
69          {
70              props.put(Context.PROVIDER_URL, jndiProviderUrl);
71          }
72  
73          if (jndiProviderProperties != null)
74          {
75              props.putAll(jndiProviderProperties);
76          }
77  
78          return props;
79      }
80  
81      public String getJndiProviderUrl()
82      {
83          return jndiProviderUrl;
84      }
85  
86      public void setJndiProviderUrl(String jndiProviderUrl)
87      {
88          this.jndiProviderUrl = jndiProviderUrl;
89      }
90  
91      public String getJndiInitialFactory()
92      {
93          return jndiInitialFactory;
94      }
95  
96      public void setJndiInitialFactory(String jndiInitialFactory)
97      {
98          this.jndiInitialFactory = jndiInitialFactory;
99      }
100 
101     public Map getJndiProviderProperties()
102     {
103         return jndiProviderProperties;
104     }
105 
106     public void setJndiProviderProperties(Map jndiProviderProperties)
107     {
108         this.jndiProviderProperties = jndiProviderProperties;
109     }
110 
111     public InitialContextFactory getContextFactory()
112     {
113         return contextFactory;
114     }
115 
116     public void setContextFactory(InitialContextFactory contextFactory)
117     {
118         if (contextFactory == null)
119         {
120             throw new IllegalArgumentException("Context factory cannot be null");
121         }
122 
123         this.contextFactory = contextFactory;
124     }
125 
126     public void dispose()
127     {
128         // Does nothing
129     }
130 
131     public void initialise() throws InitialisationException
132     {
133         // Does nothing
134     }
135 
136     public void start() throws MuleException
137     {
138         // Does nothing
139     }
140 
141     public void stop() throws MuleException
142     {
143         // Does nothing
144     }
145 }