/**
*
*/
package poxy;
import java.lang.reflect.InvocationHandler;
import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;
import java.lang.reflect.Proxy;
import java.util.*;
import callback.PrimitiveUtil;
/**
* More industrial-level dynamic proxy , which main detach the Invocation handler and the real instance of a proxy class,
* which can store many proxy handler with them listener, this is very useful.
* Mean while, it also can work in multi thread env, so you can use it with Thread pool and Scheduler!
**
*/
public class IndustrialDynamicProxy {
/*
* container of iface & his InvocationHandler
* Note: work in multithread env
*/
private Map _iface2Handler = new HashMap();
/*
* Generates the ’caller’ side of the caller/listener relationship.
*/
public Object generateCaller(Class iFace, CallBackType type) {
CallBackHanlder newHandler;
Object proxy=null;
//check iface
if(!iFace.isInterface()){
throw new IllegalArgumentException("Class [" + iFace.getName() + "] is not an interface");
}
//generate a new handler
newHandler = new CallBackHanlder(type);
//generate the face’s proxy
proxy = Proxy.newProxyInstance(iFace.getClassLoader(),
new Class[] { iFace },
newHandler);
//register iface & proxy
synchronized(_iface2Handler){
if(_iface2Handler.containsKey(iFace)){
throw new IllegalArgumentException("Caller already generated " + " for interface [" +iFace.getName() + "]");
}
_iface2Handler.put(iFace, newHandler);
}
return proxy;
}