FileDocCategorySizeDatePackage
CldcPaymentModule.javaAPI DocphoneME MR2 API (J2ME)15749Wed May 02 18:00:44 BST 2007com.sun.j2me.payment

CldcPaymentModule

public class CldcPaymentModule extends PaymentModule
This class extends the PaymentModule class with the device dependent methods.
version

Fields Summary
private static SecurityToken
classSecurityToken
This class has a different security domain than the MIDlet suite.
static Random
random
Random generator for debug purpose
private static final String
PAYMENT_ID_FILE_NAME
The name of the file where to store the Midlet PaymentID
private static final int
PAYMENT_ID_RECORD
Record ID for application payment ID store
private TransactionStore
transactionStore
Instance of TransactionStore
private Utils
utilities
Instance of Utils class
private Object
PreemptToken
an preempt token object to pass to donePreempting
Constructors Summary
protected CldcPaymentModule()
Creates a new instance of CldcPaymentModule.


               
      
    
Methods Summary
public final voidcleanUp()
The cleanUp method for the TCK tests. Should be removed when not needed!!! Erases the transaction store.

throws
IOException indicating Transaction Store failure

        getTransactionStore().cleanUp();
    
public TransactionModuleImplcreateTransactionModule(java.lang.Object object)
It's a factory method for TransactionModuleImpl.

param
object the application MIDlet initiating a payment transaction
return
a new instance of a TransactionModuleImpl subclass.
throws
TransactionModuleException indicates a creation failure

        return new CldcTransactionModuleImpl(object);
    
public final java.lang.String[]getMissedRecordsHeaders(int suiteId)
Return missed(pending) transactions headers for given MIdlet suite.

param
suiteId MIDlet suite ID
return
header of missed records

        CldcTransactionStoreImpl transactionStore =
            (CldcTransactionStoreImpl) getTransactionStore();
        CldcTransactionRecordImpl[] recs = null;
        String[] headers = null;
        try {
            int appID = getPaymentID(suiteId);
            recs = (CldcTransactionRecordImpl[])
                   transactionStore.getMissedTransactions(appID);

            // there is no missed transaction
            if (recs == null) {
                return null;
            }
            headers = new String[recs.length];
            StringBuffer buff = new StringBuffer();
            for (int i = 0; i < headers.length; i++) {
                buff.setLength(0);
                buff.append(recs[i].getFeatureTitle());
                buff.append(": ");
                buff.append(recs[i].getPrice());
                buff.append(recs[i].getCurrency());
                headers[i] = buff.toString();
            }
        } catch (IOException e) {
            // skip, return as it is
        }
        return headers;
    
public final intgetPaymentID(int suiteId)
Return application payment ID for given midlet suite. Create such ID if it is necessary.

param
suiteId suite ID
return
payment ID

        RecordStoreImpl store = null;
        int paymentID = -1;
        try {
            store = RecordStoreImpl.openRecordStore(
                classSecurityToken, suiteId, PAYMENT_ID_FILE_NAME, false);
            try {
                byte[] data = new byte[4];
                data = store.getRecord(1);
                if (data.length == 4) {
                    paymentID = CldcTransactionStoreImpl.
                                getIntFromByteArray(data);
                } else {
                    paymentID = -1;
                }
            } finally {
                store.closeRecordStore();
            }
        } catch (RecordStoreNotFoundException ex) {
            try {
                int appPaymentId = PaymentModule.getInstance().
                                   getNextApplicationID();
                store = RecordStoreImpl.openRecordStore(
                    classSecurityToken, suiteId, PAYMENT_ID_FILE_NAME, true);
                try {
                    byte[] data = CldcTransactionStoreImpl.
                                  getByteArrayFromInt(appPaymentId);
                    store.addRecord(data, 0, data.length);
                    paymentID = appPaymentId;
                } finally {
                    store.closeRecordStore();
                }
            } catch (RecordStoreException e) {
                if (Logging.REPORT_LEVEL <= Logging.ERROR) {
                    Logging.report(Logging.ERROR, LogChannels.LC_AMS,
                                   "Storage Failure: Can not store Payment ID");
                }
            } catch (IOException e) {
                if (Logging.REPORT_LEVEL <= Logging.ERROR) {
                    Logging.report(Logging.ERROR, LogChannels.LC_AMS,
                                   "getPaymentID threw an IOException: " +
                                   e.getMessage());
                }
            }
        } catch (RecordStoreException ex) {
            if (Logging.REPORT_LEVEL <= Logging.ERROR) {
                Logging.report(Logging.ERROR, LogChannels.LC_AMS,
                               "Storage Failure: Can not read Payment ID");
            }
        }
        return paymentID;
    
public final intgetSizeUsedInStore(int applicationID)
Returns the size the given MIDlet suite uses in the transaction store. This size doesn't include the size of the passed transactions (it includes only the part of the store which is removed when the MIDletSuite is uninstalled).

param
applicationID the payment application ID of the MIDlet suite
return
the size the MIDlet suite takes in the store

        TransactionStore transactionStore = getTransactionStore();
        int size = 0;
        try {
            size = transactionStore.getSizeUsedByApplication(applicationID);
        } catch (IOException e) {
            // ignore
        }

        return size;
    
