/*
* 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.struts.panels;
import javax.servlet.ServletException;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.apache.struts.action.ActionServlet;
import org.apache.struts.action.PlugIn;
import org.apache.struts.action.RequestProcessor;
import org.apache.struts.config.ControllerConfig;
import org.apache.struts.config.ModuleConfig;
import de.odysseus.calyxo.base.ModuleContext;
import de.odysseus.calyxo.base.conf.ConfigException;
import de.odysseus.calyxo.base.access.AccessSupport;
import de.odysseus.calyxo.panels.PanelsService;
import de.odysseus.calyxo.panels.misc.PanelsAccessor;
import de.odysseus.calyxo.struts.base.StrutsModuleGroup;
/**
*
* @author Christoph Beck
*/
public class PanelsPlugIn implements PlugIn {
private Log log = LogFactory.getLog(PanelsPlugIn.class);
private static final String COMPOSABLE_REQUEST_PROCESSOR = "org.apache.struts.chain.ComposableRequestProcessor";
private String config;
private ModuleContext context;
private PanelsService service;
/* (non-Javadoc)
* @see org.apache.struts.action.PlugIn#init(org.apache.struts.action.ActionServlet, org.apache.struts.config.ModuleConfig)
*/
public void init(ActionServlet servlet, ModuleConfig moduleConfig) throws ServletException {
StrutsModuleGroup modules = StrutsModuleGroup.getInstance(servlet);
context = modules.getStrutsModuleContext(moduleConfig);
if (context == null) {
throw new ServletException("No module context! BasePlugIn installed?");
}
log.debug("Initializing Calyxo Panels for Struts module '" + context.getName() + "'.");
service = new PanelsService();
try {
service.init(context, config);
} catch (ConfigException e) {
throw new ServletException(e);
}
// we have to use a modified request processor
ControllerConfig controllerConfig = moduleConfig.getControllerConfig();
String processor = controllerConfig.getProcessorClass();
if (processor == null || processor.equals(RequestProcessor.class.getName())) {
controllerConfig.setProcessorClass(PanelsRequestProcessor.class.getName());
} else {
// assert processor to be a subclass of PanelsRequestProcessor
// or ComposableRequestProcessor (since struts 1.3)
Class clazz = null;
try {
clazz = context.getClassLoader().loadClass(processor);
} catch (Exception e) {
throw new ServletException("Bad request processor class", e);
}
if (PanelsRequestProcessor.class.isAssignableFrom(clazz)) {
;
} else {
try {
Class composableRequestProcessor =
context.getClassLoader().loadClass(COMPOSABLE_REQUEST_PROCESSOR);
if (composableRequestProcessor.isAssignableFrom(clazz)) {
if (context.getInitParameter("chainConfig") == null) {
throw new ServletException(processor + " requires a customized chain configuration");
}
} else {
throw new ServletException(processor + " is not a subclass of " + PanelsRequestProcessor.class.getName() + " or " + COMPOSABLE_REQUEST_PROCESSOR);
}
} catch (ClassNotFoundException e) { // struts 1.2
throw new ServletException(processor + " is not a subclass of " + PanelsRequestProcessor.class.getName());
}
}
}
AccessSupport access = AccessSupport.getInstance(context);
access.put("panels", new PanelsAccessor(context));
}
/* (non-Javadoc)
* @see org.apache.struts.action.PlugIn#destroy()
*/
public void destroy() {
service.destroy(context);
}
/**
* Set comma-separated list of configuration files
*
* @param string
*/
public void setConfig(String string) {
config = string;
}
}