01: /******************************************************************************
02: * PersistentMap.java
03: *****************************************************************************/package org.openlaszlo.cache;
04:
05: import java.io.*;
06: import java.util.*;
07:
08: /** This class implements a subset of java.util.AbstractMap semantics,
09: persisted to the file named by cacheDirectory. */
10: public class PersistentMap extends Cache {
11: protected final Map map = new HashMap();
12:
13: public PersistentMap(String name, File cacheDirectory,
14: Properties props) throws IOException {
15: super (name, cacheDirectory, props);
16: }
17:
18: public Object get(Serializable key) {
19: Object value = this .map.get(key);
20: if (value != null)
21: return value;
22: Item item = this .getItem(key);
23: if (item == null)
24: return null;
25: InputStream is = item.getStream();
26: try {
27: value = new ObjectInputStream(is).readObject();
28: } catch (ClassNotFoundException e) {
29: return null;
30: } catch (IOException e) {
31: return null;
32: } finally {
33: try {
34: is.close();
35: } catch (IOException e) {
36: }
37: }
38: this .map.put(key, value);
39: return value;
40: }
41:
42: public void put(Serializable key, final Serializable value) {
43: this .map.put(key, value);
44: try {
45: Item item = this .findItem(key, null, false);
46: PipedInputStream is = new PipedInputStream();
47: final PipedOutputStream os = new PipedOutputStream();
48: is.connect(os);
49: new Thread() {
50: public void run() {
51: try {
52: ObjectOutputStream oos = new ObjectOutputStream(
53: os);
54: oos.writeObject(value);
55: oos.close();
56: } catch (IOException e) {
57: throw new RuntimeException(e);
58: }
59: }
60: }.start();
61: item.update(is, null);
62: item.updateInfo();
63: item.markClean();
64: } catch (java.io.IOException e) {
65: throw new RuntimeException(e);
66: }
67: }
68: }
|