tomee-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From andygumbre...@apache.org
Subject tomee git commit: Mojo needs to know/generate the random port
Date Thu, 05 Nov 2015 01:26:09 GMT
Repository: tomee
Updated Branches:
  refs/heads/master a021113c5 -> 372f58fa3


Mojo needs to know/generate the random port


Project: http://git-wip-us.apache.org/repos/asf/tomee/repo
Commit: http://git-wip-us.apache.org/repos/asf/tomee/commit/372f58fa
Tree: http://git-wip-us.apache.org/repos/asf/tomee/tree/372f58fa
Diff: http://git-wip-us.apache.org/repos/asf/tomee/diff/372f58fa

Branch: refs/heads/master
Commit: 372f58fa35fb01535982ddc9307fc6517eb2fa25
Parents: a021113
Author: andygumbrecht@apache.org <andy.is@gmx.de>
Authored: Thu Nov 5 02:25:48 2015 +0100
Committer: andygumbrecht@apache.org <andy.is@gmx.de>
Committed: Thu Nov 5 02:25:48 2015 +0100

----------------------------------------------------------------------
 .../org/apache/openejb/config/RemoteServer.java | 22 ++++---
 .../org/apache/openejb/util/NetworkUtil.java    |  6 ++
 .../openejb/maven/plugin/AbstractTomEEMojo.java | 65 +++++++++++++++-----
 3 files changed, 68 insertions(+), 25 deletions(-)
----------------------------------------------------------------------


