001    /**
002     *
003     *  Licensed to the Apache Software Foundation (ASF) under one or more
004     *  contributor license agreements.  See the NOTICE file distributed with
005     *  this work for additional information regarding copyright ownership.
006     *  The ASF licenses this file to You under the Apache License, Version 2.0
007     *  (the "License"); you may not use this file except in compliance with
008     *  the License.  You may obtain a copy of the License at
009     *
010     *     http://www.apache.org/licenses/LICENSE-2.0
011     *
012     *  Unless required by applicable law or agreed to in writing, software
013     *  distributed under the License is distributed on an "AS IS" BASIS,
014     *  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
015     *  See the License for the specific language governing permissions and
016     *  limitations under the License.
017     */
018    package org.apache.geronimo.deployment.plugin.local;
019    
020    import java.net.URI;
021    import javax.enterprise.deploy.shared.CommandType;
022    import javax.enterprise.deploy.spi.TargetModuleID;
023    
024    import org.apache.geronimo.deployment.plugin.TargetModuleIDImpl;
025    import org.apache.geronimo.kernel.InternalKernelException;
026    import org.apache.geronimo.kernel.Kernel;
027    import org.apache.geronimo.kernel.config.ConfigurationManager;
028    import org.apache.geronimo.kernel.config.ConfigurationUtil;
029    import org.apache.geronimo.kernel.config.NoSuchConfigException;
030    import org.apache.geronimo.kernel.repository.Artifact;
031    
032    /**
033     * @version $Rev: 470597 $ $Date: 2006-11-02 15:30:55 -0800 (Thu, 02 Nov 2006) $
034     */
035    public class UndeployCommand extends CommandSupport {
036        private static final String[] UNINSTALL_SIG = {URI.class.getName()};
037        private final Kernel kernel;
038        private final TargetModuleID[] modules;
039    
040        public UndeployCommand(Kernel kernel, TargetModuleID modules[]) {
041            super(CommandType.UNDEPLOY);
042            this.kernel = kernel;
043            this.modules = modules;
044        }
045    
046        public void run() {
047            try {
048                ConfigurationManager configurationManager = ConfigurationUtil.getConfigurationManager(kernel);
049                try {
050                    for (int i = 0; i < modules.length; i++) {
051                        TargetModuleIDImpl module = (TargetModuleIDImpl) modules[i];
052    
053                        Artifact moduleID = Artifact.create(module.getModuleID());
054                        try {
055                            if(!configurationManager.isOnline()) {
056                                    //If an offline undeploy, need to load the configuration first, so that stopConfiguration()
057                                    //can resolve the configuration and successfully set load=false in attribute manager, otherwise
058                                    //starting the server will fail attempting to start an config that does not exist.
059                                    configurationManager.loadConfiguration(moduleID);
060                            }
061                            
062                            configurationManager.stopConfiguration(moduleID);
063    
064    
065                            configurationManager.unloadConfiguration(moduleID);
066                            updateStatus("Module " + moduleID + " unloaded.");
067                        } catch (InternalKernelException e) {
068                            // this is cause by the kernel being already shutdown
069                        } catch (NoSuchConfigException e) {
070                            // module was already unloaded - just continue
071                        }
072    
073                        try {
074                            configurationManager.uninstallConfiguration(moduleID);
075                            updateStatus("Module " + moduleID + " uninstalled.");
076                            addModule(module);
077                        } catch (NoSuchConfigException e) {
078                            // module was already undeployed - just continue
079                        }
080                    }
081                } finally {
082                    ConfigurationUtil.releaseConfigurationManager(kernel, configurationManager);
083                }
084    
085                //todo: this will probably never happen because the command line args are compared to actual modules
086                if (getModuleCount() < modules.length) {
087                    updateStatus("Some of the modules to undeploy were not previously deployed.  This is not treated as an error.");
088                }
089                complete("Completed");
090            } catch (Exception e) {
091                doFail(e);
092            }
093        }
094    }