View Javadoc

1   /**
2    * Licensed to the Apache Software Foundation (ASF) under one
3    * or more contributor license agreements.  See the NOTICE file
4    * distributed with this work for additional information
5    * regarding copyright ownership.  The ASF licenses this file
6    * to you under the Apache License, Version 2.0 (the
7    * "License"); you may not use this file except in compliance
8    * with the License.  You may obtain a copy of the License at
9    *
10   *     http://www.apache.org/licenses/LICENSE-2.0
11   *
12   * Unless required by applicable law or agreed to in writing, software
13   * distributed under the License is distributed on an "AS IS" BASIS,
14   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15   * See the License for the specific language governing permissions and
16   * limitations under the License.
17   */
18  package org.apache.hadoop.hbase;
19  
20  import java.io.IOException;
21  import java.lang.reflect.InvocationTargetException;
22  import java.lang.reflect.Method;
23  import java.util.Map.Entry;
24  
25  import org.apache.commons.logging.Log;
26  import org.apache.commons.logging.LogFactory;
27  import org.apache.hadoop.hbase.classification.InterfaceAudience;
28  import org.apache.hadoop.hbase.classification.InterfaceStability;
29  import org.apache.hadoop.conf.Configuration;
30  import org.apache.hadoop.hbase.io.util.HeapMemorySizeUtil;
31  import org.apache.hadoop.hbase.util.VersionInfo;
32  
33  /**
34   * Adds HBase configuration files to a Configuration
35   */
36  @InterfaceAudience.Public
37  @InterfaceStability.Stable
38  public class HBaseConfiguration extends Configuration {
39  
40    private static final Log LOG = LogFactory.getLog(HBaseConfiguration.class);
41  
42    /**
43     * Instantinating HBaseConfiguration() is deprecated. Please use
44     * HBaseConfiguration#create() to construct a plain Configuration
45     */
46    @Deprecated
47    public HBaseConfiguration() {
48      //TODO:replace with private constructor, HBaseConfiguration should not extend Configuration
49      super();
50      addHbaseResources(this);
51      LOG.warn("instantiating HBaseConfiguration() is deprecated. Please use"
52          + " HBaseConfiguration#create() to construct a plain Configuration");
53    }
54  
55    /**
56     * Instantiating HBaseConfiguration() is deprecated. Please use
57     * HBaseConfiguration#create(conf) to construct a plain Configuration
58     */
59    @Deprecated
60    public HBaseConfiguration(final Configuration c) {
61      //TODO:replace with private constructor
62      this();
63      merge(this, c);
64    }
65  
66    private static void checkDefaultsVersion(Configuration conf) {
67      if (conf.getBoolean("hbase.defaults.for.version.skip", Boolean.FALSE)) return;
68      String defaultsVersion = conf.get("hbase.defaults.for.version");
69      String thisVersion = VersionInfo.getVersion();
70      if (!thisVersion.equals(defaultsVersion)) {
71        throw new RuntimeException(
72          "hbase-default.xml file seems to be for an older version of HBase (" +
73          defaultsVersion + "), this version is " + thisVersion);
74      }
75    }
76  
77    public static Configuration addHbaseResources(Configuration conf) {
78      conf.addResource("hbase-default.xml");
79      conf.addResource("hbase-site.xml");
80  
81      checkDefaultsVersion(conf);
82      HeapMemorySizeUtil.checkForClusterFreeMemoryLimit(conf);
83      return conf;
84    }
85  
86    /**
87     * Creates a Configuration with HBase resources
88     * @return a Configuration with HBase resources
89     */
90    public static Configuration create() {
91      Configuration conf = new Configuration();
92      // In case HBaseConfiguration is loaded from a different classloader than
93      // Configuration, conf needs to be set with appropriate class loader to resolve
94      // HBase resources.
95      conf.setClassLoader(HBaseConfiguration.class.getClassLoader());
96      return addHbaseResources(conf);
97    }
98  
99    /**
100    * @param that Configuration to clone.
101    * @return a Configuration created with the hbase-*.xml files plus
102    * the given configuration.
103    */
104   public static Configuration create(final Configuration that) {
105     Configuration conf = create();
106     merge(conf, that);
107     return conf;
108   }
109 
110   /**
111    * Merge two configurations.
112    * @param destConf the configuration that will be overwritten with items
113    *                 from the srcConf
114    * @param srcConf the source configuration
115    **/
116   public static void merge(Configuration destConf, Configuration srcConf) {
117     for (Entry<String, String> e : srcConf) {
118       destConf.set(e.getKey(), e.getValue());
119     }
120   }
121 
122   /**
123    * @return whether to show HBase Configuration in servlet
124    */
125   public static boolean isShowConfInServlet() {
126     boolean isShowConf = false;
127     try {
128       if (Class.forName("org.apache.hadoop.conf.ConfServlet") != null) {
129         isShowConf = true;
130       }
131     } catch (LinkageError e) {
132        // should we handle it more aggressively in addition to log the error?
133        LOG.warn("Error thrown: ", e);
134     } catch (ClassNotFoundException ce) {
135       LOG.debug("ClassNotFound: ConfServlet");
136       // ignore
137     }
138     return isShowConf;
139   }
140 
141   /**
142    * Get the value of the <code>name</code> property as an <code>int</code>, possibly
143    * referring to the deprecated name of the configuration property.
144    * If no such property exists, the provided default value is returned,
145    * or if the specified value is not a valid <code>int</code>,
146    * then an error is thrown.
147    *
148    * @param name property name.
149    * @param deprecatedName a deprecatedName for the property to use
150    * if non-deprecated name is not used
151    * @param defaultValue default value.
152    * @throws NumberFormatException when the value is invalid
153    * @return property value as an <code>int</code>,
154    *         or <code>defaultValue</code>.
155    */
156   // TODO: developer note: This duplicates the functionality of deprecated
157   // property support in Configuration in Hadoop 2. But since Hadoop-1 does not
158   // contain these changes, we will do our own as usual. Replace these when H2 is default.
159   public static int getInt(Configuration conf, String name,
160       String deprecatedName, int defaultValue) {
161     if (conf.get(deprecatedName) != null) {
162       LOG.warn(String.format("Config option \"%s\" is deprecated. Instead, use \"%s\""
163         , deprecatedName, name));
164       return conf.getInt(deprecatedName, defaultValue);
165     } else {
166       return conf.getInt(name, defaultValue);
167     }
168   }
169 
170   /**
171    * Get the password from the Configuration instance using the
172    * getPassword method if it exists. If not, then fall back to the
173    * general get method for configuration elements.
174    * @param conf configuration instance for accessing the passwords
175    * @param alias the name of the password element
176    * @param defPass the default password
177    * @return String password or default password
178    * @throws IOException
179    */
180   public static String getPassword(Configuration conf, String alias,
181       String defPass) throws IOException {
182     String passwd = null;
183     try {
184       Method m = Configuration.class.getMethod("getPassword", String.class);
185       char[] p = (char[]) m.invoke(conf, alias);
186       if (p != null) {
187         LOG.debug(String.format("Config option \"%s\" was found through" +
188             " the Configuration getPassword method.", alias));
189         passwd = new String(p);
190       }
191       else {
192         LOG.debug(String.format(
193             "Config option \"%s\" was not found. Using provided default value",
194             alias));
195         passwd = defPass;
196       }
197     } catch (NoSuchMethodException e) {
198       // this is a version of Hadoop where the credential
199       //provider API doesn't exist yet
200       LOG.debug(String.format(
201           "Credential.getPassword method is not available." +
202           " Falling back to configuration."));
203       passwd = conf.get(alias, defPass);
204     } catch (SecurityException e) {
205       throw new IOException(e.getMessage(), e);
206     } catch (IllegalAccessException e) {
207       throw new IOException(e.getMessage(), e);
208     } catch (IllegalArgumentException e) {
209       throw new IOException(e.getMessage(), e);
210     } catch (InvocationTargetException e) {
211       throw new IOException(e.getMessage(), e);
212     }
213     return passwd;
214   }
215 
216   /** For debugging.  Dump configurations to system output as xml format.
217    * Master and RS configurations can also be dumped using
218    * http services. e.g. "curl http://master:16010/dump"
219    */
220   public static void main(String[] args) throws Exception {
221     HBaseConfiguration.create().writeXml(System.out);
222   }
223 }