package refactoring.struts; import java.util.ArrayList; import java.util.Enumeration; import java.util.HashMap; import java.util.List; import java.util.Map; import org.apache.struts.config.ActionConfig; import org.apache.struts.config.ExceptionConfig; import org.apache.struts.config.FormBeanConfig; import org.apache.struts.config.ForwardConfig; import org.apache.struts.config.ModuleConfig; import refactoring.struts.ActionServletPlaceholder.ConfigPlaceholder; public class RefactorStart { public static void main(String[] args) throws Exception { ConfigPlaceholder config = ActionServletPlaceholder.getConfigFilesWrapper(); Enumeration paramNames = config.getInitParameterNames(); List moduleConfigs = new ArrayList(); Map missingLogs = new HashMap(); String paramName = ""; String paramValue = config.getInitParameter("config"); String prefix = ""; ModuleConfig mc = ActionServletPlaceholder.initModuleConfig(prefix, paramValue); mc.freeze(); moduleConfigs.add(mc); System.out.println("Key: " + paramName + "\nValue: " + paramValue + "\n"); while (paramNames.hasMoreElements()) { paramName = paramNames.nextElement(); paramValue = config.getInitParameter(paramName); /* * FIXME the struts ActionServlet ignores anything without start * config/ However, that excludes our default config file. The * ActionServlet assumes a nameless module, "", with respective file * being in "/WEB-INF/struts-config.xml" which is not the case of * fenix */ /* * NOTE we have the fenix project in the classpath because we need * classes over there, namely for mappings and for formbeans fields' * types */ if (!paramName.startsWith("config/")) { continue; } System.out.println("Key: " + paramName + "\nValue: " + paramValue + "\n"); prefix = paramName.substring(6); mc = ActionServletPlaceholder.initModuleConfig(prefix, paramValue); mc.freeze(); moduleConfigs.add(mc); } for (ModuleConfig moduleConfig : moduleConfigs) { System.out.println("#-----------------#\nNew module: " + moduleConfig.getPrefix()); for (FormBeanConfig formBeanConfig : moduleConfig.findFormBeanConfigs()) { /* * System.out.println("----->Form bean of: " + * moduleConfig.getPrefix() + "\tdynamic? " + * formBeanConfig.getDynamic() + "\n"); */ if(formBeanConfig.getDynamic()) { FormBeanCreator.createFormBean(formBeanConfig, moduleConfig.getPrefix()); } } LogMissingDispatchAction missingLog = new LogMissingDispatchAction(); for (ActionConfig actionConfig : moduleConfig.findActionConfigs()) { ActionMappingCreator.createActionMapping(moduleConfig.getPrefix(), actionConfig, missingLog); List exceptionHandlersAnnotations = new ArrayList(); for (ExceptionConfig exceptionConfig : actionConfig.findExceptionConfigs()) { exceptionHandlersAnnotations.add(ExceptionsCreator.createException(exceptionConfig)); } if (!exceptionHandlersAnnotations.isEmpty()) { ExceptionsCreator.createExceptionsAnnotation(exceptionHandlersAnnotations); } List forwardAnnotations = new ArrayList(); for (ForwardConfig forwardConfig : actionConfig.findForwardConfigs()) { forwardAnnotations.add(ForwardCreator.createForward(forwardConfig)); } if (!forwardAnnotations.isEmpty()) { ForwardCreator.createForwards(forwardAnnotations); } } missingLogs.put(moduleConfig, missingLog); } } /* * Form bean classes reconstruction takes into account: for each * ActionMapping, if we have the name property defined, then that's the name * of the bean. thus we use moduleConfig.findFormBeanConfig(thatname) and * get the FormBeanConfig. then we open the file that corresponds to the * class of the dispatch action of this ActionMapping, by using the type * property of this ActionMapping. In that file, we have to programatically * write the source code corresponding to the FormBeanConfig acquired. * * Alternatively, we can create the FormBean independently in a .java, and * import it in the DispatchAction acquired with the type of the * ActionMapping, and then add to the @Mapping that will be created, the * formBeanClass=thatclass.class */ private static void printMissingLog(Map missingLogs) { System.out.println("##############Missing log###############"); for (Map.Entry moduleLog : missingLogs.entrySet()) { StringBuilder DAsMissing = new StringBuilder(); for (Map.Entry> missesLog : moduleLog.getValue().getLoggedMappings().entrySet()) { DAsMissing.append(missesLog.getKey() + " | "); } System.out.println("-->Log of module: " + moduleLog.getKey().getPrefix() + " ---> missing files: " + DAsMissing.toString()); } } }