1 24 25 package org.objectweb.cjdbc.controller.core; 26 27 import java.io.File ; 28 import java.io.FileInputStream ; 29 import java.io.FileOutputStream ; 30 import java.io.PrintWriter ; 31 import java.io.StringWriter ; 32 import java.util.Date ; 33 import java.util.Enumeration ; 34 import java.util.Hashtable ; 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 52 public class ReportManager 53 { 54 55 56 static Trace logger = Trace 57 .getLogger(ReportManager.class 58 .getName()); 59 60 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 reportLocation = ControllerConstants.REPORT_LOCATION; 68 69 private Controller controller; 70 private StringBuffer buffer; 71 private FileOutputStream fos; 72 73 78 public ReportManager(Controller controller) 79 { 80 this.controller = controller; 81 buffer = new StringBuffer (); 82 } 84 85 89 public void startReport() 90 { 91 writeTitle("CJDBC (version:" + Constants.VERSION + ") REPORT generated on " 92 + new Date ().toString()); 93 writeJavaProperties(); 94 writeControllerSettings(); 95 writeControllerInfo(); 96 } 97 98 104 public ReportManager(Controller controller, boolean showLogsOnly) 105 { 106 this(controller); 107 this.showLogsOnly = showLogsOnly; 108 } 109 110 116 public ReportManager(Controller controller, Exception e) 117 { 118 this(controller); 119 writeException(e); 120 } 121 122 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 e) 138 { 139 e.printStackTrace(); 140 } 141 } 142 143 144 public void writeControllerSettings() 145 { 146 writeHeader("CONTROLLER SETTINGS"); 147 write(controller.getConfiguration()); 148 } 149 150 151 public void writeJavaProperties() 152 { 153 writeHeader("JAVA SETTINGS"); 154 write(System.getProperties()); 155 } 156 157 158 public void writeLogs() 159 { 160 writeHeader("LOG CONFIGURATION"); 161 String 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 179 public void writeException(Exception e) 180 { 181 writeHeader("EXCEPTION DESCRIPTION"); 182 write(e.getClass().toString()); 183 write(e.getMessage()); 184 write(e.toString()); 185 StringWriter sw = new StringWriter (); 186 PrintWriter pw = new PrintWriter (sw); 187 e.printStackTrace(pw); 188 write(sw.toString()); 189 } 190 191 196 public String generate() 197 { 198 writeLogs(); 200 try 201 { 202 File reportFile = new File (reportLocation + File.separator 203 + ControllerConstants.REPORT_FILE); 204 reportFile.getParentFile().mkdirs(); 205 fos = new FileOutputStream (reportFile); 206 fos.write(buffer.toString().getBytes()); 207 fos.close(); 208 String returned = buffer.toString(); 209 buffer.delete(0, buffer.length()); 211 return returned; 212 } 213 catch (Exception e) 214 { 215 e.printStackTrace(); 216 return ""; 217 } 218 } 219 220 225 public String generateJustLogs() 226 { 227 String 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 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 247 248 private void write(String string) 249 { 250 try 251 { 252 buffer.append(string.toString()); 253 } 254 catch (Exception e) 255 { 256 e.printStackTrace(); 257 } 258 } 259 260 private void writeFile(String filename) 261 { 262 try 263 { 264 File f = new File (filename); 265 FileInputStream fis = new FileInputStream (f); 266 byte[] logs = new byte[(int) f.length()]; 267 fis.read(logs); 268 write(new String (logs)); 269 } 270 catch (Exception e) 271 { 272 e.printStackTrace(); 273 } 274 } 275 276 private void write(Hashtable table) 277 { 278 buffer.append(ReadWrite.write(table, true)); 279 } 280 281 private void writeTitle(String 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 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 logFile = new File (appender.getFile()); 312 logFile.deleteOnExit(); 313 } 314 catch (Exception e) 315 { 316 logger.debug("Failed to set deleteOnExit on log file", e); 318 } 319 } 320 321 324 public final void setSettings(Hashtable settings) 325 { 326 if (settings == null) 328 { 329 reportEnabled = false; 330 } 331 else if (settings.containsKey(ControllerXmlTags.ATT_REPORT_ENABLED)) 332 { 333 reportEnabled = new Boolean ((String ) 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 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 ((String ) settings 356 .get(ControllerXmlTags.ATT_REPORT_ENABLE_FILE_LOGGING)) 357 .booleanValue(); 358 if (!enableFileLogging) 359 { 360 } 362 } 363 if (settings.containsKey(ControllerXmlTags.ATT_REPORT_GENERATE_ON_FATAL)) 364 { 365 generateOnFatal = new Boolean ((String ) 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 ((String ) 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 ((String ) 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 ) 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 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 407 public boolean isEnableFileLogging() 408 { 409 return enableFileLogging; 410 } 411 412 415 public boolean isGenerateOnFatal() 416 { 417 return reportEnabled && generateOnFatal; 418 } 419 420 423 public boolean isGenerateOnShutdown() 424 { 425 return reportEnabled && generateOnShutdown; 426 } 427 428 431 public boolean isHideSensitiveData() 432 { 433 return hideSensitiveData; 434 } 435 436 439 public boolean isReportEnabled() 440 { 441 return reportEnabled; 442 } 443 444 447 public String getReportLocation() 448 { 449 return reportLocation; 450 } 451 452 457 public void setEnableFileLogging(boolean enableFileLogging) 458 { 459 this.enableFileLogging = enableFileLogging; 460 } 461 462 467 public void setGenerateOnFatal(boolean generateOnFatal) 468 { 469 this.generateOnFatal = generateOnFatal; 470 } 471 472 477 public void setGenerateOnShutdown(boolean generateOnShutdown) 478 { 479 this.generateOnShutdown = generateOnShutdown; 480 } 481 482 487 public void setHideSensitiveData(boolean hideSensitiveData) 488 { 489 this.hideSensitiveData = hideSensitiveData; 490 } 491 492 497 public void setReportEnabled(boolean reportEnabled) 498 { 499 this.reportEnabled = reportEnabled; 500 } 501 502 507 public void setReportLocation(String reportLocation) 508 { 509 this.reportLocation = reportLocation; 510 } 511 } | Popular Tags |