コード例 #1
0
  public void reportScriptFileError(File script, ScriptException e) {
    String dir = script.getParent();
    String name = script.getName() + ".error.log";
    if (dir != null) {
      File file = new File(dir + "/" + name);
      FileOutputStream fos = null;
      try {
        if (!file.exists()) {
          file.createNewFile();
        }

        fos = new FileOutputStream(file);
        String errorHeader =
            "Error on: "
                + file.getCanonicalPath()
                + "\r\nLine: "
                + e.getLineNumber()
                + " - Column: "
                + e.getColumnNumber()
                + "\r\n\r\n";
        fos.write(errorHeader.getBytes());
        fos.write(e.getMessage().getBytes());
        _log.warn(
            "Failed executing script: "
                + script.getAbsolutePath()
                + ". See "
                + file.getName()
                + " for details.");
      } catch (IOException ioe) {
        _log.warn(
            "Failed executing script: "
                + script.getAbsolutePath()
                + "\r\n"
                + e.getMessage()
                + "Additionally failed when trying to write an error report on script directory. Reason: "
                + ioe.getMessage());
        ioe.printStackTrace();
      } finally {
        try {
          fos.close();
        } catch (Exception e1) {
        }
      }
    } else {
      _log.warn(
          "Failed executing script: "
              + script.getAbsolutePath()
              + "\r\n"
              + e.getMessage()
              + "Additionally failed when trying to write an error report on script directory.");
    }
  }
コード例 #2
0
  private boolean refresh(String importString) {
    this.scope = new Scope(new VariableList(), Parser.getExistingFunctionScope());

    TreeMap imports = this.parser.getImports();
    imports.clear();

    if (importString.length() > 0) {
      String[] importList = importString.split(",");

      for (int i = 0; i < importList.length; ++i) {
        try {
          this.parser.importFile(importList[i], this.scope);
        } catch (ScriptException e) {
          // The user changed the script since it was validated
          KoLmafia.updateDisplay(MafiaState.ERROR, e.getMessage());
          return false;
        } catch (Exception e) {
          StaticEntity.printStackTrace(e);
          return false;
        }
      }
    }

    this.lastImportString = importString;
    return true;
  }
コード例 #3
0
  protected void fillRecipients(Message email, Execution execution, JCRSessionWrapper session)
      throws MessagingException {
    try {
      // to
      AddressTemplate to = getTemplate().getTo();
      if (to != null) {
        fillRecipients(to, email, Message.RecipientType.TO, execution, session);
      }

      // cc
      AddressTemplate cc = getTemplate().getCc();
      if (cc != null) {
        fillRecipients(cc, email, Message.RecipientType.CC, execution, session);
      }

      // bcc
      AddressTemplate bcc = getTemplate().getBcc();
      if (bcc != null) {
        fillRecipients(bcc, email, Message.RecipientType.BCC, execution, session);
      }
    } catch (ScriptException e) {
      logger.error(e.getMessage(), e);
    } catch (RepositoryException e) {
      logger.error(e.getMessage(), e);
    }
  }
コード例 #4
0
 private void preConfigure() {
   // Jython sys.path
   String dataPackDirForwardSlashes = SCRIPT_FOLDER.getPath().replaceAll("\\\\", "/");
   String configScript = "import sys;sys.path.insert(0,'" + dataPackDirForwardSlashes + "');";
   try {
     this.eval("jython", configScript);
   } catch (ScriptException e) {
     _log.fatal("Failed preconfiguring jython: " + e.getMessage());
   }
 }
コード例 #5
0
  protected void fillContent(Message email, Execution execution, JCRSessionWrapper session)
      throws MessagingException {
    String text = getTemplate().getText();
    String html = getTemplate().getHtml();
    List<AttachmentTemplate> attachmentTemplates = getTemplate().getAttachmentTemplates();

    try {
      if (html != null || !attachmentTemplates.isEmpty()) {
        // multipart
        MimeMultipart multipart = new MimeMultipart("related");

        BodyPart p = new MimeBodyPart();
        Multipart alternatives = new MimeMultipart("alternative");
        p.setContent(alternatives, "multipart/alternative");
        multipart.addBodyPart(p);

        // html
        if (html != null) {
          BodyPart htmlPart = new MimeBodyPart();
          html = evaluateExpression(execution, html, session);
          htmlPart.setContent(html, "text/html; charset=UTF-8");
          alternatives.addBodyPart(htmlPart);
        }

        // text
        if (text != null) {
          BodyPart textPart = new MimeBodyPart();
          text = evaluateExpression(execution, text, session);
          textPart.setContent(text, "text/plain; charset=UTF-8");
          alternatives.addBodyPart(textPart);
        }

        // attachments
        if (!attachmentTemplates.isEmpty()) {
          addAttachments(execution, multipart);
        }

        email.setContent(multipart);
      } else if (text != null) {
        // unipart
        text = evaluateExpression(execution, text, session);
        email.setText(text);
      }
    } catch (RepositoryException e) {
      logger.error(e.getMessage(), e);
    } catch (ScriptException e) {
      logger.error(e.getMessage(), e);
    }
  }
コード例 #6
0
 protected void fillSubject(Message email, Execution execution, JCRSessionWrapper session)
     throws MessagingException {
   String subject = getTemplate().getSubject();
   if (subject != null) {
     try {
       String evaluatedSubject =
           evaluateExpression(execution, subject, session).replaceAll("[\r\n]", "");
       email.setSubject(WordUtils.abbreviate(evaluatedSubject, 60, 74, "..."));
     } catch (RepositoryException e) {
       logger.error(e.getMessage(), e);
     } catch (ScriptException e) {
       logger.error(e.getMessage(), e);
     }
   }
 }
コード例 #7
0
  /**
   * Fills the <code>from</code> attribute of the given email. The sender addresses are an optional
   * element in the mail template. If absent, each mail server supplies the current user's email
   * address.
   *
   * @see {@link InternetAddress#getLocalAddress(Session)}
   */
  protected void fillFrom(Message email, Execution execution, JCRSessionWrapper session)
      throws MessagingException {
    try {
      AddressTemplate fromTemplate = getTemplate().getFrom();
      // "from" attribute is optional
      if (fromTemplate == null) return;

      // resolve and parse addresses
      String addresses = fromTemplate.getAddresses();
      if (addresses != null) {
        addresses = evaluateExpression(execution, addresses, session);
        // non-strict parsing applies to a list of mail addresses entered by a human
        email.addFrom(InternetAddress.parse(addresses, false));
      }

      EnvironmentImpl environment = EnvironmentImpl.getCurrent();
      IdentitySession identitySession = environment.get(IdentitySession.class);
      AddressResolver addressResolver = environment.get(AddressResolver.class);

      // resolve and tokenize users
      String userList = fromTemplate.getUsers();
      if (userList != null) {
        String[] userIds = tokenizeActors(userList, execution, session);
        List<User> users = identitySession.findUsersById(userIds);
        email.addFrom(resolveAddresses(users, addressResolver));
      }

      // resolve and tokenize groups
      String groupList = fromTemplate.getGroups();
      if (groupList != null) {
        for (String groupId : tokenizeActors(groupList, execution, session)) {
          Group group = identitySession.findGroupById(groupId);
          email.addFrom(addressResolver.resolveAddresses(group));
        }
      }
    } catch (ScriptException e) {
      logger.error(e.getMessage(), e);
    } catch (RepositoryException e) {
      logger.error(e.getMessage(), e);
    }
  }
コード例 #8
0
  @Test
  public void add_error_information() throws Exception {
    final String SCRIPT = "src/test/resources/bsh/test2.bsh";

    BugFreeBeanShell test =
        new BugFreeBeanShell() {
          @Override
          public void beanshellSetup() throws Exception {
            setBshFileName(SCRIPT);
          }
        };
    test.setUp();

    try {
      test.exec();
      fail("A EvalError shall be trown!");
    } catch (ScriptException x) {
      then(x.getMessage())
          .contains(
              String.format(
                  "%s:%d at '%s'", "src/test/resources/bsh/test2.bsh", 5, "hello .prop "));
    }
  }
コード例 #9
0
  @Test
  public void shouldMapScriptErrorCodeToMessage() {
    // given
    ScriptException se;

    // when
    se = new ScriptException(CONTEXT_NOT_RECOGNISED, context);
    // then
    assertThat(se.getMessage()).isEqualTo(format("Script type not recognised: {0}", context));

    // when
    se = new ScriptException(LANGUAGE_NOT_SUPPORTED, language);
    // then
    assertThat(se.getMessage())
        .isEqualTo(format("Scripting language not supported: {0}", language));

    // when
    se = new ScriptException(FIND_BY_NAME_FAILED, scriptName, realm);
    // then
    assertThat(se.getMessage())
        .isEqualTo(format("Failed to read script called {0} from realm {1}", scriptName, realm));

    // when
    se = new ScriptException(FIND_BY_UUID_FAILED, uuid, realm);
    // then
    assertThat(se.getMessage())
        .isEqualTo(format("Failed to read script with UUID {0} from realm " + "{1}", uuid, realm));

    // when
    se = new ScriptException(DELETE_FAILED, uuid, realm);
    // then
    assertThat(se.getMessage())
        .isEqualTo(
            format("Failed to delete script with UUID {0} from realm " + "{1}", uuid, realm));

    // when
    se = new ScriptException(RETRIEVE_FAILED, uuid, realm);
    // then
    assertThat(se.getMessage())
        .isEqualTo(
            format("Failed to retrieve script with UUID {0} from " + "realm {1}", uuid, realm));

    // when
    se = new ScriptException(RETRIEVE_ALL_FAILED, realm);
    // then
    assertThat(se.getMessage())
        .isEqualTo(format("Failed to retrieve scripts from realm {0}", realm));

    // when
    se = new ScriptException(SAVE_FAILED, uuid, realm);
    // then
    assertThat(se.getMessage())
        .isEqualTo(format("Failed to save script with UUID {0} in realm " + "{1}", uuid, realm));

    // when
    se = new ScriptException(MISSING_SCRIPT_UUID);
    // then
    assertThat(se.getMessage()).isEqualTo("Script UUID must be specified");

    // when
    se = new ScriptException(MISSING_SCRIPT_NAME);
    // then
    assertThat(se.getMessage()).isEqualTo("Script name must be specified");

    // when
    se = new ScriptException(MISSING_SCRIPT);
    // then
    assertThat(se.getMessage()).isEqualTo("A script must be specified");

    // when
    se = new ScriptException(MISSING_SCRIPTING_LANGUAGE);
    // then
    assertThat(se.getMessage()).isEqualTo("Scripting language must be specified");

    // when
    se = new ScriptException(MISSING_SCRIPT_CONTEXT);
    // then
    assertThat(se.getMessage()).isEqualTo("Script type must be specified");

    // when
    se = new ScriptException(SCRIPT_NAME_EXISTS, scriptName, realm);
    // then
    assertThat(se.getMessage())
        .isEqualTo(format("Script with name {0} already exist in realm {1}", scriptName, realm));

    // when
    se = new ScriptException(SCRIPT_UUID_EXISTS, uuid, realm);
    // then
    assertThat(se.getMessage())
        .isEqualTo(format("Script with UUID {0} already exist in realm {1}", uuid, realm));

    // when
    se = new ScriptException(SCRIPT_UUID_NOT_FOUND, uuid, realm);
    // then
    assertThat(se.getMessage())
        .isEqualTo(format("Script with UUID {0} could not be found in realm {1}", uuid, realm));

    // when
    se = new ScriptException(FILTER_BOOLEAN_LITERAL_FALSE);
    // then
    assertThat(se.getMessage())
        .isEqualTo("The 'boolean literal' filter with value of 'false' is not supported");

    // when
    se = new ScriptException(FILTER_EXTENDED_MATCH);
    // then
    assertThat(se.getMessage()).isEqualTo("The 'extended match' filter is not supported");

    // when
    se = new ScriptException(FILTER_GREATER_THAN);
    // then
    assertThat(se.getMessage()).isEqualTo("The 'greater than' filter is not supported");

    // when
    se = new ScriptException(FILTER_GREATER_THAN_OR_EQUAL);
    // then
    assertThat(se.getMessage()).isEqualTo("The 'greater than or equal' filter is not supported");

    // when
    se = new ScriptException(FILTER_LESS_THAN);
    // then
    assertThat(se.getMessage()).isEqualTo("The 'less than' filter is not supported");

    // when
    se = new ScriptException(FILTER_LESS_THAN_OR_EQUAL);
    // then
    assertThat(se.getMessage()).isEqualTo("The 'less than or equal' filter is not supported");

    // when
    se = new ScriptException(FILTER_NOT);
    // then
    assertThat(se.getMessage()).isEqualTo("The 'not' filter is not supported");

    // when
    se = new ScriptException(FILTER_PRESENT);
    // then
    assertThat(se.getMessage()).isEqualTo("The 'present' filter is not supported");
  }
コード例 #10
0
ファイル: Repl.java プロジェクト: anba/es6draft
 private void handleException(Realm realm, ScriptException e) {
   String message = formatMessage("uncaught_exception", e.getMessage(realm.defaultContext()));
   console.printf("%s%n", message);
   printScriptStackTrace(realm, e);
   printStackTrace(e);
 }