001: /**
002: * Copyright 2006 Webmedia Group Ltd.
003: *
004: * Licensed under the Apache License, Version 2.0 (the "License");
005: * you may not use this file except in compliance with the License.
006: * You may obtain a copy of the License at
007: *
008: * http://www.apache.org/licenses/LICENSE-2.0
009: *
010: * Unless required by applicable law or agreed to in writing, software
011: * distributed under the License is distributed on an "AS IS" BASIS,
012: * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
013: * See the License for the specific language governing permissions and
014: * limitations under the License.
015: **/package org.araneaframework.integration.spring;
016:
017: import java.io.IOException;
018: import java.net.MalformedURLException;
019: import java.util.HashMap;
020: import java.util.Map;
021: import java.util.Properties;
022: import javax.servlet.ServletException;
023: import org.araneaframework.core.AraneaRuntimeException;
024: import org.araneaframework.core.util.ClassLoaderUtil;
025: import org.araneaframework.http.ServletServiceAdapterComponent;
026: import org.araneaframework.http.core.BaseAraneaDispatcherServlet;
027: import org.springframework.beans.factory.BeanFactory;
028: import org.springframework.beans.factory.config.ConfigurableListableBeanFactory;
029: import org.springframework.beans.factory.config.PropertyPlaceholderConfigurer;
030: import org.springframework.beans.factory.support.BeanDefinitionRegistry;
031: import org.springframework.beans.factory.support.DefaultListableBeanFactory;
032: import org.springframework.beans.factory.support.RootBeanDefinition;
033: import org.springframework.beans.factory.xml.XmlBeanDefinitionReader;
034: import org.springframework.context.ApplicationContext;
035: import org.springframework.context.ConfigurableApplicationContext;
036: import org.springframework.core.io.ClassPathResource;
037: import org.springframework.web.context.WebApplicationContext;
038: import org.springframework.web.context.support.ServletContextResource;
039: import org.springframework.web.context.support.WebApplicationContextUtils;
040:
041: /**
042: * Aranea dispatcher servlet that assembles Aranea components according
043: * to the configuration specified in Spring bean configuration files.
044: */
045: public class AraneaSpringDispatcherServlet extends
046: BaseAraneaDispatcherServlet {
047: private static boolean isSpringWebPresent = true;
048:
049: static {
050: try {
051: Class
052: .forName("org.springframework.web.context.WebApplicationContext");
053: } catch (ClassNotFoundException e) {
054: isSpringWebPresent = false;
055: }
056: }
057:
058: public static final String ARANEA_START = "araneaApplicationStart";
059:
060: public static final String ARANEA_DEFAULT_CONF_XML = "conf/default-aranea-conf.xml";
061: public static final String ARANEA_DEFAULT_CONF_PROPERTIES = "conf/default-aranea-conf.properties";
062:
063: public static final String DEFAULT_ARANEA_ROOT = "araneaApplicationRoot";
064: public static final String DEFAULT_ARANEA_CUSTOM_CONF_XML = "/WEB-INF/aranea-conf.xml";
065: public static final String DEFAULT_ARANEA_CUSTOM_CONF_PROPERTIES = "/WEB-INF/aranea-conf.properties";
066:
067: public static final String ARANEA_CUSTOM_CONF_XML_INIT_PARAMETER = "araneaCustomConfXML";
068: public static final String ARANEA_CUSTOM_CONF_PROPERTIES_INIT_PARAMETER = "araneaCustomConfProperties";
069: public static final String ARANEA_START_CLASS_INIT_PARAMETER = "araneaApplicationStart";
070: public static final String ARANEA_ROOT_INIT_PARAMETER = "araneaApplicationRoot";
071:
072: protected BeanFactory webCtx;
073: protected BeanFactory beanFactory;
074:
075: public void init() throws ServletException {
076: //Reading init-param's
077: String araneaCustomConfXml = DEFAULT_ARANEA_CUSTOM_CONF_XML;
078: if (getServletConfig().getInitParameter(
079: ARANEA_CUSTOM_CONF_XML_INIT_PARAMETER) != null)
080: araneaCustomConfXml = getServletConfig().getInitParameter(
081: ARANEA_CUSTOM_CONF_XML_INIT_PARAMETER);
082:
083: String araneaCustomConfProperties = DEFAULT_ARANEA_CUSTOM_CONF_PROPERTIES;
084: if (getServletConfig().getInitParameter(
085: ARANEA_CUSTOM_CONF_PROPERTIES_INIT_PARAMETER) != null)
086: araneaCustomConfProperties = getServletConfig()
087: .getInitParameter(
088: ARANEA_CUSTOM_CONF_PROPERTIES_INIT_PARAMETER);
089:
090: isSpringWebPresent = isSpringWebPresent ? WebApplicationContextUtils
091: .getWebApplicationContext(getServletContext()) != null
092: : isSpringWebPresent;
093:
094: if (isSpringWebPresent()) {
095: //Getting the Spring loaded main web application context
096: webCtx = WebApplicationContextUtils
097: .getWebApplicationContext(getServletContext());
098: beanFactory = ((ConfigurableApplicationContext) webCtx)
099: .getBeanFactory();
100: } else {
101: beanFactory = new DefaultListableBeanFactory();
102: }
103:
104: // Loading default Aranea configuration
105: XmlBeanDefinitionReader confReader = new XmlBeanDefinitionReader(
106: (BeanDefinitionRegistry) beanFactory);
107: confReader.loadBeanDefinitions(new ClassPathResource(
108: ARANEA_DEFAULT_CONF_XML));
109:
110: //Loading default properties
111: PropertyPlaceholderConfigurer cfg = new PropertyPlaceholderConfigurer();
112: cfg.setLocation(new ClassPathResource(
113: ARANEA_DEFAULT_CONF_PROPERTIES));
114: cfg.setIgnoreUnresolvablePlaceholders(true);
115:
116: //Loading custom properties
117: Properties localConf = new Properties();
118: try {
119: if (getServletContext().getResource(
120: araneaCustomConfProperties) != null)
121: localConf.load(getServletContext().getResourceAsStream(
122: araneaCustomConfProperties));
123: } catch (IOException e) {
124: throw new ServletException(e);
125: }
126: cfg.setProperties(localConf);
127: cfg.setLocalOverride(true);
128:
129: //Loading custom configuration
130: try {
131: if (getServletContext().getResource(araneaCustomConfXml) != null) {
132: confReader
133: .loadBeanDefinitions(new ServletContextResource(
134: getServletContext(),
135: araneaCustomConfXml));
136: }
137: } catch (MalformedURLException e) {
138: throw new AraneaRuntimeException(e);
139: }
140:
141: //Applying properties to active configuration
142: cfg
143: .postProcessBeanFactory((ConfigurableListableBeanFactory) beanFactory);
144:
145: //Reading the starting widget from an init parameter
146: if (getServletConfig().getInitParameter(
147: ARANEA_START_CLASS_INIT_PARAMETER) != null) {
148: Class startClass;
149: try {
150: startClass = ClassLoaderUtil
151: .loadClass(getServletConfig().getInitParameter(
152: ARANEA_START_CLASS_INIT_PARAMETER));
153: } catch (ClassNotFoundException e) {
154: throw new AraneaRuntimeException(e);
155: }
156:
157: RootBeanDefinition startBeanDef = new RootBeanDefinition(
158: startClass);
159: startBeanDef.setSingleton(false);
160: ((BeanDefinitionRegistry) beanFactory)
161: .registerBeanDefinition(ARANEA_START, startBeanDef);
162: }
163:
164: super .init();
165: }
166:
167: protected ServletServiceAdapterComponent buildRootComponent() {
168: //Getting the Aranea root component name
169: String araneaRoot = DEFAULT_ARANEA_ROOT;
170: if (getServletConfig().getInitParameter(
171: ARANEA_ROOT_INIT_PARAMETER) != null)
172: araneaRoot = getServletConfig().getInitParameter(
173: ARANEA_ROOT_INIT_PARAMETER);
174:
175: //Creating the root bean
176: ServletServiceAdapterComponent adapter = (ServletServiceAdapterComponent) beanFactory
177: .getBean(araneaRoot);
178: return adapter;
179: }
180:
181: protected Map getEnvironmentEntries() {
182: Map result = new HashMap();
183: result.put(BeanFactory.class, beanFactory);
184: if (isSpringWebPresent()) {
185: result.put(ApplicationContext.class, webCtx);
186: result.put(WebApplicationContext.class, webCtx);
187: }
188: return result;
189: }
190:
191: /**
192: * Returns <code>true</code> iff Spring web application context is present. When
193: * web application context is present, dispatcher servlet will use it, instead of
194: * creating new BeanFactory itself.
195: *
196: * This method should only be called after {@link AraneaSpringDispatcherServlet#init()} has run.
197: * @since 1.1
198: */
199: protected boolean isSpringWebPresent() {
200: return isSpringWebPresent;
201: }
202: }
|