ModuleEventLog.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.io.PrintStream;
019 import java.util.ArrayList;
020 import java.util.List;
021 
022 import org.qedeq.base.trace.Trace;
023 import org.qedeq.kernel.bo.common.QedeqBo;
024 
025 
026 /**
027  * This class organizes the logging of module events.
028  *
029  * TODO mime 20080317: must this be a singleton?
030  *
031  @author  Michael Meyling
032  */
033 public final class ModuleEventLog implements ModuleEventListener {
034 
035     /** This class. */
036     private static final Class CLASS = ModuleEventLog.class;
037 
038     /** The one and only instance. */
039     private static ModuleEventLog instance = new ModuleEventLog();
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 ModuleEventLog getInstance() {
051         return instance;
052     }
053 
054     /**
055      * Don't use me outside of this class.
056      */
057     private ModuleEventLog() {
058     }
059 
060     /**
061      * Add listener.
062      *
063      @param   log Add this listener.
064      */
065     public final void addLog(final ModuleEventListener log) {
066         loggers.add(log);
067     }
068 
069     /**
070      * Remove listener.
071      *
072      @param   log Remove this listener.
073      */
074     public final void removeLog(final ModuleEventListener log) {
075         loggers.remove(log);
076     }
077 
078     /**
079      * Add stream listener.
080      *
081      @param   out Put messages into this stream.
082      */
083     public final void addLog(final PrintStream out) {
084         final ModuleEventListener log = new DefaultModuleEventListener(out);
085         loggers.add(log);
086     }
087 
088     public void addModule(final QedeqBo prop) {
089         for (int i = 0; i < loggers.size(); i++) {
090             try {   // we don't know if the ModuleEventListener is free of programming errors...
091                 ((ModuleEventListenerloggers.get(i)).addModule(prop);
092             catch (RuntimeException e) {
093                 Trace.fatal(CLASS, this, "addModule",
094                     "ModuleEventListener throwed RuntimeException", e);
095             }
096         }
097     }
098 
099     public void stateChanged(final QedeqBo prop) {
100         for (int i = 0; i < loggers.size(); i++) {
101             try {   // we don't know if the ModuleEventListener is free of programming errors...
102                 ((ModuleEventListenerloggers.get(i)).stateChanged(prop);
103             catch (RuntimeException e) {
104                 Trace.fatal(CLASS, this, "stateChanged",
105                     "ModuleEventListener throwed RuntimeException", e);
106             }
107         }
108     }
109 
110     public void removeModule(final QedeqBo prop) {
111         for (int i = 0; i < loggers.size(); i++) {
112             try {   // we don't know if the ModuleEventListener is free of programming errors...
113                 ((ModuleEventListenerloggers.get(i)).removeModule(prop);
114             catch (RuntimeException e) {
115                 Trace.fatal(CLASS, this, "removeModule",
116                     "ModuleEventListener throwed RuntimeException", e);
117             }
118         }
119     }
120 
121 
122 }