Skip to content

Commit

Permalink
Make instance.properties file name configurable
Browse files Browse the repository at this point in the history
  • Loading branch information
Ke Wang committed Mar 27, 2015
1 parent 3acc75f commit 1e90a27
Show file tree
Hide file tree
Showing 5 changed files with 124 additions and 127 deletions.
100 changes: 50 additions & 50 deletions micro-core/src/main/java/com/aol/micro/server/config/Config.java
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -30,82 +27,85 @@
@Getter
@Wither
public class Config {

private final String defaultDataSourceName;
private final ImmutableSet<Class> classes;
private final ImmutableMap<String,String> properties;
private final ImmutableMap<String, String> properties;

private final String propertiesName;
private final ImmutableMap<String,List<String>> dataSources;
private final String instancePropertiesName;
private final ImmutableMap<String, List<String>> 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<String,List<String>> newMap = new HashMap<>(dataSources);
newMap.put(dataSource,Arrays.asList(packages));

public Config withEntityScanDataSource(String dataSource, String... packages) {
Map<String, List<String>> 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
*
* @param packages Packages to scan for hibernate entities
* @return New Config object, with configured packages
*/
public Config withEntityScan(String... packages){
Map<String,List<String>> newMap = new HashMap<>(dataSources);
newMap.put(defaultDataSourceName,Arrays.asList(packages));
public Config withEntityScan(String... packages) {
Map<String, List<String>> 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
*
* @param c Array of additional Spring configuration classes
* @return New Config object, with configured packages
*/
public Config withDefaultDataSource(Class... c){
List<Class> 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<Class> 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
Expand All @@ -114,29 +114,29 @@ 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<Class> 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<Class> 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
*
* @param c Array of additional Spring configuration classes
* @return New Config object, with configured packages
*/
public Config withHibernateClasses(Class... c){
Set<Class> 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<Class> 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));
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -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;

}
Original file line number Diff line number Diff line change
Expand Up @@ -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<Class> classes = buildClasses(class1, microserver);

Map<String, String> 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<String, String> buildProperties(Microserver microserver) {
Map<String,String> properties = LazySeq.of(microserver.properties())
.grouped(2)
.stream()
.collect(Collectors.toMap(prop -> prop.get(0), prop -> prop.get(1)));
Map<String, String> properties = LazySeq.of(microserver.properties()).grouped(2).stream()
.collect(Collectors.toMap(prop -> prop.get(0), prop -> prop.get(1)));
return properties;
}

private List<Class> buildClasses(Class class1, Microserver microserver) {
List<Class> 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;
Expand Down
Loading

0 comments on commit 1e90a27

Please sign in to comment.