http://git-wip-us.apache.org/repos/asf/tomee/blob/372f58fa/container/openejb-core/src/main/java/org/apache/openejb/config/RemoteServer.java
----------------------------------------------------------------------
diff --git a/container/openejb-core/src/main/java/org/apache/openejb/config/RemoteServer.java
b/container/openejb-core/src/main/java/org/apache/openejb/config/RemoteServer.java
index 7ca43ae..5de85cc 100644
--- a/container/openejb-core/src/main/java/org/apache/openejb/config/RemoteServer.java
+++ b/container/openejb-core/src/main/java/org/apache/openejb/config/RemoteServer.java
@@ -357,15 +357,17 @@ public class RemoteServer {
                 throw (RuntimeException) new OpenEJBRuntimeException("Cannot start the server.
 Exception: " + e.getClass().getName() + ": " + e.getMessage()).initCause(e);
             }
 
-            if (debug) {
-                if (!connect(port, Integer.MAX_VALUE)) {
-                    destroy();
-                    throw new OpenEJBRuntimeException("Could not connect to server");
-                }
-            } else {
-                if (!connect(port, tries)) {
-                    destroy();
-                    throw new OpenEJBRuntimeException("Could not connect to server");
+            if (port > 0) {
+                if (debug) {
+                    if (!connect(port, Integer.MAX_VALUE)) {
+                        destroy();
+                        throw new OpenEJBRuntimeException("Could not connect to server");
+                    }
+                } else {
+                    if (!connect(port, tries)) {
+                        destroy();
+                        throw new OpenEJBRuntimeException("Could not connect to server");
+                    }
                 }
             }
 
@@ -412,7 +414,7 @@ public class RemoteServer {
             final Field f = server.get().getClass().getDeclaredField("pid");
             f.setAccessible(true);
             final int pid = (Integer) f.get(server.get());
-            new ProcessBuilder("kill",  "-3",  Integer.toString(pid))
+            new ProcessBuilder("kill", "-3", Integer.toString(pid))
                     .redirectOutput(ProcessBuilder.Redirect.INHERIT)
                     .redirectError(ProcessBuilder.Redirect.INHERIT)
                     .start();

http://git-wip-us.apache.org/repos/asf/tomee/blob/372f58fa/container/openejb-core/src/main/java/org/apache/openejb/util/NetworkUtil.java
----------------------------------------------------------------------
diff --git a/container/openejb-core/src/main/java/org/apache/openejb/util/NetworkUtil.java
b/container/openejb-core/src/main/java/org/apache/openejb/util/NetworkUtil.java
index 242cad5..f08ab2e 100644
--- a/container/openejb-core/src/main/java/org/apache/openejb/util/NetworkUtil.java
+++ b/container/openejb-core/src/main/java/org/apache/openejb/util/NetworkUtil.java
@@ -30,6 +30,12 @@ public final class NetworkUtil {
         // no-op
     }
 
+    /**
+     * Get the next available port using ServerSocket(0).
+     * This is not random and will result in port duplicates if many threads call this concurrently
+     *
+     * @return an unused port at the time of calling
+     */
     public static int getNextAvailablePort() {
         return getNextAvailablePort(new int[]{0});
     }

http://git-wip-us.apache.org/repos/asf/tomee/blob/372f58fa/maven/tomee-maven-plugin/src/main/java/org/apache/openejb/maven/plugin/AbstractTomEEMojo.java
----------------------------------------------------------------------
diff --git a/maven/tomee-maven-plugin/src/main/java/org/apache/openejb/maven/plugin/AbstractTomEEMojo.java
b/maven/tomee-maven-plugin/src/main/java/org/apache/openejb/maven/plugin/AbstractTomEEMojo.java
index 9c4ce56..d28a6c8 100644
--- a/maven/tomee-maven-plugin/src/main/java/org/apache/openejb/maven/plugin/AbstractTomEEMojo.java
+++ b/maven/tomee-maven-plugin/src/main/java/org/apache/openejb/maven/plugin/AbstractTomEEMojo.java
@@ -43,6 +43,7 @@ import org.apache.openejb.loader.Zips;
 import org.apache.openejb.maven.plugin.cli.Args;
 import org.apache.openejb.maven.util.XmlFormatter;
 import org.apache.openejb.util.Join;
+import org.apache.openejb.util.NetworkUtil;
 import org.apache.openejb.util.OpenEjbVersion;
 import org.apache.tomee.util.QuickServerXmlParser;
 import org.codehaus.plexus.configuration.PlexusConfiguration;
@@ -272,7 +273,7 @@ public abstract class AbstractTomEEMojo extends AbstractAddressMojo {
 
     @Parameter
     protected List<String> endorsedLibs;
-    
+
     @Parameter
     protected List<String> javaagents;
 
@@ -364,7 +365,7 @@ public abstract class AbstractTomEEMojo extends AbstractAddressMojo {
      * the actual path used in server.xml for the https keystore if relevant.
      * Common usage will be to put in src/main/tomee/conf a keystore foo.jks
      * and set this value to ${catalina.base}/foo.jks.
-     *
+     * <p/>
      * Note: if not set we'll check for any *.jks in conf/. You can set it to "ignore" to
skip this.
      */
     @Parameter(property = "tomee-plugin.keystore")
@@ -460,7 +461,7 @@ public abstract class AbstractTomEEMojo extends AbstractAddressMojo {
                             final Class<?> clazz = tccl.loadClass(customizer);
                             try {
                                 clazz.getMethod("main", String[].class)
-                                    .invoke(null, new String[] { catalinaBase.getAbsolutePath()
});
+                                        .invoke(null, new String[]{catalinaBase.getAbsolutePath()});
                             } catch (final NoSuchMethodException noMainEx) {
                                 try {
                                     final Constructor<?> cons = clazz.getConstructor(File.class);
@@ -885,28 +886,28 @@ public abstract class AbstractTomEEMojo extends AbstractAddressMojo
{
 
             // ensure connector is not commented
             value = value.replace("<Service name=\"Catalina\">", "<Service name=\"Catalina\">\n"
-                    + "    <Connector port=\"" + tomeeHttpsPort + "\" protocol=\"HTTP/1.1\"
SSLEnabled=\"true\"\n" +
+                    + "    <Connector port=\"" + this.getTomeeHttpsPortChecked() + "\"
protocol=\"HTTP/1.1\" SSLEnabled=\"true\"\n" +
                     "                scheme=\"https\" secure=\"true\"\n" +
                     "                clientAuth=\"false\" sslProtocol=\"TLS\" keystoreFile=\""
+ keystorePath + "\" />\n");
         }
 
         if (tomeeHttpPort != null) {
-            value = value.replace("\"" + parser.http() + '"', '"' + tomeeHttpPort + '"');
+            value = value.replace("\"" + parser.http() + "\"", "\"" + this.getTomeeHttpPortChecked()
+ "\"");
         }
         if (tomeeHttpsPort != null) {
-            value = value.replace("\"" + parser.https() + '"', '"' + tomeeHttpsPort + '"');
+            value = value.replace("\"" + parser.https() + "\"", "\"" + this.getTomeeHttpsPortChecked()
+ "\"");
         }
         if (tomeeAjpPort != null) {
-            value = value.replace("\"" + parser.ajp() + '"', '"' + tomeeAjpPort + '"');
+            value = value.replace("\"" + parser.ajp() + "\"", "\"" + tomeeAjpPort + "\"");
         }
         if (tomeeShutdownPort != null) {
-            value = value.replace("\"" + parser.stop() + '"', '"' + tomeeShutdownPort + '"');
+            value = value.replace("\"" + parser.stop() + "\"", "\"" + this.getTomeeShutdownPortChecked()
+ "\"");
         }
         if (webappDir != null) {
-            value = value.replace("\"" + parser.value("app-base", "webapps") + '"', '"' +
webappDir + '"');
+            value = value.replace("\"" + parser.value("app-base", "webapps") + "\"", "\""
+ webappDir + "\"");
         }
         if (tomeeHost != null) {
-            value = value.replace("\"" + parser.host() + '"', '"' + tomeeHost + '"');
+            value = value.replace("\"" + parser.host() + "\"", "\"" + tomeeHost + "\"");
         }
 
         if (!original.equals(value)) {
@@ -1000,7 +1001,7 @@ public abstract class AbstractTomEEMojo extends AbstractAddressMojo
{
             System.setProperty("openejb.server.debug", "true");
             System.setProperty("server.debug.port", Integer.toString(debugPort));
         }
-        System.setProperty("server.shutdown.port", tomeeShutdownPort);
+        System.setProperty("server.shutdown.port", String.valueOf(this.getTomeeShutdownPortChecked()));
         System.setProperty("server.shutdown.command", tomeeShutdownCommand);
 
         server = new RemoteServer(getConnectAttempts(), debug);
@@ -1010,7 +1011,7 @@ public abstract class AbstractTomEEMojo extends AbstractAddressMojo
{
 
         if (TOM_EE.equals(container)) {
 
-            server.setPortStartup(Integer.parseInt(tomeeHttpPort == null ? tomeeHttpsPort
: tomeeHttpPort));
+            server.setPortStartup(tomeeHttpPort == null ? this.getTomeeHttpsPortChecked()
: this.getTomeeHttpPortChecked());
 
             getLog().info("Running '" + getClass().getName().replace("TomEEMojo", "").toLowerCase(Locale.ENGLISH)
                     + "'. Configured TomEE in plugin is " + tomeeHost + ":" + server.getPortStartup()
@@ -1045,7 +1046,7 @@ public abstract class AbstractTomEEMojo extends AbstractAddressMojo
{
                         break;
                     }
 
-                    if("ignore".equals(line)){
+                    if ("ignore".equals(line)) {
                         continue;
                     }
 
@@ -1067,6 +1068,36 @@ public abstract class AbstractTomEEMojo extends AbstractAddressMojo
{
         }
     }
 
+    private synchronized int getTomeeHttpPortChecked() {
+        int port = Integer.parseInt(this.tomeeHttpPort);
+
+        if (port <= 0) {
+            port = NetworkUtil.getNextAvailablePort();
+            this.tomeeHttpPort = String.valueOf(port);
+        }
+        return port;
+    }
+
+    private synchronized int getTomeeHttpsPortChecked() {
+        int port = Integer.parseInt(this.tomeeHttpsPort);
+
+        if (port <= 0) {
+            port = NetworkUtil.getNextAvailablePort();
+            this.tomeeHttpsPort = String.valueOf(port);
+        }
+        return port;
+    }
+
+    private synchronized int getTomeeShutdownPortChecked() {
+        int port = Integer.parseInt(this.tomeeShutdownPort);
+
+        if (port <= 0) {
+            port = NetworkUtil.getNextAvailablePort();
+            this.tomeeShutdownPort = String.valueOf(port);
+        }
+        return port;
+    }
+
     private String getNextLine(final Scanner reader) {
         try {
             return reader.nextLine();
@@ -1319,7 +1350,11 @@ public abstract class AbstractTomEEMojo extends AbstractAddressMojo
{
     }
 
     protected void serverCmd(final RemoteServer server, final List<String> strings)
{
-        server.start(strings, getCmd(), checkStarted);
+        try {
+            server.start(strings, getCmd(), checkStarted);
+        } catch (final OpenEJBRuntimeException e) {
+            getLog().warn("Failed to check or track server startup on port: " + this.tomeeHttpPort);
+        }
     }
 
     protected void addShutdownHooks(final RemoteServer server) {
@@ -1462,7 +1497,7 @@ public abstract class AbstractTomEEMojo extends AbstractAddressMojo
{
                 (apps != null && !apps.isEmpty())
                         || (!"pom".equals(packaging) && !"war".equals(packaging)))))
{ // webapps doesn't need apps folder in tomee
             final String rootTag = container.toLowerCase(Locale.ENGLISH);
-            if (file.isFile())  { // can be not existing since we dont always deploy tomee
but shouldn't since then apps/ is not guaranteed to work
+            if (file.isFile()) { // can be not existing since we dont always deploy tomee
but shouldn't since then apps/ is not guaranteed to work
                 try {
                     final Openejb jaxb = JaxbOpenejb.readConfig(file.getAbsolutePath());
                     boolean needAdd = true;


Mime
View raw message