Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

run multiple scripts in the same GroovyScriptEngine #782

Closed
wants to merge 3 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -12,79 +12,106 @@ import java.util.logging.Logger
* Runs provided DSL scripts via an external {@link JobManagement}.
*/
class DslScriptLoader {

private static final Logger LOGGER = Logger.getLogger(DslScriptLoader.name)
private static final Comparator<? super Item> ITEM_COMPARATOR = new ItemProcessingOrderComparator()

private static GeneratedItems runDslEngineForParent(ScriptRequest scriptRequest,
JobManagement jobManagement) throws IOException {
PrintStream logger = jobManagement.outputStream
private final JobManagement jobManagement
private final PrintStream logger

DslScriptLoader(JobManagement jobManagement) {
this.jobManagement = jobManagement
this.logger = jobManagement.outputStream
}

GeneratedItems runScripts(Collection<ScriptRequest> scriptRequests) throws IOException {
ClassLoader parentClassLoader = DslScriptLoader.classLoader
CompilerConfiguration config = createCompilerConfiguration(jobManagement)

// Otherwise baseScript won't take effect
GroovyClassLoader groovyClassLoader = new GroovyClassLoader(parentClassLoader, config)

try {
GroovyScriptEngine engine = new GroovyScriptEngine(scriptRequest.urlRoots, groovyClassLoader)
try {
engine.config = config

Binding binding = createBinding(jobManagement)

JobParent jobParent
try {
Script script
if (scriptRequest.body != null) {
logger.println('Processing provided DSL script')
Class cls = engine.groovyClassLoader.parseClass(scriptRequest.body, 'script')
script = InvokerHelper.createScript(cls, binding)
} else {
logger.println("Processing DSL script ${scriptRequest.location}")
if (!isValidScriptName(scriptRequest.location)) {
throw new DslException(
"invalid script name '${scriptRequest.location}; script names may only contain " +
'letters, digits and underscores, but may not start with a digit'
)
}
checkCollidingScriptName(scriptRequest.location, engine.groovyClassLoader, logger)
script = engine.createScript(scriptRequest.location, binding)
}
assert script instanceof JobParent

jobParent = (JobParent) script
jobParent.setJm(jobManagement)

binding.setVariable('jobFactory', jobParent)

script.run()
} catch (CompilationFailedException e) {
throw new DslException(e.message, e)
} catch (GroovyRuntimeException e) {
throw new DslScriptException(e.message, e)
} catch (ResourceException e) {
throw new IOException('Unable to run script', e)
} catch (ScriptException e) {
throw new IOException('Unable to run script', e)
runScriptsWithClassLoader(scriptRequests, groovyClassLoader, config)
} finally {
if (groovyClassLoader instanceof Closeable) {
((Closeable) groovyClassLoader).close()
}
}
}

private GeneratedItems runScriptsWithClassLoader(Collection<ScriptRequest> scriptRequests,
GroovyClassLoader groovyClassLoader,
CompilerConfiguration config) {
GeneratedItems generatedItems = new GeneratedItems()
Map<String, GroovyScriptEngine> engineCache = [:]

try {
scriptRequests.each { ScriptRequest scriptRequest ->
String key = scriptRequest.urlRoots*.toString().sort().join(',')

GroovyScriptEngine engine = engineCache[key]
if (!engine) {
engine = new GroovyScriptEngine(scriptRequest.urlRoots, groovyClassLoader)
engine.config = config
engineCache[key] = engine
}

GeneratedItems generatedItems = new GeneratedItems()
generatedItems.configFiles = extractGeneratedConfigFiles(jobParent, scriptRequest.ignoreExisting)
generatedItems.jobs = extractGeneratedJobs(jobParent, scriptRequest.ignoreExisting)
generatedItems.views = extractGeneratedViews(jobParent, scriptRequest.ignoreExisting)
generatedItems.userContents = extractGeneratedUserContents(jobParent, scriptRequest.ignoreExisting)
JobParent jobParent = runScript(scriptRequest, engine)

scheduleJobsToRun(jobParent.queueToBuild, jobManagement)
boolean ignoreExisting = scriptRequest.ignoreExisting
generatedItems.configFiles.addAll(extractGeneratedConfigFiles(jobParent, ignoreExisting))
generatedItems.jobs.addAll(extractGeneratedJobs(jobParent, ignoreExisting))
generatedItems.views.addAll(extractGeneratedViews(jobParent, ignoreExisting))
generatedItems.userContents.addAll(extractGeneratedUserContents(jobParent, ignoreExisting))

return generatedItems
} finally {
if (engine.groovyClassLoader instanceof Closeable) {
scheduleJobsToRun(jobParent.queueToBuild)
}
} finally {
engineCache.values().each { GroovyScriptEngine engine ->
if (engine?.groovyClassLoader instanceof Closeable) {
((Closeable) engine.groovyClassLoader).close()
}
}
} finally {
if (groovyClassLoader instanceof Closeable) {
((Closeable) groovyClassLoader).close()
}

generatedItems
}

private JobParent runScript(ScriptRequest scriptRequest, GroovyScriptEngine engine) {
LOGGER.log(Level.FINE, String.format("Request for ${scriptRequest.location}"))

Binding binding = createBinding()
try {
Script script
if (scriptRequest.body != null) {
logger.println('Processing provided DSL script')
Class cls = engine.groovyClassLoader.parseClass(scriptRequest.body, 'script')
script = InvokerHelper.createScript(cls, binding)
} else {
logger.println("Processing DSL script ${scriptRequest.location}")
checkValidScriptName(scriptRequest.location)
checkCollidingScriptName(scriptRequest.location, engine.groovyClassLoader, logger)
script = engine.createScript(scriptRequest.location, binding)
}
assert script instanceof JobParent

JobParent jobParent = (JobParent) script
jobParent.setJm(jobManagement)

binding.setVariable('jobFactory', jobParent)

script.run()

return jobParent
} catch (CompilationFailedException e) {
throw new DslException(e.message, e)
} catch (GroovyRuntimeException e) {
throw new DslScriptException(e.message, e)
} catch (ResourceException e) {
throw new IOException('Unable to run script', e)
} catch (ScriptException e) {
throw new IOException('Unable to run script', e)
}
}

Expand All @@ -101,6 +128,15 @@ class DslScriptLoader {
true
}

private static void checkValidScriptName(String scriptName) {
if (!isValidScriptName(scriptName)) {
throw new DslException(
"invalid script name '${scriptName}; script names may only contain " +
'letters, digits and underscores, but may not start with a digit'
)
}
}

private static void checkCollidingScriptName(String scriptFile, ClassLoader classLoader, PrintStream logger) {
String scriptName = getScriptName(scriptFile)
Package[] packages = new SnitchingClassLoader(classLoader).packages
Expand All @@ -112,22 +148,30 @@ class DslScriptLoader {
}
}

private static String getScriptName(String scriptFile) {
String fileName = new File(scriptFile).name
int idx = fileName.lastIndexOf('.')
idx > -1 ? fileName[0..idx - 1] : fileName
}

/**
* For testing a string directly.
*
* @Deprecated use {@link #runScripts(java.util.Collection)}
*/
@Deprecated
static GeneratedItems runDslEngine(String scriptBody, JobManagement jobManagement) throws IOException {
ScriptRequest scriptRequest = new ScriptRequest(null, scriptBody, new File('.').toURI().toURL())
runDslEngine(scriptRequest, jobManagement)
}

/**
* @Deprecated use {@link #runScripts(java.util.Collection)}
*/
@Deprecated
static GeneratedItems runDslEngine(ScriptRequest scriptRequest, JobManagement jobManagement) throws IOException {
runDslEngineForParent(scriptRequest, jobManagement)
DslScriptLoader loader = new DslScriptLoader(jobManagement)
loader.runScripts([scriptRequest])
}

private static String getScriptName(String scriptFile) {
String fileName = new File(scriptFile).name
int idx = fileName.lastIndexOf('.')
idx > -1 ? fileName[0..idx - 1] : fileName
}

private static Set<GeneratedJob> extractGeneratedJobs(JobParent jobParent,
Expand Down Expand Up @@ -186,7 +230,7 @@ class DslScriptLoader {
}

@SuppressWarnings('CatchException')
static void scheduleJobsToRun(List<String> jobNames, JobManagement jobManagement) {
private void scheduleJobsToRun(List<String> jobNames) {
Map<String, Throwable> exceptions = [:]
jobNames.each { String jobName ->
try {
Expand All @@ -203,7 +247,7 @@ class DslScriptLoader {
}
}

private static Binding createBinding(JobManagement jobManagement) {
private Binding createBinding() {
Binding binding = new Binding()
binding.setVariable('out', jobManagement.outputStream) // Works for println, but not System.out

Expand Down
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
package javaposse.jobdsl.dsl

class GeneratedItems {
Set<GeneratedJob> jobs
Set<GeneratedView> views
Set<GeneratedConfigFile> configFiles
Set<GeneratedUserContent> userContents
Set<GeneratedJob> jobs = []
Set<GeneratedView> views = []
Set<GeneratedConfigFile> configFiles = []
Set<GeneratedUserContent> userContents = []
}
Loading