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.omid;
19  
20  import com.google.inject.AbstractModule;
21  import com.google.inject.Provides;
22  import org.apache.omid.tools.hbase.HBaseLogin;
23  import org.apache.omid.tools.hbase.SecureHBaseConfig;
24  import org.apache.hadoop.conf.Configuration;
25  import org.apache.hadoop.hbase.HBaseConfiguration;
26  
27  import java.io.IOException;
28  
29  public class HBaseConfigModule extends AbstractModule {
30  
31      private String principal;
32      private String keytab;
33  
34      public HBaseConfigModule(String principal, String keytab) {
35          this.principal = principal;
36          this.keytab = keytab;
37      }
38  
39      @Override
40      protected void configure() {
41      }
42  
43      @Provides
44      public Configuration provideHBaseConfig() throws IOException {
45          Configuration configuration = HBaseConfiguration.create();
46          SecureHBaseConfig secureHBaseConfig = new SecureHBaseConfig();
47          secureHBaseConfig.setKeytab(keytab);
48          secureHBaseConfig.setPrincipal(principal);
49          HBaseLogin.loginIfNeeded(secureHBaseConfig);
50          return configuration;
51  
52      }
53  
54      // ----------------------------------------------------------------------------------------------------------------
55      // IMPORTANT NOTE
56      // Overriding equals() and hashCode() is necessary here to allow this module to be installed multiple times.
57      // As example, this module can be installed in a configuration using both the HBase Timestamp & Commit Table stores.
58      // ----------------------------------------------------------------------------------------------------------------
59  
60      @Override
61      public boolean equals(Object o) {
62  
63          if (this == o) return true;
64  
65          if (o == null || getClass() != o.getClass()) return false;
66  
67          HBaseConfigModule that = (HBaseConfigModule) o;
68  
69          if (principal != null ? !principal.equals(that.principal) : that.principal != null) return false;
70          return keytab != null ? keytab.equals(that.keytab) : that.keytab == null;
71  
72      }
73  
74      @Override
75      public int hashCode() {
76  
77          int result = principal != null ? principal.hashCode() : 0;
78          result = 31 * result + (keytab != null ? keytab.hashCode() : 0);
79          return result;
80  
81      }
82  
83  }