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