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.lifecycle.InitialisationException;
10  
11  import javax.naming.Context;
12  import javax.naming.NamingException;
13  
14  /**
15   * Defines a simple {@link JndiNameResolver} that maintains a {@link Context}
16   * instance opened all the time and always relies on the context to do the look
17   * ups.
18   */
19  public class SimpleJndiNameResolver extends AbstractJndiNameResolver
20  {
21  
22      // @GuardedBy(this)
23      private Context jndiContext;
24  
25      public synchronized Object lookup(String name) throws NamingException
26      {
27          return jndiContext.lookup(name);
28      }
29  
30      public void initialise() throws InitialisationException
31      {
32          if (jndiContext == null)
33          {
34              try
35              {
36                  jndiContext = createInitialContext();
37              }
38              catch (NamingException e)
39              {
40                  throw new InitialisationException(e, this);
41              }
42          }
43      }
44  
45      @Override
46      public void dispose()
47      {
48          if (jndiContext != null)
49          {
50              try
51              {
52                  jndiContext.close();
53              }
54              catch (NamingException e)
55              {
56                  logger.error("Jms connector failed to dispose properly: ", e);
57              }
58              finally
59              {
60                  jndiContext = null;
61              }
62          }
63      }
64  }