001/*
002 * Copyright (C) 2012 eXo Platform SAS.
003 *
004 * This is free software; you can redistribute it and/or modify it
005 * under the terms of the GNU Lesser General Public License as
006 * published by the Free Software Foundation; either version 2.1 of
007 * the License, or (at your option) any later version.
008 *
009 * This software is distributed in the hope that it will be useful,
010 * but WITHOUT ANY WARRANTY; without even the implied warranty of
011 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
012 * Lesser General Public License for more details.
013 *
014 * You should have received a copy of the GNU Lesser General Public
015 * License along with this software; if not, write to the Free
016 * Software Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA
017 * 02110-1301 USA, or see the FSF site: http://www.fsf.org.
018 */
019package org.crsh.text.renderers;
020
021import org.crsh.text.Color;
022import org.crsh.text.Decoration;
023import org.crsh.text.LineRenderer;
024import org.crsh.text.Renderer;
025import org.crsh.text.ui.LabelElement;
026import org.crsh.text.ui.Overflow;
027import org.crsh.text.ui.RowElement;
028import org.crsh.text.ui.TableElement;
029import org.crsh.util.Utils;
030
031import javax.management.InstanceNotFoundException;
032import javax.management.IntrospectionException;
033import javax.management.JMException;
034import javax.management.MBeanInfo;
035import javax.management.MBeanServer;
036import javax.management.ObjectName;
037import javax.management.ReflectionException;
038import java.lang.management.ManagementFactory;
039import java.util.Collections;
040import java.util.Iterator;
041import java.util.List;
042
043/**
044 * @author Julien Viet
045 */
046public class ObjectNameRenderer extends Renderer<ObjectName> {
047
048  @Override
049  public Class<ObjectName> getType() {
050    return ObjectName.class;
051  }
052
053  @Override
054  public LineRenderer renderer(Iterator<ObjectName> stream) {
055
056    MBeanServer server = ManagementFactory.getPlatformMBeanServer();
057
058    List<ObjectName> names = Utils.list(stream);
059    Collections.sort(names);
060
061    //
062    TableElement table = new TableElement().overflow(Overflow.HIDDEN).rightCellPadding(1);
063
064    // Header
065    table.add(
066      new RowElement().
067        style(Decoration.bold.fg(Color.black).bg(Color.white)).
068        add("NAME", "CLASSNAME", "MXBEAN", "DESCRIPTION")
069    );
070
071    //
072    for (ObjectName name : names) {
073
074      String className;
075      String description;
076      String mxbean;
077      try {
078        MBeanInfo info = server.getMBeanInfo(name);
079        className = info.getClassName();
080        description = info.getDescription();
081        Object mxbeanValue = info.getDescriptor().getFieldValue("mxbean");
082        mxbean = mxbeanValue != null ? mxbeanValue.toString() : "false";
083      }
084      catch (JMException ignore) {
085        className = "";
086        description = "";
087        mxbean = "";
088      }
089
090      //
091      table.row("" + name, className, mxbean, description);
092    }
093
094    //
095    return table.renderer();
096  }
097}