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