| 1 | /** |
| 2 | * BSD-style license; for more info see http://xradar.sourceforge.net/license.html |
| 3 | */ |
| 4 | package org.sourceforge.xradar; |
| 5 | |
| 6 | import java.net.URL; |
| 7 | import java.util.logging.Level; |
| 8 | import java.util.logging.Logger; |
| 9 | |
| 10 | import org.apache.xml.resolver.tools.CatalogResolver; |
| 11 | import org.sourceforge.xradar.logging.LogUtils; |
| 12 | import org.sourceforge.xradar.resolver.XRadarCatalogResolver; |
| 13 | import org.sourceforge.xradar.resolver.XRadarStreamHandlerFactory; |
| 14 | import org.sourceforge.xradar.statics.Param; |
| 15 | import org.sourceforge.xradar.statics.Preambule; |
| 16 | import org.sourceforge.xradar.util.FileUtils; |
| 17 | import org.sourceforge.xradar.util.StreamUtils; |
| 18 | import org.sourceforge.xradar.util.URLUtils; |
| 19 | |
| 20 | /** |
| 21 | * |
| 22 | * This class regroups all the methods and property common to statics and dynamics |
| 23 | * process. As it has not yet any abtract methods, it may be refactor to a simple |
| 24 | * Utility classe at the end... |
| 25 | * |
| 26 | * @author rpelisse, belaran@gmail.com |
| 27 | * |
| 28 | */ |
| 29 | public class AbstractProcess |
| 30 | { |
| 31 | private static final Logger logger = Logger.getLogger(AbstractProcess.class.getSimpleName()); |
| 32 | protected XSLTMerger merger = new XSLTMerger(); |
| 33 | |
| 34 | public static final String XRADAR_CONFIG_FILE = "radar-config-file"; |
| 35 | |
| 36 | //public static final String SYSTEM_FILE_SEPARATOR = File.separator; |
| 37 | // According to XRadarUtil.normalizeFileName that replace \ by / |
| 38 | public static final String SYSTEM_FILE_SEPARATOR = "/"; |
| 39 | |
| 40 | |
| 41 | protected static final String RELEASE_DATE = "date"; |
| 42 | protected static final String RELEASE_SYSTEM = "system"; |
| 43 | protected static final String RELEASE_VERSION = "version"; |
| 44 | |
| 45 | protected static final String SYSTEM_QUALITY_REPORT = "system_quality_report.xml"; |
| 46 | protected static final String NORMALIZED_REPORT_NAME = "radar_report_normalized.xml"; |
| 47 | protected static final String MASTER_REPORT_NAME = "masterreport_final.xml"; |
| 48 | protected static final String LOG_STATUS = "log"; |
| 49 | |
| 50 | protected static final String DOCS_HOME = "docs-home"; |
| 51 | |
| 52 | private String docsHome; |
| 53 | private String config; |
| 54 | private boolean debug = true; |
| 55 | |
| 56 | public final static String FILE_PROTOCOL = "file:/"; |
| 57 | public final static String XRADAR_PROTOCOL = "xradar:/"; |
| 58 | |
| 59 | protected AbstractProcess() { |
| 60 | // This class is not really abstract as none of its children have |
| 61 | // share common signature , anyway, it should not be instantiated, |
| 62 | //therefore, the constructor is protected.</p> |
| 63 | } |
| 64 | |
| 65 | public void initializedXmlCatalog(boolean useCatalog) { |
| 66 | if ( useCatalog ) { |
| 67 | logger.log(Level.WARNING,"Offline mode, using inner XML Catalog from path"); |
| 68 | CatalogResolver catalog = new XRadarCatalogResolver(); |
| 69 | XSLTMerger.setEntityResolver(catalog); |
| 70 | XSLTMerger.setUriResolver(catalog); |
| 71 | } |
| 72 | } |
| 73 | |
| 74 | |
| 75 | /** |
| 76 | * @return the config |
| 77 | */ |
| 78 | public String getConfig() { |
| 79 | return config; |
| 80 | } |
| 81 | |
| 82 | /** |
| 83 | * @param config the config to set |
| 84 | */ |
| 85 | public void setConfig(String config) { |
| 86 | this.config = config; |
| 87 | } |
| 88 | |
| 89 | /** |
| 90 | * @return the debug |
| 91 | */ |
| 92 | public boolean isDebug() { |
| 93 | return debug; |
| 94 | } |
| 95 | |
| 96 | /** |
| 97 | * @param debug the debug to set |
| 98 | */ |
| 99 | public void setDebug(boolean debug) { |
| 100 | this.debug = debug; |
| 101 | } |
| 102 | |
| 103 | /** |
| 104 | * @return the docsHome |
| 105 | */ |
| 106 | public String getDocsHome() { |
| 107 | return docsHome; |
| 108 | } |
| 109 | |
| 110 | /** |
| 111 | * @param docsHome the docsHome to set |
| 112 | * @throws XRadarException if the file does not exist or does not have the write permissions |
| 113 | */ |
| 114 | public void setDocsHome(String docsHome) throws XRadarException { |
| 115 | // If running on windows, docs-home maybe a perverted URL containing base \ and / |
| 116 | // this.docsHome = XRadarUtils.normalizeFileName(docsHome); |
| 117 | this.docsHome = docsHome; |
| 118 | |
| 119 | //TODO: Add some test ( dir exits ? writable ?) |
| 120 | //File dir = new File (docsHome) |
| 121 | //if (dir.exists() && dir.canWrite()){ |
| 122 | logger.finest("DOCS_HOME is :" + this.docsHome); |
| 123 | //else |
| 124 | //throw new XRadarException ("File " + docsHome + " does not exist or is not writable");*/ |
| 125 | } |
| 126 | |
| 127 | |
| 128 | /* |
| 129 | * Simple wrapper to increase code readability |
| 130 | */ |
| 131 | protected String getDefault(String id) |
| 132 | { |
| 133 | return DefaultReportValues.getString(id); |
| 134 | } |
| 135 | |
| 136 | /* |
| 137 | * Simple wrapper to increase code readability |
| 138 | */ |
| 139 | protected int getDefaultIntValue(String id) |
| 140 | { |
| 141 | return Integer.parseInt(DefaultReportValues.getString(id)); |
| 142 | } |
| 143 | |
| 144 | /* |
| 145 | * Set the $log param according to the current log level |
| 146 | */ |
| 147 | protected void addLogLevelDefinitionToPreambule(Preambule preambule) |
| 148 | { |
| 149 | preambule.getParams().put( |
| 150 | LOG_STATUS, |
| 151 | new Param(LOG_STATUS,Boolean.valueOf(LogUtils.isLogEnabled(logger)).toString())); |
| 152 | } |
| 153 | |
| 154 | protected void copyImagesToWebsite(String imagesTargetPrefix) throws XRadarException |
| 155 | { |
| 156 | logger.fine("Copying images to website"); |
| 157 | int nbStaticResources = Integer.valueOf(DefaultReportValues.getString("radar.internal.static.nbResources")); |
| 158 | logger.finest("Copying" + nbStaticResources + " statics resources."); |
| 159 | for ( int resource = 1; resource <= nbStaticResources ; resource++ ) |
| 160 | { |
| 161 | logger.finest("Loading resources url and target"); |
| 162 | String resourcePath = DefaultReportValues.getString("radar.internal.static.resources." + resource); |
| 163 | String targetPath = DefaultReportValues.getString("radar.internal.static.resources.target." + resource); |
| 164 | logger.finest("Static resource ID:[" + resource + "]. Copying it from " + resourcePath + " to " + imagesTargetPrefix + SYSTEM_FILE_SEPARATOR + targetPath ); |
| 165 | String filename = imagesTargetPrefix + SYSTEM_FILE_SEPARATOR + targetPath; |
| 166 | FileUtils.createParentDirectories(filename); |
| 167 | StreamUtils.copy(URLUtils.openStream(resourcePath),StreamUtils.createOutputStream(filename)); |
| 168 | } |
| 169 | logger.fine("Images resources copying done."); |
| 170 | } |
| 171 | |
| 172 | |
| 173 | /** |
| 174 | * <p>Very important method ! This method register the resolver, which allow the JVM to |
| 175 | * actual resolve the 'xradar' protocol and find the file requested.</p> |
| 176 | */ |
| 177 | public static void registerResolvers() { |
| 178 | /* |
| 179 | * Here there is an issue. if running this several time in the same jvm |
| 180 | * the StreamHandler is already defined and the following exception is |
| 181 | * thrown. i did not find a better way to avoid this than doing this catch. |
| 182 | */ |
| 183 | // FIXME: This exception catching is ugly !!!!!! |
| 184 | // TODO: Maybe simply use the setURIResolver on the transformer ? |
| 185 | try |
| 186 | { |
| 187 | // Adding to JVM our own URL resolver to resolve the 'xradar' protocol |
| 188 | URL.setURLStreamHandlerFactory(new XRadarStreamHandlerFactory()); |
| 189 | } |
| 190 | catch ( java.lang.Error e) |
| 191 | { |
| 192 | //FIXME: this so ugly, it actually requires an other fixme ! |
| 193 | // Only if we run into this error we should not take care of the exception |
| 194 | if ( ! "class java.lang.Error".equals(e.getClass()) && |
| 195 | ! "factory already defined".equals(e.getMessage() ) ) |
| 196 | { |
| 197 | throw e; |
| 198 | } |
| 199 | } |
| 200 | } |
| 201 | } |