diff --git a/micro-boot/build.gradle b/micro-boot/build.gradle index 230676931..67473c7f0 100644 --- a/micro-boot/build.gradle +++ b/micro-boot/build.gradle @@ -5,7 +5,11 @@ dependencies { compile 'org.springframework.boot:spring-boot:1.2.1.RELEASE' compile 'org.springframework.boot:spring-boot-autoconfigure:1.2.1.RELEASE' //compile project(':micro-core') +<<<<<<< HEAD compile group: 'com.aol.microservices', name:'microserver-core', version:'0.55' +======= + compile group: 'com.aol.microservices', name:'microserver-core', version:'0.54' +>>>>>>> 3521be673c00ffe96533acc6ff62127cc80bc0a7 } diff --git a/micro-core/src/main/java/com/aol/micro/server/module/ConfigurableModule.java b/micro-core/src/main/java/com/aol/micro/server/module/ConfigurableModule.java index 0fe643383..32ac3680c 100644 --- a/micro-core/src/main/java/com/aol/micro/server/module/ConfigurableModule.java +++ b/micro-core/src/main/java/com/aol/micro/server/module/ConfigurableModule.java @@ -4,12 +4,15 @@ import java.util.List; import java.util.Map; import java.util.Set; +import java.util.function.Consumer; import java.util.function.Supplier; import javax.servlet.Filter; import javax.servlet.Servlet; import javax.servlet.ServletContextListener; +import org.glassfish.grizzly.http.server.HttpServer; + import lombok.AllArgsConstructor; import lombok.experimental.Builder; import lombok.experimental.Wither; @@ -38,8 +41,17 @@ public class ConfigurableModule implements Module{ private final Set springConfigurationClasses; private final Map propertyOverrides; private final List defaultJaxRsPackages; + private final Consumer serverConfigManager; private final boolean resetAll; + @Override + public Consumer getServerConfigManager(){ + if(serverConfigManager!=null) + return serverConfigManager; + + return Module.super.getServerConfigManager(); + } + @Override public List getDefaultJaxRsPackages() { if(defaultJaxRsPackages!=null) diff --git a/micro-core/src/main/java/com/aol/micro/server/module/Environment.java b/micro-core/src/main/java/com/aol/micro/server/module/Environment.java index 49e6d1571..148a9e087 100644 --- a/micro-core/src/main/java/com/aol/micro/server/module/Environment.java +++ b/micro-core/src/main/java/com/aol/micro/server/module/Environment.java @@ -1,11 +1,15 @@ package com.aol.micro.server.module; +import java.net.InetAddress; import java.util.Collection; import java.util.Map; import java.util.Optional; import java.util.Properties; import java.util.stream.Collectors; +import org.jooq.lambda.fi.util.function.CheckedSupplier; + +import com.aol.simple.react.exceptions.ExceptionSoftener; import com.google.common.collect.ImmutableMap; import com.google.common.collect.Maps; @@ -34,12 +38,21 @@ public void assureModule(Module module) { if (!modulePort.containsKey(module.getContext())) { Map builder = Maps.newHashMap(); builder.putAll(modulePort); - builder.put(module.getContext(), ModuleBean.builder().port(getPort(module)).build()); + builder.put(module.getContext(), ModuleBean.builder().host(getHost(module)).port(getPort(module)).build()); modulePort = ImmutableMap.copyOf(builder); } } + private String getHost(Module module) { + CheckedSupplier s = ()->InetAddress.getLocalHost().getHostName(); + try{ + return Optional.ofNullable(properties.get(module.getContext() + ".host")).orElse(s.get()).toString(); + }catch(Throwable e){ + throw new RuntimeException(e); + } + } + private int getPort(Module module) { return Integer.valueOf(Optional.ofNullable(properties.get(module.getContext() + ".port")).orElse(nextPort++).toString()); diff --git a/micro-core/src/main/java/com/aol/micro/server/module/Module.java b/micro-core/src/main/java/com/aol/micro/server/module/Module.java index aa8d44339..3189154eb 100644 --- a/micro-core/src/main/java/com/aol/micro/server/module/Module.java +++ b/micro-core/src/main/java/com/aol/micro/server/module/Module.java @@ -5,11 +5,13 @@ import java.util.List; import java.util.Map; import java.util.Set; +import java.util.function.Consumer; import javax.servlet.Filter; import javax.servlet.Servlet; import javax.servlet.ServletContextListener; +import org.glassfish.grizzly.http.server.HttpServer; import org.springframework.web.context.ContextLoaderListener; import org.springframework.web.context.WebApplicationContext; @@ -32,6 +34,9 @@ public interface Module { + default Consumer getServerConfigManager(){ + return server->{}; + } default List getPackages(){ return ImmutableList.of(); } diff --git a/micro-core/src/main/java/com/aol/micro/server/servers/grizzly/GrizzlyApplication.java b/micro-core/src/main/java/com/aol/micro/server/servers/grizzly/GrizzlyApplication.java index 6018dc146..a79ed9a1a 100644 --- a/micro-core/src/main/java/com/aol/micro/server/servers/grizzly/GrizzlyApplication.java +++ b/micro-core/src/main/java/com/aol/micro/server/servers/grizzly/GrizzlyApplication.java @@ -69,7 +69,7 @@ public void run(CompletableFuture start, CompletableFuture end) { addListeners(webappContext); HttpServer httpServer = HttpServer.createSimpleServer(null, "0.0.0.0", serverData.getPort()); - + serverData.getModule().getServerConfigManager().accept(httpServer); addAccessLog(httpServer); if (SSLProperties != null) this.createSSLListener(serverData.getPort()); diff --git a/micro-core/src/test/java/app/single/serverconfig/com/aol/micro/server/SingleClassTest.java b/micro-core/src/test/java/app/single/serverconfig/com/aol/micro/server/SingleClassTest.java new file mode 100644 index 000000000..af9af9878 --- /dev/null +++ b/micro-core/src/test/java/app/single/serverconfig/com/aol/micro/server/SingleClassTest.java @@ -0,0 +1,64 @@ +package app.single.serverconfig.com.aol.micro.server; + +import static org.hamcrest.CoreMatchers.is; +import static org.junit.Assert.*; + +import java.util.concurrent.ExecutionException; + +import javax.ws.rs.GET; +import javax.ws.rs.Path; +import javax.ws.rs.Produces; + +import org.junit.After; +import org.junit.Before; +import org.junit.Test; + +import com.aol.micro.server.MicroserverApp; +import com.aol.micro.server.auto.discovery.RestResource; +import com.aol.micro.server.config.Microserver; +import com.aol.micro.server.module.ConfigurableModule; +import com.aol.micro.server.testing.RestAgent; + + +@Path("/single") +public class SingleClassTest implements RestResource{ + + RestAgent rest = new RestAgent(); + + boolean called; + MicroserverApp server; + @Before + public void startServer(){ + called = false; + server = new MicroserverApp( ConfigurableModule.builder() + .context("hello") + .serverConfigManager(server->called=true) + .build()); + server.start(); + + } + + @After + public void stopServer(){ + server.stop(); + } + + @Test + public void runAppAndBasicTest() throws InterruptedException, ExecutionException{ + + + + assertThat(rest.get("http://localhost:8080/hello/single/ping"),is("ok")); + assertTrue(called); + + } + + @GET + @Produces("text/plain") + @Path("/ping") + public String ping() { + return "ok"; + } + + +} \ No newline at end of file diff --git a/micro-core/src/test/java/com/aol/micro/server/module/ConfigurableModuleTest.java b/micro-core/src/test/java/com/aol/micro/server/module/ConfigurableModuleTest.java index 5a013e75a..40842ff5e 100644 --- a/micro-core/src/test/java/com/aol/micro/server/module/ConfigurableModuleTest.java +++ b/micro-core/src/test/java/com/aol/micro/server/module/ConfigurableModuleTest.java @@ -2,18 +2,20 @@ import static org.hamcrest.Matchers.hasItem; import static org.hamcrest.Matchers.is; -import static org.junit.Assert.assertThat; +import static org.junit.Assert.*; import java.util.ArrayList; import java.util.HashMap; import java.util.List; import java.util.Map; import java.util.Set; +import java.util.function.Consumer; import javax.servlet.Filter; import javax.servlet.Servlet; import javax.servlet.ServletContextListener; +import org.glassfish.grizzly.http.server.HttpServer; import org.junit.Before; import org.junit.Test; @@ -40,6 +42,7 @@ public class ConfigurableModuleTest { private List defaultJaxRsPackages; private Module m = () -> "module"; + Consumer serverConfigManager = server-> {}; @Before public void setup(){ @@ -55,7 +58,9 @@ public void setup(){ servlets = new HashMap<>(); springConfigurationClasses = ImmutableSet.of(this.getClass()); + module = ConfigurableModule.builder() + .serverConfigManager(serverConfigManager ) .defaultJaxRsPackages(defaultJaxRsPackages) .context(context) .defaultResources(defaultResources) @@ -73,6 +78,19 @@ public void setup(){ .build(); } @Test + public void testGetServerConfigManager() { + assertThat(module.withResetAll(true).getServerConfigManager(),is(serverConfigManager)); + } + @Test + public void testGetServerConfigManagerNull() { + try { + module.withServerConfigManager(null) + .getServerConfigManager().accept(null); + }catch(Exception e){ + fail(e.getMessage()); + } + } + @Test public void testGetRestResourceClassesResetAll() { assertThat(module.withResetAll(true).getRestResourceClasses(),is(resourceClasses)); } diff --git a/micro-core/src/test/java/com/aol/micro/server/module/EnvironmentTest.java b/micro-core/src/test/java/com/aol/micro/server/module/EnvironmentTest.java index 727ce297d..da53eb332 100644 --- a/micro-core/src/test/java/com/aol/micro/server/module/EnvironmentTest.java +++ b/micro-core/src/test/java/com/aol/micro/server/module/EnvironmentTest.java @@ -1,8 +1,10 @@ package com.aol.micro.server.module; -import static org.hamcrest.CoreMatchers.is; +import static org.hamcrest.CoreMatchers.*; import static org.junit.Assert.assertThat; +import java.net.InetAddress; +import java.net.UnknownHostException; import java.util.Properties; import org.junit.Test; @@ -37,4 +39,28 @@ public void testGetModuleBeanOverridePort() { environment.assureModule(() ->"context"); assertThat(environment.getModuleBean(()-> "context").getPort(), is(8081)); } + + @Test + public void testDefaultHost() throws UnknownHostException { + String host =InetAddress.getLocalHost().getHostName(); + Environment environment = new Environment(new Properties()); + environment.assureModule(() ->"context"); + assertThat(environment.getModuleBean(()-> "context").getHost(), is(host)); + } + @Test + public void testDefaultHostNotNull() throws UnknownHostException { + + Environment environment = new Environment(new Properties()); + environment.assureModule(() ->"context"); + assertThat(environment.getModuleBean(()-> "context").getHost(), is(not(nullValue()))); + } + @Test + public void testHostOverride() throws UnknownHostException { + + Properties props = new Properties(); + props.put("context.host", "overriden-host"); + Environment environment = new Environment(props); + environment.assureModule(() ->"context"); + assertThat(environment.getModuleBean(()-> "context").getHost(), is("overriden-host")); + } } diff --git a/micro-core/src/test/java/com/aol/micro/server/servers/ServerRunnerTest.java b/micro-core/src/test/java/com/aol/micro/server/servers/ServerRunnerTest.java index 27424eb36..80c25bbbc 100644 --- a/micro-core/src/test/java/com/aol/micro/server/servers/ServerRunnerTest.java +++ b/micro-core/src/test/java/com/aol/micro/server/servers/ServerRunnerTest.java @@ -22,8 +22,8 @@ public class ServerRunnerTest { private GrizzlyApplication serverApplication1; private GrizzlyApplication serverApplication2; private ServerData[] registered; - int server1Count =0; - int server2Count =0; + volatile int server1Count =0; + volatile int server2Count =0; @Before public void setUp() { @@ -35,14 +35,16 @@ public void setUp() { serverApplication1 = new GrizzlyApplication(AllData.builder().serverData(data1).build()){ public void run(CompletableFuture start,CompletableFuture end) { - start.complete(true); server1Count++; + start.complete(true); + } }; serverApplication2 = new GrizzlyApplication(AllData.builder().serverData(data2).build()){ public void run(CompletableFuture start,CompletableFuture end) { - start.complete(true); server2Count++; + start.complete(true); + } };