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 java.io.ByteArrayInputStream;
22 import java.io.ByteArrayOutputStream;
23 import java.io.DataInput;
24 import java.io.DataOutput;
25 import java.io.IOException;
26 import java.io.InputStream;
27 import java.io.ObjectInputStream;
28 import java.io.ObjectOutputStream;
29 import java.io.Serializable;
30 import java.lang.reflect.Array;
31 import java.lang.reflect.InvocationTargetException;
32 import java.lang.reflect.Method;
33 import java.util.ArrayList;
34 import java.util.HashMap;
35 import java.util.List;
36 import java.util.Map;
37 import java.util.NavigableSet;
38
39 import org.apache.commons.logging.Log;
40 import org.apache.commons.logging.LogFactory;
41 import org.apache.hadoop.hbase.classification.InterfaceAudience;
42 import org.apache.hadoop.conf.Configurable;
43 import org.apache.hadoop.conf.Configuration;
44 import org.apache.hadoop.conf.Configured;
45 import org.apache.hadoop.hbase.ClusterStatus;
46 import org.apache.hadoop.hbase.HColumnDescriptor;
47 import org.apache.hadoop.hbase.HConstants;
48 import org.apache.hadoop.hbase.HRegionInfo;
49 import org.apache.hadoop.hbase.HTableDescriptor;
50 import org.apache.hadoop.hbase.KeyValue;
51 import org.apache.hadoop.hbase.client.Action;
52 import org.apache.hadoop.hbase.client.Append;
53 import org.apache.hadoop.hbase.client.Delete;
54 import org.apache.hadoop.hbase.client.Get;
55 import org.apache.hadoop.hbase.client.Increment;
56 import org.apache.hadoop.hbase.client.MultiAction;
57 import org.apache.hadoop.hbase.client.MultiResponse;
58 import org.apache.hadoop.hbase.client.Put;
59 import org.apache.hadoop.hbase.client.Result;
60 import org.apache.hadoop.hbase.client.Row;
61 import org.apache.hadoop.hbase.client.RowMutations;
62 import org.apache.hadoop.hbase.client.Scan;
63 import org.apache.hadoop.hbase.filter.BinaryComparator;
64 import org.apache.hadoop.hbase.filter.BitComparator;
65 import org.apache.hadoop.hbase.filter.ByteArrayComparable;
66 import org.apache.hadoop.hbase.filter.ColumnCountGetFilter;
67 import org.apache.hadoop.hbase.filter.ColumnPrefixFilter;
68 import org.apache.hadoop.hbase.filter.ColumnRangeFilter;
69 import org.apache.hadoop.hbase.filter.CompareFilter;
70 import org.apache.hadoop.hbase.filter.CompareFilter.CompareOp;
71 import org.apache.hadoop.hbase.filter.DependentColumnFilter;
72 import org.apache.hadoop.hbase.filter.FirstKeyOnlyFilter;
73 import org.apache.hadoop.hbase.filter.InclusiveStopFilter;
74 import org.apache.hadoop.hbase.filter.KeyOnlyFilter;
75 import org.apache.hadoop.hbase.filter.PageFilter;
76 import org.apache.hadoop.hbase.filter.PrefixFilter;
77 import org.apache.hadoop.hbase.filter.QualifierFilter;
78 import org.apache.hadoop.hbase.filter.RandomRowFilter;
79 import org.apache.hadoop.hbase.filter.RowFilter;
80 import org.apache.hadoop.hbase.filter.SingleColumnValueExcludeFilter;
81 import org.apache.hadoop.hbase.filter.SingleColumnValueFilter;
82 import org.apache.hadoop.hbase.filter.SkipFilter;
83 import org.apache.hadoop.hbase.filter.ValueFilter;
84 import org.apache.hadoop.hbase.filter.WhileMatchFilter;
85 import org.apache.hadoop.hbase.io.DataOutputOutputStream;
86 import org.apache.hadoop.hbase.io.WritableWithSize;
87 import org.apache.hadoop.hbase.protobuf.ProtobufUtil;
88 import org.apache.hadoop.hbase.protobuf.generated.ClientProtos;
89 import org.apache.hadoop.hbase.regionserver.RegionOpeningState;
90 import org.apache.hadoop.hbase.regionserver.wal.HLogKey;
91 import org.apache.hadoop.hbase.regionserver.wal.WALEdit;
92 import org.apache.hadoop.hbase.wal.WAL.Entry;
93 import org.apache.hadoop.hbase.wal.WALKey;
94 import org.apache.hadoop.hbase.util.Bytes;
95 import org.apache.hadoop.hbase.util.ProtoUtil;
96 import org.apache.hadoop.io.MapWritable;
97 import org.apache.hadoop.io.Text;
98 import org.apache.hadoop.io.Writable;
99 import org.apache.hadoop.io.WritableFactories;
100 import org.apache.hadoop.io.WritableUtils;
101
102 import com.google.protobuf.Message;
103 import com.google.protobuf.RpcController;
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124 @Deprecated
125 @InterfaceAudience.Private
126 class HbaseObjectWritableFor96Migration implements Writable, WritableWithSize, Configurable {
127 private final static Log LOG = LogFactory.getLog(HbaseObjectWritableFor96Migration.class);
128
129
130
131
132 static final Map<Integer, Class<?>> CODE_TO_CLASS =
133 new HashMap<Integer, Class<?>>();
134 static final Map<Class<?>, Integer> CLASS_TO_CODE =
135 new HashMap<Class<?>, Integer>();
136
137
138 private static final byte NOT_ENCODED = 0;
139
140
141
142 private static final int GENERIC_ARRAY_CODE;
143 private static final int NEXT_CLASS_CODE;
144 static {
145
146
147
148
149
150
151
152
153 int code = NOT_ENCODED + 1;
154
155 addToMap(Boolean.TYPE, code++);
156 addToMap(Byte.TYPE, code++);
157 addToMap(Character.TYPE, code++);
158 addToMap(Short.TYPE, code++);
159 addToMap(Integer.TYPE, code++);
160 addToMap(Long.TYPE, code++);
161 addToMap(Float.TYPE, code++);
162 addToMap(Double.TYPE, code++);
163 addToMap(Void.TYPE, code++);
164
165
166 addToMap(String.class, code++);
167 addToMap(byte [].class, code++);
168 addToMap(byte [][].class, code++);
169
170
171 addToMap(Text.class, code++);
172 addToMap(Writable.class, code++);
173 addToMap(Writable [].class, code++);
174 code++;
175 addToMap(NullInstance.class, code++);
176
177
178 addToMap(HColumnDescriptor.class, code++);
179 addToMap(HConstants.Modify.class, code++);
180
181
182
183
184
185 addToMap(Integer.class, code++);
186 addToMap(Integer[].class, code++);
187
188
189 code++;
190 code++;
191
192 addToMap(HRegionInfo.class, code++);
193 addToMap(HRegionInfo[].class, code++);
194 code++;
195 code++;
196 addToMap(HTableDescriptor.class, code++);
197 addToMap(MapWritable.class, code++);
198
199
200
201
202 addToMap(ClusterStatus.class, code++);
203 addToMap(Delete.class, code++);
204 addToMap(Get.class, code++);
205 addToMap(KeyValue.class, code++);
206 addToMap(KeyValue[].class, code++);
207 addToMap(Put.class, code++);
208 addToMap(Put[].class, code++);
209 addToMap(Result.class, code++);
210 addToMap(Result[].class, code++);
211 addToMap(Scan.class, code++);
212
213 addToMap(WhileMatchFilter.class, code++);
214 addToMap(PrefixFilter.class, code++);
215 addToMap(PageFilter.class, code++);
216 addToMap(InclusiveStopFilter.class, code++);
217 addToMap(ColumnCountGetFilter.class, code++);
218 addToMap(SingleColumnValueFilter.class, code++);
219 addToMap(SingleColumnValueExcludeFilter.class, code++);
220 addToMap(BinaryComparator.class, code++);
221 addToMap(BitComparator.class, code++);
222 addToMap(CompareFilter.class, code++);
223 addToMap(RowFilter.class, code++);
224 addToMap(ValueFilter.class, code++);
225 addToMap(QualifierFilter.class, code++);
226 addToMap(SkipFilter.class, code++);
227 addToMap(ByteArrayComparable.class, code++);
228 addToMap(FirstKeyOnlyFilter.class, code++);
229 addToMap(DependentColumnFilter.class, code++);
230
231 addToMap(Delete [].class, code++);
232
233 addToMap(Entry.class, code++);
234 addToMap(Entry[].class, code++);
235 addToMap(HLogKey.class, code++);
236
237 addToMap(List.class, code++);
238
239 addToMap(NavigableSet.class, code++);
240 addToMap(ColumnPrefixFilter.class, code++);
241
242
243 addToMap(Row.class, code++);
244 addToMap(Action.class, code++);
245 addToMap(MultiAction.class, code++);
246 addToMap(MultiResponse.class, code++);
247
248
249
250 code++;
251 addToMap(Increment.class, code++);
252
253 addToMap(KeyOnlyFilter.class, code++);
254
255
256 addToMap(Serializable.class, code++);
257
258 addToMap(RandomRowFilter.class, code++);
259
260 addToMap(CompareOp.class, code++);
261
262 addToMap(ColumnRangeFilter.class, code++);
263
264
265 code++;
266
267
268 addToMap(RegionOpeningState.class, code++);
269
270 addToMap(HTableDescriptor[].class, code++);
271
272 addToMap(Append.class, code++);
273
274 addToMap(RowMutations.class, code++);
275
276 addToMap(Message.class, code++);
277
278
279 GENERIC_ARRAY_CODE = code++;
280 addToMap(Array.class, GENERIC_ARRAY_CODE);
281
282 addToMap(RpcController.class, code++);
283
284
285 NEXT_CLASS_CODE = code;
286 }
287
288 private Class<?> declaredClass;
289 private Object instance;
290 private Configuration conf;
291
292
293 HbaseObjectWritableFor96Migration() {
294 super();
295 }
296
297
298
299
300 HbaseObjectWritableFor96Migration(Object instance) {
301 set(instance);
302 }
303
304
305
306
307
308 HbaseObjectWritableFor96Migration(Class<?> declaredClass, Object instance) {
309 this.declaredClass = declaredClass;
310 this.instance = instance;
311 }
312
313
314 Object get() { return instance; }
315
316
317 Class<?> getDeclaredClass() { return declaredClass; }
318
319
320
321
322
323 void set(Object instance) {
324 this.declaredClass = instance.getClass();
325 this.instance = instance;
326 }
327
328
329
330
331 @Override
332 public String toString() {
333 return "OW[class=" + declaredClass + ",value=" + instance + "]";
334 }
335
336
337 public void readFields(DataInput in) throws IOException {
338 readObject(in, this, this.conf);
339 }
340
341 public void write(DataOutput out) throws IOException {
342 writeObject(out, instance, declaredClass, conf);
343 }
344
345 public long getWritableSize() {
346 return getWritableSize(instance, declaredClass, conf);
347 }
348
349 private static class NullInstance extends Configured implements Writable {
350 Class<?> declaredClass;
351
352 @SuppressWarnings("unused")
353 public NullInstance() { super(null); }
354
355
356
357
358
359 public NullInstance(Class<?> declaredClass, Configuration conf) {
360 super(conf);
361 this.declaredClass = declaredClass;
362 }
363
364 public void readFields(DataInput in) throws IOException {
365 this.declaredClass = CODE_TO_CLASS.get(WritableUtils.readVInt(in));
366 }
367
368 public void write(DataOutput out) throws IOException {
369 writeClassCode(out, this.declaredClass);
370 }
371 }
372
373 static Integer getClassCode(final Class<?> c)
374 throws IOException {
375 Integer code = CLASS_TO_CODE.get(c);
376 if (code == null ) {
377 if (List.class.isAssignableFrom(c)) {
378 code = CLASS_TO_CODE.get(List.class);
379 } else if (Writable.class.isAssignableFrom(c)) {
380 code = CLASS_TO_CODE.get(Writable.class);
381 } else if (c.isArray()) {
382 code = CLASS_TO_CODE.get(Array.class);
383 } else if (Message.class.isAssignableFrom(c)) {
384 code = CLASS_TO_CODE.get(Message.class);
385 } else if (Serializable.class.isAssignableFrom(c)){
386 code = CLASS_TO_CODE.get(Serializable.class);
387 } else if (Scan.class.isAssignableFrom(c)) {
388 code = CLASS_TO_CODE.get(Scan.class);
389 }
390 }
391 return code;
392 }
393
394
395
396
397 static int getNextClassCode(){
398 return NEXT_CLASS_CODE;
399 }
400
401
402
403
404
405
406
407 static void writeClassCode(final DataOutput out, final Class<?> c)
408 throws IOException {
409 Integer code = getClassCode(c);
410
411 if (code == null) {
412 LOG.error("Unsupported type " + c);
413 StackTraceElement[] els = new Exception().getStackTrace();
414 for(StackTraceElement elem : els) {
415 LOG.error(elem.getMethodName());
416 }
417 throw new UnsupportedOperationException("No code for unexpected " + c);
418 }
419 WritableUtils.writeVInt(out, code);
420 }
421
422 static long getWritableSize(Object instance, Class declaredClass,
423 Configuration conf) {
424 return 0L;
425 }
426
427
428
429
430
431
432
433
434
435 @SuppressWarnings("unchecked")
436 static void writeObject(DataOutput out, Object instance,
437 Class declaredClass,
438 Configuration conf)
439 throws IOException {
440
441 Object instanceObj = instance;
442 Class declClass = declaredClass;
443
444 if (instanceObj == null) {
445 instanceObj = new NullInstance(declClass, conf);
446 declClass = Writable.class;
447 }
448 writeClassCode(out, declClass);
449 if (declClass.isArray()) {
450
451
452 if (declClass.equals(byte [].class)) {
453 Bytes.writeByteArray(out, (byte [])instanceObj);
454 } else {
455
456 if (getClassCode(declaredClass) == GENERIC_ARRAY_CODE) {
457 Class<?> componentType = declaredClass.getComponentType();
458 writeClass(out, componentType);
459 }
460
461 int length = Array.getLength(instanceObj);
462 out.writeInt(length);
463 for (int i = 0; i < length; i++) {
464 Object item = Array.get(instanceObj, i);
465 writeObject(out, item,
466 item.getClass(), conf);
467 }
468 }
469 } else if (List.class.isAssignableFrom(declClass)) {
470 List list = (List)instanceObj;
471 int length = list.size();
472 out.writeInt(length);
473 for (int i = 0; i < length; i++) {
474 Object elem = list.get(i);
475 writeObject(out, elem,
476 elem == null ? Writable.class : elem.getClass(), conf);
477 }
478 } else if (declClass == String.class) {
479 Text.writeString(out, (String)instanceObj);
480 } else if (declClass.isPrimitive()) {
481 if (declClass == Boolean.TYPE) {
482 out.writeBoolean(((Boolean)instanceObj).booleanValue());
483 } else if (declClass == Character.TYPE) {
484 out.writeChar(((Character)instanceObj).charValue());
485 } else if (declClass == Byte.TYPE) {
486 out.writeByte(((Byte)instanceObj).byteValue());
487 } else if (declClass == Short.TYPE) {
488 out.writeShort(((Short)instanceObj).shortValue());
489 } else if (declClass == Integer.TYPE) {
490 out.writeInt(((Integer)instanceObj).intValue());
491 } else if (declClass == Long.TYPE) {
492 out.writeLong(((Long)instanceObj).longValue());
493 } else if (declClass == Float.TYPE) {
494 out.writeFloat(((Float)instanceObj).floatValue());
495 } else if (declClass == Double.TYPE) {
496 out.writeDouble(((Double)instanceObj).doubleValue());
497 } else if (declClass == Void.TYPE) {
498 } else {
499 throw new IllegalArgumentException("Not a primitive: "+declClass);
500 }
501 } else if (declClass.isEnum()) {
502 Text.writeString(out, ((Enum)instanceObj).name());
503 } else if (Message.class.isAssignableFrom(declaredClass)) {
504 Text.writeString(out, instanceObj.getClass().getName());
505 ((Message)instance).writeDelimitedTo(
506 DataOutputOutputStream.constructOutputStream(out));
507 } else if (Writable.class.isAssignableFrom(declClass)) {
508 Class <?> c = instanceObj.getClass();
509 Integer code = CLASS_TO_CODE.get(c);
510 if (code == null) {
511 out.writeByte(NOT_ENCODED);
512 Text.writeString(out, c.getName());
513 } else {
514 writeClassCode(out, c);
515 }
516 ((Writable)instanceObj).write(out);
517 } else if (Serializable.class.isAssignableFrom(declClass)) {
518 Class <?> c = instanceObj.getClass();
519 Integer code = CLASS_TO_CODE.get(c);
520 if (code == null) {
521 out.writeByte(NOT_ENCODED);
522 Text.writeString(out, c.getName());
523 } else {
524 writeClassCode(out, c);
525 }
526 ByteArrayOutputStream bos = null;
527 ObjectOutputStream oos = null;
528 try{
529 bos = new ByteArrayOutputStream();
530 oos = new ObjectOutputStream(bos);
531 oos.writeObject(instanceObj);
532 byte[] value = bos.toByteArray();
533 out.writeInt(value.length);
534 out.write(value);
535 } finally {
536 if(bos!=null) bos.close();
537 if(oos!=null) oos.close();
538 }
539 } else if (Scan.class.isAssignableFrom(declClass)) {
540 Scan scan = (Scan)instanceObj;
541 byte [] scanBytes = ProtobufUtil.toScan(scan).toByteArray();
542 out.writeInt(scanBytes.length);
543 out.write(scanBytes);
544 } else if (Entry.class.isAssignableFrom(declClass)) {
545
546
547 Class <?> c = instanceObj.getClass();
548 Integer code = CLASS_TO_CODE.get(c);
549 if (code == null) {
550 out.writeByte(NOT_ENCODED);
551 Text.writeString(out, c.getName());
552 } else {
553 writeClassCode(out, c);
554 }
555 final Entry entry = (Entry)instanceObj;
556
557 WALKey key = entry.getKey();
558 if (!(key instanceof HLogKey)) {
559 throw new IOException("Can't write Entry '" + instanceObj + "' due to key class '" +
560 key.getClass() + "'");
561 }
562 ((HLogKey)key).write(out);
563 entry.getEdit().write(out);
564 } else {
565 throw new IOException("Can't write: "+instanceObj+" as "+declClass);
566 }
567 }
568
569
570
571
572 static void writeClass(DataOutput out, Class<?> c) throws IOException {
573 Integer code = CLASS_TO_CODE.get(c);
574 if (code == null) {
575 WritableUtils.writeVInt(out, NOT_ENCODED);
576 Text.writeString(out, c.getName());
577 } else {
578 WritableUtils.writeVInt(out, code);
579 }
580 }
581
582
583 static Class<?> readClass(Configuration conf, DataInput in) throws IOException {
584 Class<?> instanceClass = null;
585 int b = (byte)WritableUtils.readVInt(in);
586 if (b == NOT_ENCODED) {
587 String className = Text.readString(in);
588 try {
589 instanceClass = getClassByName(conf, className);
590 } catch (ClassNotFoundException e) {
591 LOG.error("Can't find class " + className, e);
592 throw new IOException("Can't find class " + className, e);
593 }
594 } else {
595 instanceClass = CODE_TO_CLASS.get(b);
596 }
597 return instanceClass;
598 }
599
600
601
602
603
604
605
606
607
608 static Object readObject(DataInput in, Configuration conf)
609 throws IOException {
610 return readObject(in, null, conf);
611 }
612
613
614
615
616
617
618
619
620
621
622 @SuppressWarnings("unchecked")
623 static Object readObject(DataInput in,
624 HbaseObjectWritableFor96Migration objectWritable, Configuration conf)
625 throws IOException {
626 Class<?> declaredClass = CODE_TO_CLASS.get(WritableUtils.readVInt(in));
627 Object instance;
628 if (declaredClass.isPrimitive()) {
629 if (declaredClass == Boolean.TYPE) {
630 instance = Boolean.valueOf(in.readBoolean());
631 } else if (declaredClass == Character.TYPE) {
632 instance = Character.valueOf(in.readChar());
633 } else if (declaredClass == Byte.TYPE) {
634 instance = Byte.valueOf(in.readByte());
635 } else if (declaredClass == Short.TYPE) {
636 instance = Short.valueOf(in.readShort());
637 } else if (declaredClass == Integer.TYPE) {
638 instance = Integer.valueOf(in.readInt());
639 } else if (declaredClass == Long.TYPE) {
640 instance = Long.valueOf(in.readLong());
641 } else if (declaredClass == Float.TYPE) {
642 instance = Float.valueOf(in.readFloat());
643 } else if (declaredClass == Double.TYPE) {
644 instance = Double.valueOf(in.readDouble());
645 } else if (declaredClass == Void.TYPE) {
646 instance = null;
647 } else {
648 throw new IllegalArgumentException("Not a primitive: "+declaredClass);
649 }
650 } else if (declaredClass.isArray()) {
651 if (declaredClass.equals(byte [].class)) {
652 instance = Bytes.readByteArray(in);
653 } else {
654 int length = in.readInt();
655 instance = Array.newInstance(declaredClass.getComponentType(), length);
656 for (int i = 0; i < length; i++) {
657 Array.set(instance, i, readObject(in, conf));
658 }
659 }
660 } else if (declaredClass.equals(Array.class)) {
661 Class<?> componentType = readClass(conf, in);
662 int length = in.readInt();
663 instance = Array.newInstance(componentType, length);
664 for (int i = 0; i < length; i++) {
665 Array.set(instance, i, readObject(in, conf));
666 }
667 } else if (List.class.isAssignableFrom(declaredClass)) {
668 int length = in.readInt();
669 instance = new ArrayList(length);
670 for (int i = 0; i < length; i++) {
671 ((ArrayList)instance).add(readObject(in, conf));
672 }
673 } else if (declaredClass == String.class) {
674 instance = Text.readString(in);
675 } else if (declaredClass.isEnum()) {
676 instance = Enum.valueOf((Class<? extends Enum>) declaredClass,
677 Text.readString(in));
678 } else if (declaredClass == Message.class) {
679 String className = Text.readString(in);
680 try {
681 declaredClass = getClassByName(conf, className);
682 instance = tryInstantiateProtobuf(declaredClass, in);
683 } catch (ClassNotFoundException e) {
684 LOG.error("Can't find class " + className, e);
685 throw new IOException("Can't find class " + className, e);
686 }
687 } else if (Scan.class.isAssignableFrom(declaredClass)) {
688 int length = in.readInt();
689 byte [] scanBytes = new byte[length];
690 in.readFully(scanBytes);
691 ClientProtos.Scan.Builder scanProto = ClientProtos.Scan.newBuilder();
692 ProtobufUtil.mergeFrom(scanProto, scanBytes);
693 instance = ProtobufUtil.toScan(scanProto.build());
694 } else {
695 Class instanceClass = null;
696 int b = (byte)WritableUtils.readVInt(in);
697 if (b == NOT_ENCODED) {
698 String className = Text.readString(in);
699 if ("org.apache.hadoop.hbase.regionserver.wal.HLog$Entry".equals(className)) {
700 className = Entry.class.getName();
701 }
702 try {
703 instanceClass = getClassByName(conf, className);
704 } catch (ClassNotFoundException e) {
705 LOG.error("Can't find class " + className, e);
706 throw new IOException("Can't find class " + className, e);
707 }
708 } else {
709 instanceClass = CODE_TO_CLASS.get(b);
710 }
711 if(Writable.class.isAssignableFrom(instanceClass)){
712 Writable writable = WritableFactories.newInstance(instanceClass, conf);
713 try {
714 writable.readFields(in);
715 } catch (Exception e) {
716 LOG.error("Error in readFields", e);
717 throw new IOException("Error in readFields" , e);
718 }
719 instance = writable;
720 if (instanceClass == NullInstance.class) {
721 declaredClass = ((NullInstance)instance).declaredClass;
722 instance = null;
723 }
724 } else if (Entry.class.isAssignableFrom(instanceClass)) {
725
726 final HLogKey key = new HLogKey();
727 final WALEdit edit = new WALEdit();
728 key.readFields(in);
729 edit.readFields(in);
730 instance = new Entry(key, edit);
731 } else {
732 int length = in.readInt();
733 byte[] objectBytes = new byte[length];
734 in.readFully(objectBytes);
735 ByteArrayInputStream bis = null;
736 ObjectInputStream ois = null;
737 try {
738 bis = new ByteArrayInputStream(objectBytes);
739 ois = new ObjectInputStream(bis);
740 instance = ois.readObject();
741 } catch (ClassNotFoundException e) {
742 LOG.error("Class not found when attempting to deserialize object", e);
743 throw new IOException("Class not found when attempting to " +
744 "deserialize object", e);
745 } finally {
746 if(bis!=null) bis.close();
747 if(ois!=null) ois.close();
748 }
749 }
750 }
751 if (objectWritable != null) {
752 objectWritable.declaredClass = declaredClass;
753 objectWritable.instance = instance;
754 }
755 return instance;
756 }
757
758
759
760
761
762
763
764
765
766
767 static Message tryInstantiateProtobuf(
768 Class<?> protoClass,
769 DataInput dataIn) throws IOException {
770
771 try {
772 if (dataIn instanceof InputStream) {
773
774
775 Method parseMethod = getStaticProtobufMethod(protoClass,
776 "parseDelimitedFrom", InputStream.class);
777 return (Message)parseMethod.invoke(null, (InputStream)dataIn);
778 } else {
779
780
781
782
783 int size = ProtoUtil.readRawVarint32(dataIn);
784 if (size < 0) {
785 throw new IOException("Invalid size: " + size);
786 }
787
788 byte[] data = new byte[size];
789 dataIn.readFully(data);
790 Method parseMethod = getStaticProtobufMethod(protoClass,
791 "parseFrom", byte[].class);
792 return (Message)parseMethod.invoke(null, data);
793 }
794 } catch (InvocationTargetException e) {
795
796 if (e.getCause() instanceof IOException) {
797 throw (IOException)e.getCause();
798 } else {
799 throw new IOException(e.getCause());
800 }
801 } catch (IllegalAccessException iae) {
802 throw new AssertionError("Could not access parse method in " +
803 protoClass);
804 }
805 }
806
807 static Method getStaticProtobufMethod(Class<?> declaredClass, String method,
808 Class<?> ... args) {
809
810 try {
811 return declaredClass.getMethod(method, args);
812 } catch (Exception e) {
813
814 throw new AssertionError("Protocol buffer class " + declaredClass +
815 " does not have an accessible parseFrom(InputStream) method!");
816 }
817 }
818
819 @SuppressWarnings("unchecked")
820 private static Class getClassByName(Configuration conf, String className)
821 throws ClassNotFoundException {
822 if(conf != null) {
823 return conf.getClassByName(className);
824 }
825 ClassLoader cl = Thread.currentThread().getContextClassLoader();
826 if(cl == null) {
827 cl = HbaseObjectWritableFor96Migration.class.getClassLoader();
828 }
829 return Class.forName(className, true, cl);
830 }
831
832 private static void addToMap(final Class<?> clazz, final int code) {
833 CLASS_TO_CODE.put(clazz, code);
834 CODE_TO_CLASS.put(code, clazz);
835 }
836
837 public void setConf(Configuration conf) {
838 this.conf = conf;
839 }
840
841 public Configuration getConf() {
842 return this.conf;
843 }
844 }