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.metrics; 019 020import java.util.ArrayList; 021import java.util.List; 022import java.util.ServiceLoader; 023import org.apache.hadoop.hbase.util.ReflectionUtils; 024import org.apache.yetus.audience.InterfaceAudience; 025import org.slf4j.Logger; 026import org.slf4j.LoggerFactory; 027 028@InterfaceAudience.Private 029public final class MetricRegistriesLoader { 030 private static final Logger LOG = LoggerFactory.getLogger(MetricRegistries.class); 031 032 private static final String defaultClass = 033 "org.apache.hadoop.hbase.metrics.impl.MetricRegistriesImpl"; 034 035 private MetricRegistriesLoader() { 036 } 037 038 /** 039 * Creates a {@link MetricRegistries} instance using the corresponding {@link MetricRegistries} 040 * available to {@link ServiceLoader} on the classpath. If no instance is found, then default 041 * implementation will be loaded. 042 * @return A {@link MetricRegistries} implementation. 043 */ 044 public static MetricRegistries load() { 045 List<MetricRegistries> availableImplementations = getDefinedImplemantations(); 046 return load(availableImplementations); 047 } 048 049 /** 050 * Creates a {@link MetricRegistries} instance using the corresponding {@link MetricRegistries} 051 * available to {@link ServiceLoader} on the classpath. If no instance is found, then default 052 * implementation will be loaded. 053 * @return A {@link MetricRegistries} implementation. 054 */ 055 static MetricRegistries load(List<MetricRegistries> availableImplementations) { 056 057 if (availableImplementations.size() == 1) { 058 // One and only one instance -- what we want/expect 059 MetricRegistries impl = availableImplementations.get(0); 060 LOG.info("Loaded MetricRegistries " + impl.getClass()); 061 return impl; 062 } else if (availableImplementations.isEmpty()) { 063 try { 064 return ReflectionUtils.newInstance((Class<MetricRegistries>) Class.forName(defaultClass)); 065 } catch (ClassNotFoundException e) { 066 throw new RuntimeException(e); 067 } 068 } else { 069 // Tell the user they're doing something wrong, and choose the first impl. 070 StringBuilder sb = new StringBuilder(); 071 for (MetricRegistries factory : availableImplementations) { 072 if (sb.length() > 0) { 073 sb.append(", "); 074 } 075 sb.append(factory.getClass()); 076 } 077 LOG.warn("Found multiple MetricRegistries implementations: " + sb 078 + ". Using first found implementation: " + availableImplementations.get(0)); 079 return availableImplementations.get(0); 080 } 081 } 082 083 private static List<MetricRegistries> getDefinedImplemantations() { 084 ServiceLoader<MetricRegistries> loader = ServiceLoader.load(MetricRegistries.class); 085 List<MetricRegistries> availableFactories = new ArrayList<>(); 086 for (MetricRegistries impl : loader) { 087 availableFactories.add(impl); 088 } 089 return availableFactories; 090 } 091}