summaryrefslogtreecommitdiff
path: root/src/main/java
diff options
context:
space:
mode:
authorTrygve Laugstøl <trygvis@inamo.no>2014-05-06 22:30:40 +0200
committerTrygve Laugstøl <trygvis@inamo.no>2014-05-06 22:30:40 +0200
commit7cf971cfe478fc4a170ea17274f44b24eae97e2b (patch)
treea9155d9d732462e2122bbe4d11323a07f442439f /src/main/java
parent7e2d2c074c3f09266dda31c772f4bec61e8d5742 (diff)
downloadactivemq-plugin-7cf971cfe478fc4a170ea17274f44b24eae97e2b.tar.gz
activemq-plugin-7cf971cfe478fc4a170ea17274f44b24eae97e2b.tar.bz2
activemq-plugin-7cf971cfe478fc4a170ea17274f44b24eae97e2b.tar.xz
activemq-plugin-7cf971cfe478fc4a170ea17274f44b24eae97e2b.zip
o Support for triggering builds from MQ messages with an MVEL expression.
Diffstat (limited to 'src/main/java')
-rw-r--r--src/main/java/org/jenkinsci/plugins/activemq/ActiveMqBuildTrigger.java101
-rw-r--r--src/main/java/org/jenkinsci/plugins/activemq/ActiveMqPlugin.java27
2 files changed, 128 insertions, 0 deletions
diff --git a/src/main/java/org/jenkinsci/plugins/activemq/ActiveMqBuildTrigger.java b/src/main/java/org/jenkinsci/plugins/activemq/ActiveMqBuildTrigger.java
new file mode 100644
index 0000000..f3a2a79
--- /dev/null
+++ b/src/main/java/org/jenkinsci/plugins/activemq/ActiveMqBuildTrigger.java
@@ -0,0 +1,101 @@
+package org.jenkinsci.plugins.activemq;
+
+import hudson.Extension;
+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.kohsuke.stapler.QueryParameter;
+import org.mvel2.MVEL;
+import org.slf4j.Logger;
+
+import java.io.Serializable;
+import java.util.Map;
+
+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.slf4j.LoggerFactory.getLogger;
+
+public class ActiveMqBuildTrigger extends Trigger<BuildableItem> {
+
+ private static final Logger log = getLogger(ActiveMqBuildTrigger.class);
+
+ private String mvel;
+
+ private transient Serializable compiledMvel;
+
+ // parameters['jobName'] == 'fast-build'
+ @DataBoundConstructor
+ public ActiveMqBuildTrigger(String mvel) {
+ log.info("ActiveMqBuildTrigger.ActiveMqBuildTrigger");
+ setMvel_(mvel);
+ }
+
+ public String getMvel() {
+ log.info("ActiveMqBuildTrigger.getMvel");
+ return mvel;
+ }
+
+ private void setMvel_(String mvel) {
+ mvel = trimToNull(mvel);
+ if (mvel == null) {
+ this.mvel = null;
+ this.compiledMvel = null;
+ return;
+ }
+
+ try {
+ compiledMvel = compileExpression(mvel);
+ this.mvel = mvel;
+ } catch (Exception e) {
+ log.warn("Unable to compile MVEL", e);
+ }
+ }
+
+// public FormValidation doCheckMvel(@QueryParameter String mvel) {
+// mvel = trimToNull(mvel);
+// if (mvel == null) {
+// return FormValidation.ok();
+// }
+// try {
+// compileExpression(mvel);
+// return FormValidation.ok();
+// } catch (Exception e) {
+// log.info("Unable to compile MVEL: " + mvel, e);
+// return FormValidation.error(e, "Unable to compile MVEL");
+// }
+// }
+
+// public Serializable getCompiledMvel() {
+// return compiledMvel;
+// }
+
+ public boolean matches(BuildRequest req) {
+ if (compiledMvel == null) {
+ return false;
+ }
+
+ Map<String, Map<String, String>> context = singletonMap("parameters", req.parameters);
+ Boolean b = MVEL.executeExpression(compiledMvel, context, Boolean.class);
+
+ return b != null && b;
+ }
+
+ @Extension
+ public static class DescriptorImpl extends TriggerDescriptor {
+
+ @Override
+ public boolean isApplicable(Item item) {
+ return item instanceof BuildableItem;
+ }
+
+ @Override
+ public String getDisplayName() {
+ return "ActiveMQ Trigger";
+ }
+ }
+}
diff --git a/src/main/java/org/jenkinsci/plugins/activemq/ActiveMqPlugin.java b/src/main/java/org/jenkinsci/plugins/activemq/ActiveMqPlugin.java
index c9c277c..cc73898 100644
--- a/src/main/java/org/jenkinsci/plugins/activemq/ActiveMqPlugin.java
+++ b/src/main/java/org/jenkinsci/plugins/activemq/ActiveMqPlugin.java
@@ -2,9 +2,12 @@ package org.jenkinsci.plugins.activemq;
import hudson.Extension;
import hudson.Plugin;
+import hudson.model.AbstractProject;
import hudson.model.Build;
+import hudson.model.Cause;
import hudson.model.TaskListener;
import jenkins.model.GlobalConfiguration;
+import jenkins.model.Jenkins;
import org.slf4j.Logger;
import javax.annotation.Nonnull;
@@ -88,5 +91,29 @@ public class ActiveMqPlugin extends Plugin implements ActiveMqClient.BuildReques
public void onBuildRequest(ActiveMqClient.BuildRequest req) {
log.info("ActiveMqPlugin.onBuildRequest");
log.info(req.parameters.toString());
+
+ for (AbstractProject project : Jenkins.getInstance().getAllItems(AbstractProject.class)) {
+ log.info("Checking project " + project.getDisplayName());
+ ActiveMqBuildTrigger trigger = (ActiveMqBuildTrigger) project.getTrigger(ActiveMqBuildTrigger.class);
+
+ if (trigger == null) {
+ log.debug("No trigger configured");
+ continue;
+ }
+
+ if (trigger.matches(req)) {
+ log.debug("MVEL matched!");
+ project.scheduleBuild(new ActiveMqCause());
+ } else {
+ log.debug("MVEL did not match");
+ }
+ }
+ }
+
+ public static class ActiveMqCause extends Cause {
+ @Override
+ public String getShortDescription() {
+ return "ActiveMQ cause";
+ }
}
}