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.PrintWriter;
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 jline.ConsoleReader;
032:
033: /**
034: * The CLI deployer logic to start.
035: *
036: * @version $Rev: 597481 $ $Date: 2007-11-22 11:25:03 -0800 (Thu, 22 Nov 2007) $
037: */
038: public class CommandStart extends AbstractCommand {
039:
040: public void execute(ConsoleReader consoleReader,
041: ServerConnection connection, CommandArgs commandArgs)
042: throws DeploymentException {
043: ProgressObject po = null;
044: try {
045: String[] args = commandArgs.getArgs();
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(
054: "Unable to load module list from server", e);
055: }
056: List modules = new ArrayList();
057: for (int i = 0; i < args.length; i++) {
058: modules.addAll(DeployUtils.identifyTargetModuleIDs(
059: allModules, args[i], false));
060: }
061: TargetModuleID[] ids = (TargetModuleID[]) modules
062: .toArray(new TargetModuleID[modules.size()]);
063: boolean multiple = isMultipleTargets(ids);
064: po = runCommand(consoleReader, mgr, ids);
065: TargetModuleID[] done = po.getResultTargetModuleIDs();
066: consoleReader.printNewline();
067: for (int i = 0; i < done.length; i++) {
068: TargetModuleID id = done[i];
069: DeployUtils
070: .println(
071: getAction()
072: + " "
073: + id.getModuleID()
074: + ((multiple && id.getTarget() != null) ? " on "
075: + id.getTarget()
076: .getName()
077: : "")
078: + (id.getWebURL() == null
079: || !getAction().equals(
080: "Started") ? ""
081: : " @ "
082: + id
083: .getWebURL()),
084: 4, consoleReader);
085: if (id.getChildTargetModuleID() != null) {
086: for (int j = 0; j < id.getChildTargetModuleID().length; j++) {
087: TargetModuleID child = id
088: .getChildTargetModuleID()[j];
089: DeployUtils
090: .println(
091: " `-> "
092: + child.getModuleID()
093: + (child.getWebURL() == null
094: || getAction()
095: .toLowerCase()
096: .indexOf(
097: "started") == -1 ? ""
098: : " @ "
099: + child
100: .getWebURL()),
101: 4, consoleReader);
102: }
103: } // Also print childs if existing in earlier configuration
104: else {
105: java.util.Iterator iterator = DeployUtils
106: .identifyTargetModuleIDs(allModules,
107: id.getModuleID(), false).iterator();
108: if (iterator.hasNext()) {
109: TargetModuleID childs = (TargetModuleID) iterator
110: .next();
111: if (childs.getChildTargetModuleID() != null) {
112: for (int j = 0; j < childs
113: .getChildTargetModuleID().length; j++) {
114: TargetModuleID child = childs
115: .getChildTargetModuleID()[j];
116: DeployUtils
117: .println(
118: " `-> "
119: + child
120: .getModuleID()
121: + (child
122: .getWebURL() == null
123: || getAction()
124: .toLowerCase()
125: .indexOf(
126: "started") == -1 ? ""
127: : " @ "
128: + child
129: .getWebURL()),
130: 4, consoleReader);
131: }
132: }
133: }
134: }
135: // consoleReader.printNewline();
136: }
137: } catch (IOException e) {
138: throw new DeploymentException("could not write to console",
139: e);
140: }
141: if (po.getDeploymentStatus().isFailed()) {
142: throw new DeploymentException("Operation failed: "
143: + po.getDeploymentStatus().getMessage());
144: }
145: }
146:
147: protected ProgressObject runCommand(ConsoleReader out,
148: DeploymentManager mgr, TargetModuleID[] ids) {
149: ProgressObject po = mgr.start(ids);
150: waitForProgress(out, po);
151: return po;
152: }
153:
154: protected String getAction() {
155: return "Started";
156: }
157:
158: }
|