/*
* 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.base;
import java.net.MalformedURLException;
import java.net.URL;
import java.util.ArrayList;
import java.util.StringTokenizer;
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.config.ModuleConfig;
import de.odysseus.calyxo.base.I18nSupport;
import de.odysseus.calyxo.base.ModuleContext;
import de.odysseus.calyxo.base.misc.BaseAccessor;
import de.odysseus.calyxo.base.access.AccessSupport;
import de.odysseus.calyxo.base.conf.ConfigException;
import de.odysseus.calyxo.base.conf.impl.BaseRootConfigParser;
import de.odysseus.calyxo.base.conf.impl.RootConfigImpl;
import de.odysseus.calyxo.struts.misc.StrutsAccessor;
/**
* Calyxo base plugin.
* This plugin installs a struts module context with i18n and access support.
*
* @author Christoph Beck
*/
public class BasePlugIn implements PlugIn {
private Log log = LogFactory.getLog(BasePlugIn.class);
private String config;
private String module;
private StrutsModuleContext context;
private StrutsModuleGroup modules;
private static RootConfigImpl parseConfigs(ModuleContext context, String configs) throws ConfigException {
// Create list of urls to config files
ArrayList list = new ArrayList();
StringTokenizer tokens = new StringTokenizer(configs,",");
while (tokens.hasMoreTokens()) {
String token = tokens.nextToken().trim();
URL url = null;
try {
url = context.getServletContext().getResource(token);
} catch (MalformedURLException e) {
throw new ConfigException(e);
}
if (url == null) {
throw new ConfigException("Could not find resource " + token);
} else {
list.add(url);
}
}
URL[] inputs = (URL[])list.toArray(new URL[list.size()]);
// parse files into configuration bean
BaseRootConfigParser parser = new BaseRootConfigParser(context);
return parser.parse(inputs);
}
/* (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 {
// get module name (default is module prefix)
String name = module != null ? module : moduleConfig.getPrefix();
log.debug("Initializing Calyxo Base for Struts module '" + name + "'.");
// create module context
context = new StrutsModuleContext(servlet, moduleConfig, name);
log.info("Associating module '" + name + "' with '" + context.getPath("*") + "'");
// install i18n support
I18nSupport i18n = new StrutsI18nSupport(context);
context.setAttribute(I18nSupport.I18N_SUPPORT_KEY, i18n);
// install access support with base and struts accessors
AccessSupport access = new AccessSupport();
access.put("base", new BaseAccessor(context));
access.put("struts", new StrutsAccessor(context));
context.setAttribute(AccessSupport.ACCESS_SUPPORT_KEY, access);
// parse base configuaration files
if (config != null) {
try {
parseConfigs(context, config);
} catch (ConfigException e) {
throw new ServletException("Could not parse configuration file(s)", e);
}
}
// get global struts module container
modules = StrutsModuleGroup.getInstance(servlet);
// add context to container
modules.add(context);
}
/* (non-Javadoc)
* @see org.apache.struts.action.PlugIn#destroy()
*/
public void destroy() {
context.removeAttribute(I18nSupport.I18N_SUPPORT_KEY);
context.removeAttribute(AccessSupport.ACCESS_SUPPORT_KEY);
modules.remove(context);
}
/**
* @return commma separated list of configuration files
*/
public String getConfig() {
return config;
}
/**
* Set comma-separated list of configuration files
*
* @param value
*/
public void setConfig(String value) {
config = value;
}
/**
* @return the module name property
*/
public String getModule() {
return module;
}
/**
* set the module name property
*/
public void setModule(String value) {
this.module = value;
}
}