KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > tctest > NestedTransactionApp


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.config.TransparencyClassSpec;
9 import com.tc.simulator.app.ApplicationConfig;
10 import com.tc.simulator.listener.ListenerProvider;
11 import com.tc.util.Assert;
12 import com.tc.util.concurrent.ThreadUtil;
13 import com.tctest.runner.AbstractTransparentApp;
14
15 import java.util.ArrayList JavaDoc;
16 import java.util.LinkedList JavaDoc;
17 import java.util.List JavaDoc;
18
19 public class NestedTransactionApp extends AbstractTransparentApp {
20
21   private static final String JavaDoc TARGET_CLASS_NAME = "com.tctest.NestedTransactionApp";
22   private static final String JavaDoc TARGET_INNER_CLASS_NAME = "com.tctest.NestedTransactionApp$TestObj";
23
24   private final static int ACTIONS = 20;
25   public final static int NODE_COUNT = 3;
26
27   private int myCount;
28   private int totalCount;
29
30   private final List JavaDoc list1;
31   private List JavaDoc list2 = new ArrayList JavaDoc();
32   private List JavaDoc list3 = new ArrayList JavaDoc();
33
34   public NestedTransactionApp(String JavaDoc appId, ApplicationConfig cfg, ListenerProvider listenerProvider) {
35     super(appId, cfg, listenerProvider);
36     this.list1 = new LinkedList JavaDoc();
37     this.myCount = ACTIONS;
38     this.totalCount = ACTIONS * NODE_COUNT;
39   }
40
41   public static void visitL1DSOConfig(ConfigVisitor visitor, DSOClientConfigHelper config) {
42     TransparencyClassSpec spec = config.getOrCreateSpec(TARGET_CLASS_NAME);
43
44     config.getOrCreateSpec(TARGET_INNER_CLASS_NAME);
45
46     spec.addRoot("list1", "stuff1");
47     spec.addRoot("list2", "stuff2");
48     spec.addRoot("list3", "stuff3");
49
50     String JavaDoc methodPattern = "* " + TARGET_CLASS_NAME + ".add1(..)";
51     config.addWriteAutolock(methodPattern);
52
53     methodPattern = "* " + TARGET_CLASS_NAME + ".add2(..)";
54     config.addWriteAutolock(methodPattern);
55
56     methodPattern = "* " + TARGET_CLASS_NAME + ".add3(..)";
57     config.addWriteAutolock(methodPattern);
58
59     methodPattern = "* " + TARGET_CLASS_NAME + ".move(..)";
60     config.addWriteAutolock(methodPattern);
61
62     methodPattern = "* " + TARGET_CLASS_NAME + ".remove1(..)";
63     config.addWriteAutolock(methodPattern);
64
65     methodPattern = "* " + TARGET_CLASS_NAME + ".remove2(..)";
66     config.addWriteAutolock(methodPattern);
67
68     methodPattern = "* " + TARGET_CLASS_NAME + ".finished(..)";
69     config.addWriteAutolock(methodPattern);
70
71     methodPattern = "* " + TARGET_CLASS_NAME + ".notDone(..)";
72     config.addWriteAutolock(methodPattern);
73
74   }
75
76   public void add1(TestObj testObj) {
77     synchronized (list1) {
78       int s = list1.size();
79       list1.add(testObj);
80       Assert.eval(s + 1 == list1.size());
81       // System.out.println("Added1:"+list1.size());
82
}
83   }
84
85   public void add3(TestObj testObj) {
86     synchronized (list3) {
87       int s = list3.size();
88       list3.add(testObj);
89       Assert.eval(s + 1 == list3.size());
90       // System.out.println("Added1:"+list1.size());
91
}
92   }
93
94   public void add2(TestObj testObj) {
95     synchronized (list2) {
96       try {
97         int s = list2.size();
98         list2.add(testObj);
99         Assert.eval(s + 1 == list2.size());
100         // System.out.println("Added2:"+list2.size());
101
} catch (Throwable JavaDoc e) {
102         e.printStackTrace();
103       }
104     }
105   }
106
107   public void move() {
108     synchronized (list1) {
109       synchronized (list2) {
110         try {
111           if (list1.size() > 0) {
112             // System.out.println("Moving from 1 to 2");
113
int s = list1.size();
114             TestObj obj = remove1();
115             // Assert.eval(s - 1 == list1.size());
116
if (s - 1 != list1.size()) { throw new AssertionError JavaDoc("s - 1 (" + (s - 1) + ") != list1.size() ("
117                                                                   + list1.size() + ")"); }
118             // System.out.println("list1 size:"+list1.size());
119
add2(obj);
120           }
121         } catch (Throwable JavaDoc e) {
122           e.printStackTrace();
123         }
124       }
125     }
126   }
127
128   public TestObj remove1() {
129     TestObj to = null;
130     try {
131       int s = list1.size();
132       if (s > 0) {
133         to = (TestObj) list1.remove(0);
134         if (s - 1 != list1.size()) { throw new AssertionError JavaDoc("s - 1 (" + (s - 1) + ") != list1.size() ("
135                                                               + list1.size() + ")"); }
136         // Assert.eval(s - 1 == list1.size());
137
}
138     } catch (Throwable JavaDoc e) {
139       e.printStackTrace();
140     }
141     return to;
142
143   }
144
145   public void run() {
146     add3(new TestObj());
147
148     for (int i = 0; i < myCount; i++) {
149       add1(new TestObj());
150     }
151
152     while (notDone()) {
153       ThreadUtil.reallySleep(50);
154       move();
155     }
156     finished();
157   }
158
159   private void finished() {
160     synchronized (list2) {
161       if (list2.size() != totalCount) { throw new AssertionError JavaDoc("list2.size()=" + list2.size() + ", expected: "
162                                                                  + totalCount); }
163     }
164   }
165
166   private boolean notDone() {
167     synchronized (list2) {
168       if (list2.size() > totalCount - 10) System.err.println("list2.size()=" + list2.size() + " still less than "
169                                                              + totalCount + ". NOT DONE. list1.size()=" + list1.size()
170                                                              + ", list3.size()=" + list3.size());
171       return list2.size() < totalCount;
172     }
173   }
174
175   private class TestObj {
176     //
177
}
178 }
Popular Tags