Search in sources :

Example 1 with ApduConnectionOpenedInExclusiveModeException

use of es.gob.jmulticard.apdu.connection.ApduConnectionOpenedInExclusiveModeException in project jmulticard by ctt-gob-es.

the class SmartcardIoConnection method open.

/**
 * {@inheritDoc}
 */
@Override
public void open() throws ApduConnectionException {
    // Desactivamos las respuestas automaticas para evitar los problemas con el canal seguro
    // $NON-NLS-1$ //$NON-NLS-2$
    System.setProperty("sun.security.smartcardio.t0GetResponse", "false");
    // $NON-NLS-1$ //$NON-NLS-2$
    System.setProperty("sun.security.smartcardio.t1GetResponse", "false");
    if (isExclusiveUse() && isOpen()) {
        throw new ApduConnectionOpenedInExclusiveModeException();
    }
    final List<CardTerminal> terminales;
    try {
        terminales = TerminalFactory.getDefault().terminals().list();
    } catch (final Exception e) {
        throw new NoReadersFoundException(// $NON-NLS-1$
        "No se han podido listar los lectores del sistema: " + e, // $NON-NLS-1$
        e);
    }
    try {
        if (terminales.size() < 1) {
            throw new NoReadersFoundException();
        }
        if (this.terminalNumber == -1) {
            final long[] cadsWithCard = getTerminals(true);
            if (cadsWithCard.length > 0) {
                this.terminalNumber = (int) cadsWithCard[0];
            } else {
                throw new ApduConnectionException(// $NON-NLS-1$
                "En el sistema no hay ningun terminal con tarjeta insertada");
            }
        }
        if (terminales.size() <= this.terminalNumber) {
            throw new ApduConnectionException(// $NON-NLS-1$
            "No se detecto el lector de tarjetas numero " + Integer.toString(this.terminalNumber));
        }
        this.card = terminales.get(this.terminalNumber).connect(this.protocol.toString());
    } catch (final javax.smartcardio.CardNotPresentException e) {
        throw new CardNotPresentException(e);
    } catch (final CardException e) {
        throw new ApduConnectionException(// $NON-NLS-1$ //$NON-NLS-2$
        "No se ha podido abrir la conexion con el lector de tarjetas numero " + Integer.toString(this.terminalNumber) + ": " + e, // $NON-NLS-1$ //$NON-NLS-2$
        e);
    }
    if (this.exclusive) {
        try {
            this.card.beginExclusive();
        } catch (final CardException e) {
            throw new ApduConnectionException(// $NON-NLS-1$ //$NON-NLS-2$
            "No se ha podido abrir la conexion exclusiva con el lector de tarjetas numero " + Integer.toString(this.terminalNumber) + ": " + e, // $NON-NLS-1$ //$NON-NLS-2$
            e);
        }
    }
    this.canal = this.card.getBasicChannel();
}
Also used : CardTerminal(javax.smartcardio.CardTerminal) CardException(javax.smartcardio.CardException) ApduConnectionOpenedInExclusiveModeException(es.gob.jmulticard.apdu.connection.ApduConnectionOpenedInExclusiveModeException) CardNotPresentException(es.gob.jmulticard.apdu.connection.CardNotPresentException) CardNotPresentException(es.gob.jmulticard.apdu.connection.CardNotPresentException) ApduConnectionException(es.gob.jmulticard.apdu.connection.ApduConnectionException) CardException(javax.smartcardio.CardException) ApduConnectionOpenedInExclusiveModeException(es.gob.jmulticard.apdu.connection.ApduConnectionOpenedInExclusiveModeException) NoReadersFoundException(es.gob.jmulticard.apdu.connection.NoReadersFoundException) LostChannelException(es.gob.jmulticard.apdu.connection.LostChannelException) NoReadersFoundException(es.gob.jmulticard.apdu.connection.NoReadersFoundException) ApduConnectionException(es.gob.jmulticard.apdu.connection.ApduConnectionException)

Aggregations

ApduConnectionException (es.gob.jmulticard.apdu.connection.ApduConnectionException)1 ApduConnectionOpenedInExclusiveModeException (es.gob.jmulticard.apdu.connection.ApduConnectionOpenedInExclusiveModeException)1 CardNotPresentException (es.gob.jmulticard.apdu.connection.CardNotPresentException)1 LostChannelException (es.gob.jmulticard.apdu.connection.LostChannelException)1 NoReadersFoundException (es.gob.jmulticard.apdu.connection.NoReadersFoundException)1 CardException (javax.smartcardio.CardException)1 CardTerminal (javax.smartcardio.CardTerminal)1