/*
* 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;
import java.util.HashMap;
import de.odysseus.calyxo.base.ModuleContext;
import de.odysseus.calyxo.base.conf.ConfigException;
/**
* Plugin context.
* An instance of this class is passed to plugins during initialization.
* A plugin may register a {@link Filter} class or a {@link Dispatcher}
* instance.
*
* @author Christoph Beck
*/
public class PluginContext {
private Dispatcher dispatcher;
private final Dispatcher defaultDispatcher;
private final HashMap dispatchers = new HashMap();
private final HashMap filters = new HashMap();
private final Class initialDefaultActionClass;
private Class defaultActionClass;
private final ModuleContext module;
/**
* Constructor.
* @param module our module context
*/
public PluginContext(ModuleContext module, Class defaultActionClass, Dispatcher defaultDispatcher) {
super();
this.module = module;
this.defaultDispatcher = defaultDispatcher;
this.dispatcher = defaultDispatcher;
this.defaultActionClass = defaultActionClass;
this.initialDefaultActionClass = defaultActionClass;
}
/**
* Answer our module context.
*/
public ModuleContext getModuleContext() {
return module;
}
/**
* Get filter class for specified name
*/
public Class getFilterClass(String name) {
return (Class)filters.get(name);
}
/**
* Set filter class for specified name
*/
public void setFilterClass(String name, Class clazz) throws ConfigException {
if (filters.containsKey(name)) {
throw new ConfigException("Filter '" + name + "' already exists");
}
if (clazz.isInterface() || !Filter.class.isAssignableFrom(clazz)) {
throw new ConfigException("Bad filter class '" + clazz.getName() + "'");
}
filters.put(name, clazz);
}
/**
* Get dispatcher for specified name
*/
public Dispatcher getDispatcher(String name) {
return (Dispatcher)dispatchers.get(name);
}
/**
* Set dispatcher for specified name
*/
public void setDispatcher(String name, Dispatcher value) throws ConfigException {
if (dispatchers.containsKey(name)) {
throw new ConfigException("Dispatcher '" + name + "' already exists");
}
dispatchers.put(name, value);
}
/**
* Set the default dispatcher.
*/
public void setDefaultDispatcher(Dispatcher dispatcher) throws ConfigException {
if (this.dispatcher != defaultDispatcher) {
throw new ConfigException("Default dispatcher already set");
}
this.dispatcher = dispatcher;
}
/**
* Get the default dispatcher
*/
public Dispatcher getDefaultDispatcher() {
return dispatcher;
}
/**
* Get the default action class
*/
public Class getDefaultActionClass() {
return defaultActionClass;
}
/**
* Set the default action class
*/
public void setDefaultActionClass(Class clazz) throws ConfigException {
if (defaultActionClass != initialDefaultActionClass) {
throw new ConfigException("Default action class already set");
}
if (clazz.isInterface() || !Action.class.isAssignableFrom(clazz)) {
throw new ConfigException("Bad action class '" + clazz.getName() + "'");
}
defaultActionClass = clazz;
}
}