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
This commit is contained in:
Pinaki Poddar 2008-07-02 19:50:37 +00:00
parent 25a69ffe0a
commit 1ad09239b3
2 changed files with 53 additions and 1 deletions

View File

@ -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;
}
}
}

View File

@ -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());
}
}