예제 #1
0
  /**
   * Laedt einen existierenden oder erstellt einen neuen Schluessel.
   *
   * @param key der Schluessel.
   * @param create true, wenn ein neuer erstellt werden soll.
   * @return der Schluessel.
   * @throws ApplicationException
   * @throws OperationCanceledException
   */
  private HBCIPassport load(RDHKey key, boolean create)
      throws ApplicationException, OperationCanceledException {
    HBCICallback callback = null;
    try {
      String filename = key.getFilename();

      if (create) {
        Logger.info("create " + getPassportType() + " key " + filename);
      } else {
        Logger.info("load " + getPassportType() + " key " + filename);

        File f = new File(filename);
        if (!f.exists()) {
          InsertKeyDialog kd = new InsertKeyDialog(f);
          Boolean b = (Boolean) kd.open();
          if (b == null || !b.booleanValue())
            throw new OperationCanceledException(
                i18n.tr("Schlüsseldiskette nicht eingelegt oder nicht lesbar"));
        }
      }

      HBCI plugin = (HBCI) Application.getPluginLoader().getPlugin(HBCI.class);
      callback = plugin.getHBCICallback();
      if (callback != null && (callback instanceof HBCICallbackSWT))
        ((HBCICallbackSWT) callback).setCurrentHandle(new PassportHandleImpl());
      else Logger.warn("unable to register current handle, callback: " + callback);

      String type = getPassportType();
      HBCIUtils.setParam("client.passport.default", type); // ist eigentlich nicht noetig
      HBCIUtils.setParam("client.passport." + type + ".filename", filename);
      HBCIUtils.setParam("client.passport." + type + ".init", "1");
      return AbstractHBCIPassport.getInstance(type);
    } catch (Exception e) {
      OperationCanceledException oce =
          (OperationCanceledException) HBCIFactory.getCause(e, OperationCanceledException.class);
      if (oce != null) throw oce;

      ApplicationException ae =
          (ApplicationException) HBCIFactory.getCause(e, ApplicationException.class);
      if (ae != null) throw ae;

      NeedKeyAckException ack =
          (NeedKeyAckException) HBCIFactory.getCause(e, NeedKeyAckException.class);
      if (ack != null) {
        String text =
            i18n.tr(
                "Bitte senden Sie den INI-Brief an Ihre Bank und warten Sie auf die Freischaltung durch die Bank.");
        Application.getMessagingFactory()
            .sendMessage(new StatusBarMessage(text, StatusBarMessage.TYPE_ERROR));
        throw new ApplicationException(text);
      }

      InvalidPassphraseException ipe =
          (InvalidPassphraseException) HBCIFactory.getCause(e, InvalidPassphraseException.class);
      if (ipe != null) {
        String text = i18n.tr("Das Passwort für die Schlüsseldatei ist falsch.");
        Application.getMessagingFactory()
            .sendMessage(new StatusBarMessage(text, StatusBarMessage.TYPE_ERROR));
        throw new ApplicationException(text);
      }

      // Keine brauchbare Exception gefunden
      Logger.error("unable to load " + getPassportType() + " key", e);
      throw new ApplicationException(
          i18n.tr("Fehler beim Laden des Schlüssels: {0}", e.getMessage()), e);
    } finally {
      if (callback != null && (callback instanceof HBCICallbackSWT))
        ((HBCICallbackSWT) callback).setCurrentHandle(null);
    }
  }