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 * <p>
40 * SpringComponentManager
41 * </p>
42 * <p>
43 *
44 * </p>
45 *
46 * @author <a href="mailto:weaver@apache.org">Scott T. Weaver</a>
47 * @version $Id: SpringComponentManager.java 517719 2007-03-13 15:05:48Z ate $
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 private boolean started = false;
61
62 public SpringComponentManager(String[] bootConfigs, String[] appConfigs, ServletContext servletContext,
63 String appRoot)
64 {
65 File appRootDir = new File(appRoot);
66 System.setProperty(JetspeedEngineConstants.APPLICATION_ROOT_KEY, appRootDir.getAbsolutePath());
67
68 if (bootConfigs != null && bootConfigs.length > 0)
69 {
70 bootCtx = new XmlWebApplicationContext();
71 ((XmlWebApplicationContext) bootCtx).setServletContext(servletContext);
72 ((XmlWebApplicationContext) bootCtx).setConfigLocations(bootConfigs);
73 }
74 else
75 {
76 bootCtx = new GenericApplicationContext();
77 }
78
79 appContext = new XmlWebApplicationContext();
80 ((XmlWebApplicationContext) appContext).setParent(bootCtx);
81 ((XmlWebApplicationContext) appContext).setServletContext(servletContext);
82 ((XmlWebApplicationContext) appContext).setConfigLocations(appConfigs);
83
84 factories = new ArrayList();
85 factories.add(appContext);
86
87 servletContext.setAttribute(WebApplicationContext.ROOT_WEB_APPLICATION_CONTEXT_ATTRIBUTE, appContext);
88 }
89
90 public SpringComponentManager(String[] bootConfigs, String[] appConfigs, ServletContext servletContext,
91 String appRoot, Map preconfiguredBeans)
92 {
93 this(bootConfigs, appConfigs, servletContext, appRoot);
94 this.preconfiguredBeans = preconfiguredBeans;
95 }
96
97
98 public SpringComponentManager(String[] bootConfigs, String[] appConfigs, String appRoot)
99 {
100 PropertyPlaceholderConfigurer ppc = new PropertyPlaceholderConfigurer();
101 Properties p = new Properties();
102
103 ppc.setProperties(p);
104
105 if (bootConfigs != null && bootConfigs.length > 0)
106 {
107 bootCtx = new FileSystemXmlApplicationContext(bootConfigs, false);
108 bootCtx.addBeanFactoryPostProcessor(ppc);
109 bootCtx.refresh();
110 }
111 else
112 {
113 bootCtx = new GenericApplicationContext();
114 }
115
116 appContext = new FileSystemXmlApplicationContext(appConfigs, false, bootCtx);
117 appContext.addBeanFactoryPostProcessor(ppc);
118 appContext.refresh();
119 factories = new ArrayList();
120 factories.add(appContext);
121
122 }
123
124 /***
125 * <p>
126 * getComponent
127 * </p>
128 *
129 * @see org.apache.jetspeed.components.ComponentManagement#getComponent(java.lang.Object)
130 * @param componentName
131 * @return
132 */
133 public Object getComponent(Object componentName)
134 {
135 if (componentName instanceof Class)
136 {
137 return appContext.getBean(((Class) componentName).getName());
138 }
139 else
140 {
141 return appContext.getBean(componentName.toString());
142 }
143 }
144
145 /***
146 * <p>
147 * getComponent
148 * </p>
149 *
150 * @see org.apache.jetspeed.components.ComponentManagement#getComponent(java.lang.Object,
151 * java.lang.Object)
152 * @param containerName
153 * @param componentName
154 * @return
155 */
156 public Object getComponent(Object containerName, Object componentName)
157 {
158 return getComponent(componentName);
159 }
160
161 /***
162 * <p>
163 * getContainer
164 * </p>
165 *
166 * @see org.apache.jetspeed.components.ContainerManagement#getContainer(java.lang.String)
167 * @param containerName
168 * @return
169 */
170 public Object getContainer(String containerName)
171 {
172 return appContext;
173 }
174
175 /***
176 * <p>
177 * getRootContainer
178 * </p>
179 *
180 * @see org.apache.jetspeed.components.ContainerManagement#getRootContainer()
181 * @return
182 */
183 public Object getRootContainer()
184 {
185 return appContext;
186 }
187
188 /***
189 * <p>
190 * getContainers
191 * </p>
192 *
193 * @see org.apache.jetspeed.components.ContainerManagement#getContainers()
194 * @return
195 */
196 public Collection getContainers()
197 {
198 return factories;
199 }
200
201 /***
202 * <p>
203 * stop
204 * </p>
205 *
206 * @see org.apache.jetspeed.components.ContainerManagement#stop()
207 *
208 */
209 public void stop()
210 {
211 appContext.close();
212 bootCtx.close();
213 started = false;
214 }
215
216 public ApplicationContext getApplicationContext()
217 {
218 return appContext;
219 }
220
221 public void addComponent(String name, Object bean)
222 {
223 if (preconfiguredBeans == null)
224 {
225 preconfiguredBeans = new HashMap();
226 }
227 preconfiguredBeans.put(name, bean);
228
229 if (started)
230 {
231 bootCtx.getBeanFactory().registerSingleton(name, bean);
232 }
233 }
234
235 public void start()
236 {
237 bootCtx.refresh();
238 if (preconfiguredBeans != null)
239 {
240 Iterator itr = preconfiguredBeans.entrySet().iterator();
241 while (itr.hasNext())
242 {
243 Map.Entry entry = (Map.Entry) itr.next();
244 bootCtx.getBeanFactory().registerSingleton(entry.getKey().toString(), entry.getValue());
245 }
246 }
247
248 appContext.refresh();
249 started = true;
250 }
251
252 }