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.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   * Utility class that is used to generate a Checksum object.
31   * The Checksum implementation is pluggable and an application
32   * can specify their own class that implements their own
33   * Checksum algorithm.
34   */
35  @InterfaceAudience.Private
36  public class ChecksumFactory {
37  
38    static private final Class<?>[] EMPTY_ARRAY = new Class[]{};
39  
40    /**
41     * Create a new instance of a Checksum object.
42     * @return The newly created Checksum object
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     * Returns a Constructor that can be used to create a Checksum object.
55     * @param className classname for which an constructor is created
56     * @return a new Constructor object
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    /** Create an object for the given class and initialize it from conf
73     *
74     * @param theClass class of which an object is created
75     * @return a new object
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     * Load a class by name.
91     * @param name the class name.
92     * @return the class object.
93     * @throws ClassNotFoundException if the class is not found.
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 }