/*
 * JBoss, Home of Professional Open Source.
 * Copyright 2011, Red Hat, Inc., and individual contributors
 * as indicated by the @author tags. See the copyright.txt file in the
 * distribution for a full listing of individual contributors.
 *
 * This is free software; you can redistribute it and/or modify it
 * under the terms of the GNU Lesser General Public License as
 * published by the Free Software Foundation; either version 2.1 of
 * the License, or (at your option) any later version.
 *
 * This software is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
 * Lesser General Public License for more details.
 *
 * You should have received a copy of the GNU Lesser General Public
 * License along with this software; if not, write to the Free
 * Software Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA
 * 02110-1301 USA, or see the FSF site: http://www.fsf.org.
 */

package org.jboss.as.server.operations;


import org.jboss.as.controller.ControlledProcessState;
import org.jboss.as.controller.OperationContext;
import org.jboss.as.controller.OperationFailedException;
import org.jboss.as.controller.OperationStepHandler;
import org.jboss.as.controller.SimpleAttributeDefinition;
import org.jboss.as.controller.SimpleAttributeDefinitionBuilder;
import org.jboss.as.controller.SimpleOperationDefinition;
import org.jboss.as.controller.SimpleOperationDefinitionBuilder;
import org.jboss.as.controller.descriptions.ModelDescriptionConstants;
import org.jboss.as.process.ExitCodes;
import org.jboss.as.server.controller.descriptions.ServerDescriptions;
import org.jboss.dmr.ModelNode;
import org.jboss.dmr.ModelType;

/**
 * Handler that shuts down the standalone server.
 *
 * @author Jason T. Greene
 */
public class ServerShutdownHandler implements OperationStepHandler {
    private static final SimpleAttributeDefinition RESTART = new SimpleAttributeDefinitionBuilder(ModelDescriptionConstants.RESTART, ModelType.BOOLEAN)
            .setDefaultValue(new ModelNode(false))
            .setAllowNull(true)
            .build();
    public static final SimpleOperationDefinition DEFINITION = new SimpleOperationDefinitionBuilder("shutdown", ServerDescriptions.getResourceDescriptionResolver())
            .setParameters(RESTART)
            .setRuntimeOnly()
            .build();


    private final ControlledProcessState processState;

    public ServerShutdownHandler(ControlledProcessState processState) {
        this.processState = processState;
    }

    /**
     * {@inheritDoc}
     */
    @Override
    public void execute(OperationContext context, ModelNode operation) throws OperationFailedException {
        final boolean restart = RESTART.resolveModelAttribute(context, operation).asBoolean();
        context.addStep(new OperationStepHandler() {
            @Override
            public void execute(OperationContext context, ModelNode operation) throws OperationFailedException {
                // Even though we don't read from the service registry, we have a reference to a
                // service-fronted 'processState' so tell the controller we are modifying a service
                context.getServiceRegistry(true);
                processState.setStopping();
                final Thread thread = new Thread(new Runnable() {
                    public void run() {
                        System.exit(restart ? ExitCodes.RESTART_PROCESS_FROM_STARTUP_SCRIPT : ExitCodes.NORMAL);
                    }
                });
                // The intention is that this shutdown is graceful, and so the client gets a reply.
                // At the time of writing we did not yet have graceful shutdown.
                thread.setName("Management Triggered Shutdown");
                thread.start();
                context.completeStep(OperationContext.RollbackHandler.NOOP_ROLLBACK_HANDLER);
            }
        }, OperationContext.Stage.RUNTIME);

        context.stepCompleted();
    }
}

@@ -2858,24 +2858,166 @@
 sBoolean();%0A
+        // Acquire the controller lock to prevent new write ops and wait until current ones are done%0A        context.acquireControllerLock();%0A
         cont
@@ -3439,16 +3439,353 @@
 (true);%0A
+                context.completeStep(new OperationContext.ResultHandler() %7B%0A                    @Override%0A                    public void handleResult(OperationContext.ResultAction resultAction, OperationContext context, ModelNode operation) %7B%0A                        if(resultAction == OperationContext.ResultAction.KEEP) %7B%0A            
         
@@ -3816,24 +3816,36 @@
 Stopping();%0A
+            
             
@@ -3910,32 +3910,44 @@
                 
+       
+     
 public void run(
@@ -3946,24 +3946,36 @@
 oid run() %7B%0A
+            
             
@@ -4099,30 +4099,66 @@
     
-%7D%0A                %7D);%0A
+            %7D%0A                            %7D);%0A            
     
@@ -4265,24 +4265,36 @@
             
+            
 // At the ti
@@ -4354,32 +4354,44 @@
                 
+      
+      
 thread.setName(%22
@@ -4431,32 +4431,44 @@
                 
+            
 thread.start();%0A
@@ -4487,83 +4487,57 @@
     
-context.completeStep(OperationContext.RollbackHandler.NOOP_ROLLBACK_HANDLER
+        %7D%0A                    %7D%0A                %7D
 );%0A 

fix :shutdown oepration handler