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.base.conf.impl;
017:
018: import de.odysseus.calyxo.base.ModuleContext;
019: import de.odysseus.calyxo.base.conf.SetConfig;
020: import de.odysseus.calyxo.base.conf.ConfigException;
021:
022: /**
023: * Set configuration implementation.
024: *
025: * @author Christoph Beck
026: */
027: public class SetConfigImpl extends ValueConfigImpl implements SetConfig {
028: private String var;
029: private String scopeName;
030:
031: private int scope;
032: private Object variable;
033:
034: /*
035: * (non-Javadoc)
036: * @see de.odysseus.calyxo.base.conf.impl.ConfigImpl#_getElementName()
037: */
038: protected String _getElementName() {
039: return "set";
040: }
041:
042: /*
043: * (non-Javadoc)
044: * @see de.odysseus.calyxo.base.conf.impl.ConfigImpl#_putAttributes(de.odysseus.calyxo.base.conf.impl.ConfigImpl.Attributes)
045: */
046: protected void _putAttributes(Attributes map) {
047: super ._putAttributes(map);
048: map.put("name", var);
049: map.put("scope", scopeName);
050: }
051:
052: /*
053: * (non-Javadoc)
054: * @see de.odysseus.calyxo.base.conf.impl.ConfigImpl#_evaluate(de.odysseus.calyxo.base.ModuleContext)
055: */
056: protected void _evaluate(ModuleContext context)
057: throws ConfigException {
058: super ._evaluate(context);
059:
060: Object object;
061: Object value = eval(context.getClassLoader());
062: switch (scope) {
063: case LOCAL_SCOPE:
064: variable = value;
065: break;
066: case MODULE_SCOPE:
067: object = context.getAttribute(var);
068: if (object == null) {
069: context.setAttribute(var, value);
070: }
071: break;
072: case APPLICATION_SCOPE:
073: object = context.getServletContext().getAttribute(var);
074: if (object == null) {
075: context.getServletContext().setAttribute(var, value);
076: }
077: break;
078: }
079: }
080:
081: /**
082: * Lookup variable.
083: * Answer value, if the specified name matches the receiver's name,
084: * null else.
085: */
086: Object lookupVariable(String name) {
087: return scope == LOCAL_SCOPE && this .var.equals(name) ? variable
088: : super .lookupVariable(name);
089: }
090:
091: /**
092: * Get var property
093: */
094: public String getVar() {
095: return var;
096: }
097:
098: /**
099: * Set var property
100: */
101: public void setVar(String string) {
102: var = string;
103: }
104:
105: /*
106: * (non-Javadoc)
107: * @see de.odysseus.calyxo.base.conf.SetConfig#getScope()
108: */
109: public int getScope() {
110: return scope;
111: }
112:
113: /**
114: * Set scope by name
115: */
116: public void setScopeName(String value) throws ConfigException {
117: if (value == null || value.equals("local")) {
118: scope = LOCAL_SCOPE;
119: } else if (value.equals("module")) {
120: scope = MODULE_SCOPE;
121: } else if (value.equals("application")) {
122: scope = APPLICATION_SCOPE;
123: } else {
124: throw new ConfigException("Bad scope name " + value
125: + " in " + toInlineString());
126: }
127: scopeName = value;
128: }
129: }
|