Skip to content

Commit

Permalink
JBIDE-21994 - Cannot show OpenShift 3 application via live reload
Browse files Browse the repository at this point in the history
Using the server.getHost() method now that it's set to an actual
route for OS servers
Calling the IURLProvider delegate on the given IServer to get the
module context root when building the URL to invoke from the
browser, via the LiveReload proxy
Also fixed 'core' dependencies, removing some 'ui' bundles
  • Loading branch information
xcoulon authored and fbricon committed Apr 1, 2016
1 parent eeaa6e4 commit bf3ca4c
Show file tree
Hide file tree
Showing 12 changed files with 277 additions and 39 deletions.
8 changes: 3 additions & 5 deletions plugins/org.jboss.tools.livereload.core/META-INF/MANIFEST.MF
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,7 @@ Bundle-SymbolicName: org.jboss.tools.livereload.core;singleton:=true
Bundle-Version: 1.3.1.qualifier
Bundle-Activator: org.jboss.tools.livereload.core.internal.JBossLiveReloadCoreActivator
Bundle-Vendor: JBoss by Red Hat
Require-Bundle: org.eclipse.ui,
org.eclipse.core.runtime,
Require-Bundle: org.eclipse.core.runtime,
org.eclipse.core.resources;bundle-version="3.8.1",
org.eclipse.core.databinding;bundle-version="1.4.1",
org.junit;bundle-version="4.10.0";resolution:=optional,
Expand All @@ -29,16 +28,15 @@ Require-Bundle: org.eclipse.ui,
org.eclipse.jetty.proxy;bundle-version="[9.2.13,9.2.14)",
org.apache.commons.io;bundle-version="2.0.1",
org.jboss.tools.common;bundle-version="3.4.0",
org.jboss.tools.common.ui;bundle-version="3.6.0",
org.eclipse.debug.core;bundle-version="3.8.0",
org.jboss.ide.eclipse.as.core;bundle-version="3.0.0",
org.eclipse.ui.browser;bundle-version="3.4.200",
org.eclipse.jst.server.tomcat.core;bundle-version="1.1.500",
com.fasterxml.jackson.core.jackson-annotations;bundle-version="2.5.0",
com.fasterxml.jackson.core.jackson-core;bundle-version="2.5.0",
com.fasterxml.jackson.core.jackson-databind;bundle-version="2.5.0",
org.apache.aries.spifly.dynamic.bundle;bundle-version="1.0.2",
org.jboss.tools.usage;bundle-version="2.1.1"
org.jboss.tools.usage;bundle-version="2.1.1",
org.eclipse.core.commands
Bundle-RequiredExecutionEnvironment: JavaSE-1.7
Bundle-ActivationPolicy: lazy
Bundle-ClassPath: .,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
import java.util.concurrent.TimeUnit;
import java.util.concurrent.TimeoutException;

import org.eclipse.ui.plugin.AbstractUIPlugin;
import org.eclipse.core.runtime.Plugin;
import org.eclipse.wst.server.core.IServer;
import org.jboss.tools.livereload.core.internal.server.jetty.LiveReloadServer;
import org.jboss.tools.livereload.core.internal.util.Logger;
Expand All @@ -19,7 +19,7 @@
/**
* The activator class controls the plug-in life cycle
*/
public class JBossLiveReloadCoreActivator extends AbstractUIPlugin {
public class JBossLiveReloadCoreActivator extends Plugin {

/** The plug-in ID. */
public static final String PLUGIN_ID = "org.jboss.tools.livereload.core"; //$NON-NLS-1$
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,8 @@ public class JettyServerRunner implements Runnable {

/**
* Starts the server
* @param liveReloadServer the {@link Server} to start
* @return the corresponding {@link JettyServerRunner} if the server started, <code>null</code> otherwise.
*
* @throws TimeoutException
*/
Expand All @@ -57,7 +59,7 @@ public static JettyServerRunner start(final Server liveReloadServer) throws Time
public boolean isComplete() {
// task is complete if server started or if the runner status is not OK (ie, an error occurred)
final boolean started = runner.isStarted();
final boolean statusOk = runner.status != null && !runner.status.isOK();
final boolean statusOk = runner.isStatusOk();
return started || statusOk;
}
};
Expand All @@ -69,10 +71,18 @@ public boolean isComplete() {
stop(runner);
throw new TimeoutException("Failed to start " + liveReloadServer + " within expected time (reason: timeout)");
}
Logger.debug("Server {} started (success={})", liveReloadServer, runner.status.isOK());
return runner;
boolean success = runner.isStatusOk();
Logger.debug("Server {} started (success={})", liveReloadServer, success);
if(success) {
return runner;
}
return null;
}

/**
* Stops the given {@link JettyServerRunner} associated with a Jetty {@link Server}
* @param runner the runner to stop
*/
public static void stop(final JettyServerRunner runner) {
if (runner != null) {
try {
Expand Down Expand Up @@ -162,7 +172,11 @@ boolean isStarted() {
* @return
*/
public boolean isSuccessfullyStarted() {
return isStarted() && (status != null && status.isOK());
return isStarted() && isStatusOk();
}

protected boolean isStatusOk() {
return status != null && status.isOK();
}

/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -137,7 +137,7 @@ public void startServer() throws CoreException {
this.liveReloadServer = new LiveReloadServer(server.getName(), server.getHost(), this.websocketPort, true, allowRemoteConnections,
enableScriptInjection);
this.liveReloadServerRunnable = JettyServerRunner.start(liveReloadServer);
if(!this.liveReloadServerRunnable.isSuccessfullyStarted()) {
if(liveReloadServerRunnable == null || !this.liveReloadServerRunnable.isSuccessfullyStarted()) {
setServerStopped();
} else {
// listen to file changes in the workspace
Expand Down Expand Up @@ -325,25 +325,28 @@ private void startProxy(final IServer startedServer) {
}
// create a new proxy
else {
final int targetPort = WSTUtils.getWebPort(startedServer);
if (targetPort != -1) {
try {
// now, let's init and start the embedded jetty proxy server
// from the
// server attributes
final boolean allowRemoteConnections = isRemoteConnectionsAllowed();
final boolean enableScriptInjection = isScriptInjectionEnabled();
final String proxyHost = getProxyHost();
final int proxyPort = getProxyPort(startedServer);
final LiveReloadProxyServer proxyServer = new LiveReloadProxyServer(proxyHost, proxyPort, startedServer.getHost(), targetPort,
websocketPort, allowRemoteConnections, enableScriptInjection);
proxyServers.put(startedServer, proxyServer);
final JettyServerRunner proxyRunner = JettyServerRunner.start(proxyServer);
try {
final int targetPort = WSTUtils.getWebPort(startedServer);
if (targetPort == -1) {
return;
}
final String targetHost = WSTUtils.getWebHost(startedServer);
// now, let's init and start the embedded jetty proxy server
// from the
// server attributes
final boolean allowRemoteConnections = isRemoteConnectionsAllowed();
final boolean enableScriptInjection = isScriptInjectionEnabled();
final String proxyHost = getProxyHost();
final int proxyPort = getProxyPort(startedServer);
final LiveReloadProxyServer proxyServer = new LiveReloadProxyServer(proxyHost, proxyPort, targetHost, targetPort,
websocketPort, allowRemoteConnections, enableScriptInjection);
proxyServers.put(startedServer, proxyServer);
final JettyServerRunner proxyRunner = JettyServerRunner.start(proxyServer);
if(proxyRunner != null) {
proxyRunners.put(startedServer, proxyRunner);
} catch (Exception e) {
Logger.error("Failed to create or start LiveReload proxy for server " + startedServer.getName(), e);
}

} catch (Exception e) {
Logger.error("Failed to create or start LiveReload proxy for server " + startedServer.getName(), e);
}
}
}
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,62 @@
package org.jboss.tools.livereload.core.internal.util;

/**
* Convenient utility class to build URL path and make sure {@code /} separators
* are properly handled.
*/
public class PathBuilder {

private final StringBuilder path = new StringBuilder();

/**
* Constructor helper
* @param pathFragment the initial path fragment to begin with
* @return a new instance of the {@link PathBuilder}
*/
public static PathBuilder from(final String pathFragment) {
final PathBuilder pathBuilder = new PathBuilder();
return pathBuilder.path(pathFragment);
}

/**
* Adds the given {@code pathFragment} to the current path, while making
* sure that a single {@code '/'} character is used to separate the given
* {@code pathFragment} from the previous ones.
*
* @param pathFragment
* @return this {@link PathBuilder}
*/
public PathBuilder path(final String pathFragment) {
if (pathFragment == null) {
return this;
}
if (!pathFragment.startsWith("/")) {
if (!currentPathEndsWith('/')) {
this.path.append("/").append(pathFragment);
} else if (currentPathEndsWith('/')) {
this.path.append(pathFragment);
}
} else {
if (!currentPathEndsWith('/')) {
this.path.append(pathFragment);
} else if (currentPathEndsWith('/') && pathFragment.length() > 1) {
this.path.append(pathFragment.substring(1));
} else if (this.path.toString().length() == 0) {
this.path.append(pathFragment);
}
}
return this;
}

private boolean currentPathEndsWith(final char lastChar) {
return this.path.length() > 0 && this.path.charAt(this.path.length() - 1) == lastChar;
}

/**
* @return a {@link String} representation of the Path
*/
public String build() {
return this.path.toString();
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@
import org.eclipse.core.runtime.jobs.Job;
import org.eclipse.debug.core.ILaunchManager;
import org.eclipse.jst.server.tomcat.core.internal.TomcatServerBehaviour;
import org.eclipse.wst.server.core.IModule;
import org.eclipse.wst.server.core.IRuntime;
import org.eclipse.wst.server.core.IRuntimeType;
import org.eclipse.wst.server.core.IRuntimeWorkingCopy;
Expand All @@ -42,8 +43,12 @@
import org.eclipse.wst.server.core.ServerCore;
import org.eclipse.wst.server.core.ServerEvent;
import org.eclipse.wst.server.core.ServerPort;
import org.eclipse.wst.server.core.model.IURLProvider;
import org.eclipse.wst.server.core.model.ServerBehaviourDelegate;
import org.jboss.ide.eclipse.as.core.server.IDeployableServer;
import org.jboss.ide.eclipse.as.core.server.IJBossServer;
import org.jboss.ide.eclipse.as.core.server.internal.ExtendedServerPropertiesAdapterFactory;
import org.jboss.ide.eclipse.as.core.server.internal.extendedproperties.ServerExtendedProperties;
import org.jboss.tools.livereload.core.internal.JBossLiveReloadCoreActivator;
import org.jboss.tools.livereload.core.internal.server.jetty.LiveReloadProxyServer;
import org.jboss.tools.livereload.core.internal.server.wst.LiveReloadLaunchConfiguration;
Expand All @@ -59,7 +64,8 @@ public class WSTUtils {

public static final String LIVERELOAD_RUNTIME_TYPE = "org.jboss.tools.livereload.serverTypeRuntime";
public static final String LIVERELOAD_SERVER_TYPE = "org.jboss.tools.livereload.serverType";
public static final String OPENSHIFT_SERVER_TYPE = "org.jboss.tools.openshift.express.openshift.server.type";
public static final String OPENSHIFT_EXPRESS_SERVER_TYPE = "org.jboss.tools.openshift.express.openshift.server.type";
public static final String OPENSHIFT_SERVER_TYPE = "org.jboss.tools.openshift.server.type";
public static final String HTTP_PREVIEW_SERVER_TYPE = "org.eclipse.wst.server.preview.server";
public static final String HTTP_PREVIEW_PORT = "port";
public static final String TEST_PREVIEW_SERVER_TYPE = "org.jboss.tools.livereload.test.previewServerType";
Expand Down Expand Up @@ -185,12 +191,22 @@ public static IServer extractServer(final String browserLocation, final List<ISe
return null;
}

/**
* Retrieves the hostname for the given server
* @param server the server to analyze
* @return the server hostname or <code>null</code> if it could not be determined
* @throws CoreException if something went wrong
*/
public static String getWebHost(final IServer server) throws CoreException {
return server.getHost();
}

/**
* Returns the Web Port for the given {@link IServer}, <code>8080</code> if the server is of an unknown type or <code>-1</code> if
* the server is not started or if the server type is unknown.
*
* @param server
* @return
* @param server the server to analyze
* @return the port on which the server listens
*/
public static int getWebPort(final IServer server) {
// ignore not-started servers and unknown server types
Expand Down Expand Up @@ -508,5 +524,52 @@ public boolean equals(Object obj) {
return true;
}
}

/**
* Builds the URL for to reach a given {@code module} deployed on a given
* {@code IServer}, passing through a proxy listening on the given
* {@code host}:{@code port}.
*
* @param host
* the proxy host
* @param port
* the proxy port
* @param server
* the IServer on which the given {@link IModule} is deployed
* @param module
* the IModule to analyze
* @return the full context for the given {@link IModule} or
* <code>null</code>
* @throws MalformedURLException
*/
public static URL getModuleURL(String host, int port, final IServer server, final IModule module) throws MalformedURLException {
return new URL("http", host, port, PathBuilder.from("/").path(getContextRoot(server, module)).path("/").build());
}

/**
* Gets the context root for the given {@code module} deployed on the given {@code server}.
* @param server the {@link IServer} on which the module is deployed
* @param module the {@link IModule} whose context root is to be found
* @return the specific context root known by the {@link IServer} or the module name if no specific logic is involved.
*/
private static String getContextRoot(final IServer server, final IModule module) {
final IURLProvider urlProvider = getURLProvider(server);
if(urlProvider != null) {
final URL moduleURL = urlProvider.getModuleRootURL(module);
if(moduleURL != null) {
return moduleURL.getPath();
}
}
return module.getName();
}

/**
* Gets the {@link IURLProvider} for the given {@code server}.
* @param server the server to analyze
* @return the associated {@link IURLProvider} or <code>null</code> if none exists.
*/
private static IURLProvider getURLProvider(final IServer server) {
return (IURLProvider) server.loadAdapter(IURLProvider.class, new NullProgressMonitor());
}

}
3 changes: 2 additions & 1 deletion plugins/org.jboss.tools.livereload.ui/META-INF/MANIFEST.MF
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,8 @@ Require-Bundle: org.eclipse.ui,
org.eclipse.core.databinding.property;bundle-version="1.4.200",
org.eclipse.jface.databinding,
org.eclipse.ui.workbench.texteditor;bundle-version="3.9.100",
org.eclipse.ui.ide;bundle-version="3.11.0"
org.eclipse.ui.ide;bundle-version="3.11.0",
org.eclipse.jst.server.core
Bundle-RequiredExecutionEnvironment: JavaSE-1.7
Bundle-ActivationPolicy: lazy
Bundle-ClassPath: lib/core-2.2.jar,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ public boolean test(Object receiver, String property, Object[] args, Object expe
return false;
}
final IServer appServer = appModule.getServer();
return appServer != null && !WSTUtils.OPENSHIFT_SERVER_TYPE.equals(appServer.getServerType().getId());
return appServer != null && !WSTUtils.OPENSHIFT_EXPRESS_SERVER_TYPE.equals(appServer.getServerType().getId());
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -185,7 +185,8 @@ public void run() {
}
});
}
} catch (MalformedURLException | PartInitException e) {
} catch (MalformedURLException | CoreException e) {
MessageDialog.openError(Display.getDefault().getActiveShell(), "LiveReload Error", e.getMessage());
Logger.error("Failed to Open in Web Browser via LiveReload", e);
}
}
Expand All @@ -199,15 +200,15 @@ private static URL computeURL(final IPath file, final IServer liveReloadServer)
return new URL("http", host, port, location.toString());
}

private static URL computeURL(final IServerModule appModule) throws MalformedURLException {
private static URL computeURL(final IServerModule appModule) throws MalformedURLException, CoreException {
final LiveReloadProxyServer liveReloadProxyServer = WSTUtils.findLiveReloadProxyServer(appModule.getServer());
if(liveReloadProxyServer == null) {
return null;
}
final int proxyPort = liveReloadProxyServer.getProxyPort();
final String host = liveReloadProxyServer.getProxyHost();
final URL url = new URL("http", host, proxyPort, "/" + appModule.getModule()[0].getName());
return url;
final URL moduleURL = WSTUtils.getModuleURL(host, proxyPort, appModule.getServer(), appModule.getModule()[0]);
return moduleURL;
}

}
3 changes: 2 additions & 1 deletion tests/org.jboss.tools.livereload.test/META-INF/MANIFEST.MF
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,8 @@ Require-Bundle: org.eclipse.ui,
ch.qos.logback.classic;bundle-version="1.0.0",
ch.qos.logback.core;bundle-version="1.0.0",
org.jboss.tools.locus.mockito;bundle-version="1.9.5",
org.assertj.core;bundle-version="2.1.0"
org.assertj.core;bundle-version="2.1.0",
org.eclipse.jst.server.core;bundle-version="1.2.400"
Bundle-RequiredExecutionEnvironment: JavaSE-1.7
Bundle-ActivationPolicy: lazy
Bundle-ClassPath: .
Loading

0 comments on commit bf3ca4c

Please sign in to comment.