KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > tctest > TransparentWaitNotifyApp


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.simulator.app.ApplicationConfig;
9 import com.tc.simulator.listener.ListenerProvider;
10 import com.tc.util.Assert;
11 import com.tctest.runner.AbstractTransparentApp;
12
13 import java.util.HashMap JavaDoc;
14
15 public class TransparentWaitNotifyApp extends AbstractTransparentApp {
16   private static final String JavaDoc DONE = "done Biatch!";
17
18   private static final int INITIAL_STAGE = 0;
19   private static final int ABOUT_TO_WAIT = 1;
20   private static final int WAIT_COMPLETE = 2;
21
22   private final HashMap JavaDoc sharedRoot = new HashMap JavaDoc();
23
24   public TransparentWaitNotifyApp(String JavaDoc globalId, ApplicationConfig cfg, ListenerProvider listenerProvider) {
25     super(globalId, cfg, listenerProvider);
26
27     if ((getIntensity() != 1) || (getParticipantCount() != 2)) {
28       //
29
throw Assert.failure("Invalid parameters");
30     }
31
32   }
33
34   public void run() {
35     if ((new Integer JavaDoc(this.getApplicationId()).intValue() % 2) == 0) {
36       testWaiter();
37     } else {
38       testNotify();
39     }
40     notifyResult(Boolean.TRUE);
41   }
42
43   private void testNotify() {
44     moveToStage(INITIAL_STAGE);
45     moveToStageAndWait(ABOUT_TO_WAIT);
46
47     synchronized (sharedRoot) {
48       System.err.println("NOTIFY");
49       sharedRoot.notify();
50     }
51
52     moveToStageAndWait(WAIT_COMPLETE);
53
54     synchronized (sharedRoot) {
55       Assert.eval("key not in map", sharedRoot.containsKey(DONE));
56     }
57   }
58
59   private void testWaiter() {
60     moveToStage(INITIAL_STAGE);
61
62     synchronized (sharedRoot) {
63       moveToStage(ABOUT_TO_WAIT);
64
65       System.err.println("WAIT");
66
67       try {
68         sharedRoot.wait();
69         System.err.println("NOTIFIED");
70       } catch (InterruptedException JavaDoc e) {
71         throw new RuntimeException JavaDoc(e);
72       }
73
74
75       sharedRoot.put(DONE, null);
76     }
77
78     moveToStage(WAIT_COMPLETE);
79   }
80
81   public static void visitL1DSOConfig(ConfigVisitor visitor, DSOClientConfigHelper config) {
82     String JavaDoc testClassName = TransparentWaitNotifyApp.class.getName();
83     config.addRoot(testClassName, "sharedRoot", "sharedRootLock", true);
84     String JavaDoc methodExpression = "* " + testClassName + ".*(..)";
85     System.err.println("Adding autolock for: " + methodExpression);
86     config.addWriteAutolock(methodExpression);
87   }
88 }
Popular Tags