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
018 package org.apache.geronimo.deployment.cli;
019
020 import java.io.File;
021 import java.io.IOException;
022 import java.util.ArrayList;
023 import java.util.List;
024
025 import javax.enterprise.deploy.spi.DeploymentManager;
026 import javax.enterprise.deploy.spi.Target;
027 import javax.enterprise.deploy.spi.TargetModuleID;
028 import javax.enterprise.deploy.spi.exceptions.TargetException;
029 import javax.enterprise.deploy.spi.status.ProgressObject;
030
031 import org.apache.geronimo.cli.deployer.CommandArgs;
032 import org.apache.geronimo.common.DeploymentException;
033 import org.apache.geronimo.kernel.repository.Artifact;
034 import jline.ConsoleReader;
035
036 /**
037 * The CLI deployer logic to redeploy.
038 *
039 * @version $Rev: 706640 $ $Date: 2008-10-21 14:44:05 +0000 (Tue, 21 Oct 2008) $
040 */
041 public class CommandRedeploy extends AbstractCommand {
042
043 public void execute(ConsoleReader consoleReader, ServerConnection connection, CommandArgs commandArgs) throws DeploymentException {
044 ProgressObject po = null;
045 try {
046 String[] args = commandArgs.getArgs();
047
048 if (args.length == 0) {
049 throw new DeploymentSyntaxException("Must specify a module or plan (or both) and optionally module IDs to replace");
050 }
051
052 DeploymentManager mgr = connection.getDeploymentManager();
053 Target[] allTargets = mgr.getTargets();
054 TargetModuleID[] allModules;
055 try {
056 allModules = mgr.getAvailableModules(null, allTargets);
057 } catch(TargetException e) {
058 throw new DeploymentException("Unable to load modules from server", e);
059 }
060
061 List modules = new ArrayList();
062 File module = null;
063 File plan = null;
064 File test = new File(args[0]); // Guess whether the first argument is a module or a plan
065 if(!test.exists()) {
066 throw new DeploymentSyntaxException("Module or plan file does not exist: " + test.getAbsolutePath());
067 }
068 if(!test.canRead()) {
069 throw new DeploymentException("Cannot read file "+test.getAbsolutePath());
070 }
071 if(DeployUtils.isJarFile(test) || test.isDirectory()) {
072 module = test;
073 } else {
074 plan = test;
075 }
076 if(args.length > 1) { // Guess whether the second argument is a module, plan, ModuleID, or TargetModuleID
077 test = new File(args[1]);
078 if(test.exists() && test.canRead() && !args[1].equals(args[0])) {
079 if(DeployUtils.isJarFile(test) || test.isDirectory()) {
080 if(module != null) {
081 throw new DeploymentSyntaxException("Module and plan cannot both be JAR files or directories!");
082 }
083 module = test;
084 } else {
085 if(plan != null) {
086 throw new DeploymentSyntaxException("Module or plan must be a JAR file or directory!");
087 }
088 plan = test;
089 }
090 } else {
091 modules.addAll(DeployUtils.identifyTargetModuleIDs(allModules, args[1], false));
092 }
093 }
094 for(int i=2; i<args.length; i++) { // Any arguments beyond 2 must be a ModuleID or TargetModuleID
095 modules.addAll(DeployUtils.identifyTargetModuleIDs(allModules, args[i], false));
096 }
097 // If we don't have any moduleIDs, try to guess one.
098 if(modules.size() == 0 && connection.isGeronimo()) {
099 emit(consoleReader, "No ModuleID or TargetModuleID provided. Attempting to guess based on the content of the "+(plan == null ? "archive" : "plan")+".");
100 String moduleId = null;
101 try {
102 if(plan != null) {
103 moduleId = DeployUtils.extractModuleIdFromPlan(plan);
104 if(moduleId == null) { // plan just doesn't have a config ID
105 String fileName = module == null ? plan.getName() : module.getName();
106 int pos = fileName.lastIndexOf('.');
107 String artifactId = pos > -1 ? module.getName().substring(0, pos) : module.getName();
108 moduleId = Artifact.DEFAULT_GROUP_ID+"/"+artifactId+"//";
109 emit(consoleReader, "Unable to locate Geronimo deployment plan in archive. Calculating default ModuleID from archive name.");
110 }
111 } else if(module != null) {
112 moduleId = DeployUtils.extractModuleIdFromArchive(module);
113 if(moduleId == null) {
114 int pos = module.getName().lastIndexOf('.');
115 String artifactId = pos > -1 ? module.getName().substring(0, pos) : module.getName();
116 moduleId = Artifact.DEFAULT_GROUP_ID+"/"+artifactId+"//";
117 emit(consoleReader, "Unable to locate Geronimo deployment plan in archive. Calculating default ModuleID from archive name.");
118 }
119 }
120 } catch (IOException e) {
121 throw new DeploymentException("Unable to read input files: "+e.getMessage(), e);
122 }
123 if(moduleId != null) {
124 emit(consoleReader, "Attempting to use ModuleID '"+moduleId+"'");
125 modules.addAll(DeployUtils.identifyTargetModuleIDs(allModules, moduleId, true));
126 } else {
127 emit(consoleReader, "Unable to calculate a ModuleID from supplied module and/or plan.");
128 }
129 }
130 if(modules.size() == 0) { // Either not deploying to Geronimo or unable to identify modules
131 throw new DeploymentSyntaxException("No ModuleID or TargetModuleID available. Nothing to do. Maybe you should add a ModuleID or TargetModuleID to the command line?");
132 }
133 if(module != null) {
134 module = module.getAbsoluteFile();
135 }
136 if(plan != null) {
137 plan = plan.getAbsoluteFile();
138 }
139 // Now that we've sorted out all the arguments, do the work
140 TargetModuleID[] ids = (TargetModuleID[]) modules.toArray(new TargetModuleID[modules.size()]);
141 boolean multiple = isMultipleTargets(ids);
142 po = mgr.redeploy(ids, module, plan);
143 waitForProgress(consoleReader, po);
144 TargetModuleID[] done = po.getResultTargetModuleIDs();
145 for(int i = 0; i < done.length; i++) {
146 TargetModuleID id = done[i];
147 emit(consoleReader, "Redeployed "+id.getModuleID()+(multiple ? " on "+id.getTarget().getName() : "")+(id.getWebURL() == null ? "" : " @ "+id.getWebURL()));
148 if(id.getChildTargetModuleID() != null) {
149 for (int j = 0; j < id.getChildTargetModuleID().length; j++) {
150 TargetModuleID child = id.getChildTargetModuleID()[j];
151 emit(consoleReader, " `-> "+child.getModuleID()+(child.getWebURL() == null ? "" : " @ "+child.getWebURL()));
152 }
153 }
154 }
155 } catch (IOException e) {
156 throw new DeploymentException("Could not write to console", e);
157 }
158 if(po.getDeploymentStatus().isFailed()) {
159 throw new DeploymentException("Operation failed: "+po.getDeploymentStatus().getMessage());
160 }
161 }
162
163 }