View Javadoc

1   /*
2    *
3    * Licensed to the Apache Software Foundation (ASF) under one
4    * or more contributor license agreements.  See the NOTICE file
5    * distributed with this work for additional information
6    * regarding copyright ownership.  The ASF licenses this file
7    * to you under the Apache License, Version 2.0 (the
8    * "License"); you may not use this file except in compliance
9    * with the License.  You may obtain a copy of the License at
10   *
11   *   http://www.apache.org/licenses/LICENSE-2.0
12   *
13   * Unless required by applicable law or agreed to in writing, software
14   * distributed under the License is distributed on an "AS IS" BASIS,
15   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
16   * See the License for the specific language governing permissions and
17   * limitations under the License.
18   */
19  
20  package org.apache.hadoop.hbase.rest;
21  
22  import java.io.IOException;
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.HTablePool;
28  
29  /**
30   * Singleton class encapsulating global REST servlet state and functions.
31   */
32  @InterfaceAudience.Private
33  public class RESTServlet implements Constants {
34    private static RESTServlet INSTANCE;
35    private final Configuration conf;
36    private final HTablePool pool;
37    private final MetricsREST metrics = new MetricsREST();
38    private final HBaseAdmin admin;
39  
40    /**
41     * @return the RESTServlet singleton instance
42     * @throws IOException
43     */
44    public synchronized static RESTServlet getInstance() throws IOException {
45      assert(INSTANCE != null);
46      return INSTANCE;
47    }
48  
49    /**
50     * @param conf Existing configuration to use in rest servlet
51     * @return the RESTServlet singleton instance
52     * @throws IOException
53     */
54    public synchronized static RESTServlet getInstance(Configuration conf)
55    throws IOException {
56      if (INSTANCE == null) {
57        INSTANCE = new RESTServlet(conf);
58      }
59      return INSTANCE;
60    }
61  
62    public synchronized static void stop() {
63      if (INSTANCE != null)  INSTANCE = null;
64    }
65  
66    /**
67     * Constructor with existing configuration
68     * @param conf existing configuration
69     * @throws IOException
70     */
71    RESTServlet(Configuration conf) throws IOException {
72      this.conf = conf;
73      int maxSize = conf.getInt("hbase.rest.htablepool.size", 10);
74      this.pool = new HTablePool(conf, maxSize);
75      this.admin = new HBaseAdmin(conf);
76    }
77  
78    HBaseAdmin getAdmin() {
79      return admin;
80    }
81  
82    HTablePool getTablePool() {
83      return pool;
84    }
85  
86    Configuration getConfiguration() {
87      return conf;
88    }
89  
90    MetricsREST getMetrics() {
91      return metrics;
92    }
93  
94    /**
95     * Helper method to determine if server should
96     * only respond to GET HTTP method requests.
97     * @return boolean for server read-only state
98     */
99    boolean isReadOnly() {
100     return getConfiguration().getBoolean("hbase.rest.readonly", false);
101   }
102 }