Java MBean convertToOpenMBeanAttribute(final MBeanAttributeInfo in, final String prefix)

Here you can find the source of convertToOpenMBeanAttribute(final MBeanAttributeInfo in, final String prefix)

Description

convert To Open M Bean Attribute

License

Open Source License

Parameter

Parameter Description
in AttributeInfo to convert.
prefix Prefix to add to names of new attributes. If null, nothing is added.

Return

OpenMBeanAttributeInfo version of in.

Declaration

public static OpenMBeanAttributeInfo convertToOpenMBeanAttribute(final MBeanAttributeInfo in,
        final String prefix) 

Method Source Code

//package com.java2s;
/* JmxUtils/*from  w w w .j  a v a  2  s .c  o m*/
 * 
 * Created on May 18, 2005
 *
 * Copyright (C) 2005 Internet Archive.
 * 
 * This file is part of the Heritrix web crawler (crawler.archive.org).
 * 
 * Heritrix is free software; you can redistribute it and/or modify
 * it under the terms of the GNU Lesser Public License as published by
 * the Free Software Foundation; either version 2.1 of the License, or
 * any later version.
 * 
 * Heritrix is distributed in the hope that it will be useful, 
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU Lesser Public License for more details.
 * 
 * You should have received a copy of the GNU Lesser Public License
 * along with Heritrix; if not, write to the Free Software
 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
 */

import java.util.Arrays;

import java.util.List;

import javax.management.MBeanAttributeInfo;

import javax.management.openmbean.InvalidOpenTypeException;

import javax.management.openmbean.OpenMBeanAttributeInfo;
import javax.management.openmbean.OpenMBeanAttributeInfoSupport;

import javax.management.openmbean.OpenType;
import javax.management.openmbean.SimpleType;

public class Main {
    private static final List OPENTYPES = Arrays.asList(OpenType.ALLOWED_CLASSNAMES);

    /**
     * @param in AttributeInfo to convert.
     * @return OpenMBeanAttributeInfo version of <code>in</code>.
     */
    public static OpenMBeanAttributeInfo convertToOpenMBeanAttribute(MBeanAttributeInfo in) {
        return convertToOpenMBeanAttribute(in, null);
    }

    /**
     * @param in AttributeInfo to convert.
     * @param prefix Prefix to add to names of new attributes. If null, nothing
     * is added.
     * @return OpenMBeanAttributeInfo version of <code>in</code>.
     */
    public static OpenMBeanAttributeInfo convertToOpenMBeanAttribute(final MBeanAttributeInfo in,
            final String prefix) {
        return createOpenMBeanAttributeInfo(getOpenType(in.getType()), in, prefix);
    }

    /**
     * @param type Type of new OpenMBeanAttributeInfo.
     * @param in The MBeanAttributeInfo we're converting.
     * @param prefix Prefix to add to name of new Attribute (If null, nothing
     * is added).
     * @return New OpenMBeanAttributeInfo based on <code>in</code>.
     */
    public static OpenMBeanAttributeInfo createOpenMBeanAttributeInfo(final OpenType type,
            final MBeanAttributeInfo in, final String prefix) {
        return new OpenMBeanAttributeInfoSupport(((prefix != null) ? prefix + in.getName() : in.getName()),
                in.getDescription(), type, in.isReadable(), in.isWritable(), in.isIs());
    }

    /**
     * @param classString Java class name.
     * @return Its OpenType equivalent.
     */
    public static OpenType getOpenType(final String classString) {
        return getOpenType(classString, null);
    }

    /**
     * @param classString Java class name.
     * @param defaultType If no equivalent found, use passed defaultType.
     * @return Its OpenType equivalent.
     */
    public static OpenType getOpenType(String classString, final OpenType defaultType) {
        if (classString.equals("void")) {
            return SimpleType.VOID;
        }
        if (!isOpenType(classString)) {
            throw new InvalidOpenTypeException(classString);
        }
        if (classString.equals(String.class.getName())) {
            return SimpleType.STRING;
        } else if (classString.equals(Boolean.class.getName())) {
            return SimpleType.BOOLEAN;
        } else if (classString.equals(Long.class.getName())) {
            return SimpleType.LONG;
        } else if (classString.equals(Integer.class.getName())) {
            return SimpleType.INTEGER;
        } else if (classString.equals(Float.class.getName())) {
            return SimpleType.FLOAT;
        } else if (classString.equals(Double.class.getName())) {
            return SimpleType.DOUBLE;
        } else if (defaultType != null) {
            return defaultType;
        }
        throw new RuntimeException("Unsupported type: " + classString);
    }

    public static boolean isOpenType(final Class c) {
        return isOpenType(c.getName());
    }

    public static boolean isOpenType(final String classname) {
        return OPENTYPES.contains(classname);
    }
}

Related

  1. chooseMBean(Set mbeans)
  2. composeMBeanName(String objTypeName, String objInstanceName)
  3. convertToOpenMBeanOperation(MBeanOperationInfo in)
  4. createMBeanServer()
  5. createOpenMBeanAttributeInfo(final OpenType type, final MBeanAttributeInfo in, final String prefix)
  6. createProxy(final ObjectName objectName, final Class mbeanInterface, final MBeanServer mbeanServer)