Saturday, November 20, 2004

Re: General Stuff

Mohn.. could you post more on the Java Proxy classes you mentioned in your recent AOP like post. I am aware of basic Reflection and stuff but what you mentioned was something new. Does .Net have something similar?

Look for the Proxy class in java.lang.reflect in the Java Documentation. It will probably explain things much better.

But for just a basic overview. Proxy has something like a factory method called newProxyInstance() which will give you back an object instance. You give newProxyInstance() a list of interfaces to implement and an InvocationHandler. So the object instance that you get back will in effect "implement" the list of interfaces you gave it. You can cast the object to any one of those interfaces and invoke methods on it.

This is where the InvocationHandler comes in. InvocationHandler is an interface with one method... "invoke( Object proxy, Method method, Object[] args )". Everytime you invoke a method on the proxy object, the invoke() method in InvocationHandler is called and the system passes it the object instance on which the method was called (proxy), the method that was called (method) and the arguments passed to the method (args). This is the point where you do something useful.

Here's a simple example - might help to understand.

Here are two basic interfaces.

public interface Interface1
{
    public void method1();
}

public interface Interface2
{
    public void method2();
}


You want your proxy object to implement these interfaces. You pass it to newProxyInstance() along with an InvocationHandler.

Class[] interfaces = new Class[] { Interface1.class, Interface2.class };

InvocationHandler myInvocationHandler = new MyInvocationHandler();

Object proxy = Proxy.newProxyInstance( interfaces, myInvocationHandler );


At this point you have an object (proxy) that implements the interfaces, but doesn't do anything. So we have to write an InvocationHandler that will intercept the method calls and do something useful.

public class MyInvocationHandler : InvocationHandler
{
    public Object invoke( Object proxy, Method method, Object[] args )
    {
        if ( method.getName() == "method1" )
        {
            System.out.println( "method1" );
        }

        if ( method.getName() == "method2" )
        {
            System.out.println( "method2" );
        }

        return null;
    }
}


So here, MyInvocationHandler intercepts the method calls and does something. So if you invoke the methods on the proxy object...

Interface1 if1 = (Interface1) proxy;
if1.method1();

Interface2 if2 = (Interface2) proxy;
if2.method2();


... it will output...

method1
method2


The important thing to realize is that everything is dynamic. There is no concrete class anywhere that implements Interface1 and Interface2. It's all done at runtime. And what I said last post about AOP was that in MyInvocationHandler's invoke method, I could add anything I wanted before and after the method call...

public class MyInvocationHandler : InvocationHandler
{
    public Object invoke( Object proxy, Method method, Object[] args )
    {
        System.out.println( "Rahul Revo codeword" );
        System.out.println( "Rahul Revo codeword" );

        if ( method.getName() == "method1" )
        {
            System.out.println( "method1" );
        }

        if ( method.getName() == "method2" )
        {
            System.out.println( "method2" );
        }

        System.out.println( "Rahul Revo codeword" );
        System.out.println( "Rahul Revo codeword" );

        return null;
    }
}


The output now will be...

Rahul Revo codeword
Rahul Revo codeword
method1
Rahul Revo codeword
Rahul Revo codeword
Rahul Revo codeword
Rahul Revo codeword
method2
Rahul Revo codeword
Rahul Revo codeword


I couldn't find anything similar to Proxy class in .NET. I would be surprised if you couldn't do it though... just have to find out how! I'll look some more and get back to you.

Also I have changed my display name. Simply to get codeword as a link when I search for myself on google. ;). I do get a few hits at ncb.ernet.in and a few linux posts I made, but codeWord would be much better to be linked to.

lol. You realize this is a private blog right? It's not listed in the blogger directory anywhere, so no links pointing to it. Google ranks based on popularity of site. Based on that, I wouldn't get my hopes up. Anyway, I helped you out... gave the search bots something to chase on your behalf.

No comments: