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.util;
8   
9   import java.util.Hashtable;
10  import java.util.Map;
11  
12  import javax.naming.Context;
13  import javax.naming.InitialContext;
14  import javax.naming.NamingException;
15  
16  /**
17   * Common code for initialising the JNDI context.
18   */
19  public final class JndiContextHelper
20  {
21      /** Do not instanciate. */
22      private JndiContextHelper ()
23      {
24          // no-op
25      }
26  
27      /**
28       * Create a new initial context.
29       * 
30       * @param environment JNDI properties or <code>null</code>. In the latter case
31       *            a default constructor of <code>InitialContext</code> will be
32       *            called with standard JNDI lookup properties semantics.
33       * @return jndi context
34       * @throws NamingException if there was a JNDI error
35       */
36      public static Context initialise(final Map environment) throws NamingException
37      {
38          Context context;
39          if (environment != null && environment.size() > 0)
40          {
41              context = new InitialContext(new Hashtable(environment));
42          }
43          else
44          {
45              context = new InitialContext();
46          }
47  
48          return context;
49      }
50  }