Skip to content
Permalink
Browse files

Merge pull request #67 from gcummings/JENKINS-17910

JENKINS-17910 - groovy.lang.MissingMethodException
  • Loading branch information
slide committed May 22, 2013
2 parents 9aae5d2 + 8677931 commit af74632c11fdb241fb003e2078f502f4ec89f019
@@ -0,0 +1,76 @@
package hudson.plugins.emailext.plugins.content;

import java.io.IOException;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.jenkinsci.plugins.tokenmacro.MacroEvaluationException;
import org.jenkinsci.plugins.tokenmacro.TokenMacro;

import com.google.common.collect.ArrayListMultimap;
import com.google.common.collect.ListMultimap;

import groovy.lang.Script;
import hudson.model.AbstractBuild;
import hudson.model.TaskListener;
import hudson.plugins.emailext.plugins.ContentBuilder;

public abstract class EmailExtScript extends Script {

private void populateArgs(Object args, Map<String, String> map, ListMultimap<String, String> multiMap) {
if(args instanceof Object[]) {
Object[] argArray = (Object[])args;
if(argArray.length > 0) {
Map<String, Object> argMap = (Map<String, Object>)argArray[0];
for(Map.Entry<String, Object> entry : argMap.entrySet()) {
String value = entry.getValue().toString();
if(entry.getValue() instanceof List) {
List valueList = (List)entry.getValue();
for(Object v : valueList) {
multiMap.put(entry.getKey(), v.toString());
}
value = valueList.get(valueList.size() - 1).toString();
} else {
multiMap.put(entry.getKey(), value);
}
map.put(entry.getKey(), value);
}
}
}
}

public Object methodMissing(String name, Object args)
throws MacroEvaluationException, IOException, InterruptedException {

TokenMacro macro = null;
for(TokenMacro m : TokenMacro.all()) {
if(m.acceptsMacroName(name)) {
macro = m;
break;
}
}

if(macro == null) {
for(TokenMacro m : ContentBuilder.getPrivateMacros()) {
if(m.acceptsMacroName(name)) {
macro = m;
break;
}
}
}

if(macro != null) {
Map<String, String> argsMap = new HashMap<String, String>();
ListMultimap<String, String> argsMultimap = ArrayListMultimap.create();
populateArgs(args, argsMap, argsMultimap);

// Get the build and listener from the binding.
AbstractBuild<?, ?> build = (AbstractBuild<?, ?>)this.getBinding().getVariable("build");
TaskListener listener = (TaskListener)this.getBinding().getVariable("listener");

return macro.evaluate(build, listener, name, argsMap, argsMultimap);
}
return String.format("[Could not find content token (check your usage): %s]", name);
}
}
@@ -1,22 +1,15 @@
package hudson.plugins.emailext.plugins.content;

import com.google.common.collect.ArrayListMultimap;
import com.google.common.collect.ListMultimap;
import groovy.lang.Binding;
import groovy.lang.GroovyRuntimeException;
import groovy.lang.GroovyShell;
import groovy.text.SimpleTemplateEngine;
import hudson.model.TaskListener;
import hudson.model.AbstractBuild;
import hudson.model.Hudson;
import hudson.model.TaskListener;
import hudson.plugins.emailext.plugins.EmailToken;
import hudson.plugins.emailext.ExtendedEmailPublisher;
import hudson.plugins.emailext.ScriptSandbox;
import hudson.plugins.emailext.plugins.ContentBuilder;

import jenkins.model.Jenkins;

import org.apache.commons.io.IOUtils;
import hudson.plugins.emailext.plugins.EmailToken;

import java.io.File;
import java.io.FileInputStream;
@@ -26,18 +19,18 @@
import java.io.InputStreamReader;
import java.util.Collections;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.logging.Level;
import java.util.logging.Logger;

import jenkins.model.Jenkins;

