2 Replies Latest reply: Feb 13, 2013 12:01 PM by safarmer RSS

    Is thoe host application supposed to be stay-resident?

    936354
      When I start my backend, I create a host application object.
      When the user sweeps the card, the host application is already gone.
      package view;
      
      import java.awt.BorderLayout;
      
      import java.awt.EventQueue;
      import java.sql.SQLException;
      
      import javax.swing.JFrame;
      import javax.swing.JPanel;
      import javax.swing.border.EmptyBorder;
      import javax.swing.JTree;
      import javax.swing.JScrollPane;
      import javax.swing.JTable;
      import javax.swing.JMenuBar;
      import javax.swing.JSplitPane;
      import javax.swing.border.EtchedBorder;
      
      public class ServerView extends JFrame {
      
           private JPanel contentPane;
           private JTable table;
           private Database db;
           private SmartCard sc;
      
           public JTable getLogTable()
           {
                return table;
           }
           /**
            * Launch the application.
            */
           public static void main(String[] args) {
                EventQueue.invokeLater(new Runnable() {
                     public void run() {
                          try {
                               ServerView frame = new ServerView();
                               frame.setVisible(true);
                          } catch (Exception e) {
                               e.printStackTrace();
                          }
                     }
                });
           }
      
           /**
            * Create the frame.
            */
           public ServerView() {
                setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
                setBounds(100, 100, 450, 470);
                
                JMenuBar menuBar = new JMenuBar();
                setJMenuBar(menuBar);
                contentPane = new JPanel();
                contentPane.setBorder(new EmptyBorder(5, 5, 5, 5));
                setContentPane(contentPane);
                contentPane.setLayout(new BorderLayout(0, 0));
                
                JSplitPane splitPane = new JSplitPane();
                contentPane.add(splitPane, BorderLayout.CENTER);
                
                JTree tree = new JTree();
                splitPane.setLeftComponent(tree);
                
                table = new JTable();
                table.setBorder(new EtchedBorder(EtchedBorder.LOWERED, null, null));
                splitPane.setRightComponent(table);
                
           
                try {
                     db = new Database();
                     db.setView(this);
                     sc = new SmartCard();
                     int empId = sc.getEmpId();
                     db.UpdateEmployeeOut(empId);
                     
                     refresh();
                     
                     
                      
                } catch (SQLException e) {
                     // TODO Auto-generated catch block
                     e.printStackTrace();
                }
                
                
           }
           
           public void refresh()
           {
                try {
                     db.UpdateTable();
                } catch (SQLException e) {
                     // TODO Auto-generated catch block
                     e.printStackTrace();
                }
           }
      
      }
      package view;
      
      import HostApplication.SimulatorTest;
      
      public class SmartCard {
           
           
           static private SimulatorTest simulator;
           
           public SmartCard()
           {
                simulator = new SimulatorTest("Test");
                
              
           }
           
           public int getEmpId()
           {
                simulator.testApplet();
                int empId = simulator.getEmpId();
                return empId;
                
           }
           
           
      
      }
      Host application:
      package HostApplication;
      
       
      
      import javacard.framework.AID;
      
      import javax.smartcardio.CommandAPDU;
      import javax.smartcardio.ResponseAPDU;
      
      import timeAttend.TimeAttendApplet;
      
      import junit.framework.TestCase;
      
      import com.licel.jcardsim.base.Simulator;
      
      /**
       * Test class Simulator.
       */
      public class SimulatorTest extends TestCase {
      
          public SimulatorTest(String testName) {
              super(testName);
          }
      
          protected void setUp() throws Exception {
              super.setUp();
          }
      
          protected void tearDown() throws Exception {
              super.tearDown();
          }
      
          /**
           * Test of installApplet method, of class Simulator.
           */
          public void testApplet() {
              Simulator simulator = new Simulator();
              byte[] appletAIDBytes = new byte[] { (byte)0x04, (byte) 0x00, (byte) 0X01, (byte) 0X02, (byte) 0X03,
                      (byte) 0X04, (byte) 0X05, (byte) 0X06, (byte) 0X07, (byte) 0X08, (byte) 0X09, (byte) 0X00, (byte) 0x7F, };
              AID appletAID = new AID(appletAIDBytes, (short) 0, (byte) appletAIDBytes.length);
              simulator.installApplet(appletAID, TimeAttendApplet.class);
              simulator.selectApplet(appletAID);
              // test NOP
              ResponseAPDU response = simulator.transmitCommand(new CommandAPDU(0x80, 0x00, 0x00, 0x00));
              byte[] res = response.getBytes();
              
              byte[] res2 = new byte[res.length-2];
              int i = 0;
               
              while (res[i] != -112)
              {
                   res2[i] = res;
           i++;
      }

      String str = new String(res2, 0, res2.length);

      id = Integer.parseInt(str);



      }

      public int getEmpId()
      {
           return id;
      }


      private int id;
      }


      Applet:
      package timeAttend;
      
      import javacard.framework.APDU;
      import javacard.framework.Applet;
      import javacard.framework.ISO7816;
      import javacard.framework.ISOException;
      import javacard.framework.Util;
      
      public class TimeAttendApplet extends Applet {
      
           private TimeAttendApplet() {
                register();
           }
      
            private static final byte[] hello = { (byte)'1', (byte)'2', (byte)'3'
               ,(byte)'4',(byte)'5',  
            };
       
            private static final byte HW_CLA = (byte) 0x80;
            private static final byte HW_INS = (byte) 0x00;
            /**
             * Installs this applet.
             * 
             * @param bArray
             *            the array containing installation parameters
             * @param bOffset
             *            the starting offset in bArray
             * @param bLength
             *            the length in bytes of the parameter data in bArray
             */
            public static void install(byte[] bArray, short bOffset, byte bLength) {
                new TimeAttendApplet();
            }
           
            /**
             * Only this class's install method should create the applet object.
             */
        
       
      
            /**
             * Processes an incoming APDU.
             * 
             * @see APDU
             * @param apdu
             *            the incoming APDU
             */
             
            public void process(APDU apdu) {
                     //Insert your code here
                 //    if (selectingApplet()) {
                 //        return;
                 //    }
                byte[] buffer = apdu.getBuffer();
                byte CLA = (byte) (buffer[ISO7816.OFFSET_CLA] & 0xFF);
                byte INS = (byte) (buffer[ISO7816.OFFSET_INS] & 0xFF);
                          
             //  getHello(apdu);
                
                if (CLA != HW_CLA)
                {
                    ISOException.throwIt(ISO7816.SW_CLA_NOT_SUPPORTED);
                }
                
                switch (INS)
                {
                    case HW_INS:
                        getHello(apdu);
                        break;
                    default:
                        ISOException.throwIt(ISO7816.SW_INS_NOT_SUPPORTED);
                        
                }
            }
                 
            private void getHello(APDU apdu)
            {
                byte[] buffer = apdu.getBuffer();
                short length = (short) hello.length;
                
                Util.arrayCopyNonAtomic(hello, (short)0, buffer, (short)0, length);
                apdu.setOutgoingAndSend((short)0, length);
            }
       
      }
      Here the simulator object is the host application, and it cannot read the Applet and showing
      java.net.BindException: Address already in use: JVM_Bind
      Exception on TCP/IP socket port # : 9025

      Is my assumption of the error correct? or the port has been opened twice?
      Thanks
      Jack

      Edited by: 933351 on 2013/1/22 下午 11:28

      Edited by: 933351 on 2013/1/22 下午 11:37