From 2444b52ae696342045f35f4b4659e856143101e7 Mon Sep 17 00:00:00 2001 From: Ke Wang Date: Wed, 10 Jun 2015 12:18:38 +0100 Subject: [PATCH 1/4] Fix for issue access log issue --- .../com/aol/micro/server/config/Classes.java | 3 ++- .../micro/server/servers/AccessLogConfig.java | 19 +++++++++++++++++++ .../servers/grizzly/GrizzlyApplication.java | 4 ++-- .../spring/SpringApplicationConfigurator.java | 13 ------------- 4 files changed, 23 insertions(+), 16 deletions(-) create mode 100644 micro-core/src/main/java/com/aol/micro/server/servers/AccessLogConfig.java diff --git a/micro-core/src/main/java/com/aol/micro/server/config/Classes.java b/micro-core/src/main/java/com/aol/micro/server/config/Classes.java index 2b795f297..21b48f02c 100644 --- a/micro-core/src/main/java/com/aol/micro/server/config/Classes.java +++ b/micro-core/src/main/java/com/aol/micro/server/config/Classes.java @@ -8,6 +8,7 @@ import com.aol.micro.server.events.ConfigureActiveJobsAspect; import com.aol.micro.server.module.ConfigureEnviroment; import com.aol.micro.server.rest.resources.ConfigureResources; +import com.aol.micro.server.servers.AccessLogConfig; import com.aol.micro.server.spring.datasource.DataSourceBuilder; import com.aol.micro.server.spring.datasource.JdbcConfig; import com.aol.micro.server.spring.datasource.hibernate.DAOProvider; @@ -36,7 +37,7 @@ public enum Classes { * Codahale Metrics, Event tracking etc */ CORE_CLASSES(PropertyFileConfig.class, MiscellaneousConfig.class, AopConfig.class, CodahaleMetricsConfigurer.class, - ConfigureActiveJobsAspect.class, ScheduleAndAsyncConfig.class, ConfigureResources.class, ConfigureEnviroment.class), + ConfigureActiveJobsAspect.class, ScheduleAndAsyncConfig.class, ConfigureResources.class, ConfigureEnviroment.class, AccessLogConfig.class), JDBC_CLASSES(JdbcConfig.class, DAOProvider.class, DataSourceBuilder.class, SQL.class, SpringDataConfig.class), ROMA_ROW_MAPPER(RomaRowMapperConfig.class), HIBERNATE_CLASSES(HibernateConfig.class, JdbcConfig.class, GenericHibernateService.class, DAOProvider.class, DataSourceBuilder.class, SQL.class), diff --git a/micro-core/src/main/java/com/aol/micro/server/servers/AccessLogConfig.java b/micro-core/src/main/java/com/aol/micro/server/servers/AccessLogConfig.java new file mode 100644 index 000000000..7f7c41f46 --- /dev/null +++ b/micro-core/src/main/java/com/aol/micro/server/servers/AccessLogConfig.java @@ -0,0 +1,19 @@ +package com.aol.micro.server.servers; + +import org.springframework.beans.factory.annotation.Value; +import org.springframework.context.annotation.Bean; +import org.springframework.context.annotation.Configuration; + +import com.aol.micro.server.servers.AccessLogLocationBean; + +@Configuration +public class AccessLogConfig { + + @Value("${access.log.output:./logs/}") + private String accessLogLocation; + + @Bean + public AccessLogLocationBean accessLogLocationBean() { + return new AccessLogLocationBean(accessLogLocation); + } +} 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 b8939b8d1..b9a15ddf0 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 @@ -61,7 +61,7 @@ public void run(CompletableFuture start, CompletableFuture end) { WebappContext webappContext = new WebappContext("WebappContext", ""); - new ServletContextListenerConfigurer(serverData, servletContextListenerData,servletRequestListenerData); + new ServletContextListenerConfigurer(serverData, servletContextListenerData, servletRequestListenerData); addServlet(webappContext); @@ -109,7 +109,7 @@ private void addAccessLog(HttpServer httpServer) { try { String accessLogLocation = serverData.getRootContext().getBean(AccessLogLocationBean.class).getAccessLogLocation(); - accessLogLocation = accessLogLocation + replaceSlash(serverData.getModule().getContext()) + "-access.log"; + accessLogLocation = accessLogLocation + "/" + replaceSlash(serverData.getModule().getContext()) + "-access.log"; final AccessLogBuilder builder = new AccessLogBuilder(accessLogLocation); builder.rotatedDaily(); diff --git a/micro-core/src/main/java/com/aol/micro/server/spring/SpringApplicationConfigurator.java b/micro-core/src/main/java/com/aol/micro/server/spring/SpringApplicationConfigurator.java index 9769feba2..17bb15713 100644 --- a/micro-core/src/main/java/com/aol/micro/server/spring/SpringApplicationConfigurator.java +++ b/micro-core/src/main/java/com/aol/micro/server/spring/SpringApplicationConfigurator.java @@ -1,8 +1,6 @@ package com.aol.micro.server.spring; -import java.util.Optional; import java.util.Properties; -import java.util.stream.Stream; import javax.sql.DataSource; @@ -16,14 +14,11 @@ import com.aol.micro.server.config.Config; import com.aol.micro.server.config.ConfigAccessor; -import com.aol.micro.server.config.Microserver; -import com.aol.micro.server.servers.AccessLogLocationBean; import com.aol.micro.server.spring.datasource.DataSourceBuilder; import com.aol.micro.server.spring.datasource.JdbcConfig; import com.aol.micro.server.spring.datasource.hibernate.DAOBuilder; import com.aol.micro.server.spring.datasource.hibernate.DAOProvider; import com.aol.micro.server.spring.datasource.hibernate.HibernateSessionBuilder; -import com.aol.micro.server.utility.UsefulStaticMethods; class SpringApplicationConfigurator implements SpringBuilder { private final Logger logger = LoggerFactory.getLogger(this.getClass()); @@ -35,14 +30,11 @@ public ConfigurableApplicationContext createSpringApp(Config config, Class... cl rootContext.setAllowCircularReferences(config.isAllowCircularReferences()); rootContext.register(classes); - rootContext.scan(config.getBasePackages()); rootContext.refresh(); logger.debug("Configuring Additional Spring Beans"); ConfigurableListableBeanFactory beanFactory = ((ConfigurableApplicationContext) rootContext).getBeanFactory(); - beanFactory.registerSingleton(AccessLogLocationBean.class.getCanonicalName(), createAccessLogLocationBean(rootContext)); - config.getDataSources().keySet().stream().filter(it -> !new ConfigAccessor().get().getDefaultDataSourceName().equals(it)).forEach(name -> { JdbcConfig jdbc = buildJdbcProperties(rootContext, name); DataSource dataSource = buildDataSource(name, jdbc); @@ -82,9 +74,4 @@ private JdbcConfig buildJdbcProperties(AnnotationConfigWebApplicationContext roo return JdbcConfig.builder().properties((Properties) rootContext.getBean("propertyFactory")).name(name).build(); } - private AccessLogLocationBean createAccessLogLocationBean(AnnotationConfigWebApplicationContext rootContext) { - Properties props = (Properties) rootContext.getBean("propertyFactory"); - String location = Optional.ofNullable((String) props.get("access.log.output")).orElse("./logs/"); - return new AccessLogLocationBean(location); - } } From bf70a977197899828e859d0b7be429f4ef654ec5 Mon Sep 17 00:00:00 2001 From: Ke Wang Date: Wed, 10 Jun 2015 12:55:56 +0100 Subject: [PATCH 2/4] Added unit test for access log --- .../micro/server/servers/AccessLogConfig.java | 4 +- .../server/servers/AccessLogConfigTest.java | 47 +++++++++++++++++++ 2 files changed, 48 insertions(+), 3 deletions(-) create mode 100644 micro-core/src/test/java/app/micro/server/servers/AccessLogConfigTest.java diff --git a/micro-core/src/main/java/com/aol/micro/server/servers/AccessLogConfig.java b/micro-core/src/main/java/com/aol/micro/server/servers/AccessLogConfig.java index 7f7c41f46..4c7c4550d 100644 --- a/micro-core/src/main/java/com/aol/micro/server/servers/AccessLogConfig.java +++ b/micro-core/src/main/java/com/aol/micro/server/servers/AccessLogConfig.java @@ -4,12 +4,10 @@ import org.springframework.context.annotation.Bean; import org.springframework.context.annotation.Configuration; -import com.aol.micro.server.servers.AccessLogLocationBean; - @Configuration public class AccessLogConfig { - @Value("${access.log.output:./logs/}") + @Value("${access.log.output:./logs}") private String accessLogLocation; @Bean diff --git a/micro-core/src/test/java/app/micro/server/servers/AccessLogConfigTest.java b/micro-core/src/test/java/app/micro/server/servers/AccessLogConfigTest.java new file mode 100644 index 000000000..595c74ff1 --- /dev/null +++ b/micro-core/src/test/java/app/micro/server/servers/AccessLogConfigTest.java @@ -0,0 +1,47 @@ +package app.micro.server.servers; + +import static org.hamcrest.CoreMatchers.is; +import static org.junit.Assert.assertThat; + +import java.io.File; + +import org.junit.After; +import org.junit.Before; +import org.junit.Test; + +import com.aol.micro.server.MicroserverApp; +import com.aol.micro.server.config.Config; +import com.google.common.collect.ImmutableMap; + +public class AccessLogConfigTest { + + MicroserverApp server; + + File logFile; + + @Before + public void startServer() { + + logFile = new File(System.getProperty("java.io.tmpdir") + "access-log-app-access.log"); + logFile.delete(); + + assertThat(logFile.exists(), is(false)); + + server = new MicroserverApp(Config.instance().withProperties(ImmutableMap.of("access.log.output", System.getProperty("java.io.tmpdir"))), + () -> "access-log-app"); + server.start(); + + } + + @After + public void stopServer() { + server.stop(); + } + + @Test + public void runAppAndBasicTest() { + + assertThat(logFile.exists(), is(true)); + + } +} From 9ee976a61d14decd9b5894a8d7301be0a33ca7cf Mon Sep 17 00:00:00 2001 From: Ke Wang Date: Wed, 10 Jun 2015 13:15:18 +0100 Subject: [PATCH 3/4] Improve the unit test --- .../test/java/app/micro/server/servers/AccessLogConfigTest.java | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/micro-core/src/test/java/app/micro/server/servers/AccessLogConfigTest.java b/micro-core/src/test/java/app/micro/server/servers/AccessLogConfigTest.java index 595c74ff1..447489a0f 100644 --- a/micro-core/src/test/java/app/micro/server/servers/AccessLogConfigTest.java +++ b/micro-core/src/test/java/app/micro/server/servers/AccessLogConfigTest.java @@ -27,7 +27,7 @@ public void startServer() { assertThat(logFile.exists(), is(false)); - server = new MicroserverApp(Config.instance().withProperties(ImmutableMap.of("access.log.output", System.getProperty("java.io.tmpdir"))), + server = new MicroserverApp(Config.instance().withProperties(ImmutableMap.of("access.log.output", "${java.io.tmpdir}")), () -> "access-log-app"); server.start(); From 72ef03751b27b3628f1f2f5cfc6195e6610cecf2 Mon Sep 17 00:00:00 2001 From: Ke Wang Date: Wed, 10 Jun 2015 13:32:40 +0100 Subject: [PATCH 4/4] Fix unit test --- .../java/app/micro/server/servers/AccessLogConfigTest.java | 5 ++--- 1 file changed, 2 insertions(+), 3 deletions(-) diff --git a/micro-core/src/test/java/app/micro/server/servers/AccessLogConfigTest.java b/micro-core/src/test/java/app/micro/server/servers/AccessLogConfigTest.java index 447489a0f..b47978e7d 100644 --- a/micro-core/src/test/java/app/micro/server/servers/AccessLogConfigTest.java +++ b/micro-core/src/test/java/app/micro/server/servers/AccessLogConfigTest.java @@ -22,13 +22,12 @@ public class AccessLogConfigTest { @Before public void startServer() { - logFile = new File(System.getProperty("java.io.tmpdir") + "access-log-app-access.log"); + logFile = new File(System.getProperty("user.home") + "/access-log-app-access.log"); logFile.delete(); assertThat(logFile.exists(), is(false)); - server = new MicroserverApp(Config.instance().withProperties(ImmutableMap.of("access.log.output", "${java.io.tmpdir}")), - () -> "access-log-app"); + server = new MicroserverApp(Config.instance().withProperties(ImmutableMap.of("access.log.output", "${user.home}")), () -> "access-log-app"); server.start(); }