1 /**
2 * Copyright 2005-2008 the original author or authors.
3 *
4 * Licensed under the Gnu General Pubic License, Version 2.0 (the
5 * "License"); you may not use this file except in compliance with
6 * the License. You may obtain a copy of the License at
7 *
8 * http://www.opensource.org/licenses/gpl-license.php
9 *
10 * This program is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
13 * See the Gnu General Public License for more details.
14 */
15 package org.figure8.join.core;
16
17 import org.figure8.join.core.setup.BootstrapUtil;
18 import org.figure8.join.core.setup.BootstrapManager;
19 import org.figure8.join.util.LogUtil;
20
21 import org.apache.commons.logging.Log;
22 import org.springframework.beans.BeanUtils;
23 import org.springframework.beans.BeansException;
24 import org.springframework.context.ApplicationContext;
25 import org.springframework.web.context.ContextLoader;
26 import org.springframework.web.context.WebApplicationContext;
27 import org.springframework.web.context.ConfigurableWebApplicationContext;
28 import org.springframework.util.StringUtils;
29
30 import javax.servlet.ServletContext;
31 /**
32 * An extension of Spring <code>ContextLoader</code> that always load the
33 * Join bootstrap context as parent context.
34 * @author <a href="mailto:laurent.broudoux@free.fr">Laurent Broudoux</a>
35 * @version $Revision: 1.4 $
36 */
37 public class ContainerContextLoader extends ContextLoader{
38
39
40
41 /** Get a commons logger ... */
42 private static final Log log = LogUtil.getLog(ContainerContextLoader.class);
43
44 /**
45 * Name of servlet context parameter that can specify the extra config location
46 * for the root context. This locations may be added to those present in default
47 * config location depending on the result of <code>addingExtraConfigLocationAllowed()</code>
48 */
49 public static final String EXTRA_CONFIG_LOCATION_PARAM = "extraContextConfigLocation";
50
51
52
53
54 /** Creates a new instance of ContainerContextLoader */
55 public ContainerContextLoader(){
56 super();
57 }
58
59
60
61
62 /**
63 * Load the parent context for the given servlet context. Always use
64 * the Join bootstrap context in this case.
65 * @param servletContext Servlet context for whom load parent context
66 * @return The Join bootstrap context.
67 */
68 protected ApplicationContext loadParentContext(ServletContext servletContext) throws BeansException{
69 return BootstrapUtil.getBootstrapContext();
70 }
71
72 /**
73 * Instantiate the root WebApplicationContext for this loader, ie a XmlWebApplicationContext.
74 * <p>This implementation expects custom contexts to implement
75 * ConfigurableWebApplicationContext. Can be overridden in subclasses.
76 * @param servletContext current servlet context
77 * @param parent the parent ApplicationContext to use, or null if none
78 * @throws org.springframework.beans.BeansException if the context couldn't be initialized
79 * @return The newly created Join web application context
80 */
81 protected WebApplicationContext createWebApplicationContext(ServletContext servletContext, ApplicationContext parent)
82 throws BeansException{
83
84 ConfigurableWebApplicationContext wac = (ConfigurableWebApplicationContext)
85 BeanUtils.instantiateClass(DEFAULT_CONTEXT_CLASS);
86
87 wac.setParent(parent);
88 wac.setServletContext(servletContext);
89
90
91 String configLocation = servletContext.getInitParameter(CONFIG_LOCATION_PARAM);
92
93 if (addingExtraConfigLocationAllowed()){
94 String extraConfigLocation = servletContext.getInitParameter(EXTRA_CONFIG_LOCATION_PARAM);
95 if (extraConfigLocation != null && extraConfigLocation.trim().length() > 0)
96 configLocation += (ConfigurableWebApplicationContext.CONFIG_LOCATION_DELIMITERS + extraConfigLocation);
97 }
98 if (configLocation != null){
99 wac.setConfigLocations(StringUtils.tokenizeToStringArray(configLocation,
100 ConfigurableWebApplicationContext.CONFIG_LOCATION_DELIMITERS));
101 }
102
103 wac.refresh();
104 return wac;
105 }
106
107
108
109
110 /**
111 * Check if some extra configuration files locations should be added
112 * to the default one. This is a hook for subclasses to specifiy their
113 * adding criteria. This implementation return true if this context is one
114 * the asynchronous side of the application (so that asynchronous applicative
115 * services may be started)
116 * @return True if we are on asynchronous side of the application, false otherwise.
117 */
118 protected boolean addingExtraConfigLocationAllowed(){
119 BootstrapManager manager = BootstrapUtil.getBootstrapManager();
120
121 if (manager == null || !manager.isBootstrapped())
122 return false;
123
124 if (!manager.isSetupComplete())
125 log.error("Setup is not yet complete, but trying to load context ?!... - Maybe join.cfg.xml is corrupted ?");
126
127 return manager.isAsynchronousSide();
128 }
129 }