Author: rmannibucau
Date: Wed Oct 19 06:57:13 2016
New Revision: 1765540

URL: http://svn.apache.org/viewvc?rev=1765540&view=rev
Log:
better wait strategy for the main + fixing stop port setup

Added:
    
openwebbeans/microwave/trunk/microwave-core/src/test/java/org/apache/microwave/runner/
    
openwebbeans/microwave/trunk/microwave-core/src/test/java/org/apache/microwave/runner/CliTest.java
Modified:
    
openwebbeans/microwave/trunk/microwave-core/src/main/java/org/apache/microwave/Microwave.java
    
openwebbeans/microwave/trunk/microwave-core/src/main/java/org/apache/microwave/runner/Cli.java

Modified: 
openwebbeans/microwave/trunk/microwave-core/src/main/java/org/apache/microwave/Microwave.java
URL: 
http://svn.apache.org/viewvc/openwebbeans/microwave/trunk/microwave-core/src/main/java/org/apache/microwave/Microwave.java?rev=1765540&r1=1765539&r2=1765540&view=diff
==============================================================================
--- 
openwebbeans/microwave/trunk/microwave-core/src/main/java/org/apache/microwave/Microwave.java
 (original)
+++ 
openwebbeans/microwave/trunk/microwave-core/src/main/java/org/apache/microwave/Microwave.java
 Wed Oct 19 06:57:13 2016
@@ -84,9 +84,11 @@ public class Microwave implements AutoCl
     @Getter
     private final Builder configuration;
 
-    protected InternalTomcat tomcat;
+    @Getter
     protected File base;
 
+    protected InternalTomcat tomcat;
+
     // we can undeploy webapps with that later
     private final Map<String, Context> contexts = new HashMap<>();
 
@@ -94,6 +96,10 @@ public class Microwave implements AutoCl
         this.configuration = builder;
     }
 
+    public Tomcat getTomcat() {
+        return tomcat;
+    }
+
     public Microwave deployClasspath() {
         return deployClasspath("");
     }
@@ -179,6 +185,7 @@ public class Microwave implements AutoCl
         } else {
             tomcat = new InternalTomcat();
         }
+        tomcat.getServer().setPort(configuration.stopPort);
 
         { // setup
             base = new File(getBaseDir());

Modified: 
openwebbeans/microwave/trunk/microwave-core/src/main/java/org/apache/microwave/runner/Cli.java
URL: 
http://svn.apache.org/viewvc/openwebbeans/microwave/trunk/microwave-core/src/main/java/org/apache/microwave/runner/Cli.java?rev=1765540&r1=1765539&r2=1765540&view=diff
==============================================================================
--- 
openwebbeans/microwave/trunk/microwave-core/src/main/java/org/apache/microwave/runner/Cli.java
 (original)
+++ 
openwebbeans/microwave/trunk/microwave-core/src/main/java/org/apache/microwave/runner/Cli.java
 Wed Oct 19 06:57:13 2016
@@ -37,7 +37,6 @@ import java.util.HashMap;
 import java.util.List;
 import java.util.Map;
 import java.util.Properties;
-import java.util.concurrent.CountDownLatch;
 import java.util.stream.Stream;
 
 import static java.util.Optional.ofNullable;
@@ -74,33 +73,16 @@ public class Cli {
             return;
         }
 
-        final CountDownLatch latch = new CountDownLatch(1);
-        final Thread hook = new Thread() {
-            @Override
-            public void run() {
-                latch.countDown();
-            }
-        };
-        Runtime.getRuntime().addShutdownHook(hook);
         try (final Microwave microwave = new Microwave(buildConfig(line, 
fields))) {
             final String ctx = line.getOptionValue("context", "");
+            final String fixedCtx = !ctx.isEmpty() && !ctx.startsWith("/") ? 
'/' + ctx : ctx;
             final String war = line.getOptionValue("webapp");
             if (war == null) {
-                microwave.bake(ctx);
+                microwave.bake(fixedCtx);
             } else {
-                microwave.deployWebapp(ctx, new File(war));
-            }
-            try {
-                latch.await();
-            } catch (final InterruptedException e) {
-                Thread.interrupted();
-            }
-        } finally {
-            try {
-                Runtime.getRuntime().removeShutdownHook(hook);
-            } catch (final IllegalStateException ise) {
-                // no-op
+                microwave.deployWebapp(fixedCtx, new File(war));
             }
+            microwave.getTomcat().getServer().await();
         }
     }
 
