001    /**
002     *  Licensed to the Apache Software Foundation (ASF) under one or more
003     *  contributor license agreements.  See the NOTICE file distributed with
004     *  this work for additional information regarding copyright ownership.
005     *  The ASF licenses this file to You under the Apache License, Version 2.0
006     *  (the "License"); you may not use this file except in compliance with
007     *  the License.  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.deployment.plugin.TargetModuleIDImpl;
021    import org.apache.geronimo.kernel.Kernel;
022    import org.apache.geronimo.kernel.config.ConfigurationManager;
023    import org.apache.geronimo.kernel.config.ConfigurationUtil;
024    import org.apache.geronimo.kernel.repository.Artifact;
025    
026    import java.util.List;
027    import javax.enterprise.deploy.shared.CommandType;
028    import javax.enterprise.deploy.shared.ModuleType;
029    import javax.enterprise.deploy.spi.TargetModuleID;
030    
031    /**
032     * @version $Rev: 537224 $ $Date: 2007-05-11 12:25:51 -0400 (Fri, 11 May 2007) $
033     */
034    public class StopCommand extends CommandSupport {
035        private final Kernel kernel;
036        private final TargetModuleID[] modules;
037    
038        public StopCommand(Kernel kernel, TargetModuleID modules[]) {
039            super(CommandType.STOP);
040            this.kernel = kernel;
041            this.modules = modules;
042        }
043    
044        public void run() {
045            try {
046                ConfigurationManager configurationManager = ConfigurationUtil.getConfigurationManager(kernel);
047                int alreadyStopped = 0;
048    
049                try {
050                    for (int i = 0; i < modules.length; i++) {
051                        TargetModuleID module = modules[i];
052                        Artifact moduleID = Artifact.create(module.getModuleID());
053                        org.apache.geronimo.kernel.config.LifecycleResults lcresult = null;
054    
055                        if (configurationManager.isRunning(moduleID)) {
056                            lcresult = configurationManager.stopConfiguration(moduleID);
057                            addModule(module);
058                        } else {
059                            updateStatus("Module " + moduleID + " is already stopped");
060                            alreadyStopped++;
061                        }
062    
063                        if (configurationManager.isLoaded(moduleID)) {
064                            configurationManager.unloadConfiguration(moduleID);
065                        }
066    
067                        if (lcresult != null) {
068                            java.util.Iterator iterator = lcresult.getStopped().iterator();
069                            while (iterator.hasNext()) {
070                                Artifact config = (Artifact)iterator.next();
071                                if (!config.toString().equals(module.getModuleID())) {
072                                    //TODO might be a hack
073                                    List kidsChild = loadChildren(kernel, config.toString());
074                                    //this.updateStatus("printing kidsChild="+kidsChild);
075                                    //this.updateStatus("printing config="+config.toString());
076                                    // Build a response obect containg the started configuration and a list of it's contained modules
077                                    TargetModuleIDImpl idChild = new TargetModuleIDImpl(null, config.toString(),
078                                            (String[]) kidsChild.toArray(new String[kidsChild.size()]));
079                                    if (isWebApp(kernel, config.toString())) {
080                                        idChild.setType(ModuleType.WAR);
081                                    }
082                                    if (idChild.getChildTargetModuleID() != null) {
083                                        for (int k = 0; k < idChild.getChildTargetModuleID().length; k++) {
084                                            TargetModuleIDImpl child = (TargetModuleIDImpl) idChild.getChildTargetModuleID()[k];
085                                            if (isWebApp(kernel, child.getModuleID())) {
086                                                child.setType(ModuleType.WAR);
087                                            }
088                                        }
089                                    }
090                                    addModule(idChild);
091                                }
092                            }
093                        }
094                    }
095                } finally {
096                    ConfigurationUtil.releaseConfigurationManager(kernel, configurationManager);
097                }
098                if ((getModuleCount() + alreadyStopped) < modules.length) {
099                    fail("Some modules could not be stopped");
100                } else {
101                    complete("Completed");
102                }
103            } catch (Exception e) {
104                doFail(e);
105            }
106        }
107    }