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.Iterator;
022import java.util.ServiceLoader;
023
024import org.apache.yetus.audience.InterfaceAudience;
025import org.slf4j.Logger;
026import org.slf4j.LoggerFactory;
027
028/**
029 * Class that will create many instances of classes provided by the hbase-hadoop{1|2}-compat jars.
030 */
031@InterfaceAudience.Private
032public class CompatibilityFactory {
033
034  private static final Logger LOG = LoggerFactory.getLogger(CompatibilitySingletonFactory.class);
035  public static final String EXCEPTION_START = "Could not create  ";
036  public static final String EXCEPTION_END = " Is the hadoop compatibility jar on the classpath?";
037
038  /**
039   * This is a static only class don't let any instance be created.
040   */
041  protected CompatibilityFactory() {}
042
043  public static synchronized <T> T getInstance(Class<T> klass) {
044    T instance = null;
045    try {
046      ServiceLoader<T> loader = ServiceLoader.load(klass);
047      Iterator<T> it = loader.iterator();
048      instance = it.next();
049      if (it.hasNext()) {
050        StringBuilder msg = new StringBuilder();
051        msg.append("ServiceLoader provided more than one implementation for class: ")
052           .append(klass)
053           .append(", using implementation: ").append(instance.getClass())
054           .append(", other implementations: {");
055        while (it.hasNext()) {
056          msg.append(it.next()).append(" ");
057        }
058        msg.append("}");
059        LOG.warn(msg.toString());
060      }
061    } catch (Exception e) {
062      throw new RuntimeException(createExceptionString(klass), e);
063    } catch (Error e) {
064      throw new RuntimeException(createExceptionString(klass), e);
065    }
066
067    // If there was nothing returned and no exception then throw an exception.
068    if (instance == null) {
069      throw new RuntimeException(createExceptionString(klass));
070    }
071    return instance;
072  }
073
074  protected static String createExceptionString(Class klass) {
075    return EXCEPTION_START + klass.toString() + EXCEPTION_END;
076  }
077}