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
28
29
30
31
32
33 public class ChecksumFactory {
34
35 static private final Class<?>[] EMPTY_ARRAY = new Class[]{};
36
37
38
39
40
41 static public Checksum newInstance(String className) throws IOException {
42 try {
43 Class<?> clazz = getClassByName(className);
44 return (Checksum)newInstance(clazz);
45 } catch (ClassNotFoundException e) {
46 throw new IOException(e);
47 }
48 }
49
50
51
52
53
54
55 static public Constructor<?> newConstructor(String className)
56 throws IOException {
57 try {
58 Class<?> clazz = getClassByName(className);
59 Constructor<?> ctor = clazz.getDeclaredConstructor(EMPTY_ARRAY);
60 ctor.setAccessible(true);
61 return ctor;
62 } catch (ClassNotFoundException e) {
63 throw new IOException(e);
64 } catch (java.lang.NoSuchMethodException e) {
65 throw new IOException(e);
66 }
67 }
68
69
70
71
72
73
74 static private <T> T newInstance(Class<T> theClass) {
75 T result;
76 try {
77 Constructor<T> ctor = theClass.getDeclaredConstructor(EMPTY_ARRAY);
78 ctor.setAccessible(true);
79 result = ctor.newInstance();
80 } catch (Exception e) {
81 throw new RuntimeException(e);
82 }
83 return result;
84 }
85
86
87
88
89
90
91
92 static private Class<?> getClassByName(String name)
93 throws ClassNotFoundException {
94 ClassLoader classLoader = Thread.currentThread().getContextClassLoader();
95 return Class.forName(name, true, classLoader);
96 }
97 }