Java MBean convertToOpenMBeanOperation(MBeanOperationInfo in)

Here you can find the source of convertToOpenMBeanOperation(MBeanOperationInfo in)

Description

convert To Open M Bean Operation

License

Open Source License

Parameter

Parameter Description
in MBeanOperation to convert.

Return

An OpenMBeanOperation version of the passed MBeanOperation.

Declaration

public static OpenMBeanOperationInfo convertToOpenMBeanOperation(MBeanOperationInfo in) 

Method Source Code

//package com.java2s;
/* JmxUtils//from   w  w  w . java2s. c  om
 * 
 * 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.MBeanOperationInfo;
import javax.management.MBeanParameterInfo;

import javax.management.openmbean.InvalidOpenTypeException;

import javax.management.openmbean.OpenMBeanOperationInfo;
import javax.management.openmbean.OpenMBeanOperationInfoSupport;
import javax.management.openmbean.OpenMBeanParameterInfo;
import javax.management.openmbean.OpenMBeanParameterInfoSupport;
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 MBeanOperation to convert.
     * @return An OpenMBeanOperation version of the passed MBeanOperation.
     */
    public static OpenMBeanOperationInfo convertToOpenMBeanOperation(MBeanOperationInfo in) {
        return convertToOpenMBeanOperation(in, null, null);
    }

    /**
     * @param in MBeanOperation to convert.
     * @param prefix A prefix to add to the name of new operation.
     * @param returnType Return type to use in new operation.  Use this
     * parameter to override the passed <code>in</code> return type.
     * @return An OpenMBeanOperation version of the passed MBeanOperation.
     */
    public static OpenMBeanOperationInfo convertToOpenMBeanOperation(final MBeanOperationInfo in,
            final String prefix, final OpenType returnType) {
        MBeanParameterInfo[] params = in.getSignature();
        OpenMBeanParameterInfo[] signature = new OpenMBeanParameterInfo[params.length];
        for (int i = 0; i < params.length; i++) {
            signature[i] = convertToOpenMBeanOperationInfo(params[i]);
        }
        return new OpenMBeanOperationInfoSupport(((prefix != null) ? prefix + in.getName() : in.getName()),
                in.getDescription(), signature, (returnType != null) ? returnType : getOpenType(in.getReturnType()),
                convertImpact(in.getImpact()));
    }

    /**
     * @param in MBeanParameterInfo to convert to an OpenMBeanParameterInfo.
     * @return OpenMBeanParameterInfo version of <code>in</code>
     */
    public static OpenMBeanParameterInfo convertToOpenMBeanOperationInfo(MBeanParameterInfo in) {
        return new OpenMBeanParameterInfoSupport(in.getName(), in.getDescription(), getOpenType(in.getType()));
    }

    /**
     * @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 int convertImpact(final int impact) {
        if (impact != MBeanOperationInfo.ACTION && impact != MBeanOperationInfo.ACTION_INFO
                && impact != MBeanOperationInfo.INFO) {
            // Don't allow back MBeanOperationInfo.UNKNOWN impact.
            return MBeanOperationInfo.ACTION_INFO;
        }
        return impact;
    }

    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. convertToOpenMBeanAttribute(final MBeanAttributeInfo in, final String prefix)
  4. createMBeanServer()
  5. createOpenMBeanAttributeInfo(final OpenType type, final MBeanAttributeInfo in, final String prefix)
  6. createProxy(final ObjectName objectName, final Class mbeanInterface, final MBeanServer mbeanServer)
  7. createTimerMBean(ObjectName name, MBeanServer server)