Samples JDK
SerializationSvc.java
1 package com.freemindcafe.serialization;
2 
3 import java.io.BufferedReader;
4 import java.io.ByteArrayInputStream;
5 import java.io.ByteArrayOutputStream;
6 import java.io.FileNotFoundException;
7 import java.io.IOException;
8 import java.io.InputStream;
9 import java.io.InputStreamReader;
10 import java.util.zip.GZIPInputStream;
11 import java.util.zip.GZIPOutputStream;
12 
13 import javax.xml.parsers.DocumentBuilder;
14 import javax.xml.parsers.DocumentBuilderFactory;
15 import javax.xml.parsers.ParserConfigurationException;
16 
17 import org.apache.commons.io.IOUtils;
18 import org.w3c.dom.Document;
19 import org.w3c.dom.NamedNodeMap;
20 import org.w3c.dom.Node;
21 import org.w3c.dom.NodeList;
22 import org.xml.sax.SAXException;
23 
24 import com.thoughtworks.xstream.XStream;
25 import com.thoughtworks.xstream.converters.reflection.PureJavaReflectionProvider;
26 import com.thoughtworks.xstream.io.HierarchicalStreamDriver;
27 import com.thoughtworks.xstream.io.json.JettisonMappedXmlDriver;
28 import com.thoughtworks.xstream.io.xml.StaxDriver;
29 import com.thoughtworks.xstream.mapper.ClassAliasingMapper;
30 import com.thoughtworks.xstream.mapper.Mapper;
31 import com.thoughtworks.xstream.mapper.MapperWrapper;
32 
33 public class SerializationSvc
34 {
35  private final String aliasConfFileClassPath;
36  private final XStream xstream;
37  private final XStream jsonxstream;
38 
39  public SerializationSvc()
40  {
41  this.aliasConfFileClassPath = "/com/freemindcafe/serialization/sample1/XMLAliasing.xml";
42  xstream = new SerializeXStream(new StaxDriver());
43  jsonxstream = new SerializeXStream(new JettisonMappedXmlDriver());
44  jsonxstream.setMode(XStream.NO_REFERENCES);
45  loadSvcAliases();
46 
47  }
48 
49  public SerializationSvc(String aliasConfFileClassPath)
50  {
51  this.aliasConfFileClassPath = aliasConfFileClassPath;
52  xstream = new SerializeXStream(new StaxDriver());
53  jsonxstream = new SerializeXStream(new JettisonMappedXmlDriver());
54  jsonxstream.setMode(XStream.NO_REFERENCES);
55  loadSvcAliases();
56  }
57 
58  public SerializationSvc(String aliasConfFileClassPath, XStream xstream, XStream jsonxstream)
59  {
60  this.aliasConfFileClassPath = aliasConfFileClassPath;
61  this.xstream = xstream;
62  this.jsonxstream = jsonxstream;
63  jsonxstream.setMode(XStream.NO_REFERENCES);
64  loadSvcAliases();
65  }
66 
67  public void addAlias(String alias, Class klass)
68  {
69  xstream.alias(alias, klass);
70  jsonxstream.alias(alias, klass);
71  }
72  public String toXML(Object obj)
73  {
74  return xstream.toXML(obj);
75  }
76 
77  public Object fromXML(String s)
78  {
79  return xstream.fromXML(s);
80  }
81  public String toJSON(Object obj)
82  {
83  return jsonxstream.toXML(obj);
84  }
85 
86  public Object fromJSON(String s)
87  {
88  return jsonxstream.fromXML(s);
89  }
90 
91  public byte[] toBytes(Object obj)
92  {
93  return this.compress(this.toXML(obj));
94  }
95  public Object fromBytes(byte[] bytes)
96  {
97  String xml = this.deCompress(bytes);
98  return this.fromXML(xml);
99  }
100  /**
101  * Loads the alias information from the configuration xml file.
102  */
103  private void loadSvcAliases()
104  {
105  try
106  {
107 
108  DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance();
109 
110  DocumentBuilder db = dbf.newDocumentBuilder();
111  InputStream inputStream = SerializationSvc.class.getResourceAsStream(aliasConfFileClassPath);
112  Document dom = db.parse(inputStream);
113  NodeList nodeL = dom.getElementsByTagName("class");
114 
115  for(int i = 0; i< nodeL.getLength(); i++)
116  {
117  Node node = nodeL.item(i);
118  NamedNodeMap map = node.getAttributes();
119  Node classN = map.getNamedItem("className");
120  Node aliasN = map.getNamedItem("aliasName");
121  String className= classN.getNodeValue();
122  Class klass;
123  try
124  {
125  klass = Class.forName(className);
126  String aliasName = aliasN.getNodeValue();
127  xstream.alias(aliasName, klass);
128  jsonxstream.alias(aliasName, klass);
129  } catch (ClassNotFoundException e) {
130  System.err.println("Class not found for configuring XStream's Alias : "+className);
131  }
132  }
133 
134  } catch (FileNotFoundException e1) {
135  System.err.println("Configuration File not found for configuring XStream's Alias : "+e1);
136  //e1.printStackTrace();
137  } catch (SAXException e) {
138  System.err.println("Configuration File not parsable for configuring XStream's Alias : "+e);
139  } catch (IOException e) {
140  System.err.println("Configuration File not readable for configuring XStream's Alias : "+e);
141  } catch (ParserConfigurationException e) {
142  System.err.println("Configuration File not parsable because of configuration for configuring XStream's Alias : "+e);
143  }
144  }
145  public String getAliasConfFileName() {
146  return aliasConfFileClassPath;
147  }
148 
149  public String deCompress(byte[] bytes)
150  {
151  try
152  {
153  if (bytes == null || bytes.length == 0) {
154  return new String();
155  }
156  ByteArrayInputStream in = new ByteArrayInputStream(bytes);
157  GZIPInputStream gzip = new GZIPInputStream(in);
158  InputStreamReader reader = new InputStreamReader(gzip);
159  BufferedReader bReader = new BufferedReader(reader);
160  StringBuffer readedBuffer = new StringBuffer();
161  String readed = null;
162  while ((readed = bReader.readLine()) != null)
163  {
164  readedBuffer.append(readed);
165  }
166 
167  return readedBuffer.toString();
168  } catch (IOException e)
169  {
170  e.printStackTrace();
171  throw new RuntimeException("Could not compress the string. "+e);
172  }
173  }
174 
175  public byte[] compress(String str)
176  {
177  try
178  {
179  if (str == null || str.length() == 0) {
180  return new byte[0];
181  }
182  ByteArrayOutputStream out = new ByteArrayOutputStream();
183  GZIPOutputStream gzip;
184  gzip = new GZIPOutputStream(out);
185  gzip.write(str.getBytes());
186  gzip.close();
187  return out.toByteArray();
188  } catch (IOException e)
189  {
190  e.printStackTrace();
191  throw new RuntimeException("Could not compress the string. "+e);
192  }
193  }
194 
195 }
196