com.arsdigita.developersupport
Class LoggingProxyFactory

java.lang.Object
  extended bycom.arsdigita.developersupport.LoggingProxyFactory
All Implemented Interfaces:
LoggerConfigurator

public final class LoggingProxyFactory
extends Object
implements LoggerConfigurator

This factory produces dynamic proxies that can log and delegate all method calls to the object for which they are proxying.

After instantiating a factory instance, you can configure it via methods like setLogger(String) and setLevel(String). Once configured, the factory can produce, via its newLoggingProxy(Object, Class) method, dynamic proxy objects implementing the required interface.

Since:
2003-06-17
Version:
$Revision: #5 $ $Date: 2004/04/07 $
Author:
Vadim Nasardinov (vadimn@redhat.com)

Constructor Summary
LoggingProxyFactory()
           
 
Method Summary
 void enableStackTraces()
          Call this method to enable the logging of a stack trace for each intercepted method call.
 Object newLoggingProxy(Object proxiedObject, Class iface)
          This is equivalent to newLoggingProxy(proxiedObject, iface, false).
 Object newLoggingProxy(Object proxiedObject, Class iface, boolean configurable)
          Returns an object that implements the iface and LoggingProxy interfaces.
 void setLevel(String level)
          Sets the logging level for the logger.
 void setLogger(String logger)
          Sets the logger that will be used to log method calls.
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Constructor Detail

LoggingProxyFactory

public LoggingProxyFactory()
Method Detail

setLogger

public void setLogger(String logger)
Sets the logger that will be used to log method calls.

Specified by:
setLogger in interface LoggerConfigurator
See Also:
setLevel(String)

setLevel

public void setLevel(String level)

Sets the logging level for the logger. If you don't ever call this method to specify the logging level explicitly, it will default to "debug". If you specify an invalid logging level, the default value of "debug" will be substituted silently.

Specified by:
setLevel in interface LoggerConfigurator
See Also:
setLogger(String)

enableStackTraces

public void enableStackTraces()
Call this method to enable the logging of a stack trace for each intercepted method call.

Specified by:
enableStackTraces in interface LoggerConfigurator

newLoggingProxy

public Object newLoggingProxy(Object proxiedObject,
                              Class iface,
                              boolean configurable)
Returns an object that implements the iface and LoggingProxy interfaces.

The object works by delegating all method calls to proxiedObject. Method calls are logged on entry using the logger specified via setLogger(String). If no logger was specified, Logger.getLogger(LoggingProxyFactory.class) is used. Example:

 protected void service(HttpServletRequest req, HttpServletResponse resp) {
     LoggingProxyFactory factory = new LoggingProxyFactory();
     factory.enableStackTraces();
     factory.setLevel("info");
     HttpServletRequest reqProxy = (HttpServletRequest)
         factory.newLoggingProxy(req, HttpServletRequest.class, true);
     doStuff((HttpServletRequest) reqProxy, resp)
 }

 private void doStuff(HttpServletRequest req, HttpServletResponse resp) {
     // ...
     
     if ( Proxy.isProxyClass(req.getClass()) ) {
         LoggingProxy proxy = (LoggingProxy) req;
         HttpServletRequest realReq = (HttpServletRequest) req.getProxiedObject();
         // do stuff with the original, unwrapped request.
     }
 }
 

This allows you to track all calls to the HTTP request object that are made in your program during the execution of the service(req, resp).

Parameters:
proxiedObject - the proxied object
iface - the interface to be implemented by the returned dynamic proxy
configurable - if true, the returned object will also implement the LoggingProxy interface. Note that requiring the logging proxy to implement this additional interface may cause an IllegalArgumentException due to the fact the specified interface is not accessible to the classloader with which proxiedObject was instantiated. See also newLoggingProxy(Object, Class).
Returns:
an object that implements the interface iface. The returned object will be created by the same classloader with which proxiedObject was created.
See Also:
newLoggingProxy(Object, Class)

newLoggingProxy

public Object newLoggingProxy(Object proxiedObject,
                              Class iface)
This is equivalent to newLoggingProxy(proxiedObject, iface, false). In other words, the returned proxy does not implement the LoggingProxy interface.

See Also:
newLoggingProxy(Object, Class, boolean)


Copyright (c) 2004 Red Hat, Inc. Corporation. All Rights Reserved. Generated at July 20 2004:2337 UTC