avalon-dev mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From Antti Koivunen <anryo...@users.sourceforge.net>
Subject [POC] Release-less ComponentManager
Date Wed, 20 Feb 2002 12:10:31 GMT
Hi there,

I briefly mentioned in one mail that it would be possible to use dynamic 
proxies to provide a release-less interface for pooled components. I did 
a simple proof-of-concept implementation (shown below), in case someone 
might be interested. It of course works, but is far from efficient.

As I also mentioned, there's always a small performance hit associated 
with dynamic proxies. In many cases this is acceptable, but I would 
still suggest including a release() method of some sort in the CM 
replacement interface. However, dynamic proxies could be used at some 
level to provide additional functionality, such as access control.

(: A ;)


// Interfacer.java ----------------------------------------------------

public interface Interfacer
{
     Object lookup( String role ) throws Exception;
}


// ComponentSource.java -----------------------------------------------

public interface ComponentSource
{
     Object lookup( String role ) throws Exception;

     void release( Object obj ) throws Exception;
}


// DefaultInterfacer.java ---------------------------------------------

import java.lang.reflect.Proxy;
import java.lang.reflect.Method;
import java.lang.reflect.InvocationHandler;

import org.apache.avalon.framework.thread.ThreadSafe;

public class DefaultInterfacer implements Interfacer
{
     private ComponentSource m_source;

     public DefaultInterfacer(ComponentSource source)
     {
         m_source = source;
     }

     public Object lookup(String role) throws Exception
     {
         Object component = m_source.lookup( role );
         if ( component instanceof ThreadSafe ) {
             return component;
         }
         Class[] interfaces = new Class[] { Class.forName( role ) };
         InvocationHandler handler =
             new ReleaseHandler( m_source, role );
         return Proxy.newProxyInstance(
             Thread.currentThread().getContextClassLoader(),
             interfaces, handler );
     }

     private static class ReleaseHandler implements InvocationHandler
     {
         ComponentSource source;
         String role;

         ReleaseHandler( ComponentSource source, String role )
         {
             this.source = source;
             this.role = role;
         }

         public Object invoke(Object proxy, Method method, Object[] args)
             throws Throwable
         {
             Object component = this.source.lookup( this.role );
             Object result = method.invoke( component, args );
             this.source.release( component );
             return result;
         }
     }
}

//---------------------------------------------------------------------



--
To unsubscribe, e-mail:   <mailto:avalon-dev-unsubscribe@jakarta.apache.org>
For additional commands, e-mail: <mailto:avalon-dev-help@jakarta.apache.org>


Mime
View raw message