01: /*
02: * hgcommons 7
03: * Hammurapi Group Common Library
04: * Copyright (C) 2003 Hammurapi Group
05: *
06: * This program is free software; you can redistribute it and/or
07: * modify it under the terms of the GNU Lesser General Public
08: * License as published by the Free Software Foundation; either
09: * version 2 of the License, or (at your option) any later version.
10: *
11: * This program is distributed in the hope that it will be useful,
12: * but WITHOUT ANY WARRANTY; without even the implied warranty of
13: * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14: * Lesser General Public License for more details.
15: *
16: * You should have received a copy of the GNU Lesser General Public
17: * License along with this library; if not, write to the Free Software
18: * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
19: *
20: * URL: http://www.hammurapi.biz/hammurapi-biz/ef/xmenu/hammurapi-group/products/products/hgcommons/index.html
21: * e-Mail: support@hammurapi.biz
22: */
23: package biz.hammurapi.legacy.persistence;
24:
25: import java.io.File;
26: import java.io.FileInputStream;
27: import java.io.FileOutputStream;
28: import java.io.IOException;
29: import java.io.ObjectInputStream;
30: import java.io.ObjectOutputStream;
31: import java.io.Serializable;
32:
33: /**
34: * @author Pavel Vlasov
35: * @version $Revision: 1.1 $
36: */
37: public class FileStorage implements Storage {
38:
39: private File storageDir;
40:
41: public FileStorage(File storageDir) {
42: this .storageDir = storageDir;
43: }
44:
45: public String put(Object o) throws PersistenceException {
46: if (o instanceof Serializable) {
47: try {
48: File file = File.createTempFile("storage", ".dat",
49: storageDir);
50: ObjectOutputStream oos = new ObjectOutputStream(
51: new FileOutputStream(file));
52: try {
53: oos.writeObject(o);
54: } finally {
55: oos.close();
56: }
57: return file.getName();
58: } catch (IOException e) {
59: throw new PersistenceException(e);
60: }
61: } else {
62: return null;
63: }
64: }
65:
66: public Object get(String key) throws PersistenceException {
67: File file = new File(storageDir, key);
68: try {
69: ObjectInputStream ois = new ObjectInputStream(
70: new FileInputStream(file));
71: try {
72: return ois.readObject();
73: } finally {
74: ois.close();
75: }
76: } catch (IOException e) {
77: throw new PersistenceException(e);
78: } catch (ClassNotFoundException e) {
79: throw new PersistenceException(e);
80: }
81: }
82:
83: public void remove(String key) throws PersistenceException {
84: new File(storageDir, key).delete();
85: }
86:
87: }
|