/*
* 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.util.HashMap;
import javax.servlet.ServletConfig;
import javax.servlet.ServletContext;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.jsp.el.ExpressionEvaluator;
import org.apache.commons.el.ExpressionEvaluatorImpl;
import de.odysseus.calyxo.base.conf.ConfigException;
/**
* Module context implementation.
*
* @author Christoph Beck
*/
public class ControlModuleContext implements de.odysseus.calyxo.base.ModuleContext {
private final ExpressionEvaluator evaluator = new ExpressionEvaluatorImpl();
private final ServletConfig config;
private final ControlModuleMapping mapping;
private final HashMap scope = new HashMap();
private ClassLoader loader;
/**
* Constructor.
* @param config servlet configuration
* @param mapping servlet mapping we use
* @param loader module class loader
*/
public ControlModuleContext(ServletConfig config, ControlModuleMapping mapping, ClassLoader loader) {
super();
this.config = config;
this.mapping = mapping;
this.loader = loader;
}
/* (non-Javadoc)
* @see de.odysseus.calyxo.base.ModuleContext#getAttribute(java.lang.String)
*/
public Object getAttribute(String name) {
return scope.get(name);
}
/* (non-Javadoc)
* @see de.odysseus.calyxo.base.ModuleContext#setAttribute(java.lang.String, java.lang.Object)
*/
public void setAttribute(String name, Object value) {
scope.put(name, value);
}
/* (non-Javadoc)
* @see de.odysseus.calyxo.base.ModuleContext#removeAttribute(java.lang.String)
*/
public void removeAttribute(String name) {
scope.remove(name);
}
/* (non-Javadoc)
* @see de.odysseus.calyxo.control.ModuleContext#getName()
*/
public String getName() {
return config.getServletName();
}
/* (non-Javadoc)
* @see de.odysseus.calyxo.base.ModuleContext#getPath(java.lang.String)
*/
public String getPath(String action) {
return mapping.getPath(action);
}
/**
* Extract action from request
*/
public String getAction(HttpServletRequest request) {
return mapping.getAction(request);
}
/* (non-Javadoc)
* @see de.odysseus.calyxo.base.ModuleContext#getServletContext()
*/
public ServletContext getServletContext() {
return config.getServletContext();
}
/**
* Answer the corresponding servlet initialization parameter.
*/
public String getInitParameter(String name) {
return config.getInitParameter(name);
}
/**
* @return <code>this.getClassLoader()</code>
* @see de.odysseus.calyxo.base.ModuleContext#getClassLoader()
*/
public ClassLoader getClassLoader() {
return loader == null ? getClass().getClassLoader() : loader;
}
/**
* Set module class loader.
* The class loader may be specified either at construction time
* or may be set once via this method. Invoking this method after
* a class loader has already been set will throw an exception.
* @param loader
* @throws ConfigException
*/
public void setClassLoader(ClassLoader loader) throws ConfigException {
if (this.loader != null) {
throw new ConfigException("Module class loader must not be set more than once!");
}
this.loader = loader;
}
/*
* (non-Javadoc)
* @see de.odysseus.calyxo.base.ModuleContext#getExpressionEvaluator()
*/
public ExpressionEvaluator getExpressionEvaluator() {
return evaluator;
}
}