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
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.*;
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.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
*
* @throws CommandException
* @throws IOException
* @throws ConfigException
*/
public Runner(String _package) throws RunException {
Config config = ConfigFactory.load();
List packages = Arrays.asList( _package );
setup(config, packages);
}
/**
* 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" );
}
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) {
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 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
* @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