MBean to Printable Type - Java javax.management

Java examples for javax.management:MBean

Description

MBean to Printable Type

Demo Code

/**//  w  ww  .jav  a 2 s  .  c  o m
 * Copyright 2014 Alexey Ragozin
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
//package com.java2s;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.management.MBeanAttributeInfo;

import javax.management.MBeanInfo;

import javax.management.MBeanServerConnection;
import javax.management.ObjectName;

import javax.management.openmbean.CompositeData;
import javax.management.openmbean.TabularData;

public class Main {
    public static void main(String[] argv) throws Exception {
        String type = "java2s.com";
        System.out.println(toPrintableType(type));
    }

    private MBeanServerConnection mserver;
    static Map<String, String> TYPE_MAP = new HashMap<String, String>();

    static String toPrintableType(String type) {
        if (TYPE_MAP.containsKey(type)) {
            return TYPE_MAP.get(type);
        } else if (type.startsWith("[L")) {
            return toPrintableType(type.substring(2, type.length() - 1))
                    + "[]";
        } else {
            return type;
        }
    }

    public String get(ObjectName bean, String attr) throws Exception {
        MBeanInfo mbinfo = mserver.getMBeanInfo(bean);
        MBeanAttributeInfo ai = attrInfo(mbinfo, attr);
        if (ai == null) {
            throw new IllegalArgumentException("No such attribute '" + attr
                    + "'");
        }
        if (!ai.isReadable()) {
            throw new IllegalArgumentException("Attribute '" + attr
                    + "' is write-only");
        }
        Object v = mserver.getAttribute(bean, attr);
        String type = ai.getType();
        String text = format(v, type);
        return text;
    }

    private MBeanAttributeInfo attrInfo(MBeanInfo mbinfo, String attr) {
        for (MBeanAttributeInfo ai : mbinfo.getAttributes()) {
            if (ai.getName().equals(attr)) {
                return ai;
            }
        }
        return null;
    }

    private String format(Object v, String type) {
        if (type.equals("void")) {
            return null;
        } else if (v instanceof CompositeData[]) {
            CompositeData[] td = (CompositeData[]) v;
            if (td.length == 0) {
                return "";
            }
            List<String> header = new ArrayList<String>();
            for (String f : td[0].getCompositeType().keySet()) {
                if (!header.contains(f)) {
                    header.add(f);
                }
            }
            List<String[]> content = new ArrayList<String[]>();
            content.add(header.toArray(new String[0]));
            for (Object row : td) {
                content.add(formatRow((CompositeData) row, header));
            }
            return formatTable(content, 40, true);
        } else if (v instanceof TabularData) {
            TabularData td = (TabularData) v;
            td.getTabularType().getIndexNames();
            List<String> header = new ArrayList<String>(td.getTabularType()
                    .getIndexNames());
            for (String f : td.getTabularType().getRowType().keySet()) {
                if (!header.contains(f)) {
                    header.add(f);
                }
            }
            List<String[]> content = new ArrayList<String[]>();
            content.add(header.toArray(new String[0]));
            for (Object row : td.values()) {
                content.add(formatRow((CompositeData) row, header));
            }
            return formatTable(content, 40, true);
        } else if (v instanceof CompositeData) {
            CompositeData cd = (CompositeData) v;
            List<String[]> content = new ArrayList<String[]>();
            for (String field : cd.getCompositeType().keySet()) {
                String val = formatLine(cd.get(field), cd
                        .getCompositeType().getType(field).getClassName());
                content.add(new String[] { field + ": ", val });
            }
            return formatTable(content, 1000, false);
        } else {
            return formatLine(v, type);
        }
    }

    private String[] formatRow(CompositeData row, List<String> header) {
        String[] text = new String[header.size()];
        for (int i = 0; i != text.length; ++i) {
            String attr = header.get(i);
            text[i] = formatLine(row.get(attr), row.getCompositeType()
                    .getType(attr).getClassName());
        }
        return text;
    }

    private String formatTable(List<String[]> content, int maxCell,
            boolean table) {
        int[] width = new int[content.get(0).length];
        for (String[] row : content) {
            for (int i = 0; i != row.length; ++i) {
                width[i] = Math.min(Math.max(width[i], row[i].length()),
                        maxCell);
            }
        }

        StringBuilder sb = new StringBuilder();
        boolean header = table;
        for (String[] row : content) {
            for (int i = 0; i != width.length; ++i) {
                String cell = row[i];
                if (cell.length() > width[i]) {
                    cell = cell.substring(0, width[i] - 3) + "...";
                }
                sb.append(cell);
                for (int s = 0; s != width[i] - cell.length(); ++s) {
                    sb.append(' ');
                }
                if (table) {
                    sb.append('|');
                }
            }
            if (table) {
                sb.setLength(sb.length() - 1);
            }
            sb.append('\n');
            if (header) {
                header = false;
                for (int n : width) {
                    for (int i = 0; i != n; ++i) {
                        sb.append('-');
                    }
                    sb.append('+');
                }
                sb.setLength(sb.length() - 1);
                sb.append('\n');
            }
        }

        return sb.toString();
    }

    private String formatLine(Object v, String type) {
        if (v instanceof TabularData) {
            TabularData td = (TabularData) v;
            StringBuilder sb = new StringBuilder();
            for (Object c : td.values()) {
                sb.append(formatLine(c, td.getTabularType().getRowType()
                        .getClassName()));
                sb.append(",");
            }
            if (sb.length() > 0) {
                sb.setLength(sb.length() - 1);
            }
            return sb.toString();
        }
        if (v instanceof CompositeData[]) {
            CompositeData[] td = (CompositeData[]) v;
            StringBuilder sb = new StringBuilder();
            for (Object c : td) {
                sb.append(formatLine(c, ((CompositeData) c)
                        .getCompositeType().getClassName()));
                sb.append(",");
            }
            if (sb.length() > 0) {
                sb.setLength(sb.length() - 1);
            }
            return sb.toString();
        } else if (v instanceof CompositeData) {
            CompositeData cdata = (CompositeData) v;
            StringBuilder sb = new StringBuilder();
            sb.append("{");
            for (String attr : cdata.getCompositeType().keySet()) {
                sb.append(attr).append("=");
                sb.append(formatLine(cdata.get(attr), cdata
                        .getCompositeType().getType(attr).getClassName()));
                sb.append(',');
            }
            if (sb.length() > 1) {
                sb.setLength(sb.length() - 1);
            }
            sb.append("}");
            return sb.toString();
        } else if (v instanceof Object[]) {
            return Arrays.toString((Object[]) v);
        } else if (v instanceof boolean[]) {
            return Arrays.toString((boolean[]) v);
        } else if (v instanceof byte[]) {
            return Arrays.toString((byte[]) v);
        } else if (v instanceof char[]) {
            return Arrays.toString((char[]) v);
        } else if (v instanceof short[]) {
            return Arrays.toString((short[]) v);
        } else if (v instanceof int[]) {
            return Arrays.toString((int[]) v);
        } else if (v instanceof long[]) {
            return Arrays.toString((long[]) v);
        } else if (v instanceof float[]) {
            return Arrays.toString((float[]) v);
        } else if (v instanceof double[]) {
            return Arrays.toString((double[]) v);
        } else {
            return String.valueOf(v);
        }
    }
}

Related Tutorials