Start line:  
End line:  

Snippet Preview

Snippet HTML Code

Stack Overflow Questions
  /*
  * JBoss, Home of Professional Open Source.
  * Copyright 2011, Red Hat Middleware LLC, 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.domain.controller.operations;
 
 import static org.jboss.as.controller.descriptions.ModelDescriptionConstants.BLOCKING;
 import static org.jboss.as.controller.descriptions.ModelDescriptionConstants.GROUP;
 import static org.jboss.as.controller.descriptions.ModelDescriptionConstants.HOST;
 import static org.jboss.as.controller.descriptions.ModelDescriptionConstants.OP_ADDR;
 import static org.jboss.as.controller.descriptions.ModelDescriptionConstants.RELOAD_SERVERS;
 import static org.jboss.as.controller.descriptions.ModelDescriptionConstants.RESTART_SERVERS;
 import static org.jboss.as.controller.descriptions.ModelDescriptionConstants.SERVER_CONFIG;
 import static org.jboss.as.controller.descriptions.ModelDescriptionConstants.START_SERVERS;
 import static org.jboss.as.controller.descriptions.ModelDescriptionConstants.STOP_SERVERS;
 
 import java.util.Map;
 import java.util.Set;
 
An operation handler for the :stop-servers, :restart-servers and :start-servers commands. This belongs in the domain model but needs access to the server inventory which is initialized when setting up the host model.

Author(s):
Kabir Khan
 
 
     public static final String RESTART_SERVERS_NAME = ;
     public static final String START_SERVERS_NAME = ;
     public static final String STOP_SERVERS_NAME = ;
 
     private static final int TIMEOUT = 10000;
 
     public static void initializeServerInventory(ServerInventory serverInventory) {
         ..setServerInventory(serverInventory);
         ..setServerInventory(serverInventory);
         ..setServerInventory(serverInventory);
     }
 
     public static void registerDomainHandlers(ManagementResourceRegistration registration) {
         registerHandlers(registrationfalse);
     }
 
     public static void registerServerGroupHandlers(ManagementResourceRegistration registration) {
         registerHandlers(registrationtrue);
     }
 
     private static void registerHandlers(ManagementResourceRegistration registrationboolean serverGroup) {
     }
 
     private static OperationDefinition getOperationDefinition(boolean serverGroupString operationName) {
         final AttributeDefinition blocking = SimpleAttributeDefinitionBuilder.create(.true).build();
         return new SimpleOperationDefinitionBuilder(operationName,
                 DomainResolver.getResolver(serverGroup ? . : .))
                 .addParameter(blocking)
                .setRuntimeOnly()
                .build();
    }
    private abstract static class AbstractHackLifecycleHandler implements OperationStepHandler {
        volatile ServerInventory serverInventory;
        protected AbstractHackLifecycleHandler() {
        }

        
To be called when setting up the host model
        void setServerInventory(ServerInventory serverInventory) {
            this. = serverInventory;
        }
        String getServerGroupName(final ModelNode operation) {
            final PathAddress address = PathAddress.pathAddress(operation.get());
            if (address.size() == 0) {
                return null;
            }
            return address.getLastElement().getValue();
        }
        Set<StringgetServersForGroup(final ModelNode modelfinal String groupName){
            if (groupName == null) {
                return Collections.emptySet();
            }
            final String hostName = model.get().keys().iterator().next();
            final ModelNode serverConfig = model.get(hostName).get();
            if(! serverConfig.isDefined()) {
                return Collections.emptySet();
            }
            final Set<Stringservers = new HashSet<String>();
            for (Property config : serverConfig.asPropertyList()) {
                if (groupName.equals(config.getValue().get().asString())) {
                    servers.add(config.getName());
                }
            }
            return servers;
        }
    }
    private static class StopServersLifecycleHandler extends AbstractHackLifecycleHandler {
        static final String OPERATION_NAME = ;
        static final StopServersLifecycleHandler INSTANCE = new StopServersLifecycleHandler();
        @Override
        public void execute(final OperationContext contextfinal ModelNode operationthrows OperationFailedException {
            context.acquireControllerLock();
            context.readResource(.false);
            final String group = getServerGroupName(operation);
            final boolean blocking = operation.get().asBoolean(false);
            context.addStep(new OperationStepHandler() {
                @Override
                public void execute(OperationContext contextModelNode operationthrows OperationFailedException {
                    // Even though we don't read from the service registry, we are modifying a service
                    context.getServiceRegistry(true);
                    if (group != null) {
                        final Set<StringwaitForServers = new HashSet<String>();
                        final ModelNode model = Resource.Tools.readModel(context.readResourceFromRoot(.true));
                        for (String server : getServersForGroup(modelgroup)) {
                            .stopServer(server);
                            waitForServers.add(server);
                        }
                        if (blocking) {
                            .awaitServersState(waitForServersfalse);
                        }
                    } else {
                        .stopServers(blocking);
                    }
                    context.completeStep(..);
                }
            }, .);
            context.stepCompleted();
        }
    }
    private static class StartServersLifecycleHandler extends AbstractHackLifecycleHandler {
        static final String OPERATION_NAME = ;
        static final StartServersLifecycleHandler INSTANCE = new StartServersLifecycleHandler();
        @Override
        public void execute(final OperationContext contextfinal ModelNode operationthrows OperationFailedException {
            context.acquireControllerLock();
            context.readResource(.false);
            final ModelNode model = Resource.Tools.readModel(context.readResourceFromRoot(.true));
            final String group = getServerGroupName(operation);
            final boolean blocking = operation.get().asBoolean(false);
            context.addStep(new OperationStepHandler() {
                @Override
                public void execute(OperationContext contextModelNode operationthrows OperationFailedException {
                    final String hostName = model.get().keys().iterator().next();
                    final ModelNode serverConfig = model.get(hostName).get();
                    final Set<StringserversInGroup = getServersForGroup(modelgroup);
                    final Set<StringwaitForServers = new HashSet<String>();
                    if(serverConfig.isDefined()) {
                        // Even though we don't read from the service registry, we are modifying a service
                        context.getServiceRegistry(true);
                        for (Property config : serverConfig.asPropertyList()) {
                            final ServerStatus status = .determineServerStatus(config.getName());
                            if (status != . && status != .) {
                                if (group == null || serversInGroup.contains(config.getName())) {
                                    if (status != .) {
                                        .stopServer(config.getName(), );
                                    }
                                    .startServer(config.getName(), model);
                                    waitForServers.add(config.getName());
                                }
                            }
                        }
                        if (blocking) {
                            .awaitServersState(waitForServerstrue);
                        }
                    }
                    context.completeStep(..);
                }
            }, .);
            context.stepCompleted();
        }
    }
    private static class RestartServersLifecycleHandler extends AbstractHackLifecycleHandler {
        static final String OPERATION_NAME = ;
        @Override
        public void execute(OperationContext contextModelNode operationthrows OperationFailedException {
            context.acquireControllerLock();
            context.readResource(.false);
            final ModelNode model = Resource.Tools.readModel(context.readResourceFromRoot(.true));
            final String group = getServerGroupName(operation);
            final boolean blocking = operation.get().asBoolean(false);
            context.addStep(new OperationStepHandler() {
                @Override
                public void execute(OperationContext contextModelNode operationthrows OperationFailedException {
                    // Even though we don't read from the service registry, we are modifying a service
                    context.getServiceRegistry(true);
                    Map<StringProcessInfoprocesses = .determineRunningProcesses(true);
                    final Set<StringserversInGroup = getServersForGroup(modelgroup);
                    final Set<StringwaitForServers = new HashSet<String>();
                    for (String serverName : processes.keySet()) {
                        final String serverModelName = .getProcessServerName(serverName);
                        if (group == null || serversInGroup.contains(serverModelName)) {
                            .restartServer(serverModelNamemodel);
                            waitForServers.add(serverModelName);
                        }
                    }
                    if (blocking) {
                        .awaitServersState(waitForServerstrue);
                    }
                    context.completeStep(..);
                }
            }, .);
            context.stepCompleted();
        }
    }
    private static class ReloadServersLifecycleHandler extends AbstractHackLifecycleHandler {
        static final String OPERATION_NAME = ;
        static final ReloadServersLifecycleHandler INSTANCE = new ReloadServersLifecycleHandler();
        @Override
        public void execute(OperationContext contextModelNode operationthrows OperationFailedException {
            context.acquireControllerLock();
            context.readResource(.false);
            final ModelNode model = Resource.Tools.readModel(context.readResourceFromRoot(.true));
            final String group = getServerGroupName(operation);
            final boolean blocking = operation.get().asBoolean(false);
            context.addStep(new OperationStepHandler() {
                @Override
                public void execute(OperationContext contextModelNode operationthrows OperationFailedException {
                    // Even though we don't read from the service registry, we are modifying a service
                    context.getServiceRegistry(true);
                    Map<StringProcessInfoprocesses = .determineRunningProcesses(true);
                    final Set<StringserversInGroup = getServersForGroup(modelgroup);
                    final Set<StringwaitForServers = new HashSet<String>();
                    for (String serverName : processes.keySet()) {
                        final String serverModelName = .getProcessServerName(serverName);
                        if (group == null || serversInGroup.contains(serverModelName)) {
                            .reloadServer(serverModelNamefalse);
                            waitForServers.add(serverModelName);
                        }
                    }
                    if (blocking) {
                        .awaitServersState(waitForServerstrue);
                    }
                    context.completeStep(..);
                }
            }, .);
            context.stepCompleted();
        }
    }
New to GrepCode? Check out our FAQ X