Added: 
hive/trunk/common/src/java/org/apache/hadoop/hive/conf/SystemVariables.java
URL: 
http://svn.apache.org/viewvc/hive/trunk/common/src/java/org/apache/hadoop/hive/conf/SystemVariables.java?rev=1610279&view=auto
==============================================================================
--- hive/trunk/common/src/java/org/apache/hadoop/hive/conf/SystemVariables.java 
(added)
+++ hive/trunk/common/src/java/org/apache/hadoop/hive/conf/SystemVariables.java 
Sun Jul 13 18:54:08 2014
@@ -0,0 +1,102 @@
+/**
+ * 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.hadoop.hive.conf;
+
+import java.util.regex.Matcher;
+import java.util.regex.Pattern;
+
+import org.apache.commons.logging.Log;
+import org.apache.commons.logging.LogFactory;
+import org.apache.hadoop.conf.Configuration;
+
+public class SystemVariables {
+
+  private static final Log l4j = LogFactory.getLog(SystemVariables.class);
+  protected static Pattern varPat = 
Pattern.compile("\\$\\{[^\\}\\$\u0020]+\\}");
+
+  public static final String ENV_PREFIX = "env:";
+  public static final String SYSTEM_PREFIX = "system:";
+  public static final String HIVECONF_PREFIX = "hiveconf:";
+  public static final String HIVEVAR_PREFIX = "hivevar:";
+  public static final String SET_COLUMN_NAME = "set";
+
+  protected String getSubstitute(Configuration conf, String var) {
+    String val = null;
+    try {
+      if (var.startsWith(SYSTEM_PREFIX)) {
+        val = System.getProperty(var.substring(SYSTEM_PREFIX.length()));
+      }
+    } catch(SecurityException se) {
+      l4j.warn("Unexpected SecurityException in Configuration", se);
+    }
+    if (val == null && var.startsWith(ENV_PREFIX)) {
+      val = System.getenv(var.substring(ENV_PREFIX.length()));
+    }
+    if (val == null && conf != null && var.startsWith(HIVECONF_PREFIX)) {
+      val = conf.get(var.substring(HIVECONF_PREFIX.length()));
+    }
+    return val;
+  }
+
+  public static boolean containsVar(String expr) {
+    return expr != null && varPat.matcher(expr).find();
+  }
+
+  static String substitute(String expr) {
+    return expr == null ? null : new SystemVariables().substitute(null, expr, 
1);
+  }
+
+  static String substitute(Configuration conf, String expr) {
+    return expr == null ? null : new SystemVariables().substitute(conf, expr, 
1);
+  }
+
+  protected final String substitute(Configuration conf, String expr, int 
depth) {
+    Matcher match = varPat.matcher("");
+    String eval = expr;
+    StringBuilder builder = new StringBuilder();
+    int s = 0;
+    for (; s <= depth; s++) {
+      match.reset(eval);
+      builder.setLength(0);
+      int prev = 0;
+      boolean found = false;
+      while (match.find(prev)) {
+        String group = match.group();
+        String var = group.substring(2, group.length() - 1); // remove ${ .. }
+        String substitute = getSubstitute(conf, var);
+        if (substitute == null) {
+          substitute = group;   // append as-is
+        } else {
+          found = true;
+        }
+        builder.append(eval.substring(prev, match.start())).append(substitute);
+        prev = match.end();
+      }
+      if (!found) {
+        return eval;
+      }
+      builder.append(eval.substring(prev));
+      eval = builder.toString();
+    }
+    if (s > depth) {
+      throw new IllegalStateException(
+          "Variable substitution depth is deeper than " + depth + " for 
expression " + expr);
+    }
+    return eval;
+  }
+}

Added: hive/trunk/common/src/java/org/apache/hadoop/hive/conf/Validator.java
URL: 
http://svn.apache.org/viewvc/hive/trunk/common/src/java/org/apache/hadoop/hive/conf/Validator.java?rev=1610279&view=auto
==============================================================================
--- hive/trunk/common/src/java/org/apache/hadoop/hive/conf/Validator.java 
(added)
+++ hive/trunk/common/src/java/org/apache/hadoop/hive/conf/Validator.java Sun 
Jul 13 18:54:08 2014
@@ -0,0 +1,159 @@
+/**
+ * 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.hadoop.hive.conf;
+
+import java.util.ArrayList;
+import java.util.LinkedHashSet;
+import java.util.List;
+import java.util.Set;
+import java.util.regex.Pattern;
+
+/**
+ * validate value for a ConfVar, return non-null string for fail message
+ */
+public interface Validator {
+
+  String validate(String value);
+
+  static class StringSet implements Validator {
+
+    private final Set<String> expected = new LinkedHashSet<String>();
+
+    public StringSet(String... values) {
+      for (String value : values) {
+        expected.add(value.toLowerCase());
+      }
+    }
+
+    @Override
+    public String validate(String value) {
+      if (value == null || !expected.contains(value.toLowerCase())) {
+        return "Invalid value.. expects one of " + expected;
+      }
+      return null;
+    }
+  }
+
+  static enum RANGE_TYPE {
+    INT {
+      @Override
+      protected boolean inRange(String value, Object lower, Object upper) {
+        int ivalue = Integer.parseInt(value);
+        return (Integer)lower <= ivalue && ivalue <= (Integer)upper;
+      }
+    },
+    LONG {
+      @Override
+      protected boolean inRange(String value, Object lower, Object upper) {
+        long lvalue = Long.parseLong(value);
+        return (Long)lower <= lvalue && lvalue <= (Long)upper;
+      }
+    },
+    FLOAT {
+      @Override
+      protected boolean inRange(String value, Object lower, Object upper) {
+        float fvalue = Float.parseFloat(value);
+        return (Float)lower <= fvalue && fvalue <= (Float)upper;
+      }
+    };
+
+    public static RANGE_TYPE valueOf(Object lower, Object upper) {
+      if (lower instanceof Integer && upper instanceof Integer) {
+        assert (Integer)lower < (Integer)upper;
+        return INT;
+      } else if (lower instanceof Long && upper instanceof Long) {
+        assert (Long)lower < (Long)upper;
+        return LONG;
+      } else if (lower instanceof Float && upper instanceof Float) {
+        assert (Float)lower < (Float)upper;
+        return FLOAT;
+      }
+      throw new IllegalArgumentException("invalid range from " + lower + " to 
" + upper);
+    }
+
+    protected abstract boolean inRange(String value, Object lower, Object 
upper);
+  }
+
+  static class RangeValidator implements Validator {
+
+    private final RANGE_TYPE type;
+    private final Object lower, upper;
+
+    public RangeValidator(Object lower, Object upper) {
+      this.lower = lower;
+      this.upper = upper;
+      this.type = RANGE_TYPE.valueOf(lower, upper);
+    }
+
+    @Override
+    public String validate(String value) {
+      try {
+        if (value == null) {
+          return "Value cannot be null";
+        }
+        if (!type.inRange(value.trim(), lower, upper)) {
+          return "Invalid value  " + value + ", which should be in between " + 
lower + " and " + upper;
+        }
+      } catch (Exception e) {
+        return e.toString();
+      }
+      return null;
+    }
+  }
+
+  static class PatternSet implements Validator {
+
+    private final List<Pattern> expected = new ArrayList<Pattern>();
+
+    public PatternSet(String... values) {
+      for (String value : values) {
+        expected.add(Pattern.compile(value));
+      }
+    }
+
+    @Override
+    public String validate(String value) {
+      if (value == null) {
+        return "Invalid value.. expects one of patterns " + expected;
+      }
+      for (Pattern pattern : expected) {
+        if (pattern.matcher(value).matches()) {
+          return null;
+        }
+      }
+      return "Invalid value.. expects one of patterns " + expected;
+    }
+  }
+
+  static class RatioValidator implements Validator {
+
+    @Override
+    public String validate(String value) {
+      try {
+        float fvalue = Float.valueOf(value);
+        if (fvalue <= 0 || fvalue >= 1) {
+          return "Invalid ratio " + value + ", which should be in between 0 to 
1";
+        }
+      } catch (NumberFormatException e) {
+        return e.toString();
+      }
+      return null;
+    }
+  }
+}

Modified: 
hive/trunk/common/src/test/org/apache/hadoop/hive/conf/TestHiveConf.java
URL: 
http://svn.apache.org/viewvc/hive/trunk/common/src/test/org/apache/hadoop/hive/conf/TestHiveConf.java?rev=1610279&r1=1610278&r2=1610279&view=diff
==============================================================================
--- hive/trunk/common/src/test/org/apache/hadoop/hive/conf/TestHiveConf.java 
(original)
+++ hive/trunk/common/src/test/org/apache/hadoop/hive/conf/TestHiveConf.java 
Sun Jul 13 18:54:08 2014
@@ -18,7 +18,6 @@
 package org.apache.hadoop.hive.conf;
 
 import org.apache.hadoop.mapred.JobConf;
-import org.apache.hadoop.fs.Path;
 import org.apache.hadoop.hive.conf.HiveConf.ConfVars;
 import org.apache.hadoop.util.Shell;
 import org.apache.hive.common.util.HiveTestUtils;
@@ -50,7 +49,7 @@ public class TestHiveConf {
   }
 
   private void checkConfVar(ConfVars var, String expectedConfVarVal) throws 
Exception {
-    Assert.assertEquals(expectedConfVarVal, var.defaultVal);
+    Assert.assertEquals(expectedConfVarVal, var.getDefaultValue());
   }
 
   private void checkHiveConf(String name, String expectedHiveVal) throws 
Exception {
@@ -87,7 +86,7 @@ public class TestHiveConf {
     checkHiveConf("test.property1", "hive-site.xml");
 
     // Test HiveConf property variable substitution in hive-site.xml
-    checkHiveConf("test.var.hiveconf.property", 
ConfVars.DEFAULTPARTITIONNAME.defaultVal);
+    checkHiveConf("test.var.hiveconf.property", 
ConfVars.DEFAULTPARTITIONNAME.getDefaultValue());
   }
 
   @Test

Modified: 
hive/trunk/common/src/test/org/apache/hadoop/hive/conf/TestHiveConfRestrictList.java
URL: 
http://svn.apache.org/viewvc/hive/trunk/common/src/test/org/apache/hadoop/hive/conf/TestHiveConfRestrictList.java?rev=1610279&r1=1610278&r2=1610279&view=diff
==============================================================================
--- 
hive/trunk/common/src/test/org/apache/hadoop/hive/conf/TestHiveConfRestrictList.java
 (original)
+++ 
hive/trunk/common/src/test/org/apache/hadoop/hive/conf/TestHiveConfRestrictList.java
 Sun Jul 13 18:54:08 2014
@@ -19,7 +19,6 @@ package org.apache.hadoop.hive.conf;
 
 import junit.framework.TestCase;
 
-import org.apache.hadoop.hive.conf.HiveConf;
 import org.apache.hadoop.hive.conf.HiveConf.ConfVars;
 import org.junit.Test;
 

Modified: 
hive/trunk/common/src/test/org/apache/hadoop/hive/conf/TestHiveLogging.java
URL: 
http://svn.apache.org/viewvc/hive/trunk/common/src/test/org/apache/hadoop/hive/conf/TestHiveLogging.java?rev=1610279&r1=1610278&r2=1610279&view=diff
==============================================================================
--- hive/trunk/common/src/test/org/apache/hadoop/hive/conf/TestHiveLogging.java 
(original)
+++ hive/trunk/common/src/test/org/apache/hadoop/hive/conf/TestHiveLogging.java 
Sun Jul 13 18:54:08 2014
@@ -18,7 +18,6 @@
 package org.apache.hadoop.hive.conf;
 
 import java.io.BufferedReader;
-import java.io.IOException;
 import java.io.InputStreamReader;
 
 import junit.framework.TestCase;
@@ -26,7 +25,6 @@ import junit.framework.TestCase;
 import org.apache.hadoop.hive.common.LogUtils;
 import org.apache.hadoop.hive.conf.HiveConf.ConfVars;
 import org.apache.hive.common.util.HiveTestUtils;
-import org.apache.hadoop.hive.conf.HiveConf.ConfVars;
 
 /**
  * TestHiveLogging


Reply via email to