1 package org.cyclopsgroup.jmxterm.cmd;
2
3 import org.apache.commons.collections4.map.ListOrderedMap;
4 import org.apache.commons.lang3.Validate;
5 import org.cyclopsgroup.jcli.annotation.Argument;
6 import org.cyclopsgroup.jcli.annotation.Cli;
7 import org.cyclopsgroup.jcli.annotation.MultiValue;
8 import org.cyclopsgroup.jcli.annotation.Option;
9 import org.cyclopsgroup.jmxterm.Command;
10 import org.cyclopsgroup.jmxterm.Session;
11 import org.cyclopsgroup.jmxterm.io.ValueOutputFormat;
12
13 import javax.management.JMException;
14 import javax.management.MBeanAttributeInfo;
15 import javax.management.MBeanServerConnection;
16 import javax.management.ObjectName;
17 import java.io.IOException;
18 import java.util.ArrayList;
19 import java.util.HashMap;
20 import java.util.List;
21 import java.util.Map;
22
23
24
25
26
27
28 @Cli(name = "get", description = "Get value of MBean attribute(s)",
29 note = "* stands for all attributes. eg. get Attribute1 Attribute2 or get *")
30 public class GetCommand extends Command {
31 private List<String> attributes = new ArrayList<String>();
32
33 private String bean;
34
35 private String domain;
36
37 private boolean singleLine = false;
38
39 private String delimiter = "";
40
41 private boolean showDescription;
42
43 private boolean showQuotationMarks;
44
45 private boolean simpleFormat;
46
47 private void displayAttributes() throws IOException, JMException {
48 Session session = getSession();
49 String beanName = BeanCommand.getBeanName(bean, domain, session);
50 ObjectName name = new ObjectName(beanName);
51 session.output.printMessage("mbean = " + beanName + ":");
52 MBeanServerConnection con = session.getConnection().getServerConnection();
53 MBeanAttributeInfo[] ais = con.getMBeanInfo(name).getAttributes();
54 Map<String, MBeanAttributeInfo> attributeNames =
55 ListOrderedMap.listOrderedMap(new HashMap<String, MBeanAttributeInfo>());
56 if (attributes.contains("*")) {
57 for (MBeanAttributeInfo ai : ais) {
58 attributeNames.put(ai.getName(), ai);
59 }
60 } else {
61 for (String arg : attributes) {
62 String[] attributeNameElements = arg.split("\\.");
63
64 String firstPath = attributeNameElements[0];
65
66 for (MBeanAttributeInfo ai : ais) {
67 if (ai.getName().equals(firstPath)) {
68 attributeNames.put(arg, ai);
69 break;
70 }
71 }
72 }
73 }
74 ValueOutputFormat format = new ValueOutputFormat(2, showDescription, showQuotationMarks);
75 for (Map.Entry<String, MBeanAttributeInfo> entry : attributeNames.entrySet()) {
76 String attributeName = entry.getKey();
77 MBeanAttributeInfo i = entry.getValue();
78 if (i.isReadable()) {
79 String[] attributeNameElements = attributeName.split("\\.");
80
81 String attributeNameToRequest = attributeName;
82 if (attributeNameElements.length > 1) {
83 attributeNameToRequest = attributeNameElements[0];
84 }
85
86 Object result = con.getAttribute(name, attributeNameToRequest);
87
88 if (result instanceof javax.management.openmbean.CompositeDataSupport) {
89 if (attributeNameElements.length > 1) {
90 result = ((javax.management.openmbean.CompositeDataSupport) result)
91 .get(attributeNameElements[1]);
92 }
93 }
94
95 if (simpleFormat) {
96 format.printValue(session.output, result);
97 } else {
98 format.printExpression(session.output, attributeName, result, i.getDescription());
99 }
100 session.output.print(delimiter);
101 if (!singleLine) {
102 session.output.println("");
103 }
104 } else {
105 session.output.printMessage(i.getName() + " is not readable");
106 }
107 }
108 }
109
110 @Override
111 public List<String> doSuggestArgument() throws IOException, JMException {
112 if (getSession().getBean() != null) {
113 MBeanServerConnection con = getSession().getConnection().getServerConnection();
114 MBeanAttributeInfo[] ais =
115 con.getMBeanInfo(new ObjectName(getSession().getBean())).getAttributes();
116 List<String> results = new ArrayList<String>(ais.length);
117 for (MBeanAttributeInfo ai : ais) {
118 results.add(ai.getName());
119 }
120 return results;
121 }
122 return null;
123 }
124
125 @Override
126 protected List<String> doSuggestOption(String optionName) throws JMException {
127 if (optionName.equals("d")) {
128 return DomainsCommand.getCandidateDomains(getSession());
129 } else if (optionName.equals("b")) {
130 return BeanCommand.getCandidateBeanNames(getSession());
131 } else {
132 return null;
133 }
134 }
135
136 @Override
137 public void execute() throws JMException, IOException {
138 if (attributes.isEmpty()) {
139 throw new IllegalArgumentException("Please specify at least one attribute");
140 }
141 displayAttributes();
142 }
143
144
145
146
147 @MultiValue(listType = ArrayList.class, minValues = 1)
148 @Argument(displayName = "attr", description = "Name of attributes to select")
149 public final void setAttributes(List<String> attributes) {
150 Validate.notNull(attributes, "Attributes can't be NULL");
151 this.attributes = attributes;
152 }
153
154
155
156
157 @Option(name = "b", longName = "bean",
158 description = "MBean name where the attribute is. Optional if bean has been set")
159 public final void setBean(String bean) {
160 this.bean = bean;
161 }
162
163
164
165
166 @Option(name = "d", longName = "domain", description = "Domain of bean, optional")
167 public final void setDomain(String domain) {
168 this.domain = domain;
169 }
170
171
172
173
174 @Option(name = "i", longName = "info", description = "Show detail information of each attribute")
175 public final void setShowDescription(boolean showDescription) {
176 this.showDescription = showDescription;
177 }
178
179
180
181
182 @Option(name = "q", longName = "quots", description = "Quotation marks around value")
183 public final void setShowQuotationMarks(boolean noQuotationMarks) {
184 this.showQuotationMarks = noQuotationMarks;
185 }
186
187
188
189
190 @Option(name = "s", longName = "simple",
191 description = "Print simple expression of value without full expression")
192 public final void setSimpleFormat(boolean simpleFormat) {
193 this.simpleFormat = simpleFormat;
194 }
195
196 @Option(name = "l", longName = "delimiter",
197 description = "Sets an optional delimiter to be printed after the value")
198 public final void setDelimiter(String delimiter) {
199 this.delimiter = delimiter;
200 }
201
202 @Option(name = "n", longName = "singleLine",
203 description = "Prints result without a newline - default is false")
204 public final void setSingleLine(boolean singleLine) {
205 this.singleLine = singleLine;
206 }
207 }