1 package org.cyclopsgroup.jmxterm;
2
3 import org.apache.commons.lang3.Validate;
4 import org.cyclopsgroup.jcli.AutoCompletable;
5 import org.cyclopsgroup.jcli.annotation.Option;
6 import org.slf4j.Logger;
7 import org.slf4j.LoggerFactory;
8
9 import javax.management.JMException;
10 import java.io.IOException;
11 import java.util.List;
12
13
14
15
16
17
18
19
20 public abstract class Command implements AutoCompletable {
21 private static final Logger LOG = LoggerFactory.getLogger(Command.class);
22
23 private boolean help;
24
25 private Session session;
26
27
28
29
30
31
32
33
34
35 protected List<String> doSuggestArgument() throws IOException, JMException {
36 return null;
37 }
38
39
40
41
42
43
44
45
46
47 protected List<String> doSuggestOption(String optionName) throws IOException, JMException {
48 return null;
49 }
50
51
52
53
54
55
56
57 public abstract void execute() throws IOException, JMException;
58
59
60
61
62 public final Session getSession() {
63 return session;
64 }
65
66
67
68
69 public final boolean isHelp() {
70 return help;
71 }
72
73
74
75
76 @Option(name = "h", longName = "help", description = "Display usage")
77 public final void setHelp(boolean help) {
78 this.help = help;
79 }
80
81
82
83
84 public final void setSession(Session session) {
85 Validate.notNull(session, "Session can't be NULL");
86 this.session = session;
87 }
88
89 public final List<String> suggestArgument(String partialArg) {
90 if (partialArg != null) {
91 return null;
92 }
93 try {
94 return doSuggestArgument();
95 } catch (IOException e) {
96 if (LOG.isDebugEnabled()) {
97 LOG.debug("Couldn't suggest option", e);
98 }
99 return null;
100 } catch (JMException e) {
101 if (LOG.isDebugEnabled()) {
102 LOG.debug("Couldn't suggest option", e);
103 }
104 return null;
105 }
106 }
107
108 public final List<String> suggestOption(String name, String partialValue) {
109 if (partialValue != null) {
110 return null;
111 }
112 try {
113 return doSuggestOption(name);
114 } catch (IOException e) {
115 if (LOG.isDebugEnabled()) {
116 LOG.debug("Couldn't suggest option", e);
117 }
118 return null;
119 } catch (JMException e) {
120 if (LOG.isDebugEnabled()) {
121 LOG.debug("Couldn't suggest option", e);
122 }
123 return null;
124 }
125 }
126 }