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  
23  import org.apache.hadoop.classification.InterfaceAudience;
24  import org.apache.hadoop.conf.Configuration;
25  import org.apache.hadoop.hbase.client.HBaseAdmin;
26  import org.apache.hadoop.hbase.client.HTableInterface;
27  import org.apache.hadoop.hbase.filter.ParseFilter;
28  import org.apache.hadoop.hbase.security.UserProvider;
29  import org.apache.hadoop.hbase.util.ConnectionCache;
30  import org.apache.hadoop.security.UserGroupInformation;
31  import org.apache.log4j.Logger;
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 Logger LOG = Logger.getLogger(RESTServlet.class);
39    private static RESTServlet INSTANCE;
40    private final Configuration conf;
41    private final MetricsREST metrics = new MetricsREST();
42    private final ConnectionCache connectionCache;
43    private final UserGroupInformation realUser;
44  
45    static final String CLEANUP_INTERVAL = "hbase.rest.connection.cleanup-interval";
46    static final String MAX_IDLETIME = "hbase.rest.connection.max-idletime";
47  
48    UserGroupInformation getRealUser() {
49      return realUser;
50    }
51  
52    /**
53     * @return the RESTServlet singleton instance
54     */
55    public synchronized static RESTServlet getInstance() {
56      assert(INSTANCE != null);
57      return INSTANCE;
58    }
59  
60    /**
61     * @param conf Existing configuration to use in rest servlet
62     * @param userProvider the login user provider
63     * @return the RESTServlet singleton instance
64     * @throws IOException
65     */
66    public synchronized static RESTServlet getInstance(Configuration conf,
67        UserProvider userProvider) throws IOException {
68      if (INSTANCE == null) {
69        INSTANCE = new RESTServlet(conf, userProvider);
70      }
71      return INSTANCE;
72    }
73  
74    public synchronized static void stop() {
75      if (INSTANCE != null)  INSTANCE = null;
76    }
77  
78    /**
79     * Constructor with existing configuration
80     * @param conf existing configuration
81     * @param userProvider the login user provider
82     * @throws IOException
83     */
84    RESTServlet(final Configuration conf,
85        final UserProvider userProvider) throws IOException {
86      this.realUser = userProvider.getCurrent().getUGI();
87      this.conf = conf;
88      registerCustomFilter(conf);
89  
90      int cleanInterval = conf.getInt(CLEANUP_INTERVAL, 10 * 1000);
91      int maxIdleTime = conf.getInt(MAX_IDLETIME, 10 * 60 * 1000);
92      connectionCache = new ConnectionCache(
93        conf, userProvider, cleanInterval, maxIdleTime);
94    }
95  
96    HBaseAdmin getAdmin() throws IOException {
97      return connectionCache.getAdmin();
98    }
99  
100   /**
101    * Caller closes the table afterwards.
102    */
103   HTableInterface getTable(String tableName) throws IOException {
104     return connectionCache.getTable(tableName);
105   }
106 
107   Configuration getConfiguration() {
108     return conf;
109   }
110 
111   MetricsREST getMetrics() {
112     return metrics;
113   }
114 
115   /**
116    * Helper method to determine if server should
117    * only respond to GET HTTP method requests.
118    * @return boolean for server read-only state
119    */
120   boolean isReadOnly() {
121     return getConfiguration().getBoolean("hbase.rest.readonly", false);
122   }
123 
124   void setEffectiveUser(String effectiveUser) {
125     connectionCache.setEffectiveUser(effectiveUser);
126   }
127 
128   private void registerCustomFilter(Configuration conf) {
129     String[] filterList = conf.getStrings(Constants.CUSTOM_FILTERS);
130     if (filterList != null) {
131       for (String filterClass : filterList) {
132         String[] filterPart = filterClass.split(":");
133         if (filterPart.length != 2) {
134           LOG.warn(
135             "Invalid filter specification " + filterClass + " - skipping");
136         } else {
137           ParseFilter.registerFilter(filterPart[0], filterPart[1]);
138         }
139       }
140     }
141   }
142 }