jdk/src/share/classes/sun/security/smartcardio/CardImpl.java
author wetmore
Tue, 20 Apr 2010 14:24:06 -0700
changeset 5301 1bc2a3a47839
parent 2 90ce3da70b43
child 5506 202f599c92aa
permissions -rw-r--r--
6945604: wrong error message in CardImpl.java Reviewed-by: mullan
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
     1
/*
5301
1bc2a3a47839 6945604: wrong error message in CardImpl.java
wetmore
parents: 2
diff changeset
     2
 * Copyright 2005-2010 Sun Microsystems, Inc.  All Rights Reserved.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
     3
 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
90ce3da70b43 Initial load
duke
parents:
diff changeset
     4
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
     5
 * This code is free software; you can redistribute it and/or modify it
90ce3da70b43 Initial load
duke
parents:
diff changeset
     6
 * under the terms of the GNU General Public License version 2 only, as
90ce3da70b43 Initial load
duke
parents:
diff changeset
     7
 * published by the Free Software Foundation.  Sun designates this
90ce3da70b43 Initial load
duke
parents:
diff changeset
     8
 * particular file as subject to the "Classpath" exception as provided
90ce3da70b43 Initial load
duke
parents:
diff changeset
     9
 * by Sun in the LICENSE file that accompanied this code.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    10
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    11
 * This code is distributed in the hope that it will be useful, but WITHOUT
90ce3da70b43 Initial load
duke
parents:
diff changeset
    12
 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
90ce3da70b43 Initial load
duke
parents:
diff changeset
    13
 * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
90ce3da70b43 Initial load
duke
parents:
diff changeset
    14
 * version 2 for more details (a copy is included in the LICENSE file that
90ce3da70b43 Initial load
duke
parents:
diff changeset
    15
 * accompanied this code).
90ce3da70b43 Initial load
duke
parents:
diff changeset
    16
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    17
 * You should have received a copy of the GNU General Public License version
90ce3da70b43 Initial load
duke
parents:
diff changeset
    18
 * 2 along with this work; if not, write to the Free Software Foundation,
90ce3da70b43 Initial load
duke
parents:
diff changeset
    19
 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    20
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    21
 * Please contact Sun Microsystems, Inc., 4150 Network Circle, Santa Clara,
90ce3da70b43 Initial load
duke
parents:
diff changeset
    22
 * CA 95054 USA or visit www.sun.com if you need additional information or
90ce3da70b43 Initial load
duke
parents:
diff changeset
    23
 * have any questions.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    24
 */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    25
90ce3da70b43 Initial load
duke
parents:
diff changeset
    26
package sun.security.smartcardio;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    27
90ce3da70b43 Initial load
duke
parents:
diff changeset
    28
import java.nio.ByteBuffer;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    29
90ce3da70b43 Initial load
duke
parents:
diff changeset
    30
import javax.smartcardio.*;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    31
90ce3da70b43 Initial load
duke
parents:
diff changeset
    32
import static sun.security.smartcardio.PCSC.*;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    33
90ce3da70b43 Initial load
duke
parents:
diff changeset
    34
/**
90ce3da70b43 Initial load
duke
parents:
diff changeset
    35
 * Card implementation.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    36
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    37
 * @since   1.6
90ce3da70b43 Initial load
duke
parents:
diff changeset
    38
 * @author  Andreas Sterbenz
90ce3da70b43 Initial load
duke
parents:
diff changeset
    39
 */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    40
final class CardImpl extends Card {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    41
90ce3da70b43 Initial load
duke
parents:
diff changeset
    42
    private static enum State { OK, REMOVED, DISCONNECTED };
90ce3da70b43 Initial load
duke
parents:
diff changeset
    43
90ce3da70b43 Initial load
duke
parents:
diff changeset
    44
    // the terminal that created this card
90ce3da70b43 Initial load
duke
parents:
diff changeset
    45
    private final TerminalImpl terminal;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    46
90ce3da70b43 Initial load
duke
parents:
diff changeset
    47
    // the native SCARDHANDLE
90ce3da70b43 Initial load
duke
parents:
diff changeset
    48
    final long cardId;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    49
90ce3da70b43 Initial load
duke
parents:
diff changeset
    50
    // atr of this card
90ce3da70b43 Initial load
duke
parents:
diff changeset
    51
    private final ATR atr;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    52
90ce3da70b43 Initial load
duke
parents:
diff changeset
    53
    // protocol in use, one of SCARD_PROTOCOL_T0 and SCARD_PROTOCOL_T1
90ce3da70b43 Initial load
duke
parents:
diff changeset
    54
    final int protocol;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    55
90ce3da70b43 Initial load
duke
parents:
diff changeset
    56
    // the basic logical channel (channel 0)
90ce3da70b43 Initial load
duke
parents:
diff changeset
    57
    private final ChannelImpl basicChannel;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    58
90ce3da70b43 Initial load
duke
parents:
diff changeset
    59
    // state of this card connection
90ce3da70b43 Initial load
duke
parents:
diff changeset
    60
    private volatile State state;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    61
90ce3da70b43 Initial load
duke
parents:
diff changeset
    62
    // thread holding exclusive access to the card, or null
90ce3da70b43 Initial load
duke
parents:
diff changeset
    63
    private volatile Thread exclusiveThread;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    64
90ce3da70b43 Initial load
duke
parents:
diff changeset
    65
    CardImpl(TerminalImpl terminal, String protocol) throws PCSCException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    66
        this.terminal = terminal;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    67
        int sharingMode = SCARD_SHARE_SHARED;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    68
        int connectProtocol;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    69
        if (protocol.equals("*")) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    70
            connectProtocol = SCARD_PROTOCOL_T0 | SCARD_PROTOCOL_T1;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    71
        } else if (protocol.equalsIgnoreCase("T=0")) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    72
            connectProtocol = SCARD_PROTOCOL_T0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    73
        } else if (protocol.equalsIgnoreCase("T=1")) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    74
            connectProtocol = SCARD_PROTOCOL_T1;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    75
        } else if (protocol.equalsIgnoreCase("direct")) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    76
            // testing
