1 |
|
|
2 |
|
|
3 |
|
|
4 |
|
|
5 |
|
|
6 |
|
|
7 |
|
|
8 |
|
|
9 |
|
|
10 |
|
|
11 |
|
|
12 |
|
|
13 |
|
|
14 |
|
|
15 |
|
|
16 |
|
|
17 |
|
package org.apache.jetspeed.components; |
18 |
|
|
19 |
|
import java.io.File; |
20 |
|
import java.util.ArrayList; |
21 |
|
import java.util.Collection; |
22 |
|
import java.util.HashMap; |
23 |
|
import java.util.Iterator; |
24 |
|
import java.util.Map; |
25 |
|
import java.util.Properties; |
26 |
|
|
27 |
|
import javax.servlet.ServletContext; |
28 |
|
|
29 |
|
import org.apache.jetspeed.engine.JetspeedEngineConstants; |
30 |
|
import org.springframework.beans.factory.config.PropertyPlaceholderConfigurer; |
31 |
|
import org.springframework.context.ApplicationContext; |
32 |
|
import org.springframework.context.ConfigurableApplicationContext; |
33 |
|
import org.springframework.context.support.FileSystemXmlApplicationContext; |
34 |
|
import org.springframework.context.support.GenericApplicationContext; |
35 |
|
import org.springframework.web.context.WebApplicationContext; |
36 |
|
import org.springframework.web.context.support.XmlWebApplicationContext; |
37 |
|
|
38 |
|
|
39 |
|
|
40 |
|
|
41 |
|
|
42 |
|
|
43 |
|
|
44 |
|
|
45 |
|
|
46 |
|
|
47 |
|
|
48 |
|
|
49 |
|
|
50 |
|
public class SpringComponentManager implements ComponentManager |
51 |
|
{ |
52 |
|
protected ConfigurableApplicationContext appContext; |
53 |
|
|
54 |
|
private ConfigurableApplicationContext bootCtx; |
55 |
|
|
56 |
|
protected ArrayList factories; |
57 |
|
|
58 |
|
private Map preconfiguredBeans; |
59 |
|
|
60 |
0 |
private boolean started = false; |
61 |
|
|
62 |
|
public SpringComponentManager(String[] bootConfigs, String[] appConfigs, ServletContext servletContext, |
63 |
|
String appRoot) |
64 |
0 |
{ |
65 |
0 |
File appRootDir = new File(appRoot); |
66 |
0 |
System.setProperty(JetspeedEngineConstants.APPLICATION_ROOT_KEY, appRootDir.getAbsolutePath()); |
67 |
|
|
68 |
0 |
if (bootConfigs != null && bootConfigs.length > 0) |
69 |
|
{ |
70 |
0 |
bootCtx = new XmlWebApplicationContext(); |
71 |
0 |
((XmlWebApplicationContext) bootCtx).setServletContext(servletContext); |
72 |
0 |
((XmlWebApplicationContext) bootCtx).setConfigLocations(bootConfigs); |
73 |
|
} |
74 |
|
else |
75 |
|
{ |
76 |
0 |
bootCtx = new GenericApplicationContext(); |
77 |
|
} |
78 |
|
|
79 |
0 |
appContext = new XmlWebApplicationContext(); |
80 |
0 |
((XmlWebApplicationContext) appContext).setParent(bootCtx); |
81 |
0 |
((XmlWebApplicationContext) appContext).setServletContext(servletContext); |
82 |
0 |
((XmlWebApplicationContext) appContext).setConfigLocations(appConfigs); |
83 |
|
|
84 |
0 |
factories = new ArrayList(); |
85 |
0 |
factories.add(appContext); |
86 |
|
|
87 |
0 |
servletContext.setAttribute(WebApplicationContext.ROOT_WEB_APPLICATION_CONTEXT_ATTRIBUTE, appContext); |
88 |
0 |
} |
89 |
|
|
90 |
|
public SpringComponentManager(String[] bootConfigs, String[] appConfigs, ServletContext servletContext, |
91 |
|
String appRoot, Map preconfiguredBeans) |
92 |
|
{ |
93 |
0 |
this(bootConfigs, appConfigs, servletContext, appRoot); |
94 |
0 |
this.preconfiguredBeans = preconfiguredBeans; |
95 |
0 |
} |
96 |
|
|
97 |
|
|
98 |
|
public SpringComponentManager(String[] bootConfigs, String[] appConfigs, String appRoot) |
99 |
0 |
{ |
100 |
0 |
PropertyPlaceholderConfigurer ppc = new PropertyPlaceholderConfigurer(); |
101 |
0 |
Properties p = new Properties(); |
102 |
|
|
103 |
0 |
ppc.setProperties(p); |
104 |
|
|
105 |
0 |
if (bootConfigs != null && bootConfigs.length > 0) |
106 |
|
{ |
107 |
0 |
bootCtx = new FileSystemXmlApplicationContext(bootConfigs, false); |
108 |
0 |
bootCtx.addBeanFactoryPostProcessor(ppc); |
109 |
0 |
bootCtx.refresh(); |
110 |
|
} |
111 |
|
else |
112 |
|
{ |
113 |
0 |
bootCtx = new GenericApplicationContext(); |
114 |
|
} |
115 |
|
|
116 |
0 |
appContext = new FileSystemXmlApplicationContext(appConfigs, false, bootCtx); |
117 |
0 |
appContext.addBeanFactoryPostProcessor(ppc); |
118 |
0 |
appContext.refresh(); |
119 |
0 |
factories = new ArrayList(); |
120 |
0 |
factories.add(appContext); |
121 |
|
|
122 |
0 |
} |
123 |
|
|
124 |
|
|
125 |
|
|
126 |
|
|
127 |
|
|
128 |
|
|
129 |
|
|
130 |
|
|
131 |
|
|
132 |
|
|
133 |
|
public Object getComponent(Object componentName) |
134 |
|
{ |
135 |
0 |
if (componentName instanceof Class) |
136 |
|
{ |
137 |
0 |
return appContext.getBean(((Class) componentName).getName()); |
138 |
|
} |
139 |
|
else |
140 |
|
{ |
141 |
0 |
return appContext.getBean(componentName.toString()); |
142 |
|
} |
143 |
|
} |
144 |
|
|
145 |
|
|
146 |
|
|
147 |
|
|
148 |
|
|
149 |
|
|
150 |
|
|
151 |
|
|
152 |
|
|
153 |
|
|
154 |
|
|
155 |
|
|
156 |
|
public Object getComponent(Object containerName, Object componentName) |
157 |
|
{ |
158 |
0 |
return getComponent(componentName); |
159 |
|
} |
160 |
|
|
161 |
|
|
162 |
|
|
163 |
|
|
164 |
|
|
165 |
|
|
166 |
|
|
167 |
|
|
168 |
|
|
169 |
|
|
170 |
|
public Object getContainer(String containerName) |
171 |
|
{ |
172 |
0 |
return appContext; |
173 |
|
} |
174 |
|
|
175 |
|
|
176 |
|
|
177 |
|
|
178 |
|
|
179 |
|
|
180 |
|
|
181 |
|
|
182 |
|
|
183 |
|
public Object getRootContainer() |
184 |
|
{ |
185 |
0 |
return appContext; |
186 |
|
} |
187 |
|
|
188 |
|
|
189 |
|
|
190 |
|
|
191 |
|
|
192 |
|
|
193 |
|
|
194 |
|
|
195 |
|
|
196 |
|
public Collection getContainers() |
197 |
|
{ |
198 |
0 |
return factories; |
199 |
|
} |
200 |
|
|
201 |
|
|
202 |
|
|
203 |
|
|
204 |
|
|
205 |
|
|
206 |
|
|
207 |
|
|
208 |
|
|
209 |
|
public void stop() |
210 |
|
{ |
211 |
0 |
appContext.close(); |
212 |
0 |
bootCtx.close(); |
213 |
0 |
started = false; |
214 |
0 |
} |
215 |
|
|
216 |
|
public ApplicationContext getApplicationContext() |
217 |
|
{ |
218 |
0 |
return appContext; |
219 |
|
} |
220 |
|
|
221 |
|
public void addComponent(String name, Object bean) |
222 |
|
{ |
223 |
0 |
if (preconfiguredBeans == null) |
224 |
|
{ |
225 |
0 |
preconfiguredBeans = new HashMap(); |
226 |
|
} |
227 |
0 |
preconfiguredBeans.put(name, bean); |
228 |
|
|
229 |
0 |
if (started) |
230 |
|
{ |
231 |
0 |
bootCtx.getBeanFactory().registerSingleton(name, bean); |
232 |
|
} |
233 |
0 |
} |
234 |
|
|
235 |
|
public void start() |
236 |
|
{ |
237 |
0 |
bootCtx.refresh(); |
238 |
0 |
if (preconfiguredBeans != null) |
239 |
|
{ |
240 |
0 |
Iterator itr = preconfiguredBeans.entrySet().iterator(); |
241 |
0 |
while (itr.hasNext()) |
242 |
|
{ |
243 |
0 |
Map.Entry entry = (Map.Entry) itr.next(); |
244 |
0 |
bootCtx.getBeanFactory().registerSingleton(entry.getKey().toString(), entry.getValue()); |
245 |
0 |
} |
246 |
|
} |
247 |
|
|
248 |
0 |
appContext.refresh(); |
249 |
0 |
started = true; |
250 |
0 |
} |
251 |
|
|
252 |
|
} |