You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@tomee.apache.org by rm...@apache.org on 2016/12/03 16:37:57 UTC
[31/50] tomee git commit: TOMEE-1959 script customizers for tomee
embedded plugin
TOMEE-1959 script customizers for tomee embedded plugin
Project: http://git-wip-us.apache.org/repos/asf/tomee/repo
Commit: http://git-wip-us.apache.org/repos/asf/tomee/commit/c31477cf
Tree: http://git-wip-us.apache.org/repos/asf/tomee/tree/c31477cf
Diff: http://git-wip-us.apache.org/repos/asf/tomee/diff/c31477cf
Branch: refs/heads/tomee-1.7.x
Commit: c31477cf61c4942fa4959a3bb90af949194405fb
Parents: 4d1a577
Author: rmannibucau <rm...@apache.org>
Authored: Fri Oct 14 09:30:58 2016 +0200
Committer: rmannibucau <rm...@apache.org>
Committed: Fri Oct 14 09:30:58 2016 +0200
----------------------------------------------------------------------
.../maven/plugins/TomEEEmbeddedMojo.java | 40 ++++++++++
.../maven/plugins/TomEEEmbeddedMojoTest.java | 82 +++++++++++++++++++-
.../maven/plugin/test/JsCustomizertest.java | 2 +-
.../org/apache/tomee/embedded/Container.java | 11 +++
4 files changed, 133 insertions(+), 2 deletions(-)
----------------------------------------------------------------------
http://git-wip-us.apache.org/repos/asf/tomee/blob/c31477cf/maven/tomee-embedded-maven-plugin/src/main/java/org/apache/openejb/maven/plugins/TomEEEmbeddedMojo.java
----------------------------------------------------------------------
diff --git a/maven/tomee-embedded-maven-plugin/src/main/java/org/apache/openejb/maven/plugins/TomEEEmbeddedMojo.java b/maven/tomee-embedded-maven-plugin/src/main/java/org/apache/openejb/maven/plugins/TomEEEmbeddedMojo.java
index da697cb..1267169 100644
--- a/maven/tomee-embedded-maven-plugin/src/main/java/org/apache/openejb/maven/plugins/TomEEEmbeddedMojo.java
+++ b/maven/tomee-embedded-maven-plugin/src/main/java/org/apache/openejb/maven/plugins/TomEEEmbeddedMojo.java
@@ -49,8 +49,13 @@ import org.codehaus.plexus.configuration.PlexusConfiguration;
import org.codehaus.plexus.util.FileUtils;
import javax.naming.NamingException;
+import javax.script.ScriptEngine;
+import javax.script.ScriptEngineManager;
+import javax.script.ScriptException;
+import javax.script.SimpleBindings;
import java.io.File;
import java.io.IOException;
+import java.io.StringReader;
import java.lang.reflect.Field;
import java.lang.reflect.Modifier;
import java.net.MalformedURLException;
@@ -332,6 +337,18 @@ public class TomEEEmbeddedMojo extends AbstractMojo {
@Parameter(property = "tomee-plugin.liveReload", defaultValue = "false")
private boolean withLiveReload;
+ /**
+ * A list of js scripts executed before the container starts.
+ */
+ @Parameter
+ protected List<String> jsCustomizers;
+
+ /**
+ * A list of groovy scripts executed before the container starts. Needs to add groovy as dependency.
+ */
+ @Parameter
+ protected List<String> groovyCustomizers;
+
private Map<String, Command> commands;
private String deployedName;
@@ -385,6 +402,10 @@ public class TomEEEmbeddedMojo extends AbstractMojo {
throw new TomEERuntimeException(e);
}
}
+
+ final String base = getBase().getAbsolutePath();
+ scriptCustomization(jsCustomizers, "js", base);
+ scriptCustomization(groovyCustomizers, "groovy", base);
}
};
final Configuration config = getConfig();
@@ -478,6 +499,25 @@ public class TomEEEmbeddedMojo extends AbstractMojo {
}
}
+ private void scriptCustomization(final List<String> customizers, final String ext, final String base) {
+ if (customizers == null || customizers.isEmpty()) {
+ return;
+ }
+ final ScriptEngine engine = new ScriptEngineManager().getEngineByExtension(ext);
+ if (engine == null) {
+ throw new IllegalStateException("No engine for " + ext + ". Maybe add the JSR223 implementation as plugin dependency.");
+ }
+ for (final String js : customizers) {
+ try {
+ final SimpleBindings bindings = new SimpleBindings();
+ bindings.put("catalinaBase", base);
+ engine.eval(new StringReader(js), bindings);
+ } catch (final ScriptException e) {
+ throw new IllegalStateException(e.getMessage(), e);
+ }
+ }
+ }
+
protected Scanner newScanner() {
return new Scanner(System.in);
}
http://git-wip-us.apache.org/repos/asf/tomee/blob/c31477cf/maven/tomee-embedded-maven-plugin/src/test/java/org/apache/openejb/maven/plugins/TomEEEmbeddedMojoTest.java
----------------------------------------------------------------------
diff --git a/maven/tomee-embedded-maven-plugin/src/test/java/org/apache/openejb/maven/plugins/TomEEEmbeddedMojoTest.java b/maven/tomee-embedded-maven-plugin/src/test/java/org/apache/openejb/maven/plugins/TomEEEmbeddedMojoTest.java
index 3a51b51..e7e617e 100644
--- a/maven/tomee-embedded-maven-plugin/src/test/java/org/apache/openejb/maven/plugins/TomEEEmbeddedMojoTest.java
+++ b/maven/tomee-embedded-maven-plugin/src/test/java/org/apache/openejb/maven/plugins/TomEEEmbeddedMojoTest.java
@@ -21,15 +21,23 @@ import org.apache.maven.plugin.MojoFailureException;
import org.apache.maven.plugin.logging.SystemStreamLog;
import org.apache.openejb.config.DeploymentFilterable;
import org.apache.openejb.loader.IO;
+import org.apache.openejb.loader.SystemInstance;
import org.apache.openejb.util.NetworkUtil;
+import org.junit.Before;
import org.junit.Test;
+import javax.management.InstanceNotFoundException;
+import javax.management.IntrospectionException;
+import javax.management.MalformedObjectNameException;
+import javax.management.ObjectName;
+import javax.management.ReflectionException;
import java.io.ByteArrayInputStream;
import java.io.File;
import java.io.FileWriter;
import java.io.IOException;
import java.io.InputStream;
import java.io.UnsupportedEncodingException;
+import java.lang.management.ManagementFactory;
import java.net.URL;
import java.util.HashMap;
import java.util.Scanner;
@@ -42,11 +50,33 @@ import static java.lang.System.lineSeparator;
import static java.lang.Thread.sleep;
import static java.util.Collections.singletonList;
import static org.junit.Assert.assertEquals;
+import static org.junit.Assert.assertFalse;
import static org.junit.Assert.assertNull;
import static org.junit.Assert.assertTrue;
import static org.junit.Assert.fail;
public class TomEEEmbeddedMojoTest {
+ @Before // there can be a small latency stopping/restarting tomcat so ensure we don't have conflicts between tests
+ public void ensureTomcatIsDown() throws MalformedObjectNameException, IntrospectionException, ReflectionException {
+ for (int i = 0; i < 10; i++) {
+ try {
+ assertFalse(SystemInstance.isInitialized());
+ try {
+ assertNull(ManagementFactory.getPlatformMBeanServer().getMBeanInfo(new ObjectName("Tomcat:type=Server")));
+ } catch (final InstanceNotFoundException e) {
+ // ok
+ }
+ } catch (final AssertionError ae) {
+ try {
+ sleep(1000);
+ } catch (final InterruptedException e) {
+ Thread.interrupted();
+ fail();
+ }
+ }
+ }
+ }
+
@Test
public void run() throws MojoFailureException, MojoExecutionException, IOException, InterruptedException {
final File docBase = new File("target/TomEEEmbeddedMojoTest/base");
@@ -131,7 +161,6 @@ public class TomEEEmbeddedMojoTest {
input.close();
}
-
@Test
public void customWebResource() throws Exception {
final File docBase = new File("target/TomEEEmbeddedMojoTest/customWebResource");
@@ -176,6 +205,57 @@ public class TomEEEmbeddedMojoTest {
input.close();
}
+ @Test
+ public void customScript() throws Exception {
+ // we use a dynamic InputStream to be able to simulate commands without hacking System.in
+ final Input input = new Input();
+ final Semaphore reloaded = new Semaphore(0);
+ final CountDownLatch started = new CountDownLatch(1);
+ final TomEEEmbeddedMojo mojo = new TomEEEmbeddedMojo() {
+ @Override
+ protected Scanner newScanner() {
+ return new Scanner(input);
+ }
+ };
+ mojo.classpathAsWar = true;
+ mojo.httpPort = NetworkUtil.getNextAvailablePort();
+ mojo.ssl = false;
+ mojo.webResourceCached = false;
+ mojo.jsCustomizers = singletonList(
+ "var File = Java.type('java.io.File');" +
+ "var FileWriter = Java.type('java.io.FileWriter');" +
+ "var out = new File(catalinaBase, 'conf/app.conf');" +
+ "var writer = new FileWriter(out);" +
+ "writer.write('test=ok');" +
+ "writer.close();");
+ mojo.setLog(new SystemStreamLog() { // not the best solution but fine for now...
+ @Override
+ public void info(final CharSequence charSequence) {
+ final String string = charSequence.toString();
+ if (string.startsWith("TomEE embedded started on") || string.equals("can't start TomEE")) {
+ started.countDown();
+ } else if (string.contains("Redeployed /")) {
+ reloaded.release();
+ }
+ super.info(charSequence);
+ }
+ });
+
+ CountDownLatch stopped = null;
+ try {
+ stopped = doStart(started, mojo);
+ final File appConf = new File(System.getProperty("catalina.base"), "conf/app.conf");
+ assertTrue(appConf.exists());
+ assertEquals("ok", IO.readProperties(appConf).getProperty("test", "ko"));
+ } finally {
+ input.write("exit");
+ if (stopped != null) {
+ stopped.await(5, TimeUnit.MINUTES);
+ }
+ input.close();
+ }
+ }
+
private CountDownLatch doStart(final CountDownLatch started, final TomEEEmbeddedMojo mojo) {
final CountDownLatch stopped = new CountDownLatch(1);
http://git-wip-us.apache.org/repos/asf/tomee/blob/c31477cf/maven/tomee-maven-plugin/src/test/java/org/apache/openejb/maven/plugin/test/JsCustomizertest.java
----------------------------------------------------------------------
diff --git a/maven/tomee-maven-plugin/src/test/java/org/apache/openejb/maven/plugin/test/JsCustomizertest.java b/maven/tomee-maven-plugin/src/test/java/org/apache/openejb/maven/plugin/test/JsCustomizertest.java
index b8c340f..1d0c556 100644
--- a/maven/tomee-maven-plugin/src/test/java/org/apache/openejb/maven/plugin/test/JsCustomizertest.java
+++ b/maven/tomee-maven-plugin/src/test/java/org/apache/openejb/maven/plugin/test/JsCustomizertest.java
@@ -42,8 +42,8 @@ public class JsCustomizertest {
"" +
"var junit = resolver.resolve('junit', 'junit', '4.12');" +
"Files.copy(junit.toPath(), new File(catalinaBase, 'lib/JsCustomizertest.jar').toPath(), StandardCopyOption.REPLACE_EXISTING);"
- );
+ );
@Config
private final File catalinaBase = new File("target/JsCustomizertest");
http://git-wip-us.apache.org/repos/asf/tomee/blob/c31477cf/tomee/tomee-embedded/src/main/java/org/apache/tomee/embedded/Container.java
----------------------------------------------------------------------
diff --git a/tomee/tomee-embedded/src/main/java/org/apache/tomee/embedded/Container.java b/tomee/tomee-embedded/src/main/java/org/apache/tomee/embedded/Container.java
index aa2d054..4a30f0e 100644
--- a/tomee/tomee-embedded/src/main/java/org/apache/tomee/embedded/Container.java
+++ b/tomee/tomee-embedded/src/main/java/org/apache/tomee/embedded/Container.java
@@ -106,6 +106,7 @@ import java.util.Collection;
import java.util.Collections;
import java.util.HashMap;
import java.util.HashSet;
+import java.util.Iterator;
import java.util.List;
import java.util.Map;
import java.util.Properties;
@@ -356,6 +357,16 @@ public class Container implements AutoCloseable {
private static void addCallersAsEjbModule(final ClassLoader loader, final AppModule app, final String... additionalCallers) {
final Set<String> callers = new HashSet<>(NewLoaderLogic.callers(Filters.classes(Container.class.getName(), "org.apache.openejb.maven.plugins.TomEEEmbeddedMojo")));
+ // we don't care of these
+ callers.remove("org.apache.tomee.embedded.Container");
+ callers.remove("org.apache.tomee.gradle.embedded.TomEEEmbeddedTask");
+ final Iterator<String> callerIt = callers.iterator();
+ while (callerIt.hasNext()) { // TomEEEmbeddedMojo is also used with some anonymous classes (TomEEEmbeddedMojo$x)
+ if (callerIt.next().startsWith("org.apache.openejb.maven.plugins.TomEEEmbeddedMojo")) {
+ callerIt.remove();
+ // no break since we remove anonymous class+the mojo itself
+ }
+ }
if (additionalCallers != null && additionalCallers.length > 0) {
callers.addAll(asList(additionalCallers));
}