Mercurial > hg > thermostat-ng > web-gateway
changeset 154:b1bdc9e324c3
Resolve services war file in code.
Reviewed-by: jkang
Review-thread: http://icedtea.classpath.org/pipermail/thermostat/2017-May/023186.html
author | Severin Gehwolf <sgehwolf@redhat.com> |
---|---|
date | Thu, 18 May 2017 16:15:15 +0200 |
parents | de6a3c5c2321 |
children | aba9dff6a61a |
files | distribution/src/bin/thermostat-web-gateway.sh distribution/src/etc/services.properties distribution/src/windows/bin/thermostat-web-gateway.cmd server/src/main/java/com/redhat/thermostat/gateway/server/services/WebArchiveServiceBuilder.java server/src/test/java/com/redhat/thermostat/gateway/server/services/WebArchiveServiceBuilderTest.java |
diffstat | 5 files changed, 85 insertions(+), 30 deletions(-) [+] |
line wrap: on
line diff
--- a/distribution/src/bin/thermostat-web-gateway.sh Thu May 18 11:33:18 2017 -0400 +++ b/distribution/src/bin/thermostat-web-gateway.sh Thu May 18 16:15:15 2017 +0200 @@ -68,19 +68,5 @@ THERMOSTAT_GATEWAY_LIBS=${THERMOSTAT_GATEWAY_HOME}/libs -THERMOSTAT_GATEWAY_ETC=${THERMOSTAT_GATEWAY_HOME}/etc - -THERMOSTAT_GATEWAY_CONFIG=${THERMOSTAT_GATEWAY_ETC}/services.properties - -if [ $CYGWIN_MODE -eq 1 ]; then - THERMOSTAT_GATEWAY_SERVICES=`echo ${THERMOSTAT_GATEWAY_HOME}/services | tr \\\\ /` -else - THERMOSTAT_GATEWAY_SERVICES=`echo ${THERMOSTAT_GATEWAY_HOME}/services` -fi - -echo TGS = %THERMOSTAT_GATEWAY_SERVICES% - -sed -i -e "s|__SERVICES__|${THERMOSTAT_GATEWAY_SERVICES}|g" ${THERMOSTAT_GATEWAY_CONFIG} - export THERMOSTAT_GATEWAY_HOME java -cp "${THERMOSTAT_GATEWAY_LIBS}/*" com.redhat.thermostat.gateway.server.Start
--- a/distribution/src/etc/services.properties Thu May 18 11:33:18 2017 -0400 +++ b/distribution/src/etc/services.properties Thu May 18 16:15:15 2017 +0200 @@ -1,4 +1,4 @@ -/jvm-memory = __SERVICES__/thermostat-web-gateway-service-jvm-memory-@project.version@.war -/jvm-gc = __SERVICES__/thermostat-web-gateway-service-jvm-gc-@project.version@.war -/commands = __SERVICES__/thermostat-web-gateway-service-commands-@project.version@.war -/white-pages = __SERVICES__/thermostat-web-gateway-service-white-pages-@project.version@.war +/jvm-memory = thermostat-web-gateway-service-jvm-memory-@project.version@.war +/jvm-gc = thermostat-web-gateway-service-jvm-gc-@project.version@.war +/commands = thermostat-web-gateway-service-commands-@project.version@.war +/white-pages = thermostat-web-gateway-service-white-pages-@project.version@.war
--- a/distribution/src/windows/bin/thermostat-web-gateway.cmd Thu May 18 11:33:18 2017 -0400 +++ b/distribution/src/windows/bin/thermostat-web-gateway.cmd Thu May 18 16:15:15 2017 +0200 @@ -41,12 +41,4 @@ set THERMOSTAT_GATEWAY_LIBS=%THERMOSTAT_GATEWAY_HOME%\libs -set THERMOSTAT_GATEWAY_ETC=%THERMOSTAT_GATEWAY_HOME%\etc - -set THERMOSTAT_GATEWAY_CONFIG=%THERMOSTAT_GATEWAY_ETC%\services.properties - -set THERMOSTAT_GATEWAY_SERVICES=%THERMOSTAT_GATEWAY_HOME%\services - -powershell -command "(get-content %THERMOSTAT_GATEWAY_CONFIG%).replace('__SERVICES__','%THERMOSTAT_GATEWAY_SERVICES%').replace('\','/').replace('//','/') | set-content %THERMOSTAT_GATEWAY_CONFIG%" - java -cp "%THERMOSTAT_GATEWAY_LIBS%/*" com.redhat.thermostat.gateway.server.Start
--- a/server/src/main/java/com/redhat/thermostat/gateway/server/services/WebArchiveServiceBuilder.java Thu May 18 11:33:18 2017 -0400 +++ b/server/src/main/java/com/redhat/thermostat/gateway/server/services/WebArchiveServiceBuilder.java Thu May 18 16:15:15 2017 +0200 @@ -36,20 +36,32 @@ package com.redhat.thermostat.gateway.server.services; +import java.nio.file.Path; +import java.nio.file.Paths; import java.util.ArrayList; import java.util.List; import java.util.Map; import com.redhat.thermostat.gateway.common.core.config.Configuration; import com.redhat.thermostat.gateway.common.core.config.ConfigurationFactory; +import com.redhat.thermostat.gateway.common.core.servlet.GlobalConstants; class WebArchiveServiceBuilder extends BasicServiceBuilder { + private static final String SERVICES_FOLDER = "services"; private static final String CONTEXT_PREFIX = "/"; private final ConfigurationFactory configFactory; + private final EnvHelper envHelper; + private final PathHelper pathHelper; public WebArchiveServiceBuilder(ConfigurationFactory configFactory) { + this(configFactory, new EnvHelper(), new PathHelper()); + } + + WebArchiveServiceBuilder(ConfigurationFactory configFactory, EnvHelper envHelper, PathHelper pathHelper) { this.configFactory = configFactory; + this.envHelper = envHelper; + this.pathHelper = pathHelper; } @Override @@ -61,7 +73,8 @@ String contextPath = entry.getKey(); String serviceName = getServiceName(contextPath); Configuration config = configFactory.createServiceConfiguration(serviceName); - String warPath = entry.getValue().toString(); + String warPathCandidate = entry.getValue().toString(); + String warPath = getAbsolutePathForService(warPathCandidate); WebArchiveCoreService service = new WebArchiveCoreService(contextPath, warPath, config); serviceList.add(service); } @@ -73,4 +86,32 @@ return contextPath.substring(CONTEXT_PREFIX.length()); } + String getAbsolutePathForService(String path) { + if (pathHelper.isAbsolute(path)) { + return path; + } + // assume service web archive file is relative to + // THERMOSTAT_GATEWAY_HOME/services + String gwHome = envHelper.getEnv(GlobalConstants.GATEWAY_HOME_ENV); + Path basePath = Paths.get(gwHome, SERVICES_FOLDER, path); + return basePath.toAbsolutePath().toString(); + } + + static class EnvHelper { + + String getEnv(String var) { + return System.getenv(var); + } + + } + + static class PathHelper { + + boolean isAbsolute(String path) { + Path pathObj = Paths.get(path); + return pathObj.isAbsolute(); + } + + } + }
--- a/server/src/test/java/com/redhat/thermostat/gateway/server/services/WebArchiveServiceBuilderTest.java Thu May 18 11:33:18 2017 -0400 +++ b/server/src/test/java/com/redhat/thermostat/gateway/server/services/WebArchiveServiceBuilderTest.java Thu May 18 16:15:15 2017 +0200 @@ -39,25 +39,43 @@ import static org.junit.Assert.assertEquals; import static org.junit.Assert.assertTrue; import static org.mockito.ArgumentMatchers.anyString; +import static org.mockito.ArgumentMatchers.eq; import static org.mockito.Mockito.mock; import static org.mockito.Mockito.when; +import java.io.File; import java.util.HashMap; import java.util.List; import java.util.Map; +import org.junit.Before; import org.junit.Test; import com.redhat.thermostat.gateway.common.core.config.Configuration; import com.redhat.thermostat.gateway.common.core.config.ConfigurationFactory; +import com.redhat.thermostat.gateway.common.core.servlet.GlobalConstants; +import com.redhat.thermostat.gateway.server.services.WebArchiveServiceBuilder.EnvHelper; +import com.redhat.thermostat.gateway.server.services.WebArchiveServiceBuilder.PathHelper; public class WebArchiveServiceBuilderTest { + + private EnvHelper envHelper; + private PathHelper pathHelper; + private ConfigurationFactory configFactory; + + @Before + public void setup() { + envHelper = mock(EnvHelper.class); + pathHelper = mock(PathHelper.class); + when(pathHelper.isAbsolute(anyString())).thenReturn(true); + configFactory = mock(ConfigurationFactory.class); + when(configFactory.createServiceConfiguration(anyString())).thenReturn(mock(Configuration.class)); + } + @Test public void testBuildWebArchiveCoreServices() { - ConfigurationFactory configurationFactory = mock(ConfigurationFactory.class); - when(configurationFactory.createServiceConfiguration(anyString())).thenReturn(mock(Configuration.class)); - WebArchiveServiceBuilder serviceBuilder = new WebArchiveServiceBuilder(configurationFactory); + WebArchiveServiceBuilder serviceBuilder = new WebArchiveServiceBuilder(configFactory, envHelper, pathHelper); Configuration configuration = mock(Configuration.class); Map<String, Object> configMap = new HashMap<>(); configMap.put("/context0", "serviceName0"); @@ -73,5 +91,23 @@ assertTrue(services.get(1) instanceof WebArchiveCoreService); } + @Test + public void canResolveRelativeServiceWarFile() { + String mockGwHome = "i-do-not-exist-gw-home"; + EnvHelper customEnv = mock(EnvHelper.class); + when(customEnv.getEnv(eq(GlobalConstants.GATEWAY_HOME_ENV))).thenReturn(mockGwHome); + WebArchiveServiceBuilder serviceBuilder = new WebArchiveServiceBuilder(configFactory, customEnv, new PathHelper()); + String result = serviceBuilder.getAbsolutePathForService("foobar"); + String expected = new File(new File(mockGwHome, "services"), "foobar").getAbsolutePath(); + assertEquals(expected, result); + } + @Test + public void keepsAbsoluteServiceWarFile() { + EnvHelper env = mock(EnvHelper.class); + WebArchiveServiceBuilder serviceBuilder = new WebArchiveServiceBuilder(configFactory, env, new PathHelper()); + String orig = "/abs/path"; + String result = serviceBuilder.getAbsolutePathForService(orig); + assertEquals(orig, result); + } }