KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > tctest > ProxyTestApp


1 /*
2  * All content copyright (c) 2003-2006 Terracotta, Inc., except as may otherwise be noted in a separate copyright notice. All rights reserved.
3  */

4 package com.tctest;
5
6 import EDU.oswego.cs.dl.util.concurrent.CyclicBarrier;
7
8 import com.tc.object.bytecode.Manageable;
9 import com.tc.object.bytecode.ManagerUtil;
10 import com.tc.object.bytecode.TransparentAccess;
11 import com.tc.object.config.ConfigVisitor;
12 import com.tc.object.config.DSOClientConfigHelper;
13 import com.tc.object.config.TransparencyClassSpec;
14 import com.tc.object.loaders.IsolationClassLoader;
15 import com.tc.simulator.app.ApplicationConfig;
16 import com.tc.simulator.listener.ListenerProvider;
17 import com.tc.util.Assert;
18 import com.tctest.runner.AbstractTransparentApp;
19
20 import java.io.Serializable JavaDoc;
21 import java.lang.reflect.Field JavaDoc;
22 import java.lang.reflect.InvocationHandler JavaDoc;
23 import java.lang.reflect.Method JavaDoc;
24 import java.lang.reflect.Proxy JavaDoc;
25 import java.net.URL JavaDoc;
26 import java.net.URLClassLoader JavaDoc;
27 import java.util.Collection JavaDoc;
28 import java.util.HashMap JavaDoc;
29 import java.util.Map JavaDoc;
30
31 public class ProxyTestApp extends AbstractTransparentApp {
32   private final CyclicBarrier barrier;
33   private MyProxyInf1 proxyImpl;
34
35   private DataRoot dataRoot = new DataRoot();
36   private Map JavaDoc mapRoot = new HashMap JavaDoc();
37
38   public ProxyTestApp(String JavaDoc appId, ApplicationConfig cfg, ListenerProvider listenerProvider) {
39     super(appId, cfg, listenerProvider);
40     this.barrier = new CyclicBarrier(getParticipantCount());
41   }
42
43   public void run() {
44     try {
45       int index = barrier.barrier();
46
47       proxyDSOInterfaces1(index);
48       proxyDSOInterfaces2(index);
49       proxySystemInterface(index);
50       validProxyTest(index);
51       subclassProxyTest(index);
52       differentLoaderTest(index);
53       multipleInterfacesProxyTest(index);
54       cloneTest(index);
55       innerCloneTest(index);
56       reflectionTest(index);
57
58     } catch (Throwable JavaDoc t) {
59       notifyError(t);
60     }
61   }
62
63   /**
64    * A simple test to create a proxy with DSO interfaces.
65    */

66   private void proxyDSOInterfaces1(int index) throws Throwable JavaDoc {
67     if (index == 0) {
68       MyInvocationHandler handler = new MyInvocationHandler();
69       Proxy JavaDoc myProxy = (Proxy JavaDoc) Proxy.newProxyInstance(this.getClass().getClassLoader(), new Class JavaDoc[] { Manageable.class,
70           MyProxyInf1.class, MyProxyInf2.class, TransparentAccess.class }, handler);
71       dataRoot.setMyProxy(myProxy);
72     }
73
74     barrier.barrier();
75
76     Class JavaDoc[] interfaces = dataRoot.getMyProxy().getClass().getInterfaces();
77     Assert.assertEquals(2, interfaces.length);
78     for (int i = 0; i < interfaces.length; i++) {
79       Assert.assertFalse(interfaces[i].equals(Manageable.class.getName()));
80       Assert.assertFalse(interfaces[i].equals(TransparentAccess.class.getName()));
81     }
82
83     barrier.barrier();
84   }
85
86   private void proxyDSOInterfaces2(int index) throws Throwable JavaDoc {
87     if (index == 0) {
88       MyInvocationHandler handler = new MyInvocationHandler();
89       Object JavaDoc o = Proxy.newProxyInstance(this.getClass().getClassLoader(), new Class JavaDoc[] { Manageable.class,
90           TransparentAccess.class, Serializable JavaDoc.class }, handler);
91
92       dataRoot.setMyProxy(o);
93     }
94
95     barrier.barrier();
96
97     Class JavaDoc[] interfaces = dataRoot.getMyProxy().getClass().getInterfaces();
98     Assert.assertEquals(1, interfaces.length);
99     Assert.assertEquals(Serializable JavaDoc.class.getName(), interfaces[0].getName());
100
101     barrier.barrier();
102
103   }
104
105   private void reflectionTest(int index) throws Throwable JavaDoc {
106     if (index == 0) {
107       MyInvocationHandler handler = new MyInvocationHandler();
108       Proxy JavaDoc myProxy = (Proxy JavaDoc) Proxy.newProxyInstance(this.getClass().getClassLoader(), new Class JavaDoc[] { MyProxyInf1.class,
109           MyProxyInf2.class }, handler);
110       ((MyProxyInf2) myProxy).setStringValue("Testing2");
111       dataRoot.setMyProxy(myProxy);
112     }
113
114     barrier.barrier();
115
116     Assert.assertEquals("Testing2", ((MyProxyInf2) dataRoot.getMyProxy()).getStringValue());
117
118     barrier.barrier();
119
120     if (index == 1) {
121       MyInvocationHandler handler = new MyInvocationHandler();
122       Proxy JavaDoc myProxy = (Proxy JavaDoc) dataRoot.getMyProxy();
123       Field JavaDoc field = myProxy.getClass().getSuperclass().getDeclaredField("h"); // get the invocation handler field
124
// through reflection.
125
field.setAccessible(true);
126       synchronized (myProxy) {
127         field.set(myProxy, handler);
128       }
129     }
130
131     barrier.barrier();
132
133     Assert.assertNull(((MyProxyInf2) dataRoot.getMyProxy()).getStringValue());
134
135     barrier.barrier();
136
137     if (index == 0) {
138       synchronized (dataRoot.getMyProxy()) {
139         ((MyProxyInf1) dataRoot.getMyProxy()).setValue(2002);
140       }
141     }
142
143     barrier.barrier();
144
145     Assert.assertEquals(2002, ((MyProxyInf1) dataRoot.getMyProxy()).getValue());
146
147     barrier.barrier();
148   }
149
150   private void innerCloneTest(int index) throws Throwable JavaDoc {
151     if (index == 0) {
152       MyInvocationHandler handler = new MyInvocationHandler();
153       Proxy JavaDoc myProxy = (Proxy JavaDoc) Proxy.newProxyInstance(this.getClass().getClassLoader(), new Class JavaDoc[] { MyProxyInf1.class,
154           MyProxyInf2.class }, handler);
155       ((MyProxyInf1) myProxy).setValue(2000);
156       ((MyProxyInf2) myProxy).setStringValue("Testing");
157
158       synchronized (mapRoot) {
159         mapRoot.put("proxy", myProxy);
160       }
161
162       Map JavaDoc clonedMap = null;
163       ManagerUtil.optimisticBegin();
164       clonedMap = (Map JavaDoc) ManagerUtil.deepCopy(mapRoot);
165       ManagerUtil.optimisticCommit();
166
167       Assert.assertEquals(1, clonedMap.size());
168       Proxy JavaDoc clonedProxy = (Proxy JavaDoc) clonedMap.get("proxy");
169
170       Assert.assertFalse(clonedProxy == myProxy);
171       Assert.assertEquals(2000, ((MyProxyInf1) clonedProxy).getValue());
172       Assert.assertEquals("Testing", ((MyProxyInf2) clonedProxy).getStringValue());
173     }
174
175     barrier.barrier();
176   }
177
178   private void cloneTest(int index) throws Throwable JavaDoc {
179     if (index == 0) {
180       MyInvocationHandler handler = new MyInvocationHandler();
181       Proxy JavaDoc myProxy = (Proxy JavaDoc) Proxy.newProxyInstance(this.getClass().getClassLoader(), new Class JavaDoc[] { MyProxyInf1.class,
182           MyProxyInf2.class }, handler);
183       ((MyProxyInf1) myProxy).setValue(2000);
184       ((MyProxyInf2) myProxy).setStringValue("Testing");
185       dataRoot.setMyProxy(myProxy);
186     }
187
188     barrier.barrier();
189
190     Object JavaDoc copy = null;
191     ManagerUtil.optimisticBegin();
192     copy = ManagerUtil.deepCopy(dataRoot.getMyProxy());
193     ManagerUtil.optimisticCommit();
194
195     Assert.assertTrue(copy instanceof Proxy JavaDoc);
196     Assert.assertTrue(copy instanceof MyProxyInf1);
197     Assert.assertTrue(copy instanceof MyProxyInf2);
198     Assert.assertFalse(copy == dataRoot.getMyProxy());
199
200     barrier.barrier();
201   }
202
203   private void multipleInterfacesProxyTest(int index) throws Throwable JavaDoc {
204     if (index == 0) {
205       MyInvocationHandler handler = new MyInvocationHandler();
206       Proxy JavaDoc myProxy = (Proxy JavaDoc) Proxy.newProxyInstance(this.getClass().getClassLoader(), new Class JavaDoc[] { MyProxyInf1.class,
207           MyProxyInf2.class }, handler);
208       ((MyProxyInf1) myProxy).setValue(2000);
209       ((MyProxyInf2) myProxy).setStringValue("Testing");
210       dataRoot.setMyProxy(myProxy);
211     }
212
213     barrier.barrier();
214
215     Assert.assertTrue(dataRoot.getMyProxy() instanceof MyProxyInf1);
216     Assert.assertTrue(dataRoot.getMyProxy() instanceof MyProxyInf2);
217
218     Assert.assertEquals(2000, ((MyProxyInf1) dataRoot.getMyProxy()).getValue());
219     Assert.assertEquals("Testing", ((MyProxyInf2) dataRoot.getMyProxy()).getStringValue());
220
221     barrier.barrier();
222   }
223
224   private void differentLoaderTest(int index) throws Throwable JavaDoc {
225     if (index == 0) {
226       MyMapInvocationHandler handler = new MyMapInvocationHandler();
227
228       Assert.assertFalse(this.getClass().getClassLoader() == Map JavaDoc.class.getClassLoader());
229
230       Object JavaDoc myProxy = Proxy.newProxyInstance(this.getClass().getClassLoader(), new Class JavaDoc[] { Map JavaDoc.class }, handler);
231
232       ((Map JavaDoc) myProxy).put("key1", "value1");
233       ((Map JavaDoc) myProxy).put("key2", "value2");
234
235       dataRoot.setMyProxy(myProxy);
236
237     }
238
239     barrier.barrier();
240
241     Assert.assertTrue(dataRoot.getMyProxy().getClass().getClassLoader() instanceof IsolationClassLoader);
242     Assert.assertEquals(2, ((Map JavaDoc) dataRoot.getMyProxy()).size());
243     Assert.assertEquals("value1", ((Map JavaDoc) dataRoot.getMyProxy()).get("key1"));
244     Assert.assertEquals("value2", ((Map JavaDoc) dataRoot.getMyProxy()).get("key2"));
245
246     barrier.barrier();
247   }
248
249   private void subclassProxyTest(int index) throws Throwable JavaDoc {
250     if (index == 0) {
251       MyInvocationHandler handler = new MyInvocationHandler();
252       MyProxyInf1 myProxy = new MySubProxy(handler);
253
254       myProxy.setValue(137);
255       dataRoot.setMyProxy(myProxy);
256     }
257
258     barrier.barrier();
259
260     Assert.assertFalse(Proxy.isProxyClass(dataRoot.getMyProxy().getClass()));
261
262     Assert.assertEquals(-1, ((MyProxyInf1) dataRoot.getMyProxy()).getValue());
263
264     barrier.barrier();
265   }
266   
267   private void proxySystemInterface(int index) throws Throwable JavaDoc {
268     if (index == 0) {
269       MyInvocationHandler handler = new MyInvocationHandler();
270       Collection JavaDoc colProxyImpl = (Collection JavaDoc) Proxy.newProxyInstance(System JavaDoc.class.getClassLoader(),
271                                                        new Class JavaDoc[] { Collection JavaDoc.class }, handler);
272       Assert.assertNull(colProxyImpl.getClass().getClassLoader());
273     }
274     barrier.barrier();
275   }
276
277   private void validProxyTest(int index) throws Throwable JavaDoc {
278     if (index == 0) {
279       MyInvocationHandler handler = new MyInvocationHandler();
280       proxyImpl = (MyProxyInf1) Proxy.newProxyInstance(MyProxyInf1.class.getClassLoader(),
281                                                        new Class JavaDoc[] { MyProxyInf1.class }, handler);
282
283     }
284
285     barrier.barrier();
286
287     if (index == 1) {
288       synchronized (proxyImpl) {
289         proxyImpl.setValue(123);
290       }
291     }
292
293     barrier.barrier();
294
295     Assert.assertTrue(proxyImpl.getClass().getClassLoader() instanceof IsolationClassLoader);
296     Assert.assertEquals(123, proxyImpl.getValue());
297
298     barrier.barrier();
299
300     if (index == 0) {
301       MyInvocationHandler handler = new MyInvocationHandler();
302       MyProxyInf1 myProxy = (MyProxyInf1) Proxy.newProxyInstance(MyProxyInf1.class.getClassLoader(),
303                                                                  new Class JavaDoc[] { MyProxyInf1.class }, handler);
304       myProxy.setValue(137);
305       dataRoot.setMyProxy(myProxy);
306     }
307
308     barrier.barrier();
309
310     Assert.assertEquals(137, ((MyProxyInf1) dataRoot.getMyProxy()).getValue());
311
312     barrier.barrier();
313
314     if (index == 1) {
315       dataRoot.setMyProxy(proxyImpl);
316     }
317
318     barrier.barrier();
319
320     Assert.assertTrue(dataRoot.getMyProxy() == proxyImpl);
321     Assert.assertEquals(123, ((MyProxyInf1) dataRoot.getMyProxy()).getValue());
322
323     barrier.barrier();
324
325   }
326
327   public static void visitL1DSOConfig(ConfigVisitor visitor, DSOClientConfigHelper config) {
328     TransparencyClassSpec spec = config.getOrCreateSpec(CyclicBarrier.class.getName());
329     config.addWriteAutolock("* " + CyclicBarrier.class.getName() + "*.*(..)");
330
331     String JavaDoc testClass = ProxyTestApp.class.getName();
332     spec = config.getOrCreateSpec(testClass);
333     config.addIncludePattern(testClass + "$DataRoot");
334     config.addIncludePattern(testClass + "$MySubProxy");
335     config.addIncludePattern(testClass + "$MyInvocationHandler");
336     config.addIncludePattern(testClass + "$MyMapInvocationHandler");
337     String JavaDoc methodExpression = "* " + testClass + "*.*(..)";
338     config.addWriteAutolock(methodExpression);
339
340     spec.addRoot("barrier", "barrier");
341     spec.addRoot("proxyImpl", "proxyImpl");
342     spec.addRoot("dataRoot", "dataRoot");
343     spec.addRoot("mapRoot", "mapRoot");
344   }
345
346   private static class MySubProxy extends Proxy JavaDoc implements MyProxyInf1 {
347     public MySubProxy(InvocationHandler JavaDoc h) {
348       super(h);
349     }
350
351     public int getValue() {
352       return -1;
353     }
354
355     public void setValue(int i) {
356       //
357
}
358   }
359
360   public interface MyProxySuperIntf {
361     public void setSuperIntfValue();
362   }
363
364   public interface MyProxyInf1 {
365     public int getValue();
366
367     public void setValue(int i);
368   }
369
370   public interface MyProxyInf2 extends MyProxySuperIntf {
371     public String JavaDoc getStringValue();
372
373     public void setStringValue(String JavaDoc str);
374   }
375
376   public static class MyInvocationHandler implements InvocationHandler JavaDoc {
377     private Map JavaDoc values = new HashMap JavaDoc();
378     private Map JavaDoc stringValues = new HashMap JavaDoc();
379
380     public Object JavaDoc invoke(Object JavaDoc proxy, Method JavaDoc method, Object JavaDoc[] args) throws Throwable JavaDoc {
381       if (method.getName().equals("getValue")) {
382         return values.get(proxy);
383       } else if (method.getName().equals("setValue")) {
384         values.put(proxy, args[0]);
385         return null;
386       } else if (method.getName().equals("setStringValue")) {
387         stringValues.put(proxy, args[0]);
388         return null;
389       } else if (method.getName().equals("getStringValue")) {
390         return stringValues.get(proxy);
391       } else if (method.getName().equals("hashCode")) { return new Integer JavaDoc(System.identityHashCode(proxy)); }
392       return null;
393     }
394   }
395
396   private static class MyMapInvocationHandler implements InvocationHandler JavaDoc {
397     private Map JavaDoc values = new HashMap JavaDoc();
398
399     public Object JavaDoc invoke(Object JavaDoc proxy, Method JavaDoc method, Object JavaDoc[] args) throws Throwable JavaDoc {
400       if (method.getName().equals("get")) {
401         return values.get(args[0]);
402       } else if (method.getName().equals("put")) {
403         values.put(args[0], args[1]);
404         return null;
405       } else if (method.getName().equals("size")) {
406         return new Integer JavaDoc(values.size());
407       } else if (method.getName().equals("hashCode")) { return new Integer JavaDoc(System.identityHashCode(proxy)); }
408       return null;
409     }
410   }
411
412   private static class DataRoot {
413     private Object JavaDoc myProxy;
414
415     public DataRoot() {
416       super();
417     }
418
419     public Object JavaDoc getMyProxy() {
420       return myProxy;
421     }
422
423     public synchronized void setMyProxy(Object JavaDoc myProxy) {
424       this.myProxy = myProxy;
425     }
426   }
427
428   public static class MyProxyClassLoader extends URLClassLoader JavaDoc {
429     private static final ClassLoader JavaDoc SYSTEM_LOADER = ClassLoader.getSystemClassLoader();
430
431     public MyProxyClassLoader() {
432       super(getSystemURLS(), null);
433     }
434
435     private static URL JavaDoc[] getSystemURLS() {
436       return ((URLClassLoader JavaDoc) SYSTEM_LOADER).getURLs();
437     }
438
439     public Class JavaDoc loadClass(String JavaDoc name) throws ClassNotFoundException JavaDoc {
440       return super.loadClass(name);
441     }
442
443   }
444 }
445
Popular Tags