View Javadoc

1   /*
2    *  MicroEmulator
3    *  Copyright (C) 2001-2005 Bartek Teodorczyk <barteo@barteo.net>
4    *
5    *  This library is free software; you can redistribute it and/or
6    *  modify it under the terms of the GNU Lesser General Public
7    *  License as published by the Free Software Foundation; either
8    *  version 2.1 of the License, or (at your option) any later version.
9    *
10   *  This library is distributed in the hope that it will be useful,
11   *  but WITHOUT ANY WARRANTY; without even the implied warranty of
12   *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
13   *  Lesser General Public License for more details.
14   *
15   *  You should have received a copy of the GNU Lesser General Public
16   *  License along with this library; if not, write to the Free Software
17   *  Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
18   */
19  
20  package org.microemu.util;
21  
22  import java.util.Enumeration;
23  import java.util.Hashtable;
24  
25  import javax.microedition.rms.RecordStore;
26  import javax.microedition.rms.RecordStoreException;
27  import javax.microedition.rms.RecordStoreNotFoundException;
28  
29  import org.microemu.MicroEmulator;
30  import org.microemu.RecordStoreManager;
31  
32  public class MemoryRecordStoreManager implements RecordStoreManager {
33  	private Hashtable recordStores = new Hashtable();
34  
35  	private ExtendedRecordListener recordListener = null;
36  
37  	public void init(MicroEmulator emulator) {
38  	}
39  
40  	public String getName() {
41  		return "Memory record store";
42  	}
43  
44  	public void deleteRecordStore(String recordStoreName) throws RecordStoreNotFoundException, RecordStoreException {
45  		RecordStoreImpl recordStoreImpl = (RecordStoreImpl) recordStores.get(recordStoreName);
46  		if (recordStoreImpl == null) {
47  			throw new RecordStoreNotFoundException(recordStoreName);
48  		}
49  		if (recordStoreImpl.isOpen()) {
50  			throw new RecordStoreException();
51  		}
52  		recordStores.remove(recordStoreName);
53  
54  		fireRecordStoreListener(ExtendedRecordListener.RECORDSTORE_DELETE, recordStoreName);
55  	}
56  
57  	public RecordStore openRecordStore(String recordStoreName, boolean createIfNecessary)
58  			throws RecordStoreNotFoundException {
59  		RecordStoreImpl recordStoreImpl = (RecordStoreImpl) recordStores.get(recordStoreName);
60  		if (recordStoreImpl == null) {
61  			if (!createIfNecessary) {
62  				throw new RecordStoreNotFoundException(recordStoreName);
63  			}
64  			recordStoreImpl = new RecordStoreImpl(this, recordStoreName);
65  			recordStores.put(recordStoreName, recordStoreImpl);
66  		}
67  		recordStoreImpl.setOpen(true);
68  		if (recordListener != null) {
69  			recordStoreImpl.addRecordListener(recordListener);
70  		}
71  
72  		fireRecordStoreListener(ExtendedRecordListener.RECORDSTORE_OPEN, recordStoreName);
73  
74  		return recordStoreImpl;
75  	}
76  
77  	public String[] listRecordStores() {
78  		String[] result = null;
79  
80  		int i = 0;
81  		for (Enumeration e = recordStores.keys(); e.hasMoreElements();) {
82  			if (result == null) {
83  				result = new String[recordStores.size()];
84  			}
85  			result[i] = (String) e.nextElement();
86  			i++;
87  		}
88  
89  		return result;
90  	}
91  
92  	public void saveChanges(RecordStoreImpl recordStoreImpl) {
93  	}
94  
95  	public void init() {
96  		deleteStores();
97  	}
98  
99  	public void deleteStores() {
100 		if (recordStores != null)
101 			recordStores.clear();
102 	}
103 
104 	public int getSizeAvailable(RecordStoreImpl recordStoreImpl) {
105 		// FIXME returns too much
106 		return (int) Runtime.getRuntime().freeMemory();
107 	}
108 
109 	public void setRecordListener(ExtendedRecordListener recordListener) {
110 		this.recordListener = recordListener;
111 	}
112 
113 	public void fireRecordStoreListener(int type, String recordStoreName) {
114 		if (recordListener != null) {
115 			recordListener.recordStoreEvent(type, System.currentTimeMillis(), recordStoreName);
116 		}
117 	}
118 }