View Javadoc

1   /*
2    * EL4J, the Extension Library for the J2EE, adds incremental enhancements to
3    * the spring framework, http://el4j.sf.net
4    * Copyright (C) 2005 by ELCA Informatique SA, Av. de la Harpe 22-24,
5    * 1000 Lausanne, Switzerland, http://www.elca.ch
6    *
7    * EL4J is published under the GNU Lesser General Public License (LGPL)
8    * Version 2.1. See http://www.gnu.org/licenses/
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. See the
13   * GNU Lesser General Public License for more details.
14   *
15   * For alternative licensing, please contact info@elca.ch
16   */
17  
18  package ch.elca.el4j.web.context;
19  
20  import org.springframework.beans.BeansException;
21  import org.springframework.util.StringUtils;
22  import org.springframework.web.context.ConfigurableWebApplicationContext;
23  import org.springframework.web.context.WebApplicationContext;
24  import org.springframework.web.servlet.DispatcherServlet;
25  
26  /**
27   * This class extends Springs <code>DispatcherServlet</code>. It inherits the
28   * its complete behaviour but replaces the web application context with a
29   * {@link ch.elca.el4j.web.context.ModuleWebApplicationContext} which
30   * preserves the order of module dependencies when resources are resolved.
31   *
32   * @svnLink $Revision: 4204 $;$Date: 2010-11-02 11:44:37 +0100 (Di, 02. Nov 2010) $;$Author: swisswheel $;$URL: https://el4j.svn.sourceforge.net/svnroot/el4j/branches/el4j_3_1/el4j/framework/modules/web/jar/src/main/java/ch/elca/el4j/web/context/ModuleDispatcherServlet.java $
33   *
34   * @author Andreas Bur (ABU)
35   */
36  public class ModuleDispatcherServlet extends DispatcherServlet {
37  	/**
38  	 * Generated serial version UID.
39  	 */
40  	private static final long serialVersionUID = 2583279389917262839L;
41  
42  	/**
43  	 * The configuration locations that are included to initialize the
44  	 * application context.
45  	 */
46  	private String m_inclusiveLocations;
47  	
48  	/**
49  	 * The configuration locations that are excluded during the application
50  	 * context's initialization.
51  	 */
52  	private String m_exclusiveLocations;
53  	
54  	/** Holds whether overriding of already defined beans is allowed. */
55  	private boolean m_allowBeanDefinitionOverriding = false;
56  
57  	/**
58  	 * Whether to merge the configuration locations defined in the manifest
59  	 * files have to be merged with files looked up in the file system.
60  	 */
61  	private boolean m_mergeWithOuterResources = false;
62  	
63  	/**
64  	 * {@inheritDoc}
65  	 */
66  	protected WebApplicationContext createWebApplicationContext(
67  			WebApplicationContext parent) throws BeansException {
68  		
69  		if (logger.isDebugEnabled()) {
70  			logger.debug("Servlet with name '" + getServletName()
71  					+ "' will try to create custom WebApplicationContext "
72  					+ "context of class '" + getContextClass().getName() + "'"
73  					+ ", using parent context [" + parent + "]");
74  		}
75  
76  		String[] incl = toStringArray(getInclusiveLocations());
77  		String[] excl = toStringArray(getExclusiveLocations());
78  		
79  		ModuleWebApplicationContext wac
80  			= new ModuleWebApplicationContext(incl, excl,
81  					isAllowBeanDefinitionOverriding(),
82  					getServletContext(),
83  					m_mergeWithOuterResources, parent);
84  		
85  		wac.setNamespace(getNamespace());
86  		wac.refresh();
87  		return wac;
88  	}
89  
90  	/**
91  	 * Sets whether it's allowed to override bean definitions, as it may happen
92  	 * by using the same bean name in different configuration files, which
93  	 * are loaded sequentially.
94  	 *
95  	 * @param allowBeanDefinitionOverriding
96  	 *      Whether it's allowed to override bean definitions.
97  	 */
98  	public void setAllowBeanDefinitionOverriding(
99  			boolean allowBeanDefinitionOverriding) {
100 		
101 		m_allowBeanDefinitionOverriding = allowBeanDefinitionOverriding;
102 	}
103 
104 	/**
105 	 * Sets the list of configuration files that have to be excluded from
106 	 * the application context's configuration.
107 	 *
108 	 * @param exclusiveLocations
109 	 *      Configuration locations to exclude.
110 	 */
111 	public void setExclusiveLocations(String exclusiveLocations) {
112 		m_exclusiveLocations = exclusiveLocations;
113 	}
114 
115 	/**
116 	 * Sets the list of configuration files that have to be included when the
117 	 * application context is initialized.
118 	 *
119 	 * @param inclusiveLocations
120 	 *      Configuration locations to include.
121 	 */
122 	public void setInclusiveLocations(String inclusiveLocations) {
123 		m_inclusiveLocations = inclusiveLocations;
124 	}
125 	
126 	/**
127 	 * @return Returns whether it's allowed to override already exisiting
128 	 *      bean definitions.
129 	 */
130 	public boolean isAllowBeanDefinitionOverriding() {
131 		return m_allowBeanDefinitionOverriding;
132 	}
133 
134 	/**
135 	 * @return Returns the list of configuration locations that are excluded
136 	 *      when the application context is initialized.
137 	 */
138 	public String getExclusiveLocations() {
139 		return m_exclusiveLocations;
140 	}
141 
142 	/**
143 	 * @return Returns the list of configuration locations that are used to
144 	 *      initialize the application context.
145 	 */
146 	public String getInclusiveLocations() {
147 		String result;
148 		if (m_inclusiveLocations == null) {
149 			result = super.getContextConfigLocation();
150 		} else {
151 			result = m_inclusiveLocations;
152 		}
153 		return result;
154 	}
155 
156 	/**
157 	 * {@inheritDoc}
158 	 */
159 	public String getContextConfigLocation() {
160 		return m_inclusiveLocations;
161 	}
162 
163 	/**
164 	 * @return Returns <code>true</code> if the resource pattern resolver merges
165 	 *      items found in the manifest configuration section with items found
166 	 *      in the file system. <code>false</code> otherwise.
167 	 */
168 	public boolean isMergeWithOuterResources() {
169 		return m_mergeWithOuterResources;
170 	}
171 
172 	/**
173 	 * Sets whether configuration locations retrieved by the pattern resolver
174 	 * have to be merged with resources looked up in the file system.
175 	 *
176 	 * @param mergeWithOuterResources
177 	 *      <code>true</code> for merging configuration locations provided
178 	 *      through the manifest file with resources looked up in the file
179 	 *      system. <code>false</code> otherwise.
180 	 */
181 	public void setMergeWithOuterResources(boolean mergeWithOuterResources) {
182 		m_mergeWithOuterResources = mergeWithOuterResources;
183 	}
184 
185 	/**
186 	 * Transforms a string of tokens into a string array. The tokens have to
187 	 * be separated by {@link
188 	 * ConfigurableWebApplicationContext#CONFIG_LOCATION_DELIMITERS}.
189 	 *
190 	 * @param str
191 	 *      The string to parse.
192 	 *
193 	 * @return Returns the given string's token as array.
194 	 */
195 	private String[] toStringArray(String str) {
196 		String[] result = new String[0];
197 		if (str != null) {
198 			result =  StringUtils.tokenizeToStringArray(str,
199 				ConfigurableWebApplicationContext.CONFIG_LOCATION_DELIMITERS);
200 		}
201 		return result;
202 	}
203 }