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.conf.Configuration;
28  import org.apache.hadoop.hbase.classification.InterfaceAudience;
29  import org.apache.hadoop.hbase.classification.InterfaceStability;
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     * @deprecated Please use create() instead.
46     */
47    @Deprecated
48    public HBaseConfiguration() {
49      //TODO:replace with private constructor, HBaseConfiguration should not extend Configuration
50      super();
51      addHbaseResources(this);
52      LOG.warn("instantiating HBaseConfiguration() is deprecated. Please use"
53          + " HBaseConfiguration#create() to construct a plain Configuration");
54    }
55  
56    /**
57     * Instantiating HBaseConfiguration() is deprecated. Please use
58     * HBaseConfiguration#create(conf) to construct a plain Configuration
59     * @deprecated Please user create(conf) instead.
60     */
61    @Deprecated
62    public HBaseConfiguration(final Configuration c) {
63      //TODO:replace with private constructor
64      this();
65      merge(this, c);
66    }
67  
68    private static void checkDefaultsVersion(Configuration conf) {
69      if (conf.getBoolean("hbase.defaults.for.version.skip", Boolean.FALSE)) return;
70      String defaultsVersion = conf.get("hbase.defaults.for.version");
71      String thisVersion = VersionInfo.getVersion();
72      if (!thisVersion.equals(defaultsVersion)) {
73        throw new RuntimeException(
74          "hbase-default.xml file seems to be for an older version of HBase (" +
75          defaultsVersion + "), this version is " + thisVersion);
76      }
77    }
78  
79    public static Configuration addHbaseResources(Configuration conf) {
80      conf.addResource("hbase-default.xml");
81      conf.addResource("hbase-site.xml");
82  
83      checkDefaultsVersion(conf);
84      HeapMemorySizeUtil.checkForClusterFreeMemoryLimit(conf);
85      return conf;
86    }
87  
88    /**
89     * Creates a Configuration with HBase resources
90     * @return a Configuration with HBase resources
91     */
92    public static Configuration create() {
93      Configuration conf = new Configuration();
94      // In case HBaseConfiguration is loaded from a different classloader than
95      // Configuration, conf needs to be set with appropriate class loader to resolve
96      // HBase resources.
97      conf.setClassLoader(HBaseConfiguration.class.getClassLoader());
98      return addHbaseResources(conf);
99    }
100 
101   /**
102    * @param that Configuration to clone.
103    * @return a Configuration created with the hbase-*.xml files plus
104    * the given configuration.
105    */
106   public static Configuration create(final Configuration that) {
107     Configuration conf = create();
108     merge(conf, that);
109     return conf;
110   }
111 
112   /**
113    * Merge two configurations.
114    * @param destConf the configuration that will be overwritten with items
115    *                 from the srcConf
116    * @param srcConf the source configuration
117    **/
118   public static void merge(Configuration destConf, Configuration srcConf) {
119     for (Entry<String, String> e : srcConf) {
120       destConf.set(e.getKey(), e.getValue());
121     }
122   }
123 
124   /**
125    * @return whether to show HBase Configuration in servlet
126    */
127   public static boolean isShowConfInServlet() {
128     boolean isShowConf = false;
129     try {
130       if (Class.forName("org.apache.hadoop.conf.ConfServlet") != null) {
131         isShowConf = true;
132       }
133     } catch (LinkageError e) {
134        // should we handle it more aggressively in addition to log the error?
135        LOG.warn("Error thrown: ", e);
136     } catch (ClassNotFoundException ce) {
137       LOG.debug("ClassNotFound: ConfServlet");
138       // ignore
139     }
140     return isShowConf;
141   }
142 
143   /**
144    * Get the value of the <code>name</code> property as an <code>int</code>, possibly
145    * referring to the deprecated name of the configuration property.
146    * If no such property exists, the provided default value is returned,
147    * or if the specified value is not a valid <code>int</code>,
148    * then an error is thrown.
149    *
150    * @param name property name.
151    * @param deprecatedName a deprecatedName for the property to use
152    * if non-deprecated name is not used
153    * @param defaultValue default value.
154    * @throws NumberFormatException when the value is invalid
155    * @return property value as an <code>int</code>,
156    *         or <code>defaultValue</code>.
157    */
158   // TODO: developer note: This duplicates the functionality of deprecated
159   // property support in Configuration in Hadoop 2. But since Hadoop-1 does not
160   // contain these changes, we will do our own as usual. Replace these when H2 is default.
161   public static int getInt(Configuration conf, String name,
162       String deprecatedName, int defaultValue) {
163     if (conf.get(deprecatedName) != null) {
164       LOG.warn(String.format("Config option \"%s\" is deprecated. Instead, use \"%s\""
165         , deprecatedName, name));
166       return conf.getInt(deprecatedName, defaultValue);
167     } else {
168       return conf.getInt(name, defaultValue);
169     }
170   }
171 
172   /**
173    * Get the password from the Configuration instance using the
174    * getPassword method if it exists. If not, then fall back to the
175    * general get method for configuration elements.
176    *
177    * @param conf    configuration instance for accessing the passwords
178    * @param alias   the name of the password element
179    * @param defPass the default password
180    * @return String password or default password
181    * @throws IOException
182    */
183   public static String getPassword(Configuration conf, String alias,
184       String defPass) throws IOException {
185     String passwd = null;
186     try {
187       Method m = Configuration.class.getMethod("getPassword", String.class);
188       char[] p = (char[]) m.invoke(conf, alias);
189       if (p != null) {
190         LOG.debug(String.format("Config option \"%s\" was found through" +
191             " the Configuration getPassword method.", alias));
192         passwd = new String(p);
193       } else {
194         LOG.debug(String.format(
195             "Config option \"%s\" was not found. Using provided default value",
196             alias));
197         passwd = defPass;
198       }
199     } catch (NoSuchMethodException e) {
200       // this is a version of Hadoop where the credential
201       //provider API doesn't exist yet
202       LOG.debug(String.format(
203           "Credential.getPassword method is not available." +
204               " Falling back to configuration."));
205       passwd = conf.get(alias, defPass);
206     } catch (SecurityException e) {
207       throw new IOException(e.getMessage(), e);
208     } catch (IllegalAccessException e) {
209       throw new IOException(e.getMessage(), e);
210     } catch (IllegalArgumentException e) {
211       throw new IOException(e.getMessage(), e);
212     } catch (InvocationTargetException e) {
213       throw new IOException(e.getMessage(), e);
214     }
215     return passwd;
216   }
217 
218   /**
219    * For debugging.  Dump configurations to system output as xml format.
220    * Master and RS configurations can also be dumped using
221    * http services. e.g. "curl http://master:16010/dump"
222    */
223   public static void main(String[] args) throws Exception {
224     HBaseConfiguration.create().writeXml(System.out);
225   }
226 }