tez-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From ss...@apache.org
Subject git commit: TEZ-1285. Add Utility for Modifying Environment Variables. Contributed by Jonathan Eagles and Oleg Zhurakousky.
Date Wed, 16 Jul 2014 17:11:15 GMT
Repository: incubator-tez
Updated Branches:
  refs/heads/master ddf049b37 -> 1f0dd5d14


TEZ-1285. Add Utility for Modifying Environment Variables. Contributed
by Jonathan Eagles and Oleg Zhurakousky.


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

Branch: refs/heads/master
Commit: 1f0dd5d1446ffff7c00c5fc5cf0ad1797fbddb92
Parents: ddf049b
Author: Siddharth Seth <sseth@apache.org>
Authored: Wed Jul 16 10:10:35 2014 -0700
Committer: Siddharth Seth <sseth@apache.org>
Committed: Wed Jul 16 10:10:35 2014 -0700

----------------------------------------------------------------------
 .../tez/dag/utils/EnvironmentUpdateUtils.java   | 89 ++++++++++++++++++++
 .../dag/utils/TestEnvironmentUpdateUtils.java   | 34 ++++++++
 2 files changed, 123 insertions(+)
----------------------------------------------------------------------


http://git-wip-us.apache.org/repos/asf/incubator-tez/blob/1f0dd5d1/tez-dag/src/main/java/org/apache/tez/dag/utils/EnvironmentUpdateUtils.java
----------------------------------------------------------------------
diff --git a/tez-dag/src/main/java/org/apache/tez/dag/utils/EnvironmentUpdateUtils.java b/tez-dag/src/main/java/org/apache/tez/dag/utils/EnvironmentUpdateUtils.java
new file mode 100644
index 0000000..1bb5f7b
--- /dev/null
+++ b/tez-dag/src/main/java/org/apache/tez/dag/utils/EnvironmentUpdateUtils.java
@@ -0,0 +1,89 @@
+/**
+ * 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.tez.dag.utils;
+
+import java.lang.reflect.Field;
+import java.util.Collections;
+import java.util.HashMap;
+import java.util.Map;
+
+/**
+ * A utility class which allows one to dynamically update/change Environment variables
+ */
+public class EnvironmentUpdateUtils {
+
+  /**
+   * Allows dynamic update to the environment variables. After calling put,
+   * System.getenv(key) will then return value.
+   *
+   * @param key System environment variable
+   * @param value Value to assign to system environment variable
+   */
+  public static void put(String key, String value){
+    Map<String, String> environment = new HashMap<String, String>(System.getenv());
+    environment.put(key, value);
+    updateEnvironment(environment);
+  }
+
+  /**
+   * Allows dynamic update to a collection of environment variables. After
+   * calling putAll, System.getenv(key) will then return value for each entry
+   * in the map
+   *
+   * @param additionalEnvironment Collection where the key is the System
+   * environment variable and the value is the value to assign the system
+   * environment variable
+   */
+  public static void putAll(Map<String, String> additionalEnvironment) {
+    Map<String, String> environment = new HashMap<String, String>(System.getenv());
+    environment.putAll(additionalEnvironment);
+    updateEnvironment(environment);
+  }
+
+  /**
+   * Finds and modifies internal storage for system environment variables using
+   * reflection
+   *
+   * @param environment Collection where the key is the System
+   * environment variable and the value is the value to assign the system
+   * environment variable
+   */
+  @SuppressWarnings("unchecked")
+  private static void updateEnvironment(Map<String, String> environment) {
+    try {
+      Class<?>[] classes = Collections.class.getDeclaredClasses();
+      for (Class<?> clazz : classes) {
+        if ("java.util.Collections$UnmodifiableMap".equals(clazz.getName())) {
+          Field field = clazz.getDeclaredField("m");
+          field.setAccessible(true);
+          Object obj = field.get(System.getenv());
+          Map<String, String> map = (Map<String, String>)obj;
+          map.clear();
+          map.putAll(environment);
+        }
+      }
+    }
+    catch (NoSuchFieldException e) {
+      throw new IllegalStateException("Failed to update Environment variables", e);
+    }
+    catch (IllegalAccessException e) {
+      throw new IllegalStateException("Failed to update Environment variables", e);
+    }
+  }
+}

http://git-wip-us.apache.org/repos/asf/incubator-tez/blob/1f0dd5d1/tez-dag/src/test/java/org/apache/tez/dag/utils/TestEnvironmentUpdateUtils.java
----------------------------------------------------------------------
diff --git a/tez-dag/src/test/java/org/apache/tez/dag/utils/TestEnvironmentUpdateUtils.java
b/tez-dag/src/test/java/org/apache/tez/dag/utils/TestEnvironmentUpdateUtils.java
new file mode 100644
index 0000000..d4a42c4
--- /dev/null
+++ b/tez-dag/src/test/java/org/apache/tez/dag/utils/TestEnvironmentUpdateUtils.java
@@ -0,0 +1,34 @@
+/**
+ * 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.tez.dag.utils;
+
+import static org.junit.Assert.assertEquals;
+
+import org.junit.Test;
+
+public class TestEnvironmentUpdateUtils {
+
+  @Test
+  public void testMultipleUpdateEnvironment() {
+    EnvironmentUpdateUtils.put("test.environment1", "test.value1");
+    EnvironmentUpdateUtils.put("test.environment2", "test.value2");
+    assertEquals("Environment was not set propertly", "test.value1", System.getenv("test.environment1"));
+    assertEquals("Environment was not set propertly", "test.value2", System.getenv("test.environment2"));
+  }
+}


Mime
View raw message