| 1 | /*- |
|---|
| 2 | * Copyright (c) 2007, 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 | |
|---|
| 32 | package org.logicprobe.LogicMail.util; |
|---|
| 33 | |
|---|
| 34 | import java.util.Enumeration; |
|---|
| 35 | import java.util.Hashtable; |
|---|
| 36 | import javax.microedition.rms.RecordStore; |
|---|
| 37 | import javax.microedition.rms.RecordStoreException; |
|---|
| 38 | |
|---|
| 39 | /** |
|---|
| 40 | * DataStore implementation for the J2ME Record Management System. |
|---|
| 41 | * This implementation saves and loads everything at once, using |
|---|
| 42 | * the corresponding save() and load() methods. It is intended |
|---|
| 43 | * for use with relatively lightweight data, so on-demand I/O has |
|---|
| 44 | * not been implemented. |
|---|
| 45 | */ |
|---|
| 46 | public class RmsDataStore implements DataStore { |
|---|
| 47 | /** Name of the RMS store */ |
|---|
| 48 | private String storeName; |
|---|
| 49 | /** Name to UID mappings */ |
|---|
| 50 | private SerializableHashtable nameMap; |
|---|
| 51 | /** UID to Object mappings */ |
|---|
| 52 | private Hashtable objectMap; |
|---|
| 53 | |
|---|
| 54 | /** |
|---|
| 55 | * Creates a new instance of RmsDataStore. |
|---|
| 56 | * |
|---|
| 57 | * @param storeName Name of the RMS store to use. |
|---|
| 58 | */ |
|---|
| 59 | public RmsDataStore(String storeName) { |
|---|
| 60 | this.storeName = storeName; |
|---|
| 61 | this.objectMap = new Hashtable(); |
|---|
| 62 | this.nameMap = new SerializableHashtable(); |
|---|
| 63 | } |
|---|
| 64 | |
|---|
| 65 | public Serializable getNamedObject(String name) { |
|---|
| 66 | // Get the ID that matches the name |
|---|
| 67 | Object value = nameMap.get(name); |
|---|
| 68 | if(value instanceof Long) { |
|---|
| 69 | // Now get the object that matches the ID |
|---|
| 70 | value = objectMap.get(value); |
|---|
| 71 | return (Serializable)value; |
|---|
| 72 | } |
|---|
| 73 | else { |
|---|
| 74 | return null; |
|---|
| 75 | } |
|---|
| 76 | } |
|---|
| 77 | |
|---|
| 78 | public String[] getNamedObjects() { |
|---|
| 79 | String[] result = new String[nameMap.size()]; |
|---|
| 80 | Enumeration e = nameMap.keys(); |
|---|
| 81 | int i = 0; |
|---|
| 82 | while(e.hasMoreElements()) { |
|---|
| 83 | result[i++] = (String)e.nextElement(); |
|---|
| 84 | } |
|---|
| 85 | return result; |
|---|
| 86 | } |
|---|
| 87 | |
|---|
| 88 | public Serializable getObject(long id) { |
|---|
| 89 | return (Serializable)objectMap.get(new Long(id)); |
|---|
| 90 | } |
|---|
| 91 | |
|---|
| 92 | public void putNamedObject(String name, Serializable object) { |
|---|
| 93 | nameMap.put(name, new Long(object.getUniqueId())); |
|---|
| 94 | putObject(object); |
|---|
| 95 | } |
|---|
| 96 | |
|---|
| 97 | public void putObject(Serializable object) { |
|---|
| 98 | objectMap.put(new Long(object.getUniqueId()), object); |
|---|
| 99 | } |
|---|
| 100 | |
|---|
| 101 | public void removeNamedObject(String name) { |
|---|
| 102 | removeObject(getNamedObject(name)); |
|---|
| 103 | nameMap.remove(name); |
|---|
| 104 | } |
|---|
| 105 | |
|---|
| 106 | public void removeObject(Serializable object) { |
|---|
| 107 | objectMap.remove(new Long(object.getUniqueId())); |
|---|
| 108 | } |
|---|
| 109 | |
|---|
| 110 | public void save() { |
|---|
| 111 | // Delete the store if it already exists, |
|---|
| 112 | // as we are going to completely rewrite |
|---|
| 113 | // its contents. |
|---|
| 114 | try { |
|---|
| 115 | RecordStore.deleteRecordStore(storeName); |
|---|
| 116 | } catch (RecordStoreException exp) { |
|---|
| 117 | // do nothing |
|---|
| 118 | } |
|---|
| 119 | |
|---|
| 120 | try { |
|---|
| 121 | RecordStore store = RecordStore.openRecordStore(storeName, true); |
|---|
| 122 | byte[] byteArray; |
|---|
| 123 | |
|---|
| 124 | // Serialize the name map, and store |
|---|
| 125 | // it at the first index. |
|---|
| 126 | byteArray = SerializationUtils.serializeClass(nameMap); |
|---|
| 127 | store.addRecord(byteArray, 0, byteArray.length); |
|---|
| 128 | |
|---|
| 129 | // Store all the objects in the object map |
|---|
| 130 | Enumeration e = objectMap.elements(); |
|---|
| 131 | while (e.hasMoreElements()) { |
|---|
| 132 | byteArray = SerializationUtils.serializeClass((Serializable)e.nextElement()); |
|---|
| 133 | store.addRecord(byteArray, 0, byteArray.length); |
|---|
| 134 | } |
|---|
| 135 | store.closeRecordStore(); |
|---|
| 136 | } catch (RecordStoreException exp) { |
|---|
| 137 | // do nothing |
|---|
| 138 | } |
|---|
| 139 | } |
|---|
| 140 | |
|---|
| 141 | public void load() { |
|---|
| 142 | Object deserializedObject; |
|---|
| 143 | try { |
|---|
| 144 | RecordStore store = RecordStore.openRecordStore(storeName, false); |
|---|
| 145 | |
|---|
| 146 | int records = store.getNumRecords(); |
|---|
| 147 | |
|---|
| 148 | if(records >= 1) { |
|---|
| 149 | // Deserialize the name map |
|---|
| 150 | deserializedObject = SerializationUtils.deserializeClass(store.getRecord(1)); |
|---|
| 151 | if(deserializedObject instanceof SerializableHashtable) { |
|---|
| 152 | nameMap = (SerializableHashtable)deserializedObject; |
|---|
| 153 | } |
|---|
| 154 | else { |
|---|
| 155 | return; |
|---|
| 156 | } |
|---|
| 157 | |
|---|
| 158 | // Populate the vector |
|---|
| 159 | objectMap.clear(); |
|---|
| 160 | if(records > 1) { |
|---|
| 161 | for(int i=2;i<=records;i++) { |
|---|
| 162 | deserializedObject = SerializationUtils.deserializeClass(store.getRecord(i)); |
|---|
| 163 | if(deserializedObject instanceof Serializable) { |
|---|
| 164 | objectMap.put(new Long(((Serializable)deserializedObject).getUniqueId()), deserializedObject); |
|---|
| 165 | } |
|---|
| 166 | } |
|---|
| 167 | } |
|---|
| 168 | } |
|---|
| 169 | |
|---|
| 170 | store.closeRecordStore(); |
|---|
| 171 | } catch (RecordStoreException exp) { |
|---|
| 172 | // do nothing |
|---|
| 173 | } |
|---|
| 174 | } |
|---|
| 175 | |
|---|
| 176 | public void delete() { |
|---|
| 177 | try { |
|---|
| 178 | RecordStore.deleteRecordStore(storeName); |
|---|
| 179 | } catch (RecordStoreException exp) { |
|---|
| 180 | // do nothing |
|---|
| 181 | } |
|---|
| 182 | } |
|---|
| 183 | } |
|---|