001: /* Copyright (C) 2003 Finalist IT Group
002: *
003: * This file is part of JAG - the Java J2EE Application Generator
004: *
005: * JAG is free software; you can redistribute it and/or modify
006: * it under the terms of the GNU General Public License as published by
007: * the Free Software Foundation; either version 2 of the License, or
008: * (at your option) any later version.
009: * JAG is distributed in the hope that it will be useful,
010: * but WITHOUT ANY WARRANTY; without even the implied warranty of
011: * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
012: * GNU General Public License for more details.
013: * You should have received a copy of the GNU General Public License
014: * along with JAG; if not, write to the Free Software
015: * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
016: */
017:
018: package com.finalist.util.log;
019:
020: import org.xml.sax.InputSource;
021: import org.w3c.dom.Document;
022: import org.w3c.dom.Element;
023: import org.w3c.dom.NodeList;
024: import org.w3c.dom.NamedNodeMap;
025: import org.w3c.dom.Node;
026:
027: // jaxp 1.0.1 imports
028: import javax.xml.parsers.DocumentBuilderFactory;
029: import javax.xml.parsers.DocumentBuilder;
030:
031: import java.net.URL;
032: import java.util.HashMap;
033:
034: import com.finalist.util.PropertyReader;
035:
036: /**
037: * This class is an helper class for retrieve log settings from an xml file
038: * @author Ronald Kramp - Finalist IT Group
039: * @version $Revision: 1.1 $, $Date: 2004/11/12 14:06:44 $
040: */
041: public class XMLLogPropertyReader {
042:
043: /** name for the appender tag in the xml file */
044: public static final String APPENDER = "appender";
045:
046: /** name for the name attribute of the appender in the xml file */
047: public static final String NAME = "name";
048:
049: /** name for the logfile attribute of the appender in the xml file */
050: public static final String LOG_FILE = "logfile";
051:
052: /** name for the append attribute of the appender in the xml file */
053: public static final String APPEND = "append";
054:
055: /** name for the maxbackupindex attribute of the appender in the xml file */
056: public static final String MAX_BACKUP_INDEX = "maxbackupindex";
057:
058: /** name for the maxfilesize attribute of the appender in the xml file */
059: public static final String MAX_FILE_SIZE = "maxfilesize";
060:
061: /** name for the shownumberoflastpackages attribute of the appender in the xml file */
062: public static final String SHOW_NUMBER_OF_LAST_PACKAGES = "shownumberoflastpackages";
063:
064: /** name for the datepattern attribute of the appender in the xml file */
065: public static final String DATE_PATTERN = "datepattern";
066:
067: /** name for the messageseparator attribute of the appender in the xml file */
068: public static final String MESSAGE_SEPARATOR = "messageseparator";
069:
070: /** name for the loglevel attribute of the appender in the xml file */
071: public static final String LOG_LEVEL = "loglevel";
072:
073: /**
074: * Retrieving all appender setting from a configuration file.
075: * @param location the directory + filename where to find the log configuration xml file
076: * @return HashMap all log configurations
077: */
078: public static HashMap getLogConfigs(String location) {
079: HashMap logConfigs = new HashMap();
080: try {
081: //URL url = PropertyReader.getPropertiesURL(location);
082: URL url = PropertyReader.getPropertiesURL(location);
083: InputSource xmlInp = new InputSource(url.openStream());
084: Document doc = DocumentBuilderFactory.newInstance()
085: .newDocumentBuilder().parse(xmlInp);
086: NodeList appenders = doc.getElementsByTagName(APPENDER);
087: for (int i = 0; i < appenders.getLength(); i++) {
088: Node curNode = appenders.item(i);
089: if (curNode.getNodeName().equals(APPENDER)) {
090: NamedNodeMap nnm = curNode.getAttributes();
091: String appenderName = nnm.getNamedItem(NAME)
092: .getNodeValue();
093: String logFile = nnm.getNamedItem(LOG_FILE)
094: .getNodeValue();
095: String append = nnm.getNamedItem(APPEND)
096: .getNodeValue();
097:
098: String maxBackupIndexString = nnm.getNamedItem(
099: MAX_BACKUP_INDEX).getNodeValue();
100: int maxBackupIndex = -1;
101: try {
102: maxBackupIndex = Integer
103: .parseInt(maxBackupIndexString);
104: } catch (Exception e) {
105: System.out
106: .println("Incorrect number for maxBackupIndex with appender: "
107: + appenderName + ": " + e);
108: }
109:
110: String maxFileSizeString = nnm.getNamedItem(
111: MAX_FILE_SIZE).getNodeValue();
112: int maxFileSize = -1;
113: try {
114: maxFileSize = Integer
115: .parseInt(maxFileSizeString);
116: } catch (Exception e) {
117: System.out
118: .println("Incorrect number for maxfilesize with appender: "
119: + appenderName + ": " + e);
120: }
121:
122: String showNumberOfLastPackagesString = nnm
123: .getNamedItem(SHOW_NUMBER_OF_LAST_PACKAGES)
124: .getNodeValue();
125: int showNumberOfLastPackages = -1;
126: try {
127: showNumberOfLastPackages = Integer
128: .parseInt(showNumberOfLastPackagesString);
129: } catch (Exception e) {
130: System.out
131: .println("Incorrect number for shownumberoflastpackages with appender: "
132: + appenderName + ": " + e);
133: }
134:
135: String datePattern = nnm.getNamedItem(DATE_PATTERN)
136: .getNodeValue();
137: String messageSeparator = nnm.getNamedItem(
138: MESSAGE_SEPARATOR).getNodeValue();
139: String logLevel = nnm.getNamedItem(LOG_LEVEL)
140: .getNodeValue();
141: LogConfig lg = new LogConfig(logFile, append
142: .equals("true"), maxBackupIndex,
143: maxFileSize, showNumberOfLastPackages,
144: datePattern, messageSeparator, logLevel);
145: System.out.println("Logging for filter ["
146: + appenderName + "] with setting\n");
147: System.out.println(lg);
148: logConfigs.put(appenderName, lg);
149: }
150: }
151: } catch (Exception e) {
152: System.out.println("Exception: " + e);
153: }
154: return logConfigs;
155: }
156: }
|