1 18 19 package org.objectweb.jac.core; 20 21 import java.io.*; 22 import java.util.*; 23 import org.apache.log4j.Logger; 24 25 46 47 public class SerializedJacObject implements Serializable { 48 static Logger logger = Logger.getLogger("serialization"); 49 50 public static final String STATELESS="SerializedJacObject.STATELESS"; 51 52 68 69 public static byte[] serialize(Object src) { 70 71 ByteArrayOutputStream baos = new ByteArrayOutputStream(); 72 73 ObjectOutputStream oos = null; 74 byte[] ret = null; 75 76 if(src instanceof Collaboration) { 77 Collaboration init=(Collaboration)src; 78 Collaboration dest=new Collaboration(); 79 80 Iterator it=Collaboration.globalAttributeNames().iterator(); 81 while(it.hasNext()) { 82 String name=(String )it.next(); 83 if(init.getAttribute(name)!=null) { 84 dest.addAttribute( 85 name, 86 init.getAttribute(name)); 87 } 88 } 89 logger.debug("serializing "+dest); 90 src=dest; 91 } 92 93 try { 94 oos = (ObjectOutputStream) new JacObjectOutputStream(baos); 95 logger.debug("serialize "+src); 96 oos.writeObject(src); 97 oos.close(); 98 ret = baos.toByteArray(); 99 baos.close(); 100 } catch(Exception e) { 101 e.printStackTrace(); 102 } 103 104 return ret; 105 } 106 107 109 110 public static byte[] serializeArgs(Object [] args,Boolean [] refs) { 111 112 ByteArrayOutputStream baos = new ByteArrayOutputStream(); 113 114 ObjectOutputStream oos = null; 115 byte[] ret = null; 116 byte[][] tmpret = new byte[args.length][]; 117 118 try { 119 oos = (ObjectOutputStream) new JacObjectOutputStream(baos); 120 121 oos.writeObject(new Integer (args.length)); 122 123 for(int i=0;i<args.length;i++) { 124 if(refs!=null && refs[i].equals(Boolean.TRUE)) { 125 Collaboration.get().addAttribute(SerializedJacObject.STATELESS, Boolean.TRUE); 126 } 127 oos.writeObject(args[i]); 128 } 129 130 oos.close(); 131 ret = baos.toByteArray(); 132 133 baos.close(); 134 } catch(Exception e) { 135 e.printStackTrace(); 136 } 137 138 return ret; 139 } 140 141 158 159 public static Object deserialize(byte[] data) { 160 161 ByteArrayInputStream bais = new ByteArrayInputStream(data); 162 163 ObjectInputStream ois = null; 164 Object ret = null; 165 166 try { 167 ois = (ObjectInputStream)new JacObjectInputStream(bais); 168 ret = ois.readObject(); 169 ois.close(); 171 bais.close(); 172 } catch(Exception e) { 173 e.printStackTrace(); 174 } 175 176 return ret; 177 } 178 179 181 182 public static Object deserializeArgs(byte[] data) { 183 184 ByteArrayInputStream bais = new ByteArrayInputStream(data); 185 186 ObjectInputStream ois = null; 187 Object [] ret = null; 188 int nbArgs=0; 189 try { 190 ois = (ObjectInputStream)new JacObjectInputStream(bais); 191 nbArgs = ((Integer )ois.readObject()).intValue(); 192 ret=new Object [nbArgs]; 193 for(int i=0;i<nbArgs;i++) { 194 ret[i] = ois.readObject(); 195 } 196 ois.close(); 197 bais.close(); 198 199 } catch(Exception e) { 200 e.printStackTrace(); 201 } 202 203 return ret; 204 } 205 206 207 protected String jacObjectClassName; 208 209 210 protected HashMap fields = new HashMap(); 211 212 213 protected HashMap acInfos = new HashMap(); 214 215 217 protected boolean forwarder = true; 218 219 227 228 public SerializedJacObject ( String jacObjectClassName ) { 229 this.jacObjectClassName = jacObjectClassName; 230 } 231 232 237 238 public HashMap getFields() { 239 return fields; 240 } 241 242 247 248 public void addField ( String name, Object value ) { 249 if ( name != null ) 250 fields.put ( name, value ); 251 } 252 253 262 263 public Object getField ( String name ) { 264 if ( name == null ) return null; 265 return fields.get ( name ); 266 } 267 268 272 273 public String getJacObjectClassName() { return jacObjectClassName; } 274 275 294 295 public void setACInfos ( String acName, Object infos ) { 296 if ( acName != null ) 297 acInfos.put( acName, infos ); 298 } 299 300 320 321 public Object getACInfos(String acName) { 322 if (acName == null) return null; 323 return acInfos.get(acName); 324 } 325 326 345 346 public boolean isForwarder() { 347 return forwarder; 348 } 349 350 358 359 public void disableForwarding() { 360 forwarder = false; 361 } 362 363 371 372 public void enableForwarding() { 373 forwarder = true; 374 } 375 376 } 377 378 379 380 381 | Popular Tags |