KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > nl > justobjects > pushlet > test > TestEventPullSources


1 // Copyright (c) 2000 Just Objects B.V. <just@justobjects.nl>
2
// Distributable under LGPL license. See terms of license at gnu.org.
3

4 package nl.justobjects.pushlet.test;
5
6 import nl.justobjects.pushlet.core.Event;
7 import nl.justobjects.pushlet.core.EventPullSource;
8 import nl.justobjects.pushlet.core.SessionManager;
9
10 import java.util.Random JavaDoc;
11
12 /**
13  * Event sources for testing.
14  *
15  * @version $Id: TestEventPullSources.java,v 1.9 2005/02/28 09:14:56 justb Exp $
16  * @author Just van den Broecke - Just Objects &copy;
17  **/

18 public class TestEventPullSources {
19
20     /** Produces a fake temparature event. */
21     static public class TemperatureEventPullSource extends EventPullSource {
22         String JavaDoc[] cities = {"amsterdam", null, "rotterdam", null,
23                            "leeuwarden", null, "twente", null, "limburg", null};
24
25         public long getSleepTime() {
26             return randomLong(3000, 5000);
27         }
28
29         public Event pullEvent() {
30             int cityNumber = randomInt(0, (cities.length) / 2 - 1);
31             int nextCityIndex = 2 * cityNumber;
32
33             Event event = Event.createDataEvent("/temperature");
34
35             event.setField("number", "" + cityNumber);
36             event.setField("city", cities[nextCityIndex]);
37             if (cities[nextCityIndex + 1] == null) {
38                 cities[nextCityIndex + 1] = "" + randomInt(5, 10);
39             }
40             int currentCityValue = new Integer JavaDoc(cities[nextCityIndex + 1]).intValue();
41             int newCityValue = currentCityValue + randomInt(-2, 2);
42
43             event.setField("value", "" + newCityValue);
44             return event;
45         }
46     }
47
48     /** Produces a ping event. */
49     static public class PingEventPullSource extends EventPullSource {
50         public long getSleepTime() {
51             return 3000;
52         }
53
54         public Event pullEvent() {
55
56             return Event.createDataEvent("/pushlet/ping");
57         }
58     }
59
60     /** Produces an event related to the JVM status. */
61     static public class SystemStatusEventPullSource extends EventPullSource {
62         Runtime JavaDoc runtime = Runtime.getRuntime();
63
64         public long getSleepTime() {
65             return 4000;
66         }
67
68         public Event pullEvent() {
69             Event event = Event.createDataEvent("/system/jvm");
70             event.setField("totalMemory", "" + runtime.totalMemory());
71             event.setField("freeMemory", "" + runtime.freeMemory());
72             event.setField("maxMemory", "" + runtime.maxMemory());
73             int activeCount = Thread.activeCount();
74             event.setField("threads", "" + activeCount);
75
76             return event;
77         }
78     }
79
80     /** Produces an event related to the Dispatcher.getInstance(). status. */
81     static public class PushletStatusEventPullSource extends EventPullSource {
82
83         public long getSleepTime() {
84             return 5000;
85         }
86
87         public Event pullEvent() {
88             Event event = Event.createDataEvent("/system/pushlet");
89             // p(Dispatcher.getStatus());
90
event.setField("publisher", "" + SessionManager.getInstance().getStatus());
91             return event;
92         }
93     }
94
95
96     /** Produces events simulating stocks from the AEX. */
97     static public class AEXStocksEventPullSource extends EventPullSource {
98
99         String JavaDoc[] stocks = {"abn amro", "26",
100                            "aegon", "38",
101                            "ahold", "34",
102                            "akzo nobel", "51",
103                            "asm lith h", "26",
104                            "corus plc", "2",
105                            "dsm", "40",
106                            "elsevier", "14",
107                            "fortis (nl)", "32",
108                            "getronics", "6",
109                            "gucci", "94",
110                            "hagemeyer", "25",
111                            "heineken", "61",
112                            "ing c", "78",
113                            "klm", "66",
114                            "kon olie", "66",
115                            "kpn", "13",
116                            "numico c", "44",
117                            "philips, kon", "38",
118                            "tnt", "26",
119                            "unilever c", "62",
120                            "vendex kbb", "16",
121                            "vnu", "49",
122                            "wolt-kluw c", "25"};
123
124         public long getSleepTime() {
125             return randomLong(2000, 4000);
126         }
127
128         public Event pullEvent() {
129             Event event = Event.createDataEvent("/stocks/aex");
130             int stockNumber = randomInt(0, (stocks.length) / 2 - 1);
131             int nextStockIndex = 2 * stockNumber;
132
133             event.setField("number", "" + stockNumber);
134             event.setField("name", stocks[nextStockIndex]);
135             if (stocks[nextStockIndex + 1] == null) {
136                 stocks[nextStockIndex + 1] = "" + randomInt(50, 150);
137             }
138             int currentStockValue = new Integer JavaDoc(stocks[nextStockIndex + 1]).intValue();
139             int newStockValue = currentStockValue + randomInt(-2, 2);
140
141             event.setField("rate", "" + newStockValue + "." + randomInt(0, 99));
142             return event;
143         }
144
145     }
146
147     /** Produces an URL event for automatic webpresentation. */
148     static public class WebPresentationEventPullSource extends EventPullSource {
149         String JavaDoc slideRootDir = "http://www.justobjects.org/cowcatcher/browse/j2ee/slides/";
150         String JavaDoc[] slideURLs = {
151             "ejb/j2ee/ejbover/slide.0.0.html",
152             "ejb/j2ee/ejbover/slide.0.1.html",
153             "ejb/j2ee/ejbover/slide.0.2.html",
154             "ejb/j2ee/ejbover/slide.0.3.html",
155             "ejb/j2ee/ejbover/slide.0.4.html"
156         };
157
158         int nextSlideNumber = 0;
159
160         public long getSleepTime() {
161             return 5000;
162         }
163
164         public Event pullEvent() {
165             Event event = Event.createDataEvent("/webpres/auto");
166             event.setField("url", slideRootDir + slideURLs[nextSlideNumber++]);
167             if (nextSlideNumber == slideURLs.length) {
168                 nextSlideNumber = 0;
169             }
170             // Log.debug("Sending next slide url=" + event.getField("url"));
171
return event;
172         }
173     }
174
175     /** Produces an event related to the Dispatcher.getInstance(). status. */
176     static public class TestEventPullSource extends EventPullSource {
177         private int number = 0;
178
179         public long getSleepTime() {
180             return 2000;
181         }
182
183         public Event pullEvent() {
184             Event event = Event.createDataEvent("/system/test");
185             // p(Dispatcher.getInstance()..getStatus());
186
event.setField("nr", "" + (number++));
187             event.setField("time", "" + System.currentTimeMillis());
188             return event;
189         }
190
191     }
192
193     /** Util: object used by random number generator methods. */
194     private static Random JavaDoc random = new Random JavaDoc();
195
196     /** Util: generate a random int between min and max value.*/
197     public static int randomInt(int min, int max) {
198         int nextRandom = random.nextInt();
199         return min + (nextRandom < 0 ? -nextRandom : nextRandom) % (max - min + 1);
200     }
201
202     /** Util: generate a random long between min and max value.*/
203     public static long randomLong(long min, long max) {
204         long nextRandom = random.nextLong();
205         return min + (nextRandom < 0 ? -nextRandom : nextRandom) % (max - min + 1);
206     }
207
208     /** Util: stderr print method.*/
209     public static void e(String JavaDoc s) {
210         System.out.println(s);
211     }
212
213     /** Util: stdout print method.*/
214     public static void p(String JavaDoc s) {
215         // System.out.println(s);
216
}
217 }
218
219 /*
220  * $Log: TestEventPullSources.java,v $
221  * Revision 1.9 2005/02/28 09:14:56 justb
222  * sessmgr/dispatcher factory/singleton support
223  *
224  * Revision 1.8 2005/02/21 16:59:17 justb
225  * SessionManager and session lease introduced
226  *
227  * Revision 1.7 2005/02/18 10:07:23 justb
228  * many renamings of classes (make names compact)
229  *
230  * Revision 1.6 2005/02/18 09:54:15 justb
231  * refactor: rename Publisher Dispatcher.getInstance(). and single Subscriber class
232  *
233  * Revision 1.5 2004/09/03 22:35:37 justb
234  * Almost complete rewrite, just checking in now
235  *
236  * Revision 1.4 2003/12/03 21:16:58 justb
237  * *** empty log message ***
238  *
239  * Revision 1.3 2003/08/15 08:37:41 justb
240  * fix/add Copyright+LGPL file headers and footers
241  *
242  * Revision 1.2 2003/05/18 16:15:08 justb
243  * support for XML encoded Events
244  *
245  * Revision 1.1.1.1 2002/09/24 21:02:33 justb
246  * import to sourceforge
247  *
248  * Revision 1.1.1.1 2002/09/20 22:48:19 justb
249  * import to SF
250  *
251  * Revision 1.1.1.1 2002/09/20 14:19:01 justb
252  * first import into SF
253  *
254  * Revision 1.6 2002/07/29 10:17:22 just
255  * no message
256  *
257  * Revision 1.5 2001/02/18 23:45:13 just
258  * fixes for AEX
259  *
260  * Revision 1.4 2000/10/30 14:16:09 just
261  * no message
262  *
263  * Revision 1.3 2000/08/31 12:49:50 just
264  * added CVS comment tags for log and copyright
265  *
266  *
267  */

268
Popular Tags