My favorites | Sign in
Project Home Downloads Wiki Issues Source
READ-ONLY: This project has been archived. For more information see this post.
Search
for
  Advanced search   Search tips   Subscriptions

Issue 62 attachment: FileRecordStoreManager.java (10.5 KB)

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
/**
* MicroEmulator
* Copyright (C) 2001-2007 Bartek Teodorczyk <barteo@barteo.net>
*
* It is licensed under the following two licenses as alternatives:
* 1. GNU Lesser General Public License (the "LGPL") version 2.1 or any newer version
* 2. Apache License (the "AL") Version 2.0
*
* You may not use this file except in compliance with at least one of
* the above two licenses.
*
* You may obtain a copy of the LGPL at
* http://www.gnu.org/licenses/old-licenses/lgpl-2.1.txt
*
* You may obtain a copy of the AL at
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the LGPL or the AL for the specific language governing permissions and
* limitations.
*/

package org.microemu.app.util;

import java.io.BufferedInputStream;
import java.io.BufferedOutputStream;
import java.io.DataInputStream;
import java.io.DataOutputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.FilenameFilter;
import java.io.IOException;
import java.security.AccessControlContext;
import java.security.AccessController;
import java.security.PrivilegedActionException;
import java.security.PrivilegedExceptionAction;
import java.util.Hashtable;
import java.util.Iterator;
import java.util.List;
import java.util.Vector;

import javax.microedition.rms.InvalidRecordIDException;
import javax.microedition.rms.RecordEnumeration;
import javax.microedition.rms.RecordStore;
import javax.microedition.rms.RecordStoreException;
import javax.microedition.rms.RecordStoreNotFoundException;
import javax.microedition.rms.RecordStoreNotOpenException;

import org.microemu.MicroEmulator;
import org.microemu.RecordStoreManager;
import org.microemu.app.Config;
import org.microemu.log.Logger;
import org.microemu.util.ExtendedRecordListener;
import org.microemu.util.RecordStoreImpl;

