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    package org.apache.geronimo.deployment.plugin.local;
018    
019    import java.net.URI;
020    import javax.enterprise.deploy.shared.CommandType;
021    import javax.enterprise.deploy.spi.TargetModuleID;
022    
023    import org.apache.geronimo.deployment.plugin.TargetModuleIDImpl;
024    import org.apache.geronimo.kernel.InternalKernelException;
025    import org.apache.geronimo.kernel.Kernel;
026    import org.apache.geronimo.kernel.config.ConfigurationManager;
027    import org.apache.geronimo.kernel.config.ConfigurationUtil;
028    import org.apache.geronimo.kernel.config.NoSuchConfigException;
029    import org.apache.geronimo.kernel.repository.Artifact;
030    
031    /**
032     * @version $Rev: 476049 $ $Date: 2006-11-16 23:35:17 -0500 (Thu, 16 Nov 2006) $
033     */
034    public class UndeployCommand extends CommandSupport {
035        private static final String[] UNINSTALL_SIG = {URI.class.getName()};
036        private final Kernel kernel;
037        private final TargetModuleID[] modules;
038    
039        public UndeployCommand(Kernel kernel, TargetModuleID modules[]) {
040            super(CommandType.UNDEPLOY);
041            this.kernel = kernel;
042            this.modules = modules;
043        }
044    
045        public void run() {
046            try {
047                ConfigurationManager configurationManager = ConfigurationUtil.getConfigurationManager(kernel);
048                try {
049                    for (int i = 0; i < modules.length; i++) {
050                        TargetModuleIDImpl module = (TargetModuleIDImpl) modules[i];
051    
052                        Artifact moduleID = Artifact.create(module.getModuleID());
053                        try {
054                            if(!configurationManager.isOnline()) {
055                                    //If an offline undeploy, need to load the configuration first, so that stopConfiguration()
056                                    //can resolve the configuration and successfully set load=false in attribute manager, otherwise
057                                    //starting the server will fail attempting to start an config that does not exist.
058                                    configurationManager.loadConfiguration(moduleID);
059                            }
060                            
061                            configurationManager.stopConfiguration(moduleID);
062    
063    
064                            configurationManager.unloadConfiguration(moduleID);
065                            updateStatus("Module " + moduleID + " unloaded.");
066                        } catch (InternalKernelException e) {
067                            // this is cause by the kernel being already shutdown
068                        } catch (NoSuchConfigException e) {
069                            // module was already unloaded - just continue
070                        }
071    
072                        try {
073                            configurationManager.uninstallConfiguration(moduleID);
074                            updateStatus("Module " + moduleID + " uninstalled.");
075                            addModule(module);
076                        } catch (NoSuchConfigException e) {
077                            // module was already undeployed - just continue
078                        }
079                    }
080                } finally {
081                    ConfigurationUtil.releaseConfigurationManager(kernel, configurationManager);
082                }
083    
084                //todo: this will probably never happen because the command line args are compared to actual modules
085                if (getModuleCount() < modules.length) {
086                    updateStatus("Some of the modules to undeploy were not previously deployed.  This is not treated as an error.");
087                }
088                complete("Completed");
089            } catch (Exception e) {
090                doFail(e);
091            }
092        }
093    }