From 3d2a1964c4f503f19c7b4edbd2c0dbf3fdfb815e Mon Sep 17 00:00:00 2001 From: Trygve Laugstøl Date: Sat, 10 May 2014 21:22:27 +0200 Subject: o Using JavaScript instead of MVEL. --- .../plugins/activemq/ActiveMqBuildTrigger.java | 124 +++++++++++++-------- .../jenkinsci/plugins/activemq/ActiveMqPlugin.java | 21 +++- 2 files changed, 94 insertions(+), 51 deletions(-) (limited to 'src/main/java/org') diff --git a/src/main/java/org/jenkinsci/plugins/activemq/ActiveMqBuildTrigger.java b/src/main/java/org/jenkinsci/plugins/activemq/ActiveMqBuildTrigger.java index 0beb6eb..08a6faf 100644 --- a/src/main/java/org/jenkinsci/plugins/activemq/ActiveMqBuildTrigger.java +++ b/src/main/java/org/jenkinsci/plugins/activemq/ActiveMqBuildTrigger.java @@ -7,105 +7,100 @@ import hudson.model.Item; import hudson.triggers.Trigger; import hudson.triggers.TriggerDescriptor; import hudson.util.FormValidation; +import jenkins.model.Jenkins; import org.kohsuke.stapler.DataBoundConstructor; import org.kohsuke.stapler.QueryParameter; import org.slf4j.Logger; -import java.io.Serializable; +import javax.script.Invocable; +import javax.script.ScriptEngine; +import javax.script.ScriptException; import java.util.Map; import java.util.TreeMap; +import static java.lang.Boolean.TRUE; 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 { private static final Logger log = getLogger(ActiveMqBuildTrigger.class); - private String mvel; + private String expression; - private transient Serializable compiledMvel; + private transient ActiveMqPlugin plugin; + + private transient ScriptEngine engine; @DataBoundConstructor - public ActiveMqBuildTrigger(String mvel) { + public ActiveMqBuildTrigger(String expression) { log.info("ActiveMqBuildTrigger.ActiveMqBuildTrigger"); - this.mvel = mvel; + log.info("expression = " + expression); + this.expression = expression; } @Override public void start(AbstractProject project, boolean newInstance) { super.start(project, newInstance); + + plugin = Jenkins.getInstance().getPlugin(ActiveMqPlugin.class); + log.info("ActiveMqBuildTrigger.start"); - compileMvel(); + compileExpression(); } - public String getMvel() { - log.info("ActiveMqBuildTrigger.getMvel"); - return mvel; + public String getExpression() { + log.info("ActiveMqBuildTrigger.getExpression"); + return expression; } - public void setMvel(String mvel) { - this.mvel = mvel; + public void setExpression(String expression) { + this.expression = expression; } - private void setMvel_(String mvel) { - mvel = trimToNull(mvel); - if (mvel == null) { - log.info("ActiveMqBuildTrigger.setMvel_: mvel is null"); - this.mvel = null; - this.compiledMvel = null; + private void compileExpression() { + log.info("ActiveMqBuildTrigger.compileExpression: expression={}", expression); + + if (expression == null) { return; } - compileMvel(); - } - - private void compileMvel() { try { - compiledMvel = compileExpression(mvel); - log.info("ActiveMqBuildTrigger.compileMvel: mvel={}", mvel); + engine = plugin.createEngine(); + + engine.eval(expression); + + if (checkEngine(engine)) { + log.warn("Invalid expression: {}", expression); + } } catch (Exception e) { - log.warn("Unable to compile MVEL", e); + log.warn("Unable to compile expression", e); } } -// public FormValidation doCheckMvel(@QueryParameter String mvel) { -// log.info("ActiveMqBuildTrigger.doCheckMvel"); -// 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) { + if (engine == null) { return false; } Map parameters = new TreeMap(req.parameters); Map> context = singletonMap("parameters", parameters); try { - Boolean b = executeExpression(compiledMvel, context, Boolean.class); + Object o = ((Invocable) engine).invokeFunction("match", parameters); + + log.info("o = " + o); + + Boolean b = null; + if (o instanceof Boolean) { + b = (Boolean) o; + } return b != null && b; } catch (Exception e) { - log.warn("Could not evaluate expression"); - log.warn("Expression = {}", mvel); + log.warn("Could not evaluate expression:"); + log.warn(expression); log.warn("Context:"); for (Map.Entry> entry : context.entrySet()) { log.warn("{} = {}", entry.getKey(), entry.getValue()); @@ -127,5 +122,36 @@ public class ActiveMqBuildTrigger extends Trigger { public String getDisplayName() { return "ActiveMQ Trigger"; } + + public FormValidation doCheckExpression(@QueryParameter String expression) { + log.info("ActiveMqBuildTrigger.doCheckExpression"); + expression = trimToNull(expression); + if (expression == null) { + return FormValidation.ok(); + } + try { + ActiveMqPlugin plugin = Jenkins.getInstance().getPlugin(ActiveMqPlugin.class); + + ScriptEngine engine = plugin.createEngine(); + + engine.eval(expression); + + if (checkEngine(engine)) { + return FormValidation.ok(); + } + + return FormValidation.warning("The expression must define a function named 'match'"); + } catch (ScriptException e) { + return FormValidation.error("Could not evaluate expression: " + e.getMessage()); + } catch (Exception e) { + return FormValidation.error(e, "Could not evaluate expression"); + } + } + } + + private static boolean checkEngine(ScriptEngine engine) throws ScriptException { + Object o = engine.eval("typeof match === 'function'"); + + return TRUE.equals(o); } } diff --git a/src/main/java/org/jenkinsci/plugins/activemq/ActiveMqPlugin.java b/src/main/java/org/jenkinsci/plugins/activemq/ActiveMqPlugin.java index b82443a..cf1c2e9 100644 --- a/src/main/java/org/jenkinsci/plugins/activemq/ActiveMqPlugin.java +++ b/src/main/java/org/jenkinsci/plugins/activemq/ActiveMqPlugin.java @@ -12,6 +12,9 @@ import org.slf4j.Logger; import javax.annotation.Nonnull; import javax.jms.JMSException; +import javax.script.ScriptEngine; +import javax.script.ScriptEngineFactory; +import javax.script.ScriptEngineManager; import java.net.URISyntaxException; import java.util.Map; @@ -20,14 +23,17 @@ import static org.slf4j.LoggerFactory.getLogger; @Extension public class ActiveMqPlugin extends Plugin implements ActiveMqClient.BuildRequestListener /*implements ReconfigurableDescribable*/ { -// public static final String DISPLAY_NAME = "ActiveMQ Plugin Display Name"; - private static final Logger log = getLogger(ActiveMqPlugin.class); private transient ActiveMqClient client; + private transient ScriptEngineManager scriptEngineManager; @Override public void start() throws Exception { + // Use null as the class loader to get only the script engined defined by the JDK. With out it only the + // Groovy engine was exposed. + scriptEngineManager = new ScriptEngineManager(null); + reconfigure(); } @@ -116,4 +122,15 @@ public class ActiveMqPlugin extends Plugin implements ActiveMqClient.BuildReques return "ActiveMQ cause"; } } + + /** + * Used by other parts of the plugin. + */ + public ScriptEngine createEngine() { + log.info("ActiveMqPlugin.createEngine"); + for (ScriptEngineFactory scriptEngineFactory : scriptEngineManager.getEngineFactories()) { + log.info("scriptEngineFactory = " + scriptEngineFactory); + } + return scriptEngineManager.getEngineByName("JavaScript"); + } } -- cgit v1.2.3