KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > tctest > WaitNotifyUpgradedLocksTestApp


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.ConfigLockLevel;
8 import com.tc.object.config.DSOClientConfigHelper;
9 import com.tc.object.config.TransparencyClassSpec;
10 import com.tc.object.tx.ReadOnlyException;
11 import com.tc.simulator.app.ApplicationConfig;
12 import com.tc.simulator.listener.ListenerProvider;
13 import com.tctest.runner.AbstractTransparentApp;
14
15 import java.util.HashMap JavaDoc;
16 import java.util.HashSet JavaDoc;
17 import java.util.Map JavaDoc;
18 import java.util.Set JavaDoc;
19
20 public class WaitNotifyUpgradedLocksTestApp extends AbstractTransparentApp {
21
22   private static final int NODES = 2;
23
24   private final Set JavaDoc set = new HashSet JavaDoc();
25   private final Map JavaDoc map = new HashMap JavaDoc();
26
27   public WaitNotifyUpgradedLocksTestApp(String JavaDoc appId, ApplicationConfig cfg, ListenerProvider listenerProvider) {
28     super(appId, cfg, listenerProvider);
29
30     if (getParticipantCount() != NODES) { throw new RuntimeException JavaDoc("only use " + NODES + " nodes with this test"); }
31   }
32
33   public static void visitL1DSOConfig(ConfigVisitor visitor, DSOClientConfigHelper config) {
34     String JavaDoc testClass = WaitNotifyUpgradedLocksTestApp.class.getName();
35     TransparencyClassSpec spec = config.getOrCreateSpec(testClass);
36     config.addIncludePattern(testClass + "$*");
37     
38     String JavaDoc methodExpression = "* " + testClass + ".run()";
39     config.addWriteAutolock(methodExpression);
40
41     methodExpression = "* " + testClass + "$UpgradeWait.run()";
42     config.addWriteAutolock(methodExpression);
43
44     methodExpression = "* " + testClass + ".runRead(..)";
45     config.addAutolock(methodExpression, ConfigLockLevel.READ);
46
47     methodExpression = "* " + testClass + ".runWrite(..)";
48     config.addAutolock(methodExpression, ConfigLockLevel.WRITE);
49
50     spec.addRoot("set", "setLock");
51     spec.addRoot("map", "mapLock");
52   }
53
54   public void run() {
55     final boolean first;
56
57     synchronized (set) {
58       first = set.size() == 0;
59       if (first) set.add(new Object JavaDoc());
60     }
61
62     if (first) {
63       runUpgrade(map, new UpgradeWait());
64     } else {
65       synchronized (set) {
66         while (set.size() != NODES) {
67           try {
68             set.wait();
69           } catch (InterruptedException JavaDoc e) {
70             throw new RuntimeException JavaDoc(e);
71           }
72         }
73       }
74
75       runUpgrade(map, new Runnable JavaDoc() {
76         public void run() {
77           map.notify();
78         }
79       });
80     }
81   }
82
83   public void runUpgrade(final Map JavaDoc m, final Runnable JavaDoc action) {
84     runRead(m, new Runnable JavaDoc() {
85       public void run() {
86         runWrite(m, action);
87       }
88     });
89   }
90
91   public void runRead(Map JavaDoc m, Runnable JavaDoc action) {
92     synchronized (m) {
93       action.run();
94
95       try {
96         m.put(new Object JavaDoc(), null);
97         throw new RuntimeException JavaDoc("Not in a read only context");
98       } catch (ReadOnlyException r) {
99           // expected
100
}
101     }
102   }
103
104   public void runWrite(Map JavaDoc m, Runnable JavaDoc action) {
105     synchronized (m) {
106       action.run();
107     }
108   }
109
110   private class UpgradeWait implements Runnable JavaDoc {
111     public void run() {
112       // notify the other thread, but only after holding the upgrade on "map"
113
synchronized (set) {
114         set.add(new Object JavaDoc());
115         set.notify();
116       }
117
118       try {
119         map.wait();
120       } catch (InterruptedException JavaDoc e) {
121         throw new RuntimeException JavaDoc(e);
122       }
123     }
124   }
125
126 }
127
Popular Tags