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.PrintWriter;
020: import java.io.IOException;
021: import java.util.Arrays;
022: import java.util.List;
023: import java.util.StringTokenizer;
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.status.ProgressObject;
029:
030: import org.apache.geronimo.cli.deployer.CommandArgs;
031: import org.apache.geronimo.cli.deployer.DistributeCommandArgs;
032: import org.apache.geronimo.common.DeploymentException;
033: import org.apache.geronimo.deployment.plugin.jmx.JMXDeploymentManager;
034: import jline.ConsoleReader;
035:
036: /**
037: * The CLI deployer logic to distribute.
038: *
039: * @version $Rev: 596987 $ $Date: 2007-11-21 00:39:04 -0800 (Wed, 21 Nov 2007) $
040: */
041: public class CommandDistribute extends AbstractCommand {
042:
043: protected ProgressObject runCommand(DeploymentManager mgr,
044: ConsoleReader out, boolean inPlace, Target[] tlist,
045: File module, File plan) throws DeploymentException {
046: if (inPlace) {
047: if (!(mgr instanceof JMXDeploymentManager)) {
048: throw new DeploymentSyntaxException(
049: "Target DeploymentManager is not a Geronimo one. \n"
050: + "Cannot perform in-place deployment.");
051: }
052: JMXDeploymentManager jmxMgr = (JMXDeploymentManager) mgr;
053: try {
054: jmxMgr.setInPlace(true);
055: return mgr.distribute(tlist, module, plan);
056: } finally {
057: jmxMgr.setInPlace(false);
058: }
059: } else {
060: return mgr.distribute(tlist, module, plan);
061: }
062: }
063:
064: protected String getAction() {
065: return "Distributed";
066: }
067:
068: public void execute(ConsoleReader consoleReader,
069: ServerConnection connection, CommandArgs commandArgs)
070: throws DeploymentException {
071: if (!(commandArgs instanceof DistributeCommandArgs)) {
072: throw new DeploymentSyntaxException(
073: "CommandArgs has the type ["
074: + commandArgs.getClass() + "]; expected ["
075: + DistributeCommandArgs.class + "]");
076: }
077: DistributeCommandArgs distributeCommandArgs = (DistributeCommandArgs) commandArgs;
078:
079: BooleanHolder inPlaceHolder = new BooleanHolder();
080: inPlaceHolder.inPlace = distributeCommandArgs.isInPlace();
081:
082: List<String> targets = Arrays.asList(distributeCommandArgs
083: .getTargets());
084:
085: String[] args = distributeCommandArgs.getArgs();
086: File module = null;
087: File plan = null;
088: if (args.length > 0) {
089: File test = new File(args[0]);
090: if (DeployUtils.isJarFile(test) || test.isDirectory()) {
091: if (module != null) {
092: throw new DeploymentSyntaxException(
093: "Module and plan cannot both be JAR files or directories!");
094: }
095: module = test;
096: } else {
097: if (plan != null) {
098: throw new DeploymentSyntaxException(
099: "Module or plan must be a JAR file or directory!");
100: }
101: plan = test;
102: }
103: }
104: if (args.length > 1) {
105: File test = new File(args[1]);
106: if (DeployUtils.isJarFile(test) || test.isDirectory()) {
107: if (module != null) {
108: throw new DeploymentSyntaxException(
109: "Module and plan cannot both be JAR files or directories!");
110: }
111: module = test;
112: } else {
113: if (plan != null) {
114: throw new DeploymentSyntaxException(
115: "Module or plan must be a JAR file or directory!");
116: }
117: plan = test;
118: }
119: }
120: if (module != null) {
121: module = module.getAbsoluteFile();
122: }
123: if (plan != null) {
124: plan = plan.getAbsoluteFile();
125: }
126: try {
127: executeOnline(connection, inPlaceHolder.inPlace, targets,
128: consoleReader, module, plan);
129: } catch (IOException e) {
130: throw new DeploymentException("Could not write to output",
131: e);
132: }
133: }
134:
135: private void executeOnline(ServerConnection connection,
136: boolean inPlace, List targets, ConsoleReader out,
137: File module, File plan) throws DeploymentException,
138: IOException {
139: final DeploymentManager mgr = connection.getDeploymentManager();
140: TargetModuleID[] results;
141: boolean multipleTargets;
142: ProgressObject po;
143: if (targets.size() > 0) {
144: Target[] tlist = identifyTargets(targets, mgr);
145: multipleTargets = tlist.length > 1;
146: po = runCommand(mgr, out, inPlace, tlist, module, plan);
147: waitForProgress(out, po);
148: } else {
149: Target[] tlist = mgr.getTargets();
150: if (null == tlist) {
151: throw new IllegalStateException(
152: "No target to distribute to");
153: }
154: tlist = new Target[] { tlist[0] };
155:
156: multipleTargets = tlist.length > 1;
157: po = runCommand(mgr, out, inPlace, tlist, module, plan);
158: waitForProgress(out, po);
159: }
160:
161: // print the results that succeeded
162: results = po.getResultTargetModuleIDs();
163: for (int i = 0; i < results.length; i++) {
164: TargetModuleID result = results[i];
165: out.printString(DeployUtils.reformat(getAction()
166: + " "
167: + result.getModuleID()
168: + (multipleTargets ? " to "
169: + result.getTarget().getName() : "")
170: + (result.getWebURL() == null
171: || !getAction().equals("Deployed") ? ""
172: : " @ " + result.getWebURL()), 4, 72));
173: if (result.getChildTargetModuleID() != null) {
174: for (int j = 0; j < result.getChildTargetModuleID().length; j++) {
175: TargetModuleID child = result
176: .getChildTargetModuleID()[j];
177: out
178: .printString(DeployUtils
179: .reformat(
180: " `-> "
181: + child
182: .getModuleID()
183: + (child
184: .getWebURL() == null
185: || !getAction()
186: .equals(
187: "Deployed") ? ""
188: : " @ "
189: + child
190: .getWebURL()),
191: 4, 72));
192: }
193: }
194: }
195:
196: // if any results failed then throw so that we'll return non-0
197: // to the operating system
198: if (po.getDeploymentStatus().isFailed()) {
199: throw new DeploymentException("Operation failed: "
200: + po.getDeploymentStatus().getMessage());
201: }
202: }
203:
204: private String[] processTargets(String[] args, List targets) {
205: if (args.length >= 2 && args[0].equals("--targets")) {
206: String value = args[1];
207: StringTokenizer tok = new StringTokenizer(value, ";", false);
208: while (tok.hasMoreTokens()) {
209: targets.add(tok.nextToken());
210: }
211: String[] temp = new String[args.length - 2];
212: System.arraycopy(args, 2, temp, 0, temp.length);
213: args = temp;
214: }
215: return args;
216: }
217:
218: private String[] processInPlace(String[] args,
219: BooleanHolder inPlaceHolder) {
220: if (args.length >= 2 && args[0].equals("--inPlace")) {
221: inPlaceHolder.inPlace = true;
222: String[] temp = new String[args.length - 1];
223: System.arraycopy(args, 1, temp, 0, temp.length);
224: args = temp;
225: }
226: return args;
227: }
228:
229: private final class BooleanHolder {
230: public boolean inPlace;
231: }
232: }
|