@Override
    protected void doHandle(CommandContext ctx) throws CommandLineException {
        final ModelControllerClient client = ctx.getModelControllerClient();
        if(client == null) {
            throw new CommandLineException("Connection is not available.");
        }

        if (embeddedServerRef != null && embeddedServerRef.get() != null) {
            embeddedServerRef.get().stop();
            return;
        }

        if (!(client instanceof AwaiterModelControllerClient)) {
            throw new CommandLineException("Unsupported ModelControllerClient implementation " + client.getClass().getName());
        }
        final AwaiterModelControllerClient cliClient = (AwaiterModelControllerClient) client;

        final ModelNode op = this.buildRequestWithoutHeaders(ctx);

        boolean disconnect = true;
        final String restartValue = restart.getValue(ctx.getParsedCommandLine());
        if (Util.TRUE.equals(restartValue) ||
                ctx.isDomainMode() &&
                !isLocalHost(ctx.getModelControllerClient(), host.getValue(ctx.getParsedCommandLine()))) {
            disconnect = false;
        }

        try {
            final ModelNode response = cliClient.execute(op, true);
            if(!Util.isSuccess(response)) {
                throw new CommandLineException(Util.getFailureDescription(response));
            }
        } catch(IOException e) {
            // if it's not connected, it's assumed the connection has already been shutdown
            if(cliClient.isConnected()) {
                StreamUtils.safeClose(client);
                throw new CommandLineException("Failed to execute :shutdown", e);
            }
        }

        if (disconnect) {
            ctx.disconnectController();
        } else {
            // if I try to reconnect immediately, it'll hang for 5 sec
            // which the default connection timeout for model controller client
            // waiting half a sec on my machine works perfectly
            try {
                Thread.sleep(2000);
            } catch (InterruptedException e) {
                throw new CommandLineException("Interrupted while pausing before reconnecting.", e);
            }
            try {
                cliClient.ensureConnected(ctx.getConfig().getConnectionTimeout() + 1000);
            } catch(CommandLineException e) {
                ctx.disconnectController();
                throw e;
            } catch (IOException ex) {
                if (ex instanceof RedirectException) {
                    if (!Util.reconnectContext((RedirectException) ex, ctx)) {
                        throw new CommandLineException("Can't reconnect context.", ex);
                    }
                } else {
                    throw new CommandLineException(ex);
                }
            }
        }
    }
@ Override protected void doHandle ( CommandContext ctx ) throws CommandLineException { final ModelControllerClient client = ctx . getModelControllerClient ( ) ; if ( client == null ) { throw new CommandLineException ( "Connection is not available." ) ; } if ( embeddedServerRef != null && embeddedServerRef . get ( ) != null ) { embeddedServerRef . get ( ) . stop ( ) ; return ; } if ( ! ( client instanceof AwaiterModelControllerClient ) ) { throw new CommandLineException ( "Unsupported ModelControllerClient implementation " + client . getClass ( ) . getName ( ) ) ; } final AwaiterModelControllerClient cliClient = ( AwaiterModelControllerClient ) client ; final ModelNode op = this . buildRequestWithoutHeaders ( ctx ) ; boolean disconnect = true ; final String restartValue = restart . getValue ( ctx . getParsedCommandLine ( ) ) ; if ( Util . TRUE . equals ( restartValue ) || ctx . isDomainMode ( ) && ! isLocalHost ( ctx . getModelControllerClient ( ) , host . getValue ( ctx . getParsedCommandLine ( ) ) ) ) { disconnect = false ; } try { final ModelNode response = cliClient . execute ( op , true ) ; if ( ! Util . isSuccess ( response ) ) { throw new CommandLineException ( Util . getFailureDescription ( response ) ) ; } } catch ( IOException e ) { if ( cliClient . isConnected ( ) ) { StreamUtils . safeClose ( client ) ; throw new CommandLineException ( "Failed to execute :shutdown" , e ) ; } } if ( disconnect ) { ctx . disconnectController ( ) ; } else { try { Thread . sleep ( 2000 ) ; } catch ( InterruptedException e ) { throw new CommandLineException ( "Interrupted while pausing before reconnecting." , e ) ; } try { cliClient . ensureConnected ( ctx . getConfig ( ) . getConnectionTimeout ( ) + 1000 ) ; } catch ( CommandLineException e ) { ctx . disconnectController ( ) ; throw e ; } catch ( IOException ex ) { if ( ex instanceof RedirectException ) { if ( ! Util . reconnectContext ( ( RedirectException ) ex , ctx ) ) { throw new CommandLineException ( "Can't reconnect context." , ex ) ; } } else { throw new CommandLineException ( ex ) ; } } } }
/* (non-Javadoc)
@see org.jboss.as.cli.handlers.CommandHandlerWithHelp#doHandle(org.jboss.as.cli.CommandContext)