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

net.dharwin.common.tools.cli.api.EntryPoint Maven / Gradle / Ivy

The newest version!
package net.dharwin.common.tools.cli.api;

import java.lang.reflect.Constructor;

import net.dharwin.common.tools.cli.api.annotations.CLIEntry;
import net.dharwin.common.tools.cli.api.console.Console;
import net.dharwin.common.tools.cli.api.utils.CLIAnnotationDiscovereryListener;


import com.impetus.annovention.ClasspathDiscoverer;
import com.impetus.annovention.Discoverer;

/**
 * The entry point contains the main entry. It is in charge of
 * locating the desired CommandLineApplication implementation, as well
 * as initializing and starting it.
 * @author Sean
 *
 */
public class EntryPoint {
	
	public static void main(String[] args) {
		
		Discoverer discoverer = new ClasspathDiscoverer();
		CLIAnnotationDiscovereryListener discoveryListener =
			new CLIAnnotationDiscovereryListener(new String[] {CLIEntry.class.getName()});
		discoverer.addAnnotationListener(discoveryListener);
		discoverer.discover();

		if (discoveryListener.getDiscoveredClasses().isEmpty()) {
			Console.severe("Startup failed: Could not find CLIEntry.");
			System.exit(1);
		}
		
		String cliEntryClassName = discoveryListener.getDiscoveredClasses().get(0);

		Console.superFine("Loading CLIEntry ["+cliEntryClassName+"].");
		
		try {
			Class cliEntryClass = Class.forName(cliEntryClassName);
			if (!CommandLineApplication.class.isAssignableFrom(cliEntryClass)) {
				Console.severe("CLIEntry ["+cliEntryClassName+"] is not of type CommandLineApplication.");
				System.exit(1);
			}
			
			Constructor constructor = cliEntryClass.getConstructor();
			
			@SuppressWarnings("unchecked")
			CommandLineApplication cla =
					(CommandLineApplication)constructor.newInstance();
			cla.start();
		}
		catch (ClassNotFoundException e) {
			Console.severe("Unable to find CLIEntry class ["+cliEntryClassName+"].");
			System.exit(1);
		}
		catch (Exception e) {
			e.printStackTrace();
		}
	}
}




© 2015 - 2025 Weber Informatics LLC | Privacy Policy