001package org.crsh.text.renderers;
002
003import org.crsh.text.Color;
004import org.crsh.text.Decoration;
005import org.crsh.text.LineRenderer;
006import org.crsh.text.Renderer;
007import org.crsh.text.ui.LabelElement;
008import org.crsh.text.ui.Overflow;
009import org.crsh.text.ui.RowElement;
010import org.crsh.text.ui.TableElement;
011import org.crsh.text.ui.TreeElement;
012
013import javax.management.Descriptor;
014import javax.management.MBeanAttributeInfo;
015import javax.management.MBeanInfo;
016import javax.management.MBeanOperationInfo;
017import javax.management.MBeanParameterInfo;
018import java.util.ArrayList;
019import java.util.Iterator;
020import java.util.List;
021
022/**
023 * @author Julien Viet
024 */
025public class MBeanInfoRenderer extends Renderer<MBeanInfo> {
026
027  @Override
028  public Class<MBeanInfo> getType() {
029    return MBeanInfo.class;
030  }
031
032  @Override
033  public LineRenderer renderer(Iterator<MBeanInfo> stream) {
034
035    List<LineRenderer> renderers = new ArrayList<LineRenderer>();
036
037    while (stream.hasNext()) {
038      MBeanInfo info = stream.next();
039
040      //
041      TreeElement root = new TreeElement(info.getClassName());
042
043      // Descriptor
044      TableElement descriptor = new TableElement().
045          overflow(Overflow.HIDDEN).
046          rightCellPadding(1);
047      Descriptor descriptorInfo = info.getDescriptor();
048      if (descriptorInfo != null) {
049        for (String fieldName : descriptorInfo.getFieldNames()) {
050          String fieldValue = String.valueOf(descriptorInfo.getFieldValue(fieldName));
051          descriptor.row(fieldName, fieldValue);
052        }
053      }
054
055      // Attributes
056      TableElement attributes = new TableElement().
057          overflow(Overflow.HIDDEN).
058          rightCellPadding(1).
059          add(new RowElement().style(Decoration.bold.fg(Color.black).bg(Color.white)).add("NAME", "TYPE", "DESCRIPTION"));
060      for (MBeanAttributeInfo attributeInfo : info.getAttributes()) {
061        attributes.row(attributeInfo.getName(), attributeInfo.getType(), attributeInfo.getDescription());
062      }
063
064      // Operations
065      TreeElement operations = new TreeElement("Operations");
066      for (MBeanOperationInfo operationInfo : info.getOperations()) {
067        TableElement signature = new TableElement().
068            overflow(Overflow.HIDDEN).
069            rightCellPadding(1);
070        MBeanParameterInfo[] parameterInfos = operationInfo.getSignature();
071        for (MBeanParameterInfo parameterInfo : parameterInfos) {
072          signature.row(parameterInfo.getName(), parameterInfo.getType(), parameterInfo.getDescription());
073        }
074        TreeElement operation = new TreeElement(operationInfo.getName());
075        String impact;
076        switch (operationInfo.getImpact()) {
077          case MBeanOperationInfo.ACTION:
078            impact = "ACTION";
079            break;
080          case MBeanOperationInfo.INFO:
081            impact = "INFO";
082            break;
083          case MBeanOperationInfo.ACTION_INFO:
084            impact = "ACTION_INFO";
085            break;
086          default:
087            impact = "UNKNOWN";
088        }
089        operation.addChild(new TableElement().
090            add(
091                new RowElement().add("Type: ", operationInfo.getReturnType()),
092                new RowElement().add("Description: ", operationInfo.getDescription()),
093                new RowElement().add("Impact: ", impact),
094                new RowElement().add(new LabelElement("Signature: "), signature)
095            )
096        );
097
098        operations.addChild(operation);
099      }
100
101      //
102      root.addChild(
103        new TableElement().leftCellPadding(1).overflow(Overflow.HIDDEN).
104          row("ClassName", info.getClassName()).
105          row("Description", info.getDescription()
106        )
107      );
108      root.addChild(new TreeElement("Descriptor").addChild(descriptor));
109      root.addChild(new TreeElement("Attributes").addChild(attributes));
110      root.addChild(operations);
111
112      //
113      renderers.add(root.renderer());
114    }
115
116
117
118
119    return LineRenderer.vertical(renderers);
120  }
121}