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 */
017 package org.apache.geronimo.deployment.cli;
018
019 import java.io.File;
020 import java.io.IOException;
021
022 import javax.enterprise.deploy.spi.DeploymentManager;
023
024 import org.apache.geronimo.cli.deployer.BaseCommandArgs;
025 import org.apache.geronimo.cli.deployer.CommandArgs;
026 import org.apache.geronimo.common.DeploymentException;
027 import org.apache.geronimo.deployment.plugin.GeronimoDeploymentManager;
028 import org.apache.geronimo.kernel.repository.Artifact;
029 import org.apache.geronimo.kernel.repository.MissingDependencyException;
030 import org.apache.geronimo.system.plugin.DownloadResults;
031 import jline.ConsoleReader;
032
033 /**
034 * The CLI deployer logic to start.
035 *
036 * @version $Rev: 706640 $ $Date: 2008-10-21 14:44:05 +0000 (Tue, 21 Oct 2008) $
037 */
038 public class CommandInstallCAR extends AbstractCommand {
039
040 //todo: provide a way to handle a username and password for the remote repo?
041
042 public void execute(ConsoleReader consoleReader, ServerConnection connection, CommandArgs commandArgs) throws DeploymentException {
043 DeploymentManager dmgr = connection.getDeploymentManager();
044 if(dmgr instanceof GeronimoDeploymentManager) {
045 try {
046 GeronimoDeploymentManager mgr = (GeronimoDeploymentManager) dmgr;
047 if (commandArgs.getArgs().length == 0) {
048 throw new DeploymentException("Must specify Plugin CAR file");
049 }
050 File carFile = new File(commandArgs.getArgs()[0]);
051 carFile = carFile.getAbsoluteFile();
052 if(!carFile.exists() || !carFile.canRead()) {
053 throw new DeploymentException("CAR file cannot be read: "+carFile.getAbsolutePath());
054 }
055 //TODO figure out if there is a plausible default repo
056 Object key = mgr.startInstall(carFile, null, false, null, null);
057 long start = System.currentTimeMillis();
058 DownloadResults results = showProgress(consoleReader, mgr, key);
059 int time = (int)(System.currentTimeMillis() - start) / 1000;
060 printResults(consoleReader, results, time);
061 } catch (IOException e) {
062 throw new DeploymentException("Cannot install plugin", e);
063 }
064 } else {
065 throw new DeploymentException("Cannot install plugins using " + dmgr.getClass().getName() + " deployment manager");
066 }
067 }
068
069 static DownloadResults showProgress(ConsoleReader consoleReader, GeronimoDeploymentManager mgr, Object key) throws IOException {
070 DeployUtils.println("Checking for status every 1000ms:", 0, consoleReader);
071 String last = null, status;
072 while(true) {
073 DownloadResults results = mgr.checkOnInstall(key);
074 if(results.getCurrentFile() != null) {
075 if(results.getCurrentFilePercent() > -1) {
076 status = results.getCurrentMessage()+" ("+results.getCurrentFilePercent()+"%)";
077 } else {
078 status = results.getCurrentMessage();
079 }
080 if(last == null || !last.equals(status)) {
081 last = status;
082 DeployUtils.println(status, 0, consoleReader);
083 consoleReader.flushConsole();
084 }
085 }
086 if(results.isFinished()) {
087 if(results.isFailed()) {
088 DeployUtils.println("Installation FAILED: "+results.getFailure().getMessage(), 0, consoleReader);
089 }
090 return results;
091 }
092 try {
093 Thread.sleep(1000);
094 } catch (InterruptedException e) {
095 return results;
096 }
097 }
098 }
099
100 static void printResults(ConsoleReader consoleReader, DownloadResults results, int time) throws IOException, DeploymentException {
101 consoleReader.printNewline();
102 if (!results.isFailed()) {
103 DeployUtils.println("**** Installation Complete!", 0, consoleReader);
104 for (MissingDependencyException e : results.getSkippedPlugins()) {
105 DeployUtils.println(e.getMessage(), 0, consoleReader);
106 }
107 for (Artifact uri: results.getDependenciesPresent()) {
108 DeployUtils.println("Used existing: " + uri, 0, consoleReader);
109 }
110 for (Artifact uri: results.getDependenciesInstalled()) {
111 DeployUtils.println("Installed new: " + uri, 0, consoleReader);
112 }
113 consoleReader.printNewline();
114 if (results.getTotalDownloadBytes() > 0 && time > 0) {
115 DeployUtils.println(
116 "Downloaded " + (results.getTotalDownloadBytes() / 1024) + " kB in " + time + "s (" + results.getTotalDownloadBytes() / (1024 * time) + " kB/s)",
117 0, consoleReader);
118 }
119 }
120 }
121 }