Skip to content

Commit

Permalink
Refactor to use Java 8 lambdas
Browse files Browse the repository at this point in the history
Also, extract some methods so we can have smaller/simpler
methods at the code base.
  • Loading branch information
marcospereira committed Feb 18, 2016
1 parent 96b055b commit 34ec6cf
Show file tree
Hide file tree
Showing 6 changed files with 53 additions and 58 deletions.
77 changes: 44 additions & 33 deletions play-ebean/src/main/java/play/db/ebean/DefaultEbeanConfig.java
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,6 @@
package play.db.ebean;

import com.avaje.ebean.config.ServerConfig;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import play.Configuration;
import play.Environment;
import play.db.DBApi;
Expand All @@ -21,8 +19,6 @@
@Singleton
public class DefaultEbeanConfig implements EbeanConfig {

private static final Logger log = LoggerFactory.getLogger(DefaultEbeanConfig.class);

private final String defaultServer;
private final Map<String, ServerConfig> serverConfigs;

Expand Down Expand Up @@ -75,47 +71,62 @@ public EbeanConfig parse() {
ServerConfig serverConfig = new ServerConfig();
serverConfig.setName(key);
serverConfig.loadFromProperties();
try {
serverConfig.setDataSource(new WrappingDatasource(dbApi.getDatabase(key).getDataSource()));
} catch(Exception e) {
throw configuration.reportError(
"ebean." + key,
e.getMessage(),
e
);
}

setServerConfigDataSource(key, serverConfig);

if (config.getDefaultDatasource().equals(key)) {
serverConfig.setDefaultServer(true);
}

Set<String> classes = new HashSet<String>();
for (String load: entry.getValue()) {
load = load.trim();
if (load.endsWith(".*")) {
classes.addAll(play.libs.Classpath.getTypes(environment, load.substring(0, load.length()-2)));
} else {
classes.add(load);
}
}
for (String clazz: classes) {
try {
serverConfig.addClass(Class.forName(clazz, true, environment.classLoader()));
} catch (Throwable e) {
throw configuration.reportError(
"ebean." + key,
"Cannot register class [" + clazz + "] in Ebean server",
e
);
}
}
Set<String> classes = getModelClasses(entry);
addModelClassesToServerConfig(key, serverConfig, classes);

serverConfigs.put(key, serverConfig);
}

return new DefaultEbeanConfig(config.getDefaultDatasource(), serverConfigs);
}

private void setServerConfigDataSource(String key, ServerConfig serverConfig) {
try {
serverConfig.setDataSource(new WrappingDatasource(dbApi.getDatabase(key).getDataSource()));
} catch(Exception e) {
throw configuration.reportError(
"ebean." + key,
e.getMessage(),
e
);
}
}

private void addModelClassesToServerConfig(String key, ServerConfig serverConfig, Set<String> classes) {
for (String clazz: classes) {
try {
serverConfig.addClass(Class.forName(clazz, true, environment.classLoader()));
} catch (Throwable e) {
throw configuration.reportError(
"ebean." + key,
"Cannot register class [" + clazz + "] in Ebean server",
e
);
}
}
}

private Set<String> getModelClasses(Map.Entry<String, List<String>> entry) {
Set<String> classes = new HashSet<>();
entry.getValue().forEach(load -> {
load = load.trim();
if (load.endsWith(".*")) {
classes.addAll(play.libs.Classpath.getTypes(environment, load.substring(0, load.length()-2)));
} else {
classes.add(load);
}
});

return classes;
}

/**
* <code>DataSource</code> wrapper to ensure that every retrieved connection has auto-commit disabled.
*/
Expand Down
4 changes: 2 additions & 2 deletions play-ebean/src/main/java/play/db/ebean/EbeanConfig.java
Original file line number Diff line number Diff line change
Expand Up @@ -9,8 +9,8 @@

public interface EbeanConfig {

public String defaultServer();
String defaultServer();

public Map<String, ServerConfig> serverConfigs();
Map<String, ServerConfig> serverConfigs();

}
18 changes: 5 additions & 13 deletions play-ebean/src/main/java/play/db/ebean/EbeanDynamicEvolutions.java
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@

import com.avaje.ebean.EbeanServer;
import com.avaje.ebean.EbeanServerFactory;
import com.avaje.ebean.config.ServerConfig;
import com.avaje.ebean.dbmigration.model.CurrentModel;
import com.avaje.ebeaninternal.api.SpiEbeanServer;
import play.Environment;
Expand Down Expand Up @@ -47,11 +46,7 @@ public EbeanDynamicEvolutions(EbeanConfig config, Environment environment, Appli
* Initialise the Ebean servers.
*/
public void start() {
for (Map.Entry<String, ServerConfig> entry : config.serverConfigs().entrySet()) {
String key = entry.getKey();
ServerConfig serverConfig = entry.getValue();
servers.put(key, EbeanServerFactory.create(serverConfig));
}
config.serverConfigs().forEach((key, serverConfig) -> servers.put(key, EbeanServerFactory.create(serverConfig)));
}

/**
Expand All @@ -60,16 +55,13 @@ public void start() {
@Override
public void create() {
if (!environment.isProd()) {
for (Map.Entry<String, ServerConfig> entry : config.serverConfigs().entrySet()) {
String key = entry.getKey();
config.serverConfigs().forEach((key, serverConfig) -> {
String evolutionScript = generateEvolutionScript(servers.get(key));
if (evolutionScript != null) {
File evolutions = environment.getFile("conf/evolutions/" + key + "/1.sql");
try {
String content;
if (!evolutions.exists()) {
content = "";
} else {
String content = "";
if (evolutions.exists()) {
content = new String(Files.readAllBytes(evolutions.toPath()), "utf-8");
}

Expand All @@ -83,7 +75,7 @@ public void create() {
throw new RuntimeException(e);
}
}
}
});
}
}

Expand Down
2 changes: 0 additions & 2 deletions play-ebean/src/main/java/play/db/ebean/EbeanModule.java
Original file line number Diff line number Diff line change
Expand Up @@ -10,8 +10,6 @@
import play.api.inject.Module;
import scala.collection.Seq;

import javax.inject.Singleton;

/**
* Injection module with default Ebean components.
*/
Expand Down
9 changes: 2 additions & 7 deletions play-ebean/src/main/java/play/db/ebean/EbeanParsedConfig.java
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@
package play.db.ebean;

import com.typesafe.config.Config;
import com.typesafe.config.ConfigValue;
import com.typesafe.config.ConfigValueType;
import play.Configuration;

Expand Down Expand Up @@ -45,10 +44,7 @@ public static EbeanParsedConfig parseFromConfig(Configuration configuration) {

if (config.hasPath(ebeanConfigKey)) {
Config ebeanConfig = config.getConfig(ebeanConfigKey);

for (String key : ebeanConfig.root().keySet()) {

ConfigValue raw = ebeanConfig.getValue(key);
ebeanConfig.root().forEach((key, raw) -> {
List<String> models;
if (raw.valueType() == ConfigValueType.STRING) {
// Support legacy comma separated string
Expand All @@ -58,8 +54,7 @@ public static EbeanParsedConfig parseFromConfig(Configuration configuration) {
}

datasourceModels.put(key, models);

}
});
}
return new EbeanParsedConfig(defaultDatasource, datasourceModels);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,6 @@
import java.util.concurrent.CompletionStage;

import com.avaje.ebean.Ebean;
import com.avaje.ebean.TxCallable;
import play.mvc.Action;
import play.mvc.Http.Context;
import play.mvc.Result;
Expand Down

0 comments on commit 34ec6cf

Please sign in to comment.