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

CldcTransactionModuleImpl

public class CldcTransactionModuleImpl extends TransactionModuleImpl
This class implements parts of TransactionModuleImpl which are dependent on the CLDC. It's created by a factory method in the payment module.
version
see
CldcPaymentModule#createTransactionModule

Fields Summary
private MIDletSuite
midletSuite
MDIletSuite being run
private static final String
PAYMENT_UPDATE_FILE_NAME
The name of the file where the Midlet Payment Update Info is stored.
public static final String
PAY_VERSION_PROP
MIDlet property for the Payment Version.
private PaymentInfo
paymentInfo
MIDletSuite payment info
Constructors Summary
protected CldcTransactionModuleImpl(Object object)
Creates a new instance of CldcTransactionModuleImpl. Requires a reference to the application MIDlet which initiated the payment.

Handles the Pay-Debug-FailInitialize debug mode.

param
object the caller MIDlet
throws
TransactionModuleException indicates an error preventing the MIDlet from using the payment API
see
javax.microedition.payment.TransactionModule#TransactionModule


                                                         
       
         
        super(object);

        PaymentInfo paymentInfo = getPaymentInfo();
        if (paymentInfo == null) {
            throw new TransactionModuleException("Missing provisioning " +
                    "information");
        }

        /* Check if MIDletSuite is Trusted */
        // Removed - SecurityException is thrown if untrusted MIDlet tries to use
        // restricted permission, so no additional check is required
//         if (!paymentInfo.isDemoMode() && !getMIDletSuite().isTrusted()) {
//             throw new TransactionModuleException("MidletSuite is untrusted");
//         }

        // === DEBUG MODE ===
        // (PaymentModule.random.nextInt(192) < 64) = approx. one in three will
        // fail in this way
        if (paymentInfo.isDemoMode() && (
            paymentInfo.getDbgFailInitialize() ||
            paymentInfo.getDbgRandomTests() &&
            (CldcPaymentModule.random.nextInt(192) < 64))) {
            throw new TransactionModuleException("Debug mode");
        }
        // === DEBUG MODE ===
    
Methods Summary
protected voidcheckForPermission(int permission, java.lang.String name)
Ensures that the MIDlet will have required privileges to do a protected operation.

param
permission the required permission
param
name an additional info string
throws
SecurityException if the permission is not granted
throws
InterruptedException if the thread waiting for the permission is interrupted

        getMIDletSuite().checkForPermission(permission, name);
    
protected intgetApplicationID()
Returns the MIDlet payment ID that can be used to store transaction records for the MIDlet initiated transactions into the transaction store.

return
the ID

        return ((CldcPaymentModule) PaymentModule.getInstance())
            .getPaymentID(getMIDletSuite().getID());
    
private MIDletSuitegetMIDletSuite()
Returns the MIDlet suite of the MIDlet.

return
the MIDlet suite

        if (midletSuite == null) {
            midletSuite =
                (MIDletSuite) Scheduler.getScheduler().getMIDletSuite();
        }
        return midletSuite;
    
protected javax.microedition.payment.TransactionRecord[]getMissedTransactions()
Returns an array of the missed transactions associated with the MIDlet suite.

It handles the Pay-Debug-MissedTransactions debug mode.

return
an array of the missed transactions

        // === DEBUG MODE ===
        PaymentInfo paymentInfo = getPaymentInfo();
        if (paymentInfo.isDemoMode() &&
            paymentInfo.getDbgMissedTransactions() >= 0) {
            int applicationID = getApplicationID();

            CldcPaymentModule paymentModule = (CldcPaymentModule)
                                              PaymentModule.getInstance();
            CldcTransactionStoreImpl transactionStore =
                (CldcTransactionStoreImpl) paymentModule.getTransactionStore();
            TransactionRecord[] fakeMissed = null;

            try {
                fakeMissed = transactionStore.getMissedTransactions(
                    applicationID, true);
            } catch (IOException e) {
            }

            return fakeMissed;
        }
        // === DEBUG MODE ===
        return super.getMissedTransactions();
    
