View Javadoc

1   /**
2    * Licensed to the Apache Software Foundation (ASF) under one
3    * or more contributor license agreements.  See the NOTICE file
4    * distributed with this work for additional information
5    * regarding copyright ownership.  The ASF licenses this file
6    * to you under the Apache License, Version 2.0 (the
7    * "License"); you may not use this file except in compliance
8    * with the License.  You may obtain a copy of the License at
9    *
10   *     http://www.apache.org/licenses/LICENSE-2.0
11   *
12   * Unless required by applicable law or agreed to in writing, software
13   * distributed under the License is distributed on an "AS IS" BASIS,
14   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15   * See the License for the specific language governing permissions and
16   * limitations under the License.
17   */
18  
19  package org.apache.hadoop.hbase;
20  
21  import org.apache.commons.logging.Log;
22  import org.apache.commons.logging.LogFactory;
23  
24  import java.util.HashMap;
25  import java.util.Iterator;
26  import java.util.Map;
27  import java.util.ServiceLoader;
28  
29  /**
30   *  Factory for classes supplied by hadoop compatibility modules.  Only one of each class will be
31   *  created.
32   */
33  public class CompatibilitySingletonFactory extends CompatibilityFactory {
34    private static final Log LOG = LogFactory.getLog(CompatibilitySingletonFactory.class);
35    private static final Map<Class, Object> instances = new HashMap<Class, Object>();
36  
37    /**
38     * This is a static only class don't let anyone create an instance.
39     */
40    protected CompatibilitySingletonFactory() {  }
41  
42    /**
43     * Get the singleton instance of Any classes defined by compatibiliy jar's
44     *
45     * @return the singleton
46     */
47    @SuppressWarnings("unchecked")
48    public static synchronized <T> T getInstance(Class<T> klass) {
49      T instance = (T) instances.get(klass);
50      if (instance == null) {
51        try {
52          ServiceLoader<T> loader = ServiceLoader.load(klass);
53          Iterator<T> it = loader.iterator();
54          instance = it.next();
55          if (it.hasNext()) {
56            StringBuilder msg = new StringBuilder();
57            msg.append("ServiceLoader provided more than one implementation for class: ")
58                    .append(klass)
59                    .append(", using implementation: ").append(instance.getClass())
60                    .append(", other implementations: {");
61            while (it.hasNext()) {
62              msg.append(it.next()).append(" ");
63            }
64            msg.append("}");
65            LOG.warn(msg);
66          }
67        } catch (Exception e) {
68          throw new RuntimeException(createExceptionString(klass), e);
69        } catch (Error e) {
70          throw new RuntimeException(createExceptionString(klass), e);
71        }
72  
73        // If there was nothing returned and no exception then throw an exception.
74        if (instance == null) {
75          throw new RuntimeException(createExceptionString(klass));
76        }
77        instances.put(klass, instance);
78      }
79      return instance;
80    }
81  }