90ce3da70b43 Initial load
duke
parents:
diff changeset
    77
            connectProtocol = 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    78
            sharingMode = SCARD_SHARE_DIRECT;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    79
        } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    80
            throw new IllegalArgumentException("Unsupported protocol " + protocol);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    81
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    82
        cardId = SCardConnect(terminal.contextId, terminal.name,
90ce3da70b43 Initial load
duke
parents:
diff changeset
    83
                    sharingMode, connectProtocol);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    84
        byte[] status = new byte[2];
90ce3da70b43 Initial load
duke
parents:
diff changeset
    85
        byte[] atrBytes = SCardStatus(cardId, status);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    86
        atr = new ATR(atrBytes);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    87
        this.protocol = status[1] & 0xff;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    88
        basicChannel = new ChannelImpl(this, 0);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    89
        state = State.OK;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    90
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    91
90ce3da70b43 Initial load
duke
parents:
diff changeset
    92
    void checkState()  {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    93
        State s = state;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    94
        if (s == State.DISCONNECTED) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    95
            throw new IllegalStateException("Card has been disconnected");
90ce3da70b43 Initial load
duke
parents:
diff changeset
    96
        } else if (s == State.REMOVED) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    97
            throw new IllegalStateException("Card has been removed");
90ce3da70b43 Initial load
duke
parents:
diff changeset
    98
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    99
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   100
90ce3da70b43 Initial load
duke
parents:
diff changeset
   101
    boolean isValid() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   102
        if (state != State.OK) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   103
            return false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   104
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   105
        // ping card via SCardStatus
