From 1e90a27d4abca62e0c9561288542b0585fd50672 Mon Sep 17 00:00:00 2001 From: Ke Wang Date: Fri, 27 Mar 2015 10:10:55 +0000 Subject: [PATCH] Make instance.properties file name configurable --- .../com/aol/micro/server/config/Config.java | 100 +++++++++--------- .../aol/micro/server/config/Microserver.java | 21 ++-- .../server/config/MicroserverConfigurer.java | 21 ++-- .../spring/properties/PropertyFileConfig.java | 49 ++++----- .../config/MicroserverConfigurerTest.java | 60 ++++++----- 5 files changed, 124 insertions(+), 127 deletions(-) diff --git a/micro-core/src/main/java/com/aol/micro/server/config/Config.java b/micro-core/src/main/java/com/aol/micro/server/config/Config.java index a98975f03..6ccb7313b 100644 --- a/micro-core/src/main/java/com/aol/micro/server/config/Config.java +++ b/micro-core/src/main/java/com/aol/micro/server/config/Config.java @@ -11,14 +11,11 @@ import lombok.Getter; import lombok.experimental.Wither; -import com.google.common.collect.ImmutableList; import com.google.common.collect.ImmutableMap; import com.google.common.collect.ImmutableSet; import com.google.common.collect.Lists; import com.google.common.collect.Sets; - - /** * * Class for configuring a Spring Context for Microserver @@ -30,52 +27,57 @@ @Getter @Wither public class Config { - + private final String defaultDataSourceName; private final ImmutableSet classes; - private final ImmutableMap properties; - + private final ImmutableMap properties; + private final String propertiesName; - private final ImmutableMap> dataSources; + private final String instancePropertiesName; + private final ImmutableMap> dataSources; private final SSLProperties sslProperties; private final boolean allowCircularReferences; - - + public Config() { classes = ImmutableSet.of(); - properties= ImmutableMap.of(); + properties = ImmutableMap.of(); dataSources = ImmutableMap.of(); - defaultDataSourceName="db"; + defaultDataSourceName = "db"; propertiesName = "application.properties"; + instancePropertiesName = "instance.properties"; sslProperties = null; allowCircularReferences = false; } private static volatile Config instance = null; - - public Config set(){ + + public Config set() { instance = this; return this; } - public static Config instance(){ + + public static Config instance() { instance = new Config(); return instance; } - static Config get(){ + + static Config get() { return instance; - + } + public static void reset() { - instance =null; - + instance = null; + } - - public Config withEntityScanDataSource(String dataSource,String... packages){ - Map> newMap = new HashMap<>(dataSources); - newMap.put(dataSource,Arrays.asList(packages)); + + public Config withEntityScanDataSource(String dataSource, String... packages) { + Map> newMap = new HashMap<>(dataSources); + newMap.put(dataSource, Arrays.asList(packages)); return this.withDataSources(ImmutableMap.copyOf(newMap)); } + /** * Define the packages that hibernate should scan for Hibernate entities * Should be used in conjunction Microserver Spring Configuration classes @See Classes#HIBERNATE_CLASSES @@ -83,13 +85,12 @@ public Config withEntityScanDataSource(String dataSource,String... packages){ * @param packages Packages to scan for hibernate entities * @return New Config object, with configured packages */ - public Config withEntityScan(String... packages){ - Map> newMap = new HashMap<>(dataSources); - newMap.put(defaultDataSourceName,Arrays.asList(packages)); + public Config withEntityScan(String... packages) { + Map> newMap = new HashMap<>(dataSources); + newMap.put(defaultDataSourceName, Arrays.asList(packages)); return this.withDataSources(ImmutableMap.copyOf(newMap)); } - - + /** * Add the provided Classes to initial Spring Context as well as * @see Classes#DATASOURCE_CLASSES @@ -97,15 +98,14 @@ public Config withEntityScan(String... packages){ * @param c Array of additional Spring configuration classes * @return New Config object, with configured packages */ - public Config withDefaultDataSource(Class... c){ - List result =Lists.newArrayList(Classes.DATASOURCE_CLASSES.getClasses()); - if(classes!=null) - result.addAll(classes); - Stream.of(c).forEach(next -> result.add(next)); - return this.withClasses(ImmutableSet.copyOf(result)); + public Config withDefaultDataSource(Class... c) { + List result = Lists.newArrayList(Classes.DATASOURCE_CLASSES.getClasses()); + if (classes != null) + result.addAll(classes); + Stream.of(c).forEach(next -> result.add(next)); + return this.withClasses(ImmutableSet.copyOf(result)); } - /** * * Add the provided Classes to initial Spring Context as well as @@ -114,15 +114,15 @@ public Config withDefaultDataSource(Class... c){ * @param c Array of additional Spring configuration classes * @return New Config object, with configured packages */ - public Config withJdbcClasses(Class... c){ - List result = Lists.newArrayList(Classes.JDBC_CLASSES.getClasses()); - result.addAll(Arrays.asList(Classes.SPRING_DATA_CLASSES.getClasses())); - if(classes!=null) - result.addAll(classes); - Stream.of(c).forEach(next -> result.add(next)); - return this.withClasses(ImmutableSet.copyOf(result)); + public Config withJdbcClasses(Class... c) { + List result = Lists.newArrayList(Classes.JDBC_CLASSES.getClasses()); + result.addAll(Arrays.asList(Classes.SPRING_DATA_CLASSES.getClasses())); + if (classes != null) + result.addAll(classes); + Stream.of(c).forEach(next -> result.add(next)); + return this.withClasses(ImmutableSet.copyOf(result)); } - + /** * Add the provided Classes to initial Spring Context as well as * @see Classes#SPRING_DATA_CLASSES @@ -130,13 +130,13 @@ public Config withJdbcClasses(Class... c){ * @param c Array of additional Spring configuration classes * @return New Config object, with configured packages */ - public Config withHibernateClasses(Class... c){ - Set result = Sets.newHashSet(Classes.HIBERNATE_CLASSES.getClasses()); - result.addAll(Arrays.asList(Classes.SPRING_DATA_CLASSES.getClasses())); - if(classes!=null) - result.addAll(classes); - Stream.of(c).forEach(next -> result.add(next)); - return this.withClasses(ImmutableSet.copyOf(result)); + public Config withHibernateClasses(Class... c) { + Set result = Sets.newHashSet(Classes.HIBERNATE_CLASSES.getClasses()); + result.addAll(Arrays.asList(Classes.SPRING_DATA_CLASSES.getClasses())); + if (classes != null) + result.addAll(classes); + Stream.of(c).forEach(next -> result.add(next)); + return this.withClasses(ImmutableSet.copyOf(result)); } - + } diff --git a/micro-core/src/main/java/com/aol/micro/server/config/Microserver.java b/micro-core/src/main/java/com/aol/micro/server/config/Microserver.java index 1b0ba9226..388e0dca9 100644 --- a/micro-core/src/main/java/com/aol/micro/server/config/Microserver.java +++ b/micro-core/src/main/java/com/aol/micro/server/config/Microserver.java @@ -12,47 +12,50 @@ @Component public @interface Microserver { - /** * @return Spring auto-scan base packages */ String[] basePackages() default {}; - + /** * @return Classes to be passed to initial Spring context * e.g. Classes that have Spring @Configuration or @Component annotation * */ Class[] classes() default {}; - + /** * @return Preconfigured collections of classes to be passed to initial Spring context * They configure various useful pieces of functionality - such as property file loading, * datasources, scheduling etc */ Classes[] springClasses() default {}; - + /** * @return Property file name */ String propertiesName() default "application.properties"; - + + /** + * @return Instance property file name + */ + String instancePropertiesName() default "instance.properties"; + /** * @return Hibernate entity scan packages */ String[] entityScan() default {}; - + /** * @return Properties to be injected into spring format is * key,value,key,value comma separated list */ String[] properties() default {}; - - + /** * @return true if the spring context should allow circular dependencies * We recommend not to allow circular dependencies */ boolean allowCircularDependencies() default false; - + } diff --git a/micro-core/src/main/java/com/aol/micro/server/config/MicroserverConfigurer.java b/micro-core/src/main/java/com/aol/micro/server/config/MicroserverConfigurer.java index 8f44405a7..3f32cd154 100644 --- a/micro-core/src/main/java/com/aol/micro/server/config/MicroserverConfigurer.java +++ b/micro-core/src/main/java/com/aol/micro/server/config/MicroserverConfigurer.java @@ -14,31 +14,28 @@ public class MicroserverConfigurer implements Configurer { public Config buildConfig(Class class1) { - Microserver microserver = (Microserver)class1.getAnnotation(Microserver.class); - if(microserver==null) + Microserver microserver = (Microserver) class1.getAnnotation(Microserver.class); + if (microserver == null) return Config.instance(); List classes = buildClasses(class1, microserver); - + Map properties = buildProperties(microserver); - + return Config.instance().withEntityScan(microserver.entityScan()).withClasses(ImmutableSet.copyOf(classes)) - .withPropertiesName(microserver.propertiesName()) - .withAllowCircularReferences(microserver.allowCircularDependencies()) - .withProperties(ImmutableMap.copyOf(properties)).set(); + .withPropertiesName(microserver.propertiesName()).withInstancePropertiesName(microserver.instancePropertiesName()) + .withAllowCircularReferences(microserver.allowCircularDependencies()).withProperties(ImmutableMap.copyOf(properties)).set(); } private Map buildProperties(Microserver microserver) { - Map properties = LazySeq.of(microserver.properties()) - .grouped(2) - .stream() - .collect(Collectors.toMap(prop -> prop.get(0), prop -> prop.get(1))); + Map properties = LazySeq.of(microserver.properties()).grouped(2).stream() + .collect(Collectors.toMap(prop -> prop.get(0), prop -> prop.get(1))); return properties; } private List buildClasses(Class class1, Microserver microserver) { List classes = new ArrayList(); classes.add(class1); - if(microserver.classes()!=null) + if (microserver.classes() != null) classes.addAll(Arrays.asList(microserver.classes())); Stream.of(microserver.springClasses()).map(cl -> cl.getClasses()).forEach(array -> classes.addAll(Arrays.asList(array))); return classes; diff --git a/micro-core/src/main/java/com/aol/micro/server/spring/properties/PropertyFileConfig.java b/micro-core/src/main/java/com/aol/micro/server/spring/properties/PropertyFileConfig.java index 946572f38..6f17ef6a5 100644 --- a/micro-core/src/main/java/com/aol/micro/server/spring/properties/PropertyFileConfig.java +++ b/micro-core/src/main/java/com/aol/micro/server/spring/properties/PropertyFileConfig.java @@ -4,12 +4,9 @@ import java.io.IOException; import java.net.URL; import java.util.List; -import java.util.Map; import java.util.Optional; import java.util.Properties; -import lombok.Setter; - import org.slf4j.Logger; import org.slf4j.LoggerFactory; import org.springframework.beans.factory.config.PropertiesFactoryBean; @@ -20,18 +17,14 @@ import org.springframework.core.io.Resource; import org.springframework.core.io.UrlResource; -import com.aol.micro.server.config.Config; import com.aol.micro.server.config.ConfigAccessor; import com.google.common.collect.Lists; -import com.google.common.collect.Maps; @Configuration -public class PropertyFileConfig { +public class PropertyFileConfig { private final Logger logger = LoggerFactory.getLogger(getClass()); - - @Bean public PropertyPlaceholderConfigurer propertyPlaceholderConfigurer() throws IOException { @@ -39,21 +32,20 @@ public PropertyPlaceholderConfigurer propertyPlaceholderConfigurer() throws IOEx PropertyPlaceholderConfigurer configurer = new PropertyPlaceholderConfigurer(); Properties props = propertyFactory(); - - + configurer.setProperties(props); configurer.setSystemPropertiesMode(PropertyPlaceholderConfigurer.SYSTEM_PROPERTIES_MODE_OVERRIDE); return configurer; } - + @Bean - public Properties propertyFactory() throws IOException{ + public Properties propertyFactory() throws IOException { List resources = loadPropertyResource(); PropertiesFactoryBean factory = new PropertiesFactoryBean(); factory.setLocations(resources.toArray(new Resource[resources.size()])); factory.afterPropertiesSet(); - Properties props =factory.getObject(); - + Properties props = factory.getObject(); + props.putAll(new ConfigAccessor().get().getProperties()); return props; } @@ -62,39 +54,39 @@ private List loadPropertyResource() { List resources = Lists.newArrayList(); loadProperties().ifPresent(it -> resources.add(it)); - URL instanceResource = getClass().getClassLoader().getResource("instance.properties"); + String instancePropertyFileName = new ConfigAccessor().get().getInstancePropertiesName(); + + URL instanceResource = getClass().getClassLoader().getResource(instancePropertyFileName); if (instanceResource != null) { resources.add(new UrlResource(instanceResource)); logger.info("instance.properties added"); } - return resources; } private Optional loadProperties() { - - String applicationPropertyFileName= new ConfigAccessor().get().getPropertiesName(); - + + String applicationPropertyFileName = new ConfigAccessor().get().getPropertiesName(); + Optional resource = Optional.empty(); - if (new File("./"+applicationPropertyFileName).exists()) { - resource = Optional.of(new FileSystemResource(new File("./"+applicationPropertyFileName))); - logger.info("./"+applicationPropertyFileName+" added"); + if (new File("./" + applicationPropertyFileName).exists()) { + resource = Optional.of(new FileSystemResource(new File("./" + applicationPropertyFileName))); + logger.info("./" + applicationPropertyFileName + " added"); } URL urlResource = getClass().getClassLoader().getResource(applicationPropertyFileName); if (urlResource != null) { resource = Optional.of(new UrlResource(urlResource)); - logger.info(applicationPropertyFileName +" added"); + logger.info(applicationPropertyFileName + " added"); } if (System.getProperty("application.env") != null) { - URL envResource = getClass().getClassLoader() - .getResource(createEnvBasedPropertyFileName(applicationPropertyFileName)); + URL envResource = getClass().getClassLoader().getResource(createEnvBasedPropertyFileName(applicationPropertyFileName)); if (envResource != null) { resource = Optional.of(new UrlResource(envResource)); - logger.info(createEnvBasedPropertyFileName(applicationPropertyFileName) +" added"); + logger.info(createEnvBasedPropertyFileName(applicationPropertyFileName) + " added"); } } @@ -107,9 +99,8 @@ private Optional loadProperties() { } private String createEnvBasedPropertyFileName(String applicationPropertyFileName) { - return applicationPropertyFileName.substring(0,applicationPropertyFileName.lastIndexOf("."))+"-" - + System.getProperty("application.env") + ".properties"; + return applicationPropertyFileName.substring(0, applicationPropertyFileName.lastIndexOf(".")) + "-" + System.getProperty("application.env") + + ".properties"; } - } diff --git a/micro-core/src/test/java/com/aol/micro/server/config/MicroserverConfigurerTest.java b/micro-core/src/test/java/com/aol/micro/server/config/MicroserverConfigurerTest.java index fe01b8537..f8f117697 100644 --- a/micro-core/src/test/java/com/aol/micro/server/config/MicroserverConfigurerTest.java +++ b/micro-core/src/test/java/com/aol/micro/server/config/MicroserverConfigurerTest.java @@ -1,65 +1,71 @@ package com.aol.micro.server.config; -import static org.junit.Assert.*; -import static org.hamcrest.Matchers.*; +import static org.hamcrest.Matchers.hasItem; +import static org.hamcrest.Matchers.is; +import static org.junit.Assert.assertFalse; +import static org.junit.Assert.assertThat; +import static org.junit.Assert.assertTrue; + import org.junit.Test; -@Microserver(propertiesName="test!", properties={"hello","world"}, entityScan={"packages"}, springClasses={Classes.CORE_CLASSES}, classes={Integer.class}) +@Microserver(propertiesName = "test!", instancePropertiesName = "test2!", properties = { "hello", "world" }, entityScan = { "packages" }, springClasses = { Classes.CORE_CLASSES }, classes = { Integer.class }) public class MicroserverConfigurerTest { MicroserverConfigurer configurer = new MicroserverConfigurer(); - + @Test public void properties() { - assertThat(configurer.buildConfig(MicroserverConfigurerTest.class).getProperties().keySet(), - hasItem("hello")); - assertThat(configurer.buildConfig(MicroserverConfigurerTest.class).getProperties().values(), - hasItem("world")); + assertThat(configurer.buildConfig(MicroserverConfigurerTest.class).getProperties().keySet(), hasItem("hello")); + assertThat(configurer.buildConfig(MicroserverConfigurerTest.class).getProperties().values(), hasItem("world")); } + @Test public void propertiesName() { - assertThat(configurer.buildConfig(MicroserverConfigurerTest.class).getPropertiesName(), - is("test!")); - + assertThat(configurer.buildConfig(MicroserverConfigurerTest.class).getPropertiesName(), is("test!")); + + } + + @Test + public void instancPpropertiesName() { + assertThat(configurer.buildConfig(MicroserverConfigurerTest.class).getInstancePropertiesName(), is("test2!")); + } + @Test public void entityScan() { Config config = configurer.buildConfig(MicroserverConfigurerTest.class); - assertThat(config.getDataSources().get(config.getDefaultDataSourceName()), - hasItem("packages")); - + assertThat(config.getDataSources().get(config.getDefaultDataSourceName()), hasItem("packages")); + } + @Test public void springClasses() { Config config = configurer.buildConfig(MicroserverConfigurerTest.class); - assertThat(config.getClasses(), - hasItem(Classes.CORE_CLASSES.getClasses()[0])); - + assertThat(config.getClasses(), hasItem(Classes.CORE_CLASSES.getClasses()[0])); + } + @Test public void selfClass() { Config config = configurer.buildConfig(MicroserverConfigurerTest.class); - assertThat(config.getClasses(), - hasItem(this.getClass())); - + assertThat(config.getClasses(), hasItem(this.getClass())); + } + @Test public void additionalClasses() { Config config = configurer.buildConfig(MicroserverConfigurerTest.class); - assertThat(config.getClasses(), - hasItem(Integer.class)); - + assertThat(config.getClasses(), hasItem(Integer.class)); + } @Test public void circularReferences() { Config config = configurer.buildConfig(MicroserverConfigurerTest.class); - assertFalse(config.isAllowCircularReferences()); - config = configurer.buildConfig(MicroserverConfigurerTest.class) - .withAllowCircularReferences(true); + assertFalse(config.isAllowCircularReferences()); + config = configurer.buildConfig(MicroserverConfigurerTest.class).withAllowCircularReferences(true); assertTrue(config.isAllowCircularReferences()); } - }