public class FileRecordStoreManager implements RecordStoreManager {

private final static String RECORD_STORE_SUFFIX = ".rms";

private final static List replaceChars = new Vector();

private MicroEmulator emulator;

private Hashtable testOpenRecordStores = new Hashtable();

private ExtendedRecordListener recordListener = null;

/* The context to be used when accessing files in Webstart */
private AccessControlContext acc;

static {
replaceChars.add(":");
replaceChars.add("*");
replaceChars.add("?");
replaceChars.add("=");
replaceChars.add("|");
replaceChars.add("/");
replaceChars.add("\\");
replaceChars.add("\"");
}

private FilenameFilter filter = new FilenameFilter() {
public boolean accept(File dir, String name) {
if (name.endsWith(RECORD_STORE_SUFFIX)) {
return true;
} else {
return false;
}
}
};

public void init(MicroEmulator emulator) {
this.emulator = emulator;
this.acc = AccessController.getContext();
}

public String getName() {
return "File record store";
}

protected File getSuiteFolder() {
return new File(Config.getConfigPath(), "suite-" + emulator.getLauncher().getSuiteName());
}

private static String escapeCharacter(String charcter) {
return "_%%" + (int) (charcter.charAt(0)) + "%%_";
}

static String recordStoreName2FileName(String recordStoreName) {
for (Iterator iterator = replaceChars.iterator(); iterator.hasNext();) {
String c = (String) iterator.next();
String newValue = escapeCharacter(c);
if (c.equals("\\")) {
c = "\\\\";
}
c = "[" + c + "]";
recordStoreName = recordStoreName.replaceAll(c, newValue);
}
return recordStoreName + RECORD_STORE_SUFFIX;
}

static String fileName2RecordStoreName(String fileName) {
for (Iterator iterator = replaceChars.iterator(); iterator.hasNext();) {
String c = (String) iterator.next();
String newValue = escapeCharacter(c);
if (c.equals("\\")) {
c = "\\\\";
}
fileName = fileName.replaceAll(newValue, c);
}
return fileName.substring(0, fileName.length() - RECORD_STORE_SUFFIX.length());
}

public void deleteRecordStore(final String recordStoreName) throws RecordStoreNotFoundException,
RecordStoreException {
final File storeFile = new File(getSuiteFolder(), recordStoreName2FileName(recordStoreName));

RecordStoreImpl recordStoreImpl = (RecordStoreImpl) testOpenRecordStores.get(storeFile.getName());
if (recordStoreImpl != null && recordStoreImpl.isOpen()) {
throw new RecordStoreException();
}

try {
recordStoreImpl = loadFromDisk(storeFile);
} catch (FileNotFoundException ex) {
throw new RecordStoreNotFoundException(recordStoreName);
}

try {
AccessController.doPrivileged(new PrivilegedExceptionAction() {
public Object run() throws FileNotFoundException {
storeFile.delete();
fireRecordStoreListener(ExtendedRecordListener.RECORDSTORE_DELETE, recordStoreName);
return null;
}
}, acc);
} catch (PrivilegedActionException e) {
Logger.error("Unable remove file " + storeFile, e);
throw new RecordStoreException();
}
}

public RecordStore openRecordStore(String recordStoreName, boolean createIfNecessary) throws RecordStoreException {
File storeFile = new File(getSuiteFolder(), recordStoreName2FileName(recordStoreName));

RecordStoreImpl recordStoreImpl;
try {
recordStoreImpl = loadFromDisk(storeFile);
recordStoreImpl.setOpen(true);
} catch (FileNotFoundException e) {
if (!createIfNecessary) {
throw new RecordStoreNotFoundException(recordStoreName);
}
recordStoreImpl = new RecordStoreImpl(this, recordStoreName);
recordStoreImpl.setOpen(true);
saveToDisk(storeFile, recordStoreImpl);
}
if (recordListener != null) {
recordStoreImpl.addRecordListener(recordListener);
}

testOpenRecordStores.put(storeFile.getName(), recordStoreImpl);

fireRecordStoreListener(ExtendedRecordListener.RECORDSTORE_OPEN, recordStoreName);

return recordStoreImpl;
}

public String[] listRecordStores() {
String[] result;
try {
result = (String[]) AccessController.doPrivileged(new PrivilegedExceptionAction() {
public Object run() {
return getSuiteFolder().list(filter);
}
}, acc);
} catch (PrivilegedActionException e) {
Logger.error("Unable to access storeFiles", e);
return null;
}
if (result != null) {
if (result.length == 0) {
result = null;
} else {
for (int i = 0; i < result.length; i++) {
result[i] = fileName2RecordStoreName(result[i]);
}
}
}
return result;
}

public void deleteRecord(RecordStoreImpl recordStoreImpl, int recordId) throws RecordStoreNotOpenException, RecordStoreException
{
saveRecord(recordStoreImpl, recordId);
}

public void loadRecord(RecordStoreImpl recordStoreImpl, int recordId)
throws RecordStoreNotOpenException, InvalidRecordIDException, RecordStoreException
{
// records are loaded when record store opens
}

public void saveRecord(RecordStoreImpl recordStoreImpl, int recordId) throws RecordStoreNotOpenException, RecordStoreException
{
File storeFile = new File(getSuiteFolder(), recordStoreName2FileName(recordStoreImpl.getName()));

saveToDisk(storeFile, recordStoreImpl);
}

public void init() {
}

public void deleteStores() {
String[] stores = listRecordStores();
for (int i = 0; i < stores.length; i++) {
String store = stores[i];
try {
deleteRecordStore(store);
} catch (RecordStoreException e) {
Logger.debug("deleteRecordStore", e);
}
}
}

private RecordStoreImpl loadFromDisk(final File recordStoreFile) throws FileNotFoundException {
try {
return (RecordStoreImpl) AccessController.doPrivileged(new PrivilegedExceptionAction() {
public Object run() throws FileNotFoundException {
return loadFromDiskSecure(recordStoreFile);
}
}, acc);
} catch (PrivilegedActionException e) {
if (e.getCause() instanceof FileNotFoundException) {
throw (FileNotFoundException) e.getCause();
}
Logger.error("Unable access file " + recordStoreFile, e);
throw new FileNotFoundException();
}
}

private RecordStoreImpl loadFromDiskSecure(File recordStoreFile) throws FileNotFoundException {
RecordStoreImpl store = null;
try {
store = new RecordStoreImpl(this);
DataInputStream dis = new DataInputStream(new BufferedInputStream(new FileInputStream(recordStoreFile)));
int size = store.readHeader(dis);
for (int i = 0; i < size; i++) {
store.readRecord(dis);
}
dis.close();
} catch (FileNotFoundException e) {
throw e;
} catch (IOException e) {
Logger.error("RecordStore.loadFromDisk: ERROR reading " + recordStoreFile.getName(), e);
}
return store;
}

private void saveToDisk(final File recordStoreFile, final RecordStoreImpl recordStore) throws RecordStoreException {
try {
AccessController.doPrivileged(new PrivilegedExceptionAction() {
public Object run() throws RecordStoreException {
saveToDiskSecure(recordStoreFile, recordStore);
return null;
}
}, acc);
} catch (PrivilegedActionException e) {
if (e.getCause() instanceof RecordStoreException) {
throw (RecordStoreException) e.getCause();
}
Logger.error("Unable access file " + recordStoreFile, e);
throw new RecordStoreException();
}
}

private synchronized void saveToDiskSecure(final File recordStoreFile, final RecordStoreImpl recordStore)
throws RecordStoreException {
if (!recordStoreFile.getParentFile().exists()) {
if (!recordStoreFile.getParentFile().mkdirs()) {
throw new RecordStoreException("Unable to create recordStore directory");
}
}
try {
DataOutputStream dos = new DataOutputStream(new BufferedOutputStream(new FileOutputStream(recordStoreFile)));
recordStore.writeHeader(dos);
RecordEnumeration re = recordStore.enumerateRecords(null, null, false);
while (re.hasNextElement()) {
recordStore.writeRecord(dos, re.nextRecordId());
}
dos.close();
} catch (IOException e) {
Logger.error("RecordStore.saveToDisk: ERROR writting object to " + recordStoreFile.getName(), e);
throw new RecordStoreException(e.getMessage());
}
}

public int getSizeAvailable(RecordStoreImpl recordStoreImpl) {
// FIXME should return free space on device
return 1024 * 1024;
}

public void setRecordListener(ExtendedRecordListener recordListener) {
this.recordListener = recordListener;
}

public void fireRecordStoreListener(int type, String recordStoreName) {
if (recordListener != null) {
recordListener.recordStoreEvent(type, System.currentTimeMillis(), recordStoreName);
}
}

public boolean cleanRecordsOnCloseRecordStore() {
return true;
}
}
Powered by Google Project Hosting