简体   繁体   中英

How to use Shareable interface in Java Card applets?

I wrote two simple applets named App1 and App2 for my java card as follow :

App1 :

public class App1 extends Applet {

    private App1() {
    }

    public static void install(byte bArray[], short bOffset, byte bLength)
            throws ISOException {
        new App1().register();
    }

    public void process(APDU arg0) throws ISOException {
    if(selectingApplet()){
        return;
    }
     //I want to call "ThisMethod()" of App2 here


    }

}

App2 :

public class App2 extends Applet {

    private App2() {
    }

    public static void install(byte bArray[], short bOffset, byte bLength)
            throws ISOException {
        new App2().register();
    }

    public void process(APDU arg0) throws ISOException {

    }

    public void ThisMethod(){
        ISOException.throwIt((short)0x9001);
    }

}

As I indicate in the App1 program (As comments) I want to call ThisMethod method of App2 in the Process method of App1. As far as I know I must implement Shareable interface. But I am a little confused about it :

Questions :

1- Should I implement this method in both applets? Or I need to implement it in just App1 or App2?

2- If App1 and App2 are in a single package or in two separate packages, does it change anything?

3- Do I need Shareable interface to share an array? Or it is mandatory just for sharing methods?

  1. No, you only must create an interface that extends Shareable and then your app2 must implement that interface.

  2. It doesn't matter.

  3. Yes you also need to implement Shareable if you want to share your array

However, before you can use methods/objects shared from other applet instance, you must first store the instance of your interface in App1. You can do this by

AID aid = JCSystem.lookupAid({App2 AID byte array}, {offset}, {length});  // provide the instance AID of App2

{yourInterface} app2Instance = ({yourInterface})JCSystem.getAppletShareableInterfaceObject(aid, (byte)0);

then use app2Instance to access shared methods/objects

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM