diff --git a/sofa-serverless-runtime/sofa-serverless-common/src/main/java/com/alipay/sofa/serverless/common/util/MultiBizProperties.java b/sofa-serverless-runtime/sofa-serverless-common/src/main/java/com/alipay/sofa/serverless/common/util/MultiBizProperties.java index aab07a315..8fd9e1b9d 100644 --- a/sofa-serverless-runtime/sofa-serverless-common/src/main/java/com/alipay/sofa/serverless/common/util/MultiBizProperties.java +++ b/sofa-serverless-runtime/sofa-serverless-common/src/main/java/com/alipay/sofa/serverless/common/util/MultiBizProperties.java @@ -35,21 +35,25 @@ */ public class MultiBizProperties extends Properties { - private final String bizClassLoaderName; + private final String bizClassLoaderName; - private static final String BIZ_CLASS_LOADER = "com.alipay.sofa.ark.container.service.classloader.BizClassLoader"; + private static final String BIZ_CLASS_LOADER = "com.alipay.sofa.ark.container.service.classloader.BizClassLoader"; - private Map> modifiedKeysMap = new HashMap<>(); + private Map> modifiedKeysMap = new HashMap<>(); - private final Properties baseProperties; - private Map bizPropertiesMap; + private final Properties baseProperties; + private Map bizPropertiesMap; - public MultiBizProperties(String bizClassLoaderName, Properties baseProperties) { + private MultiBizProperties(String bizClassLoaderName, Properties baseProperties) { this.bizPropertiesMap = new HashMap<>(); this.baseProperties = baseProperties; this.bizClassLoaderName = bizClassLoaderName; } + public MultiBizProperties(String bizClassLoaderName) { + this(bizClassLoaderName, new Properties()); + } + public synchronized Object setProperty(String key, String value) { addModifiedKey(key); return getWriteProperties().setProperty(key, value); @@ -199,9 +203,10 @@ public synchronized void clear() { public synchronized Object clone() { MultiBizProperties mbp = new MultiBizProperties(bizClassLoaderName, baseProperties); mbp.bizPropertiesMap = new HashMap<>(); - bizPropertiesMap.forEach((k, p) -> mbp.put(k, p.clone())); + bizPropertiesMap.forEach((k, p) -> mbp.bizPropertiesMap.put(k, (Properties) p.clone())); mbp.bizPropertiesMap.putAll(bizPropertiesMap); - mbp.modifiedKeysMap = modifiedKeysMap; + mbp.modifiedKeysMap = new HashMap<>(); + modifiedKeysMap.forEach((k, s) -> mbp.modifiedKeysMap.put(k, new HashSet<>(s))); return mbp; } @@ -435,7 +440,7 @@ private void addModifiedKeys(Collection keys) { public static void initSystem(String bizClassLoaderName) { Properties properties = System.getProperties(); MultiBizProperties multiBizProperties = new MultiBizProperties(bizClassLoaderName, - properties); + properties); System.setProperties(multiBizProperties); } diff --git a/sofa-serverless-runtime/sofa-serverless-common/src/test/java/com/alipay/sofa/serverless/common/util/MultiBizPropertiesTest.java b/sofa-serverless-runtime/sofa-serverless-common/src/test/java/com/alipay/sofa/serverless/common/util/MultiBizPropertiesTest.java index 02613c844..19f099a59 100644 --- a/sofa-serverless-runtime/sofa-serverless-common/src/test/java/com/alipay/sofa/serverless/common/util/MultiBizPropertiesTest.java +++ b/sofa-serverless-runtime/sofa-serverless-common/src/test/java/com/alipay/sofa/serverless/common/util/MultiBizPropertiesTest.java @@ -21,15 +21,19 @@ import org.junit.Before; import org.junit.Test; +import java.io.ByteArrayInputStream; +import java.io.ByteArrayOutputStream; +import java.io.IOException; import java.net.URL; import java.net.URLClassLoader; +import java.util.Properties; public class MultiBizPropertiesTest { - private final String key1 = "test-key-1"; + private final String key1 = "test-key-1"; private final String value1 = "test-value-1"; private final String value2 = "test-value-2"; - private ClassLoader baseClassLoader; + private ClassLoader baseClassLoader; @Before public void before() { @@ -103,4 +107,54 @@ public void testGetAndClear() { Assert.assertEquals(value2, System.getProperty(key1)); } + @Test + public void testClone() { + Properties properties = System.getProperties(); + Properties other = (Properties) properties.clone(); + Assert.assertEquals(properties, other); + } + + + @Test + public void testStoreAndLoad() throws IOException { + ByteArrayOutputStream out = new ByteArrayOutputStream(); + Properties properties = new MultiBizProperties(URLClassLoader.class.getName()); + properties.put(key1, value1); + properties.putAll(System.getProperties()); + int size = properties.size(); + properties.store(out, "test store"); + properties.clear(); + Assert.assertEquals(properties.size(), 0); + ByteArrayInputStream input = new ByteArrayInputStream(out.toByteArray()); + properties.load(input); + Assert.assertEquals(properties.size(), size); + + + out = new ByteArrayOutputStream(); + properties.save(out, "test store"); + properties.clear(); + Assert.assertEquals(properties.size(), 0); + input = new ByteArrayInputStream(out.toByteArray()); + properties.load(input); + Assert.assertEquals(properties.size(), size); + + + out = new ByteArrayOutputStream(); + properties.storeToXML(out, "test store"); + properties.clear(); + Assert.assertEquals(properties.size(), 0); +// System.out.println(out); + input = new ByteArrayInputStream(out.toByteArray()); + try { + properties.loadFromXML(input); + } catch (Throwable e) { + e.printStackTrace(); + } + + Assert.assertEquals(properties.size(), size); + + Assert.assertTrue(properties.containsKey(key1)); + Assert.assertTrue(properties.contains(value1)); + } + } \ No newline at end of file