Repository: tez Updated Branches: refs/heads/master 141881cee -> cd0fc63e1
TEZ-1328. Move EnvironmentUpdateUtils to tez-common Project: http://git-wip-us.apache.org/repos/asf/tez/repo Commit: http://git-wip-us.apache.org/repos/asf/tez/commit/cd0fc63e Tree: http://git-wip-us.apache.org/repos/asf/tez/tree/cd0fc63e Diff: http://git-wip-us.apache.org/repos/asf/tez/diff/cd0fc63e Branch: refs/heads/master Commit: cd0fc63e1319694e743c525c6f6091a9df5c18f6 Parents: 141881c Author: Rajesh Balamohan <[email protected]> Authored: Wed Jul 30 03:44:32 2014 +0530 Committer: Rajesh Balamohan <[email protected]> Committed: Wed Jul 30 03:44:32 2014 +0530 ---------------------------------------------------------------------- .../tez/dag/utils/EnvironmentUpdateUtils.java | 92 ++++++++++++++++++++ .../dag/utils/TestEnvironmentUpdateUtils.java | 34 ++++++++ .../tez/dag/utils/EnvironmentUpdateUtils.java | 89 ------------------- .../dag/utils/TestEnvironmentUpdateUtils.java | 34 -------- 4 files changed, 126 insertions(+), 123 deletions(-) ---------------------------------------------------------------------- http://git-wip-us.apache.org/repos/asf/tez/blob/cd0fc63e/tez-common/src/main/java/org/apache/tez/dag/utils/EnvironmentUpdateUtils.java ---------------------------------------------------------------------- diff --git a/tez-common/src/main/java/org/apache/tez/dag/utils/EnvironmentUpdateUtils.java b/tez-common/src/main/java/org/apache/tez/dag/utils/EnvironmentUpdateUtils.java new file mode 100644 index 0000000..a8b4ced --- /dev/null +++ b/tez-common/src/main/java/org/apache/tez/dag/utils/EnvironmentUpdateUtils.java @@ -0,0 +1,92 @@ +/** + * 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 org.apache.hadoop.classification.InterfaceAudience; + +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 + */ [email protected] +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/tez/blob/cd0fc63e/tez-common/src/test/java/org/apache/tez/dag/utils/TestEnvironmentUpdateUtils.java ---------------------------------------------------------------------- diff --git a/tez-common/src/test/java/org/apache/tez/dag/utils/TestEnvironmentUpdateUtils.java b/tez-common/src/test/java/org/apache/tez/dag/utils/TestEnvironmentUpdateUtils.java new file mode 100644 index 0000000..d4a42c4 --- /dev/null +++ b/tez-common/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")); + } +} http://git-wip-us.apache.org/repos/asf/tez/blob/cd0fc63e/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 deleted file mode 100644 index 1bb5f7b..0000000 --- a/tez-dag/src/main/java/org/apache/tez/dag/utils/EnvironmentUpdateUtils.java +++ /dev/null @@ -1,89 +0,0 @@ -/** - * 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/tez/blob/cd0fc63e/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 deleted file mode 100644 index d4a42c4..0000000 --- a/tez-dag/src/test/java/org/apache/tez/dag/utils/TestEnvironmentUpdateUtils.java +++ /dev/null @@ -1,34 +0,0 @@ -/** - * 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")); - } -}
