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         // nothing to do
059     }
060 
061     /**
062      * Add listener.
063      *
064      @param   log Add this listener.
065      */
066     public final void addLog(final ModuleEventListener log) {
067         loggers.add(log);
068     }
069 
070     /**
071      * Remove listener.
072      *
073      @param   log Remove this listener.
074      */
075     public final void removeLog(final ModuleEventListener log) {
076         loggers.remove(log);
077     }
078 
079     /**
080      * Add stream listener.
081      *
082      @param   out Put messages into this stream.
083      */
084     public final void addLog(final PrintStream out) {
085         final ModuleEventListener log = new DefaultModuleEventListener(out);
086         loggers.add(log);
087     }
088 
089     public void addModule(final QedeqBo prop) {
090         for (int i = 0; i < loggers.size(); i++) {
091             try {   // we don't know if the ModuleEventListener is free of programming errors...
092                 ((ModuleEventListenerloggers.get(i)).addModule(prop);
093             catch (RuntimeException e) {
094                 Trace.fatal(CLASS, this, "addModule",
095                     "ModuleEventListener throwed RuntimeException", e);
096             }
097         }
098     }
099 
100     public void stateChanged(final QedeqBo prop) {
101         for (int i = 0; i < loggers.size(); i++) {
102             try {   // we don't know if the ModuleEventListener is free of programming errors...
103                 ((ModuleEventListenerloggers.get(i)).stateChanged(prop);
104             catch (RuntimeException e) {
105                 Trace.fatal(CLASS, this, "stateChanged",
106                     "ModuleEventListener throwed RuntimeException", e);
107             }
108         }
109     }
110 
111     public void removeModule(final QedeqBo prop) {
112         for (int i = 0; i < loggers.size(); i++) {
113             try {   // we don't know if the ModuleEventListener is free of programming errors...
114                 ((ModuleEventListenerloggers.get(i)).removeModule(prop);
115             catch (RuntimeException e) {
116                 Trace.fatal(CLASS, this, "removeModule",
117                     "ModuleEventListener throwed RuntimeException", e);
118             }
119         }
120     }
121 
122 
123 }