You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@brooklyn.apache.org by he...@apache.org on 2015/12/23 12:07:26 UTC

[63/71] [abbrv] incubator-brooklyn git commit: [SERVER] fix load of JS UI in BrooklynJavascriptGuiLauncher

[SERVER] fix load of JS UI in BrooklynJavascriptGuiLauncher


Project: http://git-wip-us.apache.org/repos/asf/incubator-brooklyn/repo
Commit: http://git-wip-us.apache.org/repos/asf/incubator-brooklyn/commit/6471500e
Tree: http://git-wip-us.apache.org/repos/asf/incubator-brooklyn/tree/6471500e
Diff: http://git-wip-us.apache.org/repos/asf/incubator-brooklyn/diff/6471500e

Branch: refs/heads/master
Commit: 6471500e2c6f044f7a555fb831e173e9031a2a65
Parents: 1f725bd
Author: Alex Heneveld <al...@cloudsoftcorp.com>
Authored: Tue Dec 22 12:32:51 2015 +0000
Committer: Alex Heneveld <al...@cloudsoftcorp.com>
Committed: Tue Dec 22 12:54:15 2015 +0000

----------------------------------------------------------------------
 .../jsgui/BrooklynJavascriptGuiLauncher.java    | 15 ++++--
 .../BrooklynJavascriptGuiLauncherTest.java      | 14 ++---
 .../brooklyn/rest/BrooklynRestApiLauncher.java  | 56 ++++++++++++++------
 .../rest/BrooklynRestApiLauncherTest.java       |  1 -
 4 files changed, 57 insertions(+), 29 deletions(-)
----------------------------------------------------------------------


