fluo-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From ktur...@apache.org
Subject incubator-fluo git commit: Fixes #813 made SimpleConfiguration serializable
Date Tue, 20 Dec 2016 21:27:13 GMT
Repository: incubator-fluo
Updated Branches:
  refs/heads/master 80833efa1 -> bf133862a


Fixes #813 made SimpleConfiguration serializable


Project: http://git-wip-us.apache.org/repos/asf/incubator-fluo/repo
Commit: http://git-wip-us.apache.org/repos/asf/incubator-fluo/commit/bf133862
Tree: http://git-wip-us.apache.org/repos/asf/incubator-fluo/tree/bf133862
Diff: http://git-wip-us.apache.org/repos/asf/incubator-fluo/diff/bf133862

Branch: refs/heads/master
Commit: bf133862a3874c944a3d588af5c6fe4c0975acce
Parents: 80833ef
Author: Keith Turner <kturner@apache.org>
Authored: Fri Dec 16 17:53:58 2016 -0500
Committer: Keith Turner <kturner@apache.org>
Committed: Fri Dec 16 17:53:58 2016 -0500

----------------------------------------------------------------------
 .../fluo/api/config/FluoConfiguration.java      |  2 +
 .../fluo/api/config/SimpleConfiguration.java    | 68 ++++++++++++++++----
 .../fluo/api/config/FluoConfigurationTest.java  | 38 +++++++++++
 3 files changed, 96 insertions(+), 12 deletions(-)
----------------------------------------------------------------------


http://git-wip-us.apache.org/repos/asf/incubator-fluo/blob/bf133862/modules/api/src/main/java/org/apache/fluo/api/config/FluoConfiguration.java
----------------------------------------------------------------------
diff --git a/modules/api/src/main/java/org/apache/fluo/api/config/FluoConfiguration.java b/modules/api/src/main/java/org/apache/fluo/api/config/FluoConfiguration.java
index 801a025..53a4819 100644
--- a/modules/api/src/main/java/org/apache/fluo/api/config/FluoConfiguration.java
+++ b/modules/api/src/main/java/org/apache/fluo/api/config/FluoConfiguration.java
@@ -37,6 +37,8 @@ import org.slf4j.LoggerFactory;
  */
 public class FluoConfiguration extends SimpleConfiguration {
 
+  private static final long serialVersionUID = 1L;
+
   private static final Logger log = LoggerFactory.getLogger(FluoConfiguration.class);
 
   public static final String FLUO_PREFIX = "fluo";

http://git-wip-us.apache.org/repos/asf/incubator-fluo/blob/bf133862/modules/api/src/main/java/org/apache/fluo/api/config/SimpleConfiguration.java
----------------------------------------------------------------------
diff --git a/modules/api/src/main/java/org/apache/fluo/api/config/SimpleConfiguration.java
b/modules/api/src/main/java/org/apache/fluo/api/config/SimpleConfiguration.java
index 9877b18..545f761 100644
--- a/modules/api/src/main/java/org/apache/fluo/api/config/SimpleConfiguration.java
+++ b/modules/api/src/main/java/org/apache/fluo/api/config/SimpleConfiguration.java
@@ -15,9 +15,15 @@
 
 package org.apache.fluo.api.config;
 
+import java.io.ByteArrayInputStream;
+import java.io.ByteArrayOutputStream;
 import java.io.File;
+import java.io.IOException;
 import java.io.InputStream;
+import java.io.ObjectInputStream;
+import java.io.ObjectOutputStream;
 import java.io.OutputStream;
+import java.io.Serializable;
 import java.util.Iterator;
 import java.util.Map;
 import java.util.Map.Entry;
@@ -41,17 +47,35 @@ import org.apache.fluo.api.exceptions.FluoException;
  * @since 1.0.0
  */
 
-public class SimpleConfiguration {
+public class SimpleConfiguration implements Serializable {
 
-  private Configuration internalConfig;
+  private static final long serialVersionUID = 1L;
 
-  public SimpleConfiguration() {
+  private transient Configuration internalConfig;
+
+  private void init() {
     CompositeConfiguration compositeConfig = new CompositeConfiguration();
     compositeConfig.setThrowExceptionOnMissing(true);
     compositeConfig.setDelimiterParsingDisabled(true);
     internalConfig = compositeConfig;
   }
 
+  private void load(InputStream in) {
+    try {
+      PropertiesConfiguration config = new PropertiesConfiguration();
+      // disabled to prevent accumulo classpath value from being shortened
+      config.setDelimiterParsingDisabled(true);
+      config.load(in);
+      ((CompositeConfiguration) internalConfig).addConfiguration(config);
+    } catch (ConfigurationException e) {
+      throw new IllegalArgumentException(e);
+    }
+  }
+
+  public SimpleConfiguration() {
+    init();
+  }
+
   private SimpleConfiguration(Configuration subset) {
     this.internalConfig = subset;
   }
@@ -77,15 +101,7 @@ public class SimpleConfiguration {
    */
   public SimpleConfiguration(InputStream in) {
     this();
-    try {
-      PropertiesConfiguration config = new PropertiesConfiguration();
-      // disabled to prevent accumulo classpath value from being shortened
-      config.setDelimiterParsingDisabled(true);
-      config.load(in);
-      ((CompositeConfiguration) internalConfig).addConfiguration(config);
-    } catch (ConfigurationException e) {
-      throw new IllegalArgumentException(e);
-    }
+    load(in);
   }
 
   /**
@@ -237,4 +253,32 @@ public class SimpleConfiguration {
 
     return builder.build();
   }
+
+  /*
+   * These custom serialization methods were added because commons config does not support
+   * serialization.
+   */
+  private void writeObject(ObjectOutputStream out) throws IOException {
+    out.defaultWriteObject();
+
+    ByteArrayOutputStream baos = new ByteArrayOutputStream();
+    save(baos);
+
+    byte[] data = baos.toByteArray();
+
+    out.writeInt(data.length);
+    out.write(data);
+  }
+
+  private void readObject(ObjectInputStream in) throws IOException, ClassNotFoundException
{
+    in.defaultReadObject();
+    init();
+
+    int len = in.readInt();
+    byte[] data = new byte[len];
+    in.readFully(data);
+
+    ByteArrayInputStream bais = new ByteArrayInputStream(data);
+    load(bais);
+  }
 }

http://git-wip-us.apache.org/repos/asf/incubator-fluo/blob/bf133862/modules/api/src/test/java/org/apache/fluo/api/config/FluoConfigurationTest.java
----------------------------------------------------------------------
diff --git a/modules/api/src/test/java/org/apache/fluo/api/config/FluoConfigurationTest.java
b/modules/api/src/test/java/org/apache/fluo/api/config/FluoConfigurationTest.java
index f7d7c97..38705db 100644
--- a/modules/api/src/test/java/org/apache/fluo/api/config/FluoConfigurationTest.java
+++ b/modules/api/src/test/java/org/apache/fluo/api/config/FluoConfigurationTest.java
@@ -15,11 +15,16 @@
 
 package org.apache.fluo.api.config;
 
+import java.io.ByteArrayInputStream;
+import java.io.ByteArrayOutputStream;
 import java.io.File;
+import java.io.ObjectInputStream;
+import java.io.ObjectOutputStream;
 import java.lang.reflect.InvocationTargetException;
 import java.util.Arrays;
 import java.util.HashSet;
 import java.util.List;
+import java.util.Map;
 import java.util.NoSuchElementException;
 
 import com.google.common.collect.ImmutableMap;
@@ -357,4 +362,37 @@ public class FluoConfigurationTest {
       }
     }
   }
+
+  @Test
+  public void testSerialization() throws Exception {
+    FluoConfiguration c1 = new FluoConfiguration();
+    c1.setAccumuloUser("fluo");
+    c1.setAccumuloPassword("fc683cd9");
+    c1.setAccumuloTable("fd1");
+    c1.setApplicationName("testS");
+    c1.setAccumuloInstance("I9");
+    c1.setAccumuloZookeepers("localhost:7171");
+    c1.setInstanceZookeepers("localhost:7171/testS");
+    c1.setWorkerThreads(100);
+    c1.addObserver(new ObserverSpecification("com.foo.Observer1"));
+
+    ByteArrayOutputStream baos = new ByteArrayOutputStream();
+    ObjectOutputStream oo = new ObjectOutputStream(baos);
+    oo.writeObject(c1);
+    // want to ensure data written after the config is not read by custom deserialization
+    oo.writeObject("testdata");
+    oo.close();
+
+    ByteArrayInputStream bais = new ByteArrayInputStream(baos.toByteArray());
+    ObjectInputStream in = new ObjectInputStream(bais);
+
+    FluoConfiguration c2 = (FluoConfiguration) in.readObject();
+    Map<String, String> m2 = c2.toMap();
+    Assert.assertEquals(c1.toMap(), c2.toMap());
+    Assert.assertEquals(9, m2.size());
+
+    Assert.assertEquals("testdata", in.readObject());
+
+    in.close();
+  }
 }


Mime
View raw message