KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > ubermq > jms > server > journal > impl > BinarySettingsRepository


1 package com.ubermq.jms.server.journal.impl;
2
3 import java.io.*;
4
5 import com.ubermq.jms.server.ServerConfig;
6 import com.ubermq.jms.server.journal.ISettingsRepository;
7 import com.ubermq.kernel.Configurator;
8 import java.util.HashMap JavaDoc;
9 import java.util.Map JavaDoc;
10
11 /**
12  * A journal implementation that stores the arbitrary objects to disk in
13  * a proprietary binary format. It may make sense to create an implementation
14  * of this to store XML someday.
15  */

16 public class BinarySettingsRepository implements ISettingsRepository
17 {
18     private static final org.apache.log4j.Logger log = org.apache.log4j.Logger.getLogger(BinarySettingsRepository.class);
19     
20     private Map JavaDoc entries;
21     private String JavaDoc name;
22
23     public BinarySettingsRepository(String JavaDoc fileName)
24         throws java.io.FileNotFoundException JavaDoc, java.io.IOException JavaDoc
25     {
26         entries = new HashMap JavaDoc();
27         name = fileName;
28         restore();
29     }
30
31     public BinarySettingsRepository()
32         throws java.io.FileNotFoundException JavaDoc, java.io.IOException JavaDoc
33     {
34         this(Configurator.getProperty(ServerConfig.LOG_FILE));
35     }
36
37     public void put(Object JavaDoc key, Object JavaDoc value)
38     {
39         entries.put(key, value);
40         flush();
41     }
42
43     public Object JavaDoc get(Object JavaDoc key)
44     {
45         restore();
46         return entries.get(key);
47     }
48
49     private synchronized void flush()
50     {
51         try
52         {
53             ObjectOutputStream oos = new ObjectOutputStream(new FileOutputStream(name));
54             oos.writeObject(entries);
55         }
56         catch (java.io.IOException JavaDoc e)
57         {
58             log.error("", e);
59         }
60     }
61
62     private synchronized void restore()
63     {
64         try
65         {
66             if (name == null)
67                 throw new FileNotFoundException();
68
69             ObjectInputStream ois = new ObjectInputStream(new FileInputStream(name));
70             entries = (Map JavaDoc)ois.readObject();
71         }
72         catch (IOException e) {
73             entries = new HashMap JavaDoc();
74         }
75         catch (Exception JavaDoc e) {
76             log.error("", e);
77         }
78     }
79 }
80
Popular Tags