Repository: incubator-omid
Updated Branches:
  refs/heads/0.9.0.0 4e9cd9976 -> 9f5474f91


[OMID-65] Extend HBaseOmidClientConfiguration creation methods

Add a constructor that takes a Properties (via its parent-interface of Map<?, 
?>) for properties to be used.
Allow creating a HBaseOmidClientConfiguration from the contents of a yaml file.

This closes #11

Change-Id: I64f5101003fed9331bc28dc9e0c1fd2c45038426
Signed-off-by: Francisco Perez-Sorrosal <fperezsorro...@apache.org>


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

Branch: refs/heads/0.9.0.0
Commit: 9f5474f912b4dfa51abba91a95f98da452b399ca
Parents: 4e9cd99
Author: Sheridan C Rawlins <s...@yahoo-inc.com>
Authored: Tue Mar 28 17:03:39 2017 -0700
Committer: Francisco Perez-Sorrosal <fperezsorro...@apache.org>
Committed: Thu Aug 10 11:41:51 2017 -0700

----------------------------------------------------------------------
 .../main/java/org/apache/omid/YAMLUtils.java    | 19 +++++++++++-----
 .../HBaseOmidClientConfiguration.java           | 23 +++++++++++++++++---
 2 files changed, 33 insertions(+), 9 deletions(-)
----------------------------------------------------------------------


http://git-wip-us.apache.org/repos/asf/incubator-omid/blob/9f5474f9/common/src/main/java/org/apache/omid/YAMLUtils.java
----------------------------------------------------------------------
diff --git a/common/src/main/java/org/apache/omid/YAMLUtils.java 
b/common/src/main/java/org/apache/omid/YAMLUtils.java
index d60d06a..7518e3f 100644
--- a/common/src/main/java/org/apache/omid/YAMLUtils.java
+++ b/common/src/main/java/org/apache/omid/YAMLUtils.java
@@ -30,6 +30,7 @@ import java.nio.charset.Charset;
 import java.util.HashMap;
 import java.util.Map;
 
+@SuppressWarnings("WeakerAccess")
 public class YAMLUtils {
 
     private static final Logger LOG = LoggerFactory.getLogger(YAMLUtils.class);
@@ -53,7 +54,7 @@ public class YAMLUtils {
     }
 
     @SuppressWarnings("unchecked")
-    private Map loadSettings(String resourcePath, String defaultResourcePath) 
throws IOException {
+    public Map loadSettings(String resourcePath, String defaultResourcePath) 
throws IOException {
         Map defaultSetting = loadAsMap(defaultResourcePath);
         Preconditions.checkState(defaultSetting.size() > 0, 
String.format("Failed to load file '%s' from classpath", defaultResourcePath));
         if (resourcePath != null) {
@@ -64,15 +65,21 @@ public class YAMLUtils {
     }
 
     @SuppressWarnings("unchecked")
-    private Map loadAsMap(String path) throws IOException {
+    public Map loadAsMap(String path) throws IOException {
         try {
             String content = Resources.toString(Resources.getResource(path), 
Charset.forName("UTF-8"));
             LOG.debug("Loaded resource file '{}'\n{}", path, content);
+            return loadStringAsMap(content);
+        } catch (IllegalArgumentException e) {
+            return new HashMap();
+        }
+    }
+
+    @SuppressWarnings("unchecked")
+    public Map loadStringAsMap(String content) {
+        try {
             Map settings = new Yaml().loadAs(content, Map.class);
-            if (settings == null) {
-                settings = new HashMap(0);
-            }
-            return settings;
+            return (settings != null) ? settings : new HashMap(0);
         } catch (IllegalArgumentException e) {
             return new HashMap();
         }

http://git-wip-us.apache.org/repos/asf/incubator-omid/blob/9f5474f9/hbase-client/src/main/java/org/apache/omid/transaction/HBaseOmidClientConfiguration.java
----------------------------------------------------------------------
diff --git 
a/hbase-client/src/main/java/org/apache/omid/transaction/HBaseOmidClientConfiguration.java
 
b/hbase-client/src/main/java/org/apache/omid/transaction/HBaseOmidClientConfiguration.java
index f5a1823..867698e 100644
--- 
a/hbase-client/src/main/java/org/apache/omid/transaction/HBaseOmidClientConfiguration.java
+++ 
b/hbase-client/src/main/java/org/apache/omid/transaction/HBaseOmidClientConfiguration.java
@@ -20,13 +20,17 @@ package org.apache.omid.transaction;
 import com.google.common.annotations.VisibleForTesting;
 import com.google.inject.Inject;
 import com.google.inject.name.Named;
+import org.apache.commons.beanutils.BeanUtils;
+import org.apache.hadoop.conf.Configuration;
+import org.apache.hadoop.hbase.HBaseConfiguration;
 import org.apache.omid.YAMLUtils;
 import org.apache.omid.metrics.MetricsRegistry;
 import org.apache.omid.tools.hbase.SecureHBaseConfig;
-import org.apache.omid.tso.client.OmidClientConfiguration.PostCommitMode;
 import org.apache.omid.tso.client.OmidClientConfiguration;
-import org.apache.hadoop.conf.Configuration;
-import org.apache.hadoop.hbase.HBaseConfiguration;
+import org.apache.omid.tso.client.OmidClientConfiguration.PostCommitMode;
+
+import java.lang.reflect.InvocationTargetException;
+import java.util.Map;
 
 /**
  * Configuration for HBase's Omid client side
@@ -44,10 +48,23 @@ public class HBaseOmidClientConfiguration extends 
SecureHBaseConfig {
     // 
----------------------------------------------------------------------------------------------------------------
     // Instantiation
     // 
----------------------------------------------------------------------------------------------------------------
+    
+    public static HBaseOmidClientConfiguration loadFromString(String 
yamlContent) {
+        return new HBaseOmidClientConfiguration(new 
YAMLUtils().loadStringAsMap(yamlContent));
+    }
+
     public HBaseOmidClientConfiguration() {
         this(CONFIG_FILE_NAME);
     }
 
+    public <K, V> HBaseOmidClientConfiguration(Map<K, V> properties) {
+        try {
+            BeanUtils.populate(this, properties);
+        } catch (IllegalAccessException | InvocationTargetException e) {
+            throw new IllegalStateException(e);
+        }
+    }
+
     @VisibleForTesting
     HBaseOmidClientConfiguration(String configFileName) {
         new YAMLUtils().loadSettings(configFileName, DEFAULT_CONFIG_FILE_NAME, 
this);

Reply via email to