View Javadoc
1 /*** 2 * ReportManager.java 3 * 4 * Project: Dependency Tool 5 * 6 * WHEN WHO WHAT 7 * 06.06.2003 pko initial public release 8 * 29.10.2002 pko creation 9 * 10 * Copyright 2003 ELCA Informatique SA 11 * Av. de la Harpe 22-24, 1000 Lausanne 13, Switzerland 12 * www.elca.ch 13 * 14 * This library is free software; you can redistribute it and/or 15 * modify it under the terms of the GNU Lesser General Public License 16 * as published by the Free Software Foundation; either version 2.1 of 17 * the License, or (at your option) any later version. 18 * 19 * This library is distributed in the hope that it will be useful, but 20 * WITHOUT ANY WARRANTY; without even the implied warranty of 21 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU 22 * Lesser General Public License for more details. 23 * 24 * You should have received a copy of the GNU Lesser General Public 25 * License along with this library; if not, write to the Free Software 26 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 27 * USA 28 */ 29 30 package ch.elca.dependency.report; 31 32 import java.io.File; 33 import java.io.FileInputStream; 34 import java.io.InputStream; 35 import java.net.URL; 36 37 import org.apache.commons.digester.xmlrules.DigesterLoader; 38 import org.apache.log4j.Logger; 39 40 import ch.elca.dependency.core.DependencyContext; 41 import ch.elca.dependency.core.DependencyModel; 42 import ch.elca.dependency.util.StringManager; 43 44 /*** 45 * <code>ReportManager</code> used as an intermediary to create and 46 * manage Reports on the analyzed Project. 47 * 48 * @see ch.elca.dependency.report.Report 49 * @see ch.elca.dependency.core.DependencyModel 50 * 51 * @author Pawel Kowalski 52 * @version 1.0-beta 53 */ 54 public class ReportManager { 55 56 /*** 57 * Log4j Logger. 58 */ 59 private static final Logger LOG = Logger.getLogger(ReportManager.class); 60 61 /*** 62 * A StringManager to access Strings 63 */ 64 private static final StringManager STR = StringManager.getManager(ReportManager.class); 65 66 /*** 67 * Path to the Digester rules and default config 68 */ 69 private static final String RULES_PATH = "ch/elca/dependency/report/"; 70 71 /*** 72 * Digester rules filename 73 */ 74 private static final String REPORT_RULES = RULES_PATH + "report-rules.xml"; 75 76 /*** 77 * Digester default report config filename 78 */ 79 private static final String DEFAULT_CONFIG = RULES_PATH + "report-conf.xml"; 80 81 //****************************************************************************************/ 82 // create a Report 83 //****************************************************************************************/ 84 85 /*** 86 * Create a Report on the specified <code>RawModel</code> and 87 * <code>DependencyModel</code>. 88 * 89 * @param filteredModel a <code>DependencyModel</code> value 90 * @param rawModel a <code>RawModel</code> value 91 * 92 * @return a <code>Report</code> value 93 * 94 * @exception ReportException if an error occurs 95 */ 96 public synchronized static Report createReport(DependencyModel dependencyModel) 97 throws ReportException { 98 return createReport(null, dependencyModel); 99 } 100 101 /*** 102 * Create a Report on the specified <code>RawModel</code> and 103 * <code>DependencyModel</code>. 104 * 105 * @param reportConfig Filename of the Report config File 106 * @param filteredModel a <code>DependencyModel</code> value 107 * @param rawModel a <code>RawModel</code> value 108 * 109 * @return a <code>Report</code> value 110 * 111 * @exception ReportException if an error occurs 112 */ 113 public synchronized static Report createReport(String reportConfig, 114 DependencyModel dependencyModel) 115 throws ReportException { 116 // check arguments 117 // 118 if (dependencyModel== null) { 119 ReportException repEx = new ReportException(ReportException.INVALID_ARGUMENTS); 120 throw repEx; 121 } 122 123 ClassLoader classLoader = Thread.currentThread().getContextClassLoader(); 124 URL rulesUrl = classLoader.getResource(REPORT_RULES); 125 InputStream configStream = null; 126 127 // try to get a config stream (either user specified or custom) 128 // 129 try { 130 configStream = (reportConfig == null ? 131 classLoader.getResourceAsStream(DEFAULT_CONFIG) : 132 new FileInputStream(reportConfig)); 133 } catch (Exception e) { 134 LOG.error(STR.getString("report.config.missing") + ": " + 135 STR.getString("report.using.default.config")); 136 configStream = classLoader.getResourceAsStream(DEFAULT_CONFIG); 137 } 138 139 // could not find or access digester rules 140 // 141 if (rulesUrl == null) { 142 ReportException repEx = new ReportException(ReportException.RULES_NOT_AVAILABLE); 143 throw repEx; 144 } 145 146 // try to load a Report as specified in the config file 147 // 148 try { 149 Report report = (Report)DigesterLoader.load(rulesUrl, classLoader, configStream); 150 report.initReport(dependencyModel); 151 return report; 152 } catch (Exception e) { 153 154 LOG.error(STR.getString("report.error.in.config") + ": " + 155 STR.getString("report.using.default.config")); 156 157 // could not setup report as requested, using default report config 158 // 159 try { 160 configStream = classLoader.getResourceAsStream(DEFAULT_CONFIG); 161 Report report = (Report)DigesterLoader.load(rulesUrl, classLoader, 162 configStream); 163 report.initReport(dependencyModel); 164 return report; 165 } catch (Exception ex) { 166 ReportException repEx 167 = new ReportException(ReportException.CREATING_DEFAULT_REPORT_FAILED, ex); 168 throw repEx; 169 } 170 } 171 } 172 173 //****************************************************************************************/ 174 // perform reporting 175 //****************************************************************************************/ 176 177 /*** 178 * Perform reporting accordingly to the settings provided within 179 * the Hashtable argument. This includes: creating an Analyzer, 180 * applying filters, choosing UserLayer, creating a Report and 181 * outputting the created Report. 182 * 183 * @param properties a <code>Hashtable</code> value 184 * 185 * @exception ReportException if an error occurs 186 */ 187 public synchronized static void performReporting(DependencyContext dependencyContext) 188 throws ReportException { 189 190 DependencyModel dependencyModel = new DependencyModel(); 191 File reportConfig = null; 192 Report report = null; 193 194 if (dependencyContext.suffices()) { 195 try { 196 dependencyModel.create(dependencyContext); 197 reportConfig = (File)dependencyContext.get(DependencyContext.REPORT_CONFIG_KEY); 198 report = ReportManager.createReport(reportConfig.getAbsolutePath(), dependencyModel); 199 report.outputReport(); 200 } catch (Exception e) { 201 LOG.error("Could not create a Dependency Model, Report failed"); 202 e.printStackTrace(); 203 } 204 } else { 205 LOG.error("Dependency Context is not sufficient for a dependency analysis"); 206 } 207 } 208 209 //****************************************************************************************/ 210 // helper methods 211 //****************************************************************************************/ 212 213 /*** 214 * Returns all available ReportInfo classes that can be used to build a Report. 215 * 216 * @return a <code>ReportInfo[]</code> value 217 * 218 * @tbd improve: read all available ReportInfos from a auto-generated properties file. 219 */ 220 public static Class[] getAvailableReportInfos() { 221 Class[] reportInfoClasses = {ch.elca.dependency.report.StatReportInfo.class, 222 ch.elca.dependency.report.PackagesReportInfo.class, 223 ch.elca.dependency.report.DependenciesReportInfo.class, 224 ch.elca.dependency.report.LayeringViolationReportInfo.class}; 225 return reportInfoClasses; 226 } 227 228 /***import ch.elca.dependency.core.Filter; 229 * Returns all available Formatter classes that can be used to format a Report. 230 * 231 * @return a <code>Class[]</code> value 232 * 233 * @tbd improve: read all available Formatters from a auto-generated properties file. 234 */ 235 public static Class[] getAvailableFormatters() { 236 Class[] fomatterClasses = {ch.elca.dependency.report.TextFormatter.class}; 237 return fomatterClasses; 238 } 239 }

This page was automatically generated by Maven