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