From b2e1912dc7dd87c741ee2855a740e964454f74f1 Mon Sep 17 00:00:00 2001 From: "A. Abram White" Date: Wed, 2 Aug 2006 17:51:43 +0000 Subject: [PATCH] Fix StoreFacadeTypeRegistry to work when multiple stores are available. Fix problems with creating a persistence FetchPlan, and with getting max depth. git-svn-id: https://svn.apache.org/repos/asf/incubator/openjpa/trunk@428069 13f79535-47bb-0310-9956-ffa450edef68 --- .../meta/strats/RelationFieldStrategy.java | 4 +- .../openjpa/util/StoreFacadeTypeRegistry.java | 57 +++++++++++++++++-- .../JDBCPersistenceProductDerivation.java | 5 +- .../persistence/simple/TestPersistence.java | 7 +++ .../persistence/EntityManagerFactoryImpl.java | 15 +++-- .../persistence/EntityManagerImpl.java | 3 +- .../apache/openjpa/persistence/Extent.java | 4 +- .../apache/openjpa/persistence/FetchPlan.java | 2 +- .../apache/openjpa/persistence/QueryImpl.java | 4 +- 9 files changed, 79 insertions(+), 22 deletions(-) diff --git a/openjpa-jdbc/src/main/java/org/apache/openjpa/jdbc/meta/strats/RelationFieldStrategy.java b/openjpa-jdbc/src/main/java/org/apache/openjpa/jdbc/meta/strats/RelationFieldStrategy.java index db69678fa..2497cdbf2 100644 --- a/openjpa-jdbc/src/main/java/org/apache/openjpa/jdbc/meta/strats/RelationFieldStrategy.java +++ b/openjpa-jdbc/src/main/java/org/apache/openjpa/jdbc/meta/strats/RelationFieldStrategy.java @@ -492,8 +492,8 @@ public class RelationFieldStrategy long id = res.getLong(cols[0]); if (!res.wasNull()) oid = store.newDataStoreId(id, relMapping, true); - } else // application id - { + } else { + // application id if (cols.length == 1) { Object val = res.getObject(cols[0], null, null); if (val != null) diff --git a/openjpa-kernel/src/main/java/org/apache/openjpa/util/StoreFacadeTypeRegistry.java b/openjpa-kernel/src/main/java/org/apache/openjpa/util/StoreFacadeTypeRegistry.java index 5d07d06ee..7b850b94a 100644 --- a/openjpa-kernel/src/main/java/org/apache/openjpa/util/StoreFacadeTypeRegistry.java +++ b/openjpa-kernel/src/main/java/org/apache/openjpa/util/StoreFacadeTypeRegistry.java @@ -13,11 +13,60 @@ public class StoreFacadeTypeRegistry { private Map _impls = new ConcurrentHashMap(); - public void registerImplementation(Class facadeType, Class implType) { - _impls.put(facadeType, implType); + /** + * Register a facade implementation. + * + * @param facadeType the facade interface + * @param storeType the store's + * {@link org.apache.openjpa.kernel.StoreManager} type, or null for generic + * @param implType the class implementing the facade + */ + public void registerImplementation(Class facadeType, Class storeType, + Class implType) { + Object key = (storeType == null) ? (Object)facadeType + : new Key(facadeType, storeType); + _impls.put(key, implType); } - public Class getImplementation(Class facadeType) { - return (Class) _impls.get(facadeType); + /** + * Return the implementation for the given facade and store. + * + * @param facadeType the facade interface + * @param storeType the store's + * {@link org.apache.openjpa.kernel.StoreManager} type, or null for generic + * @param implType the registered implementor + */ + public Class getImplementation(Class facadeType, Class storeType) { + Object key = (storeType == null) ? (Object)facadeType + : new Key(facadeType, storeType); + Class c = (Class) _impls.get(key); + // if no store-specific type, see if there is a generic avaialble + if (c == null && storeType != null) + c = (Class) _impls.get(facadeType); + return c; + } + + /** + * Lookup key for facade+store hash. + */ + private static class Key { + private final Class _facadeType; + private final Class _storeType; + + public Key(Class facadeType, Class storeType) { + _facadeType = facadeType; + _storeType = storeType; + } + + public int hashCode() { + return _facadeType.hashCode() ^ _storeType.hashCode(); + } + + public boolean equals(Object other) { + if (other == this) + return true; + Key k = (Key) other; + return _facadeType == k._facadeType && _storeType == k._storeType; + } } } diff --git a/openjpa-persistence-jdbc/src/main/java/org/apache/openjpa/persistence/jdbc/JDBCPersistenceProductDerivation.java b/openjpa-persistence-jdbc/src/main/java/org/apache/openjpa/persistence/jdbc/JDBCPersistenceProductDerivation.java index 1658c310d..b54c5e5f2 100644 --- a/openjpa-persistence-jdbc/src/main/java/org/apache/openjpa/persistence/jdbc/JDBCPersistenceProductDerivation.java +++ b/openjpa-persistence-jdbc/src/main/java/org/apache/openjpa/persistence/jdbc/JDBCPersistenceProductDerivation.java @@ -18,6 +18,7 @@ package org.apache.openjpa.persistence.jdbc; import org.apache.openjpa.conf.OpenJPAConfiguration; import org.apache.openjpa.conf.ProductDerivation; import org.apache.openjpa.jdbc.conf.JDBCConfigurationImpl; +import org.apache.openjpa.jdbc.kernel.JDBCStoreManager; import org.apache.openjpa.lib.conf.ConfigurationProvider; import org.apache.openjpa.persistence.FetchPlan; import org.apache.openjpa.persistence.PersistenceProductDerivation; @@ -39,8 +40,8 @@ public class JDBCPersistenceProductDerivation } public void beforeConfigurationLoad(OpenJPAConfiguration c) { - c.getStoreFacadeTypeRegistry().registerImplementation( - FetchPlan.class, JDBCFetchPlan.class); + c.getStoreFacadeTypeRegistry().registerImplementation(FetchPlan.class, + JDBCStoreManager.class, JDBCFetchPlan.class); if (!(c instanceof JDBCConfigurationImpl)) return; diff --git a/openjpa-persistence-jdbc/src/test/java/org/apache/openjpa/persistence/simple/TestPersistence.java b/openjpa-persistence-jdbc/src/test/java/org/apache/openjpa/persistence/simple/TestPersistence.java index cd6153ca4..94444d995 100644 --- a/openjpa-persistence-jdbc/src/test/java/org/apache/openjpa/persistence/simple/TestPersistence.java +++ b/openjpa-persistence-jdbc/src/test/java/org/apache/openjpa/persistence/simple/TestPersistence.java @@ -22,6 +22,7 @@ import javax.persistence.Persistence; import junit.framework.TestCase; import junit.textui.TestRunner; +import org.apache.openjpa.persistence.OpenJPAEntityManager; /** * Simple test case to get an EntityManager and perform some basic operations. @@ -69,6 +70,12 @@ public class TestPersistence t.setRollbackOnly(); t.rollback(); + // openjpa-facade test + assertTrue(em instanceof OpenJPAEntityManager); + OpenJPAEntityManager ojem = (OpenJPAEntityManager) em; + ojem.getFetchPlan().setMaxFetchDepth(-1); + assertEquals(-1, ojem.getFetchPlan().getMaxFetchDepth()); + em.close(); } diff --git a/openjpa-persistence/src/main/java/org/apache/openjpa/persistence/EntityManagerFactoryImpl.java b/openjpa-persistence/src/main/java/org/apache/openjpa/persistence/EntityManagerFactoryImpl.java index 991256d21..9a4d2c1ac 100644 --- a/openjpa-persistence/src/main/java/org/apache/openjpa/persistence/EntityManagerFactoryImpl.java +++ b/openjpa-persistence/src/main/java/org/apache/openjpa/persistence/EntityManagerFactoryImpl.java @@ -259,7 +259,7 @@ public class EntityManagerFactoryImpl * Create a store-specific facade for the given fetch configuration. * If no facade class exists, we use the default {@link FetchPlan}. */ - FetchPlan toFetchPlan(FetchConfiguration fetch) { + FetchPlan toFetchPlan(Broker broker, FetchConfiguration fetch) { if (fetch == null) return null; @@ -268,15 +268,16 @@ public class EntityManagerFactoryImpl inner = ((DelegatingFetchConfiguration) inner). getInnermostDelegate(); - _factory.lock(); try { if (_plan == null) { - Class cls = _factory.getConfiguration() - .getStoreFacadeTypeRegistry().getImplementation( - FetchPlan.class); + Class storeType = (broker == null) ? null : broker. + getStoreManager().getInnermostDelegate().getClass(); + Class cls = _factory.getConfiguration(). + getStoreFacadeTypeRegistry(). + getImplementation(FetchPlan.class, storeType); if (cls == null) cls = FetchPlan.class; - _plan = cls.getConstructor(FetchPlan.class); + _plan = cls.getConstructor(FetchConfiguration.class); } return _plan.newInstance(fetch); } catch (InvocationTargetException ite) { @@ -284,8 +285,6 @@ public class EntityManagerFactoryImpl (ite.getTargetException()); } catch (Exception e) { throw PersistenceExceptions.toPersistenceException(e); - } finally { - _factory.unlock(); } } } diff --git a/openjpa-persistence/src/main/java/org/apache/openjpa/persistence/EntityManagerImpl.java b/openjpa-persistence/src/main/java/org/apache/openjpa/persistence/EntityManagerImpl.java index 970abf558..a6b3eb2db 100644 --- a/openjpa-persistence/src/main/java/org/apache/openjpa/persistence/EntityManagerImpl.java +++ b/openjpa-persistence/src/main/java/org/apache/openjpa/persistence/EntityManagerImpl.java @@ -119,7 +119,8 @@ public class EntityManagerImpl _broker.lock(); try { if (_fetch == null) - _fetch = _emf.toFetchPlan(_broker.getFetchConfiguration()); + _fetch = _emf.toFetchPlan(_broker, + _broker.getFetchConfiguration()); return _fetch; } finally { _broker.unlock(); diff --git a/openjpa-persistence/src/main/java/org/apache/openjpa/persistence/Extent.java b/openjpa-persistence/src/main/java/org/apache/openjpa/persistence/Extent.java index 18b83d0a6..6e5af4bed 100644 --- a/openjpa-persistence/src/main/java/org/apache/openjpa/persistence/Extent.java +++ b/openjpa-persistence/src/main/java/org/apache/openjpa/persistence/Extent.java @@ -79,8 +79,8 @@ public class Extent try { if (_fetch == null) _fetch = ((EntityManagerFactoryImpl) _em. - getEntityManagerFactory()).toFetchPlan(_extent. - getFetchConfiguration()); + getEntityManagerFactory()).toFetchPlan(_extent.getBroker(), + _extent.getFetchConfiguration()); return _fetch; } finally { _extent.unlock(); diff --git a/openjpa-persistence/src/main/java/org/apache/openjpa/persistence/FetchPlan.java b/openjpa-persistence/src/main/java/org/apache/openjpa/persistence/FetchPlan.java index e353bf412..daecc2f60 100644 --- a/openjpa-persistence/src/main/java/org/apache/openjpa/persistence/FetchPlan.java +++ b/openjpa-persistence/src/main/java/org/apache/openjpa/persistence/FetchPlan.java @@ -83,7 +83,7 @@ public class FetchPlan { /** * The maximum fetch depth when loading an object. */ - public int getMaxFetchDepth(int depth) { + public int getMaxFetchDepth() { return _fetch.getMaxFetchDepth(); } diff --git a/openjpa-persistence/src/main/java/org/apache/openjpa/persistence/QueryImpl.java b/openjpa-persistence/src/main/java/org/apache/openjpa/persistence/QueryImpl.java index c16338f91..5c66bfec4 100644 --- a/openjpa-persistence/src/main/java/org/apache/openjpa/persistence/QueryImpl.java +++ b/openjpa-persistence/src/main/java/org/apache/openjpa/persistence/QueryImpl.java @@ -99,8 +99,8 @@ public class QueryImpl try { if (_fetch == null) _fetch = ((EntityManagerFactoryImpl) _em. - getEntityManagerFactory()).toFetchPlan(_query. - getFetchConfiguration()); + getEntityManagerFactory()).toFetchPlan(_query.getBroker(), + _query.getFetchConfiguration()); return _fetch; } finally { _query.unlock();