From 1ad09239b378a4c495a8a35cd61cda8b807b22d0 Mon Sep 17 00:00:00 2001 From: Pinaki Poddar Date: Wed, 2 Jul 2008 19:50:37 +0000 Subject: [PATCH] OPENJPA-647: ClassMetaData listens to value updates of dynamic property git-svn-id: https://svn.apache.org/repos/asf/openjpa/trunk@673476 13f79535-47bb-0310-9956-ffa450edef68 --- .../apache/openjpa/meta/ClassMetaData.java | 26 ++++++++++++++++- .../conf/TestDynamicConfiguration.java | 28 +++++++++++++++++++ 2 files changed, 53 insertions(+), 1 deletion(-) diff --git a/openjpa-kernel/src/main/java/org/apache/openjpa/meta/ClassMetaData.java b/openjpa-kernel/src/main/java/org/apache/openjpa/meta/ClassMetaData.java index 264df7fe4..787823c65 100644 --- a/openjpa-kernel/src/main/java/org/apache/openjpa/meta/ClassMetaData.java +++ b/openjpa-kernel/src/main/java/org/apache/openjpa/meta/ClassMetaData.java @@ -41,6 +41,8 @@ import org.apache.openjpa.datacache.DataCache; import org.apache.openjpa.enhance.PCRegistry; import org.apache.openjpa.enhance.Reflection; import org.apache.openjpa.enhance.PersistenceCapable; +import org.apache.openjpa.lib.conf.Value; +import org.apache.openjpa.lib.conf.ValueListener; import org.apache.openjpa.lib.log.Log; import org.apache.openjpa.lib.meta.SourceTracker; import org.apache.openjpa.lib.util.J2DoPrivHelper; @@ -79,7 +81,7 @@ import serp.util.Strings; public class ClassMetaData extends Extensions implements Comparable, SourceTracker, MetaDataContext, MetaDataModes, - Commentable { + Commentable, ValueListener { /** * Unkonwn identity type. @@ -196,6 +198,7 @@ public class ClassMetaData _repos = repos; _owner = null; setDescribedType(type); + registerForValueUpdate("DataCacheTimeout"); } /** @@ -206,6 +209,7 @@ public class ClassMetaData _repos = owner.getRepository(); setEnvClassLoader(owner.getFieldMetaData().getDefiningMetaData(). getEnvClassLoader()); + registerForValueUpdate("DataCacheTimeout"); } /** @@ -2351,4 +2355,24 @@ public class ClassMetaData return f1.getListingIndex () - f2.getListingIndex (); } } + + public void registerForValueUpdate(String...values) { + if (values == null) + return; + for (String key : values) { + Value value = getRepository().getConfiguration() + .getValue(key); + if (value != null) + value.setListener(this); + } + } + + public void valueChanged(Value val) { + if (val == null) + return; + String key = val.getProperty(); + if ("DataCacheTimeout".equals(key)) { + _cacheTimeout = Integer.MIN_VALUE; + } + } } diff --git a/openjpa-persistence-jdbc/src/test/java/org/apache/openjpa/conf/TestDynamicConfiguration.java b/openjpa-persistence-jdbc/src/test/java/org/apache/openjpa/conf/TestDynamicConfiguration.java index 83dc83028..5cebd277b 100644 --- a/openjpa-persistence-jdbc/src/test/java/org/apache/openjpa/conf/TestDynamicConfiguration.java +++ b/openjpa-persistence-jdbc/src/test/java/org/apache/openjpa/conf/TestDynamicConfiguration.java @@ -18,9 +18,13 @@ */ package org.apache.openjpa.conf; +import javax.persistence.EntityManager; + import org.apache.openjpa.lib.conf.Value; +import org.apache.openjpa.meta.ClassMetaData; import org.apache.openjpa.persistence.OpenJPAEntityManagerFactory; import org.apache.openjpa.persistence.OpenJPAEntityManagerFactorySPI; +import org.apache.openjpa.persistence.datacache.common.apps.PObject; import org.apache.openjpa.persistence.test.SingleEMFTestCase; /** @@ -31,6 +35,10 @@ import org.apache.openjpa.persistence.test.SingleEMFTestCase; */ public class TestDynamicConfiguration extends SingleEMFTestCase { + public void setUp() throws Exception { + super.setUp(PObject.class); + } + public void testConfigurationIsEqualByValueAndHashCode() { OpenJPAEntityManagerFactorySPI emf1 = createEMF(); assertNotNull(emf1); @@ -102,4 +110,24 @@ public class TestDynamicConfiguration extends SingleEMFTestCase { assertEquals(oldHash, newHash); } + + public void testClassMetaDataRecognizesDataCacheTimeoutValueChange() { + OpenJPAConfiguration conf = emf.getConfiguration(); + EntityManager em = emf.createEntityManager(); + em.getTransaction().begin(); + PObject pc = new PObject(); + em.persist(pc); + + int oldValue = conf.getDataCacheTimeout(); + + ClassMetaData meta = conf.getMetaDataRepositoryInstance().getCachedMetaData(PObject.class); + assertNotNull(meta); + assertEquals(oldValue, meta.getDataCacheTimeout()); + + int newValue = oldValue + 10; + conf.setDataCacheTimeout(newValue); + assertEquals(newValue, conf.getDataCacheTimeout()); + assertEquals(newValue, meta.getDataCacheTimeout()); + + } }