KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > tctest > LockUpgradeSystemTestApp


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 com.tc.object.config.ConfigVisitor;
7 import com.tc.object.config.DSOClientConfigHelper;
8 import com.tc.object.tx.ReadOnlyException;
9 import com.tc.simulator.app.ApplicationConfig;
10 import com.tc.simulator.listener.ListenerProvider;
11 import com.tctest.runner.AbstractTransparentApp;
12
13 import java.util.HashMap JavaDoc;
14 import java.util.Map JavaDoc;
15 import java.util.Random JavaDoc;
16
17 public class LockUpgradeSystemTestApp extends AbstractTransparentApp {
18
19   private Map JavaDoc root = new HashMap JavaDoc();
20
21   public LockUpgradeSystemTestApp(String JavaDoc globalId, ApplicationConfig cfg, ListenerProvider listenerProvider) {
22     super(globalId, cfg, listenerProvider);
23   }
24
25   public static void visitL1DSOConfig(ConfigVisitor visitor, DSOClientConfigHelper config) {
26     String JavaDoc testClassName = LockUpgradeSystemTestApp.class.getName();
27
28     config.addRoot(testClassName, "root", "root" + "Lock", true);
29
30     String JavaDoc methodExpression = "* " + testClassName + ".writeLock(..)";
31     config.addWriteAutolock(methodExpression);
32
33     methodExpression = "* " + testClassName + ".readLock(..)";
34     config.addReadAutolock(methodExpression);
35   }
36
37   public void run() {
38     Random JavaDoc random = new Random JavaDoc(new Random JavaDoc(System.currentTimeMillis() + getApplicationId().hashCode()).nextLong());
39
40     for (int i = 0; i < 100; i++) {
41       readLock(random.nextInt(4));
42     }
43   }
44
45   private void readLock(int depth) {
46     synchronized (root) {
47       // we should have a read lock now
48

49       // upgrade
50
writeLock(depth);
51
52       // shouldn't be able to write
53
tryWrite();
54     }
55   }
56
57   private void readLock() {
58     synchronized (root) {
59       tryWrite();
60     }
61   }
62
63   private void tryWrite() {
64     try {
65       root.put("key", "value");
66       throw new RuntimeException JavaDoc("read-only transaction context is busted");
67     } catch ( ReadOnlyException roe) {
68       // expected
69
}
70   }
71
72   private void writeLock(int depth) {
73     synchronized (root) {
74       readLock();
75       writeLock();
76
77       if (depth > 0) {
78         writeLock(depth - 1);
79       }
80     }
81   }
82
83   private void writeLock() {
84     synchronized (root) {
85       //
86
}
87   }
88
89 }
90
Popular Tags