public TransactionStoregetTransactionStore()
Init and return instance of TransactionStore.

return
TransactionStore

        if (transactionStore == null) {
            try {
                transactionStore =
                    new CldcTransactionStoreImpl(classSecurityToken);
            } catch (IOException e) {
            }
        }

        return transactionStore;
    
protected UtilsgetUtilities()
Returns an instance of CldcUtils class.

return
the instance


                  
       
        if (utilities == null) {
            utilities = new CldcUtils();
        }

        return utilities;
    
protected final booleanhandleAutoRequestMode(Transaction transaction)
Handles the auto request debug mode for the given transaction. It's called from the parts of the PaymentModule code where this mode can be applied. If the auto request mode is in effect the transaction state is set accordingly and the method returns true.

param
transaction the transaction
return
true if the transaction is handled in the method (= the auto request mode is in effect)

        PaymentInfo paymentInfo = getPaymentInfo(transaction);

        if (!paymentInfo.isDemoMode()) {
            return false;
        }

        switch (paymentInfo.getDbgAutoRequestMode()) {
        case PaymentInfo.AUTO_REQUEST_REJECT:
            transaction.setState(Transaction.REJECTED);
            transaction.setNeedsUI(false);
            break;

        case PaymentInfo.AUTO_REQUEST_ACCEPT:
            int[] providers = getValidProviders(paymentInfo);

            // we do have at least one supported payment provider
            assignTransaction(transaction, providers[0]);
            break;

        default:
            return false;
        }

        return true;
    
protected final booleanhandleTransactionDebugMode(Transaction transaction)
Handles the success/failure/random debug mode for the given transaction. It's called from the parts of the PaymentModule code where this mode can be applied. If the debug mode is in effect the transaction state is set accordingly and the method returns true.

param
transaction the transaction
return
true if the transaction is handled in the method

        PaymentInfo paymentInfo = getPaymentInfo(transaction);

        // (random.nextInt(128) < 64) = approx. one in two will fail
        if (paymentInfo.isDemoMode()) {
            if (paymentInfo.getDbgFailIO() ||
                (paymentInfo.getDbgRandomTests() &&
                 (random.nextInt(128) < 64))) {
                transaction.setState(Transaction.FAILED);
            } else {
                transaction.setState(Transaction.SUCCESSFUL);
            }

            transaction.setNeedsUI(false);
            return true;
        }

        return false;
    
public final voidinitializeTransactionStore(PaymentInfo paymentInfo, int suiteId, java.lang.String appName)
Initializes the transaction store for the given MIDlet suite.

Generates fake missed transactions for the Pay-Debug-MissedTransactions debug mode.

param
paymentInfo provision information
param
suiteId MDIletSuite ID
param
appName application name

        // === DEBUG MODE ===
        int paymentID = getPaymentID(suiteId);

        int numMissedTransactions = paymentInfo.getDbgMissedTransactions();

        if (paymentInfo.isDemoMode() && (numMissedTransactions > 0)) {
            CldcTransactionStoreImpl transactionStore =
                (CldcTransactionStoreImpl) getTransactionStore();
            try {
                transactionStore.generateFakeRecords(
                    paymentID,
                    appName,
                    paymentInfo,
                    "Feature ",
                    getValidProviders(paymentInfo),
                    numMissedTransactions);
            } catch (IOException e) {
                // ignore
            }
        }
        // === DEBUG MODE ===
    
protected voidpreemptDisplay(SecurityToken token, Displayable nextDisplayable)
Replaces the current Displayable with the new one if the nextDisplayable is not null or it recovers the previous Displayable if the nextDisplayable is null.

param
token a security token, which allows preempting
param
nextDisplayable the Displayable to show or null if the recovery of the old Displayable is requested

        DisplayEventHandler d =
            DisplayEventHandlerFactory.getDisplayEventHandler(token);
        if (nextDisplayable != null) {
            try {
                PreemptToken = d.preemptDisplay(nextDisplayable, true);
            } catch (InterruptedException ex) {
                if (Logging.REPORT_LEVEL <= Logging.ERROR) {
                    Logging.report(Logging.ERROR, LogChannels.LC_NONE,
                             "preemptDisplay threw an InterruptedException: " +
                              ex.getMessage());
                }
            }
        } else {
            d.donePreempting(PreemptToken);
            PreemptToken = null;
        }
    
public final voidremoveMissed(int suiteId)
Remove missed transaction for given suite.

param
suiteId suite ID

        int id = getPaymentID(suiteId);
        CldcTransactionStoreImpl transactionStore =
            (CldcTransactionStoreImpl) getTransactionStore();
        try {
            transactionStore.removeMissedTransaction(id);
        } catch (IOException e) {
            // skip
        }
    
public final voiduninstallFromStore(SecurityToken securityToken, int applicationID)
Uninstalls the given MIDlet suite from the transaction store. It means that the missed transaction records that belong to the suite are removed from the transaction store.

param
securityToken a security token with Permissions.AMS
param
applicationID the payment application ID of the MIDlet suite

        securityToken.checkIfPermissionAllowed(Permissions.AMS);

        TransactionStore transactionStore = getTransactionStore();
        try {
            transactionStore.removeApplicationRecords(applicationID);
        } catch (IOException e) {
            // ignore
        }