View Javadoc

1   /**
2    * Copyright The Apache Software Foundation
3    *
4    * Licensed to the Apache Software Foundation (ASF) under one
5    * or more contributor license agreements.  See the NOTICE file
6    * distributed with this work for additional information
7    * regarding copyright ownership.  The ASF licenses this file
8    * to you under the Apache License, Version 2.0 (the
9    * "License"); you may not use this file except in compliance
10   * with the License.  You may obtain a copy of the License at
11   *
12   *     http://www.apache.org/licenses/LICENSE-2.0
13   *
14   * Unless required by applicable law or agreed to in writing, software
15   * distributed under the License is distributed on an "AS IS" BASIS,
16   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
17   * See the License for the specific language governing permissions and
18   * limitations under the License.
19   */
20  package org.apache.hadoop.hbase.util;
21  
22  import java.io.IOException;
23  import java.lang.reflect.Constructor;
24  import java.util.zip.Checksum;
25  
26  import org.apache.hadoop.hbase.classification.InterfaceAudience;
27  
28  /**
29   * Utility class that is used to generate a Checksum object.
30   * The Checksum implementation is pluggable and an application
31   * can specify their own class that implements their own
32   * Checksum algorithm.
33   */
34  @InterfaceAudience.Private
35  public class ChecksumFactory {
36  
37    static private final Class<?>[] EMPTY_ARRAY = new Class[]{};
38  
39    /**
40     * Create a new instance of a Checksum object.
41     * @return The newly created Checksum object
42     */
43    static public Checksum newInstance(String className) throws IOException {
44      try {
45        Class<?> clazz = getClassByName(className);
46        return (Checksum)newInstance(clazz);
47      } catch (ClassNotFoundException e) {
48        throw new IOException(e);
49      }
50    }
51  
52    /**
53     * Returns a Constructor that can be used to create a Checksum object.
54     * @param className classname for which an constructor is created
55     * @return a new Constructor object
56     */
57    static public Constructor<?> newConstructor(String className)
58      throws IOException {
59      try {
60        Class<?> clazz = getClassByName(className);
61        Constructor<?> ctor = clazz.getDeclaredConstructor(EMPTY_ARRAY);
62        ctor.setAccessible(true);
63        return ctor;
64      } catch (ClassNotFoundException e) {
65        throw new IOException(e);
66      } catch (java.lang.NoSuchMethodException e) {
67        throw new IOException(e);
68      }
69    }
70  
71    /** Create an object for the given class and initialize it from conf
72     *
73     * @param theClass class of which an object is created
74     * @return a new object
75     */
76    static private <T> T newInstance(Class<T> theClass) {
77      T result;
78      try {
79        Constructor<T> ctor = theClass.getDeclaredConstructor(EMPTY_ARRAY);
80        ctor.setAccessible(true);
81        result = ctor.newInstance();
82      } catch (Exception e) {
83        throw new RuntimeException(e);
84      }
85      return result;
86    }
87  
88    /**
89     * Load a class by name.
90     * @param name the class name.
91     * @return the class object.
92     * @throws ClassNotFoundException if the class is not found.
93     */
94    static private Class<?> getClassByName(String name)
95      throws ClassNotFoundException {
96      ClassLoader classLoader = Thread.currentThread().getContextClassLoader();
97      return Class.forName(name, true, classLoader);
98    }
99  }