summaryrefslogtreecommitdiff
path: root/management/cli/src/org/apache/qpid/stac/commands/LsCommand.java
blob: 6d7006a3c94195383bb7b649f76346e51643eeef (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
/*
 *
 * Licensed to the Apache Software Foundation (ASF) under one
 * or more contributor license agreements.  See the NOTICE file
 * distributed with this work for additional information
 * regarding copyright ownership.  The ASF licenses this file
 * to you 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 org.apache.qpid.stac.commands;

import org.apache.qpid.AMQException;
import org.apache.qpid.stac.jmx.CurrentMBean;
import org.apache.qpid.stac.jmx.MBeanServerConnectionContext;
import org.apache.qpid.stac.jmx.MBeanUtils;

import javax.management.MBeanAttributeInfo;
import javax.management.MBeanOperationInfo;
import javax.management.MBeanParameterInfo;
import java.util.SortedSet;

public class LsCommand
{
    public static void execute(MBeanServerConnectionContext context)
            throws AMQException
    {
        CurrentMBean currentMBean = context.getCurrentMBean();

        SortedSet<MBeanAttributeInfo> directories = currentMBean.getOrderedObjects();
        System.out.println();
        for (MBeanAttributeInfo ai : directories)
        {
            if (!MBeanUtils.isHidden(ai))
            {
                outputAccess(ai);
                System.out.println(" " + ai.getName());
            }
        }
        System.out.println();

        SortedSet<MBeanAttributeInfo> attributes = currentMBean.getOrderedAttributes();
        for (MBeanAttributeInfo ai : attributes)
        {
            outputAccess(ai);
            System.out.printf(" %1$-15s%2$-15s %3$s\n", ai.getName(), "[" + convertType(ai.getType()) + "]",
                              currentMBean.getAttributeValue(ai.getName(), ai.getType()));
        }
        System.out.println();
        SortedSet<MBeanOperationInfo> operations = currentMBean.getOrderedOperations();

        for (MBeanOperationInfo oi : operations)
        {
            System.out.printf("-r-x %1$-15s", oi.getName());
            MBeanParameterInfo[] paramInfos = oi.getSignature();
            System.out.print("[");
            if (paramInfos.length == 0)
            {
                System.out.print("No arguments");
            }

            for (int i = 0; i < paramInfos.length; i++)
            {
                MBeanParameterInfo pi = paramInfos[i];
                System.out.printf("%1$s:%2$s%3$s", pi.getName(), convertType(pi.getType()),
                                  (i < paramInfos.length)?",":"");
            }
            System.out.println("]");
        }
        System.out.println();
    }

    private static void outputAccess(MBeanAttributeInfo ai)
    {
        boolean isObject = ai.getType().equals("javax.management.ObjectName");
        System.out.print(isObject?"d":"-");
        System.out.print(ai.isReadable()?"r":"-");
        System.out.print(ai.isWritable()?"w":"-");
        System.out.print("-");
    }

    /**
     * Converts the type name to a non-Java type (e.g. java.lang.String -> String)
     * @param javaType
     * @return a generic type
     */
    private static String convertType(String javaType)
    {
        if ("java.lang.String".equals(javaType))
        {
            return "String";
        }
        else if ("java.lang.Integer".equals(javaType))
        {
            return "Integer";
        }
        else if ("java.lang.Boolean".equals(javaType))
        {
            return "Boolean";
        }
        else if ("java.lang.Double".equals(javaType))
        {
            return "Double";
        }
        else if ("java.util.Date".equals(javaType))
        {
            return "Date";
        }
        else
        {
            return javaType;
        }
    }


}