001/**
002 * Licensed to the Apache Software Foundation (ASF) under one
003 * or more contributor license agreements.  See the NOTICE file
004 * distributed with this work for additional information
005 * regarding copyright ownership.  The ASF licenses this file
006 * to you under the Apache License, Version 2.0 (the
007 * "License"); you may not use this file except in compliance
008 * with the License.  You may obtain a copy of the License at
009 *
010 *     http://www.apache.org/licenses/LICENSE-2.0
011 *
012 * Unless required by applicable law or agreed to in writing, software
013 * distributed under the License is distributed on an "AS IS" BASIS,
014 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
015 * See the License for the specific language governing permissions and
016 * limitations under the License.
017 */
018
019package org.apache.hadoop.hbase;
020
021import java.util.HashMap;
022import java.util.Iterator;
023import java.util.Map;
024import java.util.ServiceLoader;
025
026import org.apache.yetus.audience.InterfaceAudience;
027import org.slf4j.Logger;
028import org.slf4j.LoggerFactory;
029
030/**
031 *  Factory for classes supplied by hadoop compatibility modules.  Only one of each class will be
032 *  created.
033 */
034@InterfaceAudience.Private
035public class CompatibilitySingletonFactory extends CompatibilityFactory {
036  public static enum SingletonStorage {
037    INSTANCE;
038    private final Object lock = new Object();
039    private final Map<Class, Object> instances = new HashMap<>();
040  }
041  private static final Logger LOG = LoggerFactory.getLogger(CompatibilitySingletonFactory.class);
042
043  /**
044   * This is a static only class don't let anyone create an instance.
045   */
046  protected CompatibilitySingletonFactory() {  }
047
048  /**
049   * Get the singleton instance of Any classes defined by compatibiliy jar's
050   *
051   * @return the singleton
052   */
053  @SuppressWarnings("unchecked")
054  public static <T> T getInstance(Class<T> klass) {
055    synchronized (SingletonStorage.INSTANCE.lock) {
056      T instance = (T) SingletonStorage.INSTANCE.instances.get(klass);
057      if (instance == null) {
058        try {
059          ServiceLoader<T> loader = ServiceLoader.load(klass);
060          Iterator<T> it = loader.iterator();
061          instance = it.next();
062          if (it.hasNext()) {
063            StringBuilder msg = new StringBuilder();
064            msg.append("ServiceLoader provided more than one implementation for class: ")
065                .append(klass)
066                .append(", using implementation: ").append(instance.getClass())
067                .append(", other implementations: {");
068            while (it.hasNext()) {
069              msg.append(it.next()).append(" ");
070            }
071            msg.append("}");
072            LOG.warn(msg.toString());
073          }
074        } catch (Exception e) {
075          throw new RuntimeException(createExceptionString(klass), e);
076        } catch (Error e) {
077          throw new RuntimeException(createExceptionString(klass), e);
078        }
079
080        // If there was nothing returned and no exception then throw an exception.
081        if (instance == null) {
082          throw new RuntimeException(createExceptionString(klass));
083        }
084        SingletonStorage.INSTANCE.instances.put(klass, instance);
085      }
086      return instance;
087    }
088
089  }
090}