1
2
3
4
5
6
7 package org.mule.transport.jms.jndi;
8
9 import org.mule.api.MuleException;
10
11 import java.util.Map;
12 import java.util.concurrent.ConcurrentHashMap;
13
14 import javax.naming.Context;
15 import javax.naming.NamingException;
16
17
18
19
20
21
22
23
24
25 public class CachedJndiNameResolver extends AbstractJndiNameResolver
26 {
27
28 protected Map<String, Object> cache;
29
30 public Object lookup(String name) throws NamingException
31 {
32 Object result = findInCache(name);
33
34 if (result == null) {
35 result = findInContext(name);
36 }
37
38 return result;
39 }
40
41 private Object findInContext(String name) throws NamingException
42 {
43
44 Context jndiContext = createInitialContext();
45
46 try
47 {
48 Object result = jndiContext.lookup(name);
49
50 if (result != null)
51 {
52 cache.put(name, result);
53 }
54
55 return result;
56 }
57 finally
58 {
59 jndiContext.close();
60 }
61 }
62
63 private Object findInCache(String name)
64 {
65 Object result = null;
66 if (name != null)
67 {
68 result = cache.get(name);
69 if (logger.isDebugEnabled())
70 {
71 logger.debug(String.format("Object: " + name + " was %sfound in the cache", (result == null)? "not ": ""));
72 }
73 }
74
75 return result;
76 }
77
78 @Override
79 public void initialise() {
80 cache = new ConcurrentHashMap<String, Object>();
81 }
82
83
84
85
86
87
88 @Override
89 public void stop() throws MuleException
90 {
91 cache.clear();
92 }
93 }