FileDocCategorySizeDatePackage
AccountAccessor.javaAPI DocJava Card5795Wed Mar 22 21:07:24 GMT 2006com.sun.javacard.samples.ChannelsDemo

AccountAccessor

public class AccountAccessor extends Applet implements ExtendedLength, MultiSelectable
This applet keeps track of the account information for a fictional wireless device connecting to a network service. The device has a home area, but it is also capable of operating in remote areas at a higher rate. Using the device debuts the amount of available credits. The terminal can also add credits to the account via a specific command.

Fields Summary
static final byte
AA_CLA
static final byte
GET_BALANCE
static final byte
CREDIT
static final short
MAX_BALANCE
static final short
SW_MAX_BALANCE_EXCEEDED
static final short
SW_INVALID_TRANSACTION_AMOUNT
static final byte
AREA_HOME
static final byte
AREA_REMOTE
static final byte[]
CONNECTION_MGR_AID_BYTES
static final short
SW_NO_CONNECTION
private short[]
chargeRate
private short
balance
private short
homeArea
static AccountAccessor
theAccount
Constructors Summary
private AccountAccessor(byte[] bArray, short bOffset, byte bLength)


            
        chargeRate = new short[2];
        theAccount = this;
        
        // Set up initial account information
        balance = 0;
        homeArea = (short)((short)(bArray[bOffset++] << (short)8) | 
            (short)(bArray[bOffset++] & 0x00FF));
            
        chargeRate[AREA_HOME] = (short)((short)(bArray[bOffset++] << 
            (short)8) | (short)(bArray[bOffset++] & (short)0x00FF));

        chargeRate[AREA_REMOTE] = (short)((short)(bArray[bOffset++] << 
            (short)8) | (short)(bArray[bOffset++] & (short)0x00FF));
        
        register();
    
Methods Summary
private voidcredit(APDU apdu)


        byte[] buffer = apdu.getBuffer();
        byte numBytes = buffer[ISO7816.OFFSET_LC];
        byte byteRead = (byte)(apdu.setIncomingAndReceive());

        if ( ( numBytes != 2 ) || (byteRead != 2) )
        ISOException.throwIt(ISO7816.SW_WRONG_LENGTH);

        short creditAmount = (short)
            ((short)(buffer[ISO7816.OFFSET_CDATA] << (short)8) |
            (short)(buffer[ISO7816.OFFSET_CDATA + 1]));

        if ( ( creditAmount > MAX_BALANCE)
            || ( creditAmount < (short)0 ) ) {
            ISOException.throwIt(SW_INVALID_TRANSACTION_AMOUNT);
        }
        
        if ( (short)( balance + creditAmount)  > MAX_BALANCE ) {
            ISOException.throwIt(SW_MAX_BALANCE_EXCEEDED);
        }

        JCSystem.beginTransaction();
        balance = (short)(balance + creditAmount);
        JCSystem.commitTransaction();
    
    
booleandebit(short areaCode, boolean contactless)

        short amtToDebit;
        if (areaCode == homeArea) {
            amtToDebit = chargeRate[AREA_HOME];
        } else {
            amtToDebit = chargeRate[AREA_REMOTE];
        }
        //charge more if contactless
         if(contactless)
            amtToDebit += 20;
        
        if (balance >= amtToDebit) {
            JCSystem.beginTransaction();
            balance = (short)(balance - amtToDebit);
            JCSystem.commitTransaction();
            return true;
        } else {
            return false;
        }
    
public voiddeselect()

        // Nothing to do
    
public voiddeselect(boolean appInstStillSelected)

        // Nothing to do
    
public static com.sun.javacard.samples.ChannelsDemo.AccountAccessorgetAccount()

        return theAccount;
    
private voidgetBalance(APDU apdu)

        
        AID connection_aid = JCSystem.lookupAID( CONNECTION_MGR_AID_BYTES,
                             (short)0, (byte)CONNECTION_MGR_AID_BYTES.length);
        if (!(JCSystem.isAppletActive(connection_aid)))
         ISOException.throwIt(SW_NO_CONNECTION);
        
        byte[] buffer = apdu.getBuffer();
        short le = apdu.setOutgoing();

        if ( le < 2 )
            ISOException.throwIt(ISO7816.SW_WRONG_LENGTH);

        apdu.setOutgoingLength((byte)2);

        buffer[0] = (byte)(balance >> (short)8);
        buffer[1] = (byte)(balance & (short)0x00FF);

        apdu.sendBytes((short)0, (short)2);
    
public static voidinstall(byte[] bArray, short bOffset, byte bLength)

        new AccountAccessor(bArray, bOffset, bLength);
    
public voidprocess(APDU apdu)

    
        byte[] buffer = apdu.getBuffer();
        
        if (apdu.isISOInterindustryCLA()) {
            if (buffer[ISO7816.OFFSET_INS] == (byte)(0xA4)) {
                return;
            } else {
                ISOException.throwIt (ISO7816.SW_CLA_NOT_SUPPORTED);
            }
        }
        
        switch (buffer[ISO7816.OFFSET_INS]) {
         case GET_BALANCE:   
            getBalance(apdu);
            return;
         case CREDIT:        
            credit(apdu);
            return;
         default:       
            ISOException.throwIt (ISO7816.SW_INS_NOT_SUPPORTED);
        }
    
public booleanselect()

        return true;
    
public booleanselect(boolean appInstAlreadySelected)

        return true;