public static void run(OperationContext ctx, URL script) throws OperationException, IOException { String key = script.toExternalForm(); Script cs = cache.get(key); if (cs != null) { cs.eval(ctx); return; } String path = script.getPath(); int p = path.lastIndexOf('.'); if (p == -1) { throw new OperationException("Script files must have an extension: " + script); } String ext = path.substring(p + 1).toLowerCase(); try (InputStream in = script.openStream()) { if ("mvel".equals(ext)) { Serializable c = MVEL.compileExpression(IOUtils.toString(in, UTF_8)); cs = new MvelScript(c); } else if ("groovy".equals(ext)) { cs = new GroovyScript(IOUtils.toString(in, UTF_8)); } else { throw new OperationException( "Unsupported script file: " + script + ". Only MVEL and Groovy scripts are supported"); } cache.put(key, cs); cs.eval(ctx); } }
@OperationMethod public Blob run(Blob blob) throws OperationException, IOException { if (!ctx.getPrincipal().isAdministrator()) { throw new OperationException("Not allowed. You must be administrator to run scripts"); } Object r = null; if (type.equals("mvel")) { r = MvelScript.compile(blob.getString()).eval(ctx); } else if (type.equals("groovy")) { r = new GroovyScript(blob.getString()).eval(ctx); } else { throw new OperationException("Unknown scripting language " + type); } if (r != null) { Blob b = Blobs.createBlob(r.toString()); b.setFilename("result"); return b; } return null; }