View Javadoc

1   /**
2    * Copyright 2007 The Apache Software Foundation
3    *
4    * Licensed to the Apache Software Foundation (ASF) under one
5    * or more contributor license agreements.  See the NOTICE file
6    * distributed with this work for additional information
7    * regarding copyright ownership.  The ASF licenses this file
8    * to you under the Apache License, Version 2.0 (the
9    * "License"); you may not use this file except in compliance
10   * with the License.  You may obtain a copy of the License at
11   *
12   *     http://www.apache.org/licenses/LICENSE-2.0
13   *
14   * Unless required by applicable law or agreed to in writing, software
15   * distributed under the License is distributed on an "AS IS" BASIS,
16   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
17   * See the License for the specific language governing permissions and
18   * limitations under the License.
19   */
20  package org.apache.hadoop.hbase;
21  
22  import java.util.Iterator;
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  
29  /**
30   * Adds HBase configuration files to a Configuration
31   */
32  public class HBaseConfiguration extends Configuration {
33  
34    private static final Log LOG = LogFactory.getLog(HBaseConfiguration.class);
35  
36    /**
37     * Instantinating HBaseConfiguration() is deprecated. Please use
38     * HBaseConfiguration#create() to construct a plain Configuration
39     */
40    @Deprecated
41    public HBaseConfiguration() {
42      //TODO:replace with private constructor, HBaseConfiguration should not extend Configuration
43      super();
44      addHbaseResources(this);
45      LOG.warn("instantiating HBaseConfiguration() is deprecated. Please use" +
46      		" HBaseConfiguration#create() to construct a plain Configuration");
47    }
48  
49    /**
50     * Instantiating HBaseConfiguration() is deprecated. Please use
51     * HBaseConfiguration#create(conf) to construct a plain Configuration
52     */
53    @Deprecated
54    public HBaseConfiguration(final Configuration c) {
55      //TODO:replace with private constructor
56      this();
57      for (Entry<String, String>e: c) {
58        set(e.getKey(), e.getValue());
59      }
60    }
61  
62    public static Configuration addHbaseResources(Configuration conf) {
63      conf.addResource("hbase-default.xml");
64      conf.addResource("hbase-site.xml");
65      return conf;
66    }
67  
68    /**
69     * Creates a Configuration with HBase resources
70     * @return a Configuration with HBase resources
71     */
72    public static Configuration create() {
73      Configuration conf = new Configuration();
74      return addHbaseResources(conf);
75    }
76  
77    /**
78     * Creates a clone of passed configuration.
79     * @param that Configuration to clone.
80     * @return a Configuration created with the hbase-*.xml files plus
81     * the given configuration.
82     */
83    public static Configuration create(final Configuration that) {
84      Configuration conf = create();
85      for (Entry<String, String>e: that) {
86        conf.set(e.getKey(), e.getValue());
87      }
88      return conf;
89    }
90  
91    /**
92     * Returns the hash code value for this HBaseConfiguration. The hash code of a
93     * HBaseConfiguration is defined by the xor of the hash codes of its entries.
94     *
95     * @see Configuration#iterator() How the entries are obtained.
96     */
97    @Override
98    @Deprecated
99    public int hashCode() {
100     return hashCode(this);
101   }
102 
103   /**
104    * Returns the hash code value for this HBaseConfiguration. The hash code of a
105    * Configuration is defined by the xor of the hash codes of its entries.
106    *
107    * @see Configuration#iterator() How the entries are obtained.
108    */
109   public static int hashCode(Configuration conf) {
110     int hash = 0;
111 
112     Iterator<Entry<String, String>> propertyIterator = conf.iterator();
113     while (propertyIterator.hasNext()) {
114       hash ^= propertyIterator.next().hashCode();
115     }
116     return hash;
117   }
118 
119   @Override
120   public boolean equals(Object obj) {
121     if (this == obj)
122       return true;
123     if (obj == null)
124       return false;
125     if (!(obj instanceof HBaseConfiguration))
126       return false;
127     HBaseConfiguration otherConf = (HBaseConfiguration) obj;
128     if (size() != otherConf.size()) {
129       return false;
130     }
131     Iterator<Entry<String, String>> propertyIterator = this.iterator();
132     while (propertyIterator.hasNext()) {
133       Entry<String, String> entry = propertyIterator.next();
134       String key = entry.getKey();
135       String value = entry.getValue();
136       if (!value.equals(otherConf.getRaw(key))) {
137         return false;
138       }
139     }
140 
141     return true;
142   }
143 }