1 | |
|
2 | |
|
3 | |
|
4 | |
|
5 | |
|
6 | |
|
7 | |
|
8 | |
|
9 | |
|
10 | |
|
11 | |
package org.mule.config.spring.jndi; |
12 | |
|
13 | |
import java.util.HashMap; |
14 | |
import java.util.Hashtable; |
15 | |
import java.util.Map; |
16 | |
|
17 | |
import javax.naming.Context; |
18 | |
import javax.naming.NamingException; |
19 | |
import javax.naming.spi.InitialContextFactory; |
20 | |
|
21 | |
import edu.emory.mathcs.backport.java.util.concurrent.ConcurrentHashMap; |
22 | |
|
23 | |
import org.apache.commons.logging.Log; |
24 | |
import org.apache.commons.logging.LogFactory; |
25 | |
import org.springframework.beans.factory.BeanFactory; |
26 | |
import org.springframework.context.support.AbstractXmlApplicationContext; |
27 | |
import org.springframework.core.io.ClassPathResource; |
28 | |
import org.springframework.core.io.Resource; |
29 | |
import org.springframework.core.io.ResourceEditor; |
30 | |
|
31 | |
|
32 | |
|
33 | |
|
34 | 0 | public class SpringInitialContextFactory implements InitialContextFactory |
35 | |
{ |
36 | 0 | private static final transient Log log = LogFactory.getLog(SpringInitialContextFactory.class); |
37 | |
|
38 | 0 | private static Map cache = new HashMap(); |
39 | |
|
40 | |
private static Context singleton; |
41 | |
|
42 | |
|
43 | |
|
44 | |
|
45 | |
|
46 | |
|
47 | |
public static Context makeInitialContext() |
48 | |
{ |
49 | 0 | singleton = new DefaultSpringJndiContext(); |
50 | 0 | return singleton; |
51 | |
} |
52 | |
|
53 | |
public Context getInitialContext(Hashtable environment) throws NamingException |
54 | |
{ |
55 | 0 | if (singleton != null) |
56 | |
{ |
57 | 0 | return singleton; |
58 | |
} |
59 | 0 | Resource resource = null; |
60 | 0 | Object value = environment.get(Context.PROVIDER_URL); |
61 | 0 | String key = "jndi.xml"; |
62 | 0 | if (value == null) |
63 | |
{ |
64 | 0 | resource = new ClassPathResource(key); |
65 | |
} |
66 | |
else |
67 | |
{ |
68 | 0 | if (value instanceof Resource) |
69 | |
{ |
70 | 0 | resource = (Resource) value; |
71 | |
} |
72 | |
else |
73 | |
{ |
74 | 0 | ResourceEditor editor = new ResourceEditor(); |
75 | 0 | key = value.toString(); |
76 | 0 | editor.setAsText(key); |
77 | 0 | resource = (Resource) editor.getValue(); |
78 | |
} |
79 | |
} |
80 | 0 | BeanFactory context = loadContext(resource, key); |
81 | 0 | Context answer = (Context) context.getBean("jndi"); |
82 | 0 | if (answer == null) |
83 | |
{ |
84 | 0 | log.warn("No JNDI context available in JNDI resource: " + resource); |
85 | 0 | answer = new DefaultSpringJndiContext(environment, new ConcurrentHashMap()); |
86 | |
} |
87 | 0 | return answer; |
88 | |
} |
89 | |
|
90 | |
protected BeanFactory loadContext(Resource resource, String key) |
91 | |
{ |
92 | 0 | synchronized (cache) |
93 | |
{ |
94 | 0 | BeanFactory answer = (BeanFactory) cache.get(key); |
95 | 0 | if (answer == null) |
96 | |
{ |
97 | 0 | answer = createContext(resource); |
98 | 0 | cache.put(key, answer); |
99 | |
} |
100 | 0 | return answer; |
101 | 0 | } |
102 | |
} |
103 | |
|
104 | |
protected BeanFactory createContext(Resource resource) |
105 | |
{ |
106 | 0 | log.info("Loading JNDI context from: " + resource); |
107 | 0 | return new SpringInitialContextApplicationContext(new Resource[]{resource}); |
108 | |
} |
109 | |
|
110 | |
|
111 | |
|
112 | |
|
113 | |
|
114 | 0 | class SpringInitialContextApplicationContext extends AbstractXmlApplicationContext |
115 | |
{ |
116 | |
private Resource[] configResources; |
117 | |
|
118 | |
public SpringInitialContextApplicationContext(Resource[] resources) |
119 | 0 | { |
120 | 0 | super(); |
121 | 0 | configResources = resources; |
122 | 0 | refresh(); |
123 | 0 | } |
124 | |
|
125 | |
protected Resource[] getConfigResources() |
126 | |
{ |
127 | 0 | return configResources; |
128 | |
} |
129 | |
} |
130 | |
|
131 | |
} |