1 package org.sapia.ubik.mcast; 2 3 import java.io.ByteArrayInputStream ; 4 import java.io.ByteArrayOutputStream ; 5 import java.io.IOException ; 6 import java.io.ObjectInputStream ; 7 import java.io.ObjectOutputStream ; 8 9 10 31 public class RemoteEvent implements java.io.Serializable { 32 static final int BUFSZ = 1048; 34 static int _inc = 0; 35 static final int MAX_INC = 1000; 36 37 private String _domain; 39 private String _type; 40 private long _id = generateId(); 41 private String _node; 42 private byte[] _data; 43 private boolean _wasBytes; 44 private boolean _sync; 45 46 53 public RemoteEvent(String domain, String type, Object data) 54 throws IOException { 55 _domain = domain; 56 _type = type; 57 58 if ((data != null) && data instanceof byte[]) { 63 _wasBytes = true; 64 _data = (byte[]) data; 65 } else { 66 ByteArrayOutputStream bos = new ByteArrayOutputStream (BUFSZ); 67 ObjectOutputStream ous = new ObjectOutputStream (bos); 68 69 ous.writeObject(data); 70 ous.flush(); 71 ous.close(); 72 _data = bos.toByteArray(); 73 } 74 } 75 76 82 public RemoteEvent(String type, Object data) throws IOException { 83 this(null, type, data); 84 } 85 86 92 public String getDomainName() { 93 return _domain; 94 } 95 96 101 public String getType() { 102 return _type; 103 } 104 105 110 public long getId() { 111 return _id; 112 } 113 114 120 public Object getData() throws IOException { 121 if (_data != null) { 122 if (_wasBytes) { 123 return (byte[]) _data; 124 } else { 125 ByteArrayInputStream bis = new ByteArrayInputStream ((byte[]) _data); 126 ObjectInputStream ois = new ObjectInputStream (bis); 127 128 129 try { 130 Object obj = ois.readObject(); 131 return obj; 132 } catch (ClassNotFoundException e) { 133 throw new IOException (e.getClass().getName() + " caught: " + 134 e.getMessage()); 135 } finally { 136 ois.close(); 137 } 138 } 139 } 140 141 return _data; 142 } 143 144 150 public boolean hasDomainName() { 151 return _domain != null; 152 } 153 154 158 public boolean isSync() { 159 return _sync; 160 } 161 162 167 public String getNode() { 168 return _node; 169 } 170 171 RemoteEvent setNode(String 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 |