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  /**
27   * Utility class that is used to generate a Checksum object.
28   * The Checksum implementation is pluggable and an application
29   * can specify their own class that implements their own
30   * Checksum algorithm.
31   */
32  public class ChecksumFactory {
33  
34    static private final Class<?>[] EMPTY_ARRAY = new Class[]{};
35  
36    /**
37     * Create a new instance of a Checksum object.
38     * @return The newly created Checksum object
39     */
40    static public Checksum newInstance(String className) throws IOException {
41      try {
42        Class<?> clazz = getClassByName(className);
43        return (Checksum)newInstance(clazz);
44      } catch (ClassNotFoundException e) {
45        throw new IOException(e);
46      }
47    }
48  
49    /**
50     * Returns a Constructor that can be used to create a Checksum object.
51     * @param className classname for which an constructor is created
52     * @return a new Constructor object
53     */
54    static public Constructor<?> newConstructor(String className) 
55      throws IOException {
56      try {
57        Class<?> clazz = getClassByName(className);
58        Constructor<?> ctor = clazz.getDeclaredConstructor(EMPTY_ARRAY);
59        ctor.setAccessible(true);
60        return ctor;
61      } catch (ClassNotFoundException e) {
62        throw new IOException(e);
63      } catch (java.lang.NoSuchMethodException e) {
64        throw new IOException(e);
65      }
66    }
67  
68    /** Create an object for the given class and initialize it from conf
69     *
70     * @param theClass class of which an object is created
71     * @return a new object
72     */
73    static private <T> T newInstance(Class<T> theClass) {
74      T result;
75      try {
76        Constructor<T> ctor = theClass.getDeclaredConstructor(EMPTY_ARRAY);
77        ctor.setAccessible(true);
78        result = ctor.newInstance();
79      } catch (Exception e) {
80        throw new RuntimeException(e);
81      }
82      return result;
83    }
84  
85    /**
86     * Load a class by name.
87     * @param name the class name.
88     * @return the class object.
89     * @throws ClassNotFoundException if the class is not found.
90     */
91    static private Class<?> getClassByName(String name) 
92      throws ClassNotFoundException {
93      ClassLoader classLoader = Thread.currentThread().getContextClassLoader();
94      return Class.forName(name, true, classLoader);
95    }
96  }