View Javadoc

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