KickJava   Java API By Example, From Geeks To Geeks.

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


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.InstrumentData;
32
33 class HTTPInstrumentableData
34     extends AbstractHTTPElementData
35     implements InstrumentableData
36 {
37     /* The registered flag of the remote object. */
38     private boolean m_registered;
39     
40     private List JavaDoc m_instrumentables = new ArrayList JavaDoc();
41     private HTTPInstrumentableData[] m_instrumentableAry;
42     private Map JavaDoc m_instrumentableMap = new HashMap JavaDoc();
43     
44     private List JavaDoc m_instruments = new ArrayList JavaDoc();
45     private HTTPInstrumentData[] m_instrumentAry;
46     private Map JavaDoc m_instrumentMap = new HashMap JavaDoc();
47     
48     /*---------------------------------------------------------------
49      * Constructors
50      *-------------------------------------------------------------*/

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

54     HTTPInstrumentableData( AbstractHTTPData 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 Instrumentable '" + getName() + "' to version " + getStateVersion() );
83         }
84         
85         m_registered = configuration.getAttributeAsBoolean( "registered" );
86         
87         Configuration[] instrumentableConfs = configuration.getChildren( "instrumentable" );
88         for ( int i = 0; i < instrumentableConfs.length; i++ )
89         {
90             Configuration iaConf = instrumentableConfs[i];
91             String JavaDoc iaName = iaConf.getAttribute( "name" );
92             int iaStateVersion = iaConf.getAttributeAsInteger( "state-version" );
93             
94             HTTPInstrumentableData iaData;
95             synchronized( m_instrumentables )
96             {
97                 iaData = (HTTPInstrumentableData)m_instrumentableMap.get( iaName );
98                 if ( iaData == null )
99                 {
100                     // It is new.
101
iaData = new HTTPInstrumentableData( this, iaName );
102                     iaData.enableLogging( getLogger().getChildLogger( iaName ) );
103                     m_instrumentables.add( iaData );
104                     m_instrumentableAry = null;
105                     m_instrumentableMap.put( iaName, iaData );
106                 }
107             }
108             
109             if ( recurse )
110             {
111                 iaData.update( iaConf, recurse );
112             }
113             else
114             {
115                 if ( iaStateVersion != iaData.getStateVersion() )
116                 {
117                     // Needs to be updated.
118
iaData.update();
119                 }
120             }
121         }
122         
123         Configuration[] instrumentConfs = configuration.getChildren( "instrument" );
124         for ( int i = 0; i < instrumentConfs.length; i++ )
125         {
126             Configuration iConf = instrumentConfs[i];
127             String JavaDoc iName = iConf.getAttribute( "name" );
128             int iStateVersion = iConf.getAttributeAsInteger( "state-version" );
129             
130             HTTPInstrumentData iData;
131             synchronized( m_instruments )
132             {
133                 iData = (HTTPInstrumentData)m_instrumentMap.get( iName );
134                 if ( iData == null )
135                 {
136                     // It is new.
137
iData = new HTTPInstrumentData( this, iName );
138                     iData.enableLogging( getLogger().getChildLogger( iName ) );
139                     m_instruments.add( iData );
140                     m_instrumentAry = null;
141                     m_instrumentMap.put( iName, iData );
142                 }
143             }
144             
145             if ( recurse )
146             {
147                 iData.update( iConf, recurse );
148             }
149             else
150             {
151                 if ( iStateVersion != iData.getStateVersion() )
152                 {
153                     // Needs to be updated.
154
iData.update();
155                 }
156             }
157         }
158     }
159     
160     /**
161      * Causes the InstrumentableData to update itself with the latest data from
162      * the server.
163      *
164      * @return true if successful.
165      */

166     public boolean update()
167     {
168         HTTPInstrumentManagerConnection connection =
169             (HTTPInstrumentManagerConnection)getConnection();
170         
171         Configuration configuration = connection.getState(
172             "instrumentable.xml?packed=true&name=" + urlEncode( getName() ) );
173         if ( configuration != null )
174         {
175             try
176             {
177                 update( configuration, false );
178                 return true;
179             }
180             catch ( ConfigurationException e )
181             {
182                 getLogger().debug( "Unable to update.", e );
183             }
184         }
185         
186         return false;
187     }
188     
189     /*---------------------------------------------------------------
190      * InstrumentableData Methods
191      *-------------------------------------------------------------*/

192     /**
193      * Returns the registered flag of the remote object.
194      *
195      * @return The registered flag of the remote object.
196      */

197     public boolean isRegistered()
198     {
199         return m_registered;
200     }
201     
202     /**
203      * Gets a thread-safe snapshot of the child instrumentable list.
204      *
205      * @return A thread-safe snapshot of the child instrumentable list.
206      */

207     public InstrumentableData[] getInstrumentables()
208     {
209         HTTPInstrumentableData[] instrumentables = m_instrumentableAry;
210         if ( instrumentables == null )
211         {
212             synchronized ( m_instrumentables )
213             {
214                 m_instrumentableAry = new HTTPInstrumentableData[m_instrumentables.size()];
215                 m_instrumentables.toArray( m_instrumentableAry );
216                 instrumentables = m_instrumentableAry;
217             }
218         }
219         return instrumentables;
220     }
221     
222     /**
223      * Gets a thread-safe snapshot of the instrument list.
224      *
225      * @return A thread-safe snapshot of the instrument list.
226      */

227     public InstrumentData[] getInstruments()
228     {
229         HTTPInstrumentData[] instruments = m_instrumentAry;
230         if ( instruments == null )
231         {
232             synchronized ( m_instruments )
233             {
234                 m_instrumentAry = new HTTPInstrumentData[m_instruments.size()];
235                 m_instruments.toArray( m_instrumentAry );
236                 instruments = m_instrumentAry;
237             }
238         }
239         return instruments;
240     }
241     
242     /*---------------------------------------------------------------
243      * Methods
244      *-------------------------------------------------------------*/

245 }
Popular Tags