|
1 |
| |
|
2 |
| |
|
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 |
| |
|
26 |
| |
|
27 |
| |
|
28 |
| |
|
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 |
643
| protected DynamicProxy() { }
|
|
41 |
| |
|
42 |
| |
|
43 |
| |
|
44 |
233
| public void setExecutor(Executor pvExecutor) { executor = pvExecutor; }
|
|
45 |
840
| public Executor getExecutor() { return executor; }
|
|
46 |
| |
|
47 |
342
| public void setInterceptorHandler(InterceptorHandler pvInterceptorHandler) { interceptorHandler = pvInterceptorHandler; }
|
|
48 |
568
| public InterceptorHandler getInterceptorHandler() { return interceptorHandler; }
|
|
49 |
| |
|
50 |
370
| public void setStaticProxy(StaticProxy pvStaticProxy) { staticProxy = pvStaticProxy; }
|
|
51 |
590
| public StaticProxy getStaticProxy() { return staticProxy; }
|
|
52 |
| |
|
53 |
2
| public String getUser() {
|
|
54 |
2
| return getProperties().getProperty(Property.SECURITY_USER);
|
|
55 |
| } |
|
56 |
2
| public String getPasswd() {
|
|
57 |
2
| return getProperties().getProperty(Property.SECURITY_PASSWD);
|
|
58 |
| } |
|
59 |
| |
|
60 |
126
| public void setAsynchronousCallback(AsynchronousCallback pvAsynchronousCallback, String[] pvMethodFilter, int pvMaxSizeOfThreads) {
|
|
61 |
126
| methodFilter = pvMethodFilter;
|
|
62 |
126
| threadControler.setMaxThreads(pvMaxSizeOfThreads);
|
|
63 |
126
| asyncCallImplObject = pvAsynchronousCallback;
|
|
64 |
| } |
|
65 |
| |
|
66 |
74
| public AsynchronousCallback getAsynchronousCallback() {
|
|
67 |
74
| return asyncCallImplObject;
|
|
68 |
| } |
|
69 |
| |
|
70 |
2
| public int getMaxThreads() {
|
|
71 |
2
| return threadControler.getMaxThreads();
|
|
72 |
| } |
|
73 |
| |
|
74 |
1060
| public boolean isInvocationAsynchronous() {
|
|
75 |
1060
| return (asyncCallImplObject != null);
|
|
76 |
| } |
|
77 |
| |
|
78 |
352
| public Object newInstance(Class pvClass) {
|
|
79 |
352
| setProxyClass(pvClass);
|
|
80 |
| |
|
81 |
| |
|
82 |
352
| if (getStaticProxy() != null) {
|
|
83 |
113
| Object lvStaticProxyObject = getStaticProxy().newInstance(pvClass);
|
|
84 |
111
| getStaticProxy().setProxyObject(lvStaticProxyObject);
|
|
85 |
111
| setProxyObject(lvStaticProxyObject);
|
|
86 |
| } |
|
87 |
| |
|
88 |
350
| return null;
|
|
89 |
| } |
|
90 |
| |
|
91 |
368
| public boolean isMethodInFilter(String pvMethodName) {
|
|
92 |
368
| if (methodFilter != null) {
|
|
93 |
137
| for (int i = 0; i < methodFilter.length; i++) {
|
|
94 |
138
| if (pvMethodName.equals(methodFilter[i])) {
|
|
95 |
55
| return true;
|
|
96 |
| } |
|
97 |
| } |
|
98 |
| } |
|
99 |
313
| return false;
|
|
100 |
| } |
|
101 |
| |
|
102 |
| |
|
103 |
| |
|
104 |
| |
|
105 |
| |
|
106 |
480
| protected Object invokeIfExecutorIsNull(Object pvProxy, Method pvMethod, Object[] pvArgs, Object pvElseObject) throws Exception {
|
|
107 |
480
| Object lvReturn = null;
|
|
108 |
480
| if (isInvocationAsynchronous() == true && isMethodInFilter(pvMethod.getName()) == false) {
|
|
109 |
86
| ThreadWorker lvExecuteWorker = threadControler.getThreadWorker();
|
|
110 |
84
| lvExecuteWorker.executeAsynchrone(pvProxy, pvMethod, pvArgs,
|
|
111 |
| new ExecuteCallback() { |
|
112 |
| |
|
113 |
84
| public Object execute(Object pvObject, Method pvMethod, Object[] pvArgs) throws Exception {
|
|
114 |
84
| AccessController.doPrivileged(new MethodAccessiblePrivilegedAction(pvMethod));
|
|
115 |
84
| Object lvResutlt = pvMethod.invoke(getProxyObject(), pvArgs);
|
|
116 |
83
| return lvResutlt;
|
|
117 |
| } |
|
118 |
| |
|
119 |
| }, asyncCallImplObject); |
|
120 |
| } else { |
|
121 |
394
| try {
|
|
122 |
394
| AccessController.doPrivileged(new MethodAccessiblePrivilegedAction(pvMethod));
|
|
123 |
394
| lvReturn = pvMethod.invoke(getProxyObject(), pvArgs);
|
|
124 |
| } catch (IllegalArgumentException e) { |
|
125 |
| |
|
126 |
1
| throw new IllegalArgumentException(e.getMessage() + " by method: " + pvMethod.getName()
|
|
127 |
| + " -> call of object: " + getProxyObject() + " -> args: " + Util.array2String(pvArgs)); |
|
128 |
| } |
|
129 |
| catch (InvocationTargetException e) { |
|
130 |
12
| Throwable lvThrowable = Util.findDeepestThrowable(e);
|
|
131 |
12
| if (lvThrowable instanceof Exception) {
|
|
132 |
11
| throw (Exception) lvThrowable;
|
|
133 |
| } else { |
|
134 |
1
| throw (Error) lvThrowable;
|
|
135 |
| } |
|
136 |
| } |
|
137 |
| } |
|
138 |
464
| return lvReturn;
|
|
139 |
| } |
|
140 |
| |
|
141 |
| |
|
142 |
| |
|
143 |
| |
|
144 |
| |
|
145 |
| |
|
146 |
| |
|
147 |
578
| public Object invoke(Object pvProxy, Method pvMethod, Object[] pvArgs) throws Exception {
|
|
148 |
| |
|
149 |
578
| Object lvReturn = ExecutorDecorator.notRemoteMethod(pvMethod, getProxyClass());
|
|
150 |
10
| if (lvReturn != null) { return lvReturn; }
|
|
151 |
| |
|
152 |
568
| if (isInvocationAsynchronous() == true && isMethodInFilter(pvMethod.getName()) == false) {
|
|
153 |
225
| Object lvReturnPrimitive = Converter.createDefaultValueForPrimitiveClass(pvMethod.getReturnType());
|
|
154 |
225
| ThreadWorker lvExecuteWorker = threadControler.getThreadWorker();
|
|
155 |
| |
|
156 |
225
| lvExecuteWorker.executeAsynchrone(pvProxy, pvMethod, pvArgs,
|
|
157 |
| new ExecuteCallback() { |
|
158 |
| |
|
159 |
225
| public Object execute(Object pvObject, Method pvMethod, Object[] pvArgs) throws Exception {
|
|
160 |
225
| Object lvResutlt = InvocationHandler.doInvoke(DynamicProxy.this, pvObject, pvMethod, pvArgs, getInterceptorHandler());
|
|
161 |
222
| return lvResutlt;
|
|
162 |
| } |
|
163 |
| |
|
164 |
| } |
|
165 |
| |
|
166 |
| , asyncCallImplObject); |
|
167 |
| |
|
168 |
225
| return lvReturnPrimitive;
|
|
169 |
| } else { |
|
170 |
343
| Object lvReturnValue = InvocationHandler.doInvoke(DynamicProxy.this, pvProxy, pvMethod, pvArgs, getInterceptorHandler());
|
|
171 |
327
| return lvReturnValue;
|
|
172 |
| } |
|
173 |
| } |
|
174 |
| |
|
175 |
| |
|
176 |
| } |