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.PrintWriter;
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.system.plugin.DownloadResults;
030    
031    /**
032     * The CLI deployer logic to start.
033     *
034     * @version $Rev: 535507 $ $Date: 2007-05-05 07:22:39 -0400 (Sat, 05 May 2007) $
035     */
036    public class CommandInstallCAR extends AbstractCommand {
037    
038        //todo: provide a way to handle a username and password for the remote repo?
039    
040        public void execute(PrintWriter out, ServerConnection connection, CommandArgs commandArgs) throws DeploymentException {
041            DeploymentManager dmgr = connection.getDeploymentManager();
042            if(dmgr instanceof GeronimoDeploymentManager) {
043                GeronimoDeploymentManager mgr = (GeronimoDeploymentManager) dmgr;
044                File carFile = new File(commandArgs.getArgs()[0]);
045                carFile = carFile.getAbsoluteFile();
046                if(!carFile.exists() || !carFile.canRead()) {
047                    throw new DeploymentException("CAR file cannot be read: "+carFile.getAbsolutePath());
048                }
049                Object key = mgr.startInstall(carFile, null, null);
050                long start = System.currentTimeMillis();
051                DownloadResults results = showProgress(mgr, key);
052                int time = (int)(System.currentTimeMillis() - start) / 1000;
053                System.out.println();
054                if(!results.isFailed()) {
055                    System.out.print(DeployUtils.reformat("**** Installation Complete!", 4, 72));
056                    for (int i = 0; i < results.getDependenciesPresent().length; i++) {
057                        Artifact uri = results.getDependenciesPresent()[i];
058                        System.out.print(DeployUtils.reformat("Used existing: "+uri, 4, 72));
059                    }
060                    for (int i = 0; i < results.getDependenciesInstalled().length; i++) {
061                        Artifact uri = results.getDependenciesInstalled()[i];
062                        System.out.print(DeployUtils.reformat("Installed new: "+uri, 4, 72));
063                    }
064                    if(results.getTotalDownloadBytes() > 0 && time > 0) {
065                        System.out.println();
066                        System.out.print(DeployUtils.reformat("Downloaded "+(results.getTotalDownloadBytes()/1024)+" kB in "+time+"s ("+results.getTotalDownloadBytes()/(1024*time)+" kB/s)", 4, 72));
067                    }
068                }
069                if(results.isFinished() && !results.isFailed() && results.getInstalledConfigIDs().length == 1) {
070                    Artifact target = results.getInstalledConfigIDs()[0];
071                    System.out.print(DeployUtils.reformat("Now starting "+target+"...", 4, 72));
072                    System.out.flush();
073                    new CommandStart().execute(out, connection, new BaseCommandArgs(new String[]{target.toString()}));
074                }
075            } else {
076                throw new DeploymentException("Cannot install plugins when connected to "+connection.getServerURI());
077            }
078        }
079    
080        static DownloadResults showProgress(GeronimoDeploymentManager mgr, Object key) {
081            System.out.println("Checking for status every 1000ms:");
082            String last = null, status;
083            while(true) {
084                DownloadResults results = mgr.checkOnInstall(key);
085                if(results.getCurrentFile() != null) {
086                    if(results.getCurrentFilePercent() > -1) {
087                        status = results.getCurrentMessage()+" ("+results.getCurrentFilePercent()+"%)";
088                    } else {
089                        status = results.getCurrentMessage();
090                    }
091                    if(last == null || !last.equals(status)) {
092                        last = status;
093                        System.out.println(status);
094                    }
095                }
096                if(results.isFinished()) {
097                    if(results.isFailed()) {
098                        System.err.println("Installation FAILED: "+results.getFailure().getMessage());
099                    }
100                    return results;
101                }
102                try {
103                    Thread.sleep(1000);
104                } catch (InterruptedException e) {
105                    return results;
106                }
107            }
108        }
109    }