Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Binary file added src/main/.DS_Store
Binary file not shown.
Binary file removed src/main/image/logo.xcf
Binary file not shown.
18 changes: 5 additions & 13 deletions src/main/java/org/cyclopsgroup/jmxterm/Command.java
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,9 @@

import java.io.IOException;
import java.util.List;

import javax.management.JMException;

import org.apache.commons.lang3.Validate;
import org.cyclopsgroup.jcli.AutoCompletable;
import org.cyclopsgroup.jcli.annotation.Option;
Expand Down Expand Up @@ -83,14 +85,9 @@ public final List<String> suggestArgument(String partialArg) {
}
try {
return doSuggestArgument();
} catch (IOException e) {
} catch (IOException | JMException e) {
if (LOG.isDebugEnabled()) {
LOG.debug("Couldn't suggest option", e);
}
return null;
} catch (JMException e) {
if (LOG.isDebugEnabled()) {
LOG.debug("Couldn't suggest option", e);
LOG.debug("Couldn't suggest argument", e);
}
return null;
}
Expand All @@ -102,12 +99,7 @@ public final List<String> suggestOption(String name, String partialValue) {
}
try {
return doSuggestOption(name);
} catch (IOException e) {
if (LOG.isDebugEnabled()) {
LOG.debug("Couldn't suggest option", e);
}
return null;
} catch (JMException e) {
} catch (IOException | JMException e) {
if (LOG.isDebugEnabled()) {
LOG.debug("Couldn't suggest option", e);
}
Expand Down
5 changes: 3 additions & 2 deletions src/main/java/org/cyclopsgroup/jmxterm/SyntaxUtils.java
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
import org.apache.commons.io.output.NullOutputStream;
import org.apache.commons.lang3.ClassUtils;
import org.apache.commons.lang3.StringUtils;
import org.apache.commons.lang3.Strings;
import org.apache.commons.lang3.math.NumberUtils;
import org.cyclopsgroup.jmxterm.utils.ValueFormat;

Expand Down Expand Up @@ -64,7 +65,7 @@ public static JMXServiceURL getUrl(String url, JavaProcessManager jpm) throws IO
* @return True if value is <code>null</code>
*/
public static boolean isNull(String s) {
return StringUtils.equalsIgnoreCase(NULL, s) || StringUtils.equals("*", s);
return Strings.CI.equals(NULL, s) || Strings.CS.equals("*", s);
}

/**
Expand All @@ -75,7 +76,7 @@ public static boolean isNull(String s) {
* @return Object of value
*/
public static Object parse(String expression, String type) {
if (expression == null || StringUtils.equalsIgnoreCase(NULL, expression)) {
if (expression == null || Strings.CI.equals(NULL, expression)) {
return null;
}
Class<?> c;
Expand Down
26 changes: 12 additions & 14 deletions src/main/java/org/cyclopsgroup/jmxterm/boot/CliMain.java
Original file line number Diff line number Diff line change
Expand Up @@ -6,9 +6,10 @@
import java.io.PrintWriter;
import java.util.HashMap;
import java.util.Map;

import javax.management.remote.JMXConnector;
import javax.rmi.ssl.SslRMIClientSocketFactory;
import org.apache.commons.lang3.StringUtils;

import org.cyclopsgroup.jcli.ArgumentProcessor;
import org.cyclopsgroup.jcli.GnuParser;
import org.cyclopsgroup.jmxterm.SyntaxUtils;
Expand Down Expand Up @@ -37,7 +38,7 @@ public class CliMain {

private static final String COMMAND_PROMPT = "$> ";

public static final void main(String[] args) throws Exception {
public static void main(String[] args) throws Exception {
System.exit(new CliMain().execute(args));
}

Expand Down Expand Up @@ -66,15 +67,15 @@ int execute(String[] args) throws Exception {
}

CommandOutput output;
if (StringUtils.equals(options.getOutput(), CliMainOptions.STDOUT)) {
if (CliMainOptions.STDOUT.equals(options.getOutput())) {
output = new PrintStreamCommandOutput(System.out, System.err);
} else {
File outputFile = new File(options.getOutput());
output = new FileCommandOutput(outputFile, options.isAppendToOutput());
}
try {
CommandInput input;
if (options.getInput().equals(CliMainOptions.STDIN)) {
if (CliMainOptions.STDIN.equals(options.getInput())) {
if (options.isNonInteractive()) {
input = new InputStreamCommandInput(System.in);
} else {
Expand All @@ -90,14 +91,11 @@ int execute(String[] args) throws Exception {
Runtime.getRuntime()
.addShutdownHook(
new Thread(
new Runnable() {
@Override
public void run() {
try {
history.save();
} catch (IOException e) {
System.err.println("Failed to flush command history! " + e);
}
() -> {
try {
history.save();
} catch (IOException e) {
System.err.println("Failed to flush command history! " + e);
}
}));
input = new JlineCommandInput(consoleReader, COMMAND_PROMPT);
Expand All @@ -111,8 +109,8 @@ public void run() {
}
try {
CommandCenter commandCenter = new CommandCenter(output, input);
if (input instanceof JlineCommandInput) {
((JlineCommandInput) input)
if (input instanceof JlineCommandInput commandInput) {
commandInput
.getConsole()
.setCompleter(new ConsoleCompletor(commandCenter));
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ public class CliMainOptions {

private boolean nonInteractive;

private boolean appendToOutput = false;
private boolean appendToOutput;

private String output = STDOUT;

Expand Down
30 changes: 9 additions & 21 deletions src/main/java/org/cyclopsgroup/jmxterm/cc/CommandCenter.java
Original file line number Diff line number Diff line change
Expand Up @@ -16,8 +16,6 @@
import org.apache.commons.lang3.StringUtils;
import org.apache.commons.lang3.Validate;
import org.cyclopsgroup.caff.token.EscapingValueTokenizer;
import org.cyclopsgroup.caff.token.TokenEvent;
import org.cyclopsgroup.caff.token.TokenEventHandler;
import org.cyclopsgroup.caff.token.ValueTokenizer;
import org.cyclopsgroup.jcli.ArgumentProcessor;
import org.cyclopsgroup.jmxterm.Command;
Expand Down Expand Up @@ -118,36 +116,29 @@ private void doExecute(String command) throws JMException {
}

// Take the first argument out since it's command name
final List<String> args = new ArrayList<String>();
final List<String> args = new ArrayList<>();
argTokenizer.parse(
command,
new TokenEventHandler() {
public void handleEvent(TokenEvent event) {
args.add(event.getToken());
}
});
event ->
args.add(event.getToken()));
String commandName = args.remove(0);
// Leave the rest of arguments for command
String[] commandArgs = args.toArray(ArrayUtils.EMPTY_STRING_ARRAY);
// Call command with parsed command name and arguments
try {
doExecute(commandName, commandArgs, command);
doExecute(commandName, commandArgs);
} catch (IOException e) {
throw new RuntimeIOException("Runtime IO exception: " + e.getMessage(), e);
}
}

// TODO: The casting can be removed with the next release of jcli.
@SuppressWarnings("unchecked")
private void doExecute(String commandName, String[] commandArgs, String originalCommand)
private void doExecute(String commandName, String[] commandArgs)
throws JMException, IOException {
Command cmd = commandFactory.createCommand(commandName);
if (cmd instanceof HelpCommand) {
((HelpCommand) cmd).setCommandCenter(this);
if (cmd instanceof HelpCommand command) {
command.setCommandCenter(this);
}
ArgumentProcessor<Command> ap =
(ArgumentProcessor<Command>) ArgumentProcessor.forType(cmd.getClass());

ArgumentProcessor<Command> ap = ArgumentProcessor.forType(cmd.getClass());
ap.process(commandArgs, cmd);
// Print out usage if help option is specified
if (cmd.isHelp()) {
Expand Down Expand Up @@ -175,10 +166,7 @@ public boolean execute(String command) {
try {
doExecute(command);
return true;
} catch (JMException e) {
session.output.printError(e);
return false;
} catch (RuntimeException e) {
} catch (JMException | RuntimeException e) {
session.output.printError(e);
return false;
}
Expand Down
18 changes: 8 additions & 10 deletions src/main/java/org/cyclopsgroup/jmxterm/cc/ConsoleCompletor.java
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
package org.cyclopsgroup.jmxterm.cc;

import java.util.ArrayList;
import java.util.Collections;
import java.util.List;

import org.apache.commons.lang3.StringUtils;
import org.apache.commons.lang3.Validate;
import org.cyclopsgroup.jcli.jline.CliCompletor;
Expand Down Expand Up @@ -30,12 +30,10 @@ public class ConsoleCompletor implements Completer {
public ConsoleCompletor(CommandCenter commandCenter) {
Validate.notNull(commandCenter, "Command center can't be NULL");
this.commandCenter = commandCenter;
List<String> commandNames = new ArrayList<String>(commandCenter.getCommandNames());
Collections.sort(commandNames);
this.commandNames = new ArrayList<Candidate>(commandNames.size());
for (String commandName : commandNames) {
this.commandNames.add(new Candidate(commandName));
}
this.commandNames = commandCenter.getCommandNames().stream()
.sorted()
.map(Candidate::new)
.toList();
}

@Override
Expand All @@ -48,12 +46,12 @@ public void complete(LineReader reader, ParsedLine line, List<Candidate> candida
int separatorPos = buffer.indexOf(' ');
String commandName = buffer.substring(0, separatorPos);
if (LOG.isDebugEnabled()) {
LOG.debug("Command name is [" + commandName + "]");
LOG.debug("Command name is [{}]", commandName);
}
String commandArguments = buffer.substring(separatorPos + 1);
commandArguments.replaceFirst("^\\s*", "");
if (LOG.isDebugEnabled()) {
LOG.debug("Analyzing commmand arguments [" + commandArguments + "]");
LOG.debug("Analyzing command arguments [{}]", commandArguments);
}
Command cmd = commandCenter.commandFactory.createCommand(commandName);
cmd.setSession(commandCenter.session);
Expand All @@ -76,7 +74,7 @@ private void completeCommandName(String buf, List<Candidate> candidates) {
candidates.addAll(commandNames);
} else if (buf.indexOf(' ') == -1) {
// Partial one word
List<Candidate> matchedNames = new ArrayList<Candidate>();
List<Candidate> matchedNames = new ArrayList<>();
for (Candidate commandName : commandNames) {
if (commandName.value().startsWith(buf)) {
matchedNames.add(commandName);
Expand Down
11 changes: 5 additions & 6 deletions src/main/java/org/cyclopsgroup/jmxterm/cc/HelpCommand.java
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
import java.util.ArrayList;
import java.util.Collections;
import java.util.List;

import org.apache.commons.lang3.Validate;
import org.cyclopsgroup.jcli.ArgumentProcessor;
import org.cyclopsgroup.jcli.annotation.Argument;
Expand All @@ -26,20 +27,18 @@
public class HelpCommand extends Command {
private List<String> argNames = Collections.emptyList();

private CommandCenter commandCenter = null;
private CommandCenter commandCenter;

@Override
public void execute() {
Validate.notNull(commandCenter, "Command center hasn't been set yet");
if (argNames.isEmpty()) {
List<String> commandNames = new ArrayList<String>(commandCenter.getCommandNames());
Collections.sort(commandNames);
List<String> commandNames = commandCenter.getCommandNames().stream().sorted().toList();
getSession().output.printMessage("following commands are available to use:");
for (String commandName : commandNames) {
Class<? extends Command> commandType = commandCenter.getCommandType(commandName);
org.cyclopsgroup.jcli.spi.Cli cli =
ArgumentProcessor.forType(commandType).createParsingContext().cli();
getSession().output.println(String.format("%-8s - %s", commandName, cli.getDescription()));
org.cyclopsgroup.jcli.spi.Cli cli = ArgumentProcessor.forType(commandType).createParsingContext().cli();
getSession().output.println("%-8s - %s".formatted(commandName, cli.getDescription()));
}
} else {
for (String argName : argNames) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,7 @@ public PredefinedCommandFactory(String configPath) throws IOException {
throw new IOException("Expected configuration doesn't appear in " + configPath);
}
HashMap<String, Class<? extends Command>> commands =
new HashMap<String, Class<? extends Command>>();
new HashMap<>();
for (String name : props.getStringArray("name")) {
String type = props.getString(name + ".type");
Class<? extends Command> commandType;
Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,9 @@
package org.cyclopsgroup.jmxterm.cc;

import java.lang.reflect.InvocationTargetException;
import java.util.Collections;
import java.util.Map;

import org.apache.commons.lang3.Validate;
import org.cyclopsgroup.jmxterm.Command;
import org.cyclopsgroup.jmxterm.CommandFactory;
Expand Down Expand Up @@ -29,11 +31,9 @@ public Command createCommand(String commandName) {
"Command " + commandName + " isn't valid, run help to see available commands");
}
try {
return commandType.newInstance();
} catch (InstantiationException e) {
throw new RuntimeException("Can't instantiate instance", e);
} catch (IllegalAccessException e) {
throw new RuntimeException("Illegal access", e);
return commandType.getDeclaredConstructor().newInstance();
} catch (InstantiationException | NoSuchMethodException | IllegalAccessException | InvocationTargetException e) {
throw new IllegalStateException("Can't instantiate instance", e);
}
}

Expand Down
10 changes: 4 additions & 6 deletions src/main/java/org/cyclopsgroup/jmxterm/cmd/BeanCommand.java
Original file line number Diff line number Diff line change
Expand Up @@ -54,8 +54,7 @@ public static String getBeanName(String bean, String domain, Session session)
ObjectName name = new ObjectName(bean);
con.getMBeanInfo(name);
return bean;
} catch (MalformedObjectNameException e) {
} catch (InstanceNotFoundException e) {
} catch (MalformedObjectNameException | InstanceNotFoundException e) {
}
}

Expand All @@ -68,16 +67,15 @@ public static String getBeanName(String bean, String domain, Session session)
ObjectName name = new ObjectName(domainName + ":" + bean);
con.getMBeanInfo(name);
return domainName + ":" + bean;
} catch (MalformedObjectNameException e) {
} catch (InstanceNotFoundException e) {
} catch (MalformedObjectNameException | InstanceNotFoundException e) {
}
throw new IllegalArgumentException("Bean name " + bean + " isn't valid");
}

/** Gets a list of candidate beans. */
static List<String> getCandidateBeanNames(Session session) throws MalformedObjectNameException {
try {
ArrayList<String> results = new ArrayList<String>(BeansCommand.getBeans(session, null));
ArrayList<String> results = new ArrayList<>(BeansCommand.getBeans(session, null));
String domain = session.getDomain();
if (domain != null) {
List<String> beans = BeansCommand.getBeans(session, domain);
Expand All @@ -102,7 +100,7 @@ public List<String> doSuggestArgument() throws IOException, MalformedObjectNameE

@Override
public List<String> doSuggestOption(String optionName) throws IOException {
if (optionName.equals("d")) {
if ("d".equals(optionName)) {
return DomainsCommand.getCandidateDomains(getSession());
}
return null;
Expand Down
Loading