package refactoring.struts; import java.util.ArrayList; import java.util.HashMap; import java.util.List; import java.util.Map; import java.util.Set; import java.util.TreeSet; import org.apache.struts.config.ActionConfig; import refactoring.struts.beans.MappingAnnotationBean; public class StrutsStats { public static void produceStatistics(List configs) { totalModules(configs); inexistentMappings(configs); singleForwardMappings(configs); namelessMappings(configs); } private static void totalModules(List configs) { int totalModules = configs.size(); int totalBeans = 0; List mappingBeans = new ArrayList(); for (ModuleAnnotations ma : configs) { mappingBeans.addAll(ma.getMappingAnnotations()); totalBeans += ma.getCreatedFormBeans().size(); } int builtAnnotations = mappingBeans.size(); System.out.println("Total #modules: " + totalModules + " built annotations: " + builtAnnotations + " #beans created: " + totalBeans); List mappingsFiltered = new ArrayList(); for (ModuleAnnotations ma : configs) { Map> alreadySeenDAs = new HashMap>(); for (MappingAnnotationBean mappingBean : ma.getMappingAnnotations()) { String nameDA = mappingBean.getDispatchActionClassName(); if (!alreadySeenDAs.containsKey(nameDA)) { List newList = new ArrayList(); newList.add(mappingBean); alreadySeenDAs.put(nameDA, newList); continue; } alreadySeenDAs.get(nameDA).add(mappingBean); } for (Map.Entry> entry : alreadySeenDAs.entrySet()) { List listMappings = entry.getValue(); if (listMappings.size() == 1) { mappingsFiltered.add(listMappings.get(0)); } } } Set uniqueDAs = new TreeSet(); for (MappingAnnotationBean mappingFiltered : mappingsFiltered) { uniqueDAs.add(mappingFiltered.getDispatchActionClassName()); } int numberDAs = uniqueDAs.size(); builtAnnotations = mappingsFiltered.size(); System.out.println("Removing multiple mappings to a DA inside a module, we get total #DAs: " + numberDAs + " and built annotations " + builtAnnotations); // modifying configs in place! for (ModuleAnnotations ma : configs) { List mappingRemoved = new ArrayList(); for (MappingAnnotationBean mappingBean : ma.getMappingAnnotations()) { if (!mappingsFiltered.contains(mappingBean)) { mappingRemoved.add(mappingBean); } } ma.getMappingAnnotations().removeAll(mappingRemoved); } Map> conflictsBetweenModules = new HashMap>(); for (ModuleAnnotations ma : configs) { for (MappingAnnotationBean mappingBean : ma.getMappingAnnotations()) { if(!conflictsBetweenModules.containsKey(mappingBean.getDispatchActionClassName())) { List newList = new ArrayList(); newList.add(mappingBean); conflictsBetweenModules.put(mappingBean.getDispatchActionClassName(), newList); continue; } conflictsBetweenModules.get(mappingBean.getDispatchActionClassName()).add(mappingBean); } } Map conflictsNumbers = new HashMap(); for (Map.Entry> entry : conflictsBetweenModules.entrySet()) { List listConflictingMappings = entry.getValue(); if (listConflictingMappings.size() != 1) { conflictsNumbers.put(entry.getKey(), listConflictingMappings.size()); } } int conflictsAffectedDAs = conflictsNumbers.keySet().size(); int conflictsAffectedMappings = 0; for (Integer i : conflictsNumbers.values()) { conflictsAffectedMappings += i; } System.out.println("Conflicts inter-module in same DA. Affected #DAs " + conflictsAffectedDAs + " affected #mappings: " + conflictsAffectedMappings + " one has 28, other has 8 and most are 2,3 and 4. This does not include mappings already existent in the DAs"); // Modifying in place int totalLeft = 0; int totalConflicts = 0; for (ModuleAnnotations ma : configs) { List mappingRemoved = new ArrayList(); for (MappingAnnotationBean mappingBean : ma.getMappingAnnotations()) { if (conflictsBetweenModules.get(mappingBean.getDispatchActionClassName()).size() != 1) { mappingRemoved.add(mappingBean); } } mappingRemoved.addAll(ma.getMissingDAs().getAlreadyContainsMapping()); ma.getMappingAnnotations().removeAll(mappingRemoved); totalLeft += ma.getMappingAnnotations().size(); ma.setInterModuleConflictMappings(mappingRemoved); totalConflicts += ma.getInterModuleConflictMappings().size(); } System.out.println("Confirming that we have left a total of #mappings " + totalLeft + " inter module #conflicts " + totalConflicts); } private static void inexistentMappings(List configs) { Set missingDAs = new TreeSet(); List missingMappings = new ArrayList(); for (ModuleAnnotations ma : configs) { LogMissingDispatchAction missing = ma.getMissingDAs(); missingDAs.addAll(missing.getLoggedMappings().keySet()); for (List curList : missing.getLoggedMappings().values()) { missingMappings.addAll(curList); } } System.out.println("Missing #DAs: " + missingDAs.size() + " from #mappings in struts: " + missingMappings.size()); for (String missingDA : missingDAs) { System.out.println("\t" + missingDA); } } private static void singleForwardMappings(List configs) { Set singleForwardDAs = new TreeSet(); List missingMappings = new ArrayList(); for (ModuleAnnotations ma : configs) { LogMissingDispatchAction missing = ma.getMissingDAs(); singleForwardDAs.addAll(missing.getSingleForwardMappings().keySet()); for (List curList : missing.getSingleForwardMappings().values()) { missingMappings.addAll(curList); } } System.out.println("Skipping #DAs due to singleForward: " + singleForwardDAs.size() + " from #mappings in struts " + missingMappings.size()); for (String singleForwardDA : singleForwardDAs) { System.out.println("\t" + singleForwardDA); } } private static void namelessMappings(List configs) { List namelessMappings = new ArrayList(); for (ModuleAnnotations ma : configs) { LogMissingDispatchAction missing = ma.getMissingDAs(); namelessMappings.addAll(missing.getNamelessMappings()); } System.out.println("Ignoring #Mappings due to nameless Type " + namelessMappings.size()); for (ActionConfig mapping : namelessMappings) { System.out.println("\t" + "Module: " + mapping.getModuleConfig().getPrefix() + " path: " + mapping.getPath()); } } }