001/** 002 * 003 * Licensed to the Apache Software Foundation (ASF) under one 004 * or more contributor license agreements. See the NOTICE file 005 * distributed with this work for additional information 006 * regarding copyright ownership. The ASF licenses this file 007 * to you under the Apache License, Version 2.0 (the 008 * "License"); you may not use this file except in compliance 009 * with the License. You may obtain a copy of the License at 010 * 011 * http://www.apache.org/licenses/LICENSE-2.0 012 * 013 * Unless required by applicable law or agreed to in writing, software 014 * distributed under the License is distributed on an "AS IS" BASIS, 015 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 016 * See the License for the specific language governing permissions and 017 * limitations under the License. 018 */ 019 020package org.apache.hadoop.hbase.metrics; 021 022import java.util.ArrayList; 023import java.util.List; 024import java.util.ServiceLoader; 025 026import org.apache.hadoop.hbase.util.ReflectionUtils; 027import org.apache.yetus.audience.InterfaceAudience; 028import org.slf4j.Logger; 029import org.slf4j.LoggerFactory; 030 031import org.apache.hbase.thirdparty.com.google.common.annotations.VisibleForTesting; 032 033@InterfaceAudience.Private 034public final class MetricRegistriesLoader { 035 private static final Logger LOG = LoggerFactory.getLogger(MetricRegistries.class); 036 037 private static final String defaultClass 038 = "org.apache.hadoop.hbase.metrics.impl.MetricRegistriesImpl"; 039 040 private MetricRegistriesLoader() { 041 } 042 043 /** 044 * Creates a {@link MetricRegistries} instance using the corresponding {@link MetricRegistries} 045 * available to {@link ServiceLoader} on the classpath. If no instance is found, then default 046 * implementation will be loaded. 047 * @return A {@link MetricRegistries} implementation. 048 */ 049 public static MetricRegistries load() { 050 List<MetricRegistries> availableImplementations = getDefinedImplemantations(); 051 return load(availableImplementations); 052 } 053 054 /** 055 * Creates a {@link MetricRegistries} instance using the corresponding {@link MetricRegistries} 056 * available to {@link ServiceLoader} on the classpath. If no instance is found, then default 057 * implementation will be loaded. 058 * @return A {@link MetricRegistries} implementation. 059 */ 060 @VisibleForTesting 061 static MetricRegistries load(List<MetricRegistries> availableImplementations) { 062 063 if (availableImplementations.size() == 1) { 064 // One and only one instance -- what we want/expect 065 MetricRegistries impl = availableImplementations.get(0); 066 LOG.info("Loaded MetricRegistries " + impl.getClass()); 067 return impl; 068 } else if (availableImplementations.isEmpty()) { 069 try { 070 return ReflectionUtils.newInstance((Class<MetricRegistries>)Class.forName(defaultClass)); 071 } catch (ClassNotFoundException e) { 072 throw new RuntimeException(e); 073 } 074 } else { 075 // Tell the user they're doing something wrong, and choose the first impl. 076 StringBuilder sb = new StringBuilder(); 077 for (MetricRegistries factory : availableImplementations) { 078 if (sb.length() > 0) { 079 sb.append(", "); 080 } 081 sb.append(factory.getClass()); 082 } 083 LOG.warn("Found multiple MetricRegistries implementations: " + sb 084 + ". Using first found implementation: " + availableImplementations.get(0)); 085 return availableImplementations.get(0); 086 } 087 } 088 089 private static List<MetricRegistries> getDefinedImplemantations() { 090 ServiceLoader<MetricRegistries> loader = ServiceLoader.load(MetricRegistries.class); 091 List<MetricRegistries> availableFactories = new ArrayList<>(); 092 for (MetricRegistries impl : loader) { 093 availableFactories.add(impl); 094 } 095 return availableFactories; 096 } 097}