You are viewing a plain text version of this content. The canonical link for it is here.
Posted to scm@excalibur.apache.org by cr...@apache.org on 2007/02/10 01:10:00 UTC
svn commit: r505577 [3/16] - in /excalibur/trunk: ./ components/datasource/
components/datasource/examples/bin/
components/datasource/src/java/org/apache/avalon/excalibur/datasource/
components/datasource/src/test/org/apache/avalon/excalibur/datasource...
Modified: excalibur/trunk/containerkit/instrument/client/src/java/org/apache/excalibur/instrument/client/http/HTTPInstrumentData.java
URL: http://svn.apache.org/viewvc/excalibur/trunk/containerkit/instrument/client/src/java/org/apache/excalibur/instrument/client/http/HTTPInstrumentData.java?view=diff&rev=505577&r1=505576&r2=505577
==============================================================================
--- excalibur/trunk/containerkit/instrument/client/src/java/org/apache/excalibur/instrument/client/http/HTTPInstrumentData.java (original)
+++ excalibur/trunk/containerkit/instrument/client/src/java/org/apache/excalibur/instrument/client/http/HTTPInstrumentData.java Fri Feb 9 16:09:35 2007
@@ -1,257 +1,257 @@
-/*
- * Copyright 2002-2004 The Apache Software Foundation
- * Licensed under the Apache License, Version 2.0 (the "License");
- * you may not use this file except in compliance with the License.
- * You may obtain a copy of the License at
- *
- * http://www.apache.org/licenses/LICENSE-2.0
- *
- * Unless required by applicable law or agreed to in writing, software
- * distributed under the License is distributed on an "AS IS" BASIS,
- * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or
- * implied.
- *
- * See the License for the specific language governing permissions and
- * limitations under the License.
- */
-
-package org.apache.excalibur.instrument.client.http;
-
-import java.util.ArrayList;
-import java.util.HashMap;
-import java.util.Iterator;
-import java.util.List;
-import java.util.Map;
-
-import org.apache.avalon.framework.configuration.Configuration;
-import org.apache.avalon.framework.configuration.ConfigurationException;
-
-import org.apache.excalibur.instrument.client.InstrumentData;
-import org.apache.excalibur.instrument.client.InstrumentSampleData;
-
-class HTTPInstrumentData
- extends AbstractHTTPElementData
- implements InstrumentData
-{
- /* The registered flag of the remote object. */
- private boolean m_registered;
-
- /** The type of the Instrument. */
- private int m_type;
-
- private List m_samples = new ArrayList();
- private HTTPInstrumentSampleData[] m_sampleAry;
- private Map m_sampleMap = new HashMap();
-
- /*---------------------------------------------------------------
- * Constructors
- *-------------------------------------------------------------*/
- /**
- * Creates a new HTTPInstrumentData.
- */
- HTTPInstrumentData( HTTPInstrumentableData parent,
- String name )
- {
- super( (HTTPInstrumentManagerConnection)parent.getConnection(), parent, name );
-
- m_registered = false;
- }
-
- /*---------------------------------------------------------------
- * AbstractHTTPElementData Methods
- *-------------------------------------------------------------*/
- /**
- * Update the contents of the object using values from the Configuration object.
- *
- * @param configuration Configuration object to load from.
- * @param recurse True if state should be ignored and we should drill down
- * using data in this configuration.
- *
- * @throws ConfigurationException If there are any problems.
- */
- protected void update( Configuration configuration, boolean recurse )
- throws ConfigurationException
- {
- super.update( configuration );
-
- if ( getLogger().isDebugEnabled() )
- {
- getLogger().debug(
- "Updated Instrument '" + getName() + "' to version " + getStateVersion() );
- }
-
- m_registered = configuration.getAttributeAsBoolean( "registered" );
- m_type = configuration.getAttributeAsInteger( "type" );
-
- // Samples can be added as well as removed for each update. Build up a list of
- // old samples and remove each one that still exists on the server. Any left
- // over have expired and must be removed.
- Map oldSampleMap;
- synchronized( m_samples )
- {
- oldSampleMap = new HashMap( m_sampleMap );
- }
-
- Configuration[] sampleConfs = configuration.getChildren( "sample" );
- for ( int i = 0; i < sampleConfs.length; i++ )
- {
- Configuration sConf = sampleConfs[i];
- String sName = sConf.getAttribute( "name" );
- int sStateVersion = sConf.getAttributeAsInteger( "state-version" );
-
- HTTPInstrumentSampleData sData;
- synchronized( m_samples )
- {
- sData = (HTTPInstrumentSampleData)m_sampleMap.get( sName );
- if ( sData == null )
- {
- // It is new.
- sData = new HTTPInstrumentSampleData( this, sName );
- sData.enableLogging( getLogger().getChildLogger( sName ) );
- m_samples.add( sData );
- m_sampleMap.put( sName, sData );
- m_sampleAry = null;
- }
- oldSampleMap.remove( sName );
- }
-
- if ( recurse )
- {
- sData.update( sConf );
- }
- else
- {
- if ( sStateVersion != sData.getStateVersion() )
- {
- // Needs to be updated.
- sData.update();
- }
- }
- }
-
- // Purge any old samples.
- if ( !oldSampleMap.isEmpty() )
- {
- synchronized( m_samples )
- {
- for ( Iterator iter = oldSampleMap.values().iterator(); iter.hasNext(); )
- {
- HTTPInstrumentSampleData sample = (HTTPInstrumentSampleData)iter.next();
- m_samples.remove( sample );
- m_sampleMap.remove( sample.getName() );
- m_sampleAry = null;
- }
- }
- }
- }
-
- /**
- * Causes the InstrumentData to update itself with the latest data from
- * the server.
- *
- * @return true if successful.
- */
- public boolean update()
- {
- HTTPInstrumentManagerConnection connection =
- (HTTPInstrumentManagerConnection)getConnection();
-
- Configuration configuration = connection.getState(
- "instrument.xml?packed=true&name=" + urlEncode( getName() ) );
- if ( configuration != null )
- {
- try
- {
- update( configuration, false );
-
- return true;
- }
- catch ( ConfigurationException e )
- {
- getLogger().debug( "Unable to update.", e );
- }
- }
-
- return false;
- }
-
- /*---------------------------------------------------------------
- * InstrumentData Methods
- *-------------------------------------------------------------*/
- /**
- * Returns the registered flag of the remote object.
- *
- * @return The registered flag of the remote object.
- */
- public boolean isRegistered()
- {
- return m_registered;
- }
-
- /**
- * Returns the type of the Instrument. Possible values include
- * InstrumentData.INSTRUMENT_TYPE_COUNTER,
- * InstrumentData.INSTRUMENT_TYPE_VALUE or
- * InstrumentData.INSTRUMENT_TYPE_NONE, if the type was never set.
- *
- * @return The type of the Instrument.
- */
- public int getType()
- {
- return m_type;
- }
-
- /**
- * Returns an array of the Instrument Samples assigned to the Instrument.
- *
- * @return An array of Instrument Samples.
- */
- public InstrumentSampleData[] getInstrumentSamples()
- {
- HTTPInstrumentSampleData[] samples = m_sampleAry;
- if ( samples == null )
- {
- synchronized ( m_samples )
- {
- m_sampleAry = new HTTPInstrumentSampleData[m_samples.size()];
- m_samples.toArray( m_sampleAry );
- samples = m_sampleAry;
- }
- }
- return samples;
- }
-
- /**
- * Requests that a sample be created or that its lease be updated.
- *
- * @param description Description to assign to the new sample.
- * @param interval Sample interval of the new sample.
- * @param sampleCount Number of samples in the new sample.
- * @param leaseTime Requested lease time. The server may not grant the full lease.
- * @param sampleType The type of sample to be created.
- *
- * @return True if successful.
- */
- public boolean createInstrumentSample( String description,
- long interval,
- int sampleCount,
- long leaseTime,
- int sampleType )
- {
- HTTPInstrumentManagerConnection connection =
- (HTTPInstrumentManagerConnection)getConnection();
-
- Configuration configuration = connection.getState(
- "create-sample.xml?name=" + urlEncode( getName() )
- + "&description=" + urlEncode( description ) + "&interval=" + interval
- + "&size=" + sampleCount + "&lease=" + leaseTime + "&type=" + sampleType );
-
- // If there were any errors on the server while creating the sample then null
- // will be returned.
- return configuration != null;
- }
-
- /*---------------------------------------------------------------
- * Methods
- *-------------------------------------------------------------*/
+/*
+ * Copyright 2002-2004 The Apache Software Foundation
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or
+ * implied.
+ *
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+package org.apache.excalibur.instrument.client.http;
+
+import java.util.ArrayList;
+import java.util.HashMap;
+import java.util.Iterator;
+import java.util.List;
+import java.util.Map;
+
+import org.apache.avalon.framework.configuration.Configuration;
+import org.apache.avalon.framework.configuration.ConfigurationException;
+
+import org.apache.excalibur.instrument.client.InstrumentData;
+import org.apache.excalibur.instrument.client.InstrumentSampleData;
+
+class HTTPInstrumentData
+ extends AbstractHTTPElementData
+ implements InstrumentData
+{
+ /* The registered flag of the remote object. */
+ private boolean m_registered;
+
+ /** The type of the Instrument. */
+ private int m_type;
+
+ private List m_samples = new ArrayList();
+ private HTTPInstrumentSampleData[] m_sampleAry;
+ private Map m_sampleMap = new HashMap();
+
+ /*---------------------------------------------------------------
+ * Constructors
+ *-------------------------------------------------------------*/
+ /**
+ * Creates a new HTTPInstrumentData.
+ */
+ HTTPInstrumentData( HTTPInstrumentableData parent,
+ String name )
+ {
+ super( (HTTPInstrumentManagerConnection)parent.getConnection(), parent, name );
+
+ m_registered = false;
+ }
+
+ /*---------------------------------------------------------------
+ * AbstractHTTPElementData Methods
+ *-------------------------------------------------------------*/
+ /**
+ * Update the contents of the object using values from the Configuration object.
+ *
+ * @param configuration Configuration object to load from.
+ * @param recurse True if state should be ignored and we should drill down
+ * using data in this configuration.
+ *
+ * @throws ConfigurationException If there are any problems.
+ */
+ protected void update( Configuration configuration, boolean recurse )
+ throws ConfigurationException
+ {
+ super.update( configuration );
+
+ if ( getLogger().isDebugEnabled() )
+ {
+ getLogger().debug(
+ "Updated Instrument '" + getName() + "' to version " + getStateVersion() );
+ }
+
+ m_registered = configuration.getAttributeAsBoolean( "registered" );
+ m_type = configuration.getAttributeAsInteger( "type" );
+
+ // Samples can be added as well as removed for each update. Build up a list of
+ // old samples and remove each one that still exists on the server. Any left
+ // over have expired and must be removed.
+ Map oldSampleMap;
+ synchronized( m_samples )
+ {
+ oldSampleMap = new HashMap( m_sampleMap );
+ }
+
+ Configuration[] sampleConfs = configuration.getChildren( "sample" );
+ for ( int i = 0; i < sampleConfs.length; i++ )
+ {
+ Configuration sConf = sampleConfs[i];
+ String sName = sConf.getAttribute( "name" );
+ int sStateVersion = sConf.getAttributeAsInteger( "state-version" );
+
+ HTTPInstrumentSampleData sData;
+ synchronized( m_samples )
+ {
+ sData = (HTTPInstrumentSampleData)m_sampleMap.get( sName );
+ if ( sData == null )
+ {
+ // It is new.
+ sData = new HTTPInstrumentSampleData( this, sName );
+ sData.enableLogging( getLogger().getChildLogger( sName ) );
+ m_samples.add( sData );
+ m_sampleMap.put( sName, sData );
+ m_sampleAry = null;
+ }
+ oldSampleMap.remove( sName );
+ }
+
+ if ( recurse )
+ {
+ sData.update( sConf );
+ }
+ else
+ {
+ if ( sStateVersion != sData.getStateVersion() )
+ {
+ // Needs to be updated.
+ sData.update();
+ }
+ }
+ }
+
+ // Purge any old samples.
+ if ( !oldSampleMap.isEmpty() )
+ {
+ synchronized( m_samples )
+ {
+ for ( Iterator iter = oldSampleMap.values().iterator(); iter.hasNext(); )
+ {
+ HTTPInstrumentSampleData sample = (HTTPInstrumentSampleData)iter.next();
+ m_samples.remove( sample );
+ m_sampleMap.remove( sample.getName() );
+ m_sampleAry = null;
+ }
+ }
+ }
+ }
+
+ /**
+ * Causes the InstrumentData to update itself with the latest data from
+ * the server.
+ *
+ * @return true if successful.
+ */
+ public boolean update()
+ {
+ HTTPInstrumentManagerConnection connection =
+ (HTTPInstrumentManagerConnection)getConnection();
+
+ Configuration configuration = connection.getState(
+ "instrument.xml?packed=true&name=" + urlEncode( getName() ) );
+ if ( configuration != null )
+ {
+ try
+ {
+ update( configuration, false );
+
+ return true;
+ }
+ catch ( ConfigurationException e )
+ {
+ getLogger().debug( "Unable to update.", e );
+ }
+ }
+
+ return false;
+ }
+
+ /*---------------------------------------------------------------
+ * InstrumentData Methods
+ *-------------------------------------------------------------*/
+ /**
+ * Returns the registered flag of the remote object.
+ *
+ * @return The registered flag of the remote object.
+ */
+ public boolean isRegistered()
+ {
+ return m_registered;
+ }
+
+ /**
+ * Returns the type of the Instrument. Possible values include
+ * InstrumentData.INSTRUMENT_TYPE_COUNTER,
+ * InstrumentData.INSTRUMENT_TYPE_VALUE or
+ * InstrumentData.INSTRUMENT_TYPE_NONE, if the type was never set.
+ *
+ * @return The type of the Instrument.
+ */
+ public int getType()
+ {
+ return m_type;
+ }
+
+ /**
+ * Returns an array of the Instrument Samples assigned to the Instrument.
+ *
+ * @return An array of Instrument Samples.
+ */
+ public InstrumentSampleData[] getInstrumentSamples()
+ {
+ HTTPInstrumentSampleData[] samples = m_sampleAry;
+ if ( samples == null )
+ {
+ synchronized ( m_samples )
+ {
+ m_sampleAry = new HTTPInstrumentSampleData[m_samples.size()];
+ m_samples.toArray( m_sampleAry );
+ samples = m_sampleAry;
+ }
+ }
+ return samples;
+ }
+
+ /**
+ * Requests that a sample be created or that its lease be updated.
+ *
+ * @param description Description to assign to the new sample.
+ * @param interval Sample interval of the new sample.
+ * @param sampleCount Number of samples in the new sample.
+ * @param leaseTime Requested lease time. The server may not grant the full lease.
+ * @param sampleType The type of sample to be created.
+ *
+ * @return True if successful.
+ */
+ public boolean createInstrumentSample( String description,
+ long interval,
+ int sampleCount,
+ long leaseTime,
+ int sampleType )
+ {
+ HTTPInstrumentManagerConnection connection =
+ (HTTPInstrumentManagerConnection)getConnection();
+
+ Configuration configuration = connection.getState(
+ "create-sample.xml?name=" + urlEncode( getName() )
+ + "&description=" + urlEncode( description ) + "&interval=" + interval
+ + "&size=" + sampleCount + "&lease=" + leaseTime + "&type=" + sampleType );
+
+ // If there were any errors on the server while creating the sample then null
+ // will be returned.
+ return configuration != null;
+ }
+
+ /*---------------------------------------------------------------
+ * Methods
+ *-------------------------------------------------------------*/
}
Propchange: excalibur/trunk/containerkit/instrument/client/src/java/org/apache/excalibur/instrument/client/http/HTTPInstrumentData.java
------------------------------------------------------------------------------
svn:eol-style = native
Modified: excalibur/trunk/containerkit/instrument/client/src/java/org/apache/excalibur/instrument/client/http/HTTPInstrumentManagerConnection.java
URL: http://svn.apache.org/viewvc/excalibur/trunk/containerkit/instrument/client/src/java/org/apache/excalibur/instrument/client/http/HTTPInstrumentManagerConnection.java?view=diff&rev=505577&r1=505576&r2=505577
==============================================================================
--- excalibur/trunk/containerkit/instrument/client/src/java/org/apache/excalibur/instrument/client/http/HTTPInstrumentManagerConnection.java (original)
+++ excalibur/trunk/containerkit/instrument/client/src/java/org/apache/excalibur/instrument/client/http/HTTPInstrumentManagerConnection.java Fri Feb 9 16:09:35 2007
@@ -1,431 +1,431 @@
-/*
- * Copyright 2002-2004 The Apache Software Foundation
- * Licensed under the Apache License, Version 2.0 (the "License");
- * you may not use this file except in compliance with the License.
- * You may obtain a copy of the License at
- *
- * http://www.apache.org/licenses/LICENSE-2.0
- *
- * Unless required by applicable law or agreed to in writing, software
- * distributed under the License is distributed on an "AS IS" BASIS,
- * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or
- * implied.
- *
- * See the License for the specific language governing permissions and
- * limitations under the License.
- */
-
-package org.apache.excalibur.instrument.client.http;
-
-import java.io.InputStream;
-import java.io.IOException;
-import java.io.UnsupportedEncodingException;
-import java.net.HttpURLConnection;
-import java.net.MalformedURLException;
-import java.net.URL;
-import java.net.URLEncoder;
-import java.util.ArrayList;
-import java.util.List;
-
-import org.apache.avalon.framework.configuration.Configuration;
-import org.apache.avalon.framework.configuration.ConfigurationException;
-import org.apache.avalon.framework.configuration.DefaultConfiguration;
-import org.apache.avalon.framework.configuration.DefaultConfigurationBuilder;
-import org.apache.avalon.framework.logger.AbstractLogEnabled;
-import org.apache.avalon.framework.logger.Logger;
-
-import org.apache.excalibur.instrument.client.InstrumentableData;
-import org.apache.excalibur.instrument.client.InstrumentManagerConnection;
-import org.apache.excalibur.instrument.client.InstrumentManagerConnectionListener;
-import org.apache.excalibur.instrument.client.InstrumentManagerData;
-import org.apache.excalibur.instrument.client.InstrumentSampleFrame;
-
-/**
- * A Connection to the remote InstrumentManager which connects using
- * the HTTP connector.
- *
- * @author <a href="mailto:dev@avalon.apache.org">Avalon Development Team</a>
- * @version CVS $Revision: 1.4 $ $Date: 2004/02/28 11:47:23 $
- * @since 4.1
- */
-public class HTTPInstrumentManagerConnection
- extends InstrumentManagerConnection
-{
- private URL m_url;
-
- /** Flag which keeps track of whether or not the remote server was there
- * the last time we attempted to connect. */
- private boolean m_connected;
-
- /** If we ever decide that we are not talking to an Instrument Manager then
- * disable the connection to avoid pounding the remote server with lots
- * of 404 requests. */
- private boolean m_disabled;
-
- private HTTPInstrumentManagerData m_manager;
-
- private List m_leasedSamples = new ArrayList();
- private HTTPInstrumentSampleData[] m_leasedSampleAry;
-
- /*---------------------------------------------------------------
- * Constructors
- *-------------------------------------------------------------*/
- /**
- * Creates a new HTTPInstrumentManagerConnection.
- */
- public HTTPInstrumentManagerConnection( URL url )
- {
- m_url = url;
- m_connected = false;
-
- m_manager = new HTTPInstrumentManagerData( this );
- }
-
- /*---------------------------------------------------------------
- * InstrumentManagerConnection Methods
- *-------------------------------------------------------------*/
- public void enableLogging( Logger logger )
- {
- super.enableLogging( logger );
- m_manager.enableLogging( logger.getChildLogger( "manager" ) );
- }
-
- /**
- * Returns the key used to identify this object.
- *
- * @return The key used to identify this object.
- */
- public Object getKey()
- {
- return m_url;
- }
-
- /**
- * Returns true if connected.
- *
- * @return True if connected.
- */
- public boolean isConnected()
- {
- return m_connected;
- }
-
- /**
- * Returns the Instrument Manager.
- *
- * @return The Instrument Manager.
- */
- public InstrumentManagerData getInstrumentManager()
- {
- return m_manager;
- }
-
- /**
- * Returns the title to display in the tab for the connection.
- *
- * @return The tab title.
- */
- public String getTabTitle()
- {
- if ( m_disabled )
- {
- return "[DISABLED] " + super.getTabTitle();
- }
- else
- {
- return super.getTabTitle();
- }
- }
-
- /**
- * Invokes GC on the JVM running the InstrumentManager.
- */
- protected void invokeGC()
- {
- getState( "gc.xml" );
- }
-
- /**
- * Saves the current state into a Configuration.
- *
- * @return The state as a Configuration.
- */
- public Configuration saveState()
- {
- synchronized( this )
- {
- DefaultConfiguration state = (DefaultConfiguration)super.saveState();
-
- state.setAttribute( "url", m_url.toExternalForm() );
-
- return state;
- }
- }
-
- /**
- * Loads the state from a Configuration object.
- *
- * @param state Configuration object to load state from.
- *
- * @throws ConfigurationException If there were any problems loading the
- * state.
- */
- public void loadState( Configuration state )
- throws ConfigurationException
- {
- synchronized( this )
- {
- super.loadState( state );
-
- // URL will have already been set.
- }
- }
-
- /**
- * URL encode the specified string.
- *
- * @param val String to be URL encoded.
- *
- * @return The URL encoded string.
- */
- String urlEncode( String val )
- {
- try
- {
- return URLEncoder.encode( val, "UTF8" );
- }
- catch ( UnsupportedEncodingException e )
- {
- // Should never happen.
- getLogger().error( "Bad encoding.", e );
- return val;
- }
- }
-
- /**
- * Updates all registered SampleFrames with the latest data from the
- * server. The status of all Sample Frames is also handled by this
- * method, so it must handle disconnected connections and missing or
- * expired samples correctly.
- *
- * This method overrides the default to implement a batch update to
- * get all snapshots from the server in a single request.
- */
- public void updateSampleFrames()
- {
- InstrumentSampleFrame[] frames = getSampleFrameArray();
- if ( frames.length == 0 )
- {
- // Nothing to do.
- return;
- }
-
- // Build up a set of arrays so that all of the snapshots can be requested at once.
- String[] names = new String[frames.length];
- long[] lastTimes = new long[frames.length];
- HTTPInstrumentSampleSnapshotData[] snapshots =
- new HTTPInstrumentSampleSnapshotData[frames.length];
- for ( int i = 0; i < frames.length; i++ )
- {
- InstrumentSampleFrame frame = frames[i];
- names[i] = frame.getInstrumentSampleName();
- lastTimes[i] = frame.getLastSnapshotTime();
- }
-
- // Request the snapshots. Don't bother if we know we are not connected.
- if ( isConnected() )
- {
- StringBuffer sb = new StringBuffer();
- sb.append( "snapshots.xml?packed=true&compact=true" );
- for ( int i = 0; i < frames.length; i++ )
- {
- sb.append( "&name=" );
- sb.append( this.urlEncode( names[i] ) );
- sb.append( "&base-time=" );
- sb.append( lastTimes[i] );
- }
- Configuration configuration = getState( sb.toString() );
- if ( configuration != null )
- {
- Configuration[] snapshotConfs = configuration.getChildren( "sample" );
- for ( int i = 0; i < snapshotConfs.length; i++ )
- {
- Configuration snapshotConf = snapshotConfs[i];
- String name = snapshotConf.getAttribute( "name", null );
- if ( name != null )
- {
- boolean expired = snapshotConf.getAttributeAsBoolean( "expired", false );
- if ( !expired )
- {
- // Look for the specified sample frame. Should always exist.
- for ( int j = 0; j < frames.length; j++ )
- {
- if ( name.equals( names[j] ) )
- {
- snapshots[j] =
- new HTTPInstrumentSampleSnapshotData( this, name );
- snapshots[j].enableLogging( getLogger() );
- try
- {
- snapshots[j].update( snapshotConf );
- }
- catch ( ConfigurationException e )
- {
- // Should not happen.
- getLogger().info( "Snapshot update failed.", e );
- getLogger().info( " URL: " + sb.toString() );
- getLogger().info( " i:" + i + " j:" + j );
- snapshots[j] = null;
- }
- break;
- }
- }
- }
- }
- }
- }
- }
-
- // Now we should have all available snapshots. Loop back over the frames
- // and update them as is appropriate.
- for ( int i = 0; i < frames.length; i++ )
- {
- InstrumentSampleFrame frame = frames[i];
- frame.updateSnapshot( snapshots[i] );
- }
- }
-
- /*---------------------------------------------------------------
- * Methods
- *-------------------------------------------------------------*/
- /**
- * Returns the URL of the remote InstrumentManager.
- *
- * @return The URL of the remote InstrumentManager.
- */
- URL getURL()
- {
- return m_url;
- }
-
- /**
- * Requests the current state of the object at the specified path.
- * If the request fails for any reason, including not being valid
- * content then the method will return null.
- *
- * @param path The path of the object whose state is requested.
- *
- * @return The state as a Configuration object, or null if it failed.
- */
- Configuration getState( String path )
- {
- if ( m_disabled )
- {
- return null;
- }
-
- URL url;
- try
- {
- url = new URL( m_url, path );
- }
- catch ( MalformedURLException e )
- {
- getLogger().debug( "Request failed.", e );
- return null;
- }
-
- try
- {
- HttpURLConnection conn = (HttpURLConnection)url.openConnection();
-
- if ( conn.getResponseCode() == conn.HTTP_OK )
- {
- boolean oldConnected = m_connected;
- m_connected = true;
- if ( !oldConnected )
- {
- // Notify the listeners.
- InstrumentManagerConnectionListener[] listenerArray = getListenerArray();
- for ( int i = 0; i < listenerArray.length; i++ )
- {
- listenerArray[i].opened( this );
- }
- }
-
- InputStream is = conn.getInputStream();
- try
- {
- DefaultConfigurationBuilder builder = new DefaultConfigurationBuilder();
- try
- {
- return builder.build( is );
- }
- catch ( ConfigurationException e )
- {
- getLogger().warn( "Invalid XML reveived from the server.", e );
- return null;
- }
- catch ( org.xml.sax.SAXException e )
- {
- getLogger().warn( "Invalid XML reveived from the server.", e );
- return null;
- }
- }
- finally
- {
- is.close();
- }
- }
- else
- {
- if ( ( conn.getResponseCode() == 404 )
- && path.startsWith( "instrument-manager.xml" ) )
- {
- getLogger().warn( "Requested " + url + " resulted in error code 404. "
- + "Most likely not an Instrument Manager, disabling future requests." );
- m_disabled = true;
- }
- else
- {
- getLogger().debug( "Response: " + conn.getResponseCode() + " : "
- + conn.getResponseMessage() );
- }
- return null;
- }
- }
- catch ( IOException e )
- {
- String msg = e.getMessage();
- if ( msg == null )
- {
- msg = e.toString();
- }
-
- if ( msg.indexOf( "Connect" ) >= 0 )
- {
- // Hide the stack trace as the server is simply down.
- getLogger().debug( "Request failed. URL: " + url + " Error: " + msg );
- }
- else
- {
- getLogger().debug( "Request failed. URL: " + url + " Error: ", e );
- }
-
-
- boolean oldConnected = m_connected;
- m_connected = false;
- if ( oldConnected )
- {
- // Notify the listeners.
- InstrumentManagerConnectionListener[] listenerArray = getListenerArray();
- for ( int i = 0; i < listenerArray.length; i++ )
- {
- listenerArray[i].closed( this );
- }
- }
-
- return null;
- }
- }
+/*
+ * Copyright 2002-2004 The Apache Software Foundation
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or
+ * implied.
+ *
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+package org.apache.excalibur.instrument.client.http;
+
+import java.io.InputStream;
+import java.io.IOException;
+import java.io.UnsupportedEncodingException;
+import java.net.HttpURLConnection;
+import java.net.MalformedURLException;
+import java.net.URL;
+import java.net.URLEncoder;
+import java.util.ArrayList;
+import java.util.List;
+
+import org.apache.avalon.framework.configuration.Configuration;
+import org.apache.avalon.framework.configuration.ConfigurationException;
+import org.apache.avalon.framework.configuration.DefaultConfiguration;
+import org.apache.avalon.framework.configuration.DefaultConfigurationBuilder;
+import org.apache.avalon.framework.logger.AbstractLogEnabled;
+import org.apache.avalon.framework.logger.Logger;
+
+import org.apache.excalibur.instrument.client.InstrumentableData;
+import org.apache.excalibur.instrument.client.InstrumentManagerConnection;
+import org.apache.excalibur.instrument.client.InstrumentManagerConnectionListener;
+import org.apache.excalibur.instrument.client.InstrumentManagerData;
+import org.apache.excalibur.instrument.client.InstrumentSampleFrame;
+
+/**
+ * A Connection to the remote InstrumentManager which connects using
+ * the HTTP connector.
+ *
+ * @author <a href="mailto:dev@avalon.apache.org">Avalon Development Team</a>
+ * @version CVS $Revision: 1.4 $ $Date: 2004/02/28 11:47:23 $
+ * @since 4.1
+ */
+public class HTTPInstrumentManagerConnection
+ extends InstrumentManagerConnection
+{
+ private URL m_url;
+
+ /** Flag which keeps track of whether or not the remote server was there
+ * the last time we attempted to connect. */
+ private boolean m_connected;
+
+ /** If we ever decide that we are not talking to an Instrument Manager then
+ * disable the connection to avoid pounding the remote server with lots
+ * of 404 requests. */
+ private boolean m_disabled;
+
+ private HTTPInstrumentManagerData m_manager;
+
+ private List m_leasedSamples = new ArrayList();
+ private HTTPInstrumentSampleData[] m_leasedSampleAry;
+
+ /*---------------------------------------------------------------
+ * Constructors
+ *-------------------------------------------------------------*/
+ /**
+ * Creates a new HTTPInstrumentManagerConnection.
+ */
+ public HTTPInstrumentManagerConnection( URL url )
+ {
+ m_url = url;
+ m_connected = false;
+
+ m_manager = new HTTPInstrumentManagerData( this );
+ }
+
+ /*---------------------------------------------------------------
+ * InstrumentManagerConnection Methods
+ *-------------------------------------------------------------*/
+ public void enableLogging( Logger logger )
+ {
+ super.enableLogging( logger );
+ m_manager.enableLogging( logger.getChildLogger( "manager" ) );
+ }
+
+ /**
+ * Returns the key used to identify this object.
+ *
+ * @return The key used to identify this object.
+ */
+ public Object getKey()
+ {
+ return m_url;
+ }
+
+ /**
+ * Returns true if connected.
+ *
+ * @return True if connected.
+ */
+ public boolean isConnected()
+ {
+ return m_connected;
+ }
+
+ /**
+ * Returns the Instrument Manager.
+ *
+ * @return The Instrument Manager.
+ */
+ public InstrumentManagerData getInstrumentManager()
+ {
+ return m_manager;
+ }
+
+ /**
+ * Returns the title to display in the tab for the connection.
+ *
+ * @return The tab title.
+ */
+ public String getTabTitle()
+ {
+ if ( m_disabled )
+ {
+ return "[DISABLED] " + super.getTabTitle();
+ }
+ else
+ {
+ return super.getTabTitle();
+ }
+ }
+
+ /**
+ * Invokes GC on the JVM running the InstrumentManager.
+ */
+ protected void invokeGC()
+ {
+ getState( "gc.xml" );
+ }
+
+ /**
+ * Saves the current state into a Configuration.
+ *
+ * @return The state as a Configuration.
+ */
+ public Configuration saveState()
+ {
+ synchronized( this )
+ {
+ DefaultConfiguration state = (DefaultConfiguration)super.saveState();
+
+ state.setAttribute( "url", m_url.toExternalForm() );
+
+ return state;
+ }
+ }
+
+ /**
+ * Loads the state from a Configuration object.
+ *
+ * @param state Configuration object to load state from.
+ *
+ * @throws ConfigurationException If there were any problems loading the
+ * state.
+ */
+ public void loadState( Configuration state )
+ throws ConfigurationException
+ {
+ synchronized( this )
+ {
+ super.loadState( state );
+
+ // URL will have already been set.
+ }
+ }
+
+ /**
+ * URL encode the specified string.
+ *
+ * @param val String to be URL encoded.
+ *
+ * @return The URL encoded string.
+ */
+ String urlEncode( String val )
+ {
+ try
+ {
+ return URLEncoder.encode( val, "UTF8" );
+ }
+ catch ( UnsupportedEncodingException e )
+ {
+ // Should never happen.
+ getLogger().error( "Bad encoding.", e );
+ return val;
+ }
+ }
+
+ /**
+ * Updates all registered SampleFrames with the latest data from the
+ * server. The status of all Sample Frames is also handled by this
+ * method, so it must handle disconnected connections and missing or
+ * expired samples correctly.
+ *
+ * This method overrides the default to implement a batch update to
+ * get all snapshots from the server in a single request.
+ */
+ public void updateSampleFrames()
+ {
+ InstrumentSampleFrame[] frames = getSampleFrameArray();
+ if ( frames.length == 0 )
+ {
+ // Nothing to do.
+ return;
+ }
+
+ // Build up a set of arrays so that all of the snapshots can be requested at once.
+ String[] names = new String[frames.length];
+ long[] lastTimes = new long[frames.length];
+ HTTPInstrumentSampleSnapshotData[] snapshots =
+ new HTTPInstrumentSampleSnapshotData[frames.length];
+ for ( int i = 0; i < frames.length; i++ )
+ {
+ InstrumentSampleFrame frame = frames[i];
+ names[i] = frame.getInstrumentSampleName();
+ lastTimes[i] = frame.getLastSnapshotTime();
+ }
+
+ // Request the snapshots. Don't bother if we know we are not connected.
+ if ( isConnected() )
+ {
+ StringBuffer sb = new StringBuffer();
+ sb.append( "snapshots.xml?packed=true&compact=true" );
+ for ( int i = 0; i < frames.length; i++ )
+ {
+ sb.append( "&name=" );
+ sb.append( this.urlEncode( names[i] ) );
+ sb.append( "&base-time=" );
+ sb.append( lastTimes[i] );
+ }
+ Configuration configuration = getState( sb.toString() );
+ if ( configuration != null )
+ {
+ Configuration[] snapshotConfs = configuration.getChildren( "sample" );
+ for ( int i = 0; i < snapshotConfs.length; i++ )
+ {
+ Configuration snapshotConf = snapshotConfs[i];
+ String name = snapshotConf.getAttribute( "name", null );
+ if ( name != null )
+ {
+ boolean expired = snapshotConf.getAttributeAsBoolean( "expired", false );
+ if ( !expired )
+ {
+ // Look for the specified sample frame. Should always exist.
+ for ( int j = 0; j < frames.length; j++ )
+ {
+ if ( name.equals( names[j] ) )
+ {
+ snapshots[j] =
+ new HTTPInstrumentSampleSnapshotData( this, name );
+ snapshots[j].enableLogging( getLogger() );
+ try
+ {
+ snapshots[j].update( snapshotConf );
+ }
+ catch ( ConfigurationException e )
+ {
+ // Should not happen.
+ getLogger().info( "Snapshot update failed.", e );
+ getLogger().info( " URL: " + sb.toString() );
+ getLogger().info( " i:" + i + " j:" + j );
+ snapshots[j] = null;
+ }
+ break;
+ }
+ }
+ }
+ }
+ }
+ }
+ }
+
+ // Now we should have all available snapshots. Loop back over the frames
+ // and update them as is appropriate.
+ for ( int i = 0; i < frames.length; i++ )
+ {
+ InstrumentSampleFrame frame = frames[i];
+ frame.updateSnapshot( snapshots[i] );
+ }
+ }
+
+ /*---------------------------------------------------------------
+ * Methods
+ *-------------------------------------------------------------*/
+ /**
+ * Returns the URL of the remote InstrumentManager.
+ *
+ * @return The URL of the remote InstrumentManager.
+ */
+ URL getURL()
+ {
+ return m_url;
+ }
+
+ /**
+ * Requests the current state of the object at the specified path.
+ * If the request fails for any reason, including not being valid
+ * content then the method will return null.
+ *
+ * @param path The path of the object whose state is requested.
+ *
+ * @return The state as a Configuration object, or null if it failed.
+ */
+ Configuration getState( String path )
+ {
+ if ( m_disabled )
+ {
+ return null;
+ }
+
+ URL url;
+ try
+ {
+ url = new URL( m_url, path );
+ }
+ catch ( MalformedURLException e )
+ {
+ getLogger().debug( "Request failed.", e );
+ return null;
+ }
+
+ try
+ {
+ HttpURLConnection conn = (HttpURLConnection)url.openConnection();
+
+ if ( conn.getResponseCode() == conn.HTTP_OK )
+ {
+ boolean oldConnected = m_connected;
+ m_connected = true;
+ if ( !oldConnected )
+ {
+ // Notify the listeners.
+ InstrumentManagerConnectionListener[] listenerArray = getListenerArray();
+ for ( int i = 0; i < listenerArray.length; i++ )
+ {
+ listenerArray[i].opened( this );
+ }
+ }
+
+ InputStream is = conn.getInputStream();
+ try
+ {
+ DefaultConfigurationBuilder builder = new DefaultConfigurationBuilder();
+ try
+ {
+ return builder.build( is );
+ }
+ catch ( ConfigurationException e )
+ {
+ getLogger().warn( "Invalid XML reveived from the server.", e );
+ return null;
+ }
+ catch ( org.xml.sax.SAXException e )
+ {
+ getLogger().warn( "Invalid XML reveived from the server.", e );
+ return null;
+ }
+ }
+ finally
+ {
+ is.close();
+ }
+ }
+ else
+ {
+ if ( ( conn.getResponseCode() == 404 )
+ && path.startsWith( "instrument-manager.xml" ) )
+ {
+ getLogger().warn( "Requested " + url + " resulted in error code 404. "
+ + "Most likely not an Instrument Manager, disabling future requests." );
+ m_disabled = true;
+ }
+ else
+ {
+ getLogger().debug( "Response: " + conn.getResponseCode() + " : "
+ + conn.getResponseMessage() );
+ }
+ return null;
+ }
+ }
+ catch ( IOException e )
+ {
+ String msg = e.getMessage();
+ if ( msg == null )
+ {
+ msg = e.toString();
+ }
+
+ if ( msg.indexOf( "Connect" ) >= 0 )
+ {
+ // Hide the stack trace as the server is simply down.
+ getLogger().debug( "Request failed. URL: " + url + " Error: " + msg );
+ }
+ else
+ {
+ getLogger().debug( "Request failed. URL: " + url + " Error: ", e );
+ }
+
+
+ boolean oldConnected = m_connected;
+ m_connected = false;
+ if ( oldConnected )
+ {
+ // Notify the listeners.
+ InstrumentManagerConnectionListener[] listenerArray = getListenerArray();
+ for ( int i = 0; i < listenerArray.length; i++ )
+ {
+ listenerArray[i].closed( this );
+ }
+ }
+
+ return null;
+ }
+ }
}
Propchange: excalibur/trunk/containerkit/instrument/client/src/java/org/apache/excalibur/instrument/client/http/HTTPInstrumentManagerConnection.java
------------------------------------------------------------------------------
svn:eol-style = native
Modified: excalibur/trunk/containerkit/instrument/client/src/java/org/apache/excalibur/instrument/client/http/HTTPInstrumentManagerData.java
URL: http://svn.apache.org/viewvc/excalibur/trunk/containerkit/instrument/client/src/java/org/apache/excalibur/instrument/client/http/HTTPInstrumentManagerData.java?view=diff&rev=505577&r1=505576&r2=505577
==============================================================================
--- excalibur/trunk/containerkit/instrument/client/src/java/org/apache/excalibur/instrument/client/http/HTTPInstrumentManagerData.java (original)
+++ excalibur/trunk/containerkit/instrument/client/src/java/org/apache/excalibur/instrument/client/http/HTTPInstrumentManagerData.java Fri Feb 9 16:09:35 2007
@@ -1,349 +1,349 @@
-/*
- * Copyright 2002-2004 The Apache Software Foundation
- * Licensed under the Apache License, Version 2.0 (the "License");
- * you may not use this file except in compliance with the License.
- * You may obtain a copy of the License at
- *
- * http://www.apache.org/licenses/LICENSE-2.0
- *
- * Unless required by applicable law or agreed to in writing, software
- * distributed under the License is distributed on an "AS IS" BASIS,
- * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or
- * implied.
- *
- * See the License for the specific language governing permissions and
- * limitations under the License.
- */
-
-package org.apache.excalibur.instrument.client.http;
-
-import java.util.ArrayList;
-import java.util.HashMap;
-import java.util.List;
-import java.util.Map;
-
-import org.apache.avalon.framework.configuration.Configuration;
-import org.apache.avalon.framework.configuration.ConfigurationException;
-
-import org.apache.excalibur.instrument.client.InstrumentableData;
-import org.apache.excalibur.instrument.client.InstrumentManagerData;
-
-class HTTPInstrumentManagerData
- extends AbstractHTTPData
- implements InstrumentManagerData
-{
- /* Name of the remote object. */
- private String m_name;
-
- /* Flag which keeps track of whether the manager supports batched lease updates. */
- private boolean m_batchedUpdates;
-
- /* Flag which keeps track of whether the manager is read only or not. */
- private boolean m_readOnly;
-
- private List m_instrumentables = new ArrayList();
- private HTTPInstrumentableData[] m_instrumentableAry;
- private Map m_instrumentableMap = new HashMap();
-
- /*---------------------------------------------------------------
- * Constructors
- *-------------------------------------------------------------*/
- /**
- * Creates a new HTTPInstrumentManagerData.
- */
- HTTPInstrumentManagerData( HTTPInstrumentManagerConnection connection )
- {
- super( connection, connection.getURL().toExternalForm() );
-
- m_name = connection.getURL().toExternalForm();
- }
-
- /*---------------------------------------------------------------
- * AbstractHTTPData Methods
- *-------------------------------------------------------------*/
- /**
- * Update the contents of the object using values from the Configuration object.
- *
- * @param configuration Configuration object to load from.
- * @param recurse True if state should be ignored and we should drill down
- * using data in this configuration.
- *
- * @throws ConfigurationException If there are any problems.
- */
- protected void update( Configuration configuration, boolean recurse )
- throws ConfigurationException
- {
- super.update( configuration );
-
- m_name = configuration.getAttribute( "name" );
-
- // Support for batched lease creates and renewals added in version 1.2.
- m_batchedUpdates = configuration.getAttributeAsBoolean( "batched-updates", false );
-
- // read-only attribute added in version 1.2.
- m_readOnly = configuration.getAttributeAsBoolean( "read-only", false );
-
- if ( getLogger().isDebugEnabled() )
- {
- getLogger().debug(
- "Updated InstrumentManager '" + getName() + "' to version " + getStateVersion() );
- }
-
- Configuration[] instrumentableConfs = configuration.getChildren( "instrumentable" );
- for ( int i = 0; i < instrumentableConfs.length; i++ )
- {
- Configuration iaConf = instrumentableConfs[i];
- String iaName = iaConf.getAttribute( "name" );
- int iaStateVersion = iaConf.getAttributeAsInteger( "state-version" );
-
- HTTPInstrumentableData iaData;
- synchronized ( m_instrumentables )
- {
- iaData = (HTTPInstrumentableData)m_instrumentableMap.get( iaName );
- if ( iaData == null )
- {
- // It is new.
- iaData = new HTTPInstrumentableData( this, iaName );
- iaData.enableLogging( getLogger().getChildLogger( iaName ) );
- m_instrumentables.add( iaData );
- m_instrumentableAry = null;
- m_instrumentableMap.put( iaName, iaData );
- }
- }
-
- if ( recurse )
- {
- iaData.update( iaConf, recurse );
- }
- else
- {
- if ( iaStateVersion != iaData.getStateVersion() )
- {
- // Needs to be updated.
- iaData.update();
- }
- }
- }
- }
-
- /**
- * Causes the InstrumentManagerData to update itself with the latest data
- * from the server.
- *
- * @return true if successful.
- */
- public boolean update()
- {
- HTTPInstrumentManagerConnection connection =
- (HTTPInstrumentManagerConnection)getConnection();
-
- Configuration configuration = connection.getState( "instrument-manager.xml?packed=true" );
- if ( configuration != null )
- {
- try
- {
- update( configuration, false );
-
- //updateLeasedSamples();
-
- return true;
- }
- catch ( ConfigurationException e )
- {
- getLogger().debug( "Unable to update.", e );
- }
- }
- return false;
- }
-
- /*---------------------------------------------------------------
- * InstrumentManagerData Methods
- *-------------------------------------------------------------*/
- /**
- * Returns the name.
- *
- * @return The name.
- */
- public String getName()
- {
- return m_name;
- }
-
- /**
- * Returns true if the InstrumentManager on the server is operating in
- * read-only mode.
- *
- * @return True if read-only.
- *
- * @since 1.2
- */
- public boolean isReadOnly()
- {
- return m_readOnly;
- }
-
- /**
- * Returns true if batched lease creates and renewals are implemented on
- * the server.
- *
- * @return True if read-only.
- *
- * @since 1.2
- */
- private boolean isSupportsBatchedUpdates()
- {
- return m_batchedUpdates;
- }
-
- /**
- * Gets a thread-safe snapshot of the instrumentable list.
- *
- * @return A thread-safe snapshot of the instrumentable list.
- */
- public InstrumentableData[] getInstrumentables()
- {
- HTTPInstrumentableData[] instrumentables = m_instrumentableAry;
- if ( instrumentables == null )
- {
- synchronized ( m_instrumentables )
- {
- m_instrumentableAry = new HTTPInstrumentableData[m_instrumentables.size()];
- m_instrumentables.toArray( m_instrumentableAry );
- instrumentables = m_instrumentableAry;
- }
- }
- return instrumentables;
- }
-
- /**
- * Causes the the entire instrument tree to be updated in one call. Very fast
- * when it is known that all or most data has changed.
- *
- * @return true if successful.
- */
- public boolean updateAll()
- {
- HTTPInstrumentManagerConnection connection =
- (HTTPInstrumentManagerConnection)getConnection();
-
- Configuration configuration =
- connection.getState( "instrument-manager.xml?packed=true&recurse=true" );
- if ( configuration != null )
- {
- try
- {
- update( configuration, true );
-
- //updateLeasedSamples();
-
- return true;
- }
- catch ( ConfigurationException e )
- {
- getLogger().debug( "Unable to update.", e );
- }
- }
- return false;
- }
-
- /**
- * Requests that a sample be created or that its lease be updated.
- *
- * @param instrumentName The full name of the instrument whose sample is
- * to be created or updated.
- * @param description Description to assign to the new sample.
- * @param interval Sample interval of the new sample.
- * @param sampleCount Number of samples in the new sample.
- * @param leaseTime Requested lease time. The server may not grant the
- * full lease.
- * @param sampleType The type of sample to be created.
- */
- public void createInstrumentSample( String instrumentName,
- String description,
- long interval,
- int sampleCount,
- long leaseTime,
- int sampleType )
- {
- HTTPInstrumentManagerConnection connection =
- (HTTPInstrumentManagerConnection)getConnection();
-
- connection.getState( "create-sample.xml?name=" + urlEncode( instrumentName )
- + "&description=" + urlEncode( description ) + "&interval=" + interval
- + "&size=" + sampleCount + "&lease=" + leaseTime + "&type=" + sampleType );
- }
-
- /**
- * Requests that a set of samples be created or that their leases be
- * updated. All array parameters must be of the same length.
- *
- * @param instrumentNames The full names of the instruments whose sample
- * are to be created or updated.
- * @param descriptions Descriptions to assign to the new samples.
- * @param intervals Sample intervals of the new samples.
- * @param sampleCounts Number of samples in each the new samples.
- * @param leaseTimes Requested lease times. The server may not grant the
- * full leases.
- * @param sampleTypes The types of samples to be created.
- */
- public void createInstrumentSamples( String[] instrumentNames,
- String[] descriptions,
- long[] intervals,
- int[] sampleCounts,
- long[] leaseTimes,
- int[] sampleTypes )
- {
- HTTPInstrumentManagerConnection connection =
- (HTTPInstrumentManagerConnection)getConnection();
-
- // Validate the arguments to avoid errors from misuse.
- if ( ( instrumentNames.length != descriptions.length )
- || ( instrumentNames.length != intervals.length )
- || ( instrumentNames.length != sampleCounts.length )
- || ( instrumentNames.length != leaseTimes.length )
- || ( instrumentNames.length != sampleTypes.length ) )
- {
- throw new IllegalArgumentException( "Array lengths of all parameters must be equal." );
- }
-
- // If batched updates are not supported, then do them individually
- if ( isSupportsBatchedUpdates() )
- {
- StringBuffer sb = new StringBuffer();
- sb.append( "create-samples.xml?" );
- for ( int i = 0; i < instrumentNames.length; i++ )
- {
- if ( i > 0 )
- {
- sb.append( "&" );
- }
- sb.append( "name=" );
- sb.append( urlEncode( instrumentNames[i] ) );
- sb.append( "&description=" );
- sb.append( urlEncode( descriptions[i] ) );
- sb.append( "&interval=" );
- sb.append( intervals[i] );
- sb.append( "&size=" );
- sb.append( sampleCounts[i] );
- sb.append( "&lease=" );
- sb.append( leaseTimes[i] );
- sb.append( "&type=" );
- sb.append( sampleTypes[i] );
- }
-
- connection.getState( sb.toString() );
- }
- else
- {
- for ( int i = 0; i < instrumentNames.length; i++ )
- {
- createInstrumentSample( instrumentNames[i], descriptions[i], intervals[i],
- sampleCounts[i], leaseTimes[i], sampleTypes[i] );
- }
- }
- }
-
- /*---------------------------------------------------------------
- * Methods
- *-------------------------------------------------------------*/
+/*
+ * Copyright 2002-2004 The Apache Software Foundation
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or
+ * implied.
+ *
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+package org.apache.excalibur.instrument.client.http;
+
+import java.util.ArrayList;
+import java.util.HashMap;
+import java.util.List;
+import java.util.Map;
+
+import org.apache.avalon.framework.configuration.Configuration;
+import org.apache.avalon.framework.configuration.ConfigurationException;
+
+import org.apache.excalibur.instrument.client.InstrumentableData;
+import org.apache.excalibur.instrument.client.InstrumentManagerData;
+
+class HTTPInstrumentManagerData
+ extends AbstractHTTPData
+ implements InstrumentManagerData
+{
+ /* Name of the remote object. */
+ private String m_name;
+
+ /* Flag which keeps track of whether the manager supports batched lease updates. */
+ private boolean m_batchedUpdates;
+
+ /* Flag which keeps track of whether the manager is read only or not. */
+ private boolean m_readOnly;
+
+ private List m_instrumentables = new ArrayList();
+ private HTTPInstrumentableData[] m_instrumentableAry;
+ private Map m_instrumentableMap = new HashMap();
+
+ /*---------------------------------------------------------------
+ * Constructors
+ *-------------------------------------------------------------*/
+ /**
+ * Creates a new HTTPInstrumentManagerData.
+ */
+ HTTPInstrumentManagerData( HTTPInstrumentManagerConnection connection )
+ {
+ super( connection, connection.getURL().toExternalForm() );
+
+ m_name = connection.getURL().toExternalForm();
+ }
+
+ /*---------------------------------------------------------------
+ * AbstractHTTPData Methods
+ *-------------------------------------------------------------*/
+ /**
+ * Update the contents of the object using values from the Configuration object.
+ *
+ * @param configuration Configuration object to load from.
+ * @param recurse True if state should be ignored and we should drill down
+ * using data in this configuration.
+ *
+ * @throws ConfigurationException If there are any problems.
+ */
+ protected void update( Configuration configuration, boolean recurse )
+ throws ConfigurationException
+ {
+ super.update( configuration );
+
+ m_name = configuration.getAttribute( "name" );
+
+ // Support for batched lease creates and renewals added in version 1.2.
+ m_batchedUpdates = configuration.getAttributeAsBoolean( "batched-updates", false );
+
+ // read-only attribute added in version 1.2.
+ m_readOnly = configuration.getAttributeAsBoolean( "read-only", false );
+
+ if ( getLogger().isDebugEnabled() )
+ {
+ getLogger().debug(
+ "Updated InstrumentManager '" + getName() + "' to version " + getStateVersion() );
+ }
+
+ Configuration[] instrumentableConfs = configuration.getChildren( "instrumentable" );
+ for ( int i = 0; i < instrumentableConfs.length; i++ )
+ {
+ Configuration iaConf = instrumentableConfs[i];
+ String iaName = iaConf.getAttribute( "name" );
+ int iaStateVersion = iaConf.getAttributeAsInteger( "state-version" );
+
+ HTTPInstrumentableData iaData;
+ synchronized ( m_instrumentables )
+ {
+ iaData = (HTTPInstrumentableData)m_instrumentableMap.get( iaName );
+ if ( iaData == null )
+ {
+ // It is new.
+ iaData = new HTTPInstrumentableData( this, iaName );
+ iaData.enableLogging( getLogger().getChildLogger( iaName ) );
+ m_instrumentables.add( iaData );
+ m_instrumentableAry = null;
+ m_instrumentableMap.put( iaName, iaData );
+ }
+ }
+
+ if ( recurse )
+ {
+ iaData.update( iaConf, recurse );
+ }
+ else
+ {
+ if ( iaStateVersion != iaData.getStateVersion() )
+ {
+ // Needs to be updated.
+ iaData.update();
+ }
+ }
+ }
+ }
+
+ /**
+ * Causes the InstrumentManagerData to update itself with the latest data
+ * from the server.
+ *
+ * @return true if successful.
+ */
+ public boolean update()
+ {
+ HTTPInstrumentManagerConnection connection =
+ (HTTPInstrumentManagerConnection)getConnection();
+
+ Configuration configuration = connection.getState( "instrument-manager.xml?packed=true" );
+ if ( configuration != null )
+ {
+ try
+ {
+ update( configuration, false );
+
+ //updateLeasedSamples();
+
+ return true;
+ }
+ catch ( ConfigurationException e )
+ {
+ getLogger().debug( "Unable to update.", e );
+ }
+ }
+ return false;
+ }
+
+ /*---------------------------------------------------------------
+ * InstrumentManagerData Methods
+ *-------------------------------------------------------------*/
+ /**
+ * Returns the name.
+ *
+ * @return The name.
+ */
+ public String getName()
+ {
+ return m_name;
+ }
+
+ /**
+ * Returns true if the InstrumentManager on the server is operating in
+ * read-only mode.
+ *
+ * @return True if read-only.
+ *
+ * @since 1.2
+ */
+ public boolean isReadOnly()
+ {
+ return m_readOnly;
+ }
+
+ /**
+ * Returns true if batched lease creates and renewals are implemented on
+ * the server.
+ *
+ * @return True if read-only.
+ *
+ * @since 1.2
+ */
+ private boolean isSupportsBatchedUpdates()
+ {
+ return m_batchedUpdates;
+ }
+
+ /**
+ * Gets a thread-safe snapshot of the instrumentable list.
+ *
+ * @return A thread-safe snapshot of the instrumentable list.
+ */
+ public InstrumentableData[] getInstrumentables()
+ {
+ HTTPInstrumentableData[] instrumentables = m_instrumentableAry;
+ if ( instrumentables == null )
+ {
+ synchronized ( m_instrumentables )
+ {
+ m_instrumentableAry = new HTTPInstrumentableData[m_instrumentables.size()];
+ m_instrumentables.toArray( m_instrumentableAry );
+ instrumentables = m_instrumentableAry;
+ }
+ }
+ return instrumentables;
+ }
+
+ /**
+ * Causes the the entire instrument tree to be updated in one call. Very fast
+ * when it is known that all or most data has changed.
+ *
+ * @return true if successful.
+ */
+ public boolean updateAll()
+ {
+ HTTPInstrumentManagerConnection connection =
+ (HTTPInstrumentManagerConnection)getConnection();
+
+ Configuration configuration =
+ connection.getState( "instrument-manager.xml?packed=true&recurse=true" );
+ if ( configuration != null )
+ {
+ try
+ {
+ update( configuration, true );
+
+ //updateLeasedSamples();
+
+ return true;
+ }
+ catch ( ConfigurationException e )
+ {
+ getLogger().debug( "Unable to update.", e );
+ }
+ }
+ return false;
+ }
+
+ /**
+ * Requests that a sample be created or that its lease be updated.
+ *
+ * @param instrumentName The full name of the instrument whose sample is
+ * to be created or updated.
+ * @param description Description to assign to the new sample.
+ * @param interval Sample interval of the new sample.
+ * @param sampleCount Number of samples in the new sample.
+ * @param leaseTime Requested lease time. The server may not grant the
+ * full lease.
+ * @param sampleType The type of sample to be created.
+ */
+ public void createInstrumentSample( String instrumentName,
+ String description,
+ long interval,
+ int sampleCount,
+ long leaseTime,
+ int sampleType )
+ {
+ HTTPInstrumentManagerConnection connection =
+ (HTTPInstrumentManagerConnection)getConnection();
+
+ connection.getState( "create-sample.xml?name=" + urlEncode( instrumentName )
+ + "&description=" + urlEncode( description ) + "&interval=" + interval
+ + "&size=" + sampleCount + "&lease=" + leaseTime + "&type=" + sampleType );
+ }
+
+ /**
+ * Requests that a set of samples be created or that their leases be
+ * updated. All array parameters must be of the same length.
+ *
+ * @param instrumentNames The full names of the instruments whose sample
+ * are to be created or updated.
+ * @param descriptions Descriptions to assign to the new samples.
+ * @param intervals Sample intervals of the new samples.
+ * @param sampleCounts Number of samples in each the new samples.
+ * @param leaseTimes Requested lease times. The server may not grant the
+ * full leases.
+ * @param sampleTypes The types of samples to be created.
+ */
+ public void createInstrumentSamples( String[] instrumentNames,
+ String[] descriptions,
+ long[] intervals,
+ int[] sampleCounts,
+ long[] leaseTimes,
+ int[] sampleTypes )
+ {
+ HTTPInstrumentManagerConnection connection =
+ (HTTPInstrumentManagerConnection)getConnection();
+
+ // Validate the arguments to avoid errors from misuse.
+ if ( ( instrumentNames.length != descriptions.length )
+ || ( instrumentNames.length != intervals.length )
+ || ( instrumentNames.length != sampleCounts.length )
+ || ( instrumentNames.length != leaseTimes.length )
+ || ( instrumentNames.length != sampleTypes.length ) )
+ {
+ throw new IllegalArgumentException( "Array lengths of all parameters must be equal." );
+ }
+
+ // If batched updates are not supported, then do them individually
+ if ( isSupportsBatchedUpdates() )
+ {
+ StringBuffer sb = new StringBuffer();
+ sb.append( "create-samples.xml?" );
+ for ( int i = 0; i < instrumentNames.length; i++ )
+ {
+ if ( i > 0 )
+ {
+ sb.append( "&" );
+ }
+ sb.append( "name=" );
+ sb.append( urlEncode( instrumentNames[i] ) );
+ sb.append( "&description=" );
+ sb.append( urlEncode( descriptions[i] ) );
+ sb.append( "&interval=" );
+ sb.append( intervals[i] );
+ sb.append( "&size=" );
+ sb.append( sampleCounts[i] );
+ sb.append( "&lease=" );
+ sb.append( leaseTimes[i] );
+ sb.append( "&type=" );
+ sb.append( sampleTypes[i] );
+ }
+
+ connection.getState( sb.toString() );
+ }
+ else
+ {
+ for ( int i = 0; i < instrumentNames.length; i++ )
+ {
+ createInstrumentSample( instrumentNames[i], descriptions[i], intervals[i],
+ sampleCounts[i], leaseTimes[i], sampleTypes[i] );
+ }
+ }
+ }
+
+ /*---------------------------------------------------------------
+ * Methods
+ *-------------------------------------------------------------*/
}
Propchange: excalibur/trunk/containerkit/instrument/client/src/java/org/apache/excalibur/instrument/client/http/HTTPInstrumentManagerData.java
------------------------------------------------------------------------------
svn:eol-style = native
Modified: excalibur/trunk/containerkit/instrument/client/src/java/org/apache/excalibur/instrument/client/http/HTTPInstrumentSampleData.java
URL: http://svn.apache.org/viewvc/excalibur/trunk/containerkit/instrument/client/src/java/org/apache/excalibur/instrument/client/http/HTTPInstrumentSampleData.java?view=diff&rev=505577&r1=505576&r2=505577
==============================================================================
--- excalibur/trunk/containerkit/instrument/client/src/java/org/apache/excalibur/instrument/client/http/HTTPInstrumentSampleData.java (original)
+++ excalibur/trunk/containerkit/instrument/client/src/java/org/apache/excalibur/instrument/client/http/HTTPInstrumentSampleData.java Fri Feb 9 16:09:35 2007
@@ -1,133 +1,133 @@
-/*
- * Copyright 2002-2004 The Apache Software Foundation
- * Licensed under the Apache License, Version 2.0 (the "License");
- * you may not use this file except in compliance with the License.
- * You may obtain a copy of the License at
- *
- * http://www.apache.org/licenses/LICENSE-2.0
- *
- * Unless required by applicable law or agreed to in writing, software
- * distributed under the License is distributed on an "AS IS" BASIS,
- * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or
- * implied.
- *
- * See the License for the specific language governing permissions and
- * limitations under the License.
- */
-
-package org.apache.excalibur.instrument.client.http;
-
-import org.apache.excalibur.instrument.client.InstrumentSampleData;
-import org.apache.excalibur.instrument.client.InstrumentSampleSnapshotData;
-
-import org.apache.avalon.framework.configuration.Configuration;
-import org.apache.avalon.framework.configuration.ConfigurationException;
-
-class HTTPInstrumentSampleData
- extends AbstractHTTPInstrumentSampleElementData
- implements InstrumentSampleData
-{
- /*---------------------------------------------------------------
- * Constructors
- *-------------------------------------------------------------*/
- /**
- * Creates a new HTTPInstrumentSampleData.
- */
- HTTPInstrumentSampleData( HTTPInstrumentData parent,
- String name )
- {
- super( (HTTPInstrumentManagerConnection)parent.getConnection(), parent, name );
- }
-
- /*---------------------------------------------------------------
- * AbstractHTTPElementData Methods
- *-------------------------------------------------------------*/
- /**
- * Update the contents of the object using values from the Configuration object.
- *
- * @param configuration Configuration object to load from.
- *
- * @throws ConfigurationException If there are any problems.
- */
- protected void update( Configuration configuration )
- throws ConfigurationException
- {
- super.update( configuration );
-
- if ( getLogger().isDebugEnabled() )
- {
- getLogger().debug(
- "Updated Instrument Sample '" + getName() + "' to version " + getStateVersion() );
- }
- }
-
- /**
- * Causes the InstrumentSampleData to update itself with the latest data
- * from the server.
- *
- * @return true if successful.
- */
- public boolean update()
- {
- HTTPInstrumentManagerConnection connection =
- (HTTPInstrumentManagerConnection)getConnection();
-
- Configuration configuration = connection.getState(
- "sample.xml?packed=true&name=" + urlEncode( getName() ) );
- if ( configuration != null )
- {
- try
- {
- update( configuration );
- return true;
- }
- catch ( ConfigurationException e )
- {
- getLogger().debug( "Unable to update.", e );
- }
- }
-
- return false;
- }
-
- /*---------------------------------------------------------------
- * InstrumentSampleData Methods
- *-------------------------------------------------------------*/
- /**
- * Requests that the sample's lease be updated.
- */
- public void updateLease()
- {
- HTTPInstrumentManagerConnection connection =
- (HTTPInstrumentManagerConnection)getConnection();
-
- connection.getState( "sample-lease.xml?name=" + urlEncode( getName() ) );
- }
-
- /**
- * Returns a snapshot of the data in the sample.
- *
- * @return A snapshot of the sample.
- */
- public InstrumentSampleSnapshotData getSnapshot()
- {
- HTTPInstrumentManagerConnection connection =
- (HTTPInstrumentManagerConnection)getConnection();
-
- HTTPInstrumentSampleSnapshotData snapshot =
- new HTTPInstrumentSampleSnapshotData( connection, getName() );
- snapshot.enableLogging( getLogger() );
- if ( snapshot.update() )
- {
- return snapshot;
- }
- else
- {
- return null;
- }
- }
-
- /*---------------------------------------------------------------
- * Methods
- *-------------------------------------------------------------*/
+/*
+ * Copyright 2002-2004 The Apache Software Foundation
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or
+ * implied.
+ *
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+package org.apache.excalibur.instrument.client.http;
+
+import org.apache.excalibur.instrument.client.InstrumentSampleData;
+import org.apache.excalibur.instrument.client.InstrumentSampleSnapshotData;
+
+import org.apache.avalon.framework.configuration.Configuration;
+import org.apache.avalon.framework.configuration.ConfigurationException;
+
+class HTTPInstrumentSampleData
+ extends AbstractHTTPInstrumentSampleElementData
+ implements InstrumentSampleData
+{
+ /*---------------------------------------------------------------
+ * Constructors
+ *-------------------------------------------------------------*/
+ /**
+ * Creates a new HTTPInstrumentSampleData.
+ */
+ HTTPInstrumentSampleData( HTTPInstrumentData parent,
+ String name )
+ {
+ super( (HTTPInstrumentManagerConnection)parent.getConnection(), parent, name );
+ }
+
+ /*---------------------------------------------------------------
+ * AbstractHTTPElementData Methods
+ *-------------------------------------------------------------*/
+ /**
+ * Update the contents of the object using values from the Configuration object.
+ *
+ * @param configuration Configuration object to load from.
+ *
+ * @throws ConfigurationException If there are any problems.
+ */
+ protected void update( Configuration configuration )
+ throws ConfigurationException
+ {
+ super.update( configuration );
+
+ if ( getLogger().isDebugEnabled() )
+ {
+ getLogger().debug(
+ "Updated Instrument Sample '" + getName() + "' to version " + getStateVersion() );
+ }
+ }
+
+ /**
+ * Causes the InstrumentSampleData to update itself with the latest data
+ * from the server.
+ *
+ * @return true if successful.
+ */
+ public boolean update()
+ {
+ HTTPInstrumentManagerConnection connection =
+ (HTTPInstrumentManagerConnection)getConnection();
+
+ Configuration configuration = connection.getState(
+ "sample.xml?packed=true&name=" + urlEncode( getName() ) );
+ if ( configuration != null )
+ {
+ try
+ {
+ update( configuration );
+ return true;
+ }
+ catch ( ConfigurationException e )
+ {
+ getLogger().debug( "Unable to update.", e );
+ }
+ }
+
+ return false;
+ }
+
+ /*---------------------------------------------------------------
+ * InstrumentSampleData Methods
+ *-------------------------------------------------------------*/
+ /**
+ * Requests that the sample's lease be updated.
+ */
+ public void updateLease()
+ {
+ HTTPInstrumentManagerConnection connection =
+ (HTTPInstrumentManagerConnection)getConnection();
+
+ connection.getState( "sample-lease.xml?name=" + urlEncode( getName() ) );
+ }
+
+ /**
+ * Returns a snapshot of the data in the sample.
+ *
+ * @return A snapshot of the sample.
+ */
+ public InstrumentSampleSnapshotData getSnapshot()
+ {
+ HTTPInstrumentManagerConnection connection =
+ (HTTPInstrumentManagerConnection)getConnection();
+
+ HTTPInstrumentSampleSnapshotData snapshot =
+ new HTTPInstrumentSampleSnapshotData( connection, getName() );
+ snapshot.enableLogging( getLogger() );
+ if ( snapshot.update() )
+ {
+ return snapshot;
+ }
+ else
+ {
+ return null;
+ }
+ }
+
+ /*---------------------------------------------------------------
+ * Methods
+ *-------------------------------------------------------------*/
}
Propchange: excalibur/trunk/containerkit/instrument/client/src/java/org/apache/excalibur/instrument/client/http/HTTPInstrumentSampleData.java
------------------------------------------------------------------------------
svn:eol-style = native
---------------------------------------------------------------------
To unsubscribe, e-mail: scm-unsubscribe@excalibur.apache.org
For additional commands, e-mail: scm-help@excalibur.apache.org