90ce3da70b43 Initial load
duke
parents:
diff changeset
   106
        try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   107
            SCardStatus(cardId, new byte[2]);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   108
            return true;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   109
        } catch (PCSCException e) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   110
            state = State.REMOVED;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   111
            return false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   112
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   113
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   114
90ce3da70b43 Initial load
duke
parents:
diff changeset
   115
    private void checkSecurity(String action) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   116
        SecurityManager sm = System.getSecurityManager();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   117
        if (sm != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   118
            sm.checkPermission(new CardPermission(terminal.name, action));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   119
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   120
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   121
90ce3da70b43 Initial load
duke
parents:
diff changeset
   122
    void handleError(PCSCException e) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   123
        if (e.code == SCARD_W_REMOVED_CARD) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   124
            state = State.REMOVED;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   125
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   126
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   127
90ce3da70b43 Initial load
duke
parents:
diff changeset
   128
    public ATR getATR() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   129
        return atr;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   130
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   131
90ce3da70b43 Initial load
duke
parents:
diff changeset
   132
    public String getProtocol() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   133
        switch (protocol) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   134
        case SCARD_PROTOCOL_T0:
90ce3da70b43 Initial load
duke
parents:
diff changeset
   135
            return "T=0";
90ce3da70b43 Initial load
duke
parents:
diff changeset
   136
        case SCARD_PROTOCOL_T1:
90ce3da70b43 Initial load
duke
parents:
diff changeset
   137
            return "T=1";
90ce3da70b43 Initial load
duke
parents:
diff changeset
   138
        default:
90ce3da70b43 Initial load
duke
parents:
diff changeset
   139
            // should never occur
90ce3da70b43 Initial load
duke
parents:
diff changeset
   140
            return "Unknown protocol " + protocol;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   141
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   142
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   143
90ce3da70b43 Initial load
duke
parents:
diff changeset
   144
    public CardChannel getBasicChannel() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   145
        checkSecurity("getBasicChannel");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   146
        checkState();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   147
        return basicChannel;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   148
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   149
90ce3da70b43 Initial load
duke
parents:
diff changeset
   150
    private static int getSW(byte[] b) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   151
        if (b.length < 2) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   152
            return -1;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   153
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   154
        int sw1 = b[b.length - 2] & 0xff;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   155
        int sw2 = b[b.length - 1] & 0xff;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   156
        return (sw1 << 8) | sw2;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   157
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   158
90ce3da70b43 Initial load
duke
parents:
diff changeset
   159
    private static byte[] commandOpenChannel = new byte[] {0, 0x70, 0, 0, 1};
90ce3da70b43 Initial load
duke
parents:
diff changeset
   160
90ce3da70b43 Initial load
duke
parents:
diff changeset
   161
    public CardChannel openLogicalChannel() throws CardException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   162
        checkSecurity("openLogicalChannel");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   163
        checkState();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   164
        checkExclusive();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   165
        try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   166
            byte[] response = SCardTransmit
90ce3da70b43 Initial load
duke
parents:
diff changeset
   167
                (cardId, protocol, commandOpenChannel, 0, commandOpenChannel.length);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   168
            if ((response.length != 3) || (getSW(response) != 0x9000)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   169
                throw new CardException
90ce3da70b43 Initial load
duke
parents:
diff changeset
   170
                        ("openLogicalChannel() failed, card response: "
90ce3da70b43 Initial load
duke
parents:
diff changeset
   171
                        + PCSC.toString(response));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   172
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   173
            return new ChannelImpl(this, response[0]);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   174
        } catch (PCSCException e) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   175
            handleError(e);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   176
            throw new CardException("openLogicalChannel() failed", e);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   177
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   178
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   179
90ce3da70b43 Initial load
duke
parents:
diff changeset
   180
    void checkExclusive() throws CardException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   181
        Thread t = exclusiveThread;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   182
        if (t == null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   183
            return;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   184
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   185
        if (t != Thread.currentThread()) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   186
            throw new CardException("Exclusive access established by another Thread");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   187
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   188
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   189
90ce3da70b43 Initial load
duke
parents:
diff changeset
   190
    public synchronized void beginExclusive() throws CardException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   191
        checkSecurity("exclusive");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   192
        checkState();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   193
        if (exclusiveThread != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   194
            throw new CardException
90ce3da70b43 Initial load
duke
parents:
diff changeset
   195
                    ("Exclusive access has already been assigned to Thread "
90ce3da70b43 Initial load
duke
parents:
diff changeset
   196
                    + exclusiveThread.getName());
90ce3da70b43 Initial load
duke
parents:
diff changeset
   197
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   198
        try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   199
            SCardBeginTransaction(cardId);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   200
        } catch (PCSCException e) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   201
            handleError(e);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   202
            throw new CardException("beginExclusive() failed", e);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   203
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   204
        exclusiveThread = Thread.currentThread();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   205
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   206
90ce3da70b43 Initial load
duke
parents:
diff changeset
   207
    public synchronized void endExclusive() throws CardException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   208
        checkState();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   209
        if (exclusiveThread != Thread.currentThread()) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   210
            throw new IllegalStateException
90ce3da70b43 Initial load
duke
parents:
diff changeset
   211
                    ("Exclusive access not assigned to current Thread");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   212
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   213
        try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   214
            SCardEndTransaction(cardId, SCARD_LEAVE_CARD);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   215
        } catch (PCSCException e) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   216
            handleError(e);
5301
1bc2a3a47839 6945604: wrong error message in CardImpl.java
wetmore
parents: 2
diff changeset
   217
            throw new CardException("endExclusive() failed", e);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   218
        } finally {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   219
            exclusiveThread = null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   220
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   221
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   222
90ce3da70b43 Initial load
duke
parents:
diff changeset
   223
    public byte[] transmitControlCommand(int controlCode, byte[] command)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   224
            throws CardException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   225
        checkSecurity("transmitControl");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   226
        checkState();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   227
        checkExclusive();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   228
        if (command == null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   229
            throw new NullPointerException();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   230
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   231
        try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   232
            byte[] r = SCardControl(cardId, controlCode, command);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   233
            return r;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   234
        } catch (PCSCException e) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   235
            handleError(e);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   236
            throw new CardException("transmitControlCommand() failed", e);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   237
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   238
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   239
90ce3da70b43 Initial load
duke
parents:
diff changeset
   240
    public void disconnect(boolean reset) throws CardException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   241
        if (reset) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   242
            checkSecurity("reset");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   243
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   244
        if (state != State.OK) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   245
            return;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   246
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   247
        checkExclusive();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   248
        try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   249
            SCardDisconnect(cardId, (reset ? SCARD_LEAVE_CARD : SCARD_RESET_CARD));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   250
        } catch (PCSCException e) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   251
            throw new CardException("disconnect() failed", e);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   252
        } finally {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   253
            state = State.DISCONNECTED;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   254
            exclusiveThread = null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   255
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   256
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   257
90ce3da70b43 Initial load
duke
parents:
diff changeset
   258
    public String toString() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   259
        return "PC/SC card in " + terminal.getName()
90ce3da70b43 Initial load
duke
parents:
diff changeset
   260
            + ", protocol " + getProtocol() + ", state " + state;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   261
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   262
90ce3da70b43 Initial load
duke
parents:
diff changeset
   263
    protected void finalize() throws Throwable {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   264
        try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   265
            if (state == State.OK) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   266
                SCardDisconnect(cardId, SCARD_LEAVE_CARD);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   267
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   268
        } finally {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   269
            super.finalize();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   270
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   271
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   272
90ce3da70b43 Initial load
duke
parents:
diff changeset
   273
}