KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > oddjob > persist > SerializePersister


1 /*
2  * Copyright © 2004, Rob Gordon.
3  */

4 package org.oddjob.persist;
5
6 import java.io.File JavaDoc;
7 import java.io.FileInputStream JavaDoc;
8 import java.io.FileNotFoundException JavaDoc;
9 import java.io.FileOutputStream JavaDoc;
10 import java.io.IOException JavaDoc;
11 import java.io.InputStream JavaDoc;
12 import java.io.ObjectInput JavaDoc;
13 import java.io.ObjectInputStream JavaDoc;
14 import java.io.ObjectOutput JavaDoc;
15 import java.io.ObjectOutputStream JavaDoc;
16 import java.io.OutputStream JavaDoc;
17
18 import org.apache.log4j.Logger;
19
20 /**
21  * @oddjob.description Persist and load jobs from and to a file. The file
22  * the job is persisted to is the jobs id with a .ser extension.
23  *
24  * @author Rob Gordon.
25  */

26 public class SerializePersister extends PersisterBase {
27     private static final Logger logger = Logger.getLogger(SerializePersister.class);
28
29     private final static String JavaDoc EXTENSION = ".ser";
30
31     /**
32      * @oddjob.property
33      * @oddjob.description The directory in which the filess will be created.
34      * @oddjob.required No.
35      */

36     private File JavaDoc directory;
37     
38     /**
39      * Set the directory to save files in.
40      *
41      * @param dir The directory.
42      */

43     public void setDir(File JavaDoc dir) {
44         this.directory = dir;
45     }
46     
47     /**
48      * Get the directory, files are being persisted in.
49      *
50      * @return The directory.
51      */

52     public File JavaDoc getDir() {
53         return this.directory;
54     }
55     
56     /*
57      * (non-Javadoc)
58      * @see org.oddjob.persist.PersisterBase#persist(java.lang.String, java.lang.Object)
59      */

60     public void persist(String JavaDoc id, Object JavaDoc o) throws OddjobPersistException {
61         
62         File JavaDoc file = fileFor(id);
63         try {
64             OutputStream JavaDoc os = new FileOutputStream JavaDoc(file);
65             ObjectOutput JavaDoc oo = new ObjectOutputStream JavaDoc(os);
66             oo.writeObject(o);
67             oo.close();
68             logger.debug("Saved [" + o + "], id [" + id
69                     + "] to file ]["
70                     + file + "].");
71         }
72         catch (FileNotFoundException JavaDoc e) {
73             throw new OddjobPersistException("Check directory exists!", e);
74         }
75         catch (IOException JavaDoc e) {
76             throw new OddjobPersistException("Failed writing object id ["
77                     + id + "], class [" + o.getClass().getName()
78                     + "], object [" + o + "]."
79                     , e);
80         }
81     }
82     
83     /*
84      * (non-Javadoc)
85      * @see org.oddjob.persist.PersisterBase#remove(java.lang.String)
86      */

87     public void remove(String JavaDoc id) {
88         File JavaDoc f = fileFor(id);
89         if (f.exists()) {
90             f.delete();
91         }
92     }
93
94     /*
95      * (non-Javadoc)
96      * @see org.oddjob.arooa.ComponentRestorer#restore(java.lang.String)
97      */

98     public Object JavaDoc restore(String JavaDoc id) throws OddjobPersistException {
99         
100         File JavaDoc f = fileFor(id);
101         if (!f.exists()) {
102             return null;
103         }
104         try {
105             InputStream JavaDoc is = new FileInputStream JavaDoc(f);
106             ObjectInput JavaDoc oi = new ObjectInputStream JavaDoc(is);
107             Object JavaDoc o = oi.readObject();
108             oi.close();
109             logger.debug("Loaded [" + o + "] from [" + f + "].");
110             return o;
111         }
112         catch (FileNotFoundException JavaDoc e) {
113             throw new OddjobPersistException("Check file exists!", e);
114         }
115         catch (IOException JavaDoc e) {
116             throw new OddjobPersistException("Failed reading object.", e);
117         }
118         catch (ClassNotFoundException JavaDoc e) {
119             throw new OddjobPersistException("Failed to load object.", e);
120         }
121     }
122     
123     File JavaDoc fileFor(String JavaDoc id) {
124         return new File JavaDoc(directory, id + EXTENSION);
125     }
126 }
127
Popular Tags