Search in sources :

Example 1 with CommandHandlerProvider

use of org.jboss.as.cli.CommandHandlerProvider in project wildfly-core by wildfly.

the class ExtensionsLoader method loadHandlers.

/**
 * Using the client, iterates through the available domain management model extensions
 * and tries to load CLI command handlers from their modules.
 *
 * @param registry
 * @param address
 * @param client
 */
void loadHandlers(ControllerAddress address) throws CommandLineException, CommandLineParserException {
    ModelControllerClient client = ctx.getModelControllerClient();
    assert client != null : "client is null";
    if (moduleLoader == null) {
        ctx.printLine("Warning! The CLI is running in a non-modular environment and cannot load commands from management extensions.");
        return;
    }
    if (address != null && currentAddress != null && address.equals(currentAddress)) {
        return;
    }
    // remove previously loaded commands
    resetHandlers();
    currentAddress = address;
    final ModelNode req = new ModelNode();
    req.get(Util.ADDRESS).setEmptyList();
    req.get(Util.OPERATION).set(Util.READ_CHILDREN_RESOURCES);
    req.get(Util.CHILD_TYPE).set(Util.EXTENSION);
    final ModelNode response;
    try {
        response = client.execute(req);
    } catch (IOException e) {
        throw new CommandLineException("Extensions loader failed to read extensions", e);
    }
    if (!Util.isSuccess(response)) {
        throw new CommandLineException("Extensions loader failed to read extensions: " + Util.getFailureDescription(response));
    }
    final ModelNode result = response.get(Util.RESULT);
    if (!result.isDefined()) {
        throw new CommandLineException("Extensions loader failed to read extensions: " + result.asString());
    }
    for (Property ext : result.asPropertyList()) {
        ModelNode module = ext.getValue().get(Util.MODULE);
        if (!module.isDefined()) {
            addError("Extension " + ext.getName() + " is missing module attribute");
        } else {
            final ModuleIdentifier moduleId = ModuleIdentifier.fromString(module.asString());
            ModuleClassLoader cl;
            try {
                cl = moduleLoader.loadModule(moduleId).getClassLoader();
                ServiceLoader<CommandHandlerProvider> loader = ServiceLoader.load(CommandHandlerProvider.class, cl);
                for (CommandHandlerProvider provider : loader) {
                    try {
                        registry.registerHandler(provider.createCommandHandler(ctx), provider.isTabComplete(), provider.getNames());
                        addHandlers(Arrays.asList(provider.getNames()));
                    } catch (CommandRegistry.RegisterHandlerException e) {
                        addError(e.getLocalizedMessage());
                        final List<String> addedCommands = new ArrayList<String>(Arrays.asList(provider.getNames()));
                        addedCommands.removeAll(e.getNotAddedNames());
                        addHandlers(addedCommands);
                    }
                }
                ServiceLoader<Command> loader2 = ServiceLoader.load(Command.class, cl);
                for (Command provider : loader2) {
                    try {
                        CommandContainer container = aeshRegistry.addCommand(provider);
                        addCommand(container.getParser().getProcessedCommand().name());
                    } catch (CommandLineException e) {
                        addError(e.getLocalizedMessage());
                    }
                }
            } catch (ModuleLoadException e) {
                addError("Module " + module.asString() + " from extension " + ext.getName() + " available on the server couldn't be loaded locally: " + e.getLocalizedMessage());
            }
        }
    }
    if (!errors.isEmpty()) {
        ctx.printLine("Warning! There were errors trying to load extensions. For more details, please, execute 'extension-commands --errors'");
    }
}
Also used : ModuleLoadException(org.jboss.modules.ModuleLoadException) CommandRegistry(org.jboss.as.cli.CommandRegistry) CLICommandRegistry(org.jboss.as.cli.impl.aesh.CLICommandRegistry) ModuleClassLoader(org.jboss.modules.ModuleClassLoader) IOException(java.io.IOException) CommandLineException(org.jboss.as.cli.CommandLineException) CommandHandlerProvider(org.jboss.as.cli.CommandHandlerProvider) ModelControllerClient(org.jboss.as.controller.client.ModelControllerClient) Command(org.aesh.command.Command) CommandContainer(org.aesh.command.container.CommandContainer) ArrayList(java.util.ArrayList) List(java.util.List) ModuleIdentifier(org.jboss.modules.ModuleIdentifier) ModelNode(org.jboss.dmr.ModelNode) Property(org.jboss.dmr.Property)

Example 2 with CommandHandlerProvider

use of org.jboss.as.cli.CommandHandlerProvider in project wildfly-core by wildfly.

the class CommandContextImpl method registerExtraHandlers.

private void registerExtraHandlers() throws CommandLineException, CommandLineParserException {
    ServiceLoader<CommandHandlerProvider> loader = ServiceLoader.load(CommandHandlerProvider.class);
    for (CommandHandlerProvider provider : loader) {
        cmdRegistry.registerHandler(provider.createCommandHandler(this), provider.isTabComplete(), provider.getNames());
    }
    aeshCommands.registerExtraCommands();
}
Also used : CommandHandlerProvider(org.jboss.as.cli.CommandHandlerProvider)

Aggregations

CommandHandlerProvider (org.jboss.as.cli.CommandHandlerProvider)2 IOException (java.io.IOException)1 ArrayList (java.util.ArrayList)1 List (java.util.List)1 Command (org.aesh.command.Command)1 CommandContainer (org.aesh.command.container.CommandContainer)1 CommandLineException (org.jboss.as.cli.CommandLineException)1 CommandRegistry (org.jboss.as.cli.CommandRegistry)1 CLICommandRegistry (org.jboss.as.cli.impl.aesh.CLICommandRegistry)1 ModelControllerClient (org.jboss.as.controller.client.ModelControllerClient)1 ModelNode (org.jboss.dmr.ModelNode)1 Property (org.jboss.dmr.Property)1 ModuleClassLoader (org.jboss.modules.ModuleClassLoader)1 ModuleIdentifier (org.jboss.modules.ModuleIdentifier)1 ModuleLoadException (org.jboss.modules.ModuleLoadException)1