@@ -114,6 +96,9 @@ public class Cli {
                         
ofNullable(line.getOptionValue(name)).map(Boolean::parseBoolean).orElse(true) :
                         toValue(name, line.getOptionValues(name), f.getType()))
                         .ifPresent(v -> {
+                            if (!f.isAccessible()) {
+                                f.setAccessible(true);
+                            }
                             try {
                                 f.set(config, v);
                             } catch (final IllegalAccessException e) {

Added: 
openwebbeans/microwave/trunk/microwave-core/src/test/java/org/apache/microwave/runner/CliTest.java
URL: 
http://svn.apache.org/viewvc/openwebbeans/microwave/trunk/microwave-core/src/test/java/org/apache/microwave/runner/CliTest.java?rev=1765540&view=auto
==============================================================================
--- 
openwebbeans/microwave/trunk/microwave-core/src/test/java/org/apache/microwave/runner/CliTest.java
 (added)
+++ 
openwebbeans/microwave/trunk/microwave-core/src/test/java/org/apache/microwave/runner/CliTest.java
 Wed Oct 19 06:57:13 2016
@@ -0,0 +1,91 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one
+ * or more contributor license agreements. See the NOTICE file
+ * distributed with this work for additional information
+ * regarding copyright ownership. The ASF licenses this file
+ * to you under the Apache License, Version 2.0 (the
+ * "License"); you may not use this file except in compliance
+ * with the License. You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing,
+ * software distributed under the License is distributed on an
+ * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
+ * KIND, either express or implied. See the License for the
+ * specific language governing permissions and limitations
+ * under the License.
+ */
+package org.apache.microwave.runner;
+
+import org.apache.commons.io.IOUtils;
+import org.junit.Test;
+
+import java.io.FileNotFoundException;
+import java.io.IOException;
+import java.net.ConnectException;
+import java.net.ServerSocket;
+import java.net.Socket;
+import java.net.URL;
+import java.nio.charset.StandardCharsets;
+import java.util.concurrent.TimeUnit;
+
+import static org.junit.Assert.assertEquals;
+import static org.junit.Assert.fail;
+
+public class CliTest {
+    @Test
+    public void bake() throws IOException {
+        int stop;
+        int http;
+        try (final ServerSocket s1 = new ServerSocket(0)) {
+            stop = s1.getLocalPort();
+            try (final ServerSocket s2 = new ServerSocket(0)) {
+                http = s2.getLocalPort();
+            }
+        }
+        final Thread runner = new Thread() {
+            {
+                setName("CLI thread");
+            }
+
+            @Override
+            public void run() {
+                Cli.main(new String[]{
+                        "--context=app",
+                        "--stop=" + stop,
+                        "--http=" + http
+                });
+            }
+        };
+        try {
+            runner.start();
+            for (int i = 0; i < 60; i++) {
+                try {
+                    assertEquals("{\"name\":\"test\"}", IOUtils.toString(new 
URL("http://localhost:"; + http + "/app/api/test/json")));
+                    return;
+                } catch (final AssertionError | ConnectException | 
FileNotFoundException notYet) {
+                    try {
+                        Thread.sleep(1000);
+                    } catch (final InterruptedException e) {
+                        Thread.interrupted();
+                        fail();
+                    }
+                }
+            }
+            fail("Didnt achieved the request");
+        } finally {
+            try (final Socket client = new Socket("localhost", stop)) {
+                
client.getOutputStream().write("SHUTDOWN".getBytes(StandardCharsets.UTF_8));
+            } catch (final IOException ioe) {
+                // ok, container is down now
+            } finally {
+                try {
+                    runner.join(TimeUnit.MINUTES.toMillis(1));
+                } catch (final InterruptedException e) {
+                    Thread.interrupted();
+                }
+            }
+        }
+    }
+}


Reply via email to