001: /*
002: * Copyright 2004, 2005, 2006 Odysseus Software GmbH
003: *
004: * Licensed under the Apache License, Version 2.0 (the "License");
005: * you may not use this file except in compliance with the License.
006: * You may obtain a copy of the License at
007: *
008: * http://www.apache.org/licenses/LICENSE-2.0
009: *
010: * Unless required by applicable law or agreed to in writing, software
011: * distributed under the License is distributed on an "AS IS" BASIS,
012: * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
013: * See the License for the specific language governing permissions and
014: * limitations under the License.
015: */
016: package de.odysseus.calyxo.control.base;
017:
018: import java.util.HashMap;
019:
020: import javax.servlet.ServletConfig;
021: import javax.servlet.ServletContext;
022: import javax.servlet.http.HttpServletRequest;
023: import javax.servlet.jsp.el.ExpressionEvaluator;
024:
025: import org.apache.commons.el.ExpressionEvaluatorImpl;
026:
027: import de.odysseus.calyxo.base.conf.ConfigException;
028:
029: /**
030: * Module context implementation.
031: *
032: * @author Christoph Beck
033: */
034: public class ControlModuleContext implements
035: de.odysseus.calyxo.base.ModuleContext {
036: private final ExpressionEvaluator evaluator = new ExpressionEvaluatorImpl();
037: private final ServletConfig config;
038: private final ControlModuleMapping mapping;
039: private final HashMap scope = new HashMap();
040:
041: private ClassLoader loader;
042:
043: /**
044: * Constructor.
045: * @param config servlet configuration
046: * @param mapping servlet mapping we use
047: * @param loader module class loader
048: */
049: public ControlModuleContext(ServletConfig config,
050: ControlModuleMapping mapping, ClassLoader loader) {
051: super ();
052:
053: this .config = config;
054: this .mapping = mapping;
055: this .loader = loader;
056: }
057:
058: /* (non-Javadoc)
059: * @see de.odysseus.calyxo.base.ModuleContext#getAttribute(java.lang.String)
060: */
061: public Object getAttribute(String name) {
062: return scope.get(name);
063: }
064:
065: /* (non-Javadoc)
066: * @see de.odysseus.calyxo.base.ModuleContext#setAttribute(java.lang.String, java.lang.Object)
067: */
068: public void setAttribute(String name, Object value) {
069: scope.put(name, value);
070: }
071:
072: /* (non-Javadoc)
073: * @see de.odysseus.calyxo.base.ModuleContext#removeAttribute(java.lang.String)
074: */
075: public void removeAttribute(String name) {
076: scope.remove(name);
077: }
078:
079: /* (non-Javadoc)
080: * @see de.odysseus.calyxo.control.ModuleContext#getName()
081: */
082: public String getName() {
083: return config.getServletName();
084: }
085:
086: /* (non-Javadoc)
087: * @see de.odysseus.calyxo.base.ModuleContext#getPath(java.lang.String)
088: */
089: public String getPath(String action) {
090: return mapping.getPath(action);
091: }
092:
093: /**
094: * Extract action from request
095: */
096: public String getAction(HttpServletRequest request) {
097: return mapping.getAction(request);
098: }
099:
100: /* (non-Javadoc)
101: * @see de.odysseus.calyxo.base.ModuleContext#getServletContext()
102: */
103: public ServletContext getServletContext() {
104: return config.getServletContext();
105: }
106:
107: /**
108: * Answer the corresponding servlet initialization parameter.
109: */
110: public String getInitParameter(String name) {
111: return config.getInitParameter(name);
112: }
113:
114: /**
115: * @return <code>this.getClassLoader()</code>
116: * @see de.odysseus.calyxo.base.ModuleContext#getClassLoader()
117: */
118: public ClassLoader getClassLoader() {
119: return loader == null ? getClass().getClassLoader() : loader;
120: }
121:
122: /**
123: * Set module class loader.
124: * The class loader may be specified either at construction time
125: * or may be set once via this method. Invoking this method after
126: * a class loader has already been set will throw an exception.
127: * @param loader
128: * @throws ConfigException
129: */
130: public void setClassLoader(ClassLoader loader)
131: throws ConfigException {
132: if (this .loader != null) {
133: throw new ConfigException(
134: "Module class loader must not be set more than once!");
135: }
136: this .loader = loader;
137: }
138:
139: /*
140: * (non-Javadoc)
141: * @see de.odysseus.calyxo.base.ModuleContext#getExpressionEvaluator()
142: */
143: public ExpressionEvaluator getExpressionEvaluator() {
144: return evaluator;
145: }
146: }
|