package org.jenkinsci.plugins.activemq; import hudson.Extension; import hudson.model.AbstractProject; import hudson.model.BuildableItem; import hudson.model.Item; import hudson.triggers.Trigger; import hudson.triggers.TriggerDescriptor; import org.kohsuke.stapler.DataBoundConstructor; 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 { private static final Logger log = getLogger(ActiveMqBuildTrigger.class); private String mvel; private transient Serializable compiledMvel; @DataBoundConstructor public ActiveMqBuildTrigger(String mvel) { log.info("ActiveMqBuildTrigger.ActiveMqBuildTrigger"); } @Override public void start(AbstractProject project, boolean newInstance) { super.start(project, newInstance); log.info("ActiveMqBuildTrigger.start"); setMvel_(mvel); } public String getMvel() { log.info("ActiveMqBuildTrigger.getMvel"); return mvel; } private void setMvel_(String mvel) { mvel = trimToNull(mvel); if (mvel == null) { log.info("ActiveMqBuildTrigger.setMvel_: mvel is null"); this.mvel = null; this.compiledMvel = null; return; } try { compiledMvel = compileExpression(mvel); this.mvel = mvel; log.info("ActiveMqBuildTrigger.setMvel_: 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> 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"; } } }