View Javadoc

1   /**
2    *
3    *  Licensed to the Apache Software Foundation (ASF) under one or more
4    *  contributor license agreements.  See the NOTICE file distributed with
5    *  this work for additional information regarding copyright ownership.
6    *  The ASF licenses this file to You under the Apache License, Version 2.0
7    *  (the "License"); you may not use this file except in compliance with
8    *  the License.  You may obtain a copy of the License at
9    *
10   *     http://www.apache.org/licenses/LICENSE-2.0
11   *
12   *  Unless required by applicable law or agreed to in writing, software
13   *  distributed under the License is distributed on an "AS IS" BASIS,
14   *  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15   *  See the License for the specific language governing permissions and
16   *  limitations under the License.
17   */
18  package org.apache.geronimo.deployment.plugin.local;
19  
20  import java.net.URI;
21  import javax.enterprise.deploy.shared.CommandType;
22  import javax.enterprise.deploy.spi.TargetModuleID;
23  
24  import org.apache.geronimo.deployment.plugin.TargetModuleIDImpl;
25  import org.apache.geronimo.kernel.InternalKernelException;
26  import org.apache.geronimo.kernel.Kernel;
27  import org.apache.geronimo.kernel.config.ConfigurationManager;
28  import org.apache.geronimo.kernel.config.ConfigurationUtil;
29  import org.apache.geronimo.kernel.config.NoSuchConfigException;
30  import org.apache.geronimo.kernel.repository.Artifact;
31  
32  /**
33   * @version $Rev: 470597 $ $Date: 2006-11-02 15:30:55 -0800 (Thu, 02 Nov 2006) $
34   */
35  public class UndeployCommand extends CommandSupport {
36      private static final String[] UNINSTALL_SIG = {URI.class.getName()};
37      private final Kernel kernel;
38      private final TargetModuleID[] modules;
39  
40      public UndeployCommand(Kernel kernel, TargetModuleID modules[]) {
41          super(CommandType.UNDEPLOY);
42          this.kernel = kernel;
43          this.modules = modules;
44      }
45  
46      public void run() {
47          try {
48              ConfigurationManager configurationManager = ConfigurationUtil.getConfigurationManager(kernel);
49              try {
50                  for (int i = 0; i < modules.length; i++) {
51                      TargetModuleIDImpl module = (TargetModuleIDImpl) modules[i];
52  
53                      Artifact moduleID = Artifact.create(module.getModuleID());
54                      try {
55                      	if(!configurationManager.isOnline()) {
56                      		//If an offline undeploy, need to load the configuration first, so that stopConfiguration()
57                      		//can resolve the configuration and successfully set load=false in attribute manager, otherwise
58                      		//starting the server will fail attempting to start an config that does not exist.
59                      		configurationManager.loadConfiguration(moduleID);
60                      	}
61                      	
62                          configurationManager.stopConfiguration(moduleID);
63  
64  
65                          configurationManager.unloadConfiguration(moduleID);
66                          updateStatus("Module " + moduleID + " unloaded.");
67                      } catch (InternalKernelException e) {
68                          // this is cause by the kernel being already shutdown
69                      } catch (NoSuchConfigException e) {
70                          // module was already unloaded - just continue
71                      }
72  
73                      try {
74                          configurationManager.uninstallConfiguration(moduleID);
75                          updateStatus("Module " + moduleID + " uninstalled.");
76                          addModule(module);
77                      } catch (NoSuchConfigException e) {
78                          // module was already undeployed - just continue
79                      }
80                  }
81              } finally {
82                  ConfigurationUtil.releaseConfigurationManager(kernel, configurationManager);
83              }
84  
85              //todo: this will probably never happen because the command line args are compared to actual modules
86              if (getModuleCount() < modules.length) {
87                  updateStatus("Some of the modules to undeploy were not previously deployed.  This is not treated as an error.");
88              }
89              complete("Completed");
90          } catch (Exception e) {
91              doFail(e);
92          }
93      }
94  }