1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21 package org.apache.hadoop.hbase.ipc;
22
23 import org.apache.hadoop.classification.InterfaceAudience;
24 import org.apache.hadoop.hbase.security.User;
25
26 import com.google.protobuf.BlockingService;
27
28 import java.net.InetAddress;
29
30
31
32
33
34
35
36 @InterfaceAudience.Private
37 public class RequestContext {
38 private static ThreadLocal<RequestContext> instance =
39 new ThreadLocal<RequestContext>() {
40 protected RequestContext initialValue() {
41 return new RequestContext(null, null, null);
42 }
43 };
44
45 public static RequestContext get() {
46 return instance.get();
47 }
48
49
50
51
52
53
54
55 public static User getRequestUser() {
56 RequestContext ctx = instance.get();
57 if (ctx != null) {
58 return ctx.getUser();
59 }
60 return null;
61 }
62
63
64
65
66
67 public static String getRequestUserName() {
68 User user = getRequestUser();
69 if (user != null) {
70 return user.getShortName();
71 }
72 return null;
73 }
74
75
76
77
78
79 public static boolean isInRequestContext() {
80 RequestContext ctx = instance.get();
81 if (ctx != null) {
82 return ctx.isInRequest();
83 }
84 return false;
85 }
86
87
88
89
90
91
92
93 public static void set(User user,
94 InetAddress remoteAddress, BlockingService service) {
95 RequestContext ctx = instance.get();
96 ctx.user = user;
97 ctx.remoteAddress = remoteAddress;
98 ctx.service = service;
99 ctx.inRequest = true;
100 }
101
102
103
104
105 public static void clear() {
106 RequestContext ctx = instance.get();
107 ctx.user = null;
108 ctx.remoteAddress = null;
109 ctx.service = null;
110 ctx.inRequest = false;
111 }
112
113 private User user;
114 private InetAddress remoteAddress;
115 private BlockingService service;
116
117 private boolean inRequest;
118
119 private RequestContext(User user, InetAddress remoteAddr, BlockingService service) {
120 this.user = user;
121 this.remoteAddress = remoteAddr;
122 this.service = service;
123 }
124
125 public User getUser() {
126 return user;
127 }
128
129 public InetAddress getRemoteAddress() {
130 return remoteAddress;
131 }
132
133 public BlockingService getService() {
134 return this.service;
135 }
136
137 public boolean isInRequest() {
138 return inRequest;
139 }
140 }