http://git-wip-us.apache.org/repos/asf/incubator-brooklyn/blob/6471500e/brooklyn-server/launcher/src/test/java/org/apache/brooklyn/rest/jsgui/BrooklynJavascriptGuiLauncher.java
----------------------------------------------------------------------
diff --git a/brooklyn-server/launcher/src/test/java/org/apache/brooklyn/rest/jsgui/BrooklynJavascriptGuiLauncher.java b/brooklyn-server/launcher/src/test/java/org/apache/brooklyn/rest/jsgui/BrooklynJavascriptGuiLauncher.java
index 6f04fd7..f6329b0 100644
--- a/brooklyn-server/launcher/src/test/java/org/apache/brooklyn/rest/jsgui/BrooklynJavascriptGuiLauncher.java
+++ b/brooklyn-server/launcher/src/test/java/org/apache/brooklyn/rest/jsgui/BrooklynJavascriptGuiLauncher.java
@@ -60,15 +60,22 @@ public class BrooklynJavascriptGuiLauncher {
     
     final static int FAVOURITE_PORT = 8080;
     
-    /** due to the ../jsgui trick in {@link BrooklynRestApiLauncher} we can just call that method */ 
+    /** due to the relative path search in {@link BrooklynRestApiLauncher} we can just call that method */ 
     public static Server startJavascriptAndRest() throws Exception {
         return BrooklynRestApiLauncher.startRestResourcesViaFilter();
     }
 
-    /** not much fun without a REST client. but TODO we should make it so the REST endpoint can be configured. */
-    /** relative path to webapp assumes brooklyn-server has been checked out at the same level as brooklyn-ui  */
+    /** not much fun without a REST server. 
+     * but TODO we should make it so a different REST endpoint could be configured. 
+     * or better, use node js launchers in that project (likely to come with a new ui.) 
+     * <p>
+     * relative path to webapp assumes brooklyn-server has been checked out at the same level as brooklyn-ui;
+     * see {@link BrooklynRestApiLauncher#findJsguiWebappInSource()} */
     public static Server startJavascriptWithoutRest() throws Exception {
-        WebAppContext context = new WebAppContext("../../brooklyn-ui/src/main/webapp", "/");
+        WebAppContext context = new WebAppContext(
+        		BrooklynRestApiLauncher.findJsguiWebappInSource()
+        			.or("../../brooklyn-ui/src/main/webapp"), 
+    			"/");
 
         Server server = new Server(new InetSocketAddress(Networking.LOOPBACK, Networking.nextAvailablePort(FAVOURITE_PORT)));
         server.setHandler(context);

http://git-wip-us.apache.org/repos/asf/incubator-brooklyn/blob/6471500e/brooklyn-server/launcher/src/test/java/org/apache/brooklyn/rest/jsgui/BrooklynJavascriptGuiLauncherTest.java
----------------------------------------------------------------------
diff --git a/brooklyn-server/launcher/src/test/java/org/apache/brooklyn/rest/jsgui/BrooklynJavascriptGuiLauncherTest.java b/brooklyn-server/launcher/src/test/java/org/apache/brooklyn/rest/jsgui/BrooklynJavascriptGuiLauncherTest.java
index b08645b..e03652d 100644
--- a/brooklyn-server/launcher/src/test/java/org/apache/brooklyn/rest/jsgui/BrooklynJavascriptGuiLauncherTest.java
+++ b/brooklyn-server/launcher/src/test/java/org/apache/brooklyn/rest/jsgui/BrooklynJavascriptGuiLauncherTest.java
@@ -18,16 +18,16 @@
  */
 package org.apache.brooklyn.rest.jsgui;
 
-import org.apache.brooklyn.test.HttpTestUtils;
-import org.eclipse.jetty.server.Server;
-import org.eclipse.jetty.server.handler.ContextHandler;
-import org.testng.annotations.AfterMethod;
-import org.testng.annotations.Test;
 import org.apache.brooklyn.api.mgmt.ManagementContext;
 import org.apache.brooklyn.core.entity.Entities;
 import org.apache.brooklyn.rest.BrooklynRestApiLauncherTestFixture;
 import org.apache.brooklyn.rest.util.OsgiCompat;
+import org.apache.brooklyn.util.http.HttpAsserts;
 import org.eclipse.jetty.server.NetworkConnector;
+import org.eclipse.jetty.server.Server;
+import org.eclipse.jetty.server.handler.ContextHandler;
+import org.testng.annotations.AfterMethod;
+import org.testng.annotations.Test;
 
 /** Convenience and demo for launching programmatically. */
 public class BrooklynJavascriptGuiLauncherTest {
@@ -63,11 +63,11 @@ public class BrooklynJavascriptGuiLauncherTest {
     protected void checkUrlContains(final String path, final String text) {
         //Server may return 403 until it loads completely, wait a bit
         //until it stabilizes.
-        HttpTestUtils.assertContentEventuallyContainsText(rootUrl()+path, text);
+        HttpAsserts.assertContentEventuallyContainsText(rootUrl()+path, text);
     }
 
     protected void checkEventuallyHealthy() {
-        HttpTestUtils.assertHttpStatusCodeEventuallyEquals(rootUrl(), 200);
+    	HttpAsserts.assertHttpStatusCodeEventuallyEquals(rootUrl(), 200);
     }
 
     protected String rootUrl() {

http://git-wip-us.apache.org/repos/asf/incubator-brooklyn/blob/6471500e/brooklyn-server/rest/rest-server/src/test/java/org/apache/brooklyn/rest/BrooklynRestApiLauncher.java
----------------------------------------------------------------------
diff --git a/brooklyn-server/rest/rest-server/src/test/java/org/apache/brooklyn/rest/BrooklynRestApiLauncher.java b/brooklyn-server/rest/rest-server/src/test/java/org/apache/brooklyn/rest/BrooklynRestApiLauncher.java
index 2a0bad9..f641267 100644
--- a/brooklyn-server/rest/rest-server/src/test/java/org/apache/brooklyn/rest/BrooklynRestApiLauncher.java
+++ b/brooklyn-server/rest/rest-server/src/test/java/org/apache/brooklyn/rest/BrooklynRestApiLauncher.java
@@ -43,14 +43,18 @@ import org.apache.brooklyn.rest.filter.HaMasterCheckFilter;
 import org.apache.brooklyn.rest.filter.LoggingFilter;
 import org.apache.brooklyn.rest.filter.NoCacheFilter;
 import org.apache.brooklyn.rest.filter.RequestTaggingFilter;
+import org.apache.brooklyn.rest.filter.SwaggerFilter;
 import org.apache.brooklyn.rest.security.provider.AnyoneSecurityProvider;
 import org.apache.brooklyn.rest.security.provider.SecurityProvider;
 import org.apache.brooklyn.rest.util.ManagementContextProvider;
+import org.apache.brooklyn.rest.util.OsgiCompat;
 import org.apache.brooklyn.rest.util.ShutdownHandlerProvider;
 import org.apache.brooklyn.rest.util.TestShutdownHandler;
 import org.apache.brooklyn.util.exceptions.Exceptions;
+import org.apache.brooklyn.util.guava.Maybe;
 import org.apache.brooklyn.util.net.Networking;
 import org.apache.brooklyn.util.text.WildcardGlobs;
+import org.eclipse.jetty.server.NetworkConnector;
 import org.eclipse.jetty.server.Server;
 import org.eclipse.jetty.server.handler.ContextHandler;
 import org.eclipse.jetty.servlet.FilterHolder;
@@ -63,16 +67,12 @@ import org.slf4j.LoggerFactory;
 
 import com.google.common.annotations.Beta;
 import com.google.common.base.Charsets;
-import com.google.common.base.Optional;
 import com.google.common.collect.ImmutableList;
 import com.google.common.collect.Lists;
 import com.google.common.io.Files;
 import com.sun.jersey.api.core.DefaultResourceConfig;
 import com.sun.jersey.api.core.ResourceConfig;
 import com.sun.jersey.spi.container.servlet.ServletContainer;
-import org.apache.brooklyn.rest.filter.SwaggerFilter;
-import org.apache.brooklyn.rest.util.OsgiCompat;
-import org.eclipse.jetty.server.NetworkConnector;
 
 /** Convenience and demo for launching programmatically. Also used for automated tests.
  * <p>
@@ -230,8 +230,8 @@ public class BrooklynRestApiLauncher {
         // For Eclipse, use the default option of ${workspace_loc:brooklyn-launcher}.
         // If the working directory is not set correctly, Brooklyn will be unable to find the jsgui .war
         // file and the 'gui not available' message will be shown.
-        context.setWar(this.deployJsgui && findJsguiWebapp() != null
-                       ? findJsguiWebapp()
+        context.setWar(this.deployJsgui && findJsguiWebappInSource().isPresent()
+                       ? findJsguiWebappInSource().get()
                        : createTempWebDirWithIndexHtml("Brooklyn REST API <p> (gui not available)"));
         installAsServletFilter(context, this.filters);
         return context;
@@ -380,30 +380,52 @@ public class BrooklynRestApiLauncher {
                 new InetSocketAddress(Networking.ANY_NIC, Networking.nextAvailablePort(FAVOURITE_PORT)));
     }
 
-    /** look for the JS GUI webapp in common places, returning path to it if found, or null */
-    private static String findJsguiWebapp() {
-        // could also look in maven repo ?
-        return Optional
-                .fromNullable(findMatchingFile("./brooklyn-ui/src/main/webapp"))
-                .or(findMatchingFile("./brooklyn-ui/target/*.war"))
-                .orNull();
+    /** look for the JS GUI webapp in common source places, returning path to it if found, or null.
+     * assumes `brooklyn-ui` is checked out as a sibling to `brooklyn-server`, and both are 2, 3, 1, or 0
+     * levels above the CWD. */
+    @Beta
+    public static Maybe<String> findJsguiWebappInSource() {
+    	// normally up 2 levels to where brooklyn-* folders are, then into ui
+    	// (but in rest projects it might be 3 up, and in some IDEs we might run from parent dirs.)
+        // TODO could also look in maven repo ?
+    	return findFirstMatchingFile(
+    			"../../brooklyn-ui/src/main/webapp",
+    			"../../../brooklyn-ui/src/main/webapp",
+    			"../brooklyn-ui/src/main/webapp",
+    			"./brooklyn-ui/src/main/webapp",
+    			"../../brooklyn-ui/target/*.war",
+    			"../../..brooklyn-ui/target/*.war",
+    			"../brooklyn-ui/target/*.war",
+    			"./brooklyn-ui/target/*.war");
     }
 
     /** look for the REST WAR file in common places, returning path to it if found, or null */
     private static String findRestApiWar() {
         // don't look at src/main/webapp here -- because classes won't be there!
         // could also look in maven repo ?
+    	// TODO looks like this stopped working at runtime a long time ago;
+    	// only needed for WEB_XML mode, and not used, but should remove or check?
+    	// (probably will be superseded by CXF/OSGi work however)
         return findMatchingFile("../rest/target/*.war").orNull();
     }
 
+    /** as {@link #findMatchingFile(String)} but finding the first */
+    public static Maybe<String> findFirstMatchingFile(String ...filenames) {
+    	for (String f: filenames) {
+    		Maybe<String> result = findMatchingFile(f);
+    		if (result.isPresent()) return result;
+    	}
+    	return Maybe.absent();
+    }
+    
     /** returns the supplied filename if it exists (absolute or relative to the current directory);
      * supports globs in the filename portion only, in which case it returns the _newest_ matching file.
      * <p>
      * otherwise returns null */
     @Beta // public because used in dependent test projects
-    public static Optional<String> findMatchingFile(String filename) {
+    public static Maybe<String> findMatchingFile(String filename) {
         final File f = new File(filename);
-        if (f.exists()) return Optional.of(filename);
+        if (f.exists()) return Maybe.of(filename);
         File dir = f.getParentFile();
         File result = null;
         if (dir.exists()) {
@@ -417,8 +439,8 @@ public class BrooklynRestApiLauncher {
                 if (result==null || mf.lastModified() > result.lastModified()) result = mf;
             }
         }
-        if (result==null) return Optional.absent();
-        return Optional.of(result.getAbsolutePath());
+        if (result==null) return Maybe.absent();
+        return Maybe.of(result.getAbsolutePath());
     }
 
     /** create a directory with a simple index.html so we have some content being served up */

http://git-wip-us.apache.org/repos/asf/incubator-brooklyn/blob/6471500e/brooklyn-server/rest/rest-server/src/test/java/org/apache/brooklyn/rest/BrooklynRestApiLauncherTest.java
----------------------------------------------------------------------
diff --git a/brooklyn-server/rest/rest-server/src/test/java/org/apache/brooklyn/rest/BrooklynRestApiLauncherTest.java b/brooklyn-server/rest/rest-server/src/test/java/org/apache/brooklyn/rest/BrooklynRestApiLauncherTest.java
index cfdcb28..1bf756d 100644
--- a/brooklyn-server/rest/rest-server/src/test/java/org/apache/brooklyn/rest/BrooklynRestApiLauncherTest.java
+++ b/brooklyn-server/rest/rest-server/src/test/java/org/apache/brooklyn/rest/BrooklynRestApiLauncherTest.java
@@ -26,7 +26,6 @@ import java.util.concurrent.Callable;
 
 import org.apache.brooklyn.entity.brooklynnode.BrooklynNode;
 import org.apache.brooklyn.rest.security.provider.AnyoneSecurityProvider;
-import org.apache.brooklyn.rest.util.BrooklynRestResourceUtilsTest.SampleNoOpApplication;
 import org.apache.brooklyn.test.Asserts;
 import org.apache.brooklyn.util.http.HttpAsserts;
 import org.apache.brooklyn.util.http.HttpTool;