/*
* 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.lang.reflect.Method;
import java.lang.reflect.Modifier;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.Iterator;
import de.odysseus.calyxo.base.ModuleContext;
import de.odysseus.calyxo.base.conf.ConfigException;
/**
* Base implementation class for root elements.
* A root element may have variables and offers some public
* methods for initialization. A root element also must
* implement the <code>merge(RootConfigImpl)</code>. A root element
* may also have parameters, which are used to resolve variables.
*
* @author Christoph Beck
*/
public abstract class RootConfigImpl extends StatementsConfigImpl {
private HashMap functions = new HashMap();
private ArrayList imports = new ArrayList();
private HashMap functionsMap = new HashMap();
/*
* (non-Javadoc)
* @see de.odysseus.calyxo.base.conf.impl.ConfigImpl#_addChildren(de.odysseus.calyxo.base.conf.impl.ConfigImpl.Elements)
*/
protected void _addChildren(Elements list) {
super._addChildren(list);
list.add(getFunctionsConfigs());
list.add(getImportConfigs());
}
/**
* merge with other root.
* @param other
*/
protected abstract void merge(RootConfigImpl other) throws ConfigException;
/**
* resolve according to <code>functions</code> elements
* @see javax.servlet.jsp.el.FunctionMapper#resolveFunction(java.lang.String, java.lang.String)
*/
public Method resolveFunction(String prefix, String name) {
return (Method)functionsMap.get(prefix + ":" + name);
}
/**
* resolve dynamic attributes
*
* @throws ConfigException
*/
public void resolve(ModuleContext context) throws ConfigException {
Iterator functions = getFunctionsConfigs();
while (functions.hasNext()) {
FunctionsConfigImpl value = (FunctionsConfigImpl)functions.next();
Class clazz = null;
try {
clazz = context.getClassLoader().loadClass(value.getClassName());
} catch (ClassNotFoundException e) {
throw new ConfigException("Could not find functions class '" + value.getClassName() + "'", e);
}
Method[] methods = clazz.getDeclaredMethods();
for (int i = 0; i < methods.length; i++) {
int mod = methods[i].getModifiers();
if (Modifier.isStatic(mod) && Modifier.isPublic(mod)) {
functionsMap.put(value.getPrefix() + ":" + methods[i].getName(), methods[i]);
}
}
}
_resolveTree(context);
}
/**
* initialize elements.
* Traverse elements in preorder and call their <code>_init</code>
* method.
*
* @throws ConfigException pass through exception from elements.
*/
public void initialize() throws ConfigException {
_initTree();
}
/**
* Add functions element
*/
public void add(FunctionsConfigImpl value) throws ConfigException {
String key = value.getPrefix();
if (functions.containsKey(key)) {
throw new ConfigException("Duplicate functions for prefix '" + key + "' in " + toInlineString());
}
functions.put(value.getPrefix(), value);
}
/**
* Answer function elements
*/
public Iterator getFunctionsConfigs() {
return functions.values().iterator();
}
/**
* Add import element
*/
public void add(ImportConfigImpl value) {
imports.add(value);
}
/**
* Answer import elements
*/
public Iterator getImportConfigs() {
return imports.iterator();
}
}