KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > sapia > ubik > mcast > RemoteEvent


1 package org.sapia.ubik.mcast;
2
3 import java.io.ByteArrayInputStream JavaDoc;
4 import java.io.ByteArrayOutputStream JavaDoc;
5 import java.io.IOException JavaDoc;
6 import java.io.ObjectInputStream JavaDoc;
7 import java.io.ObjectOutputStream JavaDoc;
8
9
10 /**
11  * Models a multicast event. An instance of this class strictly encapsulates its
12  * data in the form of strings, in order to avoid classloading issues when
13  * serializing/deserializing in a networked environment. A multicast is sent to a
14  * domain, or to all domains, according to the domain name information that is
15  * kept an the event.
16  * <p>
17  * Furthermore, a multicast event has a "type", which provides the event's
18  * logical type - and allows applications to register for events of a given logical
19  * type.
20  * <p>
21  * Finally, data can also be passed.
22  *
23  *
24  * @author Yanick Duchesne
25  * <dl>
26  * <dt><b>Copyright:</b><dd>Copyright &#169; 2002-2003 <a HREF="http://www.sapia-oss.org">Sapia Open Source Software</a>. All Rights Reserved.</dd></dt>
27  * <dt><b>License:</b><dd>Read the license.txt file of the jar or visit the
28  * <a HREF="http://www.sapia-oss.org/license.html">license page</a> at the Sapia OSS web site</dd></dt>
29  * </dl>
30  */

31 public class RemoteEvent implements java.io.Serializable JavaDoc {
32   // CLASS VARIABLES
33
static final int BUFSZ = 1048;
34   static int _inc = 0;
35   static final int MAX_INC = 1000;
36
37   // MEMBER VARIABLES
38
private String JavaDoc _domain;
39   private String JavaDoc _type;
40   private long _id = generateId();
41   private String JavaDoc _node;
42   private byte[] _data;
43   private boolean _wasBytes;
44   private boolean _sync;
45
46   /**
47    * Creates an instance of this class.
48    *
49    * @param the name of the domain to which this instance is targeted.
50    * @param type the event's type, which in fact is its logical type.
51    * @param data the event's data.
52    */

53   public RemoteEvent(String JavaDoc domain, String JavaDoc type, Object JavaDoc data)
54     throws IOException JavaDoc {
55     _domain = domain;
56     _type = type;
57
58     // if (data == null){
59
// //noop;
60
// }
61
// else
62
if ((data != null) && data instanceof byte[]) {
63       _wasBytes = true;
64       _data = (byte[]) data;
65     } else {
66       ByteArrayOutputStream JavaDoc bos = new ByteArrayOutputStream JavaDoc(BUFSZ);
67       ObjectOutputStream JavaDoc ous = new ObjectOutputStream JavaDoc(bos);
68
69       ous.writeObject(data);
70       ous.flush();
71       ous.close();
72       _data = bos.toByteArray();
73     }
74   }
75
76   /**
77    * Creates an instance of this class that is targeted at all domains.
78    *
79    * @param type the event's type, which in fact is its logical type.
80    * @param data the event's data.
81    */

82   public RemoteEvent(String JavaDoc type, Object JavaDoc data) throws IOException JavaDoc {
83     this(null, type, data);
84   }
85
86   /**
87    * Returns this instance's domain name.
88    *
89    * @return a domain name, or <code>null</code> if this instance
90    * is not targeted at a single domain.
91    */

92   public String JavaDoc getDomainName() {
93     return _domain;
94   }
95
96   /**
97    * Returns this instance's logical typeentifier.
98    *
99    * @return a logical typeentifier.
100    */

101   public String JavaDoc getType() {
102     return _type;
103   }
104
105   /**
106    * Returns this event's unique identifier.
107    *
108    * @return a unique ID, as a string.
109    */

110   public long getId() {
111     return _id;
112   }
113
114   /**
115    * Returns this instance's data.
116    *
117    * @return this event's data, or <code>null</code> if this
118    * instance has no data.
119    */

120   public Object JavaDoc getData() throws IOException JavaDoc {
121     if (_data != null) {
122       if (_wasBytes) {
123         return (byte[]) _data;
124       } else {
125         ByteArrayInputStream JavaDoc bis = new ByteArrayInputStream JavaDoc((byte[]) _data);
126         ObjectInputStream JavaDoc ois = new ObjectInputStream JavaDoc(bis);
127         
128
129         try {
130           Object JavaDoc obj = ois.readObject();
131           return obj;
132         } catch (ClassNotFoundException JavaDoc e) {
133           throw new IOException JavaDoc(e.getClass().getName() + " caught: " +
134             e.getMessage());
135         } finally {
136           ois.close();
137         }
138       }
139     }
140
141     return _data;
142   }
143
144   /**
145    * Returns <code>true</code> if this instance was created with a
146    * domain name - meaning that it was targeted at a single domain.
147    *
148    * @return <code>true</code> if this instance has a domain name.
149    */

150   public boolean hasDomainName() {
151     return _domain != null;
152   }
153
154   /**
155    * Returns <code>true</code> if this instance represents an event
156    * that necessitates a synchronous response.
157    */

158   public boolean isSync() {
159     return _sync;
160   }
161
162   /**
163    * Returns the identifier of the node that sent this event.
164    *
165    * @return a node identifier,
166    */

167   public String JavaDoc getNode() {
168     return _node;
169   }
170
171   RemoteEvent setNode(String JavaDoc node) {
172     _node = node;
173
174     return this;
175   }
176
177   RemoteEvent setSync() {
178     _sync = true;
179
180     return this;
181   }
182
183   private static synchronized long generateId() {
184     if (_inc++ > MAX_INC) {
185       _inc = 0;
186     }
187
188     return Long.parseLong("" + System.currentTimeMillis() + _inc);
189   }
190 }
191
Popular Tags