FileDocCategorySizeDatePackage
JavaLoyalty.javaAPI DocJava Card5791Wed Mar 22 21:07:26 GMT 2006package com.sun.javacard.samples.JavaLoyalty

JavaLoyalty

public class JavaLoyalty extends Applet implements com.sun.javacard.samples.SampleLibrary.JavaLoyaltyInterface
Minimalistic loyalty applet. JavaPurse uses grantPoints method implementing JavaLoyaltyInterface to grant loyalty points. JavaLoyalty can process only two command APDUs: READ_BALANCE and RESET_BALANCE.
author
Vadim Temkin

Fields Summary
static final byte
READ_BALANCE
static final byte
RESET_BALANCE
static final byte
CREDIT
static final byte
DEBIT
static final short
TRANSACTION_TYPE_OFFSET
static final short
TRANSACTION_AMOUNT_OFFSET
static final short
SCALE
static final short
BALANCE_MAX
short
balance
Constructors Summary
protected JavaLoyalty(byte[] bArray, short bOffset, byte bLength)
Performs memory allocations, initializations, and applet registration

param
bArray received by install.
param
bOffset received by install.
param
bLength received by install.

        balance = (short)0;
        /*
         * if AID length is not zero register Java Loyalty
         * applet with specified AID
         *
         * NOTE: all the memory allocations should be performed before register()
         */
    
        byte aidLen = bArray[bOffset];
        if (aidLen== (byte)0){
            register();
        } else {
            register(bArray, (short)(bOffset+1), aidLen);
        }
    
Methods Summary
public ShareablegetShareableInterfaceObject(AID clientAID, byte parameter)
Implements getShareableInterfaceObject method of Applet class.

JavaLoyalty could check here if the clientAID is that of JavaPurse Checking of the parameter to be agreed upon value provides additional security, or, if the Shareable Interface Object weren't JavaLoyalty itself it could return different Shareable Interface Objects for different values of clientAID and/or parameter.

SeeJava Card Runtime Environment (JCRE) Specification for details.

param
clientAID AID of the client
param
parameter additional parameter
return
JavaLoyalty object

        if (parameter == (byte)0)
            return this;
        else 
            return null;
    
public voidgrantPoints(byte[] buffer)
Implements main interaction with a client. The data is transfered through APDU buffer which is a global array accessible from any context. The format of data in the buffer is subset of Transaction Log record format: 2 bytes of 0, 1 byte of transaction type, 2 bytes amount of transaction, 4 bytes of CAD ID, 3 bytes of date, and 2 bytes of time. This sample implementation ignores everything but transaction type and amount.

param
buffer APDU buffer

        short amount = Util.getShort(buffer, TRANSACTION_AMOUNT_OFFSET);
        amount = (short) (amount / SCALE);
        switch (buffer[TRANSACTION_TYPE_OFFSET]) {
        case DEBIT: 
            balance = (short)(balance + amount); break;
        case CREDIT: 
            balance = (short) (balance - amount); break;
        }
        if (balance < 0) balance = 0;
        if (balance > BALANCE_MAX) balance = BALANCE_MAX;
        /*
         * For this simple sample applet we don't keep track of transaction locations and
         * timestamps
         */
    
public static voidinstall(byte[] bArray, short bOffset, byte bLength)
Installs Java Loyalty applet.

param
bArray install parameter array.
param
bOffset where install data begins.
param
bLength install parameter data length.

  
                              
          
              
        new JavaLoyalty(bArray, bOffset, bLength);
    
public voidprocess(APDU apdu)
Dispatches APDU commands.

param
apdu APDU

        byte[] buffer = apdu.getBuffer();
        // We don't do any additional C-APDU header checking, just rely on
        // CLA and INS bytes for dispatching.
        
        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 READ_BALANCE:  
            processReadBalance(apdu); break;
        case RESET_BALANCE: 
            processResetBalance(); break;
        default:
            ISOException.throwIt(ISO7816.SW_INS_NOT_SUPPORTED);
        }
    
voidprocessReadBalance(APDU apdu)
Sends 2 bytes of balance value in R-APDU.

param
apdu APDU

        byte[] buffer = apdu.getBuffer();
        Util.setShort(buffer, (short)0, balance);
        apdu.setOutgoingAndSend((short)0, (short)2);
    
voidprocessResetBalance()
Resets Balance.

        balance = (short)0;