org.sape.carbon.services.instrumentation.statistics
Class DefaultStatisticsInterceptor

java.lang.Object
  |
  +--org.sape.carbon.services.instrumentation.statistics.DefaultStatisticsInterceptor
All Implemented Interfaces:
Decorator, Interceptor, StatisticsInterceptor

public class DefaultStatisticsInterceptor
extends Object
implements StatisticsInterceptor, Interceptor

This Component Interceptor can be utilized to track the execution of Carbon components. It can be configured in a component template as shown here:

  <!-- The statistics Assistant -->
   <DecoratorConfiguration>
      <Factory>org.sape.carbon.services.component.assistants.StatisticsInterceptorFactory</Factory>
   </DecoratorConfiguration>
 

WARNING: This does not guarantee accurate statistics... it simply keeps there from being innacurrate information in each MethodStat object and from threads stepping on eachother while writing. When reading, it is possible to read some of the data inconsistently. This is accepted as this is designed to be "pretty" accurate, with little to no contention on basic method calls, and only.

Copyright 2002 Sapient

Since:
carbon 1.0
Version:
$Revision: 1.11 $($Author: ghinkl $ / $Date: 2003/10/09 19:33:08 $)
Author:
Greg Hinkle, January 2002

Nested Class Summary
static class DefaultStatisticsInterceptor.MethodStats
          This inner class is used to track information about an individual method on a specific component.
 
Field Summary
private  Component componentReference
          The reference to the component object
private  long componentStartTime
          The general start time of the component
private  FunctionalInterface functionalImplementation
          The reference to the implementation object of the component
private  ThreadLocal localMethodStats
          This threadlocal will hold a local map of methods to MethodStats objects, allowing each thread to accurately accumulate statistics information with no locking or contention.
private  org.apache.commons.logging.Log log
          The handle to Apache-commons logger
private  Map methodStats
          The map of component methods to Lists of MethodStat objects.
protected  Interceptor nextInterceptor
          The reference to the next interceptor in the chain
private  long statisticsStartTime
          The time at which statistics tracking began.
 
Constructor Summary
private DefaultStatisticsInterceptor()
          This object should not be insantiated manually, but only by the Component construction subsystem.
  DefaultStatisticsInterceptor(FunctionalInterface functionalImplementation)
          The constructor used by the component interceptor factory to build this interceptor.
 
Method Summary
private  void appendClassInfo(StringBuffer buf, ClassTree.ClassTreeNode node, int depth)
          Recursively appends class information to the buffer for the given node and all child nodes.
 long getCallCounts()
          The count of calls to the component.
 Class[] getExposedInterfaces()
          This should return the list of interfaces that a decorator wishes to expose through the component proxy.
 Collection getMethodStats()
          A collection of the MethodStats for each method in the instrumented component.
 Date getStartDate()
          The start date of the assisted component.
 long getStartTime()
          Returns the start time of this component
 String getStatsReport()
          Builds an HTML report of information about the assisted component.
 Object invoke(Invocation invocation)
          This method must implement the invocation of any necessary actions and the chaining the next interceptor.
 void logStats()
          Logs the statistics for the tracked component to the default logging component.
 void resetStats()
          Resets the statistics being tracked on the component to zero.
 void setComponentReference(Component component)
          Called after creation to pass a reference to the component to each of its decorators.
 void setNextInterceptor(Interceptor interceptor)
          This method sets the next interceptor as the next in the chain from the current interceptor.
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Field Detail

log

private org.apache.commons.logging.Log log
The handle to Apache-commons logger


methodStats

private Map methodStats
The map of component methods to Lists of MethodStat objects. Each method has an associated list of MethodStats objects (one for each thread that has used the component). Each thread also stores a reference to this object in ThreadLocal. Only one thread will ever update a specific MethodStats object, whereas any method may read them here to aggregate the stats information. WARNING: This does not guarantee accurate statistics... it simply keeps there from being innacurrate information in each MethodStat object and from threads stepping on eachother while writing. When reading, it is possible to read some of the data inconsistently. This is accepted as this is designed to be "pretty" accurate, with little to no contention on basic method calls, and only.


localMethodStats

private ThreadLocal localMethodStats
This threadlocal will hold a local map of methods to MethodStats objects, allowing each thread to accurately accumulate statistics information with no locking or contention.


componentStartTime

private long componentStartTime
The general start time of the component


statisticsStartTime

private long statisticsStartTime
The time at which statistics tracking began. This is either when the component was started, or the last time resetStatistics was called.


componentReference

private Component componentReference
The reference to the component object


functionalImplementation

private FunctionalInterface functionalImplementation
The reference to the implementation object of the component


nextInterceptor

protected Interceptor nextInterceptor
The reference to the next interceptor in the chain

Constructor Detail

DefaultStatisticsInterceptor

private DefaultStatisticsInterceptor()
This object should not be insantiated manually, but only by the Component construction subsystem.


DefaultStatisticsInterceptor

public DefaultStatisticsInterceptor(FunctionalInterface functionalImplementation)
The constructor used by the component interceptor factory to build this interceptor.

Parameters:
functionalImplementation - a reference to the implementation object behind the component.
Method Detail

getStartTime

public long getStartTime()
Returns the start time of this component

Specified by:
getStartTime in interface StatisticsInterceptor
Returns:
the start time, in milleseconds, of the component

getStartDate

public Date getStartDate()
The start date of the assisted component.

Specified by:
getStartDate in interface StatisticsInterceptor
Returns:
the date object of when the assisted component was started

getCallCounts

public long getCallCounts()
The count of calls to the component.

Specified by:
getCallCounts in interface StatisticsInterceptor
Returns:
the number of times any method on the assisted component were called.

getMethodStats

public Collection getMethodStats()
A collection of the MethodStats for each method in the instrumented component. This method works by aggregating the statistics information for each thread. See the warning above about its accuracy.

Specified by:
getMethodStats in interface StatisticsInterceptor
Returns:
an unmodifiable collection of method MethodStats objects.
See Also:
DefaultStatisticsInterceptor

logStats

public void logStats()
Logs the statistics for the tracked component to the default logging component.

Specified by:
logStats in interface StatisticsInterceptor

setNextInterceptor

public void setNextInterceptor(Interceptor interceptor)
This method sets the next interceptor as the next in the chain from the current interceptor.

Specified by:
setNextInterceptor in interface Interceptor
Parameters:
interceptor - the next interceptor in the chain

getExposedInterfaces

public Class[] getExposedInterfaces()
This should return the list of interfaces that a decorator wishes to expose through the component proxy. This is used by the component factory to determine what interfaces the component proxy will implement.

Specified by:
getExposedInterfaces in interface Decorator
Returns:
Class[] an array of interfaces

invoke

public Object invoke(Invocation invocation)
              throws Throwable
This method must implement the invocation of any necessary actions and the chaining the next interceptor.

Specified by:
invoke in interface Interceptor
Parameters:
invocation - the invocation to execute
Returns:
the results of the invocation's execution
Throws:
Throwable - indicates an error in the interceptor chain

setComponentReference

public void setComponentReference(Component component)
Description copied from interface: Decorator
Called after creation to pass a reference to the component to each of its decorators. The component reference should not be used within this method because not all other interceptors have this reference yet and they may rely on this reference thus leading to null pointer exceptions.

Specified by:
setComponentReference in interface Decorator
Parameters:
component - a reference to the component that this interceptor is assisting
See Also:
Decorator.setComponentReference(org.sape.carbon.core.component.Component)

getStatsReport

public String getStatsReport()

Builds an HTML report of information about the assisted component. This report contains general information about the component as well as statistics on the total method calls and information about each method of the component.

Note: It should be noted that the failure counts are only updated apon the next call to a method that has failed. If a component is failing, there well may be uncounted failures, up to a count of the total threads that have had failures on the component.

Specified by:
getStatsReport in interface StatisticsInterceptor
Returns:
an html report of information about the tracked component

appendClassInfo

private void appendClassInfo(StringBuffer buf,
                             ClassTree.ClassTreeNode node,
                             int depth)
Recursively appends class information to the buffer for the given node and all child nodes.

Parameters:
buf - the buffer to append to
node - the node to append
depth - the depth of recursion of the child nodes.

resetStats

public void resetStats()
Resets the statistics being tracked on the component to zero.

Specified by:
resetStats in interface StatisticsInterceptor


Copyright 1999-2003 Sapient Corporation. All Rights Reserved.