KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > jfox > persistent > FilePersistenter


1 /* JFox, the OpenSource J2EE Application Server
2  *
3  * Distributable under GNU LGPL license by gun.org
4  * more details please visit http://www.huihoo.org/jfox
5  */

6
7 package org.jfox.persistent;
8
9 import java.io.File JavaDoc;
10 import java.io.FileInputStream JavaDoc;
11 import java.io.FileOutputStream JavaDoc;
12 import java.io.IOException JavaDoc;
13 import java.io.ObjectInputStream JavaDoc;
14 import java.io.ObjectOutputStream JavaDoc;
15 import java.io.Serializable JavaDoc;
16 import java.util.Date JavaDoc;
17
18 import org.jfox.ioc.Component;
19 import org.jfox.ioc.ComponentContext;
20
21
22 /**
23  * @author <a HREF="mailto:young_yy@hotmail.com">Young Yang</a>
24  */

25
26 public class FilePersistenter implements Persistenter, Component {
27
28     private String JavaDoc directory = ".";
29
30     public FilePersistenter() {
31
32     }
33
34     public FilePersistenter(String JavaDoc directory) {
35         this.directory = directory;
36     }
37
38     public synchronized Object JavaDoc load(Object JavaDoc identity) throws Exception JavaDoc {
39         if(identity == null) throw new IllegalArgumentException JavaDoc("identity can not be null.");
40
41         Object JavaDoc result = null;
42         ObjectInputStream JavaDoc ois = null;
43         File JavaDoc file = null;
44         try {
45             String JavaDoc filename = getFullFileName(identity);
46             file = new File JavaDoc(filename);
47             ois = new ObjectInputStream JavaDoc(new FileInputStream JavaDoc(file));
48             result = ois.readObject();
49         }
50         catch(Exception JavaDoc e) {
51             throw e;
52         }
53         finally {
54             try {
55                 ois.close();
56                 if(file != null) {
57                     file.delete();
58                 }
59             }
60             catch(IOException JavaDoc e) {
61             }
62         }
63         return result;
64     }
65
66     // identity is the filename
67
public synchronized void store(Object JavaDoc identity, Serializable JavaDoc data) throws Exception JavaDoc {
68         if(identity == null || data == null) throw new IllegalArgumentException JavaDoc("identity and data can not be null.");
69         String JavaDoc filename = getFullFileName(identity);
70         ObjectOutputStream JavaDoc oos = null;
71         try {
72             oos = new ObjectOutputStream JavaDoc(new FileOutputStream JavaDoc(filename));
73             oos.writeObject(data);
74         }
75         catch(Exception JavaDoc e) {
76             throw e;
77         }
78         finally {
79             try {
80                 oos.close();
81             }
82             catch(IOException JavaDoc e) {
83                 e.printStackTrace();
84             }
85         }
86     }
87
88     public boolean remove(Object JavaDoc identity) {
89         if(identity == null) throw new IllegalArgumentException JavaDoc("identity can not be null.");
90         try {
91             String JavaDoc filename = getFullFileName(identity);
92             File JavaDoc file = new File JavaDoc(filename);
93             return file.delete();
94         }
95         catch(Exception JavaDoc e) {
96             e.printStackTrace();
97             return false;
98         }
99
100     }
101
102     private String JavaDoc getFullFileName(Object JavaDoc identity) {
103         return directory + File.separator + identity.toString() + ".ser";
104     }
105
106
107     public void setComponentContext(ComponentContext ctx) {
108         directory = ctx.getModuleDir() + File.separator + "temp";
109     }
110
111     public static void main(String JavaDoc[] args) throws Exception JavaDoc {
112         Date JavaDoc date = new Date JavaDoc();
113         FilePersistenter fp = new FilePersistenter();
114         String JavaDoc identity = "test";
115         fp.store(identity, date);
116         Date JavaDoc serDate = (Date JavaDoc) fp.load(identity);
117         System.out.println(serDate.toString());
118     }
119
120 }
121
122
123
Popular Tags