protected PaymentInfogetPaymentInfo()
Returns the payment provisioning information associated with the MIDlet.

return
the payment provisioning information

        if (paymentInfo == null) {
            if (getMIDletSuite().getProperty(PAY_VERSION_PROP) == null) {
                // quick check
                return null;
            }
            RecordStore store;
            PropertiesWraper props = new PropertiesWraper(getMIDletSuite());
            // try to read updated provision info
            try {
                store = RecordStore.openRecordStore(PAYMENT_UPDATE_FILE_NAME,
                    false);
                try {
                    byte[] data = new byte[store.getRecordSize(1)];
                    data = store.getRecord(1);

                    InputStream bis = new ByteArrayInputStream(data);
                    JadProperties payProps = new JadProperties();
                    payProps.load(bis);
                    bis.close();

                    paymentInfo = PaymentInfo.createFromProperties(
                        props, payProps);
                } finally {
                    store.closeRecordStore();
                }
            } catch (RecordStoreNotFoundException ex) {

                // if the is no update file try to read payment info from
                // suite properties
                try {
                    paymentInfo = PaymentInfo.createFromProperties(
                        props, props);
                } catch (PaymentException e) {
                    if (Logging.REPORT_LEVEL <= Logging.ERROR) {
                        Logging.report(Logging.ERROR, LogChannels.LC_AMS,
                                  "getPaymentInfo threw an PaymentException: " +
                                   e.getMessage());
                    }
                }
            } catch (RecordStoreException ex) {
                if (Logging.REPORT_LEVEL <= Logging.ERROR) {
                    Logging.report(Logging.ERROR, LogChannels.LC_AMS,
                              "getPaymentInfo threw an RecordStoreException: " +
                              ex.getMessage());
                }
            } catch (PaymentException ex) {
                if (Logging.REPORT_LEVEL <= Logging.ERROR) {
                    Logging.report(Logging.ERROR, LogChannels.LC_AMS,
                                 "getPaymentInfo threw an PaymentException: " +
                                 ex.getMessage());
                }
            } catch (IOException ex) {
                if (Logging.REPORT_LEVEL <= Logging.ERROR) {
                    Logging.report(Logging.ERROR, LogChannels.LC_AMS,
                                   "getPaymentInfo threw an IOException: " +
                                   ex.getMessage());
                }
            }
            if (paymentInfo != null) {
                // initialize the transaction store for this MIDletSuite only
                // for once
                ((CldcPaymentModule) PaymentModule.getInstance())
                    .initializeTransactionStore(paymentInfo,
                                                midletSuite.getID(),
                    midletSuite.getProperty(MIDletSuiteImpl.SUITE_NAME_PROP));
            }
        }
        return paymentInfo;
    
protected voidsavePaymentInfo()
Stores the payment provisioning information associated with the MIDlet.

throws
IOException indicates an output error

        PaymentInfo pInfo = getPaymentInfo();

        if (pInfo != null) {
            ByteArrayOutputStream bos = new ByteArrayOutputStream();
            OutputStreamWriter os = new OutputStreamWriter(bos, "UTF-8");
            byte[] data;

            try {
                pInfo.export(os);
                data = bos.toByteArray();
            } finally {
                os.close();
            }

            RecordStore store = null;
            try {
                store = RecordStore.openRecordStore(PAYMENT_UPDATE_FILE_NAME,
                    true);
                try {
                    if (store.getNumRecords() == 0) {
                        /* Record Store is EMPTY - add new record */
                        store.addRecord(data, 0, data.length);
                    } else {
                        /* Replace first record */
                        store.setRecord(1, data, 0, data.length);
                    }
                } finally {
                    store.closeRecordStore();
                }
            } catch (RecordStoreException e) {
                throw new IOException(
                    "Storage Failure: Can not store Payment Info");
            }
        }