001: /*
002: * $Header: /export/home/cvsroot/MyPersonalizerRepository/MyPersonalizer/Subsystems/Kernel/Sources/es/udc/mypersonalizer/kernel/log/LogManager.java,v 1.1.1.1 2004/03/25 12:08:38 fbellas Exp $
003: * $Revision: 1.1.1.1 $
004: * $Date: 2004/03/25 12:08:38 $
005: *
006: * =============================================================================
007: *
008: * Copyright (c) 2003, The MyPersonalizer Development Group
009: * (http://www.tic.udc.es/~fbellas/mypersonalizer/index.html) at
010: * University Of A Coruna
011: * All rights reserved.
012: *
013: * Redistribution and use in source and binary forms, with or without
014: * modification, are permitted provided that the following conditions are met:
015: *
016: * - Redistributions of source code must retain the above copyright notice,
017: * this list of conditions and the following disclaimer.
018: *
019: * - Redistributions in binary form must reproduce the above copyright notice,
020: * this list of conditions and the following disclaimer in the documentation
021: * and/or other materials provided with the distribution.
022: *
023: * - Neither the name of the University Of A Coruna nor the names of its
024: * contributors may be used to endorse or promote products derived from
025: * this software without specific prior written permission.
026: *
027: * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
028: * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
029: * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
030: * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE
031: * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
032: * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
033: * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
034: * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
035: * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
036: * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
037: * POSSIBILITY OF SUCH DAMAGE.
038: *
039: */
040:
041: package es.udc.mypersonalizer.kernel.log;
042:
043: import es.udc.mypersonalizer.kernel.config.KernelConfig;
044: import es.udc.mypersonalizer.kernel.config.KernelConfigManager;
045: import es.udc.mypersonalizer.kernel.config.LogConfig;
046: import es.udc.mypersonalizer.kernel.config.LogManagerConfig;
047: import java.io.File;
048: import java.io.IOException;
049: import java.util.*;
050:
051: /**
052: * This class provides logs. Concrete log classes must <b>never</b> be
053: * instantiated and must <b>always</b> be requested from this manager class.
054: * Synchronization problems might occur if this rule is not respected.
055: * <p>
056: * If this class is requested an unexistent log or a log that was unable to
057: * start, a warning will be displayed on the console and the messages will be
058: * redirected to the console.
059: *
060: * All the configuration data for this class is taken from a {@link
061: * LogManagerConfig} object.
062: *
063: * @author Fernando Bellas
064: * @author Daniel Fernandez
065: * @since 1.0
066: */
067: public final class LogManager {
068:
069: /** The console log for unavailable logs. */
070: private static ConsoleLog consoleLog = new ConsoleLog();
071:
072: /** The available logs. */
073: private static Map logs = null;
074:
075: static {
076:
077: try {
078: KernelConfig kernelConfig = KernelConfigManager.getConfig();
079:
080: LogManagerConfig logManagerConfig = kernelConfig
081: .getKernelModelConfig().getLogManagerConfig();
082:
083: List logConfigs = logManagerConfig.getLogConfigs();
084:
085: logs = new HashMap();
086:
087: Iterator logConfigsIter = logConfigs.iterator();
088: while (logConfigsIter.hasNext()) {
089: LogConfig logConfig = (LogConfig) logConfigsIter.next();
090:
091: String logName = logConfig.getName();
092: String logFileStr = logConfig.getLogFile();
093: String maxLogSizeBytesStr = logConfig
094: .getMaxLogSizeBytes();
095: String maxBackupFilesStr = logConfig
096: .getMaxBackupFiles();
097: String displayOnConsoleStr = logConfig
098: .getDisplayOnConsole();
099:
100: File logFile = new File(logFileStr.trim());
101: Long maxLogSizeBytes = new Long(maxLogSizeBytesStr
102: .trim());
103: Integer maxBackupFiles = new Integer(maxBackupFilesStr
104: .trim());
105: Boolean displayOnConsole = new Boolean(
106: displayOnConsoleStr.trim());
107:
108: try {
109: addLog(logName, logFile, maxLogSizeBytes,
110: maxBackupFiles, displayOnConsole);
111: } catch (Exception e) {
112: addConsoleLog(logName, logFile, e);
113: }
114:
115: }
116:
117: } catch (Exception e) {
118:
119: e.printStackTrace();
120:
121: }
122:
123: }
124:
125: /**
126: * Disallows creating instances of this utility class.
127: */
128: private LogManager() {
129: }
130:
131: /**
132: * Returns the log requested.
133: *
134: * @param logName the name of the requested log
135: * @return the log requested or a console log if the log does not exist
136: */
137: public static Log getLog(String logName) {
138: Log log = (Log) logs.get(logName);
139: if (log == null) {
140: System.err.println("WARNING! the log named \"" + logName
141: + "\" does not exist.");
142: return consoleLog;
143: }
144:
145: return log;
146: }
147:
148: /**
149: * Adds a new log to the available logs.
150: *
151: * @param logName the name of the log
152: * @param file the log file
153: * @param maxLogSize the maximum log size
154: * @param maxBackupFiles the maximum number of backup files
155: * @param displayOnConsole <code>Boolean.TRUE</code> if messages are
156: * to be displayed on the console
157: * @throws IOException if the log is not available
158: */
159: private static void addLog(String logName, File file,
160: Long maxLogSize, Integer maxBackupFiles,
161: Boolean displayOnConsole) throws IOException {
162:
163: /*
164: * Checks if the log's file is already being used.
165: */
166: PlainTextLog existingLog = null;
167: String existingLogName = null;
168: boolean foundExisting = false;
169: Iterator logsIterator = logs.keySet().iterator();
170: while (logsIterator.hasNext() && !foundExisting) {
171: existingLogName = (String) logsIterator.next();
172:
173: /*
174: * There are two types of logs: ConsoleLog and PlainTextLog.
175: * Only plain text logs could fail.
176: */
177: if (existingLog instanceof PlainTextLog) {
178: existingLog = (PlainTextLog) logs.get(existingLogName);
179: if (file.equals(existingLog.getFile())) {
180: foundExisting = true;
181: }
182: }
183: }
184:
185: /*
186: * There was a previous log for the same file name. Synchronization
187: * will be done on the same log instance. No data will be lost.
188: */
189: if (foundExisting) {
190: logs.put(logName, existingLog);
191:
192: System.err.println("WARNING! log named \"" + logName
193: + "\" and log named \"" + existingLogName
194: + "\" will use the same log file.");
195: System.err.println("The configuration for: \"" + logName
196: + "\" is ignored.");
197:
198: } else {
199: logs.put(logName, new PlainTextLog(file, maxLogSize
200: .longValue(), maxBackupFiles.intValue(),
201: displayOnConsole.booleanValue()));
202: }
203: }
204:
205: /**
206: * Adds a console log for unavailable file logs.
207: *
208: * @param logName the log name
209: * @param file the log file
210: * @param exception the occured exception
211: */
212: private static void addConsoleLog(String logName, File file,
213: Exception exception) {
214:
215: System.err.println("ERROR: unable to start log for file: "
216: + file);
217: System.err
218: .println("Messages will be redirected to the console.");
219: exception.printStackTrace();
220: logs.put(logName, consoleLog);
221: }
222:
223: }
|