Contents Previous Next Index

Appendix   B

SATSA-JCRMI Example


This Appendix contains an example that demonstrates the use of the SATSA-JCRMI API.

Preparing the Java Card Platform Simulator

JCRMIMIDlet connects to an example card applet that is distributed with the Java Card Development Kit. If you’ve already worked through the APDUMIDlet example, note that JCRMIMIDlet uses the same demo2 card applets as APDUMIDlet. The first step is to create a card EEPROM image that contains the required applets.

To do this, run the card simulator, specifying that its EEPROM image should be saved. In this example, the file name is demo2.eeprom:

start cref -o demo2.eeprom 

In DOS, start launches cref in a separate window. The next step is to load some of the Java Card Development Kit example applets onto the card. This example assumes that the Java Card Development Kit is installed in \java_card_kit-2_2_1.

apdutool /java_card_kit-2_2_1/samples/src/demo/demo2.scr 

After this step is complete, cref exits, saving its EEPROM image in the file you specified. You only need to perform this step once.

When you have the EEPROM image file, run cref as follows:

start cref -p 9025 -i demo2.eeprom 

When you run JCRMIMIDlet, the SATSA RI is able to find the simulated card that is running the demo2 card applets.

Source Code

The source code for this example is contained in two files, APDUMIDlet.java and com\sun\javacard\samples\RMIDemo\Purse.java. Use the jcrmic tool, described in Chapter 4, to generate a stub for the remote interface Purse.

JCRMIMIDlet Source Code

/* 
 * Copyright © 2004 Sun Microsystems, Inc.  All rights reserved. 
 * Use is subject to license terms. 
 */ 
 
import java.io.*; 
import javax.microedition.io.Connector; 
import javax.microedition.jcrmi.JavaCardRMIConnection; 
import javax.microedition.midlet.*; 
import javax.microedition.lcdui.*; 
 
import com.sun.javacard.samples.RMIDemo.Purse; 
 
public class JCRMIMIDlet 
    extends MIDlet 
    implements CommandListener, Runnable { 
  private final String kRMIURL = "jcrmi:0;AID=a0.0.0.0.62.3.1.c.8.1"; 
   
  private JavaCardRMIConnection mConnection; 
   
  private Display mDisplay; 
  private Form mMainForm; 
  private Command mExitCommand, mGoCommand, mBackCommand; 
  private Form mProgressForm; 
 
  public JCRMIMIDlet() { 
    mExitCommand = new Command("Exit", Command.EXIT, 0); 
    mGoCommand = new Command("Go", Command.SCREEN, 0); 
    mBackCommand = new Command("Back", Command.BACK, 0); 
 
    mMainForm = new Form("JCRMI Example"); 
    mMainForm.append("Press Go to use the SATSA-JCRMI API " + 
        "to connect to a card application."); 
    mMainForm.addCommand(mExitCommand); 
    mMainForm.addCommand(mGoCommand); 
    mMainForm.setCommandListener(this); 
  } 
   
  public void startApp() { 
    mDisplay = Display.getDisplay(this); 
     
    mDisplay.setCurrent(mMainForm); 
  } 
   
  public void pauseApp() {} 
   
  public void destroyApp(boolean unconditional) {} 
   
  public void commandAction(Command c, Displayable s) { 
    if (c == mExitCommand) { 
      notifyDestroyed(); 
    } 
    else if (c == mGoCommand) { 
      mProgressForm = new Form("Working..."); 
      mDisplay.setCurrent(mProgressForm); 
       
      Thread t = new Thread(this); 
      t.start(); 
    } 
    else if (c == mBackCommand) { 
      mDisplay.setCurrent(mMainForm); 
    } 
  } 
   
  public void run() { 
    try { 
      setProgress("Opening a connection"); 
      mConnection = (JavaCardRMIConnection)Connector.open(kRMIURL); 
       
      setProgress("Getting an initial reference"); 
      Purse purse = (Purse)mConnection.getInitialReference(); 
 
      short balance = purse.getBalance(); 
      setProgress("Balance = " + balance); 
       
      setProgress("Crediting 20"); 
      purse.credit((short)20); 
      setProgress("Debiting 15"); 
      purse.debit((short)15); 
       
      balance = purse.getBalance(); 
      setProgress("Balance = " + balance); 
       
      setProgress("Setting account number to 54321"); 
      purse.setAccountNumber(new byte[] {5, 4, 3, 2, 1}); 
       
      setProgress("Closing connection"); 
      mConnection.close(); 
 
      mProgressForm.setTitle("Working...done."); 
      mProgressForm.addCommand(mBackCommand); 
      mProgressForm.setCommandListener(this); 
    } 
    catch (Exception e) { 
      try { mConnection.close(); } catch (Throwable t) {} 
       
      Form f = new Form("Exception"); 
      f.append(e.toString()); 
      f.addCommand(mBackCommand); 
      f.setCommandListener(this); 
      mDisplay.setCurrent(f); 
    } 
  } 
   
  private void setProgress(String s) { 
    StringItem si = new StringItem(null, s); 
    si.setLayout(Item.LAYOUT_2 | Item.LAYOUT_NEWLINE_AFTER); 
    mProgressForm.append(si); 
  } 
} 

Purse Source Code

/* 
 * Copyright © 2002 Sun Microsystems, Inc. All rights reserved. 
 * SUN PROPRIETARY/CONFIDENTIAL. Use is subject to license terms. 
 */ 
 
package com.sun.javacard.samples.RMIDemo; 
 
import java.rmi.*; 
import javacard.framework.*; 
 
public interface Purse extends Remote { 
  public static final short UNDERFLOW = (short)0x6000; 
  public static final short OVERFLOW  = (short)0x6001; 
   
  public static final short BAD_ARGUMENT  = (short)0x6002; 
   
  public static final short MAX_AMOUNT = (short)400; // for whatever reason 
   
  public short getBalance() throws RemoteException; 
  public void debit(short m) throws RemoteException, UserException; 
  public void credit(short m) throws RemoteException, UserException; 
   
  public void setAccountNumber(byte[] number)  
      throws RemoteException, UserException; 
  public byte[] getAccountNumber() throws RemoteException; 
} 

 


Contents Previous Next Index SATSA Developer's Guide
SATSA Reference Implementation 1.0