KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > nl > justobjects > pushlet > core > Event


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.core;
5
6 import java.io.Serializable JavaDoc;
7 import java.util.HashMap JavaDoc;
8 import java.util.Iterator JavaDoc;
9 import java.util.Map JavaDoc;
10
11 /**
12  * Represents the event data.
13  *
14  * @version $Id: Event.java,v 1.10 2005/02/21 11:50:46 justb Exp $
15  * @author Just van den Broecke - Just Objects &copy;
16  **/

17 public class Event implements Protocol, Serializable JavaDoc {
18
19     protected Map JavaDoc attributes = new HashMap JavaDoc(3);
20
21     public Event(String JavaDoc anEventType) {
22         this(anEventType, null);
23     }
24
25     public Event(String JavaDoc anEventType, Map JavaDoc theAttributes) {
26
27         if (theAttributes != null) {
28             setAttrs(theAttributes);
29         }
30
31         // Set required field event type
32
setField(P_EVENT, anEventType);
33
34         // Set time in seconds since 1970
35
setField(P_TIME, System.currentTimeMillis() / 1000);
36     }
37
38     public Event(Map JavaDoc theAttributes) {
39         if (!theAttributes.containsKey(P_EVENT)) {
40             throw new IllegalArgumentException JavaDoc(P_EVENT + " not found in attributes");
41         }
42         setAttrs(theAttributes);
43     }
44
45     public static Event createDataEvent(String JavaDoc aSubject) {
46         return createDataEvent(aSubject, null);
47     }
48
49     public static Event createDataEvent(String JavaDoc aSubject, Map JavaDoc theAttributes) {
50         Event dataEvent = new Event(E_DATA, theAttributes);
51         dataEvent.setField(P_SUBJECT, aSubject);
52         return dataEvent;
53     }
54
55     public String JavaDoc getEventType() {
56         return getField(P_EVENT);
57     }
58
59     public String JavaDoc getSubject() {
60         return getField(P_SUBJECT);
61     }
62
63     public void setField(String JavaDoc name, String JavaDoc value) {
64         attributes.put(name, value);
65     }
66
67     public void setField(String JavaDoc name, int value) {
68         attributes.put(name, value + "");
69     }
70
71     public void setField(String JavaDoc name, long value) {
72         attributes.put(name, value + "");
73     }
74
75     public String JavaDoc getField(String JavaDoc name) {
76         return (String JavaDoc) attributes.get(name);
77     }
78
79     /** Return field; if null return default. */
80     public String JavaDoc getField(String JavaDoc name, String JavaDoc aDefault) {
81         String JavaDoc result = getField(name);
82         return result == null ? aDefault : result;
83     }
84
85     public Iterator JavaDoc getFieldNames() {
86         return attributes.keySet().iterator();
87     }
88
89     public String JavaDoc toString() {
90         return attributes.toString();
91     }
92
93     /** Convert to HTTP query string. */
94     public String JavaDoc toQueryString() {
95         String JavaDoc queryString = "";
96         String JavaDoc amp = "";
97         for (Iterator JavaDoc iter = getFieldNames(); iter.hasNext();) {
98             String JavaDoc nextAttrName = (String JavaDoc) iter.next();
99             String JavaDoc nextAttrValue = getField(nextAttrName);
100             queryString = queryString + amp + nextAttrName + "=" + nextAttrValue;
101             // After first add "&".
102
amp = "&";
103         }
104
105         return queryString;
106     }
107
108     public String JavaDoc toXML() {
109         String JavaDoc xmlString = "<event ";
110         for (Iterator JavaDoc iter = getFieldNames(); iter.hasNext();) {
111             String JavaDoc nextAttrName = (String JavaDoc) iter.next();
112             String JavaDoc nextAttrValue = getField(nextAttrName);
113             xmlString = xmlString + nextAttrName + "=\"" + nextAttrValue + "\" ";
114         }
115
116         xmlString += "/>";
117         return xmlString;
118     }
119
120     public Object JavaDoc clone() {
121         // Clone the Event by using copy constructor
122
return new Event(attributes);
123     }
124
125     /** Copy given attributes into event attributes */
126     private void setAttrs(Map JavaDoc theAttributes) {
127         attributes.putAll(theAttributes);
128     }
129 }
130
131 /*
132   * $Log: Event.java,v $
133   * Revision 1.10 2005/02/21 11:50:46 justb
134   * ohase1 of refactoring Subscriber into Session/Controller/Subscriber
135   *
136   * Revision 1.9 2005/02/20 13:05:32 justb
137   * removed the Postlet (integrated in Pushlet protocol)
138   *
139   * Revision 1.8 2005/02/15 13:29:24 justb
140   * add toQueryString()
141   *
142   * Revision 1.7 2005/01/18 16:47:10 justb
143   * protocol changes for v2 and publishing from pushlet client
144   *
145   * Revision 1.6 2005/01/13 14:47:15 justb
146   * control evt: send response on same (control) connection
147   *
148   * Revision 1.5 2004/09/03 22:35:37 justb
149   * Almost complete rewrite, just checking in now
150   *
151   * Revision 1.4 2004/08/15 16:00:15 justb
152   * enhancements to pull mode
153   *
154   * Revision 1.3 2003/08/15 08:37:40 justb
155   * fix/add Copyright+LGPL file headers and footers
156   *
157   * Revision 1.2 2003/05/18 16:15:08 justb
158   * support for XML encoded Events
159   *
160   * Revision 1.1.1.1 2002/09/24 21:02:30 justb
161   * import to sourceforge
162   *
163   */

164
Popular Tags