001    /**
002     *
003     * Copyright 2003-2004 The Apache Software Foundation
004     *
005     *  Licensed under the Apache License, Version 2.0 (the "License");
006     *  you may not use this file except in compliance with the License.
007     *  You may obtain a copy of the License at
008     *
009     *     http://www.apache.org/licenses/LICENSE-2.0
010     *
011     *  Unless required by applicable law or agreed to in writing, software
012     *  distributed under the License is distributed on an "AS IS" BASIS,
013     *  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
014     *  See the License for the specific language governing permissions and
015     *  limitations under the License.
016     */
017    
018    package org.apache.geronimo.deployment.plugin.local;
019    
020    import org.apache.geronimo.kernel.Kernel;
021    import org.apache.geronimo.kernel.config.ConfigurationManager;
022    import org.apache.geronimo.kernel.config.ConfigurationUtil;
023    import org.apache.geronimo.kernel.repository.Artifact;
024    
025    import javax.enterprise.deploy.shared.CommandType;
026    import javax.enterprise.deploy.spi.TargetModuleID;
027    
028    /**
029     * @version $Rev: 405229 $ $Date: 2006-05-08 16:44:03 -0700 (Mon, 08 May 2006) $
030     */
031    public class StopCommand extends CommandSupport {
032        private final Kernel kernel;
033        private final TargetModuleID[] modules;
034    
035        public StopCommand(Kernel kernel, TargetModuleID modules[]) {
036            super(CommandType.STOP);
037            this.kernel = kernel;
038            this.modules = modules;
039        }
040    
041        public void run() {
042            try {
043                ConfigurationManager configurationManager = ConfigurationUtil.getConfigurationManager(kernel);
044                try {
045                    for (int i = 0; i < modules.length; i++) {
046                        TargetModuleID module = modules[i];
047                        Artifact moduleID = Artifact.create(module.getModuleID());
048                        if(configurationManager.isRunning(moduleID)) {
049                            configurationManager.stopConfiguration(moduleID);
050                        }
051                        if(configurationManager.isLoaded(moduleID)) {
052                            configurationManager.unloadConfiguration(moduleID);
053                            addModule(module);
054                        }
055                    }
056                } finally {
057                    ConfigurationUtil.releaseConfigurationManager(kernel, configurationManager);
058                }
059                if(getModuleCount() < modules.length) {
060                    fail("Some modules could not be stopped");
061                } else {
062                    complete("Completed");
063                }
064            } catch (Exception e) {
065                doFail(e);
066            }
067        }
068    }