001    /**
002     * Licensed to the Apache Software Foundation (ASF) under one
003     * or more contributor license agreements.  See the NOTICE file
004     * distributed with this work for additional information
005     * regarding copyright ownership.  The ASF licenses this file
006     * to you under the Apache License, Version 2.0 (the
007     * "License"); you may not use this file except in compliance
008     * with the License.  You may obtain a copy of the License at
009     *
010     *     http://www.apache.org/licenses/LICENSE-2.0
011     *
012     * Unless required by applicable law or agreed to in writing, software
013     * distributed under the License is distributed on an "AS IS" BASIS,
014     * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
015     * See the License for the specific language governing permissions and
016     * limitations under the License.
017     */
018    package org.apache.hadoop.fs.viewfs;
019    
020    import java.net.URI;
021    
022    import org.apache.hadoop.conf.Configuration;
023    
024    /**
025     * Utilities for config variables of the viewFs See {@link ViewFs}
026     */
027    public class ConfigUtil {
028      /**
029       * Get the config variable prefix for the specified mount table
030       * @param mountTableName - the name of the mount table
031       * @return the config variable prefix for the specified mount table
032       */
033      public static String getConfigViewFsPrefix(final String mountTableName) {
034        return Constants.CONFIG_VIEWFS_PREFIX + "." + mountTableName;
035      }
036      
037      /**
038       * Get the config variable prefix for the default mount table
039       * @return the config variable prefix for the default mount table
040       */
041      public static String getConfigViewFsPrefix() {
042        return 
043          getConfigViewFsPrefix(Constants.CONFIG_VIEWFS_PREFIX_DEFAULT_MOUNT_TABLE);
044      }
045      
046      /**
047       * Add a link to the config for the specified mount table
048       * @param conf - add the link to this conf
049       * @param mountTableName
050       * @param src - the src path name
051       * @param target - the target URI link
052       */
053      public static void addLink(Configuration conf, final String mountTableName, 
054          final String src, final URI target) {
055        conf.set(getConfigViewFsPrefix(mountTableName) + "." +
056            Constants.CONFIG_VIEWFS_LINK + "." + src, target.toString());  
057      }
058      
059      /**
060       * Add a link to the config for the default mount table
061       * @param conf - add the link to this conf
062       * @param src - the src path name
063       * @param target - the target URI link
064       */
065      public static void addLink(final Configuration conf, final String src,
066          final URI target) {
067        addLink( conf, Constants.CONFIG_VIEWFS_DEFAULT_MOUNT_TABLE, 
068            src, target);   
069      }
070      
071      /**
072       * Add config variable for homedir for default mount table
073       * @param conf - add to this conf
074       * @param homedir - the home dir path starting with slash
075       */
076      public static void setHomeDirConf(final Configuration conf,
077          final String homedir) {
078        setHomeDirConf(  conf,
079            Constants.CONFIG_VIEWFS_DEFAULT_MOUNT_TABLE,   homedir);
080      }
081      
082      /**
083       * Add config variable for homedir the specified mount table
084       * @param conf - add to this conf
085       * @param homedir - the home dir path starting with slash
086       */
087      public static void setHomeDirConf(final Configuration conf,
088                  final String mountTableName, final String homedir) {
089        if (!homedir.startsWith("/")) {
090          throw new IllegalArgumentException("Home dir should start with /:"
091              + homedir);
092        }
093        conf.set(getConfigViewFsPrefix(mountTableName) + "." +
094            Constants.CONFIG_VIEWFS_HOMEDIR, homedir);
095      }
096      
097      /**
098       * Get the value of the home dir conf value for default mount table
099       * @param conf - from this conf
100       * @return home dir value, null if variable is not in conf
101       */
102      public static String getHomeDirValue(final Configuration conf) {
103        return getHomeDirValue(conf, Constants.CONFIG_VIEWFS_DEFAULT_MOUNT_TABLE);
104      }
105      
106      /**
107       * Get the value of the home dir conf value for specfied mount table
108       * @param conf - from this conf
109       * @param mountTableName - the mount table
110       * @return home dir value, null if variable is not in conf
111       */
112      public static String getHomeDirValue(final Configuration conf, 
113          final String mountTableName) {
114        return conf.get(getConfigViewFsPrefix(mountTableName) + "." +
115            Constants.CONFIG_VIEWFS_HOMEDIR);
116      }
117    }