/*
* 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 de.odysseus.calyxo.base.ModuleContext;
import de.odysseus.calyxo.base.conf.SetConfig;
import de.odysseus.calyxo.base.conf.ConfigException;
/**
* Set configuration implementation.
*
* @author Christoph Beck
*/
public class SetConfigImpl extends ValueConfigImpl implements SetConfig {
private String var;
private String scopeName;
private int scope;
private Object variable;
/*
* (non-Javadoc)
* @see de.odysseus.calyxo.base.conf.impl.ConfigImpl#_getElementName()
*/
protected String _getElementName() {
return "set";
}
/*
* (non-Javadoc)
* @see de.odysseus.calyxo.base.conf.impl.ConfigImpl#_putAttributes(de.odysseus.calyxo.base.conf.impl.ConfigImpl.Attributes)
*/
protected void _putAttributes(Attributes map) {
super._putAttributes(map);
map.put("name", var);
map.put("scope", scopeName);
}
/*
* (non-Javadoc)
* @see de.odysseus.calyxo.base.conf.impl.ConfigImpl#_evaluate(de.odysseus.calyxo.base.ModuleContext)
*/
protected void _evaluate(ModuleContext context) throws ConfigException {
super._evaluate(context);
Object object;
Object value = eval(context.getClassLoader());
switch (scope) {
case LOCAL_SCOPE:
variable = value;
break;
case MODULE_SCOPE:
object = context.getAttribute(var);
if (object == null) {
context.setAttribute(var, value);
}
break;
case APPLICATION_SCOPE:
object = context.getServletContext().getAttribute(var);
if (object == null) {
context.getServletContext().setAttribute(var, value);
}
break;
}
}
/**
* Lookup variable.
* Answer value, if the specified name matches the receiver's name,
* null else.
*/
Object lookupVariable(String name) {
return scope == LOCAL_SCOPE && this.var.equals(name) ? variable : super.lookupVariable(name);
}
/**
* Get var property
*/
public String getVar() {
return var;
}
/**
* Set var property
*/
public void setVar(String string) {
var = string;
}
/*
* (non-Javadoc)
* @see de.odysseus.calyxo.base.conf.SetConfig#getScope()
*/
public int getScope() {
return scope;
}
/**
* Set scope by name
*/
public void setScopeName(String value) throws ConfigException {
if (value == null || value.equals("local")) {
scope = LOCAL_SCOPE;
} else if (value.equals("module")) {
scope = MODULE_SCOPE;
} else if (value.equals("application")) {
scope = APPLICATION_SCOPE;
} else {
throw new ConfigException("Bad scope name " + value + " in " + toInlineString());
}
scopeName = value;
}
}