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.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
31
32
33
34 @InterfaceAudience.Private
35 public class RESTServlet implements Constants {
36 private static RESTServlet INSTANCE;
37 private final Configuration conf;
38 private final MetricsREST metrics = new MetricsREST();
39 private final ConnectionCache connectionCache;
40 private final UserGroupInformation realUser;
41
42 static final String CLEANUP_INTERVAL = "hbase.rest.connection.cleanup-interval";
43 static final String MAX_IDLETIME = "hbase.rest.connection.max-idletime";
44
45 UserGroupInformation getRealUser() {
46 return realUser;
47 }
48
49
50
51
52 public synchronized static RESTServlet getInstance() {
53 assert(INSTANCE != null);
54 return INSTANCE;
55 }
56
57
58
59
60
61
62
63 public synchronized static RESTServlet getInstance(Configuration conf,
64 UserProvider userProvider) throws IOException {
65 if (INSTANCE == null) {
66 INSTANCE = new RESTServlet(conf, userProvider);
67 }
68 return INSTANCE;
69 }
70
71 public synchronized static void stop() {
72 if (INSTANCE != null) INSTANCE = null;
73 }
74
75
76
77
78
79
80
81 RESTServlet(final Configuration conf,
82 final UserProvider userProvider) throws IOException {
83 this.realUser = userProvider.getCurrent().getUGI();
84 this.conf = conf;
85
86 int cleanInterval = conf.getInt(CLEANUP_INTERVAL, 10 * 1000);
87 int maxIdleTime = conf.getInt(MAX_IDLETIME, 10 * 60 * 1000);
88 connectionCache = new ConnectionCache(
89 conf, userProvider, cleanInterval, maxIdleTime);
90 }
91
92 HBaseAdmin getAdmin() throws IOException {
93 return connectionCache.getAdmin();
94 }
95
96
97
98
99 HTableInterface getTable(String tableName) throws IOException {
100 return connectionCache.getTable(tableName);
101 }
102
103 Configuration getConfiguration() {
104 return conf;
105 }
106
107 MetricsREST getMetrics() {
108 return metrics;
109 }
110
111
112
113
114
115
116 boolean isReadOnly() {
117 return getConfiguration().getBoolean("hbase.rest.readonly", false);
118 }
119
120 void setEffectiveUser(String effectiveUser) {
121 connectionCache.setEffectiveUser(effectiveUser);
122 }
123 }