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.metrics2.lib;
019
020import org.apache.commons.logging.Log;
021import org.apache.commons.logging.LogFactory;
022
023import java.lang.reflect.Method;
024
025public class DefaultMetricsSystemHelper {
026
027  private static final Log LOG = LogFactory.getLog(DefaultMetricsSystemHelper.class);
028  private final Method removeObjectMethod;
029
030  public DefaultMetricsSystemHelper() {
031    Method m;
032    try {
033      Class<? extends DefaultMetricsSystem> clazz = DefaultMetricsSystem.INSTANCE.getClass();
034      m = clazz.getDeclaredMethod("removeObjectName", String.class);
035      m.setAccessible(true);
036    } catch (NoSuchMethodException e) {
037      m = null;
038    }
039    removeObjectMethod = m;
040  }
041
042  public boolean removeObjectName(final String name) {
043    if (removeObjectMethod != null) {
044      try {
045        removeObjectMethod.invoke(DefaultMetricsSystem.INSTANCE, name);
046        return true;
047      } catch (Exception e) {
048        if (LOG.isTraceEnabled()) {
049          LOG.trace("Unable to remove object name from cache: " + name, e);
050        }
051      }
052    }
053    return false;
054  }
055}