@Nullable public DelegatingScript loadAutoDetectedScript() { DelegatingScript script = loadScriptFromSystemPropertyInducedLocation(configPropertyKey); if (script != null) return script; script = loadScriptFromClassPathLocation(classPathLocation); if (script != null) return script; script = loadScriptFromFileSystemLocation(fileSystemLocation); if (script != null) return script; return null; }
@Nullable private DelegatingScript loadScriptFromSystemPropertyInducedLocation(String propertyKey) { String location = System.getProperty(propertyKey); if (location == null || location.length() == 0) return null; DelegatingScript script = loadScriptFromClassPathLocation(location); if (script != null) return script; script = loadScriptFromFileSystemLocation(location); if (script != null) return script; throw new ConfigurationException("Cannot find configuration script '%s'", location); }
private static RunContext createBottomContext() { File spockUserHome = SpockUserHomeUtil.getSpockUserHome(); DelegatingScript script = new ConfigurationScriptLoader(spockUserHome).loadAutoDetectedScript(); List<Class<?>> classes = new ExtensionClassesLoader().loadClassesFromDefaultLocation(); return new RunContext("default", spockUserHome, script, classes); } }
@Nullable private DelegatingScript loadScriptFromFileSystemLocation(String location) { File file = new File(location); try { if (!file.exists()) return null; } catch (AccessControlException e) { // no permission to check for existence of file (e.g. on Spock Web Console), // so let's just assume it's not there and continue return null; } GroovyShell shell = createShell(); try { return (DelegatingScript) shell.parse(file); } catch (IOException e) { throw new ConfigurationException("Error reading configuration script '%s'", location); } catch (CompilationFailedException e) { throw new ConfigurationException("Error compiling configuration script '%s'", location); } }
@Nullable private DelegatingScript loadScriptFromClassPathLocation(String location) { URL url = this.getClass().getClassLoader().getResource(location); if (url == null) return null; GroovyShell shell = createShell(); GroovyCodeSource codeSource = null; // try block below for compatibility pre/post Groovy 2.4.7 (see GROOVY-8126) try { codeSource = new GroovyCodeSource(url); } catch (Exception e) { throw new ConfigurationException("Error reading configuration script '%s'", location); } try { return (DelegatingScript) shell.parse(codeSource); } catch (CompilationFailedException e) { throw new ConfigurationException("Error compiling configuration script '%s'", location); } }