当前页面: 开发资料首页 → Java 专题 → 通用的动态代理链--为你的应用程序添加AOP
摘要: 通用的动态代理链--为你的应用程序添加AOP
public interface IMyBusinessObject {
public String doExecute(String in);
}
public class MyBusinessObject implements IMyBusinessObject {
public String doExecute(String in) {
System.out.println("Here in MyBusinessObject doExecute: input :" + in);
return in;
}
}
public abstract class ADecorator implements IMyBusinessObject {
protected IMyBusinessObject target;
public void setTarget(IMyBusinessObject target_) {
this.target = target_;
}
public ADecorator(){}
public ADecorator(IMyBusinessObject target_) {
setTarget(target_);
}
}
public class DebugConcreteDecorator extends ADecorator {
public String doExecute(String in) {
System.out.println("DebugConcreteDecorator: before method : doExecute ");
String ret = target.doExecute(in);
System.out.println("DebugConcreteDecorator: after method : doExecute ");
return ret;
}
}
IMyBusinessObject aIMyBusinessObject = new MyBusinessObject();
IMyBusinessObject wrappedObject =
new DebugConcreteDecorator(aIMyBusinessObject);
wrappedObject.doExecute("Hello World");
public class AnotherConcreteDecorator extends ADecorator {
public String doExecute(String in) {
System.out.println("AnotherConcreteDecorator: Going to execute method : doExecute");
in = in + " Modified by AnotherConcreteDecorator";
String ret = target.doExecute(in);
System.out.println("AnotherConcreteDecorator: After execute method : doExecute");
return ret;
}
}
IMyBusinessObject aIMyBusinessObject = new MyBusinessObject();
IMyBusinessObject wrappedObject =
new AnotherConcreteDecorator (new DebugConcreteDecorator(aIMyBusinessObject));
wrappedObject.doExecute("Hello World");
public class MyDebugInvocationHandler
implements java.lang.reflect.InvocationHandler {
private Object target = null;
public void setTarget(Object target_) {
this.target = target_;
}
public Object invoke(Object proxy, Method method,
Object[] args) throws Throwable {
try {
System.out.println("Going to execute method : " + method.getName);
Object retObject = method.invoke(target, args);
System.out.println("After execute method : " + method.getName());
return retObject;
} catch(InvocationTargetException e) {
throw e.getTargetException();
} catch(Exception e) {
throw e;
}
}
}
IMyBusinessObject bo = new MyBusinessObject();
MyDebugInvocationHandler aMyDebugInvocationHandler =
new MyDebugInvocationHandler();
aMyDebugInvocationHandler.setTarget(bo);
IMyBusinessObject proxyObject =
(IMyBusinessObject) Proxy.newProxyInstance
(IMyBusinessObject.class.getClassLoader(),
new Class[] { IMyBusinessObject.class },
aMyDebugInvocationHandler);
System.out.println(proxyObject.doExecute("Hello World"));
MyAnotherInvocationHandler aMyAnotherInvocationHandler = new MyAnotherInvocationHandler ();
//Here we will set the proxyObject, which we get through earlier
//code snippet, instead of the business object instance
aMyAnotherInvocationHandler.setTarget(proxyObject);
IMyBusinessObject nextProxyObject =
(IMyBusinessObject) Proxy.newProxyInstance
(IMyBusinessObject.class.getClassLoader(),
new Class[] { IMyBusinessObject.class },
aMyAnotherInvocationHandler);
System.out.println(nextProxyObject.doExecute("Hello World"));
String[] interceptorClasses = {"MyDebugInterceptor",
"MyAnotherInterceptor"};
IMyBusinessObject aIMyBusinessObject =
(IMyBusinessObject)MyProxyFactory.getProxyObject
("MyBusinessObject", interceptorClasses);
String ret = aIMyBusinessObject.doExecute("Hello World");
public interface IMethodInterceptor {
Object interceptBefore(Object proxy, Method method,
Object[] args, Object realtarget);
void interceptAfter(Object proxy, Method method,
Object[] args, Object realtarget, Object retObject,
Object interceptBeforeReturnObject);
}
public class MyDebugInterceptor implements IMethodInterceptor {
public Object interceptBefore(Object proxy, Method method,
Object[] args, Object realtarget) {
System.out.println("MyDebugInterceptor: Going to execute method : ");
return null;
}
public void interceptAfter(Object proxy, Method method, Object[] args,
Object realtarget, Object retObject, Object interceptBefore) {
System.out.println("MyDebugInterceptor: After execute method : " );
}
}
public class MyAnotherInterceptor implements IMethodInterceptor {
public Object interceptBefore(Object proxy, Method method,
Object[] args, Object realtarget) {
System.out.println("MyAnotherInterceptor: Going to execute method : ");
if ( method.getName().equals("doExecute") &&
args != null && args.length >= 1 ) {
if ( args[0] instanceof String ) {
args[0] = args[0] +
" Modified by MyAnotherInterceptor";
}
return null;
}
}
public void interceptAfter(Object proxy, Method method, Object[] args,
Object realtarget, Object retObject, Object interceptBefore) {
System.out.println("MyAnotherInterceptor: After execute method : ");
}
}
public class GenericInvocationHandler
implements java.lang.reflect.InvocationHandler {
private Object target = null;
public void setTarget(Object target_) {
this.target = target_;
}
private Object realtarget = null;
public void setRealTarget(Object realtarget_) {
this.realtarget = realtarget_;
}
IMethodInterceptor methodInterceptor = null;
public void setMethodInterceptor
(IMethodInterceptor methodInterceptor_) {
this.methodInterceptor = methodInterceptor_;
}
public Object invoke(Object proxy, Method method, Object[] args)
throws Throwable {
try {
Object interceptBeforeReturnObject = null;
if ( methodInterceptor != null ) {
interceptBeforeReturnObject =
methodInterceptor.interceptBefore
(proxy, method, args, realtarget );
}
Object retObject = method.invoke(target, args);
if ( methodInterceptor != null ) {
methodInterceptor.interceptAfter
(proxy, method, args, realtarget,
retObject, interceptBeforeReturnObject );
}
return retObject;
}
catch(InvocationTargetException e) {
throw e.getTargetException();
}
catch(Exception e) {
throw e;
}
}
}
public class MyProxyFactory {
public static Object getProxyObject( String className,
String[] interceptors ) throws Throwable {
Object inputObject = getTargetObject(className);
if ( interceptors != null && interceptors.length > 0 ) {
Object inputProxiedObject = inputObject;
for ( int i=0; i < interceptors.length; i++ ) {
inputProxiedObject =
getProxyObject(inputObject, interceptors[i],
inputProxiedObject);
}
return inputProxiedObject;
}
else {
return inputObject;
}
}
private static Object getProxyObject(Object inObject,
String interceptor,Object inProxiedObject) throws Throwable {
GenericInvocationHandler invocationHandler =
new GenericInvocationHandler();
IMethodInterceptor interceptorObject =
(IMethodInterceptor)getInterceptor(interceptor);
if ( interceptor == null ) {
return inProxiedObject;
}
invocationHandler.setTarget(inProxiedObject);
invocationHandler.setRealTarget(inObject);
invocationHandler.setMethodInterceptor(interceptorObject);
return Proxy.newProxyInstance
(inObject.getClass().getClassLoader(),
inObject.getClass().getInterfaces(),
invocationHandler) ;
}
private static Object getInterceptor( String interceptors )
throws Exception {
//...
//From the class name return the class instance.
//You can use Class.forName and newInstance() method on Class
//to return the Object instance.
}
private static Object getTargetObject( String className )
throws Exception {
//...
//From the class name return the class instance.
//You can use Class.forName and newInstance() method on Class
//to return the Object instance.
}
}
MyBusinessObject
doExecute
admin,manager
public Object interceptBefore(Object proxy, Method method,
Object[] args, Object realtarget) {
String nameOfMethod = method.getName();
String targetClassName = realtarget.getClass().getName();
MethodPermissions mPerm =
SecurityFactory.getPermission(targetClassName);
If ( !mPerm.isAuthorized(MyThreadLocalRoleStore.getRole(),
nameOfMethod ) ) {
throw new RuntimeException("User not authorized");
}
return null;
}
String[] interceptorClasses = {"MyDebugInterceptor", "MyAnotherInterceptor"};
IMyBusinessObject aIMyBusinessObject =
(IMyBusinessObject)MyProxyFactory.getProxyObject
("MyBusinessObject", interceptorClasses);
String ret = aIMyBusinessObject.doExecute("Hello World");
GenericInvocationHandler代码:
public class GenericInvocationHandler
implements java.lang.reflect.InvocationHandler {
private Object realtarget = null;
public void setRealTarget(Object realtarget_) {
this.realtarget = realtarget_;
}
Object[] methodInterceptors = null;
public void setMethodInterceptors
(Object[] methodInterceptors_) {
this.methodInterceptors = methodInterceptors_;
}
public Object invoke(Object proxy, Method method, Object[] args)
throws Throwable {
try {
Object[] retInterceptBefore = null;
if ( methodInterceptors != null &&
methodInterceptors.length > 0 ) {
retInterceptBefore = new Object[methodInterceptors.length];
for ( int i= methodInterceptors.length - 1; i >= 0; i-- ) {
if ( methodInterceptors[i] != null ) {
retInterceptBefore[i] =
((IMethodInterceptor)methodInterceptors[i]).
interceptBefore(proxy,
method,args, realtarget );
}
}
}
Object retObject = method.invoke(realtarget, args);
if ( methodInterceptors != null ) {
for ( int i= 0; i < methodInterceptors.length; i++ ) {
if ( methodInterceptors[i] != null ) {
((IMethodInterceptor)methodInterceptors[i]).
interceptAfter(proxy, method, args, realtarget,
retObject, retInterceptBefore[i] );
}
}
}
return retObject;
}
catch(InvocationTargetException e) {
throw e.getTargetException();
}
catch(Exception e) {
throw e;
}
}
}
public static Object getProxyObject( String className,
String[] interceptors ) throws Throwable {
Object inputObject = getTargetObject(className);
if ( interceptors != null && interceptors.length > 0 ) {
return getProxyObject(inputObject, interceptors);
}
else {
return inputObject;
}
}
private static Object getProxyObject(Object inObject,
String[] interceptors) throws Throwable {
GenericInvocationHandler invocationHandler =
new GenericInvocationHandler();
Object[] interceptorObjects = getInterceptors(interceptors);
invocationHandler.setRealTarget(inObject);
invocationHandler.setMethodInterceptors(interceptorObjects);
return Proxy.newProxyInstance
(inObject.getClass().getClassLoader(),
inObject.getClass().getInterfaces(),
invocationHandler) ;
}
private static Object[] getInterceptors(String[] interceptors)
throws Exception {
Object[] objInterceptors = new Object[interceptors.length];
for ( int i=0; i < interceptors.length; i++ ) {
objInterceptors[i] = getInterceptor(interceptors[i]);
}
return objInterceptors;
}
synchronized(realtarget) {
retObject = method.invoke(target, args);
}
String[] invocationHandlers = {"MyDebugInvocationHandler",
"MyAnotherInvocationHandler"};
IMyBusinessObject aIMyBusinessObject =
(IMyBusinessObject)MyProxyFactory.getProxyObject
("MyBusinessObject", invocationHandlers);
String ret = aIMyBusinessObject.doExecute("Hello World");
public interface IMyInvocationHandler {
void setTarget(Object target_);
void setRealTarget(Object realtarget_);
}
public abstract class AMyInvocationHandler
implements IMyInvocationHandler,
java.lang.reflect.InvocationHandler {
protected Object target = null;
protected Object realtarget = null;
public void setTarget(Object target_) {
this.target = target_;
}
public void setRealTarget(Object realtarget_) {
this.realtarget = realtarget_;
}
}
1. public class MyDebugInvocationHandler extends AMyInvocationHandler {
public Object invoke(Object proxy, Method method, Object[] args)
throws Throwable {
try {
System.out.println("MyDebugInterceptor: Before execute method : "
+ method.getName());
Object retObject = method.invoke(target, args);
System.out.println("MyDebugInterceptor: After execute method : "
+ method.getName());
return retObject;
}
catch(InvocationTargetException e) {
throw e.getTargetException();
}
catch(Exception e) {
throw e;
}
}
}
2. public class MyAnotherInvocationHandler extends AMyInvocationHandler {
public Object invoke(Object proxy, Method method, Object[] args)
throws Throwable {
try {
System.out.println("MyAnotherInvocationHandler: Before execute method : "
+ method.getName());
if ( method.getName().equals("doExecute")
&& args != null && args.length >= 1 ) {
if ( args[0] instanceof String ) {
args[0] = args[0] + " Modified by MyAnotherInvocationHandler";
}
}
Object retObject = method.invoke(target, args);
System.out.println("MyAnotherInvocationHandler: After execute method : "
+ method.getName());
return retObject;
}
catch(InvocationTargetException e) {
throw e.getTargetException();
}
catch(Exception e) {
throw e;
}
}
}
public class MySynchronizeInvocationHandler extends AMyInvocationHandler {
public Object invoke(Object proxy, Method method, Object[] args)
throws Throwable {
Object retObject = null;
synchronized(realtarget) {
retObject = method.invoke(target, args);
}
return retObject;
}
}
public static Object getProxyObject( String className,
String[] invocationHandlers ) throws Throwable {
Object inputObject = getTargetObject(className);
if ( invocationHandlers != null &&
invocationHandlers.length > 0 ) {
Object inputProxiedObject = inputObject;
for ( int i=0; i < invocationHandlers.length; i++ ) {
AMyInvocationHandler myInvocationHandler =
(AMyInvocationHandler)getInvocationHandler
(invocationHandlers[i]);
inputProxiedObject = getProxyObject(inputObject,
myInvocationHandler, inputProxiedObject);
}
return inputProxiedObject;
}
else {
return inputObject;
}
}
public static Object getProxyObject( Object inputObject,
Object[] invocationHandlers ) throws Throwable {
if ( invocationHandlers != null
&& invocationHandlers.length > 0 ) {
Object inputProxiedObject = inputObject;
for ( int i=0; i < invocationHandlers.length; i++ ) {
inputProxiedObject = getProxyObject(inputObject,
(AMyInvocationHandler)invocationHandlers[i],
inputProxiedObject);
}
return inputProxiedObject;
}
else {
return inputObject;
}
}
private static Object getProxyObject(Object inObject,
AMyInvocationHandler myInvocationHandler,
Object inProxiedObject) throws Throwable {
if ( myInvocationHandler == null ) {
return inProxiedObject;
}
myInvocationHandler.setTarget(inProxiedObject);
myInvocationHandler.setRealTarget(inObject);
return Proxy.newProxyInstance
(inObject.getClass().getClassLoader(),
inObject.getClass().getInterfaces(),
myInvocationHandler) ;
}
public static Object getProxyObject
( Object inputObject, Object[] invocationHandlers )
MyDebugInvocationHandler dHandler = new MyDebugInvocationHandler();
MyAnotherInvocationHandler aHandler = new MyAnotherInvocationHandler();
IMyBusinessObject bo = new MyBusinessObject();
//Now set any variables to these objects
//or some of these objects may be singletons
Object[] invocationHandlers = { dHandler, aHandler };
(IMyBusinessObject)MyProxyFactory.getProxyObject
(bo , invocationHandlers);
String ret = aIMyBusinessObject.doExecute("Hello World");