summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorTrygve Laugstøl <trygvis@inamo.no>2014-05-10 21:22:27 +0200
committerTrygve Laugstøl <trygvis@inamo.no>2014-05-10 21:22:27 +0200
commit3d2a1964c4f503f19c7b4edbd2c0dbf3fdfb815e (patch)
tree059b3564e4f1235d585c09add664b811dd9221d5
parente8b94392b58cdd40a28367ed8e5e540640a1d3b9 (diff)
downloadactivemq-plugin-3d2a1964c4f503f19c7b4edbd2c0dbf3fdfb815e.tar.gz
activemq-plugin-3d2a1964c4f503f19c7b4edbd2c0dbf3fdfb815e.tar.bz2
activemq-plugin-3d2a1964c4f503f19c7b4edbd2c0dbf3fdfb815e.tar.xz
activemq-plugin-3d2a1964c4f503f19c7b4edbd2c0dbf3fdfb815e.zip
o Using JavaScript instead of MVEL.
-rw-r--r--pom.xml5
-rwxr-xr-xrun-jenkins1
-rw-r--r--src/main/java/org/jenkinsci/plugins/activemq/ActiveMqBuildTrigger.java124
-rw-r--r--src/main/java/org/jenkinsci/plugins/activemq/ActiveMqPlugin.java21
-rw-r--r--src/main/resources/org/jenkinsci/plugins/activemq/ActiveMqBuildTrigger/config.jelly2
5 files changed, 96 insertions, 57 deletions
diff --git a/pom.xml b/pom.xml
index 3cebc1e..094a28b 100644
--- a/pom.xml
+++ b/pom.xml
@@ -27,11 +27,6 @@
<artifactId>activemq-pool</artifactId>
<version>${version.activemq}</version>
</dependency>
- <dependency>
- <groupId>org.mvel</groupId>
- <artifactId>mvel2</artifactId>
- <version>2.1.9.Final</version>
- </dependency>
</dependencies>
<licenses>
diff --git a/run-jenkins b/run-jenkins
new file mode 100755
index 0000000..bed0b86
--- /dev/null
+++ b/run-jenkins
@@ -0,0 +1 @@
+MAVEN_OPTS="-agentlib:jdwp=transport=dt_socket,server=y,suspend=n,address=5005" mvn hpi:run < /dev/null
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<AbstractProject> {
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<String, String> parameters = new TreeMap<String, String>(req.parameters);
Map<String, Map<String, String>> 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<String, Map<String, String>> entry : context.entrySet()) {
log.warn("{} = {}", entry.getKey(), entry.getValue());
@@ -127,5 +122,36 @@ public class ActiveMqBuildTrigger extends Trigger<AbstractProject> {
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<ActiveMqPlugin>*/ {
-// 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");
+ }
}
diff --git a/src/main/resources/org/jenkinsci/plugins/activemq/ActiveMqBuildTrigger/config.jelly b/src/main/resources/org/jenkinsci/plugins/activemq/ActiveMqBuildTrigger/config.jelly
index bcfde6e..593b994 100644
--- a/src/main/resources/org/jenkinsci/plugins/activemq/ActiveMqBuildTrigger/config.jelly
+++ b/src/main/resources/org/jenkinsci/plugins/activemq/ActiveMqBuildTrigger/config.jelly
@@ -1,6 +1,6 @@
<?jelly escape-by-default='true'?>
<j:jelly xmlns:j="jelly:core" xmlns:st="jelly:stapler" xmlns:d="jelly:define" xmlns:l="/lib/layout" xmlns:t="/lib/hudson" xmlns:f="/lib/form">
- <f:entry title="MVEL Expression" field="mvel">
+ <f:entry title="Expression" field="expression">
<f:textarea />
</f:entry>
</j:jelly>