View Javadoc

1   /*
2    * Created on 30.04.2005 from Linke
3    *
4    */
5   package net.sf.crispy.proxy;
6   
7   import java.lang.reflect.InvocationTargetException;
8   import java.lang.reflect.Method;
9   import java.security.AccessController;
10  
11  import net.sf.crispy.Executor;
12  import net.sf.crispy.ExecutorDecorator;
13  import net.sf.crispy.InterceptorHandler;
14  import net.sf.crispy.Property;
15  import net.sf.crispy.concurrent.AsynchronousCallback;
16  import net.sf.crispy.concurrent.ExecuteCallback;
17  import net.sf.crispy.concurrent.ThreadController;
18  import net.sf.crispy.concurrent.ThreadWorker;
19  import net.sf.crispy.util.Converter;
20  import net.sf.crispy.util.MethodAccessiblePrivilegedAction;
21  import net.sf.crispy.util.Util;
22  
23  
24  /**
25   * Catch all method invocation. If Executer is registered, then delegate invocation to Executer.
26   * If not, the invoke the method <code>invokeIfExecutorIsNull</code>. 
27   * 
28   * @author Linke
29   */
30  
31  public abstract class DynamicProxy extends Proxy {
32  	
33  	private Executor executor = null;
34  	private StaticProxy staticProxy = null;
35  	private InterceptorHandler interceptorHandler = new InterceptorHandler();
36      private AsynchronousCallback asyncCallImplObject = null;
37      private String[] methodFilter = null;
38      private ThreadController threadControler = new ThreadController();
39  	
40  	protected DynamicProxy() { }
41  	
42  	
43  	
44  	public void setExecutor(Executor pvExecutor) { executor = pvExecutor; }
45  	public Executor getExecutor() { return executor; }
46  
47  	public void setInterceptorHandler(InterceptorHandler pvInterceptorHandler) { interceptorHandler = pvInterceptorHandler; }
48  	public InterceptorHandler getInterceptorHandler() { return interceptorHandler; }
49  
50  	public void setStaticProxy(StaticProxy pvStaticProxy) { staticProxy = pvStaticProxy; }
51  	public StaticProxy getStaticProxy() { return staticProxy; }
52  
53  	public String getUser() {
54  		return getProperties().getProperty(Property.SECURITY_USER);
55  	}
56  	public String getPasswd() {
57  		return getProperties().getProperty(Property.SECURITY_PASSWD);
58  	}
59  		
60  	public void setAsynchronousCallback(AsynchronousCallback pvAsynchronousCallback, String[] pvMethodFilter, int pvMaxSizeOfThreads) {
61  		methodFilter = pvMethodFilter;
62  		threadControler.setMaxThreads(pvMaxSizeOfThreads);
63  		asyncCallImplObject = pvAsynchronousCallback;
64  	}
65  	
66  	public AsynchronousCallback getAsynchronousCallback() {
67  		return asyncCallImplObject;
68  	}
69  
70  	public int getMaxThreads() {
71  		return threadControler.getMaxThreads();
72  	}
73  	
74  	public boolean isInvocationAsynchronous() {
75  		return (asyncCallImplObject != null);
76  	}
77  
78  	public Object newInstance(Class pvClass) {
79  		setProxyClass(pvClass);
80  		// Bei einem Executer wird KEIN ProxyObjekt benoetigt, 
81  		// da der Aufruf direkt an den Server delegiert wird
82  		if (getStaticProxy() != null) {
83  			Object lvStaticProxyObject = getStaticProxy().newInstance(pvClass);
84  			getStaticProxy().setProxyObject(lvStaticProxyObject);
85  			setProxyObject(lvStaticProxyObject);
86  		}
87  		
88  		return null;
89  	}
90  	
91  	public boolean isMethodInFilter(String pvMethodName) {
92  		if (methodFilter != null) {
93  			for (int i = 0; i < methodFilter.length; i++) {
94  				if (pvMethodName.equals(methodFilter[i])) {
95  					return true;
96  				}
97  			}
98  		}
99  		return false;
100 	}
101 	
102 
103 	/**
104 	 * If not Executor is set. It is the case, if StaticProxy is use in combination with DynamicProxy.
105 	 */
106 	protected Object invokeIfExecutorIsNull(Object pvProxy, Method pvMethod, Object[] pvArgs, Object pvElseObject) throws Exception {
107 		Object lvReturn = null;
108 		if (isInvocationAsynchronous() == true && isMethodInFilter(pvMethod.getName()) == false) {
109     		ThreadWorker lvExecuteWorker = threadControler.getThreadWorker();
110     		lvExecuteWorker.executeAsynchrone(pvProxy, pvMethod, pvArgs,
111     				new ExecuteCallback() {
112 
113 						public Object execute(Object pvObject, Method pvMethod, Object[] pvArgs) throws Exception {
114 							AccessController.doPrivileged(new MethodAccessiblePrivilegedAction(pvMethod));
115 							Object lvResutlt = pvMethod.invoke(getProxyObject(), pvArgs);
116 							return lvResutlt;
117 						}
118     			
119     		}, asyncCallImplObject);
120     	} else {
121     		try {
122     			AccessController.doPrivileged(new MethodAccessiblePrivilegedAction(pvMethod));
123     			lvReturn = pvMethod.invoke(getProxyObject(), pvArgs);
124     		} catch (IllegalArgumentException e) {
125     			// for more information by IllegalArgumentException
126     			throw new IllegalArgumentException(e.getMessage() + " by method: " + pvMethod.getName() 
127     							+ " -> call of object: " + getProxyObject() + " -> args: " + Util.array2String(pvArgs));
128     		} 
129     		catch (InvocationTargetException e) {
130     			Throwable lvThrowable = Util.findDeepestThrowable(e);
131     			if (lvThrowable instanceof Exception) {
132     				throw (Exception) lvThrowable;
133     			} else {
134     				throw (Error) lvThrowable;
135     			}
136     		}
137     	}    		
138     	return lvReturn;
139 	}
140 	
141 	
142 	/**
143 	 * Delegate invocation to the InvocationHandler.
144 	 * 
145 	 * @see java.lang.reflect.InvocationHandler#invoke(java.lang.Object, java.lang.reflect.Method, java.lang.Object[])
146 	 */
147 	public Object invoke(Object pvProxy, Method pvMethod, Object[] pvArgs) throws Exception {
148 		// no remote method
149 		Object lvReturn = ExecutorDecorator.notRemoteMethod(pvMethod, getProxyClass());
150 		if (lvReturn != null) { return lvReturn; }
151 		
152 		if (isInvocationAsynchronous() == true && isMethodInFilter(pvMethod.getName()) == false) {
153 			Object lvReturnPrimitive = Converter.createDefaultValueForPrimitiveClass(pvMethod.getReturnType());
154     		ThreadWorker lvExecuteWorker = threadControler.getThreadWorker();
155     		
156     		lvExecuteWorker.executeAsynchrone(pvProxy, pvMethod, pvArgs,
157 					new ExecuteCallback() {
158 
159 						public Object execute(Object pvObject, Method pvMethod, Object[] pvArgs) throws Exception {
160 							Object lvResutlt = InvocationHandler.doInvoke(DynamicProxy.this, pvObject, pvMethod, pvArgs, getInterceptorHandler());
161 							return lvResutlt;
162 						}
163     			
164     		}
165 
166     		, asyncCallImplObject);
167 
168     		return lvReturnPrimitive;
169     	} else {
170     		Object lvReturnValue = InvocationHandler.doInvoke(DynamicProxy.this, pvProxy, pvMethod, pvArgs, getInterceptorHandler());
171     		return lvReturnValue;
172     	}
173 	}
174 	
175 	
176 }