/*
* 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.control.conf.impl;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.Iterator;
import de.odysseus.calyxo.control.conf.ExceptionHandlerConfig;
import de.odysseus.calyxo.control.conf.ActionConfig;
import de.odysseus.calyxo.control.conf.DispatchesConfig;
import de.odysseus.calyxo.control.conf.DispatchConfig;
import de.odysseus.calyxo.control.conf.ExceptionHandlersConfig;
import de.odysseus.calyxo.base.conf.ConfigException;
/**
* Action configuration implementation.
*
* @author Christoph Beck
*/
public class ActionConfigImpl extends ParamsConfigImpl implements ActionConfig {
private String path;
private String className;
private String dispatcher;
private HashMap dispatchConfigs = new HashMap();
private HashMap exceptionConfigs = new HashMap();
private ArrayList filterConfigs = new ArrayList();
private ExceptionHandlersConfig globalExceptions;
private DispatchesConfig globalDispatches;
/*
* (non-Javadoc)
* @see de.odysseus.calyxo.base.conf.impl.ConfigImpl#_getDynamicAttributes()
*/
protected String[] _getDynamicAttributes() {
return new String[]{ "className" };
}
/**
* Do the following:
* <ul>
* <li>get global dispatches from 'control' element</li>
* <li>check if source attribute value is valid</li>
* <li>check if target attribute value is valid</li>
* <li>check if either type or target is set</li>
* <li>make sure our path does start with a slash</li>
* </ul>
*/
protected void _init() throws ConfigException {
super._init();
// initialize global dispatches and exceptions
ControlConfigImpl control =
(ControlConfigImpl)_getNearestAncestor(ControlConfigImpl.class);
if (control != null) {
globalDispatches = control.getDispatchesConfig();
globalExceptions = control.getExceptionsConfig();
}
// make sure path is syntactically correct
if (!path.startsWith("/")) {
log.warn("Prepending '/' to action path '" + toInlineString() + "'");
path = "/" + path;
}
}
/*
* (non-Javadoc)
* @see de.odysseus.calyxo.base.conf.impl.ConfigImpl#_getElementName()
*/
protected String _getElementName() {
return "action";
}
/*
* (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(getDispatchConfigs());
list.add(getFilterConfigs());
list.add(getExceptionHandlerConfigs());
}
/*
* (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("path", path);
map.put("type", className);
map.put("dispatcher", dispatcher);
}
/**
* Add dispatch
*/
public void add(DispatchConfigImpl value) throws ConfigException {
if (dispatchConfigs.containsKey(value.getName())) {
throw new ConfigException("Duplicate dispatch name '" + value.getName() + "' in " + toInlineString());
}
dispatchConfigs.put(value.getName(), value);
}
/*
* (non-Javadoc)
* @see de.odysseus.calyxo.control.conf.ActionConfig#findDispatchConfig(java.lang.String)
*/
public DispatchConfig findDispatchConfig(String name) {
DispatchConfig config = (DispatchConfig)dispatchConfigs.get(name);
if (config == null && globalDispatches != null) {
config = globalDispatches.getDispatchConfig(name);
}
return config;
}
/*
* (non-Javadoc)
* @see de.odysseus.calyxo.control.conf.ActionConfig#getDispatchConfigs()
*/
public Iterator getDispatchConfigs() {
return dispatchConfigs.values().iterator();
}
/*
* (non-Javadoc)
* @see de.odysseus.calyxo.control.conf.ActionConfig#getDispatchConfig(java.lang.String)
*/
public DispatchConfig getDispatchConfig(String name) {
return (DispatchConfig)dispatchConfigs.get(name);
}
/**
* Add exception element.
*/
public void add(ExceptionHandlerConfigImpl value) throws ConfigException {
if (exceptionConfigs.containsKey(value.getType())) {
throw new ConfigException("Duplicate exception type '" + value.getClassName() + "' in " + toInlineString());
}
exceptionConfigs.put(value.getType(), value);
}
/*
* (non-Javadoc)
* @see de.odysseus.calyxo.control.conf.ActionConfig#getExceptionHandlerConfig(java.lang.String)
*/
public ExceptionHandlerConfig getExceptionHandlerConfig(String type) {
return (ExceptionHandlerConfig)exceptionConfigs.get(type);
}
/*
* (non-Javadoc)
* @see de.odysseus.calyxo.control.conf.ActionConfig#findExceptionHandlerConfig(java.lang.Exception)
*/
public ExceptionHandlerConfig findExceptionHandlerConfig(Exception exception) {
ExceptionHandlerConfig config = null;
Class type = exception.getClass();
do {
String name = type.getName();
config = getExceptionHandlerConfig(name);
if (config == null && globalExceptions != null) {
config = globalExceptions.getExceptionHandlerConfig(name);
}
type = type.getSuperclass();
} while (type != null && config == null);
return config;
}
/*
* (non-Javadoc)
* @see de.odysseus.calyxo.control.conf.ActionConfig#getExceptionHandlerConfigs()
*/
public Iterator getExceptionHandlerConfigs() {
return exceptionConfigs.values().iterator();
}
/*
* (non-Javadoc)
* @see de.odysseus.calyxo.control.conf.ActionConfig#getPath()
*/
public String getPath() {
return path;
}
/**
* Set action path
*/
public void setPath(String string) {
path = string;
}
/*
* (non-Javadoc)
* @see de.odysseus.calyxo.control.conf.ActionConfig#getClassName()
*/
public String getClassName() {
return className;
}
/**
* Set action class name
*/
public void setClassName(String string) {
className = string;
}
/*
* (non-Javadoc)
* @see de.odysseus.calyxo.control.conf.ActionConfig#getDispatcher()
*/
public String getDispatcher() {
return dispatcher;
}
/**
* Set dispatcher name
*/
public void setDispatcher(String string) {
dispatcher = string;
}
/*
* (non-Javadoc)
* @see de.odysseus.calyxo.control.conf.ActionConfig#getFilterConfigs()
*/
public Iterator getFilterConfigs() {
return filterConfigs.iterator();
}
/**
* Add filter element
*/
public void add(FilterConfigImpl value) throws ConfigException {
filterConfigs.add(value);
}
}