turn DDL generation into plugin, allow for other plugins to run

This commit is contained in:
Richard Vowles committed 2013-04-07 21:56:24 +12:00
1 parent 22f67ee49a
commit cef9dd6cda
6 files changed
+77 -13

No files matched your search

@@ -14,6 +14,7 @@ import java.util.Set;
import java.util.concurrent.FutureTask;
import java.util.logging.Level;
import java.util.logging.Logger;
import java.util.ServiceLoader;
import javax.management.InstanceAlreadyExistsException;
import javax.management.MBeanServer;
@@ -68,6 +69,7 @@ import com.avaje.ebeaninternal.api.LoadManyRequest;
import com.avaje.ebeaninternal.api.ScopeTrans;
import com.avaje.ebeaninternal.api.SpiBackgroundExecutor;
import com.avaje.ebeaninternal.api.SpiEbeanServer;
import com.avaje.ebeaninternal.api.SpiEbeanPlugin;
import com.avaje.ebeaninternal.api.SpiQuery;
import com.avaje.ebeaninternal.api.SpiQuery.Mode;
import com.avaje.ebeaninternal.api.SpiQuery.Type;
@@ -177,7 +179,8 @@ public final class DefaultServer implements SpiEbeanServer {
private final CQueryEngine cqueryEngine;
private final DdlGenerator ddlGenerator;
@Deprecated
private DdlGenerator ddlGenerator;
private final ExpressionFactory ldapExpressionFactory = new LdapExpressionFactory();
@@ -213,6 +216,12 @@ public final class DefaultServer implements SpiEbeanServer {
*/
private PstmtBatch pstmtBatch;
/**
* holds plugins (e.g. ddl generator) detected by the service loader
*/
private List<SpiEbeanPlugin> ebeanPlugins;
/**
* Create the DefaultServer.
*/
@@ -254,7 +263,6 @@ public final class DefaultServer implements SpiEbeanServer {
this.autoFetchManager = config.createAutoFetchManager(this);
this.adminAutofetch = new MAdminAutofetch(autoFetchManager);
this.ddlGenerator = new DdlGenerator(this, config.getDatabasePlatform(), config.getServerConfig());
this.beanLoader = new DefaultBeanLoader(this, config.getDebugLazyLoad());
this.jsonContext = config.createJsonContext(this);
@@ -265,9 +273,40 @@ public final class DefaultServer implements SpiEbeanServer {
this.ldapQueryEngine = new LdapOrmQueryEngine(ldapConfig.isVanillaMode(), ldapConfig.getContextFactory());
}
loadAndInitializePlugins(config);
ShutdownManager.register(new Shutdown());
}
protected void loadAndInitializePlugins(InternalConfiguration config) {
List<SpiEbeanPlugin> spiPlugins = new ArrayList<SpiEbeanPlugin>();
final Iterator<SpiEbeanPlugin> plugins = ServiceLoader.load(SpiEbeanPlugin.class).iterator();
while (plugins.hasNext()) {
SpiEbeanPlugin plugin = plugins.next();
spiPlugins.add(plugin);
plugin.setup(this, this.getDatabasePlatform(), config.getServerConfig());
if (plugin instanceof DdlGenerator) // backwards compatible
ddlGenerator = (DdlGenerator)plugin;
}
ebeanPlugins = Collections.unmodifiableList(spiPlugins);
}
public List<SpiEbeanPlugin> getSpiEbeanPlugins() {
return ebeanPlugins;
}
public void executePlugins(boolean online) {
for(SpiEbeanPlugin plugin : ebeanPlugins) {
plugin.execute(online);
}
}
public boolean isDefaultDeleteMissingChildren() {
return defaultDeleteMissingChildren;
}
@@ -227,7 +227,8 @@ public class DefaultServerFactory implements BootupEbeanManager {
server.registerMBeans(mbeanServer, uniqueServerId);
// generate and run DDL if required
executeDDL(server, online);
// if there are any other tasks requiring action in their plugins, do them as well
server.executePlugins(online);
// initialise prior to registering with clusterManager
server.initialise();
@@ -22,18 +22,19 @@ import com.avaje.ebean.Transaction;
import com.avaje.ebean.config.NamingConvention;
import com.avaje.ebean.config.ServerConfig;
import com.avaje.ebean.config.dbplatform.DatabasePlatform;
import com.avaje.ebeaninternal.api.SpiEbeanPlugin;
import com.avaje.ebeaninternal.api.SpiEbeanServer;
/**
* Controls the generation of DDL and potentially runs the resulting scripts.
*/
public class DdlGenerator {
public class DdlGenerator implements SpiEbeanPlugin {
private static final Logger logger = Logger.getLogger(DdlGenerator.class.getName());
private static final Logger logger = Logger.getLogger(DdlGenerator.class.getName());
private final SpiEbeanServer server;
private SpiEbeanServer server;
private final DatabasePlatform dbPlatform;
private DatabasePlatform dbPlatform;
private int summaryLength = 80;
@@ -45,7 +46,7 @@ public class DdlGenerator {
private NamingConvention namingConvention;
public DdlGenerator(SpiEbeanServer server, DatabasePlatform dbPlatform, ServerConfig serverConfig) {
public void setup(SpiEbeanServer server, DatabasePlatform dbPlatform, ServerConfig serverConfig) {
this.server = server;
this.dbPlatform = dbPlatform;
this.generateDdl = serverConfig.isDdlGenerate();