diff --git a/src/java/com/samskivert/jdbc/depot/DepotRepository.java b/src/java/com/samskivert/jdbc/depot/DepotRepository.java index c9734b5..d116c10 100644 --- a/src/java/com/samskivert/jdbc/depot/DepotRepository.java +++ b/src/java/com/samskivert/jdbc/depot/DepotRepository.java @@ -27,6 +27,7 @@ import java.sql.SQLException; import java.util.Collection; import java.util.List; import java.util.Map; +import java.util.Set; import com.samskivert.io.PersistenceException; import com.samskivert.util.ArrayUtil; @@ -50,7 +51,7 @@ import com.samskivert.jdbc.depot.expression.ValueExp; * Provides a base for classes that provide access to persistent objects. Also defines the * mechanism by which all persistent queries and updates are routed through the distributed cache. */ -public class DepotRepository +public abstract class DepotRepository { /** * Creates a repository with the supplied connection provider and its own private persistence @@ -59,6 +60,7 @@ public class DepotRepository protected DepotRepository (ConnectionProvider conprov) { _ctx = new PersistenceContext(getClass().getName(), conprov); + _ctx.repositoryCreated(this); } /** @@ -67,8 +69,14 @@ public class DepotRepository protected DepotRepository (PersistenceContext context) { _ctx = context; + _ctx.repositoryCreated(this); } + /** + * Adds the persistent classes used by this repository to the supplied set. + */ + protected abstract void getManagedRecords (Set> classes); + /** * Loads the persistent object that matches the specified primary key. */ diff --git a/src/java/com/samskivert/jdbc/depot/PersistenceContext.java b/src/java/com/samskivert/jdbc/depot/PersistenceContext.java index 4d6eeba..db72591 100644 --- a/src/java/com/samskivert/jdbc/depot/PersistenceContext.java +++ b/src/java/com/samskivert/jdbc/depot/PersistenceContext.java @@ -20,8 +20,11 @@ package com.samskivert.jdbc.depot; +import java.util.ArrayList; +import java.util.Arrays; import java.util.HashMap; import java.util.HashSet; +import java.util.List; import java.util.Map; import java.util.Set; @@ -393,6 +396,29 @@ public class PersistenceContext listenerSet.add(listener); } + /** + * Iterates over all {@link PersistentRecord} classes managed by this context and initializes + * their {@link DepotMarshaller}. This forces migrations to run and the database schema to be + * created. + */ + public void initializeManagedRecords () + throws PersistenceException + { + for (Class rclass : _managedRecords) { + getMarshaller(rclass); + } + } + + /** + * Called when a depot repository is created. We register all persistent record classes used by + * the repository so that systems that desire it can force the resolution of all database + * tables rather than allowing resolution to happen on demand. + */ + protected void repositoryCreated (DepotRepository repo) + { + repo.getManagedRecords(_managedRecords); + } + /** * Looks up and creates, but does not initialize, the marshaller for the specified Entity * type. @@ -474,4 +500,16 @@ public class PersistenceContext protected Map, DepotMarshaller> _marshallers = new HashMap, DepotMarshaller>(); + + /** + * The set of persistent records for which this context is responsible. This data is used by + * {@link #initializeManagedRecords} to force migration/schema initialization. + */ + protected Set> _managedRecords = + new HashSet>(); + + /** + * A collection of all {@link PersistenceContext} objects in existence. + */ + protected static List _contexts = new ArrayList(); }