QedeqLog.java
001 /* This file is part of the project "Hilbert II" - http://www.qedeq.org
002  *
003  * Copyright 2000-2013,  Michael Meyling <mime@qedeq.org>.
004  *
005  * "Hilbert II" is free software; you can redistribute
006  * it and/or modify it under the terms of the GNU General Public
007  * License as published by the Free Software Foundation; either
008  * version 2 of the License, or (at your option) any later version.
009  *
010  * This program is distributed in the hope that it will be useful,
011  * but WITHOUT ANY WARRANTY; without even the implied warranty of
012  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
013  * GNU General Public License for more details.
014  */
015 
016 package org.qedeq.kernel.bo.log;
017 
018 import java.util.ArrayList;
019 import java.util.List;
020 
021 import org.qedeq.base.trace.Trace;
022 
023 
024 /**
025  * This class organizes the logging.
026  *
027  * TODO 20110606 m31: this class is a singleton but it would be better if it is not
028  *                    to accomplish this we must put a getLogInstance method in all
029  *                    important BO classes.
030  *
031  @author  Michael Meyling
032  */
033 public final class QedeqLog implements LogListener {
034 
035     /** This class. */
036     private static final Class CLASS = QedeqLog.class;
037 
038     /** The one and only instance. */
039     private static QedeqLog instance = new QedeqLog();
040 
041     /** The loggers. */
042     private List loggers = new ArrayList();
043 
044 
045     /**
046      * Get instance of Logger.
047      *
048      @return  singleton
049      */
050     public static final QedeqLog getInstance() {
051         return instance;
052     }
053 
054 
055     /**
056      * Don't use me outside of this class.
057      */
058     private QedeqLog() {
059         // nothing to do
060     }
061 
062     /**
063      * Add listener.
064      *
065      @param   log Add this listener.
066      */
067     public synchronized void addLog(final LogListener log) {
068         if (log == null) {
069             return;
070         }
071         loggers.add(log);
072         Trace.paramInfo(CLASS, this, "addLog(LogListener)""log", log.getClass());
073     }
074 
075     /**
076      * Remove listener.
077      *
078      @param   log Remove this listener.
079      */
080     public synchronized void removeLog(final LogListener log) {
081         loggers.remove(log);
082         if (log != null) {
083             Trace.paramInfo(CLASS, this, "removeLog(LogListener)""log", log.getClass());
084         }
085     }
086 
087     public synchronized void logMessageState(final String text, final String url) {
088         for (int i = 0; i < loggers.size(); i++) {
089             try {   // we don't know if the LogListener is free of programming errors...
090                 ((LogListenerloggers.get(i)).logMessageState(text, url);
091             catch (RuntimeException e) {
092                 Trace.fatal(CLASS, this, "logMessageState""LogListener throwed RuntimeException",
093                     e);
094             }
095         }
096     }
097 
098     public synchronized void logFailureState(final String text, final String url, final String description) {
099         for (int i = 0; i < loggers.size(); i++) {
100             try {   // we don't know if the LogListener is free of programming errors...
101                 ((LogListenerloggers.get(i)).logFailureState(text, url, description);
102             catch (RuntimeException e) {
103                 Trace.fatal(CLASS, this, "logFailureState""LogListener throwed RuntimeException",
104                     e);
105             }
106         }
107     }
108 
109     public synchronized void logSuccessfulState(final String text, final String url) {
110         for (int i = 0; i < loggers.size(); i++) {
111             try {   // we don't know if the LogListener is free of programming errors...
112                 ((LogListenerloggers.get(i)).logSuccessfulState(text, url);
113             catch (RuntimeException e) {
114                 Trace.fatal(CLASS, this, "logSuccessfulState",
115                     "LogListener throwed RuntimeException", e);
116             }
117         }
118     }
119 
120     public synchronized void logRequest(final String text, final String url) {
121         for (int i = 0; i < loggers.size(); i++) {
122             try {   // we don't know if the LogListener is free of programming errors...
123                 ((LogListenerloggers.get(i)).logRequest(text, url);
124             catch (RuntimeException e) {
125                 Trace.fatal(CLASS, this, "logRequest""LogListener throwed RuntimeException", e);
126             }
127         }
128     }
129 
130     public synchronized void logSuccessfulReply(final String text, final String url) {
131         for (int i = 0; i < loggers.size(); i++) {
132             try {   // we don't know if the LogListener is free of programming errors...
133                 ((LogListenerloggers.get(i)).logSuccessfulReply(text, url);
134             catch (RuntimeException e) {
135                 Trace.fatal(CLASS, this, "logSuccessfulReply""LogListener throwed RuntimeException",
136                     e);
137             }
138         }
139     }
140 
141     public synchronized void logFailureReply(final String text, final String url, final String description) {
142         for (int i = 0; i < loggers.size(); i++) {
143             try {   // we don't know if the LogListener is free of programming errors...
144                 ((LogListenerloggers.get(i)).logFailureReply(text, url, description);
145             catch (RuntimeException e) {
146                 Trace.fatal(CLASS, this, "logFailureReply""LogListener throwed RuntimeException",
147                     e);
148             }
149         }
150     }
151 
152     public synchronized void logMessage(final String text) {
153         for (int i = 0; i < loggers.size(); i++) {
154             try {   // we don't know if the LogListener is free of programming errors...
155                 ((LogListenerloggers.get(i)).logMessage(text);
156             catch (RuntimeException e) {
157                 Trace.fatal(CLASS, this, "logMessage""LogListener throwed RuntimeException", e);
158             }
159         }
160     }
161 
162 
163 }