KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > geronimo > deployment > cli > CommandRedeploy


1 /**
2  * Licensed to the Apache Software Foundation (ASF) under one or more
3  * contributor license agreements. See the NOTICE file distributed with
4  * this work for additional information regarding copyright ownership.
5  * The ASF licenses this file to You under the Apache License, Version 2.0
6  * (the "License"); you may not use this file except in compliance with
7  * the License. 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 JavaDoc;
24 import javax.enterprise.deploy.spi.Target JavaDoc;
25 import javax.enterprise.deploy.spi.TargetModuleID JavaDoc;
26 import javax.enterprise.deploy.spi.exceptions.TargetException JavaDoc;
27 import javax.enterprise.deploy.spi.status.ProgressObject JavaDoc;
28 import java.io.File JavaDoc;
29 import java.io.IOException JavaDoc;
30 import java.io.PrintWriter JavaDoc;
31 import java.util.ArrayList JavaDoc;
32 import java.util.List JavaDoc;
33
34 /**
35  * The CLI deployer logic to redeploy.
36  *
37  * @version $Rev: 476049 $ $Date: 2006-11-16 23:35:17 -0500 (Thu, 16 Nov 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 JavaDoc out, ServerConnection connection, String JavaDoc[] 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 JavaDoc mgr = connection.getDeploymentManager();
66         Target JavaDoc[] allTargets = mgr.getTargets();
67         TargetModuleID JavaDoc[] allModules;
68         try {
69             allModules = mgr.getAvailableModules(null, allTargets);
70         } catch(TargetException JavaDoc e) {
71             throw new DeploymentException("Unable to load modules from server", e);
72         }
73         List JavaDoc modules = new ArrayList JavaDoc();
74         File JavaDoc module = null;
75         File JavaDoc plan = null;
76         File JavaDoc test = new File JavaDoc(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 JavaDoc(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 JavaDoc 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 JavaDoc fileName = module == null ? plan.getName() : module.getName();
118                         int pos = fileName.lastIndexOf('.');
119                         String JavaDoc 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 JavaDoc 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 JavaDoc 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 JavaDoc[] ids = (TargetModuleID JavaDoc[]) modules.toArray(new TargetModuleID JavaDoc[modules.size()]);
153         boolean multiple = isMultipleTargets(ids);
154         ProgressObject JavaDoc po = mgr.redeploy(ids, module, plan);
155         waitForProgress(out, po);
156         TargetModuleID JavaDoc[] done = po.getResultTargetModuleIDs();
157         for(int i = 0; i < done.length; i++) {
158             TargetModuleID JavaDoc 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 JavaDoc 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 }
173
Popular Tags