View Javadoc

1   /**
2    *
3    * Copyright 2003-2004 The Apache Software Foundation
4    *
5    *  Licensed under the Apache License, Version 2.0 (the "License");
6    *  you may not use this file except in compliance with the License.
7    *  You may obtain a copy of the License at
8    *
9    *     http://www.apache.org/licenses/LICENSE-2.0
10   *
11   *  Unless required by applicable law or agreed to in writing, software
12   *  distributed under the License is distributed on an "AS IS" BASIS,
13   *  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14   *  See the License for the specific language governing permissions and
15   *  limitations under the License.
16   */
17  
18  package org.apache.geronimo.deployment.cli;
19  
20  import org.apache.geronimo.common.DeploymentException;
21  import org.apache.geronimo.kernel.repository.Artifact;
22  
23  import javax.enterprise.deploy.spi.DeploymentManager;
24  import javax.enterprise.deploy.spi.Target;
25  import javax.enterprise.deploy.spi.TargetModuleID;
26  import javax.enterprise.deploy.spi.exceptions.TargetException;
27  import javax.enterprise.deploy.spi.status.ProgressObject;
28  import java.io.File;
29  import java.io.IOException;
30  import java.io.PrintWriter;
31  import java.util.ArrayList;
32  import java.util.List;
33  
34  /**
35   * The CLI deployer logic to redeploy.
36   *
37   * @version $Rev: 406550 $ $Date: 2006-05-14 23:19:38 -0700 (Sun, 14 May 2006) $
38   */
39  public class CommandRedeploy extends AbstractCommand {
40      public CommandRedeploy() {
41          super("redeploy", "1. Common Commands", "[module] [plan] [ModuleID|TargetModuleID+]",
42                  "A shortcut to undeploy a module from one or more servers, then " +
43                  "deploy a new version.  This is not a smooth cutover -- some client " +
44                  "requests may be rejected while the redeploy takes place.\n" +
45                  "Normally both a module and plan are passed to the deployer. " +
46                  "Sometimes the module contains a plan, or requires no plan, in which case " +
47                  "the plan may be omitted.  Sometimes the plan references a module already " +
48                  "deployed in the Geronimo server environment, in which case a module does " +
49                  "not need to be provided.\n" +
50                  "If more than one TargetModuleID is provided, all TargetModuleIDs " +
51                  "must refer to the same module (just running on different targets).\n" +
52                  "Regardless of whether the old module was running or not, the new " +
53                  "module will be started.\n" +
54                  "If no ModuleID or TargetModuleID is specified, and you're deploying to "+
55                  "Geronimo, the deployer will attempt to guess the correct ModuleID for "+
56                  "you based on the module and/or plan you provided.\n"+
57                  "Note: To specify a TargetModuleID, use the form TargetName|ModuleName");
58      }
59  
60      public void execute(PrintWriter out, ServerConnection connection, String[] args) throws DeploymentException {
61          setOut(out);
62          if(args.length == 0) {
63              throw new DeploymentSyntaxException("Must specify a module or plan (or both) and optionally module IDs to replace");
64          }
65          DeploymentManager mgr = connection.getDeploymentManager();
66          Target[] allTargets = mgr.getTargets();
67          TargetModuleID[] allModules;
68          try {
69              allModules = mgr.getAvailableModules(null, allTargets);
70          } catch(TargetException e) {
71              throw new DeploymentException("Unable to load modules from server", e);
72          }
73          List modules = new ArrayList();
74          File module = null;
75          File plan = null;
76          File test = new File(args[0]); // Guess whether the first argument is a module or a plan
77          if(!test.exists()) {
78              throw new DeploymentSyntaxException("Must specify a module or plan (or both) and optionally module IDs to replace");
79          }
80          if(!test.canRead()) {
81              throw new DeploymentException("Cannot read file "+test.getAbsolutePath());
82          }
83          if(DeployUtils.isJarFile(test) || test.isDirectory()) {
84              module = test;
85          } else {
86              plan = test;
87          }
88          if(args.length > 1) { // Guess whether the second argument is a module, plan, ModuleID, or TargetModuleID
89              test = new File(args[1]);
90              if(test.exists() && test.canRead() && !args[1].equals(args[0])) {
91                  if(DeployUtils.isJarFile(test) || test.isDirectory()) {
92                      if(module != null) {
93                          throw new DeploymentSyntaxException("Module and plan cannot both be JAR files or directories!");
94                      }
95                      module = test;
96                  } else {
97                      if(plan != null) {
98                          throw new DeploymentSyntaxException("Module or plan must be a JAR file or directory!");
99                      }
100                     plan = test;
101                 }
102             } else {
103                 modules.addAll(DeployUtils.identifyTargetModuleIDs(allModules, args[1], false));
104             }
105         }
106         for(int i=2; i<args.length; i++) { // Any arguments beyond 2 must be a ModuleID or TargetModuleID
107             modules.addAll(DeployUtils.identifyTargetModuleIDs(allModules, args[i], false));
108         }
109         // If we don't have any moduleIDs, try to guess one.
110         if(modules.size() == 0 && connection.isGeronimo()) {
111             emit("No ModuleID or TargetModuleID provided.  Attempting to guess based on the content of the "+(plan == null ? "archive" : "plan")+".");
112             String moduleId = null;
113             try {
114                 if(plan != null) {
115                     moduleId = DeployUtils.extractModuleIdFromPlan(plan);
116                     if(moduleId == null) { // plan just doesn't have a config ID
117                         String fileName = module == null ? plan.getName() : module.getName();
118                         int pos = fileName.lastIndexOf('.');
119                         String artifactId = pos > -1 ? module.getName().substring(0, pos) : module.getName();
120                         moduleId = Artifact.DEFAULT_GROUP_ID+"/"+artifactId+"//";
121                         emit("Unable to locate Geronimo deployment plan in archive.  Calculating default ModuleID from archive name.");
122                     }
123                 } else if(module != null) {
124                     moduleId = DeployUtils.extractModuleIdFromArchive(module);
125                     if(moduleId == null) {
126                         int pos = module.getName().lastIndexOf('.');
127                         String artifactId = pos > -1 ? module.getName().substring(0, pos) : module.getName();
128                         moduleId = Artifact.DEFAULT_GROUP_ID+"/"+artifactId+"//";
129                         emit("Unable to locate Geronimo deployment plan in archive.  Calculating default ModuleID from archive name.");
130                     }
131                 }
132             } catch (IOException e) {
133                 throw new DeploymentException("Unable to read input files: "+e.getMessage());
134             }
135             if(moduleId != null) {
136                 emit("Attempting to use ModuleID '"+moduleId+"'");
137                 modules.addAll(DeployUtils.identifyTargetModuleIDs(allModules, moduleId, true));
138             } else {
139                 emit("Unable to calculate a ModuleID from supplied module and/or plan.");
140             }
141         }
142         if(modules.size() == 0) { // Either not deploying to Geronimo or unable to identify modules
143             throw new DeploymentSyntaxException("No ModuleID or TargetModuleID available.  Nothing to do.  Maybe you should add a ModuleID or TargetModuleID to the command line?");
144         }
145         if(module != null) {
146             module = module.getAbsoluteFile();
147         }
148         if(plan != null) {
149             plan = plan.getAbsoluteFile();
150         }
151         // Now that we've sorted out all the arguments, do the work
152         TargetModuleID[] ids = (TargetModuleID[]) modules.toArray(new TargetModuleID[modules.size()]);
153         boolean multiple = isMultipleTargets(ids);
154         ProgressObject po = mgr.redeploy(ids, module, plan);
155         waitForProgress(out, po);
156         TargetModuleID[] done = po.getResultTargetModuleIDs();
157         for(int i = 0; i < done.length; i++) {
158             TargetModuleID id = done[i];
159             emit("Redeployed "+id.getModuleID()+(multiple ? " on "+id.getTarget().getName() : "")+(id.getWebURL() == null ? "" : " @ "+id.getWebURL()));
160             if(id.getChildTargetModuleID() != null) {
161                 for (int j = 0; j < id.getChildTargetModuleID().length; j++) {
162                     TargetModuleID child = id.getChildTargetModuleID()[j];
163                     emit("  `-> "+child.getModuleID()+(child.getWebURL() == null ? "" : " @ "+child.getWebURL()));
164                 }
165             }
166         }
167         if(po.getDeploymentStatus().isFailed()) {
168             throw new DeploymentException("Operation failed: "+po.getDeploymentStatus().getMessage());
169         }
170     }
171 
172 }