001: /**********************************************************************************
002: * $URL: https://source.sakaiproject.org/svn/metaobj/tags/sakai_2-4-1/metaobj-util/tool-lib/src/java/org/sakaiproject/metaobj/utils/ioc/web/WebContextLoader.java $
003: * $Id: WebContextLoader.java 14230 2006-09-05 18:02:51Z chmaurer@iupui.edu $
004: ***********************************************************************************
005: *
006: * Copyright (c) 2004, 2005, 2006 The Sakai Foundation.
007: *
008: * Licensed under the Educational Community License, Version 1.0 (the "License");
009: * you may not use this file except in compliance with the License.
010: * You may obtain a copy of the License at
011: *
012: * http://www.opensource.org/licenses/ecl1.php
013: *
014: * Unless required by applicable law or agreed to in writing, software
015: * distributed under the License is distributed on an "AS IS" BASIS,
016: * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
017: * See the License for the specific language governing permissions and
018: * limitations under the License.
019: *
020: **********************************************************************************/package org.sakaiproject.metaobj.utils.ioc.web;
021:
022: import java.io.IOException;
023: import java.util.Properties;
024:
025: import javax.servlet.ServletContext;
026:
027: import org.apache.commons.logging.Log;
028: import org.apache.commons.logging.LogFactory;
029: import org.sakaiproject.component.cover.ComponentManager;
030: import org.sakaiproject.component.impl.SpringCompMgr;
031: import org.sakaiproject.metaobj.utils.ioc.ApplicationContextFactory;
032: import org.springframework.beans.BeanUtils;
033: import org.springframework.beans.BeansException;
034: import org.springframework.context.ApplicationContext;
035: import org.springframework.context.ApplicationContextException;
036: import org.springframework.web.context.ConfigurableWebApplicationContext;
037: import org.springframework.web.context.ContextLoader;
038: import org.springframework.web.context.WebApplicationContext;
039: import org.springframework.web.context.support.XmlWebApplicationContext;
040:
041: public class WebContextLoader extends ContextLoader {
042: private final Log logger = LogFactory.getLog(ContextLoader.class);
043: private static final String WEB_INF_PREFIX = "/WEB-INF";
044:
045: /**
046: * Default context class for ContextLoader.
047: *
048: * @see org.springframework.web.context.support.XmlWebApplicationContext
049: */
050: private static final Class DEFAULT_CONTEXT_CLASS_1_1_5 = XmlWebApplicationContext.class;
051:
052: /**
053: * Instantiate the root WebApplicationContext for this loader, either a default
054: * XmlWebApplicationContext or a custom context class if specified.
055: * This implementation expects custom contexts to implement ConfigurableWebApplicationContext.
056: * Can be overridden in subclasses.
057: *
058: * @throws BeansException if the context couldn't be initialized
059: * @see #CONTEXT_CLASS_PARAM
060: * @see org.springframework.web.context.ConfigurableWebApplicationContext
061: * @see org.springframework.web.context.support.XmlWebApplicationContext
062: */
063: protected WebApplicationContext createWebApplicationContext(
064: ServletContext servletContext, ApplicationContext parent)
065: throws BeansException {
066: String contextClassName = servletContext
067: .getInitParameter(CONTEXT_CLASS_PARAM);
068: Class contextClass = DEFAULT_CONTEXT_CLASS_1_1_5;
069: if (contextClassName != null) {
070: try {
071: contextClass = Class.forName(contextClassName, true,
072: Thread.currentThread().getContextClassLoader());
073: } catch (ClassNotFoundException ex) {
074: throw new ApplicationContextException(
075: "Failed to load context class ["
076: + contextClassName + "]", ex);
077: }
078: if (!ConfigurableWebApplicationContext.class
079: .isAssignableFrom(contextClass)) {
080: throw new ApplicationContextException(
081: "Custom context class ["
082: + contextClassName
083: + "] is not of type ConfigurableWebApplicationContext");
084: }
085: }
086: ConfigurableWebApplicationContext wac = (ConfigurableWebApplicationContext) BeanUtils
087: .instantiateClass(contextClass);
088: //wac.setParent(parent);
089: wac.setParent(((SpringCompMgr) ComponentManager.getInstance())
090: .getApplicationContext());
091: wac.setServletContext(servletContext);
092: String contextFileName = servletContext
093: .getInitParameter(CONFIG_LOCATION_PARAM);
094:
095: try {
096: wac.setConfigLocations(getConfigLocations(contextFileName));
097: } catch (IOException e) {
098: throw new ApplicationContextException(
099: "I/O error loading '"
100: + contextFileName
101: + "' from classpath while creating application context: "
102: + e.getMessage(), e);
103: }
104: wac.refresh();
105: return wac;
106: }
107:
108: protected String[] getConfigLocations(String contextFileName)
109: throws IOException {
110: Properties props = new Properties();
111: props
112: .load(this .getClass().getResourceAsStream(
113: contextFileName));
114: String[] configLocations = ApplicationContextFactory
115: .getInstance().getConfigLocations(props);
116: for (int i = 0; i < configLocations.length; i++) {
117: configLocations[i] = WEB_INF_PREFIX + configLocations[i];
118: }
119: return configLocations;
120: }
121:
122: }
|