KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > test > propagation > PropagationReplAopTest


1 package test.propagation;
2
3 import junit.framework.TestCase;
4 import org.jboss.cache.pojo.PojoCache;
5 import org.jboss.cache.pojo.PojoCacheFactory;
6 import org.jboss.cache.config.Configuration;
7 import org.jboss.cache.factories.XmlConfigurationParser;
8 import org.jboss.cache.Fqn;
9 import propagation.PropagationManager;
10 import propagation.impl.PropagationManagerImpl;
11
12 /**
13  * Driver test to illustrate the sensor netowrk supervising system using pojo cache. By using the cache, it will have:
14  * <ul>
15  * <li>automatic state fail over</li>
16  * <li>fine-grained replication</li>
17  * <li>preservation of object graph relationship</li>
18  * </ul>
19  */

20 public class PropagationReplAopTest extends TestCase {
21    private PropagationManager pm_;
22    // cache1 and cache2 are in the same clustering group.
23
private PojoCache cache1_;
24    private PojoCache cache2_;
25
26    protected void setUp() throws Exception JavaDoc {
27       cache1_ = createCache("TestCluster");
28       cache2_ = createCache("TestCluster");
29       initPm();
30    }
31
32    protected void tearDown() throws Exception JavaDoc {
33       cache1_.getCache().removeNode(new Fqn("/"));
34       cache1_.stop();
35       cache2_.stop();
36    }
37
38    private PojoCache createCache(String JavaDoc name) throws Exception JavaDoc {
39       XmlConfigurationParser parser = new XmlConfigurationParser();
40       Configuration conf = parser.parseFile("META-INF/replSync-service.xml");
41       conf.setClusterName(name); // We can set a different cluster group.
42
boolean toStart = true;
43       PojoCache cache = PojoCacheFactory.createCache(conf, toStart);
44       // Or
45
// PojoCache cache = PojoCacheFactory.createCache("META-INF/replSync-service.xml", toStart);
46
return cache;
47    }
48
49    /**
50     * Populate the propagation tree.
51     *
52     * @throws Exception
53     */

54    protected void initPm() throws Exception JavaDoc {
55       pm_ = new PropagationManagerImpl();
56
57       pm_.setRootNode("Japan");
58
59       pm_.addNode("Japan", "Tokyo"); // Tokyo station
60

61       pm_.addNode("Japan.Tokyo", "WindSensor1"); // Wind sensor device
62
pm_.addStateItem("Japan.Tokyo.WindSensor1", 1000, "power supply", 1040); // power supply
63
pm_.addStateItem("Japan.Tokyo.WindSensor1", 1001, "sensor unit", 1040); // sensor unit
64

65       pm_.addNode("Japan.Tokyo", "RainSensor1"); // rain sensor device
66
pm_.addStateItem("Japan.Tokyo.RainSensor1", 1002, "power supply", 1040); // power supply
67
pm_.addStateItem("Japan.Tokyo.RainSensor1", 1003, "sensor unit", 1040); // sensor unit
68

69       pm_.addNode("Japan", "Yokohama"); // Yokohama station
70

71       pm_.addNode("Japan.Yokohama", "WindSensor2"); // wind sensor device
72
pm_.addStateItem("Japan.Yokohama.WindSensor2", 1000, "power supply", 1040); // power supply
73
pm_.addStateItem("Japan.Yokohama.WindSensor2", 1001, "sensor unit", 1040); // sensor unit
74

75       pm_.addNode("Japan.Yokohama", "RainSensor2"); // rain sensor device
76
pm_.addStateItem("Japan.Yokohama.RainSensor2", 1002, "power supply", 1040); // power supply
77
pm_.addStateItem("Japan.Yokohama.RainSensor2", 1003, "sensor unit", 1040); // sensor unit
78

79       pm_.createNode("WindSummary", "WindSummary"); // summary node for wind sensors in this network
80
pm_.setUpperNode("WindSummary", "Japan.Tokyo.WindSensor1"); // assoication
81
pm_.setUpperNode("WindSummary", "Japan.Yokohama.WindSensor2"); // association
82

83       pm_.createNode("RainSummary", "RainSummary"); // summary node for rain sensor in this network.
84
pm_.setUpperNode("RainSummary", "Japan.Tokyo.RainSensor1"); // association
85
pm_.setUpperNode("RainSummary", "Japan.Yokohama.RainSensor2"); // association
86
}
87
88    public void testPropagation() throws Exception JavaDoc {
89       // Here we ask the pojo cache to manage pm
90
cache1_.attach("monitor", pm_);
91
92       // Output
93
printStatus("Initial state", pm_);
94
95       // Retrieve the pojo from the Server #2
96
PropagationManager pm2 = (PropagationManager) cache2_.find("monitor");
97
98       System.out.println("---------------------------------------------");
99       System.out.println("Modified on Server #1");
100        // A state has been changed in one of the item. This will be fine-grained replicated.
101
pm_.stateChange("Japan.Tokyo.RainSensor1", 1003, 1041);
102       printStatus("Japan.Tokyo.RainSensor1: id: 1003 state: 1040->1041 (retrieved from cache #2)", pm2);
103
104       System.out.println("---------------------------------------------");
105       System.out.println("Modified on Server #2");
106       // A state has been changed in one of the item. This will be fine-grained replicated.
107
pm2.stateChange("Japan.Yokohama.WindSensor2", 1001, 1041); // Modified state on cache #2
108
printStatus("Japan.Yokohama.WindSensor2: id: 1001 state: 1040->1041 (retrieved from cache #1)"
109               , pm2);
110
111    }
112
113    private void printStatus(String JavaDoc msg, PropagationManager pm) {
114       System.out.println("---------------------------------------------");
115       System.out.println(msg);
116       System.out.println("---------------------------------------------");
117       pm.printNodes();
118 // System.out.println("--------------------");
119
// pm.printNodes("WindSummary");
120
// System.out.println("--------------------");
121
// pm.printNodes("RainSummary");
122
System.out.println("\n\n");
123    }
124
125    public static void main(String JavaDoc[] args) throws Exception JavaDoc {
126       PropagationReplAopTest pmTest = new PropagationReplAopTest();
127       pmTest.setUp();
128       pmTest.testPropagation();
129       pmTest.tearDown();
130    }
131 }
132
Popular Tags