纪念今日Java开源

今天(美国时间11月13日星期一上午)Sun将宣布开放Java源代码,包括以GPL v2开放Java SE和ME,以及以CDDLGPL v2协议开放Java EE(即GlassFish,早前已经以CDDL开放了)。

详细信息,请参见http://www.sun.com/opensource/java

Sun公司现在毫无疑问是世界上最大的开放源代码的商业公司,包括近期的OpenSolaris, OpenSparc, Java, 几年前的OpenOffice, NetBeans, 以及比较久的NFS等等。OpenSource不仅是一种成功的软件开发模式,也正成为一种成功的商业模式。Sun清楚地认识这一点,并坚决地站到了潮流的前列。

 

"Agile Java 中文版" 开始预定了

涂波和我翻译的"Agile Java 中文版 —— 测试驱动开发的编程技术"在china-pubdearbook开始预定了。六月中旬应该可以面市。翻译这个事儿真是很累人。英文读起来不难,但是要把原文比较贴切地翻译出来,真是要费一番脑筋。我负责翻译了本书从第十一章开始的后半部分。希望本书的翻译质量能让读者满意。

Java动态代理的示例

public interface SomeClass {

    public abstract void someMethod();

    public abstract void someOtherMethod(final String text);
}

public class SomeClassImpl implements SomeClass{

    private String userName;

    public SomeClassImpl(final String userName) {
        this.userName = userName;
    }

    public void someMethod( ) {
        System.out.println(this.userName);
    }

    public void someOtherMethod(final String text) {
        System.out.println(text);
    }
}

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

public class MethodCountingHandler implements InvocationHandler {

    /* whatever object, you can pass one in */
    private final Object impl;

    private int invocationCount = 0;

    /* constructor */
    public MethodCountingHandler(final Object impl) {
        this.impl = impl;
    }

    /* export the invocation Count */
    public int getInvocationCount( ) {
        return invocationCount;
    }

    /* implements the interface function of InvocationHandler */
    public Object invoke(Object proxy, Method meth, Object[] args)  throws Throwable {
        try {
            this.invocationCount++;
            Object result = meth.invoke(impl, args);
            return result;
        } catch (final InvocationTargetException ex) {
            throw ex.getTargetException( );
        }
    }
}

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

public class SomeClassFactory {

    public static final SomeClass getDynamicSomeClassProxy( ) {

        /* get a implement instance of SomeClass */
        SomeClassImpl impl = new SomeClassImpl(System.getProperty("user.name"));

        if ( !(impl instanceof SomeClass) )
            return null;

        /* construct a invocation handler with the impl instance */
        InvocationHandler handler = new MethodCountingHandler(impl);

        /* get the class info, and the class loader used by this factory */
        Class[] interfaces = new Class[] { SomeClass.class };
        ClassLoader loader = SomeClassFactory.class.getClassLoader( );

        /*
        * install the handler for all implementations of this interface in this class loader
        * and return the proxy instance which accords to SomeClass interface.
        */
        SomeClass proxy = (SomeClass)Proxy.newProxyInstance(loader,
                interfaces,
                handler);

        return proxy;
    }

}

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

public class DemoDynamicProxy {

    public static final void main(final String[] args) {

        SomeClass proxy = SomeClassFactory.getDynamicSomeClassProxy( );

        proxy.someMethod( );

        proxy.someOtherMethod("Our Proxy works!");

        /* get the handler associated with this proxy instance */
        InvocationHandler handler = Proxy.getInvocationHandler(proxy);

        if (handler instanceof MethodCountingHandler) {
            System.out.println(((MethodCountingHandler)handler).getInvocationCount( ));
        }
     }

}

Java的动态代理只支持基于Interface的Method Interception. 本例是从”Hardcore Java”一书中摘取出来的.