Пример #1
1
 private static TerminalFactory loadFactory(String pn, String type)
     throws NoSuchAlgorithmException {
   TerminalFactory tf = null;
   try {
     Class<?> cls = Class.forName(pn);
     Provider p = (Provider) cls.getConstructor().newInstance();
     tf = TerminalFactory.getInstance(type == null ? "PC/SC" : type, null, p);
   } catch (ClassNotFoundException
       | InstantiationException
       | IllegalAccessException
       | IllegalArgumentException
       | InvocationTargetException
       | NoSuchMethodException
       | SecurityException e) {
     throw new RuntimeException("Could not load " + pn, e);
   } catch (NoSuchAlgorithmException e) {
     if (e.getCause() != null) {
       Class<?> cause = e.getCause().getClass();
       if (cause.equals(java.lang.UnsupportedOperationException.class)) {
         throw new NoSuchAlgorithmException(e.getCause().getMessage());
       }
       if (cause.equals(java.lang.UnsatisfiedLinkError.class)) {
         throw new NoSuchAlgorithmException(e.getCause().getMessage());
       }
     }
     throw e;
   }
   return tf;
 }
Пример #2
0
  public static void main(String[] args) throws NoSuchProviderException, NoSuchAlgorithmException {
    System.out.println("Starting rfid-reader2keyboard");

    Security.insertProviderAt(new Smartcardio(), 1);
    TerminalFactory.getInstance("PC/SC", null);

    System.out.println("The following terminals were detected:");
    System.out.println(Read.listTerminals());

    System.out.println();
    System.out.println(
        "inventid RFID capturing is currently active. Close this dialog to deactivate.");
    System.out.println(
        "The most likely reason you see this is in order to resolve any issue you ay have found. Please follow"
            + " the instructions of inventid support and send these lines to the given email address");

    executorService.scheduleAtFixedRate(errorLogger, 10, 30, TimeUnit.SECONDS);
    executorService.scheduleAtFixedRate(detectorLoop, 10, 15, TimeUnit.SECONDS);

    Read reader = new Read();
    reader.startRunning();

    Runtime.getRuntime()
        .addShutdownHook(
            new Thread() {
              public void run() {
                executorService.shutdownNow();
                System.out.println(
                    "inventid RFID capturing is now inactive. You can close this dialog");
              }
            });
  }
  public void start() {

    TerminalFactory factory;
    try {
      factory = TerminalFactory.getInstance(TERMINAL_TYPE_PCSC, null);
    } catch (NoSuchAlgorithmException e1) {
      throw new RuntimeException(
          "Impossível estabelecer um  contexto; o serviço PCSC (pcscd) está executando?", e1);
    }
    this.terminals = factory.terminals();
    factory = null;

    try {
      this.terminals.list();
    } catch (CardException e) {
      throw new RuntimeException(e.getMessage(), e);
    }

    try {
      while (!this.interrompido) {
        this.terminals.waitForChange(WAIT_TIMEOUT_MS);
        if (this.terminals.list(CardTerminals.State.CARD_INSERTION).size() > 0) {
          LOGGER.fine("cartao detectado.");
          CardTerminal terminal = this.terminals.list(CardTerminals.State.CARD_INSERTION).get(0);
          this.currentCard = terminal.connect(PROTOCOL_ANY);
          this.notifyEvent(new CardEvent(this.currentCard, EventType.CARD_AVAILABLE));
        }
        if (this.terminals.list(CardTerminals.State.CARD_REMOVAL).size() > 0) {
          LOGGER.fine("cartao removido.");
          this.currentCard = null;
          this.notifyEvent(new CardEvent(this.currentCard, EventType.CARD_REMOVED));
        }
      }
    } catch (CardException e) {
      throw new RuntimeException(e.getMessage(), e);
    }
  }