package refactoring.struts; import java.io.FileInputStream; import java.io.IOException; import java.io.InputStream; import java.net.MalformedURLException; import java.net.URL; import java.util.Enumeration; import java.util.HashMap; import java.util.Iterator; import java.util.Map; import java.util.Properties; import org.apache.commons.digester.Digester; import org.apache.struts.config.ConfigRuleSet; import org.apache.struts.config.FormBeanConfig; import org.apache.struts.config.ModuleConfig; import org.apache.struts.config.ModuleConfigFactory; import org.xml.sax.SAXException; public class ActionServletPlaceholder { public static class ConfigPlaceholder { public static final String PREFIX_FENIX_PROJECT = "/home/nmld/runtime-EclipseApplication/fenix-mod-struts/"; public static final String PROPERTIES_FILE_OF_CONFIG_FILES = "/home/nmld/runtime-EclipseApplication/fenix-mod-struts/web/WEB-INF/classes/struts-auto-mapping.properties"; public static final Map parameterMap = new HashMap(); static { parameterMap.put("config", PREFIX_FENIX_PROJECT + "web/WEB-INF/conf/struts-default.xml"); parameterMap.put("application", "resources.ApplicationResources"); try { Properties properties = new Properties(); FileInputStream in = new FileInputStream(PROPERTIES_FILE_OF_CONFIG_FILES); properties.load(in); in.close(); String strutFiles = properties.getProperty("struts.files"); for (String filename : strutFiles.split(",")) { String mapping = filename.replace("struts-", "").replace(".xml", ""); parameterMap.put("config/" + mapping, PREFIX_FENIX_PROJECT + "web/WEB-INF/conf/" + filename); } } catch (IOException e) { e.printStackTrace(); } parameterMap.put("config/person/functionalities", PREFIX_FENIX_PROJECT + "web/WEB-INF/conf/struts-functionalities.xml"); parameterMap.put("config/manager/functionalities", PREFIX_FENIX_PROJECT + "web/WEB-INF/conf/struts-functionalities.xml"); parameterMap.put("config/projectsManagement", PREFIX_FENIX_PROJECT + "web/WEB-INF/conf/struts-institucionalProjectsManagement.xml"); parameterMap.put("config/itProjectsManagement", PREFIX_FENIX_PROJECT + "web/WEB-INF/conf/struts-institucionalProjectsManagement.xml"); parameterMap.put("debug", "3"); parameterMap.put("detail", "3"); parameterMap.put("validating", "true"); } public String getInitParameter(final String name) { return parameterMap.get(name); } public Enumeration getInitParameterNames() { return new Enumeration() { private final Iterator iterator = parameterMap.keySet().iterator(); public boolean hasMoreElements() { return iterator.hasNext(); } public String nextElement() { return iterator.next(); } }; } } private static ConfigPlaceholder configFilesWrapper = null; private static Digester configDigester = null; private static final String registrations[] = { "-//Apache Software Foundation//DTD Struts Configuration 1.0//EN", "/org/apache/struts/resources/struts-config_1_0.dtd", "-//Apache Software Foundation//DTD Struts Configuration 1.1//EN", "/org/apache/struts/resources/struts-config_1_1.dtd", "-//Apache Software Foundation//DTD Struts Configuration 1.2//EN", "/org/apache/struts/resources/struts-config_1_2.dtd", "-//Sun Microsystems, Inc.//DTD Web Application 2.2//EN", "/org/apache/struts/resources/web-app_2_2.dtd", "-//Sun Microsystems, Inc.//DTD Web Application 2.3//EN", "/org/apache/struts/resources/web-app_2_3.dtd" }; private static ConfigPlaceholder init() { return ActionServletPlaceholder.configFilesWrapper = new ConfigPlaceholder(); } public static ConfigPlaceholder getConfigFilesWrapper() { return (configFilesWrapper == null ? init() : ActionServletPlaceholder.configFilesWrapper); } public static ModuleConfig initModuleConfig(String prefix, String paths) { // Parse the configuration for this module ModuleConfigFactory factoryObject = ModuleConfigFactory.createFactory(); ModuleConfig config = factoryObject.createModuleConfig(prefix); // Configure the Digester instance we will use Digester digester = initConfigDigester(); // Process each specified resource path while (paths.length() > 0) { digester.push(config); String path = null; int comma = paths.indexOf(','); if (comma >= 0) { path = paths.substring(0, comma).trim(); paths = paths.substring(comma + 1); } else { path = paths.trim(); paths = ""; } if (path.length() < 1) { break; } parseModuleConfigFile(digester, path); } // Force creation and registration of DynaActionFormClass instances // for all dynamic form beans we wil be using FormBeanConfig fbs[] = config.findFormBeanConfigs(); for (int i = 0; i < fbs.length; i++) { if (fbs[i].getDynamic()) { fbs[i].getDynaActionFormClass(); } } return config; } public static Digester initConfigDigester() { // Do we have an existing instance? if (configDigester != null) { return (configDigester); } // Create a new Digester instance with standard capabilities configDigester = new Digester(); configDigester.setNamespaceAware(true); configDigester.setValidating(true); configDigester.setUseContextClassLoader(true); configDigester.addRuleSet(new ConfigRuleSet()); for (int i = 0; i < registrations.length; i += 2) { URL url = configDigester.getClass().getResource(registrations[i + 1]); if (url != null) { configDigester.register(registrations[i], url.toString()); } } // Return the completely configured Digester instance return configDigester; } public static void parseModuleConfigFile(Digester digester, String path) { InputStream input = null; try { digester.parse(path); } catch (MalformedURLException e) { e.printStackTrace(); throw new RuntimeException("Could not load the config file: " + path + "."); } catch (IOException e) { e.printStackTrace(); throw new RuntimeException("Could not load the config file: " + path + "."); } catch (SAXException e) { e.printStackTrace(); throw new RuntimeException("Could not load the config file: " + path + "."); } finally { if (input != null) { try { input.close(); } catch (IOException e) { e.printStackTrace(); throw new RuntimeException("Could not close the config file"); } } } } }