com.tobedevoured.command.Runner Maven / Gradle / Ivy
Go to download
Show more of this group Show more artifacts with this name
Show all versions of core Show documentation
Show all versions of core Show documentation
Make Java do your bidding by turning any code into an executable
The newest version!
package com.tobedevoured.command;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.ItemEvent;
import java.awt.event.ItemListener;
import java.io.IOException;
import java.lang.reflect.InvocationTargetException;
import java.util.*;
import java.util.List;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
import javax.swing.*;
import javax.swing.border.EmptyBorder;
import org.apache.commons.lang.reflect.ConstructorUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import com.typesafe.config.Config;
import com.typesafe.config.ConfigFactory;
/**
* Run @ByYourCommand annotated classes, as a GUI or command line.
*
* @author Michael Guymon
*/
public class Runner {
public static final String COMMAND_PATTERN = "(.+)\\[(.+)\\]$";
private static Logger logger = LoggerFactory.getLogger(Runner.class);
private ByYourCommandManager manager;
public static boolean ALLOW_SYSTEM_EXIT = true;
public static boolean HAS_ERRORS = false;
/**
* Construct new instance
*
* @param _package String
* @throws RunException fails to start Runner
*/
public Runner(String _package) throws RunException {
Config config = ConfigFactory.load();
List packages = Arrays.asList( _package );
setup(config, packages);
}
/**
* Construct new instance
*
* @throws RunException fails to start Runner
*/
public Runner() throws RunException {
Config config = ConfigFactory.load();
List packages = null;
Object ref = config.getAnyRef("command.packages");
if ( ref instanceof List ) {
packages = (List)ref;
} else if ( ref instanceof String ) {
packages = new ArrayList();
packages.add( (String)ref );
} else {
throw new RunException( "command.packages is an invalid format" );
}
setup(config, packages);
}
protected ByYourCommandManager createCommandManager() {
return new ByYourCommandManager();
}
private void setup(Config config, List packages) throws RunException {
manager = createCommandManager();
if ( config.hasPath( "command.dependency_manager" ) ) {
String dependencyManager = config.getString("command.dependency_manager");
if ( dependencyManager != null ) {
try {
manager.registerDependencyResolver(dependencyManager);
} catch (CommandException e) {
throw new RunException(e);
}
}
}
try {
manager.scanForCommands( packages );
} catch (CommandException commandException) {
throw new RunException( commandException );
}
}
/**
* Shutdown runner
*/
public void shutdown() {
shutdown( false );
}
public void shutdown(boolean hasError) {
if ( ALLOW_SYSTEM_EXIT ) {
if ( hasError ) {
System.exit(1);
} else {
System.exit(0);
}
}
}
/**
* Get list of all commands
*
* @return Set
*/
public Set getCommandsDesc() {
return manager.getCommandsDesc();
}
/**
* Get list of all groups
*
* @return Set
*/
public Set getGroups() {
return manager.getGroups().keySet();
}
/**
* Get {@link Plan} for a Class
*
* @param clazz Class
* @return {@link Plan}
*/
public Plan getPlan( Class clazz ) {
return manager.getPlan( clazz );
}
/**
* Get a {@link Plan} for a notation
*
* @param notation String
* @return {@link Plan}
*/
public Plan getPlan( String notation ) {
CommandDependency dep = manager.getCommands().get( notation );
return getPlan( dep.getTarget() );
}
/**
* Remove the GUI dropdown's param [] text from command
*
* @param notation String
* @return String
*/
private String removeCommandParamText( String notation ) {
return notation.replaceAll("\\[.+\\]$", "");
}
/**
* Get {@link CommandMethod} for a String group:target or group:target:command
*
* @param commandNotation String
* @return {@link CommandMethod}
*/
public CommandMethod getCommandMethod( String commandNotation ) {
String[] notation = commandNotation.split( ":" );
Plan plan = getPlan( commandNotation );
if ( plan != null ) {
if ( notation.length == 3 ) {
return plan.getCommands().get( notation[2] );
} else {
return plan.getDefaultCommand();
}
} else {
return null;
}
}
/**
* Exec a notation
*
* @param notation String
* @return {@link CommandMethod}
* @throws CommandException faile to exec command
*/
public CommandMethod exec( String notation ) throws CommandException {
return exec( notation, null );
}
/**
* Exec a notation
*
* @param notation String
* @param params {@link List}
* @return {@link CommandMethod}
* @throws CommandException failed to exec command
*/
public CommandMethod exec( String notation, List