From e8b94392b58cdd40a28367ed8e5e540640a1d3b9 Mon Sep 17 00:00:00 2001 From: Trygve Laugstøl Date: Sat, 10 May 2014 20:30:50 +0200 Subject: o Better error messages. --- .../plugins/activemq/ActiveMqBuildTrigger.java | 38 ++++++++++++++++++---- .../jenkinsci/plugins/activemq/ActiveMqPlugin.java | 4 +-- 2 files changed, 33 insertions(+), 9 deletions(-) (limited to 'src/main/java') diff --git a/src/main/java/org/jenkinsci/plugins/activemq/ActiveMqBuildTrigger.java b/src/main/java/org/jenkinsci/plugins/activemq/ActiveMqBuildTrigger.java index 9f9c6a6..0beb6eb 100644 --- a/src/main/java/org/jenkinsci/plugins/activemq/ActiveMqBuildTrigger.java +++ b/src/main/java/org/jenkinsci/plugins/activemq/ActiveMqBuildTrigger.java @@ -6,17 +6,20 @@ import hudson.model.BuildableItem; import hudson.model.Item; import hudson.triggers.Trigger; import hudson.triggers.TriggerDescriptor; +import hudson.util.FormValidation; import org.kohsuke.stapler.DataBoundConstructor; -import org.mvel2.MVEL; +import org.kohsuke.stapler.QueryParameter; import org.slf4j.Logger; import java.io.Serializable; import java.util.Map; +import java.util.TreeMap; import static java.util.Collections.singletonMap; import static org.apache.commons.lang.StringUtils.trimToNull; import static org.jenkinsci.plugins.activemq.ActiveMqClient.BuildRequest; import static org.mvel2.MVEL.compileExpression; +import static org.mvel2.MVEL.executeExpression; import static org.slf4j.LoggerFactory.getLogger; public class ActiveMqBuildTrigger extends Trigger { @@ -30,13 +33,14 @@ public class ActiveMqBuildTrigger extends Trigger { @DataBoundConstructor public ActiveMqBuildTrigger(String mvel) { log.info("ActiveMqBuildTrigger.ActiveMqBuildTrigger"); + this.mvel = mvel; } @Override public void start(AbstractProject project, boolean newInstance) { super.start(project, newInstance); log.info("ActiveMqBuildTrigger.start"); - setMvel_(mvel); + compileMvel(); } public String getMvel() { @@ -44,6 +48,10 @@ public class ActiveMqBuildTrigger extends Trigger { return mvel; } + public void setMvel(String mvel) { + this.mvel = mvel; + } + private void setMvel_(String mvel) { mvel = trimToNull(mvel); if (mvel == null) { @@ -53,16 +61,20 @@ public class ActiveMqBuildTrigger extends Trigger { return; } + compileMvel(); + } + + private void compileMvel() { try { compiledMvel = compileExpression(mvel); - this.mvel = mvel; - log.info("ActiveMqBuildTrigger.setMvel_: mvel={}", mvel); + log.info("ActiveMqBuildTrigger.compileMvel: mvel={}", mvel); } catch (Exception e) { log.warn("Unable to compile MVEL", e); } } // public FormValidation doCheckMvel(@QueryParameter String mvel) { +// log.info("ActiveMqBuildTrigger.doCheckMvel"); // mvel = trimToNull(mvel); // if (mvel == null) { // return FormValidation.ok(); @@ -85,10 +97,22 @@ public class ActiveMqBuildTrigger extends Trigger { return false; } - Map> context = singletonMap("parameters", req.parameters); - Boolean b = MVEL.executeExpression(compiledMvel, context, Boolean.class); + Map parameters = new TreeMap(req.parameters); + Map> context = singletonMap("parameters", parameters); + try { + Boolean b = executeExpression(compiledMvel, context, Boolean.class); - return b != null && b; + return b != null && b; + } catch (Exception e) { + log.warn("Could not evaluate expression"); + log.warn("Expression = {}", mvel); + log.warn("Context:"); + for (Map.Entry> entry : context.entrySet()) { + log.warn("{} = {}", entry.getKey(), entry.getValue()); + } + log.warn("Exception", e); + return false; + } } @Extension diff --git a/src/main/java/org/jenkinsci/plugins/activemq/ActiveMqPlugin.java b/src/main/java/org/jenkinsci/plugins/activemq/ActiveMqPlugin.java index cc73898..b82443a 100644 --- a/src/main/java/org/jenkinsci/plugins/activemq/ActiveMqPlugin.java +++ b/src/main/java/org/jenkinsci/plugins/activemq/ActiveMqPlugin.java @@ -37,9 +37,9 @@ public class ActiveMqPlugin extends Plugin implements ActiveMqClient.BuildReques } public void onCompleted(Build build, @Nonnull TaskListener listener) { - log.info("ActiveMqPlugin.onCompleted"); - log.info("build variables"); + log.info("ActiveMqPlugin.onCompleted: listener={}", listener); @SuppressWarnings("unchecked") Map buildVariables = build.getBuildVariables(); + log.info("Build variables"); for (Map.Entry e : buildVariables.entrySet()) { log.info(e.getKey() + " = " + e.getValue()); } -- cgit v1.2.3