View Javadoc

1   /*
2    * Created on Nov 17, 2003
3    * 
4    * Copyright neotechnologies.org
5    */
6   package org.neo.swarm.util.serialize;
7   
8   import org.neo.swarm.core.aop.silc.comp.AspectInvocation;
9   import org.neo.swarm.core.aop.silc.comp.Perspective;
10  import org.neo.swarm.interceptor.logging.JavaLoggingInterceptor;
11  import org.neo.swarm.interceptor.tcp.TcpClientInterceptor;
12  
13  import com.thoughtworks.xstream.XStream;
14  import com.thoughtworks.xstream.converters.extended.DynamicProxyConverter;
15  import com.thoughtworks.xstream.core.ReferenceByIdMarshallingStrategy;
16  import com.thoughtworks.xstream.io.xml.XppDriver;
17  
18  /***
19   * This object does.....
20   * 
21   * @author neil.avery
22   */
23  public class XMLSerializer implements Serializer {
24  	transient final XStream xstream = new XStream(new XppDriver());
25  	
26  	public XMLSerializer(){
27  		xstream.setMarshallingStrategy(new ReferenceByIdMarshallingStrategy());
28  		xstream.registerConverter(new DynamicProxyConverter(xstream.getClassMapper()));
29  		
30  		// TODO: nga: register xstream aliases to compress produced xml - currently AppContexts are > 10K
31  		xstream.alias("invoke", AspectInvocation.class);
32  		xstream.alias("perspective", Perspective.class);
33  		xstream.alias("tcpclientintr", TcpClientInterceptor.class);
34  		xstream.alias("javaloggingintr", JavaLoggingInterceptor.class);
35  	}
36  	
37  	/*
38  	 * (non-Javadoc)
39  	 * 
40  	 * @see org.neo.swarm.util.serialize.Serializer#serialize(java.lang.Object)
41  	 */
42  	public byte[] serialize(Object object) {//throws SerializationException {
43  	//	System.out.println(xstream.toXML(object));		
44  		return xstream.toXML(object).getBytes();
45  	//	return xml.getBytes();
46  	}
47  
48  	/*
49  	 * (non-Javadoc)
50  	 * 
51  	 * @see org.neo.swarm.util.serialize.Serializer#deserialize(byte[])
52  	 */
53  	public Object deserialize(byte[] data) {// throws SerializationException {
54  		return xstream.fromXML(new String(data));
55  	}
56  }