KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > objectweb > cjdbc > controller > core > ReportManager


1 /**
2  * C-JDBC: Clustered JDBC.
3  * Copyright (C) 2002-2005 French National Institute For Research In Computer
4  * Science And Control (INRIA).
5  * Contact: c-jdbc@objectweb.org
6  *
7  * This library is free software; you can redistribute it and/or modify it
8  * under the terms of the GNU Lesser General Public License as published by the
9  * Free Software Foundation; either version 2.1 of the License, or any later
10  * version.
11  *
12  * This library is distributed in the hope that it will be useful, but WITHOUT
13  * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
14  * FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License
15  * for more details.
16  *
17  * You should have received a copy of the GNU Lesser General Public License
18  * along with this library; if not, write to the Free Software Foundation,
19  * Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA.
20  *
21  * Initial developer(s): Emmanuel Cecchet.
22  * Contributor(s): Nicolas Modrzyk, Mathieu Peltier.
23  */

24
25 package org.objectweb.cjdbc.controller.core;
26
27 import java.io.File JavaDoc;
28 import java.io.FileInputStream JavaDoc;
29 import java.io.FileOutputStream JavaDoc;
30 import java.io.PrintWriter JavaDoc;
31 import java.io.StringWriter JavaDoc;
32 import java.util.Date JavaDoc;
33 import java.util.Enumeration JavaDoc;
34 import java.util.Hashtable JavaDoc;
35
36 import org.apache.log4j.FileAppender;
37 import org.apache.log4j.Logger;
38 import org.apache.log4j.RollingFileAppender;
39 import org.objectweb.cjdbc.common.log.Trace;
40 import org.objectweb.cjdbc.common.util.Constants;
41 import org.objectweb.cjdbc.common.util.ReadWrite;
42 import org.objectweb.cjdbc.common.xml.ControllerXmlTags;
43 import org.objectweb.cjdbc.common.xml.XmlTools;
44
45 /**
46  * Class to create report from Controller
47  *
48  * @author <a HREF="mailto:Emmanuel.Cecchet@inria.fr">Emmanuel Cecchet </a>
49  * @author <a HREF="mailto:Nicolas.Modrzyk@inrialpes.fr">Nicolas Modrzyk </a>
50  * @author <a HREF="mailto:Mathieu.Peltier@inrialpes.fr">Mathieu Peltier </a>
51  */

