private static GremlinScriptContext convertContext(final ScriptContext context) {
   if (context instanceof GremlinScriptContext) return (GremlinScriptContext) context;
   else {
     GremlinScriptContext context2 = new GremlinScriptContext();
     for (int scope : context.getScopes()) {
       context2.setBindings(context.getBindings(scope), scope);
     }
     return context2;
   }
 }
  /* Utility functions for the binding classes */
  protected static ScriptEngine createScriptEngine(String language) throws ScriptException {
    ScriptEngineManager manager = new ScriptEngineManager();
    ScriptEngine engine = manager.getEngineByName(language);

    ScriptContext scriptContext = engine.getContext();
    Bindings bindings = scriptContext.getBindings(ScriptContext.ENGINE_SCOPE);
    scriptContext.setBindings(new JSFBindings(bindings), ScriptContext.ENGINE_SCOPE);

    return engine;
  }
 private static void typeCastContextBindings(final ScriptContext context) {
   for (int scope : context.getScopes()) {
     Bindings bindings = context.getBindings(scope);
     if (!(bindings instanceof VariableLibrary) && null != bindings) {
       for (String key : bindings.keySet()) {
         Object object = bindings.get(key);
         if (object instanceof Atom) {
           bindings.put(key, ((Atom) object).getValue());
         }
       }
     }
   }
 }
  public Object eval(final Reader reader, final ScriptContext context) {

    String line;
    Iterable result;
    StringBuilder script = new StringBuilder();
    BufferedReader bReader = new BufferedReader(reader);

    try {
      // read whole script before evaluation
      while ((line = bReader.readLine()) != null) {
        script.append(line).append("\n");
      }

      // evaluate script
      result = this.evaluate(script.toString(), convertContext(context));

      // flushing output streams
      context.getWriter().flush();
      typeCastContextBindings(context);

    } catch (SyntaxErrorException e) {
      throw e;
    } catch (Exception e) {
      throw new RuntimeException(e.getMessage());
    }

    return result;
  }
Esempio n. 5
0
 PageInfo getPageInfo(ScriptContext context, String path) throws IOException {
   PageInfo info = null;
   if (pageInfoCache != null) info = this.pageInfoCache.get(path);
   if (info == null) {
     String file = prefix + path + suffix;
     String actualPath = FileHelper.findRecursive(file);
     String source = FileHelper.read(actualPath);
     Object object = context.evaluate(source, path);
     info = new PageInfo((BaseFunction) object, actualPath);
     if (pageInfoCache != null) pageInfoCache.put(path, info);
   }
   return info;
 }
Esempio n. 6
0
 void serve(
     HttpServletRequest request,
     HttpServletResponse response,
     ScriptContext context,
     String path,
     Exception error)
     throws IOException {
   PageInfo info = getPageInfo(context, path);
   context.put("request", request);
   context.put("response", response);
   context.put("path", path);
   context.put("servlet", this);
   context.put("__filename", info.path);
   context.put("__pagepath", getPagePath(info.path));
   if (error != null) context.put("error", error);
   context.call(info.function);
 }
Esempio n. 7
0
  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);
      }
    }
  }
 public Object eval(ScriptContext context) throws ScriptException {
   return eval(((LuajContext) context).globals, context.getBindings(ScriptContext.ENGINE_SCOPE));
 }