Search in sources :

Example 1 with GroovyScriptEngine

use of groovy.util.GroovyScriptEngine in project groovy by apache.

the class GroovyServlet method service.

/**
     * Handle web requests to the GroovyServlet
     */
public void service(HttpServletRequest request, HttpServletResponse response) throws IOException {
    // Get the script path from the request - include aware (GROOVY-815)
    final String scriptUri = getScriptUri(request);
    // Set it to HTML by default
    response.setContentType("text/html; charset=" + encoding);
    // Set up the script context
    final ServletBinding binding = new ServletBinding(request, response, servletContext);
    setVariables(binding);
    // Run the script
    try {
        Closure closure = new Closure(gse) {

            public Object call() {
                try {
                    return ((GroovyScriptEngine) getDelegate()).run(scriptUri, binding);
                } catch (ResourceException e) {
                    throw new RuntimeException(e);
                } catch (ScriptException e) {
                    throw new RuntimeException(e);
                }
            }
        };
        GroovyCategorySupport.use(ServletCategory.class, closure);
    } catch (RuntimeException runtimeException) {
        StringBuilder error = new StringBuilder("GroovyServlet Error: ");
        error.append(" script: '");
        error.append(scriptUri);
        error.append("': ");
        Throwable e = runtimeException.getCause();
        /*
             * Null cause?!
             */
        if (e == null) {
            error.append(" Script processing failed.\n");
            error.append(runtimeException.getMessage());
            if (runtimeException.getStackTrace().length > 0)
                error.append(runtimeException.getStackTrace()[0].toString());
            servletContext.log(error.toString());
            System.err.println(error.toString());
            runtimeException.printStackTrace(System.err);
            response.sendError(HttpServletResponse.SC_INTERNAL_SERVER_ERROR, error.toString());
            return;
        }
        /*
             * Resource not found.
             */
        if (e instanceof ResourceException) {
            error.append(" Script not found, sending 404.");
            servletContext.log(error.toString());
            System.err.println(error.toString());
            response.sendError(HttpServletResponse.SC_NOT_FOUND);
            return;
        }
        /*
             * Other internal error. Perhaps syntax?!
             */
        servletContext.log("An error occurred processing the request", runtimeException);
        error.append(e.getMessage());
        if (e.getStackTrace().length > 0)
            error.append(e.getStackTrace()[0].toString());
        servletContext.log(e.toString());
        System.err.println(e.toString());
        runtimeException.printStackTrace(System.err);
        response.sendError(HttpServletResponse.SC_INTERNAL_SERVER_ERROR, e.toString());
    }
}
Also used : ScriptException(groovy.util.ScriptException) GroovyScriptEngine(groovy.util.GroovyScriptEngine) Closure(groovy.lang.Closure) ResourceException(groovy.util.ResourceException)

Example 2 with GroovyScriptEngine

use of groovy.util.GroovyScriptEngine in project cuba by cuba-platform.

the class AbstractScripting method clearCache.

@Override
public void clearCache() {
    getGroovyClassLoader().clearCache();
    javaClassLoader.clearCache();
    getPool().clear();
    GroovyScriptEngine gse = getGroovyScriptEngine();
    try {
        Field scriptCacheField = gse.getClass().getDeclaredField("scriptCache");
        scriptCacheField.setAccessible(true);
        Map scriptCacheMap = (Map) scriptCacheField.get(gse);
        scriptCacheMap.clear();
    } catch (NoSuchFieldException | IllegalAccessException e) {
    // ignore the exception
    }
}
Also used : Field(java.lang.reflect.Field) GroovyScriptEngine(groovy.util.GroovyScriptEngine)

Example 3 with GroovyScriptEngine

use of groovy.util.GroovyScriptEngine in project sponge by softelnet.

the class GroovyKnowledgeBaseInterpreter method reloadScript.

public Script reloadScript(String scriptName) {
    try {
        invalidateCache();
        GroovyScriptEngine groovy = new GroovyScriptEngine(createClasspath(getEngineOperations().getEngine()).toArray(new String[0]), shell.getClassLoader());
        Script script = groovy.createScript(scriptName, binding);
        script.run();
        return script;
    } catch (IOException | ResourceException | ScriptException e) {
        throw SpongeUtils.wrapException(e);
    }
}
Also used : Script(groovy.lang.Script) KnowledgeBaseScript(org.openksavi.sponge.kb.KnowledgeBaseScript) ScriptException(groovy.util.ScriptException) GroovyScriptEngine(groovy.util.GroovyScriptEngine) ResourceException(groovy.util.ResourceException) IOException(java.io.IOException)

Example 4 with GroovyScriptEngine

use of groovy.util.GroovyScriptEngine in project gitblit by gitblit.

the class GroovyScriptTest method test.

private void test(String script, MockGitblit gitblit, MockLogger logger, MockClientLogger clientLogger, List<ReceiveCommand> commands, RepositoryModel repository) throws Exception {
    UserModel user = new UserModel("mock");
    String gitblitUrl = GitBlitSuite.url;
    File groovyDir = repositories().getHooksFolder();
    GroovyScriptEngine gse = new GroovyScriptEngine(groovyDir.getAbsolutePath());
    Binding binding = new Binding();
    binding.setVariable("gitblit", gitblit);
    binding.setVariable("repository", repository);
    binding.setVariable("user", user);
    binding.setVariable("commands", commands);
    binding.setVariable("url", gitblitUrl);
    binding.setVariable("logger", logger);
    binding.setVariable("clientLogger", clientLogger);
    Object result = gse.run(script, binding);
    if (result instanceof Boolean) {
        if (!((Boolean) result)) {
            throw new GitBlitException(MessageFormat.format("Groovy script {0} has failed!  Hook scripts aborted.", script));
        }
    }
}
Also used : UserModel(com.gitblit.models.UserModel) Binding(groovy.lang.Binding) GroovyScriptEngine(groovy.util.GroovyScriptEngine) GitBlitException(com.gitblit.GitBlitException) AtomicBoolean(java.util.concurrent.atomic.AtomicBoolean) File(java.io.File)

Example 5 with GroovyScriptEngine

use of groovy.util.GroovyScriptEngine in project groovy-core by groovy.

the class GroovyServlet method service.

/**
 * Handle web requests to the GroovyServlet
 */
public void service(HttpServletRequest request, HttpServletResponse response) throws IOException {
    // Get the script path from the request - include aware (GROOVY-815)
    final String scriptUri = getScriptUri(request);
    // Set it to HTML by default
    response.setContentType("text/html; charset=" + encoding);
    // Set up the script context
    final ServletBinding binding = new ServletBinding(request, response, servletContext);
    setVariables(binding);
    // Run the script
    try {
        Closure closure = new Closure(gse) {

            public Object call() {
                try {
                    return ((GroovyScriptEngine) getDelegate()).run(scriptUri, binding);
                } catch (ResourceException e) {
                    throw new RuntimeException(e);
                } catch (ScriptException e) {
                    throw new RuntimeException(e);
                }
            }
        };
        GroovyCategorySupport.use(ServletCategory.class, closure);
    } catch (RuntimeException runtimeException) {
        StringBuilder error = new StringBuilder("GroovyServlet Error: ");
        error.append(" script: '");
        error.append(scriptUri);
        error.append("': ");
        Throwable e = runtimeException.getCause();
        /*
             * Null cause?!
             */
        if (e == null) {
            error.append(" Script processing failed.\n");
            error.append(runtimeException.getMessage());
            if (runtimeException.getStackTrace().length > 0)
                error.append(runtimeException.getStackTrace()[0].toString());
            servletContext.log(error.toString());
            System.err.println(error.toString());
            runtimeException.printStackTrace(System.err);
            response.sendError(HttpServletResponse.SC_INTERNAL_SERVER_ERROR, error.toString());
            return;
        }
        /*
             * Resource not found.
             */
        if (e instanceof ResourceException) {
            error.append(" Script not found, sending 404.");
            servletContext.log(error.toString());
            System.err.println(error.toString());
            response.sendError(HttpServletResponse.SC_NOT_FOUND);
            return;
        }
        /*
             * Other internal error. Perhaps syntax?!
             */
        servletContext.log("An error occurred processing the request", runtimeException);
        error.append(e.getMessage());
        if (e.getStackTrace().length > 0)
            error.append(e.getStackTrace()[0].toString());
        servletContext.log(e.toString());
        System.err.println(e.toString());
        runtimeException.printStackTrace(System.err);
        response.sendError(HttpServletResponse.SC_INTERNAL_SERVER_ERROR, e.toString());
    }
}
Also used : ScriptException(groovy.util.ScriptException) GroovyScriptEngine(groovy.util.GroovyScriptEngine) Closure(groovy.lang.Closure) ResourceException(groovy.util.ResourceException)

Aggregations

GroovyScriptEngine (groovy.util.GroovyScriptEngine)11 Binding (groovy.lang.Binding)5 File (java.io.File)5 GroovyShell (groovy.lang.GroovyShell)3 ResourceException (groovy.util.ResourceException)3 ScriptException (groovy.util.ScriptException)3 Closure (groovy.lang.Closure)2 GitBlitException (com.gitblit.GitBlitException)1 UserModel (com.gitblit.models.UserModel)1 Script (groovy.lang.Script)1 IOException (java.io.IOException)1 Field (java.lang.reflect.Field)1 AtomicBoolean (java.util.concurrent.atomic.AtomicBoolean)1 Test (org.junit.Test)1 KnowledgeBaseScript (org.openksavi.sponge.kb.KnowledgeBaseScript)1