1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20 package org.apache.hadoop.hbase.util;
21
22 import java.io.IOException;
23 import java.lang.ClassNotFoundException;
24 import java.util.zip.Checksum;
25 import java.lang.reflect.Constructor;
26
27 import org.apache.hadoop.hbase.classification.InterfaceAudience;
28
29
30
31
32
33
34
35 @InterfaceAudience.Private
36 public class ChecksumFactory {
37
38 static private final Class<?>[] EMPTY_ARRAY = new Class[]{};
39
40
41
42
43
44 static public Checksum newInstance(String className) throws IOException {
45 try {
46 Class<?> clazz = getClassByName(className);
47 return (Checksum)newInstance(clazz);
48 } catch (ClassNotFoundException e) {
49 throw new IOException(e);
50 }
51 }
52
53
54
55
56
57
58 static public Constructor<?> newConstructor(String className)
59 throws IOException {
60 try {
61 Class<?> clazz = getClassByName(className);
62 Constructor<?> ctor = clazz.getDeclaredConstructor(EMPTY_ARRAY);
63 ctor.setAccessible(true);
64 return ctor;
65 } catch (ClassNotFoundException e) {
66 throw new IOException(e);
67 } catch (java.lang.NoSuchMethodException e) {
68 throw new IOException(e);
69 }
70 }
71
72
73
74
75
76
77 static private <T> T newInstance(Class<T> theClass) {
78 T result;
79 try {
80 Constructor<T> ctor = theClass.getDeclaredConstructor(EMPTY_ARRAY);
81 ctor.setAccessible(true);
82 result = ctor.newInstance();
83 } catch (Exception e) {
84 throw new RuntimeException(e);
85 }
86 return result;
87 }
88
89
90
91
92
93
94
95 static private Class<?> getClassByName(String name)
96 throws ClassNotFoundException {
97 ClassLoader classLoader = Thread.currentThread().getContextClassLoader();
98 return Class.forName(name, true, classLoader);
99 }
100 }