1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19 package org.apache.hadoop.hbase.security.access;
20
21 import org.apache.commons.logging.Log;
22 import org.apache.commons.logging.LogFactory;
23 import org.apache.hadoop.hbase.KeyValue;
24 import org.apache.hadoop.hbase.util.Bytes;
25
26 import java.io.DataInput;
27 import java.io.DataOutput;
28 import java.io.IOException;
29
30
31
32
33
34
35
36 public class TablePermission extends Permission {
37 private static Log LOG = LogFactory.getLog(TablePermission.class);
38
39 private byte[] table;
40 private byte[] family;
41 private byte[] qualifier;
42
43
44 public TablePermission() {
45 super();
46 }
47
48
49
50
51
52
53
54
55 public TablePermission(byte[] table, byte[] family, Action... assigned) {
56 this(table, family, null, assigned);
57 }
58
59
60
61
62
63
64
65
66 public TablePermission(byte[] table, byte[] family, byte[] qualifier,
67 Action... assigned) {
68 super(assigned);
69 this.table = table;
70 this.family = family;
71 this.qualifier = qualifier;
72 }
73
74
75
76
77
78
79
80
81 public TablePermission(byte[] table, byte[] family, byte[] qualifier,
82 byte[] actionCodes) {
83 super(actionCodes);
84 this.table = table;
85 this.family = family;
86 this.qualifier = qualifier;
87 }
88
89 public boolean hasTable() {
90 return table != null;
91 }
92
93 public byte[] getTable() {
94 return table;
95 }
96
97 public boolean hasFamily() {
98 return family != null;
99 }
100
101 public byte[] getFamily() {
102 return family;
103 }
104
105 public boolean hasQualifier() {
106 return qualifier != null;
107 }
108
109 public byte[] getQualifier() {
110 return qualifier;
111 }
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126 public boolean implies(byte[] table, byte[] family, byte[] qualifier,
127 Action action) {
128 if (!Bytes.equals(this.table, table)) {
129 return false;
130 }
131
132 if (this.family != null &&
133 (family == null ||
134 !Bytes.equals(this.family, family))) {
135 return false;
136 }
137
138 if (this.qualifier != null &&
139 (qualifier == null ||
140 !Bytes.equals(this.qualifier, qualifier))) {
141 return false;
142 }
143
144
145 return super.implies(action);
146 }
147
148
149
150
151
152
153
154
155
156
157 public boolean implies(byte[] table, KeyValue kv, Action action) {
158 if (!Bytes.equals(this.table, table)) {
159 return false;
160 }
161
162 if (family != null &&
163 (Bytes.compareTo(family, 0, family.length,
164 kv.getBuffer(), kv.getFamilyOffset(), kv.getFamilyLength()) != 0)) {
165 return false;
166 }
167
168 if (qualifier != null &&
169 (Bytes.compareTo(qualifier, 0, qualifier.length,
170 kv.getBuffer(), kv.getQualifierOffset(), kv.getQualifierLength()) != 0)) {
171 return false;
172 }
173
174
175 return super.implies(action);
176 }
177
178
179
180
181
182
183
184
185
186 public boolean matchesFamily(byte[] table, byte[] family, Action action) {
187 if (!Bytes.equals(this.table, table)) {
188 return false;
189 }
190
191 if (this.family != null &&
192 (family == null ||
193 !Bytes.equals(this.family, family))) {
194 return false;
195 }
196
197
198
199 return super.implies(action);
200 }
201
202
203
204
205
206
207
208
209
210
211 public boolean matchesFamilyQualifier(byte[] table, byte[] family, byte[] qualifier,
212 Action action) {
213 if (!matchesFamily(table, family, action)) {
214 return false;
215 } else {
216 if (this.qualifier != null &&
217 (qualifier == null ||
218 !Bytes.equals(this.qualifier, qualifier))) {
219 return false;
220 }
221 }
222 return super.implies(action);
223 }
224
225 @Override
226 public boolean equals(Object obj) {
227 if (!(obj instanceof TablePermission)) {
228 return false;
229 }
230 TablePermission other = (TablePermission)obj;
231
232 if (!(Bytes.equals(table, other.getTable()) &&
233 ((family == null && other.getFamily() == null) ||
234 Bytes.equals(family, other.getFamily())) &&
235 ((qualifier == null && other.getQualifier() == null) ||
236 Bytes.equals(qualifier, other.getQualifier()))
237 )) {
238 return false;
239 }
240
241
242 return super.equals(other);
243 }
244
245 @Override
246 public int hashCode() {
247 final int prime = 37;
248 int result = super.hashCode();
249 if (table != null) {
250 result = prime * result + Bytes.hashCode(table);
251 }
252 if (family != null) {
253 result = prime * result + Bytes.hashCode(family);
254 }
255 if (qualifier != null) {
256 result = prime * result + Bytes.hashCode(qualifier);
257 }
258 return result;
259 }
260
261 public String toString() {
262 StringBuilder str = new StringBuilder("[TablePermission: ")
263 .append("table=").append(Bytes.toString(table))
264 .append(", family=").append(Bytes.toString(family))
265 .append(", qualifier=").append(Bytes.toString(qualifier))
266 .append(", actions=");
267 if (actions != null) {
268 for (int i=0; i<actions.length; i++) {
269 if (i > 0)
270 str.append(",");
271 if (actions[i] != null)
272 str.append(actions[i].toString());
273 else
274 str.append("NULL");
275 }
276 }
277 str.append("]");
278
279 return str.toString();
280 }
281
282 @Override
283 public void readFields(DataInput in) throws IOException {
284 super.readFields(in);
285 table = Bytes.readByteArray(in);
286 if (in.readBoolean()) {
287 family = Bytes.readByteArray(in);
288 }
289 if (in.readBoolean()) {
290 qualifier = Bytes.readByteArray(in);
291 }
292 }
293
294 @Override
295 public void write(DataOutput out) throws IOException {
296 super.write(out);
297 Bytes.writeByteArray(out, table);
298 out.writeBoolean(family != null);
299 if (family != null) {
300 Bytes.writeByteArray(out, family);
301 }
302 out.writeBoolean(qualifier != null);
303 if (qualifier != null) {
304 Bytes.writeByteArray(out, qualifier);
305 }
306 }
307 }