01: package org.dbbrowser.drivermanager;
02:
03: import infrastructure.propertymanager.PropertyManager;
04: import java.io.BufferedReader;
05: import java.io.BufferedWriter;
06: import java.io.FileReader;
07: import java.io.FileWriter;
08: import java.io.IOException;
09: import java.util.List;
10: import com.thoughtworks.xstream.XStream;
11:
12: /**
13: * Used to serialize the connection to a stream
14: */
15: public class ConnectionInfoSerializer {
16: private static final String CONNECTION_INFO_FILE_NAME = PropertyManager
17: .getInstance().getProperty(
18: "dbbrowser.connectioninfo.serialize.location");
19:
20: /**
21: * Serialize(save) the connection information
22: * @param listOfConnectionInfo
23: * @throws IOException
24: */
25: public static void serialize(List listOfConnectionInfo)
26: throws IOException {
27: //Serialize the object
28: XStream xstream = new XStream();
29: xstream.alias("ConnectionInfo", ConnectionInfo.class);
30: String xml = xstream.toXML(listOfConnectionInfo);
31:
32: //Write key bindings to file
33: FileWriter fw = new FileWriter(CONNECTION_INFO_FILE_NAME);
34: BufferedWriter writer = new BufferedWriter(fw);
35:
36: writer.write(xml);
37: writer.flush();
38: writer.close();
39: }
40:
41: /**
42: * Deserialize(load) the connection info
43: * @return
44: * @throws ClassNotFoundException
45: * @throws IOException
46: */
47: public static List deserialize() throws ClassNotFoundException,
48: IOException {
49: //Load the connection info
50: StringBuffer xml = new StringBuffer();
51: FileReader fr = new FileReader(CONNECTION_INFO_FILE_NAME);
52: BufferedReader reader = new BufferedReader(fr);
53:
54: String line = reader.readLine();
55:
56: while (line != null) {
57: xml.append(line);
58: line = reader.readLine();
59: }
60:
61: XStream xstream = new XStream();
62: xstream.alias("ConnectionInfo", ConnectionInfo.class);
63: return (List) xstream.fromXML(xml.toString());
64: }
65: }
|