001/**
002 *
003 * Licensed to the Apache Software Foundation (ASF) under one or more
004 * contributor license agreements.  See the NOTICE file distributed with
005 * this work for additional information regarding copyright ownership.
006 * The ASF licenses this file to You under the Apache License, Version 2.0
007 * (the "License"); you may not use this file except in compliance with
008 * 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.activemq.broker.jmx;
019
020import java.util.Collection;
021import java.util.HashMap;
022import java.util.Map;
023
024import javax.management.openmbean.CompositeData;
025import javax.management.openmbean.TabularData;
026
027/**
028 *
029 */
030public class CompositeDataHelper {
031
032    /**
033     * Extracts the named TabularData field from the CompositeData and converts it to a Map
034     * which is the method used to get the typesafe user properties.
035     */
036    public static Map getTabularMap(CompositeData cdata, String fieldName) {
037        Map map = new HashMap();
038        appendTabularMap(map, cdata, fieldName);
039        return map;
040    }
041
042    public static void appendTabularMap(Map map, CompositeData cdata, String fieldName) {
043        Object tabularObject = cdata.get(fieldName);
044        if (tabularObject instanceof TabularData) {
045            TabularData tabularData = (TabularData) tabularObject;
046            Collection<CompositeData> values = (Collection<CompositeData>) tabularData.values();
047            for (CompositeData compositeData : values) {
048                Object key = compositeData.get("key");
049                Object value = compositeData.get("value");
050                map.put(key, value);
051            }
052        }
053    }
054
055    /**
056     * Returns a map of all the user properties in the given message {@link javax.management.openmbean.CompositeData}
057     * object
058     *
059     * @param cdata
060     *
061     * @return a Map of user properties
062     */
063    public static Map getMessageUserProperties(CompositeData cdata) {
064        Map map = new HashMap();
065        appendTabularMap(map, cdata, CompositeDataConstants.STRING_PROPERTIES);
066        appendTabularMap(map, cdata, CompositeDataConstants.BOOLEAN_PROPERTIES);
067        appendTabularMap(map, cdata, CompositeDataConstants.BYTE_PROPERTIES);
068        appendTabularMap(map, cdata, CompositeDataConstants.SHORT_PROPERTIES);
069        appendTabularMap(map, cdata, CompositeDataConstants.INT_PROPERTIES);
070        appendTabularMap(map, cdata, CompositeDataConstants.LONG_PROPERTIES);
071        appendTabularMap(map, cdata, CompositeDataConstants.FLOAT_PROPERTIES);
072        appendTabularMap(map, cdata, CompositeDataConstants.DOUBLE_PROPERTIES);
073        return map;
074    }
075}