some refactoring and more error-awareness in LogalizeHandler

git-svn-id: https://svn.berlios.de/svnroot/repos/yacy/trunk@6102 6c8d7289-2bf4-0310-a012-ef5d649a1542
pull/1/head
orbiter 16 years ago
parent 2bb020a7a5
commit 041d9c253e

@ -52,11 +52,16 @@ public class LogalizerHandler extends Handler {
public LogalizerHandler() { public LogalizerHandler() {
super(); super();
configure();
} final LogManager manager = LogManager.getLogManager();
String className = getClass().getName();
if(manager.getProperty(className + ".enabled").equalsIgnoreCase("true")) enabled = true;
if(manager.getProperty(className + ".debug").equalsIgnoreCase("true")) debug = true;
private HashMap<String, Object> loadParsers() { logParserPackage = manager.getProperty(className + ".parserPackage");
final HashMap<String, Object> logParsers = new HashMap<String, Object>();
parsers = new HashMap<String, Object>();
try { try {
if (debug) System.out.println("Searching for additional content parsers in package " + logParserPackage); if (debug) System.out.println("Searching for additional content parsers in package " + logParserPackage);
// getting an uri to the parser subpackage // getting an uri to the parser subpackage
@ -70,66 +75,65 @@ public class LogalizerHandler extends Handler {
} }
for (final String filename: parserDirFiles) { for (final String filename: parserDirFiles) {
if (filename.endsWith("Log.class") || filename.endsWith("LogalizerHandler.class")) continue; if (filename.endsWith("Log.class") || filename.endsWith("LogalizerHandler.class")) continue;
final Pattern patternGetClassName = Pattern.compile(".*\\"+ File.separator +"([^\\"+ File.separator +"]+)\\.class"); System.out.println("************ logparser=" + filename);
final Matcher matcherClassName = patternGetClassName.matcher(filename); registerParser(filename);
matcherClassName.find(); }
final String className = matcherClassName.group(1); } catch (final IOException e) {
final Class<?> tempClass = Class.forName(logParserPackage+"."+className); e.printStackTrace();
if (tempClass.isInterface()) { } catch (final URISyntaxException e) {
if (debug) System.out.println(tempClass.getName() + " is an Interface"); e.printStackTrace();
} else { }
final Object theParser = tempClass.newInstance(); }
if (theParser instanceof LogParser) {
final LogParser theLogParser = (LogParser) theParser; private void registerParser(String filename) {
//System.out.println(bla.getName() + " is a logParser"); try {
logParsers.put(theLogParser.getParserType(), theParser); final Pattern patternGetClassName = Pattern.compile(".*\\"+ File.separator +"([^\\"+ File.separator +"]+)\\.class");
final Matcher matcherClassName = patternGetClassName.matcher(filename);
if (debug) System.out.println("Added " + theLogParser.getClass().getName() + " as " + theLogParser.getParserType() + " Parser."); matcherClassName.find();
} String className = matcherClassName.group(1);
else { final Class<?> tempClass = Class.forName(logParserPackage+"."+className);
//System.out.println(bla.getName() + " is not a logParser"); if (tempClass.isInterface()) {
if (debug) System.out.println("Rejected " + tempClass.getName() + ". Class does not implement the logParser-Interface"); if (debug) System.out.println(tempClass.getName() + " is an Interface");
} else {
final Object theParser = tempClass.newInstance();
if (theParser instanceof LogParser) {
final LogParser theLogParser = (LogParser) theParser;
//System.out.println(bla.getName() + " is a logParser");
parsers.put(theLogParser.getParserType(), theParser);
if (debug) System.out.println("Added " + theLogParser.getClass().getName() + " as " + theLogParser.getParserType() + " Parser.");
}
else {
//System.out.println(bla.getName() + " is not a logParser");
if (debug) System.out.println("Rejected " + tempClass.getName() + ". Class does not implement the logParser-Interface");
}
} }
} }
} catch (final ClassNotFoundException e) { } catch (final ClassNotFoundException e) {
e.printStackTrace(); e.printStackTrace();
} catch (final InstantiationException e) { } catch (final InstantiationException e) {
e.printStackTrace(); e.printStackTrace();
} catch (final IllegalAccessException e) { } catch (final IllegalAccessException e) {
e.printStackTrace(); e.printStackTrace();
} catch (final IOException e) {
e.printStackTrace();
} catch (final URISyntaxException e) {
e.printStackTrace();
} }
return logParsers;
} }
/** public void registerParser(LogParser theParser) {
* Get any configuration properties set final LogParser theLogParser = (LogParser) theParser;
*/ //System.out.println(bla.getName() + " is a logParser");
private void configure() { parsers.put(theLogParser.getParserType(), theParser);
final LogManager manager = LogManager.getLogManager();
final String className = getClass().getName(); if (debug) System.out.println("Added " + theLogParser.getClass().getName() + " as " + theLogParser.getParserType() + " Parser.");
if(manager.getProperty(className + ".enabled").equalsIgnoreCase("true")) enabled = true;
if(manager.getProperty(className + ".debug").equalsIgnoreCase("true")) debug = true;
logParserPackage = manager.getProperty(className + ".parserPackage");
parsers = loadParsers();
} }
public void publish(final LogRecord record) { public void publish(final LogRecord record) {
if (enabled) { if (enabled) {
final LogParser temp = (LogParser) parsers.get(record.getLoggerName()); final LogParser temp = (LogParser) parsers.get(record.getLoggerName());
if (temp != null) { if (temp != null) try {
final int returnV = temp.parse(record.getLevel().toString(), record.getMessage()); final int returnV = temp.parse(record.getLevel().toString(), record.getMessage());
//if (debug) System.out.println("Logalizertest: " + returnV + " --- " + record.getLevel() + " --- " + record.getMessage()); //if (debug) System.out.println("Logalizertest: " + returnV + " --- " + record.getLevel() + " --- " + record.getMessage());
if (debug) System.out.println("Logalizertest: " + returnV + " --- " + record.getLevel()); if (debug) System.out.println("Logalizertest: " + returnV + " --- " + record.getLevel());
} } catch (Exception e) {}
} }
flush(); flush();
} }

Loading…
Cancel
Save