KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > tctest > restart > unit > DSOServerRestartTestApp


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.restart.unit;
5
6 import EDU.oswego.cs.dl.util.concurrent.CyclicBarrier;
7 import EDU.oswego.cs.dl.util.concurrent.FutureResult;
8
9 import com.tc.exception.TCRuntimeException;
10 import com.tc.object.config.ConfigVisitor;
11 import com.tc.object.config.ConfigLockLevel;
12 import com.tc.object.config.DSOClientConfigHelper;
13 import com.tc.object.config.LockDefinition;
14 import com.tctest.restart.AbstractRestartTestApp;
15
16 import java.lang.reflect.InvocationTargetException JavaDoc;
17
18 public class DSOServerRestartTestApp extends AbstractRestartTestApp implements RestartUnitTestApp {
19
20   private final FutureResult fallThroughControl = new FutureResult();
21   private final Object JavaDoc[] sharedLockHolder = new Object JavaDoc[1];
22   private final Object JavaDoc[] distributedSharedLockHolder = new Object JavaDoc[1];
23   private CyclicBarrier startBarrier;
24   public DSOServerRestartTestApp(ThreadGroup JavaDoc threadGroup) {
25     super(threadGroup);
26     changeState(INIT);
27   }
28
29   public String JavaDoc toString() {
30     return getClass().getName() + "[" + getID() + ", " + state.peek() + "]";
31   }
32
33   public void setStartBarrier(CyclicBarrier startBarrier) {
34     this.startBarrier = startBarrier;
35   }
36
37   public void setSharedLock(Object JavaDoc lck) {
38     synchronized (this.sharedLockHolder) {
39       this.sharedLockHolder[0] = lck;
40     }
41   }
42
43   public void setDistributedSharedLock(Object JavaDoc lck) {
44     synchronized (this.distributedSharedLockHolder) {
45       this.distributedSharedLockHolder[0] = lck;
46     }
47   }
48
49   public Object JavaDoc getSharedLock() {
50     Object JavaDoc rv = null;
51     synchronized (this.sharedLockHolder) {
52       if (this.sharedLockHolder[0] != null) rv = this.sharedLockHolder[0];
53     }
54     if (rv == null) {
55       synchronized (this.distributedSharedLockHolder) {
56         rv = this.distributedSharedLockHolder[0];
57       }
58     }
59     System.out.println("returning shared lock...");
60     return rv;
61   }
62
63   public synchronized void reset() {
64     if (!(isEnd() || isInit())) throw new IllegalStateException JavaDoc("call to reset() when not in STOP state or INIT");
65     changeState(INIT);
66   }
67
68   public void doWait(long millis) {
69     if (!isInit()) throw new IllegalStateException JavaDoc("call to doWait() when not in INIT state.");
70     try {
71       changeState(START);
72       this.startBarrier.barrier();
73       System.out.println("About to call basicDoWait()...");
74       basicDoWait(millis);
75       changeState(END);
76     } catch (Exception JavaDoc e) {
77       throw new TCRuntimeException(e);
78     }
79   }
80
81   private void basicDoWait(long millis) throws InterruptedException JavaDoc {
82     Object JavaDoc sharedLock = getSharedLock();
83     synchronized (sharedLock) {
84       changeState(WAITER);
85       sharedLock.wait(millis);
86       changeState(HOLDER);
87       // clear the state so no one can check it until it is set again.
88
this.state.clear();
89     }
90   }
91
92   public void doNotify() {
93     Object JavaDoc sharedLock = getSharedLock();
94     synchronized (sharedLock) {
95       System.out.println("About to call notify()");
96       sharedLock.notify();
97     }
98   }
99
100   public void doNotifyAll() {
101     Object JavaDoc sharedLock = getSharedLock();
102     synchronized (sharedLock) {
103       System.out.println("About to call notifyAll()");
104       sharedLock.notifyAll();
105     }
106   }
107
108   public void attemptLock() {
109     if (!isInit()) throw new IllegalStateException JavaDoc("call to attemptLock() when not in INIT state.");
110     try {
111       changeState(START);
112       this.startBarrier.barrier();
113       System.out.println("About to call basicAttemptLock()...");
114       basicAttemptLock();
115       changeState(END);
116     } catch (Exception JavaDoc e) {
117       throw new TCRuntimeException(e);
118     }
119   }
120
121   private void basicAttemptLock() throws InterruptedException JavaDoc, InvocationTargetException JavaDoc {
122     synchronized (getSharedLock()) {
123       // synchronizing on the shared lock simulates the named lock on this
124
// method.
125
changeState(HOLDER);
126       fallThroughControl.get();
127       // clear the state so no one can check the state until it is set again.
128
this.state.clear();
129     }
130   }
131
132   public void blockShutdown(final FutureResult blocker) throws Exception JavaDoc {
133     final FutureResult callback = new FutureResult();
134     new Thread JavaDoc(this.threadGroup, new Runnable JavaDoc() {
135       public void run() {
136         try {
137           syncBlockShutdown(blocker, callback);
138         } catch (Exception JavaDoc e ) {
139           throw new TCRuntimeException(e);
140         }
141       }
142     }).start();
143     callback.get();
144   }
145
146   private void syncBlockShutdown(FutureResult blocker, FutureResult callback) throws InterruptedException JavaDoc,
147       InvocationTargetException JavaDoc {
148     System.out.println("Blocking shutdown...");
149     callback.set(new Object JavaDoc());
150     blocker.get();
151   }
152
153   public void fallThrough() {
154     if (!isHolder()) throw new IllegalStateException JavaDoc("Attempt to release a lock without owning it.");
155     try {
156       fallThroughControl.set(new Object JavaDoc());
157       synchronized (this) {
158         while (!isEnd())
159           wait();
160       }
161     } catch (InterruptedException JavaDoc e) {
162       throw new TCRuntimeException(e);
163     }
164   }
165
166   public static void visitL1DSOConfig(ConfigVisitor visitor, DSOClientConfigHelper cfg) {
167     String JavaDoc appClassName = DSOServerRestartTestApp.class.getName();
168     cfg.addIncludePattern(appClassName);
169     cfg.addRoot(appClassName, "distributedSharedLockHolder", appClassName + ".distributedSharedLockHolder", true);
170     cfg.addWriteAutolock("void " + appClassName + ".setDistributedSharedLock(java.lang.Object)");
171     cfg.addReadAutolock("java.lang.Object " + appClassName + ".getSharedLock()");
172
173     cfg.addWriteAutolock("void " + appClassName + ".basicDoWait(..)");
174     cfg.addWriteAutolock("void " + appClassName + ".doNotify()");
175     cfg.addWriteAutolock("void " + appClassName + ".doNotifyAll()");
176     LockDefinition lockDefinition = new LockDefinition("basicAttemptLock", ConfigLockLevel.WRITE);
177     lockDefinition.commit();
178     cfg.addLock("void " + appClassName + ".basicAttemptLock()", lockDefinition);
179
180     lockDefinition = new LockDefinition("blockShutdown", ConfigLockLevel.WRITE);
181     lockDefinition.commit();
182     cfg.addLock("void " + appClassName + ".syncBlockShutdown(..)", lockDefinition);
183   }
184
185 }
186
Popular Tags