mirror of
https://github.com/ebean-orm/ebean.git
synced 2026-10-23 10:03:59 +00:00
Refactor EbeanServerFactory internals - use SpiContainerFactory interface and service loader
This commit is contained in:
@@ -1,12 +1,12 @@
|
||||
package io.ebean;
|
||||
|
||||
import io.ebean.common.SpiContainer;
|
||||
import io.ebean.config.ContainerConfig;
|
||||
import io.ebean.config.ServerConfig;
|
||||
import io.ebean.service.SpiContainerShutdown;
|
||||
import io.ebean.service.SpiContainer;
|
||||
import io.ebean.service.SpiContainerFactory;
|
||||
|
||||
import javax.persistence.PersistenceException;
|
||||
import java.lang.reflect.Constructor;
|
||||
import java.util.Iterator;
|
||||
import java.util.Properties;
|
||||
import java.util.ServiceLoader;
|
||||
|
||||
@@ -31,8 +31,6 @@ import java.util.ServiceLoader;
|
||||
public class EbeanServerFactory {
|
||||
|
||||
|
||||
private static final String DEFAULT_CONTAINER = "io.ebeaninternal.server.core.DefaultContainer";
|
||||
|
||||
private static SpiContainer container;
|
||||
|
||||
/**
|
||||
@@ -100,9 +98,7 @@ public class EbeanServerFactory {
|
||||
* </p>
|
||||
*/
|
||||
public static synchronized void shutdown() {
|
||||
for (SpiContainerShutdown shutdown : ServiceLoader.load(SpiContainerShutdown.class)) {
|
||||
shutdown.shutdown();
|
||||
}
|
||||
container.shutdown();
|
||||
}
|
||||
|
||||
|
||||
@@ -138,14 +134,10 @@ public class EbeanServerFactory {
|
||||
*/
|
||||
protected static SpiContainer createContainer(ContainerConfig containerConfig) {
|
||||
|
||||
String implClassName = System.getProperty("ebean.container", DEFAULT_CONTAINER);
|
||||
|
||||
try {
|
||||
Class<?> cls = Class.forName(implClassName);
|
||||
Constructor<?> constructor = cls.getConstructor(ContainerConfig.class);
|
||||
return (SpiContainer) constructor.newInstance(containerConfig);
|
||||
} catch (Exception ex) {
|
||||
throw new RuntimeException(ex);
|
||||
Iterator<SpiContainerFactory> factories = ServiceLoader.load(SpiContainerFactory.class).iterator();
|
||||
if (factories.hasNext()) {
|
||||
return factories.next().create(containerConfig);
|
||||
}
|
||||
throw new IllegalStateException("Service loader didn't find a SpiContainerFactory?");
|
||||
}
|
||||
}
|
||||
|
||||
+1
-1
@@ -1,4 +1,4 @@
|
||||
package io.ebean.common;
|
||||
package io.ebean.service;
|
||||
|
||||
import io.ebean.EbeanServer;
|
||||
import io.ebean.config.ServerConfig;
|
||||
@@ -0,0 +1,14 @@
|
||||
package io.ebean.service;
|
||||
|
||||
import io.ebean.config.ContainerConfig;
|
||||
|
||||
/**
|
||||
* Provides shutdown of the entire container.
|
||||
*/
|
||||
public interface SpiContainerFactory {
|
||||
|
||||
/**
|
||||
* Create the Container that builds EbeanServer instances.
|
||||
*/
|
||||
SpiContainer create(ContainerConfig containerConfig);
|
||||
}
|
||||
@@ -1,12 +0,0 @@
|
||||
package io.ebean.service;
|
||||
|
||||
/**
|
||||
* Provides shutdown of the entire container.
|
||||
*/
|
||||
public interface SpiContainerShutdown {
|
||||
|
||||
/**
|
||||
* Shutdown the entire container - all EbeanServer instances.
|
||||
*/
|
||||
void shutdown();
|
||||
}
|
||||
@@ -0,0 +1,17 @@
|
||||
package io.ebeaninternal.server;
|
||||
|
||||
import io.ebean.config.ContainerConfig;
|
||||
import io.ebean.service.SpiContainer;
|
||||
import io.ebean.service.SpiContainerFactory;
|
||||
import io.ebeaninternal.server.core.DefaultContainer;
|
||||
|
||||
/**
|
||||
* Default container factory found via service loader.
|
||||
*/
|
||||
public class DContainerFactory implements SpiContainerFactory {
|
||||
|
||||
@Override
|
||||
public SpiContainer create(ContainerConfig containerConfig) {
|
||||
return new DefaultContainer(containerConfig);
|
||||
}
|
||||
}
|
||||
@@ -4,7 +4,7 @@ import io.ebean.BackgroundExecutor;
|
||||
import io.ebean.cache.ServerCacheFactory;
|
||||
import io.ebean.cache.ServerCacheOptions;
|
||||
import io.ebean.cache.ServerCachePlugin;
|
||||
import io.ebean.common.SpiContainer;
|
||||
import io.ebean.service.SpiContainer;
|
||||
import io.ebean.config.ContainerConfig;
|
||||
import io.ebean.config.PropertyMap;
|
||||
import io.ebean.config.ServerConfig;
|
||||
@@ -71,7 +71,7 @@ public class DefaultContainer implements SpiContainer {
|
||||
|
||||
@Override
|
||||
public void shutdown() {
|
||||
clusterManager.shutdown();
|
||||
ShutdownManager.shutdown();
|
||||
}
|
||||
|
||||
/**
|
||||
|
||||
@@ -1,14 +0,0 @@
|
||||
package io.ebeaninternal.server.lib;
|
||||
|
||||
import io.ebean.service.SpiContainerShutdown;
|
||||
|
||||
/**
|
||||
* Default container shutdown implementation.
|
||||
*/
|
||||
public class DContainerShutdown implements SpiContainerShutdown {
|
||||
|
||||
@Override
|
||||
public void shutdown() {
|
||||
ShutdownManager.shutdown();
|
||||
}
|
||||
}
|
||||
@@ -1,6 +1,6 @@
|
||||
package io.ebeaninternal.server.lib;
|
||||
|
||||
import io.ebean.common.SpiContainer;
|
||||
import io.ebean.service.SpiContainer;
|
||||
import io.ebeaninternal.api.ClassUtil;
|
||||
import io.ebeaninternal.api.SpiEbeanServer;
|
||||
import org.slf4j.Logger;
|
||||
|
||||
@@ -0,0 +1 @@
|
||||
io.ebeaninternal.server.DContainerFactory
|
||||
@@ -1 +0,0 @@
|
||||
io.ebeaninternal.server.lib.DContainerShutdown
|
||||
Reference in New Issue
Block a user