All Downloads are FREE. Search and download functionalities are using the official Maven repository.

au.csiro.sparkle.cmd.MultiCmdApp Maven / Gradle / Ivy

The newest version!
package au.csiro.sparkle.cmd;

import java.util.HashMap;
import java.util.Map;
import java.util.stream.Stream;

import au.csiro.sparkle.common.ObjectProvider;

public abstract class MultiCmdApp extends CmdApp {
	
	static class ClassProvider extends ObjectProvider{
		private final Class clazz;

		public ClassProvider(Class clazz) {
	        this.clazz = clazz;
        }
		@SuppressWarnings("unchecked")
        public ClassProvider(String className) {
	        try {
	            this.clazz = (Class) Class.forName(className);
            } catch (ClassNotFoundException ex) {
            	throw new RuntimeException(ex);
            }
	        if (!CmdApp.class.isAssignableFrom(clazz)) {
	        	throw new IllegalArgumentException("Class: " + className  + " is not a CmdApp");
	        }
        }

		@Override
        public T provide() {
			try {
	           return  clazz.newInstance();
            } catch (InstantiationException ex) {
            	throw new RuntimeException(ex);
            } catch (IllegalAccessException ex) {
            	throw new RuntimeException(ex);
            }
        }
	}	
	private final Map> modules  = new HashMap<>();
	
	public   void registerClass(String name, Class clazz) {
		register(name, new ClassProvider(clazz));
	}
	public   void register(String name, ObjectProvider provider) {
		modules.put(name, provider);
	}
	
	@Override
	public void run(String[] args) {
		if (args.length < 1) {
			usage();
		} else {
			runCommandOrClass(args[0], Stream.of(args).skip(1).toArray(n -> new String[n]));
		}
	}
	private void runCommandOrClass(String cmdOrClass, String[] args) {
	    CmdApp.runApp(args,
	    	modules.computeIfAbsent(cmdOrClass, clazz-> new ClassProvider(clazz)).provide());
    }
	
	protected String getAppName() {
		return getClass().getName();
	}
	
	@Override
	public void usage() {
	   System.out.println("Usage:");
	   System.out.format("\t%s [command|className] args*\n", getAppName());
	   System.out.println("Available commands are:");
	   modules.keySet().stream().sorted().forEach(cmd -> System.out.format("\t%s\n", cmd));
	   System.out.println("For help on a specific command use:");
	   System.out.format("\t command -h\n", getAppName());
    }
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy