KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > avalon > excalibur > component > example_im > DefaultExampleInstrumentable


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.component.example_im;
20
21 import org.apache.avalon.framework.activity.Startable;
22 import org.apache.avalon.framework.logger.AbstractLogEnabled;
23 import org.apache.excalibur.instrument.CounterInstrument;
24 import org.apache.excalibur.instrument.Instrument;
25 import org.apache.excalibur.instrument.Instrumentable;
26 import org.apache.excalibur.instrument.ValueInstrument;
27
28 /**
29  * This example application creates a component which registers several
30  * Instruments for the example.
31  *
32  * Note, this code ignores exceptions to keep the code simple.
33  *
34  * @author <a HREF="mailto:dev@avalon.apache.org">Avalon Development Team</a>
35  * @version CVS $Revision: 1.1 $ $Date: 2004/03/17 13:22:37 $
36  * @since 4.1
37  */

38 public class DefaultExampleInstrumentable
39     extends AbstractLogEnabled
40     implements ExampleInstrumentable, Startable, Runnable JavaDoc, Instrumentable
41 {
42     public static final String JavaDoc INSTRUMENT_RANDOM_QUICK_NAME = "random-quick";
43     public static final String JavaDoc INSTRUMENT_RANDOM_SLOW_NAME = "random-slow";
44     public static final String JavaDoc INSTRUMENT_RANDOM_RANDOM_NAME = "random-random";
45     public static final String JavaDoc INSTRUMENT_COUNTER_QUICK_NAME = "counter-quick";
46     public static final String JavaDoc INSTRUMENT_COUNTER_SLOW_NAME = "counter-slow";
47     public static final String JavaDoc INSTRUMENT_COUNTER_RANDOM_NAME = "counter-random";
48     public static final String JavaDoc INSTRUMENT_DOACTION_NAME = "doaction-counter";
49
50     /** Instrumentable Name assigned to this Instrumentable */
51     private String JavaDoc m_instrumentableName;
52
53     /** Instrument used to profile random values with lots of updates. */
54     private ValueInstrument m_randomQuickInstrument;
55
56     /** Instrument used to profile random values with few of updates. */
57     private ValueInstrument m_randomSlowInstrument;
58
59     /** Instrument used to profile random values with updates at a random rate. */
60     private ValueInstrument m_randomRandomInstrument;
61
62     /** Instrument used to profile random actions with lots of updates. */
63     private CounterInstrument m_counterQuickInstrument;
64
65     /** Instrument used to profile random actions with few of updates. */
66     private CounterInstrument m_counterSlowInstrument;
67
68     /** Instrument used to profile random actions with updates at a random rate. */
69     private CounterInstrument m_counterRandomInstrument;
70
71     /** Instrument used to count the number of times that doAction is called. */
72     private CounterInstrument m_doActionInstrument;
73
74     /** Thread which is used to send profile data to the random instruments. */
75     private Thread JavaDoc m_runner;
76
77     /*---------------------------------------------------------------
78      * Constructors
79      *-------------------------------------------------------------*/

80     public DefaultExampleInstrumentable()
81     {
82         // Initialize the Instrumentable elements.
83
m_randomQuickInstrument = new ValueInstrument( INSTRUMENT_RANDOM_QUICK_NAME );
84         m_randomSlowInstrument = new ValueInstrument( INSTRUMENT_RANDOM_SLOW_NAME );
85         m_randomRandomInstrument = new ValueInstrument( INSTRUMENT_RANDOM_RANDOM_NAME );
86         m_counterQuickInstrument = new CounterInstrument( INSTRUMENT_COUNTER_QUICK_NAME );
87         m_counterSlowInstrument = new CounterInstrument( INSTRUMENT_COUNTER_SLOW_NAME );
88         m_counterRandomInstrument = new CounterInstrument( INSTRUMENT_COUNTER_RANDOM_NAME );
89         m_doActionInstrument = new CounterInstrument( INSTRUMENT_DOACTION_NAME );
90     }
91
92     /*---------------------------------------------------------------
93      * ExampleInstrumentable Methods
94      *-------------------------------------------------------------*/

95     /**
96      * Example action method.
97      */

98     public void doAction()
99     {
100         getLogger().debug( "ExampleInstrumentable.doAction() called." );
101
102         // Notify the profiler.
103
m_doActionInstrument.increment();
104     }
105
106     /*---------------------------------------------------------------
107      * Startable Methods
108      *-------------------------------------------------------------*/

109     /**
110      * Start the component.
111      */

112     public void start()
113     {
114         if( m_runner == null )
115         {
116             m_runner = new Thread JavaDoc( this, "ExampleInstrumentableRunner" );
117             m_runner.start();
118         }
119     }
120
121     /**
122      * Stop the component.
123      */

124     public void stop()
125     {
126         if( m_runner != null )
127         {
128             m_runner.interrupt();
129             m_runner = null;
130         }
131     }
132
133     /*---------------------------------------------------------------
134      * Runnable Methods
135      *-------------------------------------------------------------*/

136     /**
137      * Runner thread which is responsible for sending data to the Profiler via
138      * the various random Profile Points.
139      */

140     public void run()
141     {
142         int counter = 0;
143         while( m_runner != null )
144         {
145             // Add some delay to the loop.
146
try
147             {
148                 Thread.sleep( 100 );
149             }
150             catch( InterruptedException JavaDoc e )
151             {
152                 if( m_runner == null )
153                 {
154                     return;
155                 }
156             }
157
158             // Handle the quick Profile Points
159
m_randomQuickInstrument.setValue( (int)( Math.random() * 100 ) );
160             m_counterQuickInstrument.increment();
161
162             // Handle the slow Profile Points
163
counter++;
164             if( counter >= 20 )
165             {
166                 m_randomSlowInstrument.setValue( (int)( Math.random() * 100 ) );
167                 m_counterSlowInstrument.increment();
168                 counter = 0;
169             }
170
171             // Handle the random Profile Points. Fire 10% of the time.
172
if( 100 * Math.random() < 10 )
173             {
174                 m_randomRandomInstrument.setValue( (int)( Math.random() * 100 ) );
175                 m_counterRandomInstrument.increment();
176             }
177         }
178     }
179
180     /*---------------------------------------------------------------
181      * Instrumentable Methods
182      *-------------------------------------------------------------*/

183     /**
184      * Sets the name for the Instrumentable. The Instrumentable Name is used
185      * to uniquely identify the Instrumentable during the configuration of
186      * the InstrumentManager and to gain access to an InstrumentableDescriptor
187      * through the InstrumentManager. The value should be a string which does
188      * not contain spaces or periods.
189      * <p>
190      * This value may be set by a parent Instrumentable, or by the
191      * InstrumentManager using the value of the 'instrumentable' attribute in
192      * the configuration of the component.
193      *
194      * @param name The name used to identify a Instrumentable.
195      */

196     public void setInstrumentableName( String JavaDoc name )
197     {
198         m_instrumentableName = name;
199     }
200
201     /**
202      * Gets the name of the Instrumentable.
203      *
204      * @return The name used to identify a Instrumentable.
205      */

206     public String JavaDoc getInstrumentableName()
207     {
208         return m_instrumentableName;
209     }
210
211     /**
212      * Obtain a reference to all the Instruments that the Instrumentable object
213      * wishes to expose. All sampling is done directly through the
214      * Instruments as opposed to the Instrumentable interface.
215      *
216      * @return An array of the Instruments available for profiling. Should
217      * never be null. If there are no Instruments, then
218      * EMPTY_INSTRUMENT_ARRAY can be returned. This should never be
219      * the case though unless there are child Instrumentables with
220      * Instruments.
221      */

222     public Instrument[] getInstruments()
223     {
224         return new Instrument[]
225         {
226             m_randomQuickInstrument,
227             m_randomSlowInstrument,
228             m_randomRandomInstrument,
229             m_counterQuickInstrument,
230             m_counterSlowInstrument,
231             m_counterRandomInstrument,
232             m_doActionInstrument
233         };
234     }
235
236     /**
237      * Any Object which implements Instrumentable can also make use of other
238      * Instrumentable child objects. This method is used to tell the
239      * InstrumentManager about them.
240      *
241      * @return An array of child Instrumentables. This method should never
242      * return null. If there are no child Instrumentables, then
243      * EMPTY_INSTRUMENTABLE_ARRAY can be returned.
244      */

245     public Instrumentable[] getChildInstrumentables()
246     {
247         // This instrumentable does not have any children.
248
return Instrumentable.EMPTY_INSTRUMENTABLE_ARRAY;
249     }
250 }
251
252
Popular Tags