Adding support for change of configuration properties after the configuration has been frozen.

Three methods have been added to Configuration:
  i) Configuration.getDynamicValues() returns list of Values that are dynamically modifiable.
 ii) Configuration.isDynamic(String property) affirms if the named property is dynamically modifiable.
iii) Configuration.modifyDynamic(String property, Object value) modifies the named property value even when Configuration.isReadOnly().

  Currently, OpenJPAConfigurationImpl.getDynamicValues() return 3 simple IntValue properties {dataCacheTimeout, fetchBatchSize, lockTimeout) 
 
  


git-svn-id: https://svn.apache.org/repos/asf/openjpa/trunk@566494 13f79535-47bb-0310-9956-ffa450edef68
This commit is contained in:
Pinaki Poddar 2007-08-16 05:52:17 +00:00
parent 88f293dec9
commit 8d1dacad82
5 changed files with 180 additions and 4 deletions

View File

@ -1437,4 +1437,9 @@ public class OpenJPAConfigurationImpl
public Log getConfigurationLog() {
return getLog(LOG_RUNTIME);
}
public Value[] getDynamicValues() {
return new Value[] {dataCacheTimeout,fetchBatchSize,lockTimeout};
}
}

View File

@ -223,4 +223,22 @@ public interface Configuration
* Return a copy of this configuration.
*/
public Object clone();
/**
* Modifies a <em>dynamic</em> property of this receiver even when
* {@link #setReadOnly(boolean) frozen}.
*/
public void modifyDynamic(String property, Object newValue);
/**
* Affirms if the given property can be modified <em>dynamically</em> i.e.
* even after the receiver is {@link #setReadOnly(boolean) frozen}.
*/
public boolean isDynamic(String property);
/**
* Gets the values that can be modified <em>dynamically</em> i.e.
* even after the receiver is {@link #setReadOnly(boolean) frozen}.
*/
public Value[] getDynamicValues();
}

View File

@ -803,7 +803,7 @@ public class ConfigurationImpl
ConfigurationImpl conf = (ConfigurationImpl) other;
Map p1 = (_props == null) ? toProperties(false) : _props;
Map p2 = (conf._props == null) ? conf.toProperties(false) : conf._props;
return p1.equals(p2);
return excludeDynamic(p1).equals(excludeDynamic(p2));
}
/**
@ -811,9 +811,8 @@ public class ConfigurationImpl
* {@link #toProperties}.
*/
public int hashCode() {
if (_props != null)
return _props.hashCode();
return toProperties(false).hashCode();
Map copy = (_props == null) ? toProperties(false) : _props;
return excludeDynamic(copy).hashCode();
}
/**
@ -989,4 +988,35 @@ public class ConfigurationImpl
addValue(val);
return val;
}
public void modifyDynamic(String property, Object newValue) {
if (!isDynamic(property))
throw new RuntimeException(_loc.get("not-dynamic", property)
.toString());
Value value = getValue(property);
value.setObject(newValue);
}
public boolean isDynamic(String property) {
Value[] dynamicValues = getDynamicValues();
for (int i=0; i<dynamicValues.length; i++)
if (dynamicValues[i].getProperty().equals(property))
return true;
return false;
}
public Value[] getDynamicValues() {
return new Value[0];
}
Map excludeDynamic(Map map) {
if (map == null)
return null;
Map copy = new HashMap(map);
Value[] dynamicValues = getDynamicValues();
for (int i=0; i<dynamicValues.length; i++) {
Configurations.removeProperty(dynamicValues[i].getProperty(), copy);
}
return copy;
}
}

View File

@ -110,3 +110,5 @@ Id-type: General
Id-cat: General
Id-displayorder: 50
Id-expert: true
not-dynamic: Can not modify "{0}" to "{1}" because the property is not dynamic.

View File

@ -0,0 +1,121 @@
/*
* Licensed to the Apache Software Foundation (ASF) under one
* or more contributor license agreements. See the NOTICE file
* distributed with this work for additional information
* regarding copyright ownership. The ASF licenses this file
* to you under the Apache License, Version 2.0 (the
* "License"); you may not use this file except in compliance
* with the License. You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing,
* software distributed under the License is distributed on an
* "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
* KIND, either express or implied. See the License for the
* specific language governing permissions and limitations
* under the License.
*/
package org.apache.openjpa.conf;
import javax.persistence.Persistence;
import org.apache.openjpa.persistence.test.*;
import org.apache.openjpa.conf.OpenJPAConfiguration;
import org.apache.openjpa.lib.conf.Value;
import org.apache.openjpa.persistence.OpenJPAEntityManagerFactory;
import org.apache.openjpa.persistence.OpenJPAPersistence;
/**
* Tests dynamic modification of configuration property.
*
* @author Pinaki Poddar
*
*/
public class TestDynamicConfiguration extends SingleEMFTestCase {
public void testConfigurationIsEqualByValueAndHashCode() {
OpenJPAEntityManagerFactory emf1 = createEMF();
assertNotNull(emf1);
OpenJPAConfiguration conf1 = emf1.getConfiguration();
OpenJPAEntityManagerFactory emf2 = createEMF();
assertNotNull(emf2);
OpenJPAConfiguration conf2 = emf2.getConfiguration();
assertFalse(emf1==emf2);
assertFalse(emf1.equals(emf2));
assertFalse(conf1==conf2);
assertEquals(conf1, conf2);
assertEquals(conf1.hashCode(), conf2.hashCode());
assertEquals(conf1.toProperties(false), conf2.toProperties(false));
}
public void testConfigurationIsReadOnlyAfterFirstConstruction() {
OpenJPAEntityManagerFactory emf = createEMF();
assertNotNull(emf);
OpenJPAConfiguration conf = emf.getConfiguration();
assertFalse(conf.isReadOnly());
emf.createEntityManager();
assertTrue(conf.isReadOnly());
}
public void testDynamicValuesCanNotBeChangedDirectly() {
OpenJPAEntityManagerFactory emf = createEMF();
assertNotNull(emf);
emf.createEntityManager();
OpenJPAConfiguration conf = emf.getConfiguration();
Value[] dynamicValues = conf.getDynamicValues();
assertTrue(dynamicValues.length>0);
assertTrue(conf.isDynamic("LockTimeout"));
int oldValue = conf.getLockTimeout();
int newValue = oldValue + 10;
try {
conf.setLockTimeout(newValue);
fail("Expected exception to modify configuration directly");
} catch (Exception ex) { // good
assertEquals(oldValue, conf.getLockTimeout());
}
}
public void testDynamicValuesCanBeChanged() {
OpenJPAEntityManagerFactory emf = createEMF();
assertNotNull(emf);
OpenJPAConfiguration conf = emf.getConfiguration();
Value[] dynamicValues = conf.getDynamicValues();
assertTrue(dynamicValues.length>0);
assertTrue(conf.isDynamic("LockTimeout"));
int oldValue = conf.getLockTimeout();
int newValue = oldValue + 10;
conf.modifyDynamic("LockTimeout", newValue);
assertEquals(newValue, conf.getLockTimeout());
}
public void testDynamicValuesAreCorrectlySet() {
OpenJPAEntityManagerFactory emf = createEMF();
assertNotNull(emf);
OpenJPAConfiguration conf = emf.getConfiguration();
Value[] dynamicValues = conf.getDynamicValues();
assertTrue(dynamicValues.length>0);
assertTrue(conf.isDynamic("LockTimeout"));
}
public void testDynamicChangeDoesNotChangeHashCode() {
OpenJPAEntityManagerFactory emf = createEMF();
assertNotNull(emf);
OpenJPAConfiguration conf1 = emf.getConfiguration();
int oldValue = conf1.getLockTimeout();
int newValue = oldValue+10;
int oldHash = conf1.hashCode();
conf1.modifyDynamic("LockTimeout", newValue);
int newHash = conf1.hashCode();
assertEquals(oldHash, newHash);
}
}