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  /**
28   * Utility class that is used to generate a Checksum object.
29   * The Checksum implementation is pluggable and an application
30   * can specify their own class that implements their own
31   * Checksum algorithm.
32   */
33  public class ChecksumFactory {
34  
35    static private final Class<?>[] EMPTY_ARRAY = new Class[]{};
36  
37    /**
38     * Create a new instance of a Checksum object.
39     * @return The newly created Checksum object
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     * Returns a Constructor that can be used to create a Checksum object.
52     * @param className classname for which an constructor is created
53     * @return a new Constructor object
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    /** Create an object for the given class and initialize it from conf
70     *
71     * @param theClass class of which an object is created
72     * @return a new object
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     * Load a class by name.
88     * @param name the class name.
89     * @return the class object.
90     * @throws ClassNotFoundException if the class is not found.
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  }