001: /*
002: * Licensed to the Apache Software Foundation (ASF) under one or more
003: * contributor license agreements. See the NOTICE file distributed with
004: * this work for additional information regarding copyright ownership.
005: * The ASF licenses this file to You under the Apache License, Version 2.0
006: * (the "License"); you may not use this file except in compliance with
007: * the License. You may obtain a copy of the License at
008: *
009: * http://www.apache.org/licenses/LICENSE-2.0
010: *
011: * Unless required by applicable law or agreed to in writing, software
012: * distributed under the License is distributed on an "AS IS" BASIS,
013: * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
014: * See the License for the specific language governing permissions and
015: * limitations under the License.
016: */
017: package org.apache.jetspeed.components;
018:
019: import java.io.File;
020: import java.util.ArrayList;
021: import java.util.Collection;
022: import java.util.HashMap;
023: import java.util.Iterator;
024: import java.util.Map;
025: import java.util.Properties;
026:
027: import javax.servlet.ServletContext;
028:
029: import org.apache.jetspeed.engine.JetspeedEngineConstants;
030: import org.springframework.beans.factory.config.PropertyPlaceholderConfigurer;
031: import org.springframework.context.ApplicationContext;
032: import org.springframework.context.ConfigurableApplicationContext;
033: import org.springframework.context.support.FileSystemXmlApplicationContext;
034: import org.springframework.context.support.GenericApplicationContext;
035: import org.springframework.web.context.WebApplicationContext;
036: import org.springframework.web.context.support.XmlWebApplicationContext;
037:
038: /**
039: * <p>
040: * SpringComponentManager
041: * </p>
042: * <p>
043: *
044: * </p>
045: *
046: * @author <a href="mailto:weaver@apache.org">Scott T. Weaver</a>
047: * @version $Id: SpringComponentManager.java 517719 2007-03-13 15:05:48Z ate $
048: *
049: */
050: public class SpringComponentManager implements ComponentManager {
051: protected ConfigurableApplicationContext appContext;
052:
053: private ConfigurableApplicationContext bootCtx;
054:
055: protected ArrayList factories;
056:
057: private Map preconfiguredBeans;
058:
059: private boolean started = false;
060:
061: public SpringComponentManager(String[] bootConfigs,
062: String[] appConfigs, ServletContext servletContext,
063: String appRoot) {
064: File appRootDir = new File(appRoot);
065: System.setProperty(
066: JetspeedEngineConstants.APPLICATION_ROOT_KEY,
067: appRootDir.getAbsolutePath());
068:
069: if (bootConfigs != null && bootConfigs.length > 0) {
070: bootCtx = new XmlWebApplicationContext();
071: ((XmlWebApplicationContext) bootCtx)
072: .setServletContext(servletContext);
073: ((XmlWebApplicationContext) bootCtx)
074: .setConfigLocations(bootConfigs);
075: } else {
076: bootCtx = new GenericApplicationContext();
077: }
078:
079: appContext = new XmlWebApplicationContext();
080: ((XmlWebApplicationContext) appContext).setParent(bootCtx);
081: ((XmlWebApplicationContext) appContext)
082: .setServletContext(servletContext);
083: ((XmlWebApplicationContext) appContext)
084: .setConfigLocations(appConfigs);
085:
086: factories = new ArrayList();
087: factories.add(appContext);
088:
089: servletContext
090: .setAttribute(
091: WebApplicationContext.ROOT_WEB_APPLICATION_CONTEXT_ATTRIBUTE,
092: appContext);
093: }
094:
095: public SpringComponentManager(String[] bootConfigs,
096: String[] appConfigs, ServletContext servletContext,
097: String appRoot, Map preconfiguredBeans) {
098: this (bootConfigs, appConfigs, servletContext, appRoot);
099: this .preconfiguredBeans = preconfiguredBeans;
100: }
101:
102: public SpringComponentManager(String[] bootConfigs,
103: String[] appConfigs, String appRoot) {
104: PropertyPlaceholderConfigurer ppc = new PropertyPlaceholderConfigurer();
105: Properties p = new Properties();
106: //p.setProperty(APPLICATION_ROOT_KEY,appRootDir.getAbsolutePath());
107: ppc.setProperties(p);
108:
109: if (bootConfigs != null && bootConfigs.length > 0) {
110: bootCtx = new FileSystemXmlApplicationContext(bootConfigs,
111: false);
112: bootCtx.addBeanFactoryPostProcessor(ppc);
113: bootCtx.refresh();
114: } else {
115: bootCtx = new GenericApplicationContext();
116: }
117:
118: appContext = new FileSystemXmlApplicationContext(appConfigs,
119: false, bootCtx);
120: appContext.addBeanFactoryPostProcessor(ppc);
121: appContext.refresh();
122: factories = new ArrayList();
123: factories.add(appContext);
124:
125: }
126:
127: /**
128: * <p>
129: * getComponent
130: * </p>
131: *
132: * @see org.apache.jetspeed.components.ComponentManagement#getComponent(java.lang.Object)
133: * @param componentName
134: * @return
135: */
136: public Object getComponent(Object componentName) {
137: if (componentName instanceof Class) {
138: return appContext
139: .getBean(((Class) componentName).getName());
140: } else {
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,
157: Object componentName) {
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: return appContext;
172: }
173:
174: /**
175: * <p>
176: * getRootContainer
177: * </p>
178: *
179: * @see org.apache.jetspeed.components.ContainerManagement#getRootContainer()
180: * @return
181: */
182: public Object getRootContainer() {
183: return appContext;
184: }
185:
186: /**
187: * <p>
188: * getContainers
189: * </p>
190: *
191: * @see org.apache.jetspeed.components.ContainerManagement#getContainers()
192: * @return
193: */
194: public Collection getContainers() {
195: return factories;
196: }
197:
198: /**
199: * <p>
200: * stop
201: * </p>
202: *
203: * @see org.apache.jetspeed.components.ContainerManagement#stop()
204: *
205: */
206: public void stop() {
207: appContext.close();
208: bootCtx.close();
209: started = false;
210: }
211:
212: public ApplicationContext getApplicationContext() {
213: return appContext;
214: }
215:
216: public void addComponent(String name, Object bean) {
217: if (preconfiguredBeans == null) {
218: preconfiguredBeans = new HashMap();
219: }
220: preconfiguredBeans.put(name, bean);
221:
222: if (started) {
223: bootCtx.getBeanFactory().registerSingleton(name, bean);
224: }
225: }
226:
227: public void start() {
228: bootCtx.refresh();
229: if (preconfiguredBeans != null) {
230: Iterator itr = preconfiguredBeans.entrySet().iterator();
231: while (itr.hasNext()) {
232: Map.Entry entry = (Map.Entry) itr.next();
233: bootCtx.getBeanFactory().registerSingleton(
234: entry.getKey().toString(), entry.getValue());
235: }
236: }
237:
238: appContext.refresh();
239: started = true;
240: }
241:
242: }
|