/** * When loading additional scripts, we need to remember its source text so that * when we come back from persistence we can load them again. */ @Override public Script parse(GroovyCodeSource codeSource) throws CompilationFailedException { Script s = doParse(codeSource); if (execution!=null) { execution.loadedScripts.put(s.getClass().getSimpleName(), codeSource.getScriptText()); execution.saveExecutionIfDurable(); } prepareScript(s); return s; }
/** * Finds the expected next loaded script name, like {@code Script1}. * @param path a file path being loaded (currently ignored) */ @Restricted(NoExternalUse.class) public String getNextScriptName(String path) { return shell.generateScriptName().replaceFirst("[.]groovy$", ""); }
/** * Used internally to reload the script back when coming back from the persisted state * (therefore we don't want to record this.) */ /*package*/ Script reparse(String className, String text) throws CompilationFailedException { return doParse(new GroovyCodeSource(text,className,DEFAULT_CODE_BASE)); }
private CpsScript parseScript() throws IOException { // classloader hierarchy. See doc/classloader.md trusted = new CpsGroovyShellFactory(this).forTrusted().build(); shell = new CpsGroovyShellFactory(this).withParent(trusted).build(); CpsScript s = (CpsScript) shell.reparse("WorkflowScript",script); for (Entry<String, String> e : loadedScripts.entrySet()) { shell.reparse(e.getKey(), e.getValue()); } s.execution = this; if (false) { System.out.println("scriptName="+s.getClass().getName()); System.out.println(Arrays.asList(s.getClass().getInterfaces())); System.out.println(Arrays.asList(s.getClass().getDeclaredFields())); System.out.println(Arrays.asList(s.getClass().getDeclaredMethods())); } return s; }
public CpsGroovyShell build() { ClassLoader parent = this.parent; if (parent==null) parent = makeClassLoader(); CpsGroovyShell shell = new CpsGroovyShell(parent, execution, makeConfig()); for (GroovyShellDecorator d : decorators) { d.configureShell(execution,shell); } return shell; } }
@RequirePOST public JSON doCheckScriptCompile(@AncestorInPath Item job, @QueryParameter String value) { if (!job.hasPermission(Job.CONFIGURE)) { return CpsFlowDefinitionValidator.CheckStatus.SUCCESS.asJSON(); } try { CpsGroovyShell trusted = new CpsGroovyShellFactory(null).forTrusted().build(); new CpsGroovyShellFactory(null).withParent(trusted).withSandbox(true).build().getClassLoader().parseClass(value); } catch (CompilationFailedException x) { return JSONArray.fromObject(CpsFlowDefinitionValidator.toCheckStatus(x).toArray()); } return CpsFlowDefinitionValidator.CheckStatus.SUCCESS.asJSON(); // Approval requirements are managed by regular stapler form validation (via doCheckScript) }