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 byte[] getTable() {
90 return table;
91 }
92
93 public byte[] getFamily() {
94 return family;
95 }
96
97 public byte[] getQualifier() {
98 return qualifier;
99 }
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114 public boolean implies(byte[] table, byte[] family, byte[] qualifier,
115 Action action) {
116 if (!Bytes.equals(this.table, table)) {
117 return false;
118 }
119
120 if (this.family != null &&
121 (family == null ||
122 !Bytes.equals(this.family, family))) {
123 return false;
124 }
125
126 if (this.qualifier != null &&
127 (qualifier == null ||
128 !Bytes.equals(this.qualifier, qualifier))) {
129 return false;
130 }
131
132
133 return super.implies(action);
134 }
135
136
137
138
139
140
141
142
143
144
145 public boolean implies(byte[] table, KeyValue kv, Action action) {
146 if (!Bytes.equals(this.table, table)) {
147 return false;
148 }
149
150 if (family != null &&
151 (Bytes.compareTo(family, 0, family.length,
152 kv.getBuffer(), kv.getFamilyOffset(), kv.getFamilyLength()) != 0)) {
153 return false;
154 }
155
156 if (qualifier != null &&
157 (Bytes.compareTo(qualifier, 0, qualifier.length,
158 kv.getBuffer(), kv.getQualifierOffset(), kv.getQualifierLength()) != 0)) {
159 return false;
160 }
161
162
163 return super.implies(action);
164 }
165
166
167
168
169
170
171
172
173
174 public boolean matchesFamily(byte[] table, byte[] family, Action action) {
175 if (!Bytes.equals(this.table, table)) {
176 return false;
177 }
178
179 if (this.family != null &&
180 (family == null ||
181 !Bytes.equals(this.family, family))) {
182 return false;
183 }
184
185
186
187 return super.implies(action);
188 }
189
190
191
192
193
194
195
196
197
198
199 public boolean matchesFamilyQualifier(byte[] table, byte[] family, byte[] qualifier,
200 Action action) {
201 if (!matchesFamily(table, family, action)) {
202 return false;
203 } else {
204 if (this.qualifier != null &&
205 (qualifier == null ||
206 !Bytes.equals(this.qualifier, qualifier))) {
207 return false;
208 }
209 }
210 return super.implies(action);
211 }
212
213 @Override
214 public boolean equals(Object obj) {
215 if (!(obj instanceof TablePermission)) {
216 return false;
217 }
218 TablePermission other = (TablePermission)obj;
219
220 if (!(Bytes.equals(table, other.getTable()) &&
221 ((family == null && other.getFamily() == null) ||
222 Bytes.equals(family, other.getFamily())) &&
223 ((qualifier == null && other.getQualifier() == null) ||
224 Bytes.equals(qualifier, other.getQualifier()))
225 )) {
226 return false;
227 }
228
229
230 return super.equals(other);
231 }
232
233 @Override
234 public int hashCode() {
235 final int prime = 37;
236 int result = super.hashCode();
237 if (table != null) {
238 result = prime * result + Bytes.hashCode(table);
239 }
240 if (family != null) {
241 result = prime * result + Bytes.hashCode(family);
242 }
243 if (qualifier != null) {
244 result = prime * result + Bytes.hashCode(qualifier);
245 }
246 return result;
247 }
248
249 public String toString() {
250 StringBuilder str = new StringBuilder("[TablePermission: ")
251 .append("table=").append(Bytes.toString(table))
252 .append(", family=").append(Bytes.toString(family))
253 .append(", qualifier=").append(Bytes.toString(qualifier))
254 .append(", actions=");
255 if (actions != null) {
256 for (int i=0; i<actions.length; i++) {
257 if (i > 0)
258 str.append(",");
259 if (actions[i] != null)
260 str.append(actions[i].toString());
261 else
262 str.append("NULL");
263 }
264 }
265 str.append("]");
266
267 return str.toString();
268 }
269
270 @Override
271 public void readFields(DataInput in) throws IOException {
272 super.readFields(in);
273 table = Bytes.readByteArray(in);
274 if (in.readBoolean()) {
275 family = Bytes.readByteArray(in);
276 }
277 if (in.readBoolean()) {
278 qualifier = Bytes.readByteArray(in);
279 }
280 }
281
282 @Override
283 public void write(DataOutput out) throws IOException {
284 super.write(out);
285 Bytes.writeByteArray(out, table);
286 out.writeBoolean(family != null);
287 if (family != null) {
288 Bytes.writeByteArray(out, family);
289 }
290 out.writeBoolean(qualifier != null);
291 if (qualifier != null) {
292 Bytes.writeByteArray(out, qualifier);
293 }
294 }
295 }