/*
* Copyright 2004, 2005, 2006 Odysseus Software GmbH
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package de.odysseus.calyxo.control.base;
import java.io.InputStream;
import java.util.ArrayList;
import java.util.Collections;
import java.util.HashMap;
import java.util.List;
import javax.servlet.ServletContext;
import javax.servlet.ServletException;
import org.apache.commons.digester.Digester;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import de.odysseus.calyxo.base.conf.ConfigException;
public final class ControlModuleMappings {
private static final Log log = LogFactory.getLog(ControlModuleMappings.class);
private static final String MODULE_MAPPINGS_KEY =
"de.odysseus.calyxo.control.mappings";
public static ControlModuleMappings getInstance(ServletContext context) throws ServletException {
ControlModuleMappings instance =
(ControlModuleMappings)context.getAttribute(MODULE_MAPPINGS_KEY);
if (instance == null) {
context.setAttribute(MODULE_MAPPINGS_KEY, instance = new ControlModuleMappings(context));
}
return instance;
}
public class WebInfParser {
/**
* Parse web application deployment descriptor.
* @param input
* @throws ServletException
*/
private void parseMappings(InputStream input) throws ServletException {
// Prepare a Digester to scan the web application deployment descriptor
Digester digester = new Digester();
digester.setNamespaceAware(true);
digester.setValidating(false);
// Configure the processing rules that we need
digester.addCallMethod("web-app/servlet-mapping", "add", 2);
digester.addCallParam("web-app/servlet-mapping/servlet-name", 0);
digester.addCallParam("web-app/servlet-mapping/url-pattern", 1);
digester.push(this);
try {
digester.parse(input);
} catch (Exception e) {
throw new ServletException("Could not parse /WEB-INF/web.xml", e);
}
}
/**
* Add mapping for specified servlet name.
* @param servlet servlet name
* @param pattern servlet mapping pattern, eg.
* <code>*.module</code> or <code>/module/*</code>
* @throws ConfigException if the pattern is invalid.
*/
public void add(String servlet, String pattern) throws ConfigException {
log.debug("Adding mapping " + pattern + " --> " + servlet);
addMapping(servlet, new ControlModuleMapping(pattern));
}
}
private HashMap mappings = new HashMap();
public ControlModuleMappings() {
super();
}
ControlModuleMappings(InputStream webxml) throws ServletException {
this();
new WebInfParser().parseMappings(webxml);
}
public ControlModuleMappings(ServletContext context) throws ServletException {
this(context.getResourceAsStream("/WEB-INF/web.xml"));
}
void addMapping(String moduleName, ControlModuleMapping mapping) {
List list = (ArrayList)mappings.get(moduleName);
if (list == null) {
mappings.put(moduleName, list = new ArrayList(4));
}
list.add(mapping);
}
public List getMappings(String moduleName) {
List list = (List)mappings.get(moduleName);
if (list == null) {
return Collections.EMPTY_LIST;
}
return Collections.unmodifiableList(list);
}
}