1 package org.cyclopsgroup.jmxterm.cmd;
2
3 import org.apache.commons.lang3.Validate;
4 import org.cyclopsgroup.jcli.annotation.Argument;
5 import org.cyclopsgroup.jcli.annotation.Cli;
6 import org.cyclopsgroup.jcli.annotation.Option;
7 import org.cyclopsgroup.jmxterm.Command;
8 import org.cyclopsgroup.jmxterm.Session;
9 import org.cyclopsgroup.jmxterm.SyntaxUtils;
10 import org.cyclopsgroup.jmxterm.io.RuntimeIOException;
11
12 import javax.management.InstanceNotFoundException;
13 import javax.management.JMException;
14 import javax.management.MBeanServerConnection;
15 import javax.management.MalformedObjectNameException;
16 import javax.management.ObjectName;
17 import java.io.IOException;
18 import java.util.ArrayList;
19 import java.util.List;
20
21
22
23
24
25
26 @Cli(name = "bean", description = "Display or set current selected MBean. ",
27 note = "Without any parameter, it displays current selected bean, "
28 + "otherwise it selects the bean defined by the first parameter. eg. bean java.lang:type=Memory")
29 public class BeanCommand extends Command {
30
31
32
33
34
35
36
37
38
39
40 public static String getBeanName(String bean, String domain, Session session)
41 throws JMException, IOException {
42 Validate.notNull(session, "Session can't be NULL");
43 if (bean == null) {
44 return session.getBean();
45 }
46 if (SyntaxUtils.isNull(bean)) {
47 return null;
48 }
49 MBeanServerConnection con = session.getConnection().getServerConnection();
50 if (bean.indexOf(':') != -1) {
51 try {
52 ObjectName name = new ObjectName(bean);
53 con.getMBeanInfo(name);
54 return bean;
55 } catch (MalformedObjectNameException e) {
56 } catch (InstanceNotFoundException e) {
57 }
58 }
59
60 String domainName = DomainCommand.getDomainName(domain, session);
61 if (domainName == null) {
62 throw new IllegalArgumentException(
63 "Please specify domain using either -d option or domain command");
64 }
65 try {
66 ObjectName name = new ObjectName(domainName + ":" + bean);
67 con.getMBeanInfo(name);
68 return domainName + ":" + bean;
69 } catch (MalformedObjectNameException e) {
70 } catch (InstanceNotFoundException e) {
71 }
72 throw new IllegalArgumentException("Bean name " + bean + " isn't valid");
73 }
74
75
76
77
78 static List<String> getCandidateBeanNames(Session session) throws MalformedObjectNameException {
79 try {
80 ArrayList<String> results = new ArrayList<String>(BeansCommand.getBeans(session, null));
81 String domain = session.getDomain();
82 if (domain != null) {
83 List<String> beans = BeansCommand.getBeans(session, domain);
84 for (String bean : beans) {
85 results.add(bean.substring(domain.length() + 1));
86 }
87 }
88 return results;
89 } catch (IOException e) {
90 throw new RuntimeIOException("Couldn't find candidate bean names", e);
91 }
92
93 }
94
95 private String bean;
96
97 private String domain;
98
99 @Override
100 public List<String> doSuggestArgument() throws IOException, MalformedObjectNameException {
101 return getCandidateBeanNames(getSession());
102 }
103
104 @Override
105 public List<String> doSuggestOption(String optionName) throws IOException {
106 if (optionName.equals("d")) {
107 return DomainsCommand.getCandidateDomains(getSession());
108 }
109 return null;
110 }
111
112 @Override
113 public void execute() throws IOException, JMException {
114 Session session = getSession();
115 if (bean == null) {
116 if (session.getBean() == null) {
117 session.output.println(SyntaxUtils.NULL);
118 } else {
119 session.output.println(session.getBean());
120 }
121 return;
122 }
123 String beanName = getBeanName(bean, domain, session);
124 if (beanName == null) {
125 session.setBean(null);
126 session.output.printMessage("bean is unset");
127 return;
128 }
129 ObjectName name = new ObjectName(beanName);
130 MBeanServerConnection con = session.getConnection().getServerConnection();
131 con.getMBeanInfo(name);
132 session.setBean(beanName);
133 session.output.printMessage("bean is set to " + beanName);
134 }
135
136
137
138
139
140
141 @Argument(displayName = "bean", description = "MBean name with or without domain")
142 public final void setBean(String bean) {
143 this.bean = bean;
144 }
145
146
147
148
149
150
151 @Option(name = "d", longName = "domain", description = "Domain name")
152 public final void setDomain(String domain) {
153 this.domain = domain;
154 }
155 }