View Javadoc

1   /* This file is part of the project "Hilbert II" - http://www.qedeq.org" target="alexandria_uri">http://www.qedeq.org
2    *
3    * Copyright 2000-2014,  Michael Meyling <mime@qedeq.org>.
4    *
5    * "Hilbert II" is free software; you can redistribute
6    * it and/or modify it under the terms of the GNU General Public
7    * License as published by the Free Software Foundation; either
8    * version 2 of the License, or (at your option) any later version.
9    *
10   * This program is distributed in the hope that it will be useful,
11   * but WITHOUT ANY WARRANTY; without even the implied warranty of
12   * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13   * GNU General Public License for more details.
14   */
15  
16  package org.qedeq.kernel.bo.log;
17  
18  import java.util.ArrayList;
19  import java.util.List;
20  
21  import org.qedeq.base.trace.Trace;
22  
23  
24  /**
25   * This class organizes the logging.
26   *
27   * TODO 20110606 m31: this class is a singleton but it would be better if it is not
28   *                    to accomplish this we must put a getLogInstance method in all
29   *                    important BO classes.
30   *
31   * @author  Michael Meyling
32   */
33  public final class QedeqLog implements LogListener {
34  
35      /** This class. */
36      private static final Class CLASS = QedeqLog.class;
37  
38      /** The one and only instance. */
39      private static QedeqLog instance = new QedeqLog();
40  
41      /** The loggers. */
42      private List loggers = new ArrayList();
43  
44  
45      /**
46       * Get instance of Logger.
47       *
48       * @return  singleton
49       */
50      public static final QedeqLog getInstance() {
51          return instance;
52      }
53  
54  
55      /**
56       * Don't use me outside of this class.
57       */
58      private QedeqLog() {
59          // nothing to do
60      }
61  
62      /**
63       * Add listener.
64       *
65       * @param   log Add this listener.
66       */
67      public synchronized void addLog(final LogListener log) {
68          if (log == null) {
69              return;
70          }
71          loggers.add(log);
72          Trace.paramInfo(CLASS, this, "addLog(LogListener)", "log", log.getClass());
73      }
74  
75      /**
76       * Remove listener.
77       *
78       * @param   log Remove this listener.
79       */
80      public synchronized void removeLog(final LogListener log) {
81          loggers.remove(log);
82          if (log != null) {
83              Trace.paramInfo(CLASS, this, "removeLog(LogListener)", "log", log.getClass());
84          }
85      }
86  
87      public synchronized void logMessageState(final String text, final String url) {
88          for (int i = 0; i < loggers.size(); i++) {
89              try {   // we don't know if the LogListener is free of programming errors...
90                  ((LogListener) loggers.get(i)).logMessageState(text, url);
91              } catch (RuntimeException e) {
92                  Trace.fatal(CLASS, this, "logMessageState", "LogListener throwed RuntimeException",
93                      e);
94              }
95          }
96      }
97  
98      public synchronized void logFailureState(final String text, final String url, final String description) {
99          for (int i = 0; i < loggers.size(); i++) {
100             try {   // we don't know if the LogListener is free of programming errors...
101                 ((LogListener) loggers.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                 ((LogListener) loggers.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                 ((LogListener) loggers.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                 ((LogListener) loggers.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                 ((LogListener) loggers.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                 ((LogListener) loggers.get(i)).logMessage(text);
156             } catch (RuntimeException e) {
157                 Trace.fatal(CLASS, this, "logMessage", "LogListener throwed RuntimeException", e);
158             }
159         }
160     }
161 
162 
163 }