001: /**********************************************************************************
002: * $URL: https://source.sakaiproject.org/svn/component/tags/sakai_2-4-1/component-api/component/src/java/org/sakaiproject/component/impl/ContextLoader.java $
003: * $Id: ContextLoader.java 6821 2006-03-21 18:34:36Z ggolden@umich.edu $
004: ***********************************************************************************
005: *
006: * Copyright (c) 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.component.impl;
021:
022: import java.io.IOException;
023:
024: import javax.servlet.ServletContext;
025:
026: import org.apache.commons.logging.Log;
027: import org.apache.commons.logging.LogFactory;
028: import org.sakaiproject.component.cover.ComponentManager;
029: import org.springframework.beans.BeansException;
030: import org.springframework.beans.factory.support.BeanDefinitionRegistry;
031: import org.springframework.beans.factory.xml.XmlBeanDefinitionReader;
032: import org.springframework.context.ApplicationContext;
033: import org.springframework.context.ConfigurableApplicationContext;
034: import org.springframework.util.StringUtils;
035: import org.springframework.web.context.ConfigurableWebApplicationContext;
036: import org.springframework.web.context.WebApplicationContext;
037:
038: /**
039: * <p>
040: * Sakai's extension to Spring's ContextLoader - adds the location of the ComponentManager shared AC, linking the local AC to it as parent, and loading localy hosted components into shared.
041: * </p>
042: */
043: public class ContextLoader extends
044: org.springframework.web.context.ContextLoader {
045: /** Our logger. */
046: private static Log M_log = LogFactory.getLog(ContextLoader.class);
047:
048: /** Name of servlet context parameter that can specify the config location for loading into the shared component set. */
049: public static final String SHARED_LOCATION_PARAM = "contextSharedLocation";
050:
051: /**
052: * Initialize the local ApplicationContext, link it to the shared context, and load shared definitions into the shared context.
053: *
054: * @param servletContext
055: * current servlet context
056: * @return the new WebApplicationContext
057: * @throws BeansException
058: * if the context couldn't be initialized
059: */
060: public WebApplicationContext initWebApplicationContext(
061: ServletContext servletContext) throws BeansException {
062: WebApplicationContext rv = super
063: .initWebApplicationContext(servletContext);
064:
065: // if we have a parent and any shared bean definitions, load them into the parent
066: ConfigurableApplicationContext parent = (ConfigurableApplicationContext) rv
067: .getParent();
068: if (parent != null) {
069: String sharedConfig = servletContext
070: .getInitParameter(SHARED_LOCATION_PARAM);
071: if (sharedConfig != null) {
072: String[] locations = StringUtils
073: .tokenizeToStringArray(
074: sharedConfig,
075: ConfigurableWebApplicationContext.CONFIG_LOCATION_DELIMITERS);
076: if (locations != null) {
077: XmlBeanDefinitionReader reader = new XmlBeanDefinitionReader(
078: (BeanDefinitionRegistry) parent
079: .getBeanFactory());
080:
081: for (int i = 0; i < locations.length; i++) {
082: try {
083: reader.loadBeanDefinitions(rv
084: .getResources(locations[i]));
085: } catch (IOException e) {
086: M_log
087: .warn("exception loading into parent: "
088: + e);
089: }
090: }
091: }
092: }
093: }
094:
095: return rv;
096: }
097:
098: /**
099: * Access the shared ApplicationContext
100: *
101: * @param servletContext
102: * (not used)
103: * @return The shared application context
104: */
105: protected ApplicationContext loadParentContext(
106: ServletContext servletContext) throws BeansException {
107: // get the component manager (we know it's a SpringCompMgr) and from that the shared AC
108: ConfigurableApplicationContext sharedAc = ((SpringCompMgr) ComponentManager
109: .getInstance()).getApplicationContext();
110:
111: return sharedAc;
112: }
113: }
|