1 6 7 package org.jzonic.jlo.reader; 8 9 import org.jzonic.jlo.*; 10 import org.jzonic.jlo.error.ErrorHandler; 11 import org.jzonic.jlo.filter.LogFilter; 12 import org.jzonic.jlo.formatter.*; 13 import org.jzonic.jlo.handler.*; 14 import org.w3c.dom.Document ; 15 import org.w3c.dom.NamedNodeMap ; 16 import org.w3c.dom.Node ; 17 import org.w3c.dom.NodeList ; 18 import org.xml.sax.SAXException ; 19 20 import javax.xml.parsers.DocumentBuilder ; 21 import javax.xml.parsers.DocumentBuilderFactory ; 22 import javax.xml.parsers.ParserConfigurationException ; 23 import java.io.IOException ; 24 import java.io.InputStream ; 25 import java.util.HashMap ; 26 import java.util.Hashtable ; 27 import java.util.Map ; 28 import java.util.Vector ; 29 33 public class XMLFileReader implements LogConfigurationReader { 34 35 private String fileName; 36 private static final VariableManager variableManager = VariableManager.getInstance(); 37 38 public XMLFileReader() { 39 } 40 41 public void setFileName(String fileName) { 42 this.fileName = fileName; 43 } 44 45 public LogConfiguration parseConfiguration(String configurationName) throws ReaderException { 46 ResourceLocator rl = new ResourceLocator(); 47 InputStream is = rl.findResource(fileName); 48 if ( is == null ) { 49 throw new ReaderException("the file "+fileName+" cannot be found in the classpath"); 50 } 51 DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance(); 52 dbf.setValidating(false); 53 DocumentBuilder db = null; 54 try { 55 db = dbf.newDocumentBuilder(); 56 } catch (ParserConfigurationException pce) { 57 ErrorHandler.reportError("Exception during parser configuration",pce); 58 throw new ReaderException("Exception during parser configuration:" + pce.getMessage()); 59 } 60 Document doc = null; 61 try { 62 doc = db.parse(is); 63 } catch (SAXException se) { 64 ErrorHandler.reportError("Exception while parsing the file", se); 65 throw new ReaderException( 66 "Exception while parsing the file:" + se.getMessage()); 67 } catch (IOException ioe) { 68 ErrorHandler.reportError("IO-Exception", ioe); 69 throw new ReaderException("IO-Exception:" + ioe.getMessage()); 70 } 71 return processXML(doc,configurationName); 72 } 73 74 private LogConfiguration processXML(Document doc,String configurationName) { 75 LogConfiguration lc = new LogConfiguration(configurationName); 76 getFilters(doc, lc); 77 getGenerators(doc, lc); 78 getLoggers(doc, lc); 79 getChannels(doc, lc); 80 getVariables(doc, lc); 82 getPipes(doc, lc); 83 return lc; 84 } 85 86 private void getLoggers(Document doc, LogConfiguration lc) { 87 String currentLogger; 88 NodeList nl = doc.getElementsByTagName("logger"); 89 for (int i = 0; i < nl.getLength(); i++) { 90 Node n = nl.item(i); 91 NamedNodeMap curAtt = n.getAttributes(); 92 Node curNode = curAtt.getNamedItem("name"); 93 currentLogger = curNode.getNodeValue(); 94 String lvl = "INFO"; 95 Vector lgs = new Vector (); 96 String filter = null; 97 for (Node child = n.getFirstChild();child != null;child = child.getNextSibling()) { 98 String generator = null; 99 if (child.getNodeName().equals("targets")) { 100 lvl = child.getFirstChild().getNodeValue(); 101 } 102 if (child.getNodeName().equals("generator-name")) { 103 generator = child.getFirstChild().getNodeValue(); 104 lgs.add(generator); 105 } 106 if (child.getNodeName().equals("filter-name")) { 107 filter = child.getFirstChild().getNodeValue(); 108 } 109 } 110 if (lgs.size() > 0 ) { 111 int targets = Target.parse(lvl); 112 if ( lvl == null ) { 113 ErrorHandler.reportError("No targets defined for logger:"+currentLogger+". Using ALL as targets"); 114 } 115 Logger myLogger = new Logger(currentLogger, targets,lc.getName()); 116 if ( filter != null ) { 117 myLogger.setFilter(lc.getLogFilter(filter)); 118 } 119 for ( int k = 0; k < lgs.size();k++) { 120 String lName = (String )lgs.get(k); 121 LogGenerator lg = lc.getLogGenerator(lName); 122 if (lg != null) { 123 myLogger.addLogGenerator(lg); 124 } 125 } 126 lc.addLogger(myLogger); 127 } else { 128 ErrorHandler.reportError("No generator-name found. Using default LogGenerator for "+ currentLogger); 129 LogGenerator lg = lc.getLogGenerator("Default"); 130 int targets = Target.parse(lvl); 131 Logger myLogger = new Logger(currentLogger, targets,lc.getName()); 132 myLogger.addLogGenerator(lg); 133 lc.addLogger(myLogger); 134 } 135 } 136 137 } 138 139 private void getVariables(Document doc, LogConfiguration lc) { 140 String varName; 141 String varValue; 142 NodeList nl = doc.getElementsByTagName("variable"); 143 for (int i = 0; i < nl.getLength(); i++) { 144 Node n = nl.item(i); 145 NamedNodeMap curAtt = n.getAttributes(); 146 Node curNode = curAtt.getNamedItem("name"); 147 varName = curNode.getNodeValue(); 148 Node valueNode = curAtt.getNamedItem("value"); 149 varValue = valueNode.getNodeValue(); 150 if (varName != null && varValue != null) { 151 variableManager.addVariable(varName, varValue, lc.getName()); 152 } 153 else { 154 if ( varName != null && varValue == null ) { 155 ErrorHandler.reportError("Variable definition found for:"+varName+" but no value attribute defined"); 156 } 157 else if ( varName == null && varValue != null ) { 158 ErrorHandler.reportError("Variable definition found but no name attribute"); 159 } 160 else if ( varName == null && varValue == null ) { 161 ErrorHandler.reportError("Variable tag found but no a name and a value attribute"); 162 } 163 } 164 } 165 } 166 167 private void getChannels(Document doc, LogConfiguration lc) { 168 String currentChannel; 169 NodeList nl = doc.getElementsByTagName("channel"); 170 for (int i = 0; i < nl.getLength(); i++) { 171 Node n = nl.item(i); 172 NamedNodeMap curAtt = n.getAttributes(); 173 Node curNode = curAtt.getNamedItem("name"); 174 currentChannel = curNode.getNodeValue(); 175 String generator = null; 176 String mode = "off"; 177 boolean running = false; 178 for (Node child = n.getFirstChild();child != null;child = child.getNextSibling()) { 179 if (child.getNodeName().equals("generator-name")) { 180 generator = child.getFirstChild().getNodeValue(); 181 } 182 if (child.getNodeName().equals("mode")) { 183 mode = child.getFirstChild().getNodeValue(); 184 } 185 } 186 mode = mode.toLowerCase(); 187 if (mode.equals("on")) { 188 running = true; 189 } 190 if (generator != null) { 191 LogGenerator lg = lc.getLogGenerator(generator); 192 if (lg != null) { 193 Channel myChannel = 194 new Channel(currentChannel, lg, running); 195 lc.addChannel(myChannel); 196 } 197 else { 198 ErrorHandler.reportError("Could not find LogGenerator for:"+currentChannel+". Using the default one"); 199 lg = lc.getLogGenerator("Default"); 200 Channel myChannel = new Channel(currentChannel, lg, running); 201 lc.addChannel(myChannel); 202 } 203 } 204 else { 205 ErrorHandler.reportError("LogManager getChannels: Using default LogGenerator for "+ currentChannel); 206 LogGenerator lg = lc.getLogGenerator("Default"); 207 Channel myChannel = new Channel(currentChannel, lg, running); 208 lc.addChannel(myChannel); 209 } 210 } 211 } 212 213 private void getPipes(Document doc, LogConfiguration lc) { 214 NodeList nl = doc.getElementsByTagName("pipe"); 215 for (int i = 0; i < nl.getLength(); i++) { 216 LogPipe pipe = null; 217 String generator = null; 218 String filter = null; 219 Node n = nl.item(i); 220 for (Node child = n.getFirstChild();child != null;child = child.getNextSibling()) { 221 if (child.getNodeName().equals("generator-name")) { 222 generator = child.getFirstChild().getNodeValue(); 223 } 224 if (child.getNodeName().equals("filter-name")) { 225 filter = child.getFirstChild().getNodeValue(); 226 } 227 } 228 if (generator != null) { 229 LogGenerator lg = lc.getLogGenerator(generator); 230 if (lg != null) { 231 pipe = new LogPipe(); 232 pipe.setFilter(lc.getLogFilter(filter)); 233 pipe.setGenerator(lg); 234 lc.addLogPipe(pipe); 235 } 236 } 237 else { 238 ErrorHandler.reportError("LogManager getPipes: Using default LogGenerator"); 239 LogGenerator lg = lc.getLogGenerator("Default"); 240 pipe = new LogPipe(); 241 pipe.setFilter(lc.getLogFilter(filter)); 242 pipe.setGenerator(lg); 243 lc.addLogPipe(pipe); 244 } 245 } 246 } 247 248 255 private void getFilters(Document doc, LogConfiguration lc) { 256 NodeList nl = doc.getElementsByTagName("filter"); 257 for (int i = 0; i < nl.getLength(); i++) { 258 String filterName = null; 259 String className = null; 260 Map params = new HashMap (); 261 Node n = nl.item(i); 262 NamedNodeMap curAtt = n.getAttributes(); 263 Node curNode = curAtt.getNamedItem("name"); 264 if ( curNode != null ) { 265 filterName = curNode.getNodeValue(); 266 for (Node child = n.getFirstChild();child != null;child = child.getNextSibling()) { 267 if (child.getNodeName().equals("class")) { 268 className = child.getFirstChild().getNodeValue(); 269 } 270 if (child.getNodeName().equals("parameter")) { 271 String pnName = null; 272 String pvName = null; 273 NamedNodeMap childAtt = child.getAttributes(); 274 Node pnNode = childAtt.getNamedItem("name"); 275 if ( pnNode != null) { 276 pnName = pnNode.getNodeValue(); 277 } 278 Node pvNode = childAtt.getNamedItem("value"); 279 if ( pvNode != null) { 280 pvName = pvNode.getNodeValue(); 281 } 282 if ( pnName != null && pvName != null ) { 283 params.put(pnName,pvName); 284 } 285 } 286 } 287 if ( className != null) { 288 try { 289 if ( className.indexOf(".") == -1 ) { 290 className = "org.jzonic.jlo.filter."+className; 291 } 292 Class clazz = Class.forName(className); 293 LogFilter filter = (LogFilter)clazz.newInstance(); 294 filter.setParameters(params); 295 lc.addLogFilter(filterName,filter); 296 } 297 catch (Exception e) { 298 ErrorHandler.reportError("LogManager getFilters: Cannot instantiate filter:"+filterName); 299 } 300 } 301 else { 302 ErrorHandler.reportError("LogManager getFilters: No class given for filter:"+filterName); 303 } 304 } 305 else { 306 ErrorHandler.reportError("No name defined for filter. Ignoring entry"); 307 } 308 } 309 } 310 311 312 private void getGenerators(Document doc, LogConfiguration lc) { 313 String currentGenerator = "default"; 314 NodeList nl = doc.getElementsByTagName("generator"); 315 for (int i = 0; i < nl.getLength(); i++) { 316 Node n = nl.item(i); 317 NamedNodeMap curAtt = n.getAttributes(); 318 Node curNode = curAtt.getNamedItem("name"); 319 if (curNode != null) { 320 currentGenerator = curNode.getNodeValue(); 321 } 322 Hashtable params = new Hashtable (); 323 Map fParams = new HashMap (); 324 Formatter formatter = null; 325 Handler handler = null; 326 String filter = null; 327 for (Node child = n.getFirstChild();child != null;child = child.getNextSibling()) { 328 if (child.getNodeName().equals("formatter")) { 329 formatter = getFormatter(child,lc.getName()); 330 } 331 if (child.getNodeName().equals("handler")) { 332 handler = getHandler(child,lc.getName()); 333 } 334 if (child.getNodeName().equals("filter-name")) { 335 filter = child.getFirstChild().getNodeValue(); 336 } 337 } 338 LogGenerator generator = createLogGenerator(currentGenerator, handler, formatter, params,lc.getName()); 339 if ( filter != null ) { 340 generator.setFilter(lc.getLogFilter(filter)); 341 } 342 lc.addLogGenerator(generator); 343 } 344 } 345 346 private Handler getHandler(Node n,String configName) { 347 NamedNodeMap myAtt = n.getAttributes(); 348 Node myNode = myAtt.getNamedItem("class"); 349 if ( myNode != null ) { 350 String handlerName = myNode.getNodeValue(); 351 if (handlerName != null) { 352 Handler handler = HandlerFactory.getHandler(handlerName,configName); 354 Map params = getParams(n); 355 handler.setParameter(params); 356 return handler; 357 } 358 } 359 else { 360 ErrorHandler.reportError("No class defined for the handler. Ignoring entry"); 361 } 362 return null; 363 } 364 365 private Formatter getFormatter(Node n,String configName) { 366 NamedNodeMap myAtt = n.getAttributes(); 367 Node myNode = myAtt.getNamedItem("class"); 368 if ( myNode != null ) { 369 String formatterName = myNode.getNodeValue(); 370 Formatter formatter = null; 371 if (formatterName != null) { 372 formatter = FormatterFactory.getFormatter(formatterName,configName); 374 Map params = getParams(n); 375 formatter.setParameter(params); 376 return formatter; 377 } 378 } 379 else { 380 ErrorHandler.reportError("No class defined for the formatter. Ignoring entry"); 381 } 382 return null; 383 } 384 385 private Map getParams(Node n) { 386 Map params = new HashMap (); 387 for (Node child = n.getFirstChild();child != null;child = child.getNextSibling()) { 388 if (child.getNodeName().equals("parameter")) { 389 String pnName = null; 390 String pvName = null; 391 NamedNodeMap childAtt = child.getAttributes(); 392 Node pnNode = childAtt.getNamedItem("name"); 393 if ( pnNode != null) { 394 pnName = pnNode.getNodeValue(); 395 } 396 Node pvNode = childAtt.getNamedItem("value"); 397 if ( pvNode != null) { 398 pvName = pvNode.getNodeValue(); 399 } 400 if ( pnName != null && pvName != null ) { 401 params.put(pnName,pvName); 402 } 403 } 404 } 405 return params; 406 } 407 408 public LogGenerator createLogGenerator(String name,String handlerName,String formatterName,Hashtable params,String configName) { 409 Handler handler = HandlerFactory.getHandler(handlerName,configName); 411 if (handler != null) 412 handler.setParameter(params); 413 Formatter formatter = null; 414 if (formatterName != null) { 415 formatter = FormatterFactory.getFormatter(formatterName,configName); 417 } 418 if (handler != null) { 419 return new LogGenerator(name, handler, formatter); 420 } 421 else { 422 return new LogGenerator(name, new ConsoleHandler(configName), new DefaultFormatter(configName)); 423 } 424 } 425 426 public LogGenerator createLogGenerator(String name,Handler handler,Formatter formatter,Hashtable params,String configName) { 427 if (handler != null) { 428 return new LogGenerator(name, handler, formatter); 429 } 430 else { 431 return new LogGenerator(name, new ConsoleHandler(configName), new DefaultFormatter(configName)); 432 } 433 } 434 } 435 | Popular Tags |