KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > excalibur > instrument > client > http > HTTPInstrumentData


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
20 package org.apache.excalibur.instrument.client.http;
21
22 import java.util.ArrayList JavaDoc;
23 import java.util.HashMap JavaDoc;
24 import java.util.Iterator JavaDoc;
25 import java.util.List JavaDoc;
26 import java.util.Map JavaDoc;
27
28 import org.apache.avalon.framework.configuration.Configuration;
29 import org.apache.avalon.framework.configuration.ConfigurationException;
30
31 import org.apache.excalibur.instrument.client.InstrumentData;
32 import org.apache.excalibur.instrument.client.InstrumentSampleData;
33
34 class HTTPInstrumentData
35     extends AbstractHTTPElementData
36     implements InstrumentData
37 {
38     /* The registered flag of the remote object. */
39     private boolean m_registered;
40     
41     /** The type of the Instrument. */
42     private int m_type;
43     
44     private List JavaDoc m_samples = new ArrayList JavaDoc();
45     private HTTPInstrumentSampleData[] m_sampleAry;
46     private Map JavaDoc m_sampleMap = new HashMap JavaDoc();
47     
48     /*---------------------------------------------------------------
49      * Constructors
50      *-------------------------------------------------------------*/

51     /**
52      * Creates a new HTTPInstrumentData.
53      */

54     HTTPInstrumentData( HTTPInstrumentableData parent,
55                         String JavaDoc name )
56     {
57         super( (HTTPInstrumentManagerConnection)parent.getConnection(), parent, name );
58         
59         m_registered = false;
60     }
61     
62     /*---------------------------------------------------------------
63      * AbstractHTTPElementData Methods
64      *-------------------------------------------------------------*/

65     /**
66      * Update the contents of the object using values from the Configuration object.
67      *
68      * @param configuration Configuration object to load from.
69      * @param recurse True if state should be ignored and we should drill down
70      * using data in this configuration.
71      *
72      * @throws ConfigurationException If there are any problems.
73      */

74     protected void update( Configuration configuration, boolean recurse )
75         throws ConfigurationException
76     {
77         super.update( configuration );
78         
79         if ( getLogger().isDebugEnabled() )
80         {
81             getLogger().debug(
82                 "Updated Instrument '" + getName() + "' to version " + getStateVersion() );
83         }
84         
85         m_registered = configuration.getAttributeAsBoolean( "registered" );
86         m_type = configuration.getAttributeAsInteger( "type" );
87         
88         // Samples can be added as well as removed for each update. Build up a list of
89
// old samples and remove each one that still exists on the server. Any left
90
// over have expired and must be removed.
91
Map JavaDoc oldSampleMap;
92         synchronized( m_samples )
93         {
94             oldSampleMap = new HashMap JavaDoc( m_sampleMap );
95         }
96         
97         Configuration[] sampleConfs = configuration.getChildren( "sample" );
98         for ( int i = 0; i < sampleConfs.length; i++ )
99         {
100             Configuration sConf = sampleConfs[i];
101             String JavaDoc sName = sConf.getAttribute( "name" );
102             int sStateVersion = sConf.getAttributeAsInteger( "state-version" );
103             
104             HTTPInstrumentSampleData sData;
105             synchronized( m_samples )
106             {
107                 sData = (HTTPInstrumentSampleData)m_sampleMap.get( sName );
108                 if ( sData == null )
109                 {
110                     // It is new.
111
sData = new HTTPInstrumentSampleData( this, sName );
112                     sData.enableLogging( getLogger().getChildLogger( sName ) );
113                     m_samples.add( sData );
114                     m_sampleMap.put( sName, sData );
115                     m_sampleAry = null;
116                 }
117                 oldSampleMap.remove( sName );
118             }
119             
120             if ( recurse )
121             {
122                 sData.update( sConf );
123             }
124             else
125             {
126                 if ( sStateVersion != sData.getStateVersion() )
127                 {
128                     // Needs to be updated.
129
sData.update();
130                 }
131             }
132         }
133         
134         // Purge any old samples.
135
if ( !oldSampleMap.isEmpty() )
136         {
137             synchronized( m_samples )
138             {
139                 for ( Iterator JavaDoc iter = oldSampleMap.values().iterator(); iter.hasNext(); )
140                 {
141                     HTTPInstrumentSampleData sample = (HTTPInstrumentSampleData)iter.next();
142                     m_samples.remove( sample );
143                     m_sampleMap.remove( sample.getName() );
144                     m_sampleAry = null;
145                 }
146             }
147         }
148     }
149     
150     /**
151      * Causes the InstrumentData to update itself with the latest data from
152      * the server.
153      *
154      * @return true if successful.
155      */

156     public boolean update()
157     {
158         HTTPInstrumentManagerConnection connection =
159             (HTTPInstrumentManagerConnection)getConnection();
160         
161         Configuration configuration = connection.getState(
162             "instrument.xml?packed=true&name=" + urlEncode( getName() ) );
163         if ( configuration != null )
164         {
165             try
166             {
167                 update( configuration, false );
168                 
169                 return true;
170             }
171             catch ( ConfigurationException e )
172             {
173                 getLogger().debug( "Unable to update.", e );
174             }
175         }
176         
177         return false;
178     }
179     
180     /*---------------------------------------------------------------
181      * InstrumentData Methods
182      *-------------------------------------------------------------*/

183     /**
184      * Returns the registered flag of the remote object.
185      *
186      * @return The registered flag of the remote object.
187      */

188     public boolean isRegistered()
189     {
190         return m_registered;
191     }
192     
193     /**
194      * Returns the type of the Instrument. Possible values include
195      * InstrumentData.INSTRUMENT_TYPE_COUNTER,
196      * InstrumentData.INSTRUMENT_TYPE_VALUE or
197      * InstrumentData.INSTRUMENT_TYPE_NONE, if the type was never set.
198      *
199      * @return The type of the Instrument.
200      */

201     public int getType()
202     {
203         return m_type;
204     }
205     
206     /**
207      * Returns an array of the Instrument Samples assigned to the Instrument.
208      *
209      * @return An array of Instrument Samples.
210      */

211     public InstrumentSampleData[] getInstrumentSamples()
212     {
213         HTTPInstrumentSampleData[] samples = m_sampleAry;
214         if ( samples == null )
215         {
216             synchronized ( m_samples )
217             {
218                 m_sampleAry = new HTTPInstrumentSampleData[m_samples.size()];
219                 m_samples.toArray( m_sampleAry );
220                 samples = m_sampleAry;
221             }
222         }
223         return samples;
224     }
225     
226     /**
227      * Requests that a sample be created or that its lease be updated.
228      *
229      * @param description Description to assign to the new sample.
230      * @param interval Sample interval of the new sample.
231      * @param sampleCount Number of samples in the new sample.
232      * @param leaseTime Requested lease time. The server may not grant the full lease.
233      * @param sampleType The type of sample to be created.
234      *
235      * @return True if successful.
236      */

237     public boolean createInstrumentSample( String JavaDoc description,
238                                            long interval,
239                                            int sampleCount,
240                                            long leaseTime,
241                                            int sampleType )
242     {
243         HTTPInstrumentManagerConnection connection =
244             (HTTPInstrumentManagerConnection)getConnection();
245         
246         Configuration configuration = connection.getState(
247             "create-sample.xml?name=" + urlEncode( getName() )
248             + "&description=" + urlEncode( description ) + "&interval=" + interval
249             + "&size=" + sampleCount + "&lease=" + leaseTime + "&type=" + sampleType );
250         
251         // If there were any errors on the server while creating the sample then null
252
// will be returned.
253
return configuration != null;
254     }
255     
256     /*---------------------------------------------------------------
257      * Methods
258      *-------------------------------------------------------------*/

259 }
Popular Tags