1 package com.protomatter.syslog.xml; 2 3 52 53 import java.io.*; 54 import java.net.*; 55 import java.util.*; 56 import java.text.*; 57 import com.protomatter.syslog.*; 58 import com.protomatter.util.Debug; 59 import com.protomatter.xml.*; 60 import org.jdom.*; 61 import org.jdom.output.*; 62 import org.jdom.input.*; 63 64 67 public class SyslogXML 68 { 69 private static String CLASSLOADER_WARNING_PROPERTY = "Syslog.classloader.warning"; 73 private static String WARNING_OFF = "off"; 74 75 private static boolean WARNING_DEFAULT = true; 77 78 81 public static String XML_PARSER_PROPERTY = "Syslog.xml.parser"; 82 83 86 private SyslogXML() 87 { 88 super(); 89 } 90 91 96 public static void writeConfiguration(OutputStream out) 97 throws IOException 98 { 99 Document d = getConfiguration(); 100 XMLOutputter output = new XMLOutputter(); 101 output.setIndent(" "); 104 output.setNewlines(true); 105 output.output(d, out); 107 } 108 109 113 public static Document getConfiguration() 114 { 115 Element e = new Element("Syslog"); 116 Document d = new Document(e); 117 e.setAttribute("defaultMask", Syslog.getLogMaskAsString()); 118 119 long sleepInterval = Syslog.getFlushThreadInterval(); 120 if (sleepInterval != 0) 121 e.setAttribute("flushThreadInterval", String.valueOf(sleepInterval)); 122 123 if (Syslog.getLocalHostName() != null) 124 { 125 InetAddress addr = Syslog.getLocalHostName(); 126 String name = addr.getHostName(); 127 if (name == null) 128 name = addr.getHostAddress(); 129 e.setAttribute("hostname", name); 130 } 131 132 e.setAttribute("computeCaller", String.valueOf(Syslog.getComputeCaller())); 133 e.setAttribute("alwaysComputeCaller", String.valueOf(Syslog.getAlwaysComputeCaller())); 134 135 Element debug = new Element("Debug"); 136 debug.setAttribute("enabled", String.valueOf(Debug.isEnabled())); 137 debug.setAttribute("scan", String.valueOf(Debug.getScan())); 138 Iterator list = Debug.getTraceNames(); 139 while (list.hasNext()) 140 { 141 Element config = new Element("name"); 142 config.setText((String )list.next()); 143 config.setAttribute("level", "trace"); 144 debug.getChildren().add(config); 145 } 146 list = Debug.getDebugNames(); 147 while (list.hasNext()) 148 { 149 Element config = new Element("name"); 150 config.setText((String )list.next()); 151 config.setAttribute("level", "debug"); 152 debug.getChildren().add(config); 153 } 154 list = Debug.getInfoNames(); 155 while (list.hasNext()) 156 { 157 Element config = new Element("name"); 158 config.setText((String )list.next()); 159 config.setAttribute("level", "info"); 160 debug.getChildren().add(config); 161 } 162 e.getChildren().add(debug); 163 164 list = Syslog.getLoggers(); 165 while (list.hasNext()) 166 { 167 Syslogger logger = (Syslogger)list.next(); 168 XMLConfigHelper helper = null; 169 try 170 { 171 helper = XMLConfigUtil.getConfigHelper(logger); 172 } 173 catch (Exception x) 174 { 175 x.printStackTrace(); 176 } 177 Element config = helper.getConfiguration(logger, null); 178 e.getChildren().add(config); 179 } 180 181 return d; 182 } 183 184 199 public static void configure(File xmlFile) 200 throws SyslogInitException 201 { 202 configure(xmlFile, System.getProperty(XML_PARSER_PROPERTY), WARNING_DEFAULT); 203 } 204 205 220 public static void configure(InputStream input) 221 throws SyslogInitException 222 { 223 configure(input, System.getProperty(XML_PARSER_PROPERTY), WARNING_DEFAULT); 224 } 225 226 240 public static void configure(File xmlFile, String saxDriverClass, boolean showClassloaderWarning) 241 throws SyslogInitException 242 { 243 if (!xmlFile.exists()) 244 { 245 throw new SyslogInitException(Syslog.getResourceString(MessageConstants.CANNOT_CONFIGURE_MESSAGE), 246 new FileNotFoundException( 247 MessageFormat.format(Syslog.getResourceString(MessageConstants.FILE_NOT_FOUND_MESSAGE), 248 new Object [] { xmlFile }) 249 )); 250 } 251 252 if (!showClassloaderWarning) 254 System.setProperty(CLASSLOADER_WARNING_PROPERTY, WARNING_OFF); 255 256 try 257 { 258 String rootName = "Syslog"; 259 SAXBuilder builder = null; 260 if (saxDriverClass == null) 261 builder = new SAXBuilder(); 262 else 263 builder = new SAXBuilder(saxDriverClass); 264 Document document = builder.build(xmlFile); 265 Element c = document.getRootElement(); 266 if (c == null) 267 throw new SyslogInitException(MessageFormat.format( 268 Syslog.getResourceString(MessageConstants.XML_NO_SYSLOG_ELEMENT_MESSAGE), 269 new Object [] { "Syslog" })); 270 if (!c.getName().equals(rootName)) 271 { 272 c = c.getChild(rootName, c.getNamespace()); 273 if ((c == null) || (!rootName.equals(c.getName()))) 274 throw new SyslogInitException(MessageFormat.format( 275 Syslog.getResourceString(MessageConstants.XML_NO_SYSLOG_ELEMENT_MESSAGE), 276 new Object [] { "Syslog" })); 277 } 278 configure(c); 279 } 280 catch (Exception x) 281 { 282 if (x instanceof SyslogInitException) 283 throw (SyslogInitException)x; 284 throw new SyslogInitException(Syslog.getResourceString(MessageConstants.CANNOT_CONFIGURE_MESSAGE), x); 285 } 286 } 287 288 302 public static void configure(InputStream input, String saxDriverClass, boolean showClassloaderWarning) 303 throws SyslogInitException 304 { 305 try 306 { 307 if (!showClassloaderWarning) 309 System.setProperty(CLASSLOADER_WARNING_PROPERTY, WARNING_OFF); 310 String rootName = "Syslog"; 311 SAXBuilder builder = null; 312 if (saxDriverClass == null) 313 builder = new SAXBuilder(); 314 else 315 builder = new SAXBuilder(saxDriverClass); 316 Document document = builder.build(input); 317 Element c = document.getRootElement(); 318 if (c == null) 319 throw new SyslogInitException(MessageFormat.format( 320 Syslog.getResourceString(MessageConstants.XML_NO_SYSLOG_ELEMENT_MESSAGE), 321 new Object [] { "Syslog" })); 322 if (!c.getName().equals(rootName)) 323 { 324 c = c.getChild(rootName, c.getNamespace()); 325 if ((c == null) || (!c.getName().equals(rootName))) 326 throw new SyslogInitException(MessageFormat.format( 327 Syslog.getResourceString(MessageConstants.XML_NO_SYSLOG_ELEMENT_MESSAGE), 328 new Object [] { "Syslog" })); 329 } 330 configure(c); 331 } 332 catch (Exception x) 333 { 334 x.printStackTrace(); 335 if (x instanceof SyslogInitException) 336 throw (SyslogInitException)x; 337 throw new SyslogInitException(Syslog.getResourceString(MessageConstants.CANNOT_CONFIGURE_MESSAGE), x); 338 } 339 } 340 341 405 public static boolean configure(Element syslogConfig) 406 throws SyslogInitException 407 { 408 try 410 { 411 Syslog.removeAllLoggers(); 412 Debug.clear(); 413 414 String syslogLevel = syslogConfig.getAttributeValue("defaultMask"); 415 if (syslogLevel != null) 416 Syslog.setLogMask(syslogLevel); 417 418 String interval = syslogConfig.getAttributeValue("flushThreadInterval"); 419 if (interval != null) 420 Syslog.setFlushThreadInterval(Long.parseLong(interval)); 421 422 String name = syslogConfig.getAttributeValue("hostname"); 423 if (name != null) 424 { 425 Syslog.setLocalHostName(InetAddress.getByName(name)); 426 } 427 428 String computeCaller = syslogConfig.getAttributeValue("computeCaller"); 429 if (computeCaller != null) 430 { 431 Syslog.setComputeCaller("true".equalsIgnoreCase(computeCaller)); 432 } 433 434 computeCaller = syslogConfig.getAttributeValue("alwaysComputeCaller"); 435 if (computeCaller != null) 436 { 437 Syslog.setAlwaysComputeCaller("true".equalsIgnoreCase(computeCaller)); 438 } 439 440 Iterator list = syslogConfig.getChildren("Debug", syslogConfig.getNamespace()).iterator(); 442 if (list.hasNext()) 443 { 444 Element element = (Element)list.next(); 445 446 String enabled = element.getAttributeValue("enabled"); 447 if ("true".equalsIgnoreCase(enabled)) 448 Debug.enable(); 449 else 450 Debug.disable(); 451 452 String scan = element.getAttributeValue("scan"); 453 if (scan != null) 454 Debug.setScan("true".equalsIgnoreCase(scan)); 455 456 Iterator children = element.getChildren("name", element.getNamespace()).iterator(); 457 while (children.hasNext()) 458 { 459 Element child = (Element)children.next(); 460 String severity = child.getAttributeValue("level"); 461 if ("trace".equals(severity)) 462 Debug.addInfoName(child.getText()); 463 else if ("debug".equals(severity)) 464 Debug.addDebugName(child.getText()); 465 else if ("info".equals(severity)) 466 Debug.addInfoName(child.getText()); 467 } 468 } 469 470 list = syslogConfig.getChildren("Logger", 471 syslogConfig.getNamespace()).iterator(); 472 while (list.hasNext()) 473 { 474 Element element = (Element)list.next(); 475 String lClass = element.getAttributeValue("class"); 476 String lName = element.getAttributeValue("name"); 477 try 478 { 479 Syslogger logger = (Syslogger)Class.forName(lClass).newInstance(); 480 XMLConfigHelper helper = XMLConfigUtil.getConfigHelper(logger); 481 helper.configure(logger, element); 482 logger.setName(lName); 483 Syslog.addLogger(logger); 484 } 485 catch (Exception x) 486 { 487 throw new SyslogInitException( 488 MessageFormat.format(Syslog.getResourceString(MessageConstants.CANNOT_REGISTER_LOGGER_MESSAGE), 489 new Object [] { lName }), x); 490 } 491 } 492 } 493 catch (Exception x) 494 { 495 if (x instanceof SyslogInitException) 496 throw (SyslogInitException)x; 497 throw new SyslogInitException(Syslog.getResourceString(MessageConstants.CANNOT_CONFIGURE_MESSAGE), x); 498 } 499 500 return true; 501 } 502 503 514 public static final void main(String args[]) 515 { 516 ResourceBundle bundle = null; 517 try 518 { 519 String bundleName = "com.protomatter.syslog.Syslog"; 520 bundle = ResourceBundle.getBundle(bundleName); 521 if (bundle == null) 522 { 523 System.err.println("ERROR: Cannot load resource bundle \"" + bundleName + "\""); 524 System.exit(0); 525 } 526 if (args.length == 1) 527 { 528 File file = new File(args[0]); 529 System.out.println(MessageFormat.format(bundle.getString(MessageConstants.XML_LOADING_CONFIG_FILE), 530 new Object [] { file } )); 531 long time = System.currentTimeMillis(); 532 SyslogXML.configure(file); 533 time = System.currentTimeMillis() - time; 534 System.out.println(MessageFormat.format(bundle.getString(MessageConstants.XML_LOADED_CONFIG_IN), 535 new Object [] { String.valueOf(time) } )); 536 } 537 System.out.println(bundle.getString(MessageConstants.XML_DUMPING_CONFIG)); 538 System.out.println("----------------------------------------------------------------------"); 539 SyslogXML.writeConfiguration(System.out); 540 System.out.println("----------------------------------------------------------------------"); 541 System.out.println(bundle.getString(MessageConstants.XML_CONFIG_OK)); 542 } 543 catch (Exception x) 544 { 545 System.out.println(bundle.getString(MessageConstants.XML_CONFIG_EXCEPTION)); 546 x.printStackTrace(); 547 } 548 } 549 } 550 | Popular Tags |