KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > avalon > excalibur > component > AbstractDualLogEnabledInstrumentable


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

17 package org.apache.avalon.excalibur.component;
18
19 import java.util.ArrayList JavaDoc;
20
21 import org.apache.excalibur.instrument.Instrument;
22 import org.apache.excalibur.instrument.Instrumentable;
23
24 /**
25  * Utility class to ease the construction of components that can be instrumented
26  * but must also extend AbstractDualLogEnabled.
27  * <p>
28  * Subclasses should call <code>addInstrument</code> or
29  * <code>addChildInstrumentable</code> as part of the component's
30  * initialization.
31  *
32  * @deprecated ECM is no longer supported
33  *
34  * @author <a HREF="mailto:dev@avalon.apache.org">Avalon Development Team</a>
35  */

36 public abstract class AbstractDualLogEnabledInstrumentable
37     extends AbstractDualLogEnabled
38     implements Instrumentable
39 {
40     /** Name of the instrumentable. */
41     private String JavaDoc m_instrumentableName;
42
43     /** Stores the instruments during initialization. */
44     private ArrayList JavaDoc m_instrumentList;
45
46     /** Stores the child instrumentables during initialization. */
47     private ArrayList JavaDoc m_childList;
48
49     /** Flag which is to used to keep track of when the Instrumentable has been registered. */
50     private boolean m_registered;
51
52     /*---------------------------------------------------------------
53      * Constructors
54      *-------------------------------------------------------------*/

55     /**
56      * Creates a new AbstractLogEnabledInstrumentable.
57      */

58     protected AbstractDualLogEnabledInstrumentable()
59     {
60         m_registered = false;
61         m_instrumentList = new ArrayList JavaDoc();
62         m_childList = new ArrayList JavaDoc();
63     }
64
65     /*---------------------------------------------------------------
66      * Methods
67      *-------------------------------------------------------------*/

68     /**
69      * Adds an Instrument to the list of Instruments published by the component.
70      * This method may not be called after the Instrumentable has been
71      * registered with the InstrumentManager.
72      *
73      * @param instrument Instrument to publish.
74      */

75     protected void addInstrument( Instrument instrument )
76     {
77         if( m_registered )
78         {
79             throw new IllegalStateException JavaDoc( "Instruments can not be added after the "
80                 + "Instrumentable is registered with the InstrumentManager." );
81         }
82         m_instrumentList.add( instrument );
83     }
84
85     /**
86      * Adds a child Instrumentable to the list of child Instrumentables
87      * published by the component. This method may not be called after the
88      * Instrumentable has been registered with the InstrumentManager.
89      * <p>
90      * Note that Child Instrumentables must be named by the caller using the
91      * setInstrumentableName method.
92      *
93      * @param child Child Instrumentable to publish.
94      */

95     protected void addChildInstrumentable( Instrumentable child )
96     {
97         if( m_registered )
98         {
99             throw new IllegalStateException JavaDoc( "Child Instrumentables can not be added after the "
100                 + "Instrumentable is registered with the InstrumentManager." );
101         }
102         m_childList.add( child );
103     }
104
105     /*---------------------------------------------------------------
106      * Instrumentable Methods
107      *-------------------------------------------------------------*/

108     /**
109      * Gets the name of the Instrumentable.
110      *
111      * @return The name used to identify a Instrumentable.
112      */

113     public final String JavaDoc getInstrumentableName()
114     {
115         return m_instrumentableName;
116     }
117
118     /**
119      * Sets the name for the Instrumentable. The Instrumentable Name is used
120      * to uniquely identify the Instrumentable during the configuration of
121      * the InstrumentManager and to gain access to an InstrumentableDescriptor
122      * through the InstrumentManager. The value should be a string which does
123      * not contain spaces or periods.
124      * <p>
125      * This value may be set by a parent Instrumentable, or by the
126      * InstrumentManager using the value of the 'instrumentable' attribute in
127      * the configuration of the component.
128      *
129      * @param name The name used to identify a Instrumentable.
130      */

131     public final void setInstrumentableName( String JavaDoc name )
132     {
133         m_instrumentableName = name;
134     }
135
136     /**
137      * Any Object which implements Instrumentable can also make use of other
138      * Instrumentable child objects. This method is used to tell the
139      * InstrumentManager about them.
140      *
141      * @return An array of child Instrumentables. This method should never
142      * return null. If there are no child Instrumentables, then
143      * EMPTY_INSTRUMENTABLE_ARRAY can be returned.
144      */

145     public final Instrumentable[] getChildInstrumentables()
146     {
147         m_registered = true;
148         if( m_childList.size() == 0 )
149         {
150             return Instrumentable.EMPTY_INSTRUMENTABLE_ARRAY;
151         }
152         else
153         {
154             Instrumentable[] children = new Instrumentable[ m_childList.size() ];
155             m_childList.toArray( children );
156             return children;
157         }
158     }
159
160     /**
161      * Obtain a reference to all the Instruments that the Instrumentable object
162      * wishes to expose. All sampling is done directly through the
163      * Instruments as opposed to the Instrumentable interface.
164      *
165      * @return An array of the Instruments available for profiling. Should
166      * never be null. If there are no Instruments, then
167      * EMPTY_INSTRUMENT_ARRAY can be returned. This should never be
168      * the case though unless there are child Instrumentables with
169      * Instruments.
170      */

171     public final Instrument[] getInstruments()
172     {
173         m_registered = true;
174         if( m_instrumentList.size() == 0 )
175         {
176             return Instrumentable.EMPTY_INSTRUMENT_ARRAY;
177         }
178         else
179         {
180             Instrument[] instruments = new Instrument[ m_instrumentList.size() ];
181             m_instrumentList.toArray( instruments );
182             return instruments;
183         }
184     }
185 }
186
Popular Tags