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:
021: import javax.enterprise.deploy.spi.DeploymentManager;
022:
023: import org.apache.geronimo.cli.deployer.BaseCommandArgs;
024: import org.apache.geronimo.cli.deployer.CommandArgs;
025: import org.apache.geronimo.common.DeploymentException;
026: import org.apache.geronimo.deployment.plugin.GeronimoDeploymentManager;
027: import org.apache.geronimo.kernel.repository.Artifact;
028: import org.apache.geronimo.kernel.repository.MissingDependencyException;
029: import org.apache.geronimo.system.plugin.DownloadResults;
030: import jline.ConsoleReader;
031:
032: /**
033: * The CLI deployer logic to start.
034: *
035: * @version $Rev: 615705 $ $Date: 2008-01-27 18:11:26 -0800 (Sun, 27 Jan 2008) $
036: */
037: public class CommandInstallCAR extends AbstractCommand {
038:
039: //todo: provide a way to handle a username and password for the remote repo?
040:
041: public void execute(ConsoleReader consoleReader,
042: ServerConnection connection, CommandArgs commandArgs)
043: throws DeploymentException {
044: DeploymentManager dmgr = connection.getDeploymentManager();
045: if (dmgr instanceof GeronimoDeploymentManager) {
046: try {
047: GeronimoDeploymentManager mgr = (GeronimoDeploymentManager) dmgr;
048: if (commandArgs.getArgs().length == 0) {
049: throw new DeploymentException(
050: "Must specify Plugin CAR file");
051: }
052: File carFile = new File(commandArgs.getArgs()[0]);
053: carFile = carFile.getAbsoluteFile();
054: if (!carFile.exists() || !carFile.canRead()) {
055: throw new DeploymentException(
056: "CAR file cannot be read: "
057: + carFile.getAbsolutePath());
058: }
059: //TODO figure out if there is a plausible default repo
060: Object key = mgr.startInstall(carFile, null, false,
061: null, null);
062: long start = System.currentTimeMillis();
063: DownloadResults results = showProgress(consoleReader,
064: mgr, key);
065: int time = (int) (System.currentTimeMillis() - start) / 1000;
066: printResults(consoleReader, results, time);
067: if (results.isFinished() && !results.isFailed()
068: && results.getInstalledConfigIDs().size() == 1) {
069: Artifact target = results.getInstalledConfigIDs()
070: .get(0);
071: consoleReader.printString(DeployUtils.reformat(
072: "Now starting " + target + "...", 4, 72));
073: consoleReader.flushConsole();
074: new CommandStart()
075: .execute(consoleReader, connection,
076: new BaseCommandArgs(
077: new String[] { target
078: .toString() }));
079: }
080: } catch (IOException e) {
081: throw new DeploymentException("Cannot install plugin",
082: e);
083: }
084: } else {
085: throw new DeploymentException(
086: "Cannot install plugins when connected to "
087: + connection.getServerURI());
088: }
089: }
090:
091: static DownloadResults showProgress(ConsoleReader consoleReader,
092: GeronimoDeploymentManager mgr, Object key)
093: throws IOException {
094: DeployUtils.println("Checking for status every 1000ms:", 0,
095: consoleReader);
096: String last = null, status;
097: while (true) {
098: DownloadResults results = mgr.checkOnInstall(key);
099: if (results.getCurrentFile() != null) {
100: if (results.getCurrentFilePercent() > -1) {
101: status = results.getCurrentMessage() + " ("
102: + results.getCurrentFilePercent() + "%)";
103: } else {
104: status = results.getCurrentMessage();
105: }
106: if (last == null || !last.equals(status)) {
107: last = status;
108: DeployUtils.println(status, 0, consoleReader);
109: consoleReader.flushConsole();
110: }
111: }
112: if (results.isFinished()) {
113: if (results.isFailed()) {
114: DeployUtils.println("Installation FAILED: "
115: + results.getFailure().getMessage(), 0,
116: consoleReader);
117: }
118: return results;
119: }
120: try {
121: Thread.sleep(1000);
122: } catch (InterruptedException e) {
123: return results;
124: }
125: }
126: }
127:
128: static void printResults(ConsoleReader consoleReader,
129: DownloadResults results, int time) throws IOException,
130: DeploymentException {
131: consoleReader.printNewline();
132: if (!results.isFailed()) {
133: DeployUtils.println("**** Installation Complete!", 0,
134: consoleReader);
135: for (MissingDependencyException e : results
136: .getSkippedPlugins()) {
137: DeployUtils.println(e.getMessage(), 0, consoleReader);
138: }
139: for (Artifact uri : results.getDependenciesPresent()) {
140: DeployUtils.println("Used existing: " + uri, 0,
141: consoleReader);
142: }
143: for (Artifact uri : results.getDependenciesInstalled()) {
144: DeployUtils.println("Installed new: " + uri, 0,
145: consoleReader);
146: }
147: consoleReader.printNewline();
148: if (results.getTotalDownloadBytes() > 0 && time > 0) {
149: DeployUtils.println("Downloaded "
150: + (results.getTotalDownloadBytes() / 1024)
151: + " kB in " + time + "s ("
152: + results.getTotalDownloadBytes()
153: / (1024 * time) + " kB/s)", 0, consoleReader);
154: }
155: }
156: }
157: }
|