diff --git a/hadoop-yarn-project/hadoop-yarn/hadoop-yarn-api/src/main/java/org/apache/hadoop/yarn/conf/ConfigurationProvider.java b/hadoop-yarn-project/hadoop-yarn/hadoop-yarn-api/src/main/java/org/apache/hadoop/yarn/conf/ConfigurationProvider.java index ae85e82..c5ec351 100644 --- a/hadoop-yarn-project/hadoop-yarn/hadoop-yarn-api/src/main/java/org/apache/hadoop/yarn/conf/ConfigurationProvider.java +++ b/hadoop-yarn-project/hadoop-yarn/hadoop-yarn-api/src/main/java/org/apache/hadoop/yarn/conf/ConfigurationProvider.java @@ -18,6 +18,7 @@ package org.apache.hadoop.yarn.conf; +import java.io.Closeable; import java.io.IOException; import java.io.InputStream; @@ -33,15 +34,9 @@ * Real ConfigurationProvider implementations need to derive from it and * implement load methods to actually load the configuration. */ -public abstract class ConfigurationProvider { +public abstract class ConfigurationProvider implements Closeable { - public void init(Configuration bootstrapConf) throws Exception { - initInternal(bootstrapConf); - } - - public void close() throws Exception { - closeInternal(); - } + public abstract void init(Configuration bootstrapConf) throws Exception; /** * Opens an InputStream at the indicated file @@ -54,15 +49,4 @@ public void close() throws Exception { public abstract InputStream getConfigurationInputStream( Configuration bootstrapConf, String name) throws YarnException, IOException; - - /** - * Derived classes initialize themselves using this method. - */ - public abstract void initInternal(Configuration bootstrapConf) - throws Exception; - - /** - * Derived classes close themselves using this method. - */ - public abstract void closeInternal() throws Exception; } diff --git a/hadoop-yarn-project/hadoop-yarn/hadoop-yarn-common/src/main/java/org/apache/hadoop/yarn/FileSystemBasedConfigurationProvider.java b/hadoop-yarn-project/hadoop-yarn/hadoop-yarn-common/src/main/java/org/apache/hadoop/yarn/FileSystemBasedConfigurationProvider.java index bf50cad..39f4c62 100644 --- a/hadoop-yarn-project/hadoop-yarn/hadoop-yarn-common/src/main/java/org/apache/hadoop/yarn/FileSystemBasedConfigurationProvider.java +++ b/hadoop-yarn-project/hadoop-yarn/hadoop-yarn-common/src/main/java/org/apache/hadoop/yarn/FileSystemBasedConfigurationProvider.java @@ -43,6 +43,18 @@ private Path configDir; @Override + public synchronized void init(Configuration bootstrapConf) + throws Exception { + configDir = + new Path(bootstrapConf.get(YarnConfiguration.FS_BASED_RM_CONF_STORE, + YarnConfiguration.DEFAULT_FS_BASED_RM_CONF_STORE)); + fs = configDir.getFileSystem(bootstrapConf); + if (!fs.exists(configDir)) { + fs.mkdirs(configDir); + } + } + + @Override public synchronized InputStream getConfigurationInputStream( Configuration bootstrapConf, String name) throws IOException, YarnException { @@ -50,37 +62,18 @@ public synchronized InputStream getConfigurationInputStream( throw new YarnException( "Illegal argument! The parameter should not be null or empty"); } - Path filePath; - if (YarnConfiguration.RM_CONFIGURATION_FILES.contains(name)) { - filePath = new Path(this.configDir, name); - if (!fs.exists(filePath)) { - LOG.info(filePath + " not found"); - return null; - } - } else { - filePath = new Path(name); - if (!fs.exists(filePath)) { - LOG.info(filePath + " not found"); - return null; - } - } - return fs.open(filePath); - } + Path filePath = YarnConfiguration.RM_CONFIGURATION_FILES.contains(name) ? + new Path(this.configDir, name) : new Path(name); - @Override - public synchronized void initInternal(Configuration bootstrapConf) - throws Exception { - configDir = - new Path(bootstrapConf.get(YarnConfiguration.FS_BASED_RM_CONF_STORE, - YarnConfiguration.DEFAULT_FS_BASED_RM_CONF_STORE)); - fs = configDir.getFileSystem(bootstrapConf); - if (!fs.exists(configDir)) { - fs.mkdirs(configDir); + if (!fs.exists(filePath)) { + LOG.info(filePath + " not found"); + return null; } + return fs.open(filePath); } @Override - public synchronized void closeInternal() throws Exception { + public synchronized void close() throws IOException { fs.close(); } } diff --git a/hadoop-yarn-project/hadoop-yarn/hadoop-yarn-common/src/main/java/org/apache/hadoop/yarn/LocalConfigurationProvider.java b/hadoop-yarn-project/hadoop-yarn/hadoop-yarn-common/src/main/java/org/apache/hadoop/yarn/LocalConfigurationProvider.java index cfa194f..e21835b 100644 --- a/hadoop-yarn-project/hadoop-yarn/hadoop-yarn-common/src/main/java/org/apache/hadoop/yarn/LocalConfigurationProvider.java +++ b/hadoop-yarn-project/hadoop-yarn/hadoop-yarn-common/src/main/java/org/apache/hadoop/yarn/LocalConfigurationProvider.java @@ -46,12 +46,12 @@ public InputStream getConfigurationInputStream(Configuration bootstrapConf, } @Override - public void initInternal(Configuration bootstrapConf) throws Exception { + public void init(Configuration bootstrapConf) throws Exception { // Do nothing } @Override - public void closeInternal() throws Exception { + public void close() throws IOException { // Do nothing } } diff --git a/hadoop-yarn-project/hadoop-yarn/hadoop-yarn-common/src/test/java/org/apache/hadoop/yarn/TestLocalConfigurationProvider.java b/hadoop-yarn-project/hadoop-yarn/hadoop-yarn-common/src/test/java/org/apache/hadoop/yarn/TestLocalConfigurationProvider.java new file mode 100644 index 0000000..2178dfd --- /dev/null +++ b/hadoop-yarn-project/hadoop-yarn/hadoop-yarn-common/src/test/java/org/apache/hadoop/yarn/TestLocalConfigurationProvider.java @@ -0,0 +1,47 @@ +/** + * 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.yarn; + +import org.apache.hadoop.conf.Configuration; +import org.apache.hadoop.yarn.conf.ConfigurationProvider; +import org.junit.Test; + +import java.io.InputStream; + +import static org.junit.Assert.assertEquals; + +/** + * Shows how to use try with resources for ConfigurationProvider. + */ +public class TestLocalConfigurationProvider { + + @Test + public void testTryWithResourcesStyle() throws Exception { + String filePath = this.getClass().getClassLoader().getResource( + "dummy.xml").getPath(); + + Configuration conf = new Configuration(); + try (ConfigurationProvider provider = new LocalConfigurationProvider(); + InputStream inputStream = + provider.getConfigurationInputStream(conf, filePath)) { + conf.addResource(inputStream); + assertEquals(conf.get("color"), "white"); + } + } +} diff --git a/hadoop-yarn-project/hadoop-yarn/hadoop-yarn-common/src/test/resources/dummy.xml b/hadoop-yarn-project/hadoop-yarn/hadoop-yarn-common/src/test/resources/dummy.xml new file mode 100644 index 0000000..d800e80 --- /dev/null +++ b/hadoop-yarn-project/hadoop-yarn/hadoop-yarn-common/src/test/resources/dummy.xml @@ -0,0 +1,26 @@ + + + + + + + + color + white + +