1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19 package org.apache.hadoop.hbase.rest;
20
21 import java.io.IOException;
22
23 import org.apache.hadoop.hbase.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.security.UserProvider;
28 import org.apache.hadoop.hbase.util.ConnectionCache;
29 import org.apache.hadoop.security.UserGroupInformation;
30 import org.apache.hadoop.security.authorize.ProxyUsers;
31
32
33
34
35 @InterfaceAudience.Private
36 public class RESTServlet implements Constants {
37 private static RESTServlet INSTANCE;
38 private final Configuration conf;
39 private final MetricsREST metrics = new MetricsREST();
40 private final ConnectionCache connectionCache;
41 private final UserGroupInformation realUser;
42
43 static final String CLEANUP_INTERVAL = "hbase.rest.connection.cleanup-interval";
44 static final String MAX_IDLETIME = "hbase.rest.connection.max-idletime";
45 static final String HBASE_REST_SUPPORT_PROXYUSER = "hbase.rest.support.proxyuser";
46
47 UserGroupInformation getRealUser() {
48 return realUser;
49 }
50
51
52
53
54 public synchronized static RESTServlet getInstance() {
55 assert(INSTANCE != null);
56 return INSTANCE;
57 }
58
59
60
61
62
63
64
65 public synchronized static RESTServlet getInstance(Configuration conf,
66 UserProvider userProvider) throws IOException {
67 if (INSTANCE == null) {
68 INSTANCE = new RESTServlet(conf, userProvider);
69 }
70 return INSTANCE;
71 }
72
73 public synchronized static void stop() {
74 if (INSTANCE != null) INSTANCE = null;
75 }
76
77
78
79
80
81
82
83 RESTServlet(final Configuration conf,
84 final UserProvider userProvider) throws IOException {
85 this.realUser = userProvider.getCurrent().getUGI();
86 this.conf = conf;
87
88 int cleanInterval = conf.getInt(CLEANUP_INTERVAL, 10 * 1000);
89 int maxIdleTime = conf.getInt(MAX_IDLETIME, 10 * 60 * 1000);
90 connectionCache = new ConnectionCache(
91 conf, userProvider, cleanInterval, maxIdleTime);
92 if (supportsProxyuser()) {
93 ProxyUsers.refreshSuperUserGroupsConfiguration(conf);
94 }
95 }
96
97 HBaseAdmin getAdmin() throws IOException {
98 return connectionCache.getAdmin();
99 }
100
101
102
103
104 HTableInterface getTable(String tableName) throws IOException {
105 return connectionCache.getTable(tableName);
106 }
107
108 Configuration getConfiguration() {
109 return conf;
110 }
111
112 MetricsREST getMetrics() {
113 return metrics;
114 }
115
116
117
118
119
120
121 boolean isReadOnly() {
122 return getConfiguration().getBoolean("hbase.rest.readonly", false);
123 }
124
125 void setEffectiveUser(String effectiveUser) {
126 connectionCache.setEffectiveUser(effectiveUser);
127 }
128
129 boolean supportsProxyuser() {
130 return conf.getBoolean(HBASE_REST_SUPPORT_PROXYUSER, false);
131 }
132 }