donaldp 2002/10/03 17:33:25
Modified: src/java/org/apache/avalon/phoenix/components/application
BlockEntry.java BlockInvocationHandler.java
Added: src/java/org/apache/avalon/phoenix
AstractChainedInvocable.java
Log:
Start to integrate the notion of interceptors and factorys into phoenix.
Submitted by: Igor Fedorenko <[EMAIL PROTECTED]>
Revision Changes Path
1.1
jakarta-avalon-phoenix/src/java/org/apache/avalon/phoenix/AstractChainedInvocable.java
Index: AstractChainedInvocable.java
===================================================================
package org.apache.avalon.phoenix;
import java.lang.reflect.InvocationHandler;
import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;
import org.apache.avalon.framework.logger.AbstractLogEnabled;
/**
* Subclasses of <code>AstractChainedInvocable</code> form chain of
* block invocation interceptors.
*
* <p>The idea is to be able to extend block's behaviour without changing
* block's code. One of usages of such interceptors would be
* <code>SecurityInterceptor</code> which verifies if a caller is allowed to
* perform requested operation on a block.</p>
*
* @author ifedorenko
*/
public abstract class AstractChainedInvocable extends AbstractLogEnabled
implements InvocationHandler
{
private transient Object m_object;
private InvocationHandler m_chained;
public final void setObject( Object object )
{
m_object = object;
}
public final void setChained( InvocationHandler chained )
{
m_chained = chained;
}
/**
* @see java.lang.reflect.InvocationHandler#invoke(Object, Method, Object[])
*/
public Object invoke( final Object proxy,
final Method method,
final Object[] args )
throws Throwable
{
return ( m_chained != null )
? m_chained.invoke( proxy, method, args )
: invokeObject( proxy, method, args );
}
/**
* Invoke the specified method on underlying object.
* This is called by proxy object.
*
* @param proxy the proxy object
* @param method the method invoked on proxy object
* @param args the arguments supplied to method
* @return the return value of method
* @throws Throwable if an error occurs
*/
private Object invokeObject( final Object proxy,
final Method method,
final Object[] args )
throws Throwable
{
if( null != m_object )
{
try
{
return method.invoke( m_object, args );
}
catch( final InvocationTargetException ite )
{
throw ite.getTargetException();
}
}
else
{
throw new IllegalStateException( "Using a stale object reference "
+ "to call a disposed Block." );
}
}
}
1.21 +6 -4
jakarta-avalon-phoenix/src/java/org/apache/avalon/phoenix/components/application/BlockEntry.java
Index: BlockEntry.java
===================================================================
RCS file:
/home/cvs/jakarta-avalon-phoenix/src/java/org/apache/avalon/phoenix/components/application/BlockEntry.java,v
retrieving revision 1.20
retrieving revision 1.21
diff -u -r1.20 -r1.21
--- BlockEntry.java 18 Aug 2002 08:23:33 -0000 1.20
+++ BlockEntry.java 4 Oct 2002 00:33:25 -0000 1.21
@@ -52,8 +52,10 @@
if( null != object && ! getMetaData().isDisableProxy() )
{
final BlockInfo blockInfo = getMetaData().getBlockInfo();
- final Class[] interfaces = getServiceClasses( object,
blockInfo.getServices() );
- m_invocationHandler = new BlockInvocationHandler( object, interfaces );
+ final ClassLoader classLoader = object.getClass().getClassLoader();
+ final Class[] interfaces = getServiceClasses( classLoader,
blockInfo.getServices() );
+ m_invocationHandler = new BlockInvocationHandler( classLoader,
interfaces );
+ m_invocationHandler.setObject( object );
}
m_object = object;
}
@@ -87,10 +89,10 @@
m_object = null;
}
- private Class[] getServiceClasses( final Object block, final
ServiceDescriptor[] services )
+ private Class[] getServiceClasses( final ClassLoader classLoader,
+ final ServiceDescriptor[] services )
{
final Class[] classes = new Class[ services.length + 1 ];
- final ClassLoader classLoader = block.getClass().getClassLoader();
for( int i = 0; i < services.length; i++ )
{
1.9 +8 -44
jakarta-avalon-phoenix/src/java/org/apache/avalon/phoenix/components/application/BlockInvocationHandler.java
Index: BlockInvocationHandler.java
===================================================================
RCS file:
/home/cvs/jakarta-avalon-phoenix/src/java/org/apache/avalon/phoenix/components/application/BlockInvocationHandler.java,v
retrieving revision 1.8
retrieving revision 1.9
diff -u -r1.8 -r1.9
--- BlockInvocationHandler.java 6 Aug 2002 11:57:39 -0000 1.8
+++ BlockInvocationHandler.java 4 Oct 2002 00:33:25 -0000 1.9
@@ -7,11 +7,10 @@
*/
package org.apache.avalon.phoenix.components.application;
-import java.lang.reflect.InvocationHandler;
-import java.lang.reflect.InvocationTargetException;
-import java.lang.reflect.Method;
import java.lang.reflect.Proxy;
+import org.apache.avalon.phoenix.AstractChainedInvocable;
+
/**
* This makes a dynamic proxy for an object. The object can be represented
* by one, some or all of it's interfaces.
@@ -29,8 +28,7 @@
* @author <a href="mailto:[EMAIL PROTECTED]">Paul Hammant</a>
* @version CVS $Revision$ $Date$
*/
-final class BlockInvocationHandler
- implements InvocationHandler
+final class BlockInvocationHandler extends AstractChainedInvocable
{
private transient Object m_object;
@@ -42,11 +40,10 @@
* @param object the underlying object
* @param interfaces the interfaces to proxy
*/
- protected BlockInvocationHandler( final Object object, final Class[] interfaces
)
+ protected BlockInvocationHandler( final ClassLoader classLoader,
+ final Class[] interfaces )
{
- final ClassLoader classLoader = object.getClass().getClassLoader();
-
- m_object = object;
+ super();
m_proxy = Proxy.newProxyInstance( classLoader, interfaces, this );
}
@@ -56,7 +53,7 @@
*/
public void invalidate()
{
- m_object = null;
+ setObject( null );
m_proxy = null;
}
@@ -68,38 +65,5 @@
public Object getProxy()
{
return m_proxy;
- }
-
- /**
- * Invoke the specified method on underlying object.
- * This is called by proxy object.
- *
- * @param proxy the proxy object
- * @param method the method invoked on proxy object
- * @param args the arguments supplied to method
- * @return the return value of method
- * @throws Throwable if an error occurs
- */
- public Object invoke( final Object proxy,
- final Method method,
- final Object[] args )
- throws Throwable
- {
- if( null != m_object )
- {
- try
- {
- return method.invoke( m_object, args );
- }
- catch( final InvocationTargetException ite )
- {
- throw ite.getTargetException();
- }
- }
- else
- {
- throw new IllegalStateException( "Using a stale object reference "
- + "to call a disposed Block." );
- }
}
}
--
To unsubscribe, e-mail: <mailto:[EMAIL PROTECTED]>
For additional commands, e-mail: <mailto:[EMAIL PROTECTED]>