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 */ 018package org.apache.hadoop.hbase; 019 020import java.util.HashMap; 021import java.util.Iterator; 022import java.util.Map; 023import java.util.ServiceLoader; 024import org.apache.yetus.audience.InterfaceAudience; 025import org.slf4j.Logger; 026import org.slf4j.LoggerFactory; 027 028/** 029 * Factory for classes supplied by hadoop compatibility modules. Only one of each class will be 030 * created. 031 */ 032@InterfaceAudience.Private 033public class CompatibilitySingletonFactory extends CompatibilityFactory { 034 035 @SuppressWarnings("ImmutableEnumChecker") 036 public static enum SingletonStorage { 037 INSTANCE; 038 039 private final Object lock = new Object(); 040 private final Map<Class, Object> instances = new HashMap<>(); 041 } 042 043 private static final Logger LOG = LoggerFactory.getLogger(CompatibilitySingletonFactory.class); 044 045 /** 046 * This is a static only class don't let anyone create an instance. 047 */ 048 protected CompatibilitySingletonFactory() { 049 } 050 051 /** 052 * Get the singleton instance of Any classes defined by compatibiliy jar's 053 * @return the singleton 054 */ 055 @SuppressWarnings("unchecked") 056 public static <T> T getInstance(Class<T> klass) { 057 synchronized (SingletonStorage.INSTANCE.lock) { 058 T instance = (T) SingletonStorage.INSTANCE.instances.get(klass); 059 if (instance == null) { 060 try { 061 ServiceLoader<T> loader = ServiceLoader.load(klass); 062 Iterator<T> it = loader.iterator(); 063 instance = it.next(); 064 if (it.hasNext()) { 065 StringBuilder msg = new StringBuilder(); 066 msg.append("ServiceLoader provided more than one implementation for class: ") 067 .append(klass).append(", using implementation: ").append(instance.getClass()) 068 .append(", other implementations: {"); 069 while (it.hasNext()) { 070 msg.append(it.next()).append(" "); 071 } 072 msg.append("}"); 073 LOG.warn(msg.toString()); 074 } 075 } catch (Exception e) { 076 throw new RuntimeException(createExceptionString(klass), e); 077 } catch (Error e) { 078 throw new RuntimeException(createExceptionString(klass), e); 079 } 080 081 // If there was nothing returned and no exception then throw an exception. 082 if (instance == null) { 083 throw new RuntimeException(createExceptionString(klass)); 084 } 085 SingletonStorage.INSTANCE.instances.put(klass, instance); 086 } 087 return instance; 088 } 089 090 } 091}