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 | 0 | public class CachedJndiNameResolver extends AbstractJndiNameResolver |
26 | |
{ |
27 | |
|
28 | |
protected Map<String, Object> cache; |
29 | |
|
30 | |
public Object lookup(String name) throws NamingException |
31 | |
{ |
32 | 0 | Object result = findInCache(name); |
33 | |
|
34 | 0 | if (result == null) { |
35 | 0 | result = findInContext(name); |
36 | |
} |
37 | |
|
38 | 0 | return result; |
39 | |
} |
40 | |
|
41 | |
private Object findInContext(String name) throws NamingException |
42 | |
{ |
43 | |
|
44 | 0 | Context jndiContext = createInitialContext(); |
45 | |
|
46 | |
try |
47 | |
{ |
48 | 0 | Object result = jndiContext.lookup(name); |
49 | |
|
50 | 0 | if (result != null) |
51 | |
{ |
52 | 0 | cache.put(name, result); |
53 | |
} |
54 | |
|
55 | 0 | return result; |
56 | |
} |
57 | |
finally |
58 | |
{ |
59 | 0 | jndiContext.close(); |
60 | |
} |
61 | |
} |
62 | |
|
63 | |
private Object findInCache(String name) |
64 | |
{ |
65 | 0 | Object result = null; |
66 | 0 | if (name != null) |
67 | |
{ |
68 | 0 | result = cache.get(name); |
69 | 0 | if (logger.isDebugEnabled()) |
70 | |
{ |
71 | 0 | logger.debug(String.format("Object: " + name + " was %sfound in the cache", (result == null)? "not ": "")); |
72 | |
} |
73 | |
} |
74 | |
|
75 | 0 | return result; |
76 | |
} |
77 | |
|
78 | |
@Override |
79 | |
public void initialise() { |
80 | 0 | cache = new ConcurrentHashMap<String, Object>(); |
81 | 0 | } |
82 | |
|
83 | |
|
84 | |
|
85 | |
|
86 | |
|
87 | |
|
88 | |
@Override |
89 | |
public void stop() throws MuleException |
90 | |
{ |
91 | 0 | cache.clear(); |
92 | 0 | } |
93 | |
} |