52 public class ReportManager
53 {
54
55   /** Logger instance. */
56   static Trace logger = Trace
57                                                   .getLogger(ReportManager.class
58                                                       .getName());
59
60   /** Settings */
61   boolean reportEnabled = false;
62   boolean hideSensitiveData = true;
63   boolean generateOnShutdown = true;
64   boolean generateOnFatal = true;
65   boolean enableFileLogging = true;
66   boolean showLogsOnly = false;
67   String JavaDoc reportLocation = ControllerConstants.REPORT_LOCATION;
68
69   private Controller controller;
70   private StringBuffer JavaDoc buffer;
71   private FileOutputStream JavaDoc fos;
72
73   /**
74    * Call above and write controller xml information and information
75    *
76    * @param controller to report
77    */

78   public ReportManager(Controller controller)
79   {
80     this.controller = controller;
81     buffer = new StringBuffer JavaDoc();
82     //listLoggers();
83
}
84
85   /**
86    * Starts generating the report. Effectively write the java properties,
87    * controller settings, controller info but do not write the logs yet.
88    */

89   public void startReport()
90   {
91     writeTitle("CJDBC (version:" + Constants.VERSION + ") REPORT generated on "
92         + new Date JavaDoc().toString());
93     writeJavaProperties();
94     writeControllerSettings();
95     writeControllerInfo();
96   }
97
98   /**
99    * Creates a new <code>ReportManager.java</code> object Report only logs
100    *
101    * @param controller the controller to report logs from
102    * @param showLogsOnly show logs
103    */

104   public ReportManager(Controller controller, boolean showLogsOnly)
105   {
106     this(controller);
107     this.showLogsOnly = showLogsOnly;
108   }
109
110   /**
111    * Call above and write about the exception
112    *
113    * @param controller to report
114    * @param e exception
115    */

116   public ReportManager(Controller controller, Exception JavaDoc e)
117   {
118     this(controller);
119     writeException(e);
120   }
121
122   /**
123    * Write Controller info as return by <code>getInformation()</code>
124    */

125   public void writeControllerInfo()
126   {
127     try
128     {
129       writeHeader("CONTROLLER INFO XML");
130       write(controller.getXml());
131       writeHeader("DATABASE INFO");
132       write(XmlTools
133           .applyXsl(controller.getXmlVirtualDatabases(), "c-jdbc.xsl"));
134       writeHeader("DATABASE INFO XML");
135       write(XmlTools.prettyXml(controller.getXmlVirtualDatabases()));
136     }
137     catch (Exception JavaDoc e)
138     {
139       e.printStackTrace();
140     }
141   }
142
143   /** Write all parameters from <code>ControllerFactory</code> */
144   public void writeControllerSettings()
145   {
146     writeHeader("CONTROLLER SETTINGS");
147     write(controller.getConfiguration());
148   }
149
150   /** Write All Java Properties */
151   public void writeJavaProperties()
152   {
153     writeHeader("JAVA SETTINGS");
154     write(System.getProperties());
155   }
156
157   /** Write Logs */
158   public void writeLogs()
159   {
160     writeHeader("LOG CONFIGURATION");
161     String JavaDoc s = this.getClass().getResource(ControllerConstants.LOG4J_RESOURCE)
162         .getFile();
163     writeFile(s);
164     writeHeader("LOGS");
165     if (isEnableFileLogging())
166     {
167       Logger log = Logger.getRootLogger();
168       FileAppender appender = (FileAppender) log.getAppender("Filetrace");
169       s = appender.getFile();
170       writeFile(s);
171     }
172   }
173
174   /**
175    * Write Details of the exception
176    *
177    * @param e exception to write
178    */

179   public void writeException(Exception JavaDoc e)
180   {
181     writeHeader("EXCEPTION DESCRIPTION");
182     write(e.getClass().toString());
183     write(e.getMessage());
184     write(e.toString());
185     StringWriter JavaDoc sw = new StringWriter JavaDoc();
186     PrintWriter JavaDoc pw = new PrintWriter JavaDoc(sw);
187     e.printStackTrace(pw);
188     write(sw.toString());
189   }
190
191   /**
192    * Flush and close
193    *
194    * @return report content
195    */

196   public String JavaDoc generate()
197   {
198     // Here we get all the logs before writing report
199
writeLogs();
200     try
201     {
202       File JavaDoc reportFile = new File JavaDoc(reportLocation + File.separator
203           + ControllerConstants.REPORT_FILE);
204       reportFile.getParentFile().mkdirs();
205       fos = new FileOutputStream JavaDoc(reportFile);
206       fos.write(buffer.toString().getBytes());
207       fos.close();
208       String JavaDoc returned = buffer.toString();
209       // Reset buffer
210
buffer.delete(0, buffer.length());
211       return returned;
212     }
213     catch (Exception JavaDoc e)
214     {
215       e.printStackTrace();
216       return "";
217     }
218   }
219
220   /**
221    * Get a trace of the logs only
222    *
223    * @return <code>String<code> content of the log4j output
224    */

225   public String JavaDoc generateJustLogs()
226   {
227     String JavaDoc s = this.getClass().getResource(ControllerConstants.LOG4J_RESOURCE)
228         .getFile();
229     Logger log = Logger.getRootLogger();
230     try
231     {
232       FileAppender appender = (FileAppender) log.getAppender("Filetrace");
233       s = appender.getFile();
234       writeFile(s);
235       return buffer.toString();
236     }
237     catch (Exception JavaDoc e)
238     {
239       logger
240           .warn("Filetrace appender is not of type FileAppender and cannot be dumped ("
241               + e + ")");
242       return "";
243     }
244   }
245
246   /* Write Methods */
247
248   private void write(String JavaDoc string)
249   {
250     try
251     {
252       buffer.append(string.toString());
253     }
254     catch (Exception JavaDoc e)
255     {
256       e.printStackTrace();
257     }
258   }
259
260   private void writeFile(String JavaDoc filename)
261   {
262     try
263     {
264       File JavaDoc f = new File JavaDoc(filename);
265       FileInputStream JavaDoc fis = new FileInputStream JavaDoc(f);
266       byte[] logs = new byte[(int) f.length()];
267       fis.read(logs);
268       write(new String JavaDoc(logs));
269     }
270     catch (Exception JavaDoc e)
271     {
272       e.printStackTrace();
273     }
274   }
275
276   private void write(Hashtable JavaDoc table)
277   {
278     buffer.append(ReadWrite.write(table, true));
279   }
280
281   private void writeTitle(String JavaDoc title)
282   {
283     write("==========================================================================="
284         + System.getProperty("line.separator"));
285     write("==========================================================================="
286         + System.getProperty("line.separator"));
287     write("==== " + title + System.getProperty("line.separator"));
288     write("==========================================================================="
289         + System.getProperty("line.separator"));
290     write("==========================================================================="
291         + System.getProperty("line.separator"));
292   }
293
294   private void writeHeader(String JavaDoc header)
295   {
296     write(System.getProperty("line.separator"));
297     write("############################################################"
298         + System.getProperty("line.separator"));
299     write("####\t\t" + header + System.getProperty("line.separator"));
300     write("############################################################"
301         + System.getProperty("line.separator"));
302   }
303
304   private void setLogsDeleteOnExit()
305   {
306     try
307     {
308       Logger log = Logger.getRootLogger();
309       RollingFileAppender appender = (RollingFileAppender) log
310           .getAppender("Filetrace");
311       File JavaDoc logFile = new File JavaDoc(appender.getFile());
312       logFile.deleteOnExit();
313     }
314     catch (Exception JavaDoc e)
315     {
316       // appender has been removed or is not defined.
317
logger.debug("Failed to set deleteOnExit on log file", e);
318     }
319   }
320
321   /**
322    * @param settings hashtable of settings
323    */

324   public final void setSettings(Hashtable JavaDoc settings)
325   {
326     //listLoggers();
327
if (settings == null)
328     {
329       reportEnabled = false;
330     }
331     else if (settings.containsKey(ControllerXmlTags.ATT_REPORT_ENABLED))
332     {
333       reportEnabled = new Boolean JavaDoc((String JavaDoc) settings
334           .get(ControllerXmlTags.ATT_REPORT_ENABLED)).booleanValue();
335     }
336     if (!reportEnabled)
337     {
338       hideSensitiveData = false;
339       generateOnShutdown = false;
340       generateOnFatal = false;
341       enableFileLogging = false;
342       //removeFileTraceAppender();
343
return;
344     }
345     else
346     {
347       if ("true".equals(settings
348           .get(ControllerXmlTags.ATT_REPORT_DELETE_ON_SHUTDOWN)))
349       {
350         setLogsDeleteOnExit();
351       }
352       if (settings
353           .containsKey(ControllerXmlTags.ATT_REPORT_ENABLE_FILE_LOGGING))
354       {
355         enableFileLogging = new Boolean JavaDoc((String JavaDoc) settings
356             .get(ControllerXmlTags.ATT_REPORT_ENABLE_FILE_LOGGING))
357             .booleanValue();
358         if (!enableFileLogging)
359         {
360           //removeFileTraceAppender();
361
}
362       }
363       if (settings.containsKey(ControllerXmlTags.ATT_REPORT_GENERATE_ON_FATAL))
364       {
365         generateOnFatal = new Boolean JavaDoc((String JavaDoc) settings
366             .get(ControllerXmlTags.ATT_REPORT_GENERATE_ON_FATAL))
367             .booleanValue();
368       }
369       if (settings
370           .containsKey(ControllerXmlTags.ATT_REPORT_GENERATE_ON_SHUTDOWN))
371       {
372         generateOnShutdown = new Boolean JavaDoc((String JavaDoc) settings
373             .get(ControllerXmlTags.ATT_REPORT_GENERATE_ON_SHUTDOWN))
374             .booleanValue();
375       }
376       if (settings
377           .containsKey(ControllerXmlTags.ATT_REPORT_HIDE_SENSITIVE_DATA))
378       {
379         hideSensitiveData = new Boolean JavaDoc((String JavaDoc) settings
380             .get(ControllerXmlTags.ATT_REPORT_HIDE_SENSITIVE_DATA))
381             .booleanValue();
382       }
383       if (settings.containsKey(ControllerXmlTags.ATT_REPORT_REPORT_LOCATION))
384       {
385         reportLocation = (String JavaDoc) settings
386             .get(ControllerXmlTags.ATT_REPORT_REPORT_LOCATION);
387       }
388     }
389   }
390
391   void listLoggers()
392   {
393     Logger log = Logger.getRootLogger();
394     if (!log.isDebugEnabled())
395       return;
396     Enumeration JavaDoc loggers = Logger.getDefaultHierarchy().getCurrentLoggers();
397     while (loggers.hasMoreElements())
398     {
399       Logger l = (Logger) loggers.nextElement();
400       log.debug("Found logger:" + l.getName());
401     }
402   }
403
404   /**
405    * @return Returns the enableFileLogging.
406    */

407   public boolean isEnableFileLogging()
408   {
409     return enableFileLogging;
410   }
411
412   /**
413    * @return Returns the generateOnFatal.
414    */

415   public boolean isGenerateOnFatal()
416   {
417     return reportEnabled && generateOnFatal;
418   }
419
420   /**
421    * @return Returns the generateOnShutdown.
422    */

423   public boolean isGenerateOnShutdown()
424   {
425     return reportEnabled && generateOnShutdown;
426   }
427
428   /**
429    * @return Returns the hideSensitiveData.
430    */

431   public boolean isHideSensitiveData()
432   {
433     return hideSensitiveData;
434   }
435
436   /**
437    * @return Returns the reportEnabled.
438    */

439   public boolean isReportEnabled()
440   {
441     return reportEnabled;
442   }
443
444   /**
445    * @return Returns the reportLocation.
446    */

447   public String JavaDoc getReportLocation()
448   {
449     return reportLocation;
450   }
451
452   /**
453    * Sets the enableFileLogging value.
454    *
455    * @param enableFileLogging The enableFileLogging to set.
456    */

457   public void setEnableFileLogging(boolean enableFileLogging)
458   {
459     this.enableFileLogging = enableFileLogging;
460   }
461
462   /**
463    * Sets the generateOnFatal value.
464    *
465    * @param generateOnFatal The generateOnFatal to set.
466    */

467   public void setGenerateOnFatal(boolean generateOnFatal)
468   {
469     this.generateOnFatal = generateOnFatal;
470   }
471
472   /**
473    * Sets the generateOnShutdown value.
474    *
475    * @param generateOnShutdown The generateOnShutdown to set.
476    */

477   public void setGenerateOnShutdown(boolean generateOnShutdown)
478   {
479     this.generateOnShutdown = generateOnShutdown;
480   }
481
482   /**
483    * Sets the hideSensitiveData value.
484    *
485    * @param hideSensitiveData The hideSensitiveData to set.
486    */

487   public void setHideSensitiveData(boolean hideSensitiveData)
488   {
489     this.hideSensitiveData = hideSensitiveData;
490   }
491
492   /**
493    * Sets the reportEnabled value.
494    *
495    * @param reportEnabled The reportEnabled to set.
496    */

497   public void setReportEnabled(boolean reportEnabled)
498   {
499     this.reportEnabled = reportEnabled;
500   }
501
502   /**
503    * Sets the reportLocation value.
504    *
505    * @param reportLocation The reportLocation to set.
506    */

507   public void setReportLocation(String JavaDoc reportLocation)
508   {
509     this.reportLocation = reportLocation;
510   }
511 }
Popular Tags