/*
* 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.base.conf.impl;
import java.util.Map;
import javax.servlet.ServletContext;
import javax.servlet.jsp.el.ELException;
import javax.servlet.jsp.el.VariableResolver;
import de.odysseus.calyxo.base.ModuleContext;
import de.odysseus.calyxo.base.util.MapFacade;
/**
* This variable resolver supports the following implicite objects:
* <ul>
* <li>moduleContext</li>
* <li>moduleScope</li>
* <li>applicationScope</li>
* </ul>
* This variable resolver searches local variables and scopes
* module and application (in that order).
*
* @author Christoph Beck
*/
public class ConfigImplVariableResolver implements VariableResolver {
private final ModuleContext context;
private final ConfigImpl config;
private Map moduleScope, applicationScope;
/**
* Constructor.
*/
public ConfigImplVariableResolver(ModuleContext context, ConfigImpl config) {
super();
this.context = context;
this.config = config;
}
/* (non-Javadoc)
* @see javax.servlet.jsp.el.VariableResolver#resolveVariable(java.lang.String)
*/
public Object resolveVariable(String variable) throws ELException {
if ("moduleContext".equals(variable)) {
return context;
} else if ("moduleScope".equals(variable)) {
return getModuleScope();
} else if ("applicationScope".equals(variable)) {
return getApplicationScope();
}
Object result = config.lookupVariable(variable);
if (result == null) {
result = context.getAttribute(variable);
if (result == null) {
result = context.getServletContext().getAttribute(variable);
}
}
return result;
}
private Map getApplicationScope() {
if (applicationScope == null) {
applicationScope = new MapFacade() {
ServletContext servletContext = context.getServletContext();
public Object get(Object key) {
if (key == null) {
return null;
}
return servletContext.getAttribute(key.toString());
}
};
}
return applicationScope;
}
private Map getModuleScope() {
if (moduleScope == null) {
moduleScope = new MapFacade() {
public Object get(Object key) {
if (key == null) {
return null;
}
return context.getAttribute(key.toString());
}
};
}
return moduleScope;
}
}