KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > avalon > excalibur > logger > util > AvalonTee


1 /*
2  * Licensed to the Apache Software Foundation (ASF) under one or more
3  * contributor license agreements. See the NOTICE file distributed with
4  * this work for additional information regarding copyright ownership.
5  * The ASF licenses this file to You under the Apache License, Version 2.0
6  * (the "License"); you may not use this file except in compliance with
7  * the License. You may obtain a copy of the License at
8  *
9  * http://www.apache.org/licenses/LICENSE-2.0
10  *
11  * Unless required by applicable law or agreed to in writing, software
12  * distributed under the License is distributed on an "AS IS" BASIS,
13  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or
14  * implied.
15  *
16  * See the License for the specific language governing permissions and
17  * limitations under the License.
18  */

19 package org.apache.avalon.excalibur.logger.util;
20
21 import org.apache.avalon.framework.logger.Logger;
22 import org.apache.avalon.framework.logger.LogEnabled;
23 import org.apache.avalon.framework.context.Context;
24 import org.apache.avalon.framework.context.Contextualizable;
25 import org.apache.avalon.framework.context.ContextException;
26 import org.apache.avalon.framework.configuration.Configuration;
27 import org.apache.avalon.framework.configuration.Configurable;
28 import org.apache.avalon.framework.configuration.ConfigurationException;
29 import org.apache.avalon.framework.activity.Startable;
30 import org.apache.avalon.framework.activity.Disposable;
31 import org.apache.avalon.framework.container.ContainerUtil;
32 import java.util.ArrayList JavaDoc;
33
34 /**
35  * This class broadcasts Avalon lifestyle events to several
36  * destination objects, somewhat like Unix 'tee' command
37  * directing its input both to file and to its output.
38  *
39  * The current implementation is incomplete and handles
40  * only LogEnabled, Contextutalizable, Configurable and Disposable
41  * interfaces.
42  *
43  * @author <a HREF="mailto:dev@avalon.apache.org">Avalon Development Team</a>
44  * @version CVS $Revision: 1.4 $ $Date: 2004/03/10 13:54:51 $
45  * @since 4.0
46  */

47
48 public class AvalonTee implements
49         LogEnabled,
50         Contextualizable,
51         Configurable,
52         Startable,
53         Disposable
54 {
55     /* The objects we direct our events to */
56     private ArrayList JavaDoc m_listeners = new ArrayList JavaDoc( 10 );
57     /**
58      * The number of these objects. This variable is here
59      * is to save a line of code in every reactor method
60      * rather then to optimize speed.
61      */

62     private int m_len = 0;
63
64     /* Has adding new tees been prohibited? */
65     private boolean m_readOnly = false;
66
67     /**
68      * Disallow adding more tees.
69      */

70     public void makeReadOnly()
71     {
72         m_readOnly = true;
73     }
74
75     /**
76      * Adds an object to the list of objects receiving events.
77      * @param obj the object to add; can not be null.
78      */

79     public void addTee( final Object JavaDoc obj )
80     {
81         if ( m_readOnly )
82         {
83             throw new IllegalStateException JavaDoc( "makeReadOnly() already invoked" );
84         }
85
86         if ( obj == null ) throw new NullPointerException JavaDoc( "obj" );
87         if ( m_listeners.contains( obj ) )
88         {
89             // should we complain? better not, probably
90
}
91         else
92         {
93             // adds to the end of the array
94
m_listeners.add( obj );
95             m_len = m_listeners.size();
96         }
97     }
98
99     public void enableLogging( final Logger logger )
100     {
101         for( int i = 0; i < m_len; ++i )
102         {
103             ContainerUtil.enableLogging( m_listeners.get( i ), logger );
104         }
105     }
106
107     public void contextualize( final Context context ) throws ContextException
108     {
109         for( int i = 0; i < m_len; ++i )
110         {
111             ContainerUtil.contextualize( m_listeners.get( i ), context );
112         }
113     }
114
115     public void configure( final Configuration config ) throws ConfigurationException
116     {
117         for( int i = 0; i < m_len; ++i )
118         {
119             ContainerUtil.configure( m_listeners.get( i ), config );
120         }
121     }
122
123     public void start() throws Exception JavaDoc
124     {
125         for( int i = 0; i < m_len; ++i )
126         {
127             ContainerUtil.start( m_listeners.get( i ) );
128         }
129     }
130
131     public void stop() throws Exception JavaDoc
132     {
133         for( int i = 0; i < m_len; ++i )
134         {
135             ContainerUtil.stop( m_listeners.get( i ) );
136         }
137     }
138
139     public void dispose()
140     {
141         for( int i = 0; i < m_len; ++i )
142         {
143             ContainerUtil.dispose( m_listeners.get( i ) );
144         }
145     }
146 }
147
Popular Tags