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());
}
}
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
}
}
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);
}
}
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));
}
}
}
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());
}
}
Aggregations