import org.apache.commons.io.IOUtils;
import org.apache.commons.lang.StringUtils;
import org.codehaus.groovy.control.CompilerConfiguration;
import org.codehaus.groovy.control.customizers.ImportCustomizer;
import org.codehaus.groovy.runtime.InvokerHelper;
import org.jenkinsci.plugins.tokenmacro.DataBoundTokenMacro;
import org.jenkinsci.plugins.tokenmacro.DataBoundTokenMacro.Parameter;
import org.jenkinsci.plugins.tokenmacro.MacroEvaluationException;
import org.jenkinsci.plugins.tokenmacro.TokenMacro;
import org.kohsuke.groovy.sandbox.SandboxTransformer;

@EmailToken
@@ -122,68 +115,6 @@ private InputStream getFileInputStream(String fileName)
return inputStream;
}


private class MethodMissingHandler {
private AbstractBuild<?, ?> build;
private TaskListener listener;

public MethodMissingHandler(AbstractBuild<?,?> build, TaskListener listener) {
this.build = build;
this.listener = listener;
}

private void populateArgs(Object args, Map<String, String> map, ListMultimap<String, String> multiMap) {
if(args instanceof Object[]) {
Object[] argArray = (Object[])args;
if(argArray.length > 0) {
Map<String, Object> argMap = (Map<String, Object>)argArray[0];
for(Map.Entry<String, Object> entry : argMap.entrySet()) {
String value = entry.getValue().toString();
if(entry.getValue() instanceof List) {
List valueList = (List)entry.getValue();
for(Object v : valueList) {
multiMap.put(entry.getKey(), v.toString());
}
value = valueList.get(valueList.size() - 1).toString();
} else {
multiMap.put(entry.getKey(), value);
}
map.put(entry.getKey(), value);
}
}
}
}

public Object methodMissing(String name, Object args)
throws MacroEvaluationException, IOException, InterruptedException {
TokenMacro macro = null;
for(TokenMacro m : TokenMacro.all()) {
if(m.acceptsMacroName(name)) {
macro = m;
break;
}
}

if(macro == null) {
for(TokenMacro m : ContentBuilder.getPrivateMacros()) {
if(m.acceptsMacroName(name)) {
macro = m;
break;
}
}
}

if(macro != null) {
Map<String, String> argsMap = new HashMap<String, String>();
ListMultimap<String, String> argsMultimap = ArrayListMultimap.create();
populateArgs(args, argsMap, argsMultimap);

return macro.evaluate(build, listener, name, argsMap, argsMultimap);
}
return String.format("[Could not find content token (check your usage): %s]", name);
}
}

/**
* Renders the template using a SimpleTemplateEngine
*
@@ -202,11 +133,8 @@ private String renderTemplate(AbstractBuild<?, ?> build, TaskListener listener,
binding.put("rooturl", ExtendedEmailPublisher.DESCRIPTOR.getHudsonUrl());
binding.put("project", build.getParent());

MethodMissingHandler handler = new MethodMissingHandler(build, listener);

// we add the binding to the SimpleTemplateEngine instead of the shell
GroovyShell shell = createEngine(Collections.singletonMap("methodMissing", (Object)InvokerHelper.getMethodPointer(handler, "methodMissing")));
shell.evaluate("Script.metaClass.methodMissing = methodMissing");
GroovyShell shell = createEngine(Collections.EMPTY_MAP);
SimpleTemplateEngine engine = new SimpleTemplateEngine(shell);
return engine.createTemplate(new InputStreamReader(templateStream)).make(binding).toString();
}
@@ -250,6 +178,7 @@ private GroovyShell createEngine(Map<String, Object> variables)
ClassLoader cl = Jenkins.getInstance().getPluginManager().uberClassLoader;
ScriptSandbox sandbox = null;
CompilerConfiguration cc = new CompilerConfiguration();
cc.setScriptBaseClass(EmailExtScript.class.getCanonicalName());
cc.addCompilationCustomizers(new ImportCustomizer().addStarImports(
"jenkins",
"jenkins.model",

0 comments on commit af74632

Please sign in to comment.
You can’t perform that action at this time.