/*
 * The MIT License (MIT)
 * 
 * Copyright (c) 2014 Ioannis K. Moutsatsos
 * 
 * Permission is hereby granted, free of charge, to any person obtaining a copy
 * of this software and associated documentation files (the "Software"), to deal
 * in the Software without restriction, including without limitation the rights
 * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
 * copies of the Software, and to permit persons to whom the Software is
 * furnished to do so, subject to the following conditions:
 * 
 * The above copyright notice and this permission notice shall be included in all
 * copies or substantial portions of the Software.
 * 
 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
 * SOFTWARE.
 */
package org.biouno.unochoice;

import groovy.lang.GroovyShell;
import hudson.remoting.Callable;

import java.util.Map;
import java.util.Map.Entry;

import org.codehaus.groovy.control.CompilerConfiguration;

/**
 * A callable (Jenkins remoting API) object that executes the script locally (when executed in the master)
 * or remotely. 
 */
public class ScriptCallback implements Callable<Object, Throwable> {

	private static final long serialVersionUID = 4524316203276099968L;
	
	private final String script;
	private Map<String, Object> parameters;

	public ScriptCallback(String script, Map<String, Object> parameters) {
		this.script = script;
		this.parameters = parameters;
	}
	
	public Object call() throws Throwable {
		CompilerConfiguration config = new CompilerConfiguration();
		// TODO: we can add class paths here
		GroovyShell shell = new GroovyShell(config);
		for (Entry<String, Object> parameter : parameters.entrySet()) {
			shell.setVariable(parameter.getKey(), parameter.getValue());
		}
		Object eval = shell.evaluate(script);
		return eval;
	}
	
}

@@ -1178,24 +1178,52 @@
 unochoice;%0A%0A
+import groovy.lang.Binding;%0A
 import groov
@@ -1325,16 +1325,80 @@
 p.Entry;
+%0Aimport java.util.logging.Logger;%0A%0Aimport jenkins.model.Jenkins;
 %0A%0Aimport
@@ -1651,16 +1651,17 @@
 able%3E %7B%0A
+%09
 %0A%09privat
@@ -1723,16 +1723,106 @@
 968L;%0A%09%0A
+%09private static final Logger LOGGER = Logger.getLogger(ScriptCallback.class.getName());%0A%09%0A
 %09private
@@ -2065,151 +2065,337 @@
 %7B%0A%09%09
-CompilerConfiguration config = new CompilerConfiguration();%0A%09%09// TODO: we can add class paths here%0A%09%09GroovyShell shell = new GroovyShell(config
+// we can add class paths here too if needed%0A%09%09ClassLoader cl = null;%0A%09%09try %7B%0A%09%09%09cl = Jenkins.getInstance().getPluginManager().uberClassLoader;%0A%09%09%7D catch (Throwable t) %7B%0A%09%09%09LOGGER.finest(t.getMessage());%0A%09%09%7D%0A%09%09if (cl == null) %7B%0A%09%09%09cl = Thread.currentThread().getContextClassLoader();%0A%09%09%7D%0A%09%09%0A%09%09final Binding context = new Binding(
 );%0A%09
@@ -2462,21 +2462,23 @@
 )) %7B%0A%09%09%09
-shell
+context
 .setVari
@@ -2529,16 +2529,102 @@
 ));%0A%09%09%7D%0A
+%09%09%0A%09%09GroovyShell shell = new GroovyShell(cl, context, CompilerConfiguration.DEFAULT);%0A
 %09%09Object

Fix issue #29 About class loader of "Uno-Choice Dynamic Choice Parameter (Scriptler)" (Version 0.16)