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     * @return The Constructor that can be used to create a 
53     * new Checksum object.
54     * @param theClass 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  }