1 23 package com.sun.enterprise.diagnostics.report.html; 24 25 import com.sun.enterprise.diagnostics.*; 26 import com.sun.enterprise.diagnostics.collect.DataType; 27 import com.sun.enterprise.diagnostics.collect.FileData; 28 29 30 import java.util.*; 31 import java.io.*; 32 import java.text.MessageFormat ; 33 import java.util.logging.Level ; 34 import java.util.logging.Logger ; 35 import com.sun.logging.LogDomains; 36 37 41 public class HTMLReportTemplate { 42 private static final String REPORT_NAME = File.separator + "ReportSummary.html"; 43 private Table toc; 44 private Document doc; 45 protected Element bodyElement; 46 private Element descElement; 47 private int tocNo; 48 protected ReportConfig config; 49 protected ReportTarget target; 50 private String targetName; 51 private String reportDir; 52 protected DataTraverser dataObjTraverser ; 53 54 private static String report_description = 55 "This Snapshot was generated for {0} at " + 56 "{1,time} on {1,date}"; 57 58 private static final String description = "Description"; 59 private static final String customer_information = "Customer Information"; 60 private static final String bug_ids = "Bug IDs"; 61 private static String component_details = "{0} details "; 62 private static final String checksum_details = "Checksum Information"; 63 protected static final String monitoring_information = "Monitoring Information"; 64 private static final String system_information = "System Information"; 65 private static final String installation_log = "Installation Log"; 66 private static final String domain_xml_validation_details = "Domain Validation Details"; 67 private static Logger logger = 68 LogDomains.getLogger(LogDomains.ADMIN_LOGGER); 69 70 75 public HTMLReportTemplate(ReportConfig config, Data dataObj) { 76 this.config = config; 77 this.target = config.getTarget(); 78 this.targetName = target.getName(); 79 this.reportDir = target.getIntermediateReportDir(); 80 this.dataObjTraverser = new DataTraverser(dataObj); 81 initialize(); 82 } 83 84 public void write() { 85 try { 86 String reportFile = reportDir + REPORT_NAME; 87 File file = new File(reportFile); 89 BufferedWriter writer = new BufferedWriter( 91 new FileWriter(file)); 92 93 writer.write(doc.toString()); 94 writer.flush(); 95 } catch (Exception e) { 96 e.printStackTrace(); 97 } 99 } 100 101 protected void addMiscellaneousInfo(){ 102 } 104 105 private void initialize() { 106 doc = new HTMLDocument(); 107 bodyElement = doc.getBody(); 108 109 addDocTitle(); 110 addDocHeading(); 111 addTOCSection(); 112 addDescSection(); 113 addCustomerInformation(); 114 addBugIds(); 115 addChecksumSection(); 116 addComponentDetails(); 117 addMiscellaneousInfo(); 118 } 119 120 private void addDocHeading() { 121 Element h1Element = new HTMLElement(HTMLReportConstants.H1); 122 h1Element.addAttribute(HTMLReportConstants.ALIGN, 123 HTMLReportConstants.CENTER); 124 h1Element.addText(HTMLReportConstants.report_heading); 125 bodyElement.add(h1Element); 126 } 127 128 private void addDocTitle() { 129 Element titleElement = new HTMLElement(HTMLReportConstants.TITLE); 130 titleElement.addText(HTMLReportConstants.report_heading); 131 doc.getHead().add(titleElement); 132 } 133 134 private void addTOCSection() { 135 toc = new Table(); 136 bodyElement.add(toc); 137 } 138 139 private void addDescSection() { 140 addTitle(description, true, true, 0,0); 141 descElement = new HTMLElement("desc"); 142 String description = getText(report_description, 143 new Object [] {targetName, new Date()}); 144 descElement.addText(description); 145 addElement(descElement); 146 } 147 148 private void addCustomerInformation() { 149 Iterator<Data> iterator = dataObjTraverser.getData(DataType.CUSTOMER_INFO); 150 151 if (iterator.hasNext()) { 152 Data dataObj = iterator.next(); 153 addTitle(customer_information, true, true, 0,0); 154 HTMLElement element = new HTMLElement("customer_information"); 155 if(dataObj instanceof FileData) { 156 copyFromFile(element, dataObj.getSource()); 157 } 158 element.addText(dataObj.getValues()); 159 addElement(element); 160 } 161 } 162 163 private void addChecksumSection() { 164 Iterator<Data> iterator = dataObjTraverser.getData(DataType.CHECKSUM); 165 if(iterator.hasNext()) { 166 ArrayList elements= new ArrayList(); 167 Element link = new Link(checksum_details, 168 "#"+getNextTocNo()); 169 elements.add(link); 170 toc.addRow(elements,null); 171 Element bodyLink = new Link(null,null,getTocNo()); 172 bodyElement.add(bodyLink); 173 } 174 while(iterator.hasNext()) { 175 Data checksumData = iterator.next(); 176 Iterator<Iterator<String >> details = checksumData.getTable(); 177 178 Table table = new Table(1,0); 179 Element h2 = new HTMLElement("h2"); 180 h2.addText(checksumData.getSource() + " " + checksum_details); 181 bodyElement.add(h2); 182 table.addRow(details.next(), true, null); 183 184 while(details.hasNext()) { 185 table.addRow(details.next(), false, null); 186 } 187 bodyElement.add(table); 188 } 189 190 } 191 192 private void addBugIds() { 193 if(config.getCLIOptions().getBugIds() != null && 194 config.getCLIOptions().getBugIds().trim().length() > 0) { 195 addTitle(bug_ids, true, true, 0,0); 196 HTMLElement bugIdElement = new HTMLElement(bug_ids); 197 bugIdElement.addText(config.getCLIOptions().getBugIds()); 198 addElement(bugIdElement); 199 } 200 } 201 202 203 private void addComponentDetails() { 204 String componentDetailsTitle = getText(component_details, 205 new Object [] {target.getName()}); 206 addTitle(componentDetailsTitle, true, true, 0,0); 207 Element element = new HTMLElement("component_details"); 208 addInstanceSpecificSection(element); 209 addElement(element); 210 } 211 protected void addInstanceSpecificSection(Element element) { 212 if(element != null) { 213 Iterator<ServiceConfig > configurations = config.getInstanceConfigurations(); 214 215 while(configurations.hasNext()) { 216 ServiceConfig config = configurations.next(); 217 String instanceName = config.getInstanceName(); 218 int indentation = 1; 219 String linkPrefix = "." + File.separator ; 220 String instanceFolder = reportDir; 221 if(!(instanceName.equals(TargetType.DAS.getType()))) { 222 linkPrefix = instanceName + File.separator ; 223 instanceFolder = instanceFolder + File.separator + instanceName; 224 addLink(element, instanceName, instanceName, indentation++); 225 } 226 227 addLink(element, "config", linkPrefix + "config", 228 indentation); 229 230 if(exists(instanceFolder, Constants.GENERATED)) 232 addLink(element, Constants.GENERATED, 233 linkPrefix + Constants.GENERATED, indentation); 234 if(exists(instanceFolder, Constants.APPLICATIONS)) 235 addLink(element, Constants.APPLICATIONS, 236 linkPrefix + Constants.APPLICATIONS, indentation); 237 if(exists(instanceFolder, Defaults.LOGS)) 238 addLink(element, Defaults.LOGS, linkPrefix + Defaults.LOGS, 239 indentation); 240 if(exists(instanceFolder,Defaults.DOMAIN_XML_VERIFICATION_OUTPUT)) 241 addLink(element, domain_xml_validation_details, 242 linkPrefix + Defaults.DOMAIN_XML_VERIFICATION_OUTPUT, 243 indentation); 244 addInstallationLogSection(config.isCaptureInstallLogEnabled(), 245 element, indentation); 246 addLink(config.isCaptureSystemInfoEnabled(), 247 element, DataType.SYSTEM_INFO, system_information, 248 indentation); 249 250 addMonitoringInfo(element, instanceName, indentation); 251 252 } } 254 } 255 256 protected void addMonitoringInfo(Element element, String instanceName, 257 int indentation) { 258 if (!config.getCLIOptions().isLocal()) { 259 addLink(element, monitoring_information, 260 instanceName + 261 File.separator + Defaults.MONITORING_INFO_FILE, 262 indentation); 263 } 264 } 265 private void addInstallationLogSection(boolean enabled, 266 Element element, int indentation) { 267 List<String > files = new ArrayList(2); 268 if(enabled) { 269 String [] logFiles = 270 new File( 271 target.getIntermediateReportDir()).list(new FilenameFilter() { 272 public boolean accept(File folder, String name) { 273 if( (name.contains(Constants.INSTALLATION_LOG_PREFIX)) || 274 (name.contains(Constants.SJSAS_INSTALLATION_LOG_PREFIX))) 275 return true; 276 return false; 277 } 278 }); 279 files.addAll(Arrays.asList(logFiles)); 280 } 281 282 for(String fileName : files) { 283 addLink(element, fileName, fileName, indentation); 284 } 285 } 286 287 288 private void addLink(boolean enabled, Element element, String dataType, 289 String text, int indentation) { 290 if(enabled) { 291 Iterator<Data> iterator = dataObjTraverser.getData(dataType); 292 while(iterator.hasNext()){ 293 Data data = iterator.next(); 294 addLink(element, text, data.getSource(), indentation); 295 } 296 } 297 } 298 protected boolean exists(String parent, String folder) { 299 File file = new File(parent + File.separator + 300 folder); 301 return file.exists(); 302 } 303 304 protected void addTitle(String title, boolean add2TOC, 305 boolean showTitleInBody, 306 int titleIndent, int textIndent){ 307 308 String tocTitle = indentText("", titleIndent); 309 310 if(add2TOC){ 311 ArrayList elements= new ArrayList(); 312 Element p = new HTMLElement(HTMLReportConstants.PARA); 313 p.addText(tocTitle); 314 315 Element link = new Link(title,"#"+getNextTocNo()); 316 p.add(link); 317 elements.add(p); 318 toc.addRow(elements,null); 319 } 320 321 322 Element bodyLink = new Link(null,null,getTocNo()); 323 bodyElement.add(bodyLink); 324 325 if(showTitleInBody){ 326 Element h2 = new HTMLElement(HTMLReportConstants.H2); 327 h2.addText(title); 328 bodyElement.add(h2); 329 } 330 } 331 332 protected void addElement(Element element) { 333 bodyElement.add(element); 334 bodyElement.add(new HTMLElement(HTMLReportConstants.BR)); 335 } 336 337 338 protected void addLink(Element element, String text, String link, 339 int textIndent){ 340 String indentation = indentText("", textIndent); 341 342 if(text!=null){ 343 if(link==null) 344 link="#"; 345 Link detail = new Link(text,link); 346 Element span = new HTMLElement(HTMLReportConstants.SPAN); 347 span.addText(indentation); 348 span.add(detail); 349 element.add(span); 350 element.add(new HTMLElement(HTMLReportConstants.BR)); 351 } 352 } 353 354 protected String getNextTocNo(){ 355 return String.valueOf(++tocNo); 356 } 357 358 protected String getTocNo(){ 359 return String.valueOf(tocNo); 360 } 361 362 365 private String indentText(String text, int indentIndex) { 366 String indentedText = text; 367 for(int i=1; i <=indentIndex; i++){ 368 indentedText = (Escape.getInstance(). 369 decodeEntities(" " + 370 " ")) + indentedText; 371 } 372 return indentedText; 373 374 } 375 376 private String getText(String str, Object [] args) { 377 MessageFormat form = new MessageFormat (str); 378 return form.format(str,args); 379 } 380 381 private void copyFromFile(Element element, String fileName) { 382 if ((element != null) && (fileName != null)) { 383 logger.log(Level.FINE,"fileName : " + fileName); 384 BufferedReader reader = null; 385 try { 386 reader = 387 new BufferedReader(new FileReader(fileName)); 388 389 String entry ; 390 while((entry = reader.readLine()) != null) { 391 logger.log(Level.FINE, "entry : " + entry); 392 element.addText(entry); 393 element.add(new HTMLElement("br")); 394 } 395 reader.close(); 396 397 } catch(IOException e) { 398 logger.log(Level.WARNING,"diagnostic-service.copy_failed", 399 new Object []{fileName,e.getMessage()}); 400 } finally { 401 402 } 403 } 404 } 405 } 406 | Popular Tags |