001: /*
002: *
003: *
004: * Copyright 1990-2007 Sun Microsystems, Inc. All Rights Reserved.
005: * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER
006: *
007: * This program is free software; you can redistribute it and/or
008: * modify it under the terms of the GNU General Public License version
009: * 2 only, as published by the Free Software Foundation.
010: *
011: * This program is distributed in the hope that it will be useful, but
012: * WITHOUT ANY WARRANTY; without even the implied warranty of
013: * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
014: * General Public License version 2 for more details (a copy is
015: * included at /legal/license.txt).
016: *
017: * You should have received a copy of the GNU General Public License
018: * version 2 along with this work; if not, write to the Free Software
019: * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA
020: * 02110-1301 USA
021: *
022: * Please contact Sun Microsystems, Inc., 4150 Network Circle, Santa
023: * Clara, CA 95054 or visit www.sun.com if you need additional
024: * information or have any questions.
025: */
026:
027: package com.sun.j2me.payment;
028:
029: import java.io.*;
030:
031: import com.sun.midp.io.j2me.storage.*;
032:
033: import com.sun.midp.security.*;
034: import javax.microedition.rms.*;
035: import com.sun.midp.rms.*;
036: import com.sun.midp.midlet.MIDletSuite;
037:
038: /**
039: * This class extends RMS API to implement blocked operations
040: * for Transaction Store
041: * The {@link com.sun.midp.rms.RecordStoreImpl} class is used
042: * to access Transaction Store
043: * The global native mutex is used to manage access to Transaction Store
044: *
045: * @see RecordStoreImpl
046: *
047: * @version 1.1
048: */
049: class TransactionStorageImpl {
050:
051: /** The name of the RMS where to store the transaction records. */
052: static final String PAYMENT_FILE_NAME = "paytrans";
053:
054: /** The name of the SuiteId where to store the transaction records. */
055: static final int PAYMENT_SUITEID_NAME = MIDletSuite.INTERNAL_SUITE_ID;
056:
057: /** Indicates if a Storage is opened. */
058: private boolean isOpen = false;
059:
060: /** Real Record Store. */
061: private RecordStoreImpl store;
062:
063: /**
064: * Constructor. Opens Transaction Store location and locks it
065: * If the Transaction Store is already locked the calling thread is blocking
066: * untill Transaction Store is unlocked
067: *
068: * @param token security token for authorization
069: * @param create if true, create the record store if it doesn't exist
070: *
071: * @exception RecordStoreException if something goes wrong setting up
072: * the new RecordStore.
073: * @exception RecordStoreNotFoundException if can't find the record store
074: * and create is set to false.
075: * @exception RecordStoreFullException if there is no room in storage
076: * to create a new record store
077: *
078: * @see com.sun.midp.rms.RecordStoreImpl#openRecordStore
079: */
080: TransactionStorageImpl(SecurityToken token, boolean create)
081: throws RecordStoreException, RecordStoreFullException,
082: RecordStoreNotFoundException {
083: lockStore();
084: store = RecordStoreImpl.openRecordStore(token,
085: PAYMENT_SUITEID_NAME, PAYMENT_FILE_NAME, create);
086: }
087:
088: /**
089: * Delete record from the Transaction Store.
090: *
091: * @param recordId the ID of the record to delete
092: *
093: * @exception RecordStoreNotOpenException if the record store is
094: * not open
095: * @exception InvalidRecordIDException if the recordId is invalid
096: * @exception RecordStoreException if a general record store
097: * exception occurs
098: * @see com.sun.midp.rms.RecordStoreImpl#deleteRecord
099: */
100: void deleteRecord(int recordId) throws RecordStoreNotOpenException,
101: InvalidRecordIDException, RecordStoreException {
102: checkOpen();
103: store.deleteRecord(recordId);
104: }
105:
106: /**
107: * Updates content of the record in the Transaction Store
108: *
109: * @param recordId the ID of the record to use in this operation
110: * @param newData the new data to store in the record
111: *
112: * @exception RecordStoreNotOpenException if the record store is
113: * not open
114: * @exception InvalidRecordIDException if the recordId is invalid
115: * @exception RecordStoreException if a general record store
116: * exception occurs
117: * @exception RecordStoreFullException if the operation cannot be
118: * completed because the record store has no more room
119: *
120: * @see com.sun.midp.rms.RecordStoreImpl#setRecord
121: */
122: void setRecord(int recordId, byte[] newData)
123: throws RecordStoreNotOpenException,
124: InvalidRecordIDException, RecordStoreException,
125: RecordStoreFullException {
126:
127: checkOpen();
128: store.setRecord(recordId, newData, 0, newData.length);
129: }
130:
131: /**
132: * Returns all of the recordId's currently in the Transaction Store.
133: *
134: * @exception RecordStoreNotOpenException if the record store is
135: * not open
136: * @return an array of the recordId's currently in the record store
137: * or null if the record store is closed.
138: *
139: * @see com.sun.midp.rms.RecordStoreImpl#getRecordIDs
140: */
141: int[] getRecordIDs() throws RecordStoreNotOpenException {
142: checkOpen();
143: return store.getRecordIDs();
144: }
145:
146: /**
147: * Returns a copy of the data stored in the given record.
148: *
149: * @param recordId the ID of the record to use in this operation
150: *
151: * @exception RecordStoreNotOpenException if the record store is
152: * not open
153: * @exception InvalidRecordIDException if the recordId is invalid
154: * @exception RecordStoreException if a general record store
155: * exception occurs
156: *
157: * @return the data stored in the given record. Note that if the
158: * record has no data, this method will return null.
159: *
160: * @see com.sun.midp.rms.RecordStoreImpl#getRecord
161: */
162: public byte[] getRecord(int recordId)
163: throws RecordStoreNotOpenException,
164: InvalidRecordIDException, RecordStoreException {
165: checkOpen();
166: return store.getRecord(recordId);
167: }
168:
169: /**
170: * Close Transaction Store and unlocks it.
171: * All threads waiting for Transaction Store will be unblocked
172: *
173: * @exception RecordStoreNotOpenException if the record store is
174: * not open
175: * @exception RecordStoreException if a different record
176: * store-related exception occurred
177: *
178: * @see com.sun.midp.rms.RecordStoreImpl#closeRecordStore
179: */
180: public void closeStore() throws RecordStoreNotOpenException,
181: RecordStoreException {
182: checkOpen();
183: try {
184: store.closeRecordStore();
185: } finally {
186: unlockStore();
187: }
188: };
189:
190: /**
191: * Adds a new record into the Transaction Store
192: *
193: * @param data the data to be stored in this record.
194: *
195: * @return the recordId for the new record
196: *
197: * @exception RecordStoreNotOpenException if the record store is
198: * not open
199: * @exception RecordStoreException if a different record
200: * store-related exception occurred
201: * @exception RecordStoreFullException if the operation cannot be
202: * completed because the record store has no more room
203: * @exception SecurityException if the MIDlet has read-only access
204: * to the RecordStore
205: *
206: * @see com.sun.midp.rms.RecordStoreImpl#addRecord
207: */
208: int addRecord(byte[] data) throws RecordStoreNotOpenException,
209: RecordStoreException, RecordStoreFullException {
210: checkOpen();
211: return store.addRecord(data, 0, data.length);
212: }
213:
214: /**
215: * Returns the size (in bytes) of the record.
216: *
217: * @param recordId the ID of the record to use in this operation
218: *
219: * @return the size (in bytes) of the MIDlet data available
220: * in the given record
221: *
222: * @exception RecordStoreNotOpenException if the record store is
223: * not open
224: * @exception InvalidRecordIDException if the recordId is invalid
225: * @exception RecordStoreException if a general record store
226: * exception occurs
227: *
228: * @see com.sun.midp.rms.RecordStoreImpl#getRecordSize
229: */
230: public int getRecordSize(int recordId)
231: throws RecordStoreNotOpenException,
232: InvalidRecordIDException, RecordStoreException {
233: checkOpen();
234: return store.getRecordSize(recordId);
235: }
236:
237: /**
238: * Returns the number of records currently in the Transaction Store.
239: *
240: * @return the number of records currently in the record store
241: *
242: * @exception RecordStoreNotOpenException if the record store is
243: * not open
244: *
245: * @see com.sun.midp.rms.RecordStoreImpl#getNumRecords
246: */
247: int getNumRecords() throws RecordStoreNotOpenException {
248: checkOpen();
249: return store.getNumRecords();
250: }
251:
252: /**
253: * Throws a RecordStoreNotOpenException if the RecordStore
254: * is closed.
255: *
256: * @exception RecordStoreNotOpenException if RecordStore is closed
257: *
258: * @see com.sun.midp.rms.RecordStoreImpl#checkOpen
259: */
260: private void checkOpen() throws RecordStoreNotOpenException {
261: if (!isOpen) {
262: throw new RecordStoreNotOpenException();
263: }
264: }
265:
266: /**
267: * Deletes Transaction Store
268: *
269: * @param token security token for authorization
270: *
271: * @exception RecordStoreException if a record store-related
272: * exception occurred
273: * @exception RecordStoreNotFoundException if the record store
274: * could not be found
275: *
276: * @see com.sun.midp.rms.RecordStoreImpl#deleteRecordStore
277: */
278: static void deleteStore(SecurityToken token)
279: throws RecordStoreException, RecordStoreNotFoundException {
280: RecordStoreImpl.deleteRecordStore(token, PAYMENT_SUITEID_NAME,
281: PAYMENT_FILE_NAME);
282: }
283:
284: /**
285: * Waits for access to Transaction Store
286: *
287: */
288: private native void lockStore();
289:
290: /**
291: * Unlocks Transaction Store
292: *
293: */
294: private native void unlockStore();
295:
296: /**
297: * Finalizes the store.
298: *
299: */
300: private native void finalize();
301:
302: }
|