mirror of
https://github.com/ebean-orm/ebean.git
synced 2026-11-22 15:10:02 +00:00
Merge pull request #2864 from FOCONIS/feature/override-service-objects
NEW: Override service objects with DatabaseConfig.putServiceObject
This commit is contained in:
6 files changed
+87
-7
No files matched your search
@@ -615,7 +615,7 @@ public class DatabaseConfig {
|
|||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Put a service object into configuration such that it can be passed to a plugin.
|
* Put a service object into configuration such that it can be used by ebean or a plugin.
|
||||||
* <p>
|
* <p>
|
||||||
* For example, put IgniteConfiguration in to be passed to the Ignite plugin.
|
* For example, put IgniteConfiguration in to be passed to the Ignite plugin.
|
||||||
*/
|
*/
|
||||||
@@ -623,6 +623,28 @@ public class DatabaseConfig {
|
|||||||
serviceObject.put(key, configObject);
|
serviceObject.put(key, configObject);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Put a service object into configuration such that it can be used by ebean or a plugin.
|
||||||
|
* <p>
|
||||||
|
* For example, put IgniteConfiguration in to be passed to the Ignite plugin.
|
||||||
|
* You can also override some SPI objects that should be used for that Database. Currently, the following
|
||||||
|
* objects are possible.
|
||||||
|
* <ul>
|
||||||
|
* <li>DataSourceAlertFactory (e.g. add different alert factories for different ebean instances)</li>
|
||||||
|
* <li>DocStoreFactory</li>
|
||||||
|
* <li>XmapService</li>
|
||||||
|
* <li>SpiLoggerFactory (e.g. add custom logger for a certain ebean instance)</li>
|
||||||
|
* <li>AutoTuneServiceProvider</li>
|
||||||
|
* <li>SpiProfileHandler</li>
|
||||||
|
* <li>SlowQueryListener (e.g. add custom query listener for a certain ebean instance)</li>
|
||||||
|
* <li>ServerCacheNotifyPlugin</li>
|
||||||
|
* <li>SpiDdlGenneratorProvider</li>
|
||||||
|
* </ul>
|
||||||
|
*/
|
||||||
|
public <T> void putServiceObject(Class<T> iface, T configObject) {
|
||||||
|
serviceObject.put(serviceObjectKey(iface), configObject);
|
||||||
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Return the service object given the key.
|
* Return the service object given the key.
|
||||||
*/
|
*/
|
||||||
@@ -631,7 +653,7 @@ public class DatabaseConfig {
|
|||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Put a service object into configuration such that it can be passed to a plugin.
|
* Put a service object into configuration such that it can be used by ebean or a plugin.
|
||||||
*
|
*
|
||||||
* <pre>{@code
|
* <pre>{@code
|
||||||
*
|
*
|
||||||
@@ -656,7 +678,7 @@ public class DatabaseConfig {
|
|||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Used by plugins to obtain service objects.
|
* Used by ebean or plugins to obtain service objects.
|
||||||
*
|
*
|
||||||
* <pre>{@code
|
* <pre>{@code
|
||||||
*
|
*
|
||||||
|
|||||||
@@ -307,7 +307,10 @@ public final class DefaultServer implements SpiServer, SpiEbeanServer {
|
|||||||
*/
|
*/
|
||||||
public void start() {
|
public void start() {
|
||||||
if (config.isRunMigration() && TenantMode.DB != config.getTenantMode()) {
|
if (config.isRunMigration() && TenantMode.DB != config.getTenantMode()) {
|
||||||
final AutoMigrationRunner migrationRunner = ServiceUtil.service(AutoMigrationRunner.class);
|
AutoMigrationRunner migrationRunner = config.getServiceObject(AutoMigrationRunner.class);
|
||||||
|
if (migrationRunner == null) {
|
||||||
|
migrationRunner = ServiceUtil.service(AutoMigrationRunner.class);
|
||||||
|
}
|
||||||
if (migrationRunner == null) {
|
if (migrationRunner == null) {
|
||||||
throw new IllegalStateException("No AutoMigrationRunner found. Probably ebean-migration is not in the classpath?");
|
throw new IllegalStateException("No AutoMigrationRunner found. Probably ebean-migration is not in the classpath?");
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -117,7 +117,10 @@ final class InitDataSource {
|
|||||||
* Attach DataSourceAlert via service loader if present.
|
* Attach DataSourceAlert via service loader if present.
|
||||||
*/
|
*/
|
||||||
private void attachAlert(DataSourceConfig dsConfig) {
|
private void attachAlert(DataSourceConfig dsConfig) {
|
||||||
DataSourceAlertFactory alertFactory = ServiceUtil.service(DataSourceAlertFactory.class);
|
DataSourceAlertFactory alertFactory = config.getServiceObject(DataSourceAlertFactory.class);
|
||||||
|
if (alertFactory == null) {
|
||||||
|
alertFactory = ServiceUtil.service(DataSourceAlertFactory.class);
|
||||||
|
}
|
||||||
if (alertFactory != null) {
|
if (alertFactory != null) {
|
||||||
dsConfig.setAlert(alertFactory.createAlert());
|
dsConfig.setAlert(alertFactory.createAlert());
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -144,7 +144,12 @@ public final class InternalConfiguration {
|
|||||||
}
|
}
|
||||||
|
|
||||||
private <S> S service(Class<S> cls) {
|
private <S> S service(Class<S> cls) {
|
||||||
return ServiceUtil.service(cls);
|
S service = config.getServiceObject(cls);
|
||||||
|
if (service != null) {
|
||||||
|
return service;
|
||||||
|
} else {
|
||||||
|
return ServiceUtil.service(cls);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
private List<XmapEbean> readExternalMapping() {
|
private List<XmapEbean> readExternalMapping() {
|
||||||
|
|||||||
@@ -111,7 +111,10 @@ public final class DefaultTypeManager implements TypeManager {
|
|||||||
}
|
}
|
||||||
|
|
||||||
private void loadGeoTypeBinder(DatabaseConfig config) {
|
private void loadGeoTypeBinder(DatabaseConfig config) {
|
||||||
final GeoTypeProvider provider = ServiceUtil.service(GeoTypeProvider.class);
|
GeoTypeProvider provider = config.getServiceObject(GeoTypeProvider.class);
|
||||||
|
if (provider == null) {
|
||||||
|
provider = ServiceUtil.service(GeoTypeProvider.class);
|
||||||
|
}
|
||||||
if (provider != null) {
|
if (provider != null) {
|
||||||
geoTypeBinder = provider.createBinder(config);
|
geoTypeBinder = provider.createBinder(config);
|
||||||
}
|
}
|
||||||
|
|||||||
+44
@@ -4,13 +4,53 @@ import io.ebean.Database;
|
|||||||
import io.ebean.DatabaseFactory;
|
import io.ebean.DatabaseFactory;
|
||||||
import io.ebean.config.DatabaseConfig;
|
import io.ebean.config.DatabaseConfig;
|
||||||
import io.ebean.event.ServerConfigStartup;
|
import io.ebean.event.ServerConfigStartup;
|
||||||
|
import io.ebeaninternal.api.SpiLogger;
|
||||||
|
import io.ebeaninternal.api.SpiLoggerFactory;
|
||||||
import org.junit.jupiter.api.Test;
|
import org.junit.jupiter.api.Test;
|
||||||
import org.tests.model.basic.UTDetail;
|
import org.tests.model.basic.UTDetail;
|
||||||
|
|
||||||
|
import java.util.HashSet;
|
||||||
|
import java.util.Set;
|
||||||
|
|
||||||
import static org.assertj.core.api.Assertions.assertThat;
|
import static org.assertj.core.api.Assertions.assertThat;
|
||||||
|
|
||||||
public class EbeanServerFactory_ServerConfigStart_Test {
|
public class EbeanServerFactory_ServerConfigStart_Test {
|
||||||
|
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Demo, how to intercept logging for a certain database.
|
||||||
|
* In conjunction to ServiceLoader, you can add different loggers to different databases
|
||||||
|
*/
|
||||||
|
private static class MySpiLoggerFactory implements SpiLoggerFactory {
|
||||||
|
Set<String> loggers = new HashSet<>();
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public SpiLogger create(String name) {
|
||||||
|
loggers.add(name);
|
||||||
|
// just return a dummy here
|
||||||
|
return new SpiLogger() {
|
||||||
|
@Override
|
||||||
|
public boolean isDebug() {
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public boolean isTrace() {
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public void debug(String msg) {
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public void trace(String msg) {
|
||||||
|
}
|
||||||
|
};
|
||||||
|
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
public void test() throws InterruptedException {
|
public void test() throws InterruptedException {
|
||||||
|
|
||||||
@@ -30,9 +70,13 @@ public class EbeanServerFactory_ServerConfigStart_Test {
|
|||||||
// act - register an instance
|
// act - register an instance
|
||||||
OnStartup onStartup = new OnStartup();
|
OnStartup onStartup = new OnStartup();
|
||||||
config.addServerConfigStartup(onStartup);
|
config.addServerConfigStartup(onStartup);
|
||||||
|
MySpiLoggerFactory loggerFactory = new MySpiLoggerFactory();
|
||||||
|
config.putServiceObject(SpiLoggerFactory.class, loggerFactory);
|
||||||
|
|
||||||
Database db = DatabaseFactory.create(config);
|
Database db = DatabaseFactory.create(config);
|
||||||
|
|
||||||
|
assertThat(loggerFactory.loggers).containsExactlyInAnyOrder("io.ebean.SQL", "io.ebean.SUM", "io.ebean.TXN");
|
||||||
|
|
||||||
assertThat(onStartup.calledWithConfig).isSameAs(config);
|
assertThat(onStartup.calledWithConfig).isSameAs(config);
|
||||||
assertThat(OnStartupViaClass.calledWithConfig).isSameAs(config);
|
assertThat(OnStartupViaClass.calledWithConfig).isSameAs(config);
|
||||||
|
|
||||||
|
|||||||
Reference in new issue
Block a user