root/trunk/LogicMail/src/org/logicprobe/LogicMail/util/PersistentObjectDataStore.java

Revision 341, 5.1 kB (checked in by octorian, 4 weeks ago)
  • Property svn:mime-type set to text/plain
Line 
1/*-
2 * Copyright (c) 2008, Derek Konigsberg
3 * All rights reserved.
4 *
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions
7 * are met:
8 *
9 * 1. Redistributions of source code must retain the above copyright
10 *    notice, this list of conditions and the following disclaimer.
11 * 2. Redistributions in binary form must reproduce the above copyright
12 *    notice, this list of conditions and the following disclaimer in the
13 *    documentation and/or other materials provided with the distribution.
14 * 3. Neither the name of the project nor the names of its
15 *    contributors may be used to endorse or promote products derived
16 *    from this software without specific prior written permission.
17 *
18 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
19 * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
20 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
21 * FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
22 * COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
23 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
24 * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
25 * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
26 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
27 * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
28 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED
29 * OF THE POSSIBILITY OF SUCH DAMAGE.
30 */
31
32package org.logicprobe.LogicMail.util;
33
34import java.util.Enumeration;
35import java.util.Hashtable;
36import java.util.Vector;
37
38import net.rim.device.api.system.PersistentObject;
39import net.rim.device.api.system.PersistentStore;
40
41public class PersistentObjectDataStore implements DataStore {
42        /** Unique id for the root persistent object */
43        private long storeUid;
44        /** Root persistent object store */
45    private PersistentObject store;
46    /** Name to UID mappings */
47    private Hashtable nameMap;
48    /** UID to Object mappings */
49    private Hashtable objectMap;
50   
51    /**
52     * Creates a new instance of RmsDataStore.
53     * @param storeUid Unique ID of the store to use.
54     */
55    public PersistentObjectDataStore(long storeUid) {
56        this.storeUid = storeUid;
57        this.store = PersistentStore.getPersistentObject(storeUid);
58        this.objectMap = new Hashtable();
59        this.nameMap = new Hashtable();
60    }
61
62        public Serializable getNamedObject(String name) {
63        // Get the ID that matches the name
64        Object value = nameMap.get(name);
65        if(value instanceof Long) {
66            // Now get the object that matches the ID
67            value = objectMap.get(value);
68            return (Serializable)value;
69        }
70        else {
71            return null;
72        }
73        }
74       
75        public String[] getNamedObjects() {
76        String[] result = new String[nameMap.size()];
77        Enumeration e = nameMap.keys();
78        int i = 0;
79        while(e.hasMoreElements()) {
80            result[i++] = (String)e.nextElement();
81        }
82        return result;
83        }
84
85        public Serializable getObject(long id) {
86        return (Serializable)objectMap.get(new Long(id));
87        }
88       
89        public void putNamedObject(String name, Serializable object) {
90        nameMap.put(name, new Long(object.getUniqueId()));
91        putObject(object);
92        }
93
94        public void putObject(Serializable object) {
95        objectMap.put(new Long(object.getUniqueId()), object);
96        }
97       
98        public void removeNamedObject(String name) {
99        removeObject(getNamedObject(name));
100        nameMap.remove(name);
101        }
102
103        public void removeObject(Serializable object) {
104        objectMap.remove(new Long(object.getUniqueId()));
105        }
106       
107        public void save() {
108                Vector objectData = new Vector();
109               
110                byte[] byteArray;
111                Enumeration e = objectMap.elements();
112        while (e.hasMoreElements()) {
113            byteArray = SerializationUtils.serializeClass((Serializable)e.nextElement());
114            objectData.addElement(byteArray);
115        }
116
117                Object[] storeData = { nameMap, objectData };
118
119                synchronized(store) {
120                        store.setContents(storeData);
121                        store.commit();
122                }
123        }
124       
125        public void load() {
126                Hashtable newNameMap = null;
127                Vector newObjectMap = null;
128                synchronized(store) {
129                        Object[] storeData = (Object[])store.getContents();
130                        if(storeData != null) {
131                                newNameMap = (Hashtable)storeData[0];
132                                newObjectMap = (Vector)storeData[1];
133                        }
134                }
135                if(newNameMap != null && newObjectMap != null) {
136                        nameMap = newNameMap;
137                        Object deserializedObject;
138                        int size = newObjectMap.size();
139                        for(int i=0; i<size; i++) {
140                                deserializedObject = SerializationUtils.deserializeClass((byte[])newObjectMap.elementAt(i));
141                if(deserializedObject instanceof Serializable) {
142                    objectMap.put(new Long(((Serializable)deserializedObject).getUniqueId()), deserializedObject);
143                }
144                        }
145                }
146        }
147
148        public void delete() {
149                PersistentStore.destroyPersistentObject(storeUid);
150        }
151}
Note: See TracBrowser for help on using the browser.