de.is24.util.monitoring.wrapper
Class GenericMonitoringWrapper<E>

java.lang.Object
  extended by de.is24.util.monitoring.wrapper.GenericMonitoringWrapper<E>
Type Parameters:
E - the interface of the object being wrapped.
All Implemented Interfaces:
InvocationHandler

public class GenericMonitoringWrapper<E>
extends Object
implements InvocationHandler

This class provides the ability to wrap arbitrary objects and monitor the execution times of methods invoked on the object. The wrapped object must be described by an interface so that the wrapper can generate a proxy object during runtime.

The wrapper can be configured with a GenericMonitoringWrapper.TimingReporter that is used to report the timinig measurement of a single method invocation. A default implementation based on the InApplicationMonitor is provided.

Use one of the static wrapObject(Class, Object) or wrapObject(Class, Object, TimingReporter) factory methods to wrap a given object. Here is a small code snipped demonstrating the use:

 final Connection connection = dataSource.getConnection();
 final wrappedConnection = GenericMonitoringWrapper.wrapObject(Connection.class, connection);
 
Now, if you make method calls on the wrappedConnection object, each method invocation will be reported to the InApplicationMonitor, as the GenericMonitoringWrapper.InApplicationMonitorTimingReporter is the default reporter to use in case none is given.

Author:
Alexander Metzner
See Also:
InApplicationMonitor, Proxy

Nested Class Summary
static class GenericMonitoringWrapper.InApplicationMonitorTimingReporter
          Default implementation of the GenericMonitoringWrapper.TimingReporter interface that uses the InApplicationMonitor as it's backend.
static interface GenericMonitoringWrapper.TimingReporter
          Interface for objects that receive and handle timinig information for a given method invocation.
 
Field Summary
private  GenericMonitoringWrapper.TimingReporter reporter
          The reported used to report method invocation timings.
private  Object target
          The target object being wrapped.
private  Class<E> targetClass
          Stores the interface the wrapped object want's to expose.
 
Constructor Summary
protected GenericMonitoringWrapper(Class<E> targetClass, Object target, GenericMonitoringWrapper.TimingReporter timingReporter)
          Constructs a new wrapper object.
 
Method Summary
 Object invoke(Object proxy, Method method, Object[] args)
          Handles method invocations on the generated proxy.
static
<E> E
wrapObject(Class<E> clazz, Object target)
          Wraps the given object and returns the reporting proxy.
static
<E> E
wrapObject(Class<E> clazz, Object target, GenericMonitoringWrapper.TimingReporter timingReporter)
          Wraps the given object and returns the reporting proxy.
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Field Detail

targetClass

private final Class<E> targetClass
Stores the interface the wrapped object want's to expose.


target

private final Object target
The target object being wrapped.


reporter

private final GenericMonitoringWrapper.TimingReporter reporter
The reported used to report method invocation timings.

Constructor Detail

GenericMonitoringWrapper

protected GenericMonitoringWrapper(Class<E> targetClass,
                                   Object target,
                                   GenericMonitoringWrapper.TimingReporter timingReporter)
Constructs a new wrapper object. Use internally by the static wrapObject(Class, Object) factory methods.

Parameters:
targetClass - the class object of the public interface to wrap
target - the target object to wrap
timingReporter - the timing reporter
Method Detail

wrapObject

public static <E> E wrapObject(Class<E> clazz,
                               Object target)
Wraps the given object and returns the reporting proxy. Uses the GenericMonitoringWrapper.InApplicationMonitorTimingReporter to report the timings.

Type Parameters:
E - the type of the public interface of the wrapped object
Parameters:
clazz - the class object to the interface
target - the object to wrap
Returns:
the monitoring wrapper

wrapObject

public static <E> E wrapObject(Class<E> clazz,
                               Object target,
                               GenericMonitoringWrapper.TimingReporter timingReporter)
Wraps the given object and returns the reporting proxy. Uses the given timing reporter to report timings.

Type Parameters:
E - the type of the public interface of the wrapped object
Parameters:
clazz - the class object to the interface
target - the object to wrap
timingReporter - the reporter to report timing information to
Returns:
the monitoring wrapper

invoke

public Object invoke(Object proxy,
                     Method method,
                     Object[] args)
              throws Throwable
Handles method invocations on the generated proxy. Measures the time needed to execute the given method on the wrapped object.

Specified by:
invoke in interface InvocationHandler
Throws:
Throwable
See Also:
InvocationHandler.invoke(Object, java.lang.reflect.Method, Object[])


Copyright © 2013 Immobilien Scout GmbH. All Rights Reserved.