KickJava   Java API By Example, From Geeks To Geeks.

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


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.StringTokenizer JavaDoc;
23
24 import org.apache.excalibur.instrument.client.Data;
25 import org.apache.excalibur.instrument.client.InstrumentSampleSnapshotData;
26
27 import org.apache.avalon.framework.configuration.Configuration;
28 import org.apache.avalon.framework.configuration.ConfigurationException;
29
30 class HTTPInstrumentSampleSnapshotData
31     extends AbstractHTTPInstrumentSampleElementData
32     implements InstrumentSampleSnapshotData
33 {
34     /** Array of values which make up the sample. */
35     private int[] m_samples;
36     
37     /*---------------------------------------------------------------
38      * Constructors
39      *-------------------------------------------------------------*/

40     /**
41      * Creates a new HTTPInstrumentSampleSnapshotData.
42      *
43      * @param connection The connection used to communicate with the server.
44      * @param name The name of the data element.
45      */

46     HTTPInstrumentSampleSnapshotData( HTTPInstrumentManagerConnection connection,
47                                       String JavaDoc name )
48     {
49         super( connection, null, name );
50     }
51     
52     /*---------------------------------------------------------------
53      * AbstractHTTPElementData Methods
54      *-------------------------------------------------------------*/

55     /**
56      * Returns the parent data object.
57      *
58      * @return The parent data object.
59      */

60     public Data getParent()
61     {
62         throw new IllegalStateException JavaDoc( "getParent() can not be called for snapshots." );
63     }
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      *
70      * @throws ConfigurationException If there are any problems.
71      */

72     protected void update( Configuration configuration )
73         throws ConfigurationException
74     {
75         super.update( configuration );
76         
77         if ( getLogger().isDebugEnabled() )
78         {
79             getLogger().debug( "Updated Instrument Sample snapshot '" + getName() + "' "
80                 + "to version " + getStateVersion() );
81         }
82         
83         // Get the actual number of samples returned.
84
int count = configuration.getAttributeAsInteger( "count", getSize() );
85         
86         m_samples = new int[count];
87         
88         String JavaDoc rawSamples = configuration.getChild( "values" ).getValue( "" );
89         StringTokenizer JavaDoc st = new StringTokenizer JavaDoc( rawSamples, ", " );
90         
91         // The token count should always match the size. But mne careful just in
92
// case as the server may not be the same version.
93
int i = 0;
94         while ( st.hasMoreTokens() && ( i < m_samples.length ) )
95         {
96             int value;
97             try
98             {
99                 value = Integer.parseInt( st.nextToken() );
100             }
101             catch ( NumberFormatException JavaDoc e )
102             {
103                 value = 0;
104             }
105             m_samples[i] = value;
106             i++;
107         }
108     }
109     
110     /**
111      * Causes the InstrumentSampleSnapshotData to update itself with the latest
112      * data from the server.
113      *
114      * @return true if successful.
115      */

116     public boolean update()
117     {
118         HTTPInstrumentManagerConnection connection =
119             (HTTPInstrumentManagerConnection)getConnection();
120         
121         Configuration configuration = connection.getState(
122             "snapshot.xml?packed=true&name=" + urlEncode( getName() ) + "&compact=true" );
123         if ( configuration != null )
124         {
125             try
126             {
127                 update( configuration );
128                 
129                 return true;
130             }
131             catch ( ConfigurationException e )
132             {
133                 getLogger().debug( "Unable to update.", e );
134             }
135         }
136         
137         return false;
138     }
139     
140     /*---------------------------------------------------------------
141      * InstrumentSampleSnapshotData Methods
142      *-------------------------------------------------------------*/

143     /**
144      * Returns an array of the individual values which make up the sample.
145      *
146      * @return An array of sample values.
147      */

148     public int[] getSamples()
149     {
150         return m_samples;
151     }
152     
153     /*---------------------------------------------------------------
154      * Methods
155      *-------------------------------------------------------------*/

156 }
Popular Tags