KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > sun > enterprise > diagnostics > report > html > HTMLReportTemplate


1 /*
2  * The contents of this file are subject to the terms
3  * of the Common Development and Distribution License
4  * (the License). You may not use this file except in
5  * compliance with the License.
6  *
7  * You can obtain a copy of the license at
8  * https://glassfish.dev.java.net/public/CDDLv1.0.html or
9  * glassfish/bootstrap/legal/CDDLv1.0.txt.
10  * See the License for the specific language governing
11  * permissions and limitations under the License.
12  *
13  * When distributing Covered Code, include this CDDL
14  * Header Notice in each file and include the License file
15  * at glassfish/bootstrap/legal/CDDLv1.0.txt.
16  * If applicable, add the following below the CDDL Header,
17  * with the fields enclosed by brackets [] replaced by
18  * you own identifying information:
19  * "Portions Copyrighted [year] [name of copyright owner]"
20  *
21  * Copyright 2006 Sun Microsystems, Inc. All rights reserved.
22  */

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 JavaDoc;
33 import java.util.logging.Level JavaDoc;
34 import java.util.logging.Logger JavaDoc;
35 import com.sun.logging.LogDomains;
36
37 /**
38  *
39  * @author Manisha Umbarje
40  */

41 public class HTMLReportTemplate {
42     private static final String JavaDoc 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 JavaDoc targetName;
51     private String JavaDoc reportDir;
52     protected DataTraverser dataObjTraverser ;
53
54     private static String JavaDoc report_description =
55             "This Snapshot was generated for {0} at " +
56             "{1,time} on {1,date}";
57
58     private static final String JavaDoc description = "Description";
59     private static final String JavaDoc customer_information = "Customer Information";
60     private static final String JavaDoc bug_ids = "Bug IDs";
61     private static String JavaDoc component_details = "{0} details ";
62     private static final String JavaDoc checksum_details = "Checksum Information";
63     protected static final String JavaDoc monitoring_information = "Monitoring Information";
64     private static final String JavaDoc system_information = "System Information";
65     private static final String JavaDoc installation_log = "Installation Log";
66     private static final String JavaDoc domain_xml_validation_details = "Domain Validation Details";
67     private static Logger JavaDoc logger =
68             LogDomains.getLogger(LogDomains.ADMIN_LOGGER);
69
70      /**
71       * Creates a new instance of HTMLReportTemplate
72       * @targetName name of the target for which HTML report is being generated
73       * @reportDir directory which contains data to generate HTML report summary
74       */

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 JavaDoc reportFile = reportDir + REPORT_NAME;
87             //System.out.println("Report Summary File Name : " + reportFile);
88
File file = new File(reportFile);
89             //System.out.println("DOC : " + doc.toString());
90
BufferedWriter writer = new BufferedWriter(
91                     new FileWriter(file));
92
93             writer.write(doc.toString());
94             writer.flush();
95         } catch (Exception JavaDoc e) {
96             e.printStackTrace();
97             //@todo handle exception intelligently
98
}
99     }
100
101     protected void addMiscellaneousInfo(){
102         // Do nothing for PE
103
}
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 JavaDoc description = getText(report_description,
143                 new Object JavaDoc[] {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 JavaDoc>> 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 JavaDoc componentDetailsTitle = getText(component_details,
205                 new Object JavaDoc[] {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 JavaDoc instanceName = config.getInstanceName();
218                 int indentation = 1;
219                 String JavaDoc linkPrefix = "." + File.separator ;
220                 String JavaDoc 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                 //add generated and applications only if directories exist
231
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             }//while
253
}
254     }
255
256     protected void addMonitoringInfo(Element element, String JavaDoc 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 JavaDoc> files = new ArrayList(2);
268         if(enabled) {
269             String JavaDoc[] logFiles =
270                     new File(
271                     target.getIntermediateReportDir()).list(new FilenameFilter() {
272                public boolean accept(File folder, String JavaDoc 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 JavaDoc fileName : files) {
283             addLink(element, fileName, fileName, indentation);
284         }
285     }
286
287
288     private void addLink(boolean enabled, Element element, String JavaDoc dataType,
289             String JavaDoc 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 JavaDoc parent, String JavaDoc folder) {
299         File file = new File(parent + File.separator +
300                 folder);
301         return file.exists();
302     }
303
304      protected void addTitle(String JavaDoc title, boolean add2TOC,
305             boolean showTitleInBody,
306             int titleIndent, int textIndent){
307
308         String JavaDoc 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 JavaDoc text, String JavaDoc link,
339             int textIndent){
340         String JavaDoc 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 JavaDoc getNextTocNo(){
355       return String.valueOf(++tocNo);
356     }
357
358     protected String JavaDoc getTocNo(){
359       return String.valueOf(tocNo);
360     }
361
362     /**
363      * Indent a text
364      */

365     private String JavaDoc indentText(String JavaDoc text, int indentIndex) {
366         String JavaDoc indentedText = text;
367         for(int i=1; i <=indentIndex; i++){
368             indentedText = (Escape.getInstance().
369              decodeEntities("&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;" +
370              "&nbsp;&nbsp;&nbsp;")) + indentedText;
371         }
372         return indentedText;
373
374     }
375
376     private String JavaDoc getText(String JavaDoc str, Object JavaDoc[] args) {
377         MessageFormat JavaDoc form = new MessageFormat JavaDoc(str);
378         return form.format(str,args);
379     }
380
381     private void copyFromFile(Element element, String JavaDoc 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 JavaDoc 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 JavaDoc[]{fileName,e.getMessage()});
400             } finally {
401
402             }
403         }
404     }
405 }
406
Popular Tags