Mercurial > hg > thermostat-ng > web-gateway
changeset 213:cf039006fb0c
Set ServiceConfiguration as an attribute for services to use
Reviewed-by: jerboaa
Review-thread: http://icedtea.classpath.org/pipermail/thermostat/2017-July/024287.html
author | Jie Kang <jkang@redhat.com> |
---|---|
date | Tue, 25 Jul 2017 10:48:10 -0400 |
parents | 7c1e56d64667 |
children | d9afe9c11b06 |
files | common/core/src/main/java/com/redhat/thermostat/gateway/common/core/servlet/GlobalConstants.java common/core/src/main/java/com/redhat/thermostat/gateway/common/core/servlet/ServiceConfigGettingContextListener.java common/core/src/test/java/com/redhat/thermostat/gateway/common/core/servlet/ServiceConfigGettingContextListenerTest.java common/mongodb/src/main/java/com/redhat/thermostat/gateway/common/mongodb/servlet/StorageConnectionSettingListener.java common/mongodb/src/test/java/com/redhat/thermostat/gateway/common/mongodb/servlet/StorageConnectionSettingListenerTest.java server/src/main/java/com/redhat/thermostat/gateway/server/services/WebArchiveCoreService.java server/src/test/java/com/redhat/thermostat/gateway/server/services/WebArchiveCoreServiceTest.java |
diffstat | 7 files changed, 35 insertions(+), 175 deletions(-) [+] |
line wrap: on
line diff
--- a/common/core/src/main/java/com/redhat/thermostat/gateway/common/core/servlet/GlobalConstants.java Tue Jul 25 09:02:08 2017 -0400 +++ b/common/core/src/main/java/com/redhat/thermostat/gateway/common/core/servlet/GlobalConstants.java Tue Jul 25 10:48:10 2017 -0400 @@ -41,4 +41,5 @@ public static final String GATEWAY_HOME_KEY = GATEWAY_PREFIX + ".HOME"; public static final String GATEWAY_HOME_ENV = "THERMOSTAT_GATEWAY_HOME"; public static final String SERVICE_NAME_KEY = GATEWAY_PREFIX + ".SERVICE_NAME"; + public static final String SERVICE_CONFIG_KEY = GATEWAY_PREFIX + ".SERVICE_CONFIG"; }
--- a/common/core/src/main/java/com/redhat/thermostat/gateway/common/core/servlet/ServiceConfigGettingContextListener.java Tue Jul 25 09:02:08 2017 -0400 +++ /dev/null Thu Jan 01 00:00:00 1970 +0000 @@ -1,77 +0,0 @@ -/* - * Copyright 2012-2017 Red Hat, Inc. - * - * This file is part of Thermostat. - * - * Thermostat is free software; you can redistribute it and/or modify - * it under the terms of the GNU General Public License as published - * by the Free Software Foundation; either version 2, or (at your - * option) any later version. - * - * Thermostat is distributed in the hope that it will be useful, but - * WITHOUT ANY WARRANTY; without even the implied warranty of - * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU - * General Public License for more details. - * - * You should have received a copy of the GNU General Public License - * along with Thermostat; see the file COPYING. If not see - * <http://www.gnu.org/licenses/>. - * - * Linking this code with other modules is making a combined work - * based on this code. Thus, the terms and conditions of the GNU - * General Public License cover the whole combination. - * - * As a special exception, the copyright holders of this code give - * you permission to link this code with independent modules to - * produce an executable, regardless of the license terms of these - * independent modules, and to copy and distribute the resulting - * executable under terms of your choice, provided that you also - * meet, for each linked independent module, the terms and conditions - * of the license of that module. An independent module is a module - * which is not derived from or based on this code. If you modify - * this code, you may extend this exception to your version of the - * library, but you are not obligated to do so. If you do not wish - * to do so, delete this exception statement from your version. - */ - -package com.redhat.thermostat.gateway.common.core.servlet; - -import javax.servlet.ServletContext; -import javax.servlet.ServletContextEvent; - -import com.redhat.thermostat.gateway.common.core.config.Configuration; -import com.redhat.thermostat.gateway.common.core.config.ConfigurationFactory; - -public class ServiceConfigGettingContextListener extends GatewayHomeSettingContextListener { - - private ServletContext ctx; - - public ServiceConfigGettingContextListener() { - super(); - } - - ServiceConfigGettingContextListener(EnvHelper helper) { - super(helper); - } - - @Override - public void contextInitialized(ServletContextEvent sce) { - super.contextInitialized(sce); - ctx = sce.getServletContext(); - } - - @Override - public void contextDestroyed(ServletContextEvent sce) { - super.contextDestroyed(sce); - ctx = null; - } - - public Configuration getServiceConfig(String serviceName) { - String gatewayHome; - synchronized (ctx) { - gatewayHome = (String)ctx.getAttribute(GlobalConstants.GATEWAY_HOME_KEY); - } - ConfigurationFactory factory = new ConfigurationFactory(gatewayHome); - return factory.createServiceConfiguration(serviceName); - } -}
--- a/common/core/src/test/java/com/redhat/thermostat/gateway/common/core/servlet/ServiceConfigGettingContextListenerTest.java Tue Jul 25 09:02:08 2017 -0400 +++ /dev/null Thu Jan 01 00:00:00 1970 +0000 @@ -1,93 +0,0 @@ -/* - * Copyright 2012-2017 Red Hat, Inc. - * - * This file is part of Thermostat. - * - * Thermostat is free software; you can redistribute it and/or modify - * it under the terms of the GNU General Public License as published - * by the Free Software Foundation; either version 2, or (at your - * option) any later version. - * - * Thermostat is distributed in the hope that it will be useful, but - * WITHOUT ANY WARRANTY; without even the implied warranty of - * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU - * General Public License for more details. - * - * You should have received a copy of the GNU General Public License - * along with Thermostat; see the file COPYING. If not see - * <http://www.gnu.org/licenses/>. - * - * Linking this code with other modules is making a combined work - * based on this code. Thus, the terms and conditions of the GNU - * General Public License cover the whole combination. - * - * As a special exception, the copyright holders of this code give - * you permission to link this code with independent modules to - * produce an executable, regardless of the license terms of these - * independent modules, and to copy and distribute the resulting - * executable under terms of your choice, provided that you also - * meet, for each linked independent module, the terms and conditions - * of the license of that module. An independent module is a module - * which is not derived from or based on this code. If you modify - * this code, you may extend this exception to your version of the - * library, but you are not obligated to do so. If you do not wish - * to do so, delete this exception statement from your version. - */ - -package com.redhat.thermostat.gateway.common.core.servlet; - -import static org.junit.Assert.assertEquals; -import static org.mockito.ArgumentMatchers.eq; -import static org.mockito.Mockito.mock; -import static org.mockito.Mockito.verify; -import static org.mockito.Mockito.when; - -import java.util.HashMap; -import java.util.Map; - -import javax.servlet.ServletContext; -import javax.servlet.ServletContextEvent; - -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.ConfigurationTest; -import com.redhat.thermostat.gateway.common.core.servlet.GatewayHomeSettingContextListener.EnvHelper; - -public class ServiceConfigGettingContextListenerTest extends ConfigurationTest { - - private ServletContext ctxt; - private ServletContextEvent evt; - private EnvHelper mockEnv; - private String testRoot; - - @Before - public void setup() { - evt = mock(ServletContextEvent.class); - ctxt = mock(ServletContext.class); - when(evt.getServletContext()).thenReturn(ctxt); - mockEnv = mock(EnvHelper.class); - testRoot = getTestRoot(); - when(mockEnv.getEnv(eq(GlobalConstants.GATEWAY_HOME_ENV))).thenReturn(testRoot); - } - - @Test - public void canGetServiceConfigViaListener() { - String serviceName = "test-service"; - Map<String, Object> expected = new HashMap<String, Object>(); - expected.put("foo", "service-value"); - expected.put("test", "me"); - expected.put("bar", "baz"); - Map<String, Object> servicesConfig = new HashMap<String, Object>(); - servicesConfig.put("/service1", "/path/to/microservice.war"); - expected.put("SERVICES", servicesConfig); - ServiceConfigGettingContextListener listener = new ServiceConfigGettingContextListener(mockEnv); - listener.contextInitialized(evt); - verify(ctxt).setAttribute(eq(GlobalConstants.GATEWAY_HOME_KEY), eq(testRoot)); - when(ctxt.getAttribute(eq(GlobalConstants.GATEWAY_HOME_KEY))).thenReturn(testRoot); - Configuration config = listener.getServiceConfig(serviceName); - Map<String, Object> actual = config.asMap(); - assertEquals(expected, actual); - } -}
--- a/common/mongodb/src/main/java/com/redhat/thermostat/gateway/common/mongodb/servlet/StorageConnectionSettingListener.java Tue Jul 25 09:02:08 2017 -0400 +++ b/common/mongodb/src/main/java/com/redhat/thermostat/gateway/common/mongodb/servlet/StorageConnectionSettingListener.java Tue Jul 25 10:48:10 2017 -0400 @@ -44,11 +44,11 @@ import javax.servlet.ServletContextEvent; import com.redhat.thermostat.gateway.common.core.config.Configuration; +import com.redhat.thermostat.gateway.common.core.servlet.GatewayHomeSettingContextListener; import com.redhat.thermostat.gateway.common.core.servlet.GlobalConstants; -import com.redhat.thermostat.gateway.common.core.servlet.ServiceConfigGettingContextListener; import com.redhat.thermostat.gateway.common.mongodb.ThermostatMongoStorage; -public class StorageConnectionSettingListener extends ServiceConfigGettingContextListener { +public class StorageConnectionSettingListener extends GatewayHomeSettingContextListener { @Override public void contextDestroyed(ServletContextEvent event) { @@ -71,8 +71,10 @@ } Map<String, String> getMongoStorageConfig(ServletContext ctx) { - String serviceName = ctx.getInitParameter(GlobalConstants.SERVICE_NAME_KEY); - Configuration serviceConfig = getServiceConfig(serviceName); + Configuration serviceConfig = (Configuration) ctx.getAttribute(GlobalConstants.SERVICE_CONFIG_KEY); + if (serviceConfig == null) { + throw new IllegalStateException("No service configuration for service: " + ctx.getInitParameter(GlobalConstants.SERVICE_NAME_KEY)); + } Configuration mongoConfiguration = new MongoConfigurationAdapter(serviceConfig); return convert(mongoConfiguration.asMap()); }
--- a/common/mongodb/src/test/java/com/redhat/thermostat/gateway/common/mongodb/servlet/StorageConnectionSettingListenerTest.java Tue Jul 25 09:02:08 2017 -0400 +++ b/common/mongodb/src/test/java/com/redhat/thermostat/gateway/common/mongodb/servlet/StorageConnectionSettingListenerTest.java Tue Jul 25 10:48:10 2017 -0400 @@ -54,6 +54,8 @@ 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.common.mongodb.ThermostatMongoStorage; import com.redhat.thermostat.gateway.common.mongodb.configuration.MongoConfiguration; @@ -70,7 +72,7 @@ when(evt.getServletContext()).thenReturn(ctxt); when(ctxt.getInitParameter(eq(GlobalConstants.GATEWAY_HOME_KEY))).thenReturn(getTestGatewayRoot()); when(ctxt.getAttribute(eq(GlobalConstants.GATEWAY_HOME_KEY))).thenReturn(getTestGatewayRoot()); - when(ctxt.getInitParameter(eq(GlobalConstants.SERVICE_NAME_KEY))).thenReturn("foo-service"); + when(ctxt.getAttribute(eq(GlobalConstants.SERVICE_CONFIG_KEY))).thenReturn(getTestGatewayConfiguration()); } @Test @@ -97,6 +99,10 @@ return decodeFilePath(rootUrl); } + private Configuration getTestGatewayConfiguration() { + return new ConfigurationFactory(getTestGatewayRoot()).createServiceConfiguration("foo-service"); + } + private String decodeFilePath(URL url) { try { // Spaces are encoded as %20 in URLs. Use URLDecoder.decode() so
--- a/server/src/main/java/com/redhat/thermostat/gateway/server/services/WebArchiveCoreService.java Tue Jul 25 09:02:08 2017 -0400 +++ b/server/src/main/java/com/redhat/thermostat/gateway/server/services/WebArchiveCoreService.java Tue Jul 25 10:48:10 2017 -0400 @@ -58,6 +58,7 @@ import com.redhat.thermostat.gateway.common.core.config.Configuration; import com.redhat.thermostat.gateway.common.core.config.ServiceConfiguration; +import com.redhat.thermostat.gateway.common.core.servlet.GlobalConstants; import com.redhat.thermostat.gateway.server.auth.basic.BasicLoginService; import com.redhat.thermostat.gateway.server.auth.basic.BasicUserStore; import com.redhat.thermostat.gateway.server.auth.keycloak.KeycloakConfiguration; @@ -82,6 +83,10 @@ webAppContext.setContextPath(contextPath); webAppContext.setWar(warPath); + + webAppContext.setAttribute(GlobalConstants.SERVICE_CONFIG_KEY, serviceConfig); + webAppContext.addSystemClass(Configuration.class.getName()); + initializeWebSockets(server, webAppContext); setupAuthForContext(webAppContext);
--- a/server/src/test/java/com/redhat/thermostat/gateway/server/services/WebArchiveCoreServiceTest.java Tue Jul 25 09:02:08 2017 -0400 +++ b/server/src/test/java/com/redhat/thermostat/gateway/server/services/WebArchiveCoreServiceTest.java Tue Jul 25 10:48:10 2017 -0400 @@ -37,6 +37,7 @@ package com.redhat.thermostat.gateway.server.services; import static org.junit.Assert.assertEquals; +import static org.junit.Assert.assertSame; import static org.junit.Assert.assertTrue; import static org.mockito.Mockito.mock; import static org.mockito.Mockito.when; @@ -58,6 +59,7 @@ import com.redhat.thermostat.gateway.common.core.config.Configuration; import com.redhat.thermostat.gateway.common.core.config.ServiceConfiguration; +import com.redhat.thermostat.gateway.common.core.servlet.GlobalConstants; import com.redhat.thermostat.gateway.server.auth.basic.BasicLoginService; import com.redhat.thermostat.gateway.server.auth.keycloak.KeycloakRequestFilter; @@ -92,6 +94,20 @@ } @Test + public void testServiceConfigIsAdded() { + Map<String, Object> configurationMap = new HashMap<>(); + + Configuration configuration = mock(Configuration.class); + when(configuration.asMap()).thenReturn(configurationMap); + + WebArchiveCoreService service = new WebArchiveCoreService(contextPath, warPath, configuration); + ServletContextHandler servletContextHandler = service.createServletContextHandler(mock(Server.class)); + WebAppContext webAppContext = (WebAppContext) servletContextHandler; + + assertSame(configuration, webAppContext.getAttribute(GlobalConstants.SERVICE_CONFIG_KEY)); + } + + @Test public void testServiceWithKeycloakAuth() { Map<String, Object> configurationMap = new HashMap<>(); configurationMap.put(ServiceConfiguration.ConfigurationKey.SECURITY_KEYCLOAK.name(), "true");