View Javadoc

1   /*
2    * Copyright 2010 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  
21  package org.apache.hadoop.hbase.rest;
22  
23  import java.io.IOException;
24  
25  import org.apache.hadoop.conf.Configuration;
26  import org.apache.hadoop.hbase.client.HBaseAdmin;
27  import org.apache.hadoop.hbase.client.HTablePool;
28  import org.apache.hadoop.hbase.rest.metrics.RESTMetrics;
29  
30  /**
31   * Singleton class encapsulating global REST servlet state and functions.
32   */
33  public class RESTServlet implements Constants {
34    private static RESTServlet INSTANCE;
35    private final Configuration conf;
36    private final HTablePool pool;
37    private final RESTMetrics metrics = new RESTMetrics();
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    RESTMetrics 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 }