KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > tcverify > DSOVerifier


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.tcverify;
5
6 import com.tc.logging.TCLogger;
7 import com.tc.logging.TCLogging;
8 import com.tc.util.Assert;
9 import com.tc.verify.VerificationException;
10
11 import java.util.HashMap JavaDoc;
12 import java.util.Map JavaDoc;
13
14 /**
15  * Verifies that DSO is working correctly.
16  */

17 public class DSOVerifier {
18
19   private static final TCLogger logger = TCLogging.getTestingLogger(DSOVerifier.class);
20
21   private static final long TIMEOUT = 1 * 60 * 1000; // 1 minute
22
private static final long POLL_PERIOD = 1 * 1000; // 1 second
23

24   private final int myID;
25   private final int otherID;
26   private Map JavaDoc verifierMap = new HashMap JavaDoc();
27   private final Integer JavaDoc myIDInteger;
28   private final Integer JavaDoc otherIDInteger;
29
30   public DSOVerifier(int myID, int otherID) {
31     Assert.eval(myID != otherID);
32     this.myID = myID;
33     this.otherID = otherID;
34     this.myIDInteger = new Integer JavaDoc(this.myID);
35     this.otherIDInteger = new Integer JavaDoc(this.otherID);
36   }
37
38   public void verify() throws VerificationException {
39     setValue(this.myID);
40
41     long startTime = System.currentTimeMillis();
42     boolean correct = false;
43
44     while ((System.currentTimeMillis() - startTime) < TIMEOUT) {
45       logger.debug(myID + ": Fetching value from map.");
46       String JavaDoc value = (String JavaDoc) getValue(this.otherID);
47       logger.debug(myID + ": Got: " + value);
48       if (value != null) {
49         if (value.equals("PRESENT-" + otherID)) {
50           correct = true;
51           break;
52         } else {
53           throw new VerificationException("Got unexpected value '" + value + "' from other VM.");
54         }
55       }
56
57       try {
58         Thread.sleep(POLL_PERIOD);
59       } catch (InterruptedException JavaDoc ie) {
60         // whatever
61
}
62     }
63
64     logger.debug("All done. Correct? " + correct);
65     if (!correct) throw new VerificationException("Waited " + (System.currentTimeMillis() - startTime)
66                                                   + " milliseconds, but didn't get other VM's signal. Is DSO broken?");
67   }
68
69   private Object JavaDoc getValue(int id) {
70     logger.debug("Returning value for " + id);
71     return verifierMap.get(otherIDInteger);
72   }
73
74   private void setValue(int id) {
75     logger.debug("Setting value for " + id + " to " + id);
76     verifierMap.put(myIDInteger, "PRESENT-" + this.myID);
77   }
78
79   public static void main(String JavaDoc[] args) {
80     if (args.length != 2) {
81       System.err.println("Usage:");
82       System.err.println(" java " + DSOVerifier.class.getName() + " my-id other-id");
83       System.exit(2);
84     }
85
86     DSOVerifier verifier = new DSOVerifier(Integer.parseInt(args[0]), Integer.parseInt(args[1]));
87     try {
88       verifier.verify();
89       System.out.println("L2-DSO-OK: Terracotta L2 DSO server is running and DSOing properly.");
90       System.exit(0);
91     } catch (Throwable JavaDoc t) {
92       System.out.println("L2-DSO-FAIL: " + t.getMessage());
93       t.printStackTrace();
94       System.exit(1);
95     }
96   }
97 }
Free Books   Free Magazines  
Popular Tags