use of javax.smartcardio.CardException in project jdk8u_jdk by JetBrains.
the class TestExclusive method main.
public static void main(String[] args) throws Exception {
CardTerminal terminal = getTerminal(args);
if (terminal == null) {
System.out.println("Skipping the test: " + "no card terminals available");
return;
}
// establish a connection with the card
Card card = terminal.connect("T=0");
System.out.println("card: " + card);
Thread thread = new Thread(new OtherThread(card));
thread.setDaemon(true);
thread.start();
card.beginExclusive();
exclusive = true;
Thread.sleep(1000);
System.out.println("=1=resuming...");
CardChannel channel = card.getBasicChannel();
System.out.println("=1=Transmitting...");
transmitTestCommand(channel);
System.out.println("=1=OK");
try {
card.beginExclusive();
} catch (CardException e) {
System.out.println("=1=OK: " + e);
}
card.endExclusive();
try {
card.endExclusive();
} catch (IllegalStateException e) {
System.out.println("=1=OK: " + e);
}
exclusive = false;
Thread.sleep(1000);
// disconnect
card.disconnect(true);
if (!otherOK) {
throw new Exception("Secondary thread failed");
}
System.out.println("=1=OK.");
}
use of javax.smartcardio.CardException 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();
}
use of javax.smartcardio.CardException in project open-ecard by ecsec.
the class PCSCTerminals method list.
public List<SCIOTerminal> list(State state, boolean firstTry) throws SCIOException {
LOG.trace("Entering list().");
try {
CardTerminals.State scState = convertState(state);
// get terminals with the specified state from the SmartcardIO
List<CardTerminal> scList = terminals.list(scState);
ArrayList<SCIOTerminal> list = convertTerminals(scList);
LOG.trace("Leaving list().");
return Collections.unmodifiableList(list);
} catch (CardException ex) {
SCIOErrorCode code = getCode(ex);
if (code == SCIOErrorCode.SCARD_E_NO_READERS_AVAILABLE) {
LOG.debug("No reader available exception.");
return Collections.emptyList();
} else if (code == SCIOErrorCode.SCARD_E_NO_SERVICE || code == SCIOErrorCode.SCARD_E_SERVICE_STOPPED) {
if (firstTry) {
LOG.debug("No service available exception, reloading PCSC and trying again.");
reloadFactory();
return list(state, false);
} else {
LOG.debug("No service available exception, returning empty list.");
return Collections.emptyList();
}
}
String msg = "Failed to retrieve list from terminals instance.";
LOG.error(msg, ex);
throw new SCIOException(msg, code, ex);
}
}
use of javax.smartcardio.CardException in project jdk8u_jdk by JetBrains.
the class TestConnectAgain method main.
public static void main(String[] args) throws Exception {
CardTerminal terminal = getTerminal(args);
if (terminal == null) {
System.out.println("Skipping the test: " + "no card terminals available");
return;
}
Card card = terminal.connect("T=0");
CardChannel channel = card.getBasicChannel();
transmitTestCommand(channel);
Card card2 = terminal.connect("*");
if (card != card2) {
throw new Exception("Different card object");
}
card2 = terminal.connect("T=0");
if (card != card2) {
throw new Exception("Different card object");
}
System.out.println("Remove card!");
terminal.waitForCardAbsent(0);
try {
transmitTestCommand(channel);
throw new Exception();
} catch (CardException e) {
System.out.println("OK: " + e);
}
System.out.println("Insert card!");
terminal.waitForCardPresent(0);
try {
transmitTestCommand(channel);
throw new Exception();
} catch (IllegalStateException e) {
System.out.println("OK: " + e);
}
card = terminal.connect("*");
if (card == card2) {
throw new Exception("Old card object");
}
try {
transmitTestCommand(channel);
throw new Exception();
} catch (IllegalStateException e) {
System.out.println("OK: " + e);
}
channel = card.getBasicChannel();
transmitTestCommand(channel);
card2 = terminal.connect("*");
if (card != card2) {
throw new Exception("Different card object");
}
// disconnect
card.disconnect(true);
System.out.println("OK.");
}
use of javax.smartcardio.CardException in project jdk8u_jdk by JetBrains.
the class TestControl method main.
public static void main(String[] args) throws Exception {
CardTerminal terminal = getTerminal(args);
if (terminal == null) {
System.out.println("Skipping the test: " + "no card terminals available");
return;
}
// establish a connection with the card
Card card = terminal.connect("T=0");
System.out.println("card: " + card);
byte[] data = new byte[] { 2 };
try {
byte[] resp = card.transmitControlCommand(IOCTL_SMARTCARD_VENDOR_IFD_EXCHANGE, data);
System.out.println("Firmware: " + toString(resp));
throw new Exception();
} catch (CardException e) {
// we currently don't know of any control commands that work with
// our readers. call the function just to make sure we don't crash
// or throw the wrong exception
System.out.println("OK: " + e);
e.printStackTrace(System.out);
}
try {
card.transmitControlCommand(IOCTL_SMARTCARD_VENDOR_IFD_EXCHANGE, null);
} catch (NullPointerException e) {
System.out.println("OK: " + e);
}
// disconnect
card.disconnect(true);
System.out.println("OK.");
}
Aggregations