Welcome to the CreativeIT Wiki! » XWiki Space » GroovyConsoleExecutor

GroovyConsoleExecutor

Last modified by Holger Dick on 2009/05/19 15:08
<% import org.codehaus.groovy.control.MultipleCompilationErrorsException if(request.xpage == "plain" && xwiki.hasAccessLevel("programming", doc.fullName)) { def scriptText = request.getParameter("script") ?: "'The received script was null.'" def output = new StringWriter() def binding = new Binding([out: new PrintWriter(output), xwiki:xwiki, context:context, request:request, util:util]) def stacktrace = new StringWriter() def errWriter = new PrintWriter(stacktrace) def result = "" try { result = new GroovyShell(binding).evaluate(scriptText) } catch (MultipleCompilationErrorsException e) { stacktrace.append(e.message - 'startup failed, Script1.groovy: ') } catch (Throwable t) { sanitizeStacktrace(t) def cause = t while (cause = cause?.cause) { sanitizeStacktrace(cause) } t.printStackTrace(errWriter) } response.contentType = "application/json" out.println "{ executionResult: \"" + escape(result) + "\", " + "outputText: \"" + escape(output) + "\", " + "stacktraceText: \"" + escape(stacktrace) + "\"}" } else { println "This is a groovy executor" } def escape(object) { object.toString().replaceAll(/\n/, /\\\n/).replaceAll(/"/, /\\"/) } def sanitizeStacktrace(t) { def filtered = [ 'com.google.', 'org.mortbay.', 'java.', 'javax.', 'sun.', 'groovy.', 'org.codehaus.groovy.', 'com.xpn.xwiki.render', 'org.apache.velocity', 'org.xwiki.velocity', 'com.xpn.xwiki', 'org.apache.struts', 'Script1' ] def trace = t.getStackTrace() def newTrace = [] trace.each { stackTraceElement -> if (filtered.every { !stackTraceElement.className.startsWith(it) }) { newTrace << stackTraceElement } } def clean = newTrace.toArray(newTrace as StackTraceElement[]) t.stackTrace = clean } %>
Tags:
Created by Administrator on 2009/05/07 10:28

This wiki is licensed under a Creative Commons 2.0 license
XWiki Enterprise 2.7.1.${buildNumber} - Documentation