Search in sources :

Example 1 with Command

use of org.apache.ratis.shell.cli.Command in project incubator-ratis by apache.

the class RatisShell method loadCommands.

/**
 * Get instances of all subclasses of {@link Command} in a sub-package called "command" the given
 * package.
 *
 * @param pkgName package prefix to look in
 * @param classArgs type of args to instantiate the class
 * @param objectArgs args to instantiate the class
 * @return a mapping from command name to command instance
 */
public static Map<String, Command> loadCommands(String pkgName, Class[] classArgs, Object[] objectArgs) {
    Map<String, Command> commandsMap = new HashMap<>();
    Reflections reflections = new Reflections(pkgName);
    for (Class<? extends Command> cls : reflections.getSubTypesOf(Command.class)) {
        // Add commands from <pkgName>.command.*
        if (cls.getPackage().getName().equals(pkgName + ".command") && !Modifier.isAbstract(cls.getModifiers())) {
            // Only instantiate a concrete class
            final Command cmd = ReflectionUtils.newInstance(cls, classArgs, objectArgs);
            commandsMap.put(cmd.getCommandName(), cmd);
        }
    }
    return commandsMap;
}
Also used : Command(org.apache.ratis.shell.cli.Command) HashMap(java.util.HashMap) Reflections(org.reflections.Reflections)

Aggregations

HashMap (java.util.HashMap)1 Command (org.apache.ratis.shell.cli.Command)1 Reflections (org.reflections.Reflections)1