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.HTablePool;
27  import org.apache.hadoop.hbase.rest.metrics.RESTMetrics;
28  
29  /**
30   * Singleton class encapsulating global REST servlet state and functions.
31   */
32  public class RESTServlet implements Constants {
33    private static RESTServlet INSTANCE;
34    private final Configuration conf;
35    private final HTablePool pool;
36    private final RESTMetrics metrics = new RESTMetrics();
37  
38    /**
39     * @return the RESTServlet singleton instance
40     * @throws IOException
41     */
42    public synchronized static RESTServlet getInstance() throws IOException {
43      assert(INSTANCE != null);
44      return INSTANCE;
45    }
46  
47    /**
48     * @param conf Existing configuration to use in rest servlet
49     * @return the RESTServlet singleton instance
50     * @throws IOException
51     */
52    public synchronized static RESTServlet getInstance(Configuration conf)
53    throws IOException {
54      if (INSTANCE == null) {
55        INSTANCE = new RESTServlet(conf);
56      }
57      return INSTANCE;
58    }
59  
60    public synchronized static void stop() {
61      if (INSTANCE != null)  INSTANCE = null;
62    }
63  
64    /**
65     * Constructor with existing configuration
66     * @param conf existing configuration
67     * @throws IOException.
68     */
69    RESTServlet(Configuration conf) throws IOException {
70      this.conf = conf;
71      this.pool = new HTablePool(conf, 10);
72    }
73  
74    HTablePool getTablePool() {
75      return pool;
76    }
77  
78    Configuration getConfiguration() {
79      return conf;
80    }
81  
82    RESTMetrics getMetrics() {
83      return metrics;
84    }
85  
86    /**
87     * Helper method to determine if server should
88     * only respond to GET HTTP method requests.
89     * @return boolean for server read-only state
90     */
91    boolean isReadOnly() {
92      return getConfiguration().getBoolean("hbase.rest.readonly", false);
93    }
94  }