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