htrace-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From cmcc...@apache.org
Subject incubator-htrace git commit: HTRACE-351. Add JavaPropertyConfiguration (cmccabe)
Date Fri, 01 Apr 2016 19:17:31 GMT
Repository: incubator-htrace
Updated Branches:
  refs/heads/master 51cfa3a11 -> ab94ce6f1


HTRACE-351. Add JavaPropertyConfiguration (cmccabe)


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

Branch: refs/heads/master
Commit: ab94ce6f1def76f05ccbba4494da71c4746a23a7
Parents: 51cfa3a
Author: Colin P. Mccabe <cmccabe@apache.org>
Authored: Thu Mar 31 17:35:17 2016 -0700
Committer: Colin P. Mccabe <cmccabe@apache.org>
Committed: Fri Apr 1 12:17:13 2016 -0700

----------------------------------------------------------------------
 .../htrace/core/JavaPropertyConfiguration.java  | 82 ++++++++++++++++++++
 .../htrace/core/TestHTraceConfiguration.java    | 18 +++++
 2 files changed, 100 insertions(+)
----------------------------------------------------------------------


http://git-wip-us.apache.org/repos/asf/incubator-htrace/blob/ab94ce6f/htrace-core4/src/main/java/org/apache/htrace/core/JavaPropertyConfiguration.java
----------------------------------------------------------------------
diff --git a/htrace-core4/src/main/java/org/apache/htrace/core/JavaPropertyConfiguration.java
b/htrace-core4/src/main/java/org/apache/htrace/core/JavaPropertyConfiguration.java
new file mode 100644
index 0000000..7af87e3
--- /dev/null
+++ b/htrace-core4/src/main/java/org/apache/htrace/core/JavaPropertyConfiguration.java
@@ -0,0 +1,82 @@
+/*
+ * 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.htrace.core;
+
+import org.apache.commons.logging.Log;
+import org.apache.commons.logging.LogFactory;
+
+import java.util.Iterator;
+import java.util.LinkedList;
+
+/**
+ * This is an implementation of HTraceConfiguration which draws its properties
+ * from global Java Properties.
+ */
+public final class JavaPropertyConfiguration extends HTraceConfiguration {
+  private static final Log LOG =
+      LogFactory.getLog(JavaPropertyConfiguration.class);
+
+  public static class Builder {
+    final LinkedList<String> prefixes;
+
+    public Builder() {
+      prefixes = new LinkedList<String>();
+      prefixes.add("htrace.");
+    }
+
+    public Builder clearPrefixes() {
+      prefixes.clear();
+      return this;
+    }
+
+    public Builder addPrefix(String prefix) {
+      prefixes.add(prefix);
+      return this;
+    }
+
+    JavaPropertyConfiguration build() {
+      return new JavaPropertyConfiguration(prefixes);
+    }
+  }
+
+  private final String[] prefixes;
+
+  private JavaPropertyConfiguration(LinkedList<String> prefixes) {
+    this.prefixes = new String[prefixes.size()];
+    int i = 0;
+    for (Iterator<String> it = prefixes.descendingIterator(); it.hasNext(); ) {
+      this.prefixes[i++] = it.next();
+    }
+  }
+
+  @Override
+  public String get(String key) {
+    for (String prefix : prefixes) {
+      String val = System.getProperty(prefix + key);
+      if (val != null) {
+        return val;
+      }
+    }
+    return null;
+  }
+
+  @Override
+  public String get(String key, String defaultValue) {
+    String val = get(key);
+    return (val != null) ? val : defaultValue;
+  }
+}

http://git-wip-us.apache.org/repos/asf/incubator-htrace/blob/ab94ce6f/htrace-core4/src/test/java/org/apache/htrace/core/TestHTraceConfiguration.java
----------------------------------------------------------------------
diff --git a/htrace-core4/src/test/java/org/apache/htrace/core/TestHTraceConfiguration.java
b/htrace-core4/src/test/java/org/apache/htrace/core/TestHTraceConfiguration.java
index 7ca897f..3b4ea53 100644
--- a/htrace-core4/src/test/java/org/apache/htrace/core/TestHTraceConfiguration.java
+++ b/htrace-core4/src/test/java/org/apache/htrace/core/TestHTraceConfiguration.java
@@ -59,4 +59,22 @@ public class TestHTraceConfiguration {
     assertEquals(5, configuration.getInt("d", -999));
     assertEquals(-999, configuration.getInt("absent", -999));
   }
+
+  @Test
+  public void testPropertyConfiguration() throws Exception {
+    System.setProperty("supercalifragilistic.property", "1");
+    System.setProperty("htrace.supercalifragilistic.property", "2");
+    System.setProperty("foo.htrace.supercalifragilistic.property", "3");
+    JavaPropertyConfiguration conf =
+      new JavaPropertyConfiguration.Builder().
+        addPrefix("foo.htrace.").
+        build();
+    assertEquals("3", conf.get("supercalifragilistic.property"));
+    JavaPropertyConfiguration conf2 =
+      new JavaPropertyConfiguration.Builder().
+        build();
+    assertEquals("2", conf2.get("supercalifragilistic.property"));
+    assertTrue(null == conf.get("superfly"));
+    assertTrue(null == conf2.get("superfly"));
+  }
 }


Mime
View raw message