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