KickJava   Java API By Example, From Geeks To Geeks.

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


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.List JavaDoc;
25 import java.util.Map JavaDoc;
26
27 import org.apache.avalon.framework.configuration.Configuration;
28 import org.apache.avalon.framework.configuration.ConfigurationException;
29
30 import org.apache.excalibur.instrument.client.InstrumentableData;
31 import org.apache.excalibur.instrument.client.InstrumentManagerData;
32
33 class HTTPInstrumentManagerData
34     extends AbstractHTTPData
35     implements InstrumentManagerData
36 {
37     /* Name of the remote object. */
38     private String JavaDoc m_name;
39     
40     /* Flag which keeps track of whether the manager supports batched lease updates. */
41     private boolean m_batchedUpdates;
42     
43     /* Flag which keeps track of whether the manager is read only or not. */
44     private boolean m_readOnly;
45     
46     private List JavaDoc m_instrumentables = new ArrayList JavaDoc();
47     private HTTPInstrumentableData[] m_instrumentableAry;
48     private Map JavaDoc m_instrumentableMap = new HashMap JavaDoc();
49     
50     /*---------------------------------------------------------------
51      * Constructors
52      *-------------------------------------------------------------*/

53     /**
54      * Creates a new HTTPInstrumentManagerData.
55      */

56     HTTPInstrumentManagerData( HTTPInstrumentManagerConnection connection )
57     {
58         super( connection, connection.getURL().toExternalForm() );
59         
60         m_name = connection.getURL().toExternalForm();
61     }
62     
63     /*---------------------------------------------------------------
64      * AbstractHTTPData Methods
65      *-------------------------------------------------------------*/

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

75     protected void update( Configuration configuration, boolean recurse )
76         throws ConfigurationException
77     {
78         super.update( configuration );
79         
80         m_name = configuration.getAttribute( "name" );
81         
82         // Support for batched lease creates and renewals added in version 1.2.
83
m_batchedUpdates = configuration.getAttributeAsBoolean( "batched-updates", false );
84         
85         // read-only attribute added in version 1.2.
86
m_readOnly = configuration.getAttributeAsBoolean( "read-only", false );
87         
88         if ( getLogger().isDebugEnabled() )
89         {
90             getLogger().debug(
91                 "Updated InstrumentManager '" + getName() + "' to version " + getStateVersion() );
92         }
93         
94         Configuration[] instrumentableConfs = configuration.getChildren( "instrumentable" );
95         for ( int i = 0; i < instrumentableConfs.length; i++ )
96         {
97             Configuration iaConf = instrumentableConfs[i];
98             String JavaDoc iaName = iaConf.getAttribute( "name" );
99             int iaStateVersion = iaConf.getAttributeAsInteger( "state-version" );
100             
101             HTTPInstrumentableData iaData;
102             synchronized ( m_instrumentables )
103             {
104                 iaData = (HTTPInstrumentableData)m_instrumentableMap.get( iaName );
105                 if ( iaData == null )
106                 {
107                     // It is new.
108
iaData = new HTTPInstrumentableData( this, iaName );
109                     iaData.enableLogging( getLogger().getChildLogger( iaName ) );
110                     m_instrumentables.add( iaData );
111                     m_instrumentableAry = null;
112                     m_instrumentableMap.put( iaName, iaData );
113                 }
114             }
115             
116             if ( recurse )
117             {
118                 iaData.update( iaConf, recurse );
119             }
120             else
121             {
122                 if ( iaStateVersion != iaData.getStateVersion() )
123                 {
124                     // Needs to be updated.
125
iaData.update();
126                 }
127             }
128         }
129     }
130     
131     /**
132      * Causes the InstrumentManagerData to update itself with the latest data
133      * from the server.
134      *
135      * @return true if successful.
136      */

137     public boolean update()
138     {
139         HTTPInstrumentManagerConnection connection =
140             (HTTPInstrumentManagerConnection)getConnection();
141         
142         Configuration configuration = connection.getState( "instrument-manager.xml?packed=true" );
143         if ( configuration != null )
144         {
145             try
146             {
147                 update( configuration, false );
148                 
149                 //updateLeasedSamples();
150

151                 return true;
152             }
153             catch ( ConfigurationException e )
154             {
155                 getLogger().debug( "Unable to update.", e );
156             }
157         }
158         return false;
159     }
160     
161     /*---------------------------------------------------------------
162      * InstrumentManagerData Methods
163      *-------------------------------------------------------------*/

164     /**
165      * Returns the name.
166      *
167      * @return The name.
168      */

169     public String JavaDoc getName()
170     {
171         return m_name;
172     }
173     
174     /**
175      * Returns true if the InstrumentManager on the server is operating in
176      * read-only mode.
177      *
178      * @return True if read-only.
179      *
180      * @since 1.2
181      */

182     public boolean isReadOnly()
183     {
184         return m_readOnly;
185     }
186     
187     /**
188      * Returns true if batched lease creates and renewals are implemented on
189      * the server.
190      *
191      * @return True if read-only.
192      *
193      * @since 1.2
194      */

195     private boolean isSupportsBatchedUpdates()
196     {
197         return m_batchedUpdates;
198     }
199     
200     /**
201      * Gets a thread-safe snapshot of the instrumentable list.
202      *
203      * @return A thread-safe snapshot of the instrumentable list.
204      */

205     public InstrumentableData[] getInstrumentables()
206     {
207         HTTPInstrumentableData[] instrumentables = m_instrumentableAry;
208         if ( instrumentables == null )
209         {
210             synchronized ( m_instrumentables )
211             {
212                 m_instrumentableAry = new HTTPInstrumentableData[m_instrumentables.size()];
213                 m_instrumentables.toArray( m_instrumentableAry );
214                 instrumentables = m_instrumentableAry;
215             }
216         }
217         return instrumentables;
218     }
219     
220     /**
221      * Causes the the entire instrument tree to be updated in one call. Very fast
222      * when it is known that all or most data has changed.
223      *
224      * @return true if successful.
225      */

226     public boolean updateAll()
227     {
228         HTTPInstrumentManagerConnection connection =
229             (HTTPInstrumentManagerConnection)getConnection();
230         
231         Configuration configuration =
232             connection.getState( "instrument-manager.xml?packed=true&recurse=true" );
233         if ( configuration != null )
234         {
235             try
236             {
237                 update( configuration, true );
238                 
239                 //updateLeasedSamples();
240

241                 return true;
242             }
243             catch ( ConfigurationException e )
244             {
245                 getLogger().debug( "Unable to update.", e );
246             }
247         }
248         return false;
249     }
250     
251     /**
252      * Requests that a sample be created or that its lease be updated.
253      *
254      * @param instrumentName The full name of the instrument whose sample is
255      * to be created or updated.
256      * @param description Description to assign to the new sample.
257      * @param interval Sample interval of the new sample.
258      * @param sampleCount Number of samples in the new sample.
259      * @param leaseTime Requested lease time. The server may not grant the
260      * full lease.
261      * @param sampleType The type of sample to be created.
262      */

263     public void createInstrumentSample( String JavaDoc instrumentName,
264                                         String JavaDoc description,
265                                         long interval,
266                                         int sampleCount,
267                                         long leaseTime,
268                                         int sampleType )
269     {
270         HTTPInstrumentManagerConnection connection =
271             (HTTPInstrumentManagerConnection)getConnection();
272         
273         connection.getState( "create-sample.xml?name=" + urlEncode( instrumentName )
274             + "&description=" + urlEncode( description ) + "&interval=" + interval
275             + "&size=" + sampleCount + "&lease=" + leaseTime + "&type=" + sampleType );
276     }
277     
278     /**
279      * Requests that a set of samples be created or that their leases be
280      * updated. All array parameters must be of the same length.
281      *
282      * @param instrumentNames The full names of the instruments whose sample
283      * are to be created or updated.
284      * @param descriptions Descriptions to assign to the new samples.
285      * @param intervals Sample intervals of the new samples.
286      * @param sampleCounts Number of samples in each the new samples.
287      * @param leaseTimes Requested lease times. The server may not grant the
288      * full leases.
289      * @param sampleTypes The types of samples to be created.
290      */

291     public void createInstrumentSamples( String JavaDoc[] instrumentNames,
292                                          String JavaDoc[] descriptions,
293                                          long[] intervals,
294                                          int[] sampleCounts,
295                                          long[] leaseTimes,
296                                          int[] sampleTypes )
297     {
298         HTTPInstrumentManagerConnection connection =
299             (HTTPInstrumentManagerConnection)getConnection();
300         
301         // Validate the arguments to avoid errors from misuse.
302
if ( ( instrumentNames.length != descriptions.length )
303             || ( instrumentNames.length != intervals.length )
304             || ( instrumentNames.length != sampleCounts.length )
305             || ( instrumentNames.length != leaseTimes.length )
306             || ( instrumentNames.length != sampleTypes.length ) )
307         {
308             throw new IllegalArgumentException JavaDoc( "Array lengths of all parameters must be equal." );
309         }
310         
311         // If batched updates are not supported, then do them individually
312
if ( isSupportsBatchedUpdates() )
313         {
314             StringBuffer JavaDoc sb = new StringBuffer JavaDoc();
315             sb.append( "create-samples.xml?" );
316             for ( int i = 0; i < instrumentNames.length; i++ )
317             {
318                 if ( i > 0 )
319                 {
320                     sb.append( "&" );
321                 }
322                 sb.append( "name=" );
323                 sb.append( urlEncode( instrumentNames[i] ) );
324                 sb.append( "&description=" );
325                 sb.append( urlEncode( descriptions[i] ) );
326                 sb.append( "&interval=" );
327                 sb.append( intervals[i] );
328                 sb.append( "&size=" );
329                 sb.append( sampleCounts[i] );
330                 sb.append( "&lease=" );
331                 sb.append( leaseTimes[i] );
332                 sb.append( "&type=" );
333                 sb.append( sampleTypes[i] );
334             }
335             
336             connection.getState( sb.toString() );
337         }
338         else
339         {
340             for ( int i = 0; i < instrumentNames.length; i++ )
341             {
342                 createInstrumentSample( instrumentNames[i], descriptions[i], intervals[i],
343                     sampleCounts[i], leaseTimes[i], sampleTypes[i] );
344             }
345         }
346     }
347     
348     /*---------------------------------------------------------------
349      * Methods
350      *-------------------------------------------------------------*/

351 }
Popular Tags