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