Package javax.script

Examples of javax.script.ScriptException


        .getBindings(ScriptContext.ENGINE_SCOPE);

    SlingScriptHelper helper = (SlingScriptHelper) bindings
        .get(SlingBindings.SLING);
    if (helper == null) {
      throw new ScriptException("SlingScriptHelper missing from bindings");
    }

    // ensure GET request
    if (helper.getRequest() != null && !"GET".equals(helper.getRequest().getMethod())) {
      throw new ScriptException(
          "Python scripting only supports GET requests");
    }

    final ClassLoader oldClassLoader = Thread.currentThread()
        .getContextClassLoader();
    try {
      Thread.currentThread().setContextClassLoader(
          getClass().getClassLoader());
      StringBuffer scriptString = new StringBuffer();
      BufferedReader bufferedScript = new BufferedReader(script);
      String nextLine = bufferedScript.readLine();
      String newLine = System.getProperty("line.separator");
      while (nextLine != null) {
        scriptString.append(nextLine);
        scriptString.append(newLine);
        nextLine = bufferedScript.readLine();
      }

      // set writer
      interp.setOut(scriptContext.getWriter());
      interp.setErr(scriptContext.getErrorWriter());
     
      // set all bindings
      for (Object entryObj : bindings.entrySet()) {
        Map.Entry<?, ?> entry = (Map.Entry<?, ?>) entryObj;
        interp.set((String) entry.getKey(), entry.getValue());
      }

      // execute Python code
      interp.exec(scriptString.toString());

    } catch (Throwable t) {
      final ScriptException ex = new ScriptException(
          "Failure running Python script:" + t);
      ex.initCause(t);
      throw ex;
    } finally {
      Thread.currentThread().setContextClassLoader(oldClassLoader);
    }
    return null;
View Full Code Here


    private static SlingScriptHelper getScriptHelper(ScriptContext context) throws ScriptException {
        SlingBindings bindings = new SlingBindings();
        bindings.putAll(context.getBindings(ScriptContext.ENGINE_SCOPE));
        SlingScriptHelper scriptHelper = bindings.getSling();
        if (scriptHelper == null) {
            throw new ScriptException("Error retrieving Sling script helper from script context");
        }
        return scriptHelper;
    }
View Full Code Here

    public Object eval(Reader reader, ScriptContext ctx) throws ScriptException {
        Template template = null;
        try {
            template = templateEngine.createTemplate(reader);
        } catch (IOException e) {
            throw new ScriptException("Unable to compile GSP script: " + e.getMessage());
        } catch (ClassNotFoundException e) {
            throw new ScriptException("Unable to compile GSP script: " + e.getMessage());
        }

        Bindings bindings = ctx.getBindings(ScriptContext.ENGINE_SCOPE);

        Writable result = template.make(bindings);

        try {
            result.writeTo(ctx.getWriter());
        } catch (IOException e) {
            throw new ScriptException("Unable to write result of script execution: " + e.getMessage());
        }

        return null;
    }
View Full Code Here

  public Object eval(Reader reader, ScriptContext scriptContext) throws ScriptException {
    Bindings bindings = scriptContext.getBindings(ScriptContext.ENGINE_SCOPE);
    SlingScriptHelper helper = (SlingScriptHelper) bindings.get(SlingBindings.SLING);
    if (helper == null) {
      throw new ScriptException("SlingScriptHelper missing from bindings");
    }
   
    String scriptName = helper.getScript().getScriptResource().getPath();
   
    try {
      XplBuilder xplBuilder = new XplBuilder();
      Pipeline xpl = (Pipeline) xplBuilder.build(reader);
      xpl.getEnv().setSling(helper);
      xpl.eval();
    } catch (Throwable t) {
      log.error("Failure running XProc script.", t);
      final ScriptException se = new ScriptException("Failure running XProc script " + scriptName);
      se.initCause(t);
      throw se;
    }
   
    return null;
  }
View Full Code Here

    public Object eval(Reader reader, ScriptContext scriptContext)
            throws ScriptException {
        Bindings bindings = scriptContext.getBindings(ScriptContext.ENGINE_SCOPE);
        SlingScriptHelper helper = (SlingScriptHelper) bindings.get(SlingBindings.SLING);
        if (helper == null) {
            throw new ScriptException("SlingScriptHelper missing from bindings");
        }

        // ensure GET request
        if (!"GET".equals(helper.getRequest().getMethod())) {
            throw new ScriptException(
                "FreeMarker templates only support GET requests");
        }

        String scriptName = helper.getScript().getScriptResource().getPath();

        try {
            Template tmpl = new Template(scriptName, reader, configuration);
            bindings.put("currentNode", new NodeModel((Node) bindings.get("currentNode")));
            tmpl.process(bindings, scriptContext.getWriter());
        } catch (Throwable t) {
            log.error("Failure running Freemarker script.", t);
            throw new ScriptException("Failure running FreeMarker script "
                + scriptName);
        }

        return null;
    }
View Full Code Here

    public Object eval(final Reader reader, final ScriptContext scriptContext) throws ScriptException {
        final Bindings bindings = scriptContext.getBindings(ScriptContext.ENGINE_SCOPE);
        final SlingScriptHelper helper = (SlingScriptHelper) bindings.get(SlingBindings.SLING);

        if (helper == null) {
            throw new ScriptException("SlingScriptHelper missing from bindings");
        }

        final SlingHttpServletRequest request = helper.getRequest();
        final SlingHttpServletResponse response = helper.getResponse();
        final ServletContext servletContext = null; // only used by Thymeleaf's ServletContextResourceResolver

        final Locale locale = helper.getResponse().getLocale();
        final String scriptName = helper.getScript().getScriptResource().getPath();

        try {
            final IContext context = new SlingWebContext(request, response, servletContext, locale, bindings);
            thymeleafScriptEngineFactory.getTemplateEngine().process(scriptName, context, scriptContext.getWriter());
        } catch (Exception e) {
            logger.error("Failure rendering Thymeleaf template '{}': {}", scriptName, e.getMessage());
            throw new ScriptException(e);
        }

        return null;
    }
View Full Code Here

            return rhinoContext.evaluateReader(scope, scriptReader, scriptName,
                lineNumber, securityDomain);

        } catch (JavaScriptException t) {
           
            final ScriptException se = new ScriptException(t.details(),
                t.sourceName(), t.lineNumber());

            ((Logger) bindings.get(SlingBindings.LOG)).error(t.getScriptStackTrace());
            se.setStackTrace(t.getStackTrace());
            throw se;
           
        } catch (Throwable t) {
           
            final ScriptException se = new ScriptException("Failure running script " + scriptName
                + ": " + t.getMessage());
            se.initCause(t);
            throw se;
           
        } finally {
           
            Context.exit();
View Full Code Here

            SlingScriptHelper scriptHelper = (SlingScriptHelper) props.get(SLING);
            if (scriptHelper != null) {
                try {
                    callJsp(props, scriptHelper);
                } catch (Exception e) {
                    throw new ScriptException(e);
                }
            }
            return null;
        }
View Full Code Here

                    Context.javaToJS(entry.getValue(), scope));
        }
        try {
            return cx.evaluateString(scope, script, filename, 1, null);
        } catch (Error e) {
            throw new ScriptException(e.getMessage());
        } catch (RhinoException e) {
            if (e instanceof WrappedException) {
                Throwable cause = e.getCause();
                if (cause instanceof WorldEditException) {
                    throw cause;
                }
            }

            String msg;
            int line = (line = e.lineNumber()) == 0 ? -1 : line;

            if (e instanceof JavaScriptException) {
                msg = String.valueOf(((JavaScriptException) e).getValue());
            } else {
                msg = e.getMessage();
            }

            ScriptException scriptException =
                    new ScriptException(msg, e.sourceName(), line);
            scriptException.initCause(e);

            throw scriptException;
        } finally {
            Context.exit();
        }
View Full Code Here

                msg = String.valueOf(((JavaScriptException) e).getValue());
            } else {
                msg = e.getMessage();
            }

            ScriptException scriptException =
                    new ScriptException(msg, e.sourceName(), line);
            scriptException.initCause(e);

            throw scriptException;
        } finally {
            Context.exit();
        }
View Full Code Here

TOP

Related Classes of javax.script.ScriptException

Copyright © 2018 www.massapicom. All rights reserved.
All source code are property of their respective owners. Java is a trademark of Sun Microsystems, Inc and owned by ORACLE Inc. Contact coftware#gmail.com.