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 javax.management.openmbean.CompositeData;
021import javax.management.openmbean.TabularData;
022import java.util.Collection;
023import java.util.HashMap;
024import java.util.Map;
025
026/**
027 * 
028 */
029public class CompositeDataHelper {
030
031    /**
032     * Extracts the named TabularData field from the CompositeData and converts it to a Map
033     * which is the method used to get the typesafe user properties.
034     */
035    public static Map getTabularMap(CompositeData cdata, String fieldName) {
036        Map map = new HashMap();
037        appendTabularMap(map, cdata, fieldName);
038        return map;
039    }
040
041    public static void appendTabularMap(Map map, CompositeData cdata, String fieldName) {
042        Object tabularObject = cdata.get(fieldName);
043        if (tabularObject instanceof TabularData) {
044            TabularData tabularData = (TabularData) tabularObject;
045            Collection<CompositeData> values = (Collection<CompositeData>) tabularData.values();
046            for (CompositeData compositeData : values) {
047                Object key = compositeData.get("key");
048                Object value = compositeData.get("value");
049                map.put(key, value);
050            }
051        }
052    }
053
054    /**
055     * Returns a map of all the user properties in the given message {@link javax.management.openmbean.CompositeData}
056     * object
057     *
058     * @param cdata
059     * @return
060     */
061    public static Map getMessageUserProperties(CompositeData cdata) {
062        Map map = new HashMap();
063        appendTabularMap(map, cdata, CompositeDataConstants.STRING_PROPERTIES);
064        appendTabularMap(map, cdata, CompositeDataConstants.BOOLEAN_PROPERTIES);
065        appendTabularMap(map, cdata, CompositeDataConstants.BYTE_PROPERTIES);
066        appendTabularMap(map, cdata, CompositeDataConstants.SHORT_PROPERTIES);
067        appendTabularMap(map, cdata, CompositeDataConstants.INT_PROPERTIES);
068        appendTabularMap(map, cdata, CompositeDataConstants.LONG_PROPERTIES);
069        appendTabularMap(map, cdata, CompositeDataConstants.FLOAT_PROPERTIES);
070        appendTabularMap(map, cdata, CompositeDataConstants.DOUBLE_PROPERTIES);
071        return map;
072    }
073}