/**
*
*/
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!
*
* @author daniel
*
*/
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;
}
/*
* Register a listener for a given interface. If the caller has not
* been registered, then an error will be thrown.
*/
public void registerListener(Class iFace, Object o) {
CallBackHanlder handler;
if(!iFace.isAssignableFrom(o.getClass())){
throw new IllegalArgumentException("Object [" + o + "] does not " +
"implement [" + iFace.getName() +
"]");
}
synchronized (_iface2Handler) {
handler = (CallBackHanlder) _iface2Handler.get(iFace);
}
if(handler == null){
throw new IllegalArgumentException("No callback for interface [" +
iFace.getName() + "] exists");
}
handler.addListener(o);
}
/**
* @param args
*/
public static void main(String[] args) {
//Base testhandler
IndustrialDynamicProxy x=new IndustrialDynamicProxy();
//register iface, get the proxy
MyInterface caller = (MyInterface)x.generateCaller(MyInterface.class, CallBackType.RETURN_LAST);
//register the listener(real instance)
x.registerListener(MyInterface.class, new MyInterface(){
@Override
public int addTwo(int a, int b) {
System.out.println("Target method called");
return a + b;
}
});
//call the listener
int result=caller.addTwo(2, 4);
//take a look
System.out.println("addTwo(2,4):"+result);
}
/**
* An inner class to operate listeners in private
*/
static class CallBackHanlder implements InvocationHandler{
/*
* container of listeners (real instance)
* Note: work in multithread env
*/
private Set _listeners = new HashSet();
//tool of get right Type
private CallBackType _type = null;
CallBackHanlder(CallBackType type){
this._type=type;
}
private void addListener(Object o){
synchronized(_listeners){
_listeners.add(o);
}
}
@Override
public Object invoke(Object proxy, Method method, Object[] args)
throws Throwable {
Set listeners;
synchronized (_listeners) {
listeners = new HashSet(_listeners);
}
return _type.callListeners(method, args, listeners);
}
}
}
//Interface
interface MyInterface {
int addTwo(int a, int b);
}
/*
* CallBackType,
*/
abstract class CallBackType {
public static final CallBackType RETURN_LAST = new CallBackType() {
public Object callListeners(Method meth, Object[] methArgs,Set listeners)throws Throwable
{
Object last = null;
for (Iterator i=listeners.iterator(); i.hasNext(); ) {
Object listener = (Object)i.next();
try {
last = meth.invoke(listener, methArgs);
} catch(InvocationTargetException e) {
throw e.getTargetException();
}
}
if (last == null) {
// Nobody listening ...
return PrimitiveUtil.getBasicValue(meth.getReturnType());
}
return last;
}
};
public abstract Object callListeners(Method meth, Object[] methArgs,
Set listeners)
throws Throwable;
}
分享到:
相关推荐
ios-webkit-debug-proxy-1.9.0-win64-bin
gcr.io/kubebuilder/kube-rbac-proxy:v0.13.0 镜像
docker-letsencrypt-nginx-proxy-companion-examples, 结合 Docker gen和 letsencrypt Nginx 代理伙伴的示例 docker-letsencrypt-nginx-proxy-companion-examples这个库是使用 nginx代理插件, docker gen和 docker-...
flex-messaging-proxy.jarflex-messaging-proxy.jarflex-messaging-proxy.jarflex-messaging-proxy.jar
赠送jar包:netty-handler-proxy-4.1.73.Final.jar; 赠送原API文档:netty-handler-proxy-4.1.73.Final-javadoc.jar; 赠送源代码:netty-handler-proxy-4.1.73.Final-sources.jar; 赠送Maven依赖信息文件:netty-...
赠送jar包:netty-handler-proxy-4.1.73.Final.jar; 赠送原API文档:netty-handler-proxy-4.1.73.Final-javadoc.jar; 赠送源代码:netty-handler-proxy-4.1.73.Final-sources.jar; 赠送Maven依赖信息文件:netty-...
**前端开源库-babel-plugin-proxy** 在前端开发中,JavaScript 的新特性往往需要浏览器支持才能运行,而 ES2015(也称为 ES6)及其后续版本引入了大量的新特性和语法糖,例如类、箭头函数、解构赋值等。然而,不同...
java运行依赖jar包
《Python库解析:jupyter-server-proxy-1.0b1》 在Python的世界里,Jupyter Notebook是一款广受欢迎的交互式编程环境,尤其适合数据科学家和开发者进行代码编写、数据分析和文档编写。而jupyter-server-proxy是...
php-http-proxy, 在基于workerman的PHP中,HTTP代理 php-http-proxy基于workerman的PHP编写的HTTP代理。启动。php start.php 启动 -d停止停止。php start.php 停止状态。php start.php 状态其他链接https
赠送jar包:hadoop-yarn-server-web-proxy-2.6.0.jar; 赠送原API文档:hadoop-yarn-server-web-proxy-2.6.0-javadoc.jar; 赠送源代码:hadoop-yarn-server-web-proxy-2.6.0-sources.jar; 赠送Maven依赖信息文件:...
目录使用REST API REST API基础通过传递令牌进行身份验证获取路由表添加新路线删除路线自定义错误页面基于主机的路由故障排除 安装先决条件: Node.js≥6 如果要在Linux中安装configurable-http-proxy ,则可以按照...
在 Node.js 开发中,`http-proxy-middleware` 是一款非常实用的中间件,主要用于将 HTTP 请求代理转发到其他服务器,这在构建 API 网关、微服务架构或者需要跨域访问时非常有用。这个中间件简化了配置过程,使得...
proxy ( host , options ) ; 要将URLS代理到主机“ ”,请执行以下操作: var proxy = require ( 'koa-better-http-proxy' ) ; var Koa = require ( 'koa' ) ; var app = new Koa ( ) ; app . use ( proxy ( '...
开源项目-mtojek-aws-lambda-go-proxy.zip,mtojek/aws-lambda-go-proxy: Pass Lambda events to the application running on your machine | Debug real traffic locally | Forget about redeployments
node-https-proxy-agent, HTTPS端点的HTTP代理 `http.Agent` 实现 https-proxy-agent HTTPS的HTTP代理 http.Agent 实现 这个模块为连接到指定的HTTP或者HTTPS代理服务器提供了 http.Agent 实现,并且可以与内置的...
分析-Container-proxy-ptr与-Container-proxy-ptr12
koa-server-http-proxy koa2 http-proxy-middleware。安装$ npm install koa-server-http-proxy --save例const Koa = require ( 'koa' )const app = new Koa ( )const proxy = require ( 'koa-server-http-proxy' )...
赠送jar包:netty-handler-proxy-4.1.68.Final.jar; 赠送原API文档:netty-handler-proxy-4.1.68.Final-javadoc.jar; 赠送源代码:netty-handler-proxy-4.1.68.Final-sources.jar; 赠送Maven依赖信息文件:netty-...
赠送jar包:netty-handler-proxy-4.1.74.Final.jar; 赠送原API文档:netty-handler-proxy-4.1.74.Final-javadoc.jar; 赠送源代码:netty-handler-proxy-4.1.74.Final-sources.jar; 赠送Maven依赖信息文件:netty-...