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  
19  package org.apache.hadoop.hbase.rest;
20  
21  import java.io.IOException;
22  import java.security.PrivilegedAction;
23  
24  import org.apache.hadoop.classification.InterfaceAudience;
25  import org.apache.hadoop.conf.Configuration;
26  import org.apache.hadoop.hbase.client.HBaseAdmin;
27  import org.apache.hadoop.hbase.client.HConnectionManager;
28  import org.apache.hadoop.hbase.client.HConnectionWrapper;
29  import org.apache.hadoop.hbase.client.HTableInterface;
30  import org.apache.hadoop.hbase.client.HTablePool;
31  import org.apache.hadoop.security.UserGroupInformation;
32  
33  /**
34   * Singleton class encapsulating global REST servlet state and functions.
35   */
36  @InterfaceAudience.Private
37  public class RESTServlet implements Constants {
38    private static RESTServlet INSTANCE;
39    private final Configuration conf;
40    private final HTablePool pool;
41    private final MetricsREST metrics = new MetricsREST();
42    private final HBaseAdmin admin;
43    private final UserGroupInformation ugi;
44  
45    /**
46     * @return the RESTServlet singleton instance
47     * @throws IOException
48     */
49    public synchronized static RESTServlet getInstance() throws IOException {
50      assert(INSTANCE != null);
51      return INSTANCE;
52    }
53  
54    /**
55     * @param conf Existing configuration to use in rest servlet
56     * @return the RESTServlet singleton instance
57     * @throws IOException
58     */
59    public synchronized static RESTServlet getInstance(Configuration conf)
60    throws IOException {
61      return getInstance(conf, null);
62    }
63  
64    public synchronized static RESTServlet getInstance(Configuration conf,
65        UserGroupInformation ugi) throws IOException {
66      if (INSTANCE == null) {
67        INSTANCE = new RESTServlet(conf, ugi);
68      }
69      return INSTANCE;
70    }
71  
72    public synchronized static void stop() {
73      if (INSTANCE != null)  INSTANCE = null;
74    }
75  
76    /**
77     * Constructor with existing configuration
78     * @param conf existing configuration
79     * @throws IOException
80     */
81    RESTServlet(final Configuration conf,
82        final UserGroupInformation ugi) throws IOException {
83      this.conf = conf;
84      this.ugi = ugi;
85      int maxSize = conf.getInt("hbase.rest.htablepool.size", 10);
86      if (ugi == null) {
87        pool = new HTablePool(conf, maxSize);
88        admin = new HBaseAdmin(conf);
89      } else {
90        admin = new HBaseAdmin(new HConnectionWrapper(ugi,
91          HConnectionManager.getConnection(new Configuration(conf))));
92  
93        pool = new HTablePool(conf, maxSize) {
94          /**
95           * A HTablePool adapter. It makes sure the real user is
96           * always used in creating any table so that the HConnection
97           * is not any proxy user in case impersonation with
98           * RESTServletContainer.
99           */
100         @Override
101         protected HTableInterface createHTable(final String tableName) {
102           return ugi.doAs(new PrivilegedAction<HTableInterface>() {
103             @Override
104             public HTableInterface run() {
105               return callCreateHTable(tableName);
106              }
107            });
108           
109         }
110 
111         /**
112          * A helper method used to call super.createHTable.
113          */
114         HTableInterface callCreateHTable(final String tableName) {
115           return super.createHTable(tableName);
116         }
117       };
118     }
119   }
120 
121   HBaseAdmin getAdmin() {
122     return admin;
123   }
124 
125   HTablePool getTablePool() {
126     return pool;
127   }
128 
129   Configuration getConfiguration() {
130     return conf;
131   }
132 
133   MetricsREST getMetrics() {
134     return metrics;
135   }
136 
137   /**
138    * Helper method to determine if server should
139    * only respond to GET HTTP method requests.
140    * @return boolean for server read-only state
141    */
142   boolean isReadOnly() {
143     return getConfiguration().getBoolean("hbase.rest.readonly", false);
144   }
145 
146   UserGroupInformation getUser() {
147     return ugi;
148   }
149 }