public static String getClassForFile(File script) {
   String path = script.getAbsolutePath();
   String scpPath = SCRIPT_FOLDER.getAbsolutePath();
   if (path.startsWith(scpPath)) {
     int idx = path.lastIndexOf('.');
     return path.substring(scpPath.length() + 1, idx);
   }
   return null;
 }
  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.");
    }
  }
Beispiel #3
0
 public static void main(String[] args) throws Exception {
   System.out.println("\nTest7\n");
   File file = new File(System.getProperty("test.src", "."), "Test7.js");
   Reader r = new FileReader(file);
   ScriptEngineManager m = new ScriptEngineManager();
   ScriptEngine eng = Helper.getJsEngine(m);
   if (eng == null) {
     System.out.println("Warning: No js engine found; test vacuously passes.");
     return;
   }
   eng.put("filename", file.getAbsolutePath());
   eng.eval(r);
   String str = (String) eng.get("firstLine");
   // do not change first line in Test7.js -- we check it here!
   if (!str.equals("//this is the first line of Test7.js")) {
     throw new RuntimeException("unexpected first line");
   }
 }
  public void executeScript(ScriptEngine engine, File file)
      throws FileNotFoundException, ScriptException {
    BufferedReader reader = new BufferedReader(new InputStreamReader(new FileInputStream(file)));

    if (Config.SCRIPT_DEBUG) {
      _log.info("Loading Script: " + file.getAbsolutePath());
    }

    if (Config.SCRIPT_ERROR_LOG) {
      String name = file.getAbsolutePath() + ".error.log";
      File errorLog = new File(name);
      if (errorLog.isFile()) {
        errorLog.delete();
      }
    }

    if (engine instanceof Compilable && Config.SCRIPT_ALLOW_COMPILATION) {
      ScriptContext context = new SimpleScriptContext();
      context.setAttribute(
          "mainClass",
          getClassForFile(file).replace('/', '.').replace('\\', '.'),
          ScriptContext.ENGINE_SCOPE);
      context.setAttribute(ScriptEngine.FILENAME, file.getName(), ScriptContext.ENGINE_SCOPE);
      context.setAttribute(
          "classpath", SCRIPT_FOLDER.getAbsolutePath(), ScriptContext.ENGINE_SCOPE);
      context.setAttribute(
          "sourcepath", SCRIPT_FOLDER.getAbsolutePath(), ScriptContext.ENGINE_SCOPE);
      context.setAttribute(
          JythonScriptEngine.JYTHON_ENGINE_INSTANCE, engine, ScriptContext.ENGINE_SCOPE);

      setCurrentLoadingScript(file);
      ScriptContext ctx = engine.getContext();
      try {
        engine.setContext(context);
        if (Config.SCRIPT_CACHE) {
          CompiledScript cs = _cache.loadCompiledScript(engine, file);
          cs.eval(context);
        } else {
          Compilable eng = (Compilable) engine;
          CompiledScript cs = eng.compile(reader);
          cs.eval(context);
        }
      } finally {
        engine.setContext(ctx);
        setCurrentLoadingScript(null);
        context.removeAttribute(ScriptEngine.FILENAME, ScriptContext.ENGINE_SCOPE);
        context.removeAttribute("mainClass", ScriptContext.ENGINE_SCOPE);
      }
    } else {
      ScriptContext context = new SimpleScriptContext();
      context.setAttribute(
          "mainClass",
          getClassForFile(file).replace('/', '.').replace('\\', '.'),
          ScriptContext.ENGINE_SCOPE);
      context.setAttribute(ScriptEngine.FILENAME, file.getName(), ScriptContext.ENGINE_SCOPE);
      context.setAttribute(
          "classpath", SCRIPT_FOLDER.getAbsolutePath(), ScriptContext.ENGINE_SCOPE);
      context.setAttribute(
          "sourcepath", SCRIPT_FOLDER.getAbsolutePath(), ScriptContext.ENGINE_SCOPE);
      setCurrentLoadingScript(file);
      try {
        engine.eval(reader, context);
      } finally {
        setCurrentLoadingScript(null);
        engine.getContext().removeAttribute(ScriptEngine.FILENAME, ScriptContext.ENGINE_SCOPE);
        engine.getContext().removeAttribute("mainClass", ScriptContext.ENGINE_SCOPE);
      }
    }
  }
Beispiel #5
0
 public static String temp_filePath(String namePattern, String ext)
     throws IOException, FileNotFoundException {
   File temp = File.createTempFile("temp-file-name", ".tmp");
   return temp.getAbsolutePath();
 }