1
2
3
4
5
6 package net.sourceforge.jdbdump.connect;
7
8 import java.io.IOException;
9 import java.io.InputStream;
10 import java.io.ObjectInputStream;
11 import java.io.ObjectOutputStream;
12 import java.io.OutputStream;
13 import java.io.Serializable;
14 import java.util.Hashtable;
15
16 /***
17 * This abstract method is a definition of classes that manage access to configuration data, wherever it is stored.
18 * Implementing subclasses can access configuration data on filesystem, using database, over FTP connection,
19 * or somewhere else, depending on the implementation.
20 *
21 * @author tymoteusz
22 * @todo more subclasses:)
23 * @todo storing old configurations instead of changing them to new one.
24 */
25 public abstract class ConfigurationIO {
26
27 public boolean getDefaultAutosave(){
28 return true;
29 }
30
31 public boolean getDefaultAutoload(){
32 return false;
33 }
34
35 public ConfigurationIO(){
36 }
37
38 /***
39 * This method feeds this class' configuration data with current data grabbed from its storage place.
40 * @throws IOException if storage place is inaccessible
41 * @throws ClassNotFoundException
42 *
43 */
44 @SuppressWarnings("unchecked")
45 public Hashtable<String, Serializable> load() throws IOException{
46 Hashtable<String, Serializable> data;
47 ObjectInputStream iStream = null;
48 try{
49 iStream = new ObjectInputStream(getInputStream());
50 data = (Hashtable<String, Serializable>) iStream.readObject();
51 iStream.close();
52 }catch(Exception e){
53 data = new Hashtable();
54 }
55 if(iStream != null){
56 iStream.close();
57 }
58 return data;
59 }
60
61 /***
62 * This method puts this class' current configuration data to its storage place.
63 * @throws IOException if storage place is inaccessible
64 *
65 */
66 public void save(Hashtable<String, Serializable> data) throws IOException{
67 ObjectOutputStream oStream = new ObjectOutputStream(getOutputStream());
68 oStream.writeObject(data);
69 oStream.close();
70 }
71
72 /***
73 * Implementations of this abstract method should cover all logic for reading data from it's storage place,
74 * and provide access to it through an InputStream object.
75 * @return InputStream object from which configuration data can be retrieved
76 * @throws IOException
77 */
78 protected abstract InputStream getInputStream() throws IOException;
79
80 /***
81 * Implementations of this abstract method should cover all logic for writing data to it's storage place,
82 * and provide access to it through an OutputStream object.
83 * @return OutputStream object to which configuration data can be stored
84 * @throws IOException
85 */
86 protected abstract OutputStream getOutputStream() throws IOException;
87
88 }