001: //LogalizerHandler.java
002: //-------------------------------------
003: //part of YACY
004: //(C) by Michael Peter Christen; mc@anomic.de
005: //first published on http://www.anomic.de
006: //Frankfurt, Germany, 2004
007: //
008: //This file ist contributed by Matthias Soehnholz
009: //last major change: $LastChangedDate: 2008-01-23 23:08:32 +0000 (Mi, 23 Jan 2008) $ by $LastChangedBy: orbiter $
010: //Revision: $LastChangedRevision: 4382 $
011: //
012: //This program is free software; you can redistribute it and/or modify
013: //it under the terms of the GNU General Public License as published by
014: //the Free Software Foundation; either version 2 of the License, or
015: //(at your option) any later version.
016: //
017: //This program is distributed in the hope that it will be useful,
018: //but WITHOUT ANY WARRANTY; without even the implied warranty of
019: //MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
020: //GNU General Public License for more details.
021: //
022: //You should have received a copy of the GNU General Public License
023: //along with this program; if not, write to the Free Software
024: //Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
025: //
026: //Using this software in any meaning (reading, learning, copying, compiling,
027: //running) means that you agree that the Author(s) is (are) not responsible
028: //for cost, loss of data or any harm that may be caused directly or indirectly
029: //by usage of this softare or this documentation. The usage of this software
030: //is on your own risk. The installation and usage (starting/running) of this
031: //software may allow other people or application to access your computer and
032: //any attached devices and is highly dependent on the configuration of the
033: //software which must be done by the user of the software; the author(s) is
034: //(are) also not responsible for proper configuration and usage of the
035: //software, even if provoked by documentation provided together with
036: //the software.
037: //
038: //Any changes to this file according to the GPL as documented in the file
039: //gpl.txt aside this file in the shipment you received can be done to the
040: //lines that follows this copyright notice here, but changes must not be
041: //done inside the copyright notive above. A re-distribution must contain
042: //the intact and unchanged copyright notice.
043: //Contributions and changes to the program code must be marked as such.
044:
045: package de.anomic.server.logging;
046:
047: import java.io.File;
048: import java.io.FilenameFilter;
049: import java.net.URI;
050: import java.net.URISyntaxException;
051: import java.util.HashMap;
052: import java.util.Hashtable;
053: import java.util.Iterator;
054: import java.util.Set;
055: import java.util.logging.Handler;
056: import java.util.logging.LogManager;
057: import java.util.logging.LogRecord;
058:
059: import de.anomic.plasma.plasmaParser;
060: import de.anomic.server.logging.logParsers.LogParser;
061:
062: public class LogalizerHandler extends Handler {
063:
064: public static boolean enabled = false;
065: public static boolean debug = false;
066: private String logParserPackage;
067: private HashMap<String, Object> parsers;
068:
069: public LogalizerHandler() {
070: super ();
071: configure();
072: }
073:
074: private HashMap<String, Object> loadParsers() {
075: HashMap<String, Object> parsers = new HashMap<String, Object>();
076: try {
077: if (debug)
078: System.out
079: .println("Searching for additional content parsers in package "
080: + logParserPackage);
081: // getting an uri to the parser subpackage
082: String packageURI = plasmaParser.class.getResource(
083: "/" + logParserPackage.replace('.', '/'))
084: .toString();
085: if (debug)
086: System.out.println("LogParser directory is "
087: + packageURI);
088:
089: File parserDir = new File(new URI(packageURI));
090: //System.out.println(parserDir.toString());
091: String[] parserDirFiles = parserDir.list(parserNameFilter);
092: if (parserDirFiles == null && debug) {
093: System.out.println("Can't find any parsers in "
094: + parserDir.getAbsolutePath());
095: }
096: //System.out.println(parserDirFiles.length);
097: for (int i = 0; i < parserDirFiles.length; i++) {
098: String tmp = parserDirFiles[i].substring(0,
099: parserDirFiles[i].indexOf(".class"));
100: Class<?> tempClass = Class.forName(logParserPackage
101: + "." + tmp);
102: if (tempClass.isInterface()) {
103: if (debug)
104: System.out.println(tempClass.getName()
105: + " is an Interface");
106: } else {
107: Object theParser = tempClass.newInstance();
108: if (theParser instanceof LogParser) {
109: LogParser theLogParser = (LogParser) theParser;
110: //System.out.println(bla.getName() + " is a logParser");
111: parsers.put(theLogParser.getParserType(),
112: theParser);
113:
114: if (debug)
115: System.out.println("Added "
116: + theLogParser.getClass().getName()
117: + " as "
118: + theLogParser.getParserType()
119: + " Parser.");
120: } else {
121: //System.out.println(bla.getName() + " is not a logParser");
122: if (debug)
123: System.out
124: .println("Rejected "
125: + tempClass.getName()
126: + ". Class does not implement the logParser-Interface");
127:
128: }
129: }
130: }
131: } catch (ClassNotFoundException e) {
132: e.printStackTrace();
133: } catch (InstantiationException e) {
134: e.printStackTrace();
135: } catch (IllegalAccessException e) {
136: e.printStackTrace();
137: } catch (URISyntaxException e) {
138: e.printStackTrace();
139: }
140: return parsers;
141: }
142:
143: /**
144: * Get any configuration properties set
145: */
146: private void configure() {
147: LogManager manager = LogManager.getLogManager();
148: String className = getClass().getName();
149:
150: if (manager.getProperty(className + ".enabled")
151: .equalsIgnoreCase("true"))
152: enabled = true;
153: if (manager.getProperty(className + ".debug").equalsIgnoreCase(
154: "true"))
155: debug = true;
156:
157: logParserPackage = manager.getProperty(className
158: + ".parserPackage");
159:
160: parsers = loadParsers();
161: }
162:
163: public void publish(LogRecord record) {
164: if (enabled) {
165: LogParser temp = (LogParser) parsers.get(record
166: .getLoggerName());
167: if (temp != null) {
168: int returnV = temp.parse(record.getLevel().toString(),
169: record.getMessage());
170: //if (debug) System.out.println("Logalizertest: " + returnV + " --- " + record.getLevel() + " --- " + record.getMessage());
171: if (debug)
172: System.out.println("Logalizertest: " + returnV
173: + " --- " + record.getLevel());
174: }
175: }
176: }
177:
178: public Set<String> getParserNames() {
179: return parsers.keySet();
180: }
181:
182: public LogParser getParser(int number) {
183: String o;
184: Iterator<String> it = parsers.keySet().iterator();
185: int i = 0;
186: while (it.hasNext()) {
187: o = it.next();
188: if (i++ == number)
189: return (LogParser) parsers.get(o);
190: }
191: return null;
192: }
193:
194: public Hashtable<String, Object> getParserResults(
195: LogParser parsername) {
196: return parsername.getResults();
197: }
198:
199: public void close() throws SecurityException {
200: // TODO Auto-generated method stub
201:
202: }
203:
204: public void flush() {
205: // TODO Auto-generated method stub
206:
207: }
208:
209: private static final FilenameFilter parserNameFilter = new FilenameFilter() {
210: public boolean accept(File dir, String name) {
211: return name.matches(".*.class");
212: }
213: };
214: }
|