PageRenderTime 26ms CodeModel.GetById 11ms app.highlight 11ms RepoModel.GetById 1ms app.codeStats 0ms

/tags/release-0.2.0-rc0/hive/external/common/src/java/org/apache/hadoop/hive/common/metrics/MetricsMBeanImpl.java

#
Java | 155 lines | 115 code | 17 blank | 23 comment | 7 complexity | 53fa4807f7bd86f012e952d69ebd017b MD5 | raw file
  1/**
  2 * Licensed to the Apache Software Foundation (ASF) under one
  3 * or more contributor license agreements.  See the NOTICE file
  4 * distributed with this work for additional information
  5 * regarding copyright ownership.  The ASF licenses this file
  6 * to you under the Apache License, Version 2.0 (the
  7 * "License"); you may not use this file except in compliance
  8 * with the License.  You may obtain a copy of the License at
  9 *
 10 *     http://www.apache.org/licenses/LICENSE-2.0
 11 *
 12 * Unless required by applicable law or agreed to in writing, software
 13 * distributed under the License is distributed on an "AS IS" BASIS,
 14 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 15 * See the License for the specific language governing permissions and
 16 * limitations under the License.
 17 */
 18package org.apache.hadoop.hive.common.metrics;
 19
 20import java.io.IOException;
 21import java.util.HashMap;
 22import java.util.Map;
 23
 24import javax.management.Attribute;
 25import javax.management.AttributeList;
 26import javax.management.AttributeNotFoundException;
 27import javax.management.InvalidAttributeValueException;
 28import javax.management.MBeanAttributeInfo;
 29import javax.management.MBeanConstructorInfo;
 30import javax.management.MBeanException;
 31import javax.management.MBeanInfo;
 32import javax.management.MBeanNotificationInfo;
 33import javax.management.MBeanOperationInfo;
 34import javax.management.ReflectionException;
 35
 36
 37public class MetricsMBeanImpl implements  MetricsMBean {
 38
 39    Map<String,Object> metricsMap = new HashMap<String,Object>();
 40
 41    MBeanAttributeInfo[] attributeInfos;
 42    boolean dirtyAttributeInfoCache = true;
 43
 44    MBeanConstructorInfo[] ctors = null;
 45    MBeanOperationInfo[] ops = null;
 46    MBeanNotificationInfo[] notifs = null;
 47
 48    @Override
 49    public Object getAttribute(String arg0) throws AttributeNotFoundException,
 50            MBeanException, ReflectionException {
 51      synchronized(metricsMap) {
 52        if (metricsMap.containsKey(arg0)) {
 53          return metricsMap.get(arg0);
 54        } else {
 55          throw new AttributeNotFoundException("Key [" + arg0 + "] not found/tracked");
 56        }
 57      }
 58    }
 59
 60    @Override
 61    public AttributeList getAttributes(String[] arg0) {
 62      AttributeList results = new AttributeList();
 63      synchronized(metricsMap) {
 64        for (String key : arg0) {
 65          results.add(new Attribute(key,metricsMap.get(key)));
 66        }
 67      }
 68      return results;
 69    }
 70
 71    @Override
 72    public MBeanInfo getMBeanInfo() {
 73        if (dirtyAttributeInfoCache) {
 74          synchronized(metricsMap) {
 75            attributeInfos = new MBeanAttributeInfo[metricsMap.size()];
 76            int i = 0;
 77            for (String key : metricsMap.keySet()) {
 78              attributeInfos[i] = new MBeanAttributeInfo(
 79                  key, metricsMap.get(key).getClass().getName(), key, true, false, false);
 80              i++;
 81            }
 82            dirtyAttributeInfoCache = false;
 83          }
 84        }
 85        return new MBeanInfo(
 86            this.getClass().getName(), "metrics information",
 87            attributeInfos, ctors, ops, notifs);
 88    }
 89
 90    @Override
 91    public Object invoke(String arg0, Object[] arg1, String[] arg2)
 92            throws MBeanException, ReflectionException {
 93        // no invocations.
 94        return null;
 95    }
 96
 97    @Override
 98    public void setAttribute(Attribute attr) throws AttributeNotFoundException,
 99            InvalidAttributeValueException, MBeanException, ReflectionException {
100        try {
101            put(attr.getName(),attr.getValue());
102        } catch (Exception e) {
103            throw new MBeanException(e);
104        }
105    }
106
107    @Override
108    public AttributeList setAttributes(AttributeList arg0) {
109        AttributeList attributesSet = new AttributeList();
110        for (Attribute attr : arg0.asList()) {
111            try {
112                setAttribute(attr);
113                attributesSet.add(attr);
114            } catch (AttributeNotFoundException e) {
115                // ignore exception - we simply don't add this attribute 
116                // back in to the resultant set.
117            } catch (InvalidAttributeValueException e) {
118                // ditto
119            } catch (MBeanException e) {
120                // likewise
121            } catch (ReflectionException e) {
122                // and again, one last time.
123            }
124        }
125        return attributesSet;
126    }
127
128    public boolean hasKey(String name) {
129      synchronized(metricsMap) {
130        return metricsMap.containsKey(name);
131      }
132    }
133
134    public void put(String name, Object value) throws IOException {
135      synchronized(metricsMap) {
136        if (!metricsMap.containsKey(name)) {
137          dirtyAttributeInfoCache = true;
138        }
139        metricsMap.put(name, value);
140      }
141    }
142
143    public Object get(String name) throws IOException {
144        try {
145          return getAttribute(name);
146        } catch (AttributeNotFoundException e) {
147            throw new IOException(e);
148        } catch (MBeanException e) {
149            throw new IOException(e);
150        } catch (ReflectionException e) {
151            throw new IOException(e);
152        }
153    }
154
155}