Docjar: A Java Source and Docuemnt Enginecom.*    java.*    javax.*    org.*    all    new    plug-in

Quick Search    Search Deep

Source code: junit/log4j/JUnitAppenderSingleton.java


1   package junit.log4j;
2   /*
3    * This library is free software; you can redistribute it and/or
4    * modify it under the terms of the GNU Lesser General Public
5    * License as published by the Free Software Foundation; either
6    * version 2 of the License, or (at your option) any later version.<p>
7    *
8    * This library is distributed in the hope that it will be useful,
9    * but WITHOUT ANY WARRANTY; without even the implied warranty of
10   * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
11   * Lesser General Public License for more details.<p>
12   *
13   * You should have received a copy of the GNU Lesser General Public
14   * License along with this library; if not, write to the Free Software
15   * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307  USA<br>
16   * http://www.gnu.org/copyleft/lesser.html
17   */
18  
19  import java.util.Iterator;
20  import java.util.LinkedList;
21  import org.apache.log4j.Layout;
22  import org.apache.log4j.spi.LoggingEvent;
23  
24  // Configuration Management Information: 
25  // -------------------------------------
26  // $Id: JUnitAppenderSingleton.java,v 1.2 2001/11/12 21:36:14 wreissen Exp $
27  //
28  // Version History:
29  // ----------------
30  // $Log: JUnitAppenderSingleton.java,v $
31  // Revision 1.2  2001/11/12 21:36:14  wreissen
32  // javadoc bug fixes
33  //
34  // Revision 1.1  2001/11/12 17:57:15  wreissen
35  // initial version.
36  //
37  //
38  // ***********************************************************************************
39  /**
40   * <p>Singleton that distributes 
41   * {@link org.apache.log4j.Layout#format(org.apache.log4j.spi.LoggingEvent)
42   *        logging events} to all registered
43   * {@link junit.log4j.AppenderListener logging listeners}.</p>
44   *
45   *
46   * Created: Mon Nov 12 10:21:18 2001
47   *
48   * @author <a href="mailto:wolfgang@openfuture.de">Wolfgang Reissenberger</a>
49   * @version $Revision: 1.2 $
50   */
51  
52  public class JUnitAppenderSingleton  {
53      
54      private static JUnitAppenderSingleton instance;
55      private static Layout layout;
56      private static LinkedList listeners;
57  
58      /**
59       * Creates a new <code>JUnitAppenderSingleton</code> instance.
60       *
61       */
62      protected JUnitAppenderSingleton() {
63    listeners = new LinkedList();
64      }
65  
66  
67      /**
68       * Return the instance of this class. If no instance is present,
69       * a new one will be instantiated first.
70       *
71       * @return a <code>JUnitAppenderSingleton</code> value
72       */
73      protected static synchronized JUnitAppenderSingleton getInstance() {
74    if (instance == null) {
75        instance = new JUnitAppenderSingleton();
76    }
77    
78    return instance;
79      }
80  
81  
82      /**
83       * Sends the event as
84       * {@link org.apache.log4j.Layout#format(org.apache.log4j.spi.LoggingEvent)
85       *        formatted message} to all 
86       * {@link junit.log4j.AppenderListener logging listeners}.
87       * @param event a <code>LoggingEvent</code> value
88       */
89      protected synchronized static void addLoggingEvent(LoggingEvent event) {
90    if (event == null) return;
91  
92    String message;
93    if (getLayout() != null) {
94        message = getLayout().format(event);
95    } else message = event.getMessage().toString();
96  
97    if (message == null) return;
98  
99    Iterator it = cloneListeners().iterator();
100 
101   while(it.hasNext()) {
102       Object listener =  it.next();
103 
104       if (listener instanceof AppenderListener) {
105     ((AppenderListener) listener).addMessage(message);
106       }
107   }
108     }
109 
110 
111     /**
112      * Get the value of layout.
113      * @return value of layout.
114      */
115     public static synchronized Layout getLayout() {
116   return getInstance().layout;
117     }
118     
119     /**
120      * Set the value of layout.
121      * @param v  Value to assign to layout.
122      */
123     public static synchronized void setLayout(Layout  v) {
124   getInstance().layout = v;
125     }
126 
127 
128     /**
129      * Registers a TestListener.
130      * @param listener an <code>AppenderListener</code> value
131      */
132     public synchronized static void addListener(AppenderListener listener) {
133   getInstance().listeners.add(listener);
134     }
135     /**
136      * Unregisters a TestListener.
137      * @param listener an <code>AppenderListener</code> value
138      */
139     public synchronized static void removeListener(AppenderListener listener) {
140   getInstance().listeners.remove(listener);
141     }
142 
143 
144     /**
145      * Clone the current list of listeners.
146      *
147      * @return a <code>LinkedList</code> value
148      */
149     public static synchronized LinkedList cloneListeners() {
150   return (LinkedList) getInstance().listeners.clone();
151     }
152 
153 
154 } // JUnitAppenderSingleton