001 /**
002 *
003 * Copyright 2003-2004 The Apache Software Foundation
004 *
005 * Licensed under the Apache License, Version 2.0 (the "License");
006 * you may not use this file except in compliance with the License.
007 * 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.cli;
019
020 import org.apache.geronimo.common.DeploymentException;
021 import org.apache.geronimo.kernel.repository.Artifact;
022
023 import javax.enterprise.deploy.spi.DeploymentManager;
024 import javax.enterprise.deploy.spi.Target;
025 import javax.enterprise.deploy.spi.TargetModuleID;
026 import javax.enterprise.deploy.spi.exceptions.TargetException;
027 import javax.enterprise.deploy.spi.status.ProgressObject;
028 import java.io.File;
029 import java.io.IOException;
030 import java.io.PrintWriter;
031 import java.util.ArrayList;
032 import java.util.List;
033
034 /**
035 * The CLI deployer logic to redeploy.
036 *
037 * @version $Rev: 406550 $ $Date: 2006-05-14 23:19:38 -0700 (Sun, 14 May 2006) $
038 */
039 public class CommandRedeploy extends AbstractCommand {
040 public CommandRedeploy() {
041 super("redeploy", "1. Common Commands", "[module] [plan] [ModuleID|TargetModuleID+]",
042 "A shortcut to undeploy a module from one or more servers, then " +
043 "deploy a new version. This is not a smooth cutover -- some client " +
044 "requests may be rejected while the redeploy takes place.\n" +
045 "Normally both a module and plan are passed to the deployer. " +
046 "Sometimes the module contains a plan, or requires no plan, in which case " +
047 "the plan may be omitted. Sometimes the plan references a module already " +
048 "deployed in the Geronimo server environment, in which case a module does " +
049 "not need to be provided.\n" +
050 "If more than one TargetModuleID is provided, all TargetModuleIDs " +
051 "must refer to the same module (just running on different targets).\n" +
052 "Regardless of whether the old module was running or not, the new " +
053 "module will be started.\n" +
054 "If no ModuleID or TargetModuleID is specified, and you're deploying to "+
055 "Geronimo, the deployer will attempt to guess the correct ModuleID for "+
056 "you based on the module and/or plan you provided.\n"+
057 "Note: To specify a TargetModuleID, use the form TargetName|ModuleName");
058 }
059
060 public void execute(PrintWriter out, ServerConnection connection, String[] args) throws DeploymentException {
061 setOut(out);
062 if(args.length == 0) {
063 throw new DeploymentSyntaxException("Must specify a module or plan (or both) and optionally module IDs to replace");
064 }
065 DeploymentManager mgr = connection.getDeploymentManager();
066 Target[] allTargets = mgr.getTargets();
067 TargetModuleID[] allModules;
068 try {
069 allModules = mgr.getAvailableModules(null, allTargets);
070 } catch(TargetException e) {
071 throw new DeploymentException("Unable to load modules from server", e);
072 }
073 List modules = new ArrayList();
074 File module = null;
075 File plan = null;
076 File test = new File(args[0]); // Guess whether the first argument is a module or a plan
077 if(!test.exists()) {
078 throw new DeploymentSyntaxException("Must specify a module or plan (or both) and optionally module IDs to replace");
079 }
080 if(!test.canRead()) {
081 throw new DeploymentException("Cannot read file "+test.getAbsolutePath());
082 }
083 if(DeployUtils.isJarFile(test) || test.isDirectory()) {
084 module = test;
085 } else {
086 plan = test;
087 }
088 if(args.length > 1) { // Guess whether the second argument is a module, plan, ModuleID, or TargetModuleID
089 test = new File(args[1]);
090 if(test.exists() && test.canRead() && !args[1].equals(args[0])) {
091 if(DeployUtils.isJarFile(test) || test.isDirectory()) {
092 if(module != null) {
093 throw new DeploymentSyntaxException("Module and plan cannot both be JAR files or directories!");
094 }
095 module = test;
096 } else {
097 if(plan != null) {
098 throw new DeploymentSyntaxException("Module or plan must be a JAR file or directory!");
099 }
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 }