View Javadoc

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