com.tobedevoured.command.Runner Maven / Gradle / Ivy
package com.tobedevoured.command;
import java.awt.BorderLayout;
import java.awt.Component;
import java.awt.GraphicsEnvironment;
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.ArrayList;
import java.util.List;
import java.util.Set;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
import javax.swing.JButton;
import javax.swing.JComboBox;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JTextField;
import javax.swing.UIManager;
import org.apache.commons.beanutils.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
*
* @throws CommandException
* @throws IOException
* @throws ConfigException
*/
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" );
}
manager = new ByYourCommandManager();
if ( config.hasPath( "command.dependency_manager" ) ) {
String dependencyManager = config.getString("command.dependency_manager");
if ( dependencyManager != null ) {
try {
manager.registerDependencyManager(dependencyManager);
} catch (CommandException e) {
throw new RunException(e);
}
}
}
try {
manager.scanForCommands( packages );
} catch (CommandException commandException) {
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 {@link Set} of resouce paths for {@link ApplicationContext}
*
* @param commandNotation String
* @return Set
* @throws CommandException
*/
/*
public Set getCommandContexts( String commandNotation ) throws CommandException {
return this.manager.getCommandContexts( commandNotation );
}
*/
/**
* 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 Planable} for a Class
*
* @param clazz Class
* @return {@link Planable}
*/
public Planable getPlan( Class clazz ) {
return manager.getPlan( clazz );
}
/**
* Get a {@link Planable} for a notation
*
* @param notation String
* @return {@link Planable}
*/
public Planable getPlan( String notation ) {
CommandDependency dep = manager.getCommands().get( notation );
return getPlan( dep.getTarget() );
}
/**
* Remove the GUI dropdown's param [] text from command
*
* @param command notation
* @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( ":" );
Planable 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
* @return {@link CommandMethod}
* @throws CommandException
*/
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
*/
public CommandMethod exec( String notation, List
© 2015 - 2025 Weber Informatics LLC | Privacy Policy