001    /**
002     *
003     * Copyright 2005 The Apache Software Foundation
004     *
005     *  Licensed under the Apache License, Version 2.0 (the "License");
006     *  you may not use this file except in compliance with the License.
007     *  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    import javax.enterprise.deploy.spi.DeploymentManager;
022    import org.apache.geronimo.common.DeploymentException;
023    import org.apache.geronimo.deployment.plugin.GeronimoDeploymentManager;
024    import org.apache.geronimo.kernel.repository.Artifact;
025    import org.apache.geronimo.system.plugin.DownloadResults;
026    
027    /**
028     * The CLI deployer logic to start.
029     *
030     * @version $Rev: 410741 $ $Date: 2006-05-31 21:35:48 -0700 (Wed, 31 May 2006) $
031     */
032    public class CommandInstallCAR extends AbstractCommand {
033        public CommandInstallCAR() {
034            super("install-plugin", "3. Geronimo Plugins", "PluginFile",
035                    "Installs a Geronimo plugin you've exported from a Geronimo server " +
036                    "or downloaded from an external repository.  The file must be a " +
037                    "properly configured Geronimo CAR file.  This is used to add new " +
038                    "functionality to the Geronimo server.");
039        }
040    
041        //todo: provide a way to handle a username and password for the remote repo?
042    
043        public CommandInstallCAR(String command, String group, String helpArgumentList, String helpText) {
044            super(command, group, helpArgumentList, helpText);
045        }
046    
047        public void execute(PrintWriter out, ServerConnection connection, String[] args) throws DeploymentException {
048            if(args.length == 0) {
049                throw new DeploymentSyntaxException("Must specify Plugin CAR file");
050            }
051            DeploymentManager dmgr = connection.getDeploymentManager();
052            if(dmgr instanceof GeronimoDeploymentManager) {
053                GeronimoDeploymentManager mgr = (GeronimoDeploymentManager) dmgr;
054                File carFile = new File(args[0]);
055                carFile = carFile.getAbsoluteFile();
056                if(!carFile.exists() || !carFile.canRead()) {
057                    throw new DeploymentException("CAR file cannot be read: "+carFile.getAbsolutePath());
058                }
059                Object key = mgr.startInstall(carFile, null, null);
060                long start = System.currentTimeMillis();
061                DownloadResults results = showProgress(mgr, key);
062                int time = (int)(System.currentTimeMillis() - start) / 1000;
063                System.out.println();
064                if(!results.isFailed()) {
065                    System.out.print(DeployUtils.reformat("**** Installation Complete!", 4, 72));
066                    for (int i = 0; i < results.getDependenciesPresent().length; i++) {
067                        Artifact uri = results.getDependenciesPresent()[i];
068                        System.out.print(DeployUtils.reformat("Used existing: "+uri, 4, 72));
069                    }
070                    for (int i = 0; i < results.getDependenciesInstalled().length; i++) {
071                        Artifact uri = results.getDependenciesInstalled()[i];
072                        System.out.print(DeployUtils.reformat("Installed new: "+uri, 4, 72));
073                    }
074                    if(results.getTotalDownloadBytes() > 0 && time > 0) {
075                        System.out.println();
076                        System.out.print(DeployUtils.reformat("Downloaded "+(results.getTotalDownloadBytes()/1024)+" kB in "+time+"s ("+results.getTotalDownloadBytes()/(1024*time)+" kB/s)", 4, 72));
077                    }
078                }
079                if(results.isFinished() && !results.isFailed() && results.getInstalledConfigIDs().length == 1) {
080                    Artifact target = results.getInstalledConfigIDs()[0];
081                    System.out.print(DeployUtils.reformat("Now starting "+target+"...", 4, 72));
082                    System.out.flush();
083                    new CommandStart().execute(out, connection, new String[]{target.toString()});
084                }
085            } else {
086                throw new DeploymentException("Cannot install plugins when connected to "+connection.getServerURI());
087            }
088        }
089    
090        static DownloadResults showProgress(GeronimoDeploymentManager mgr, Object key) {
091            System.out.println("Checking for status every 1000ms:");
092            String last = null, status;
093            while(true) {
094                DownloadResults results = mgr.checkOnInstall(key);
095                if(results.getCurrentFile() != null) {
096                    if(results.getCurrentFilePercent() > -1) {
097                        status = results.getCurrentMessage()+" ("+results.getCurrentFilePercent()+"%)";
098                    } else {
099                        status = results.getCurrentMessage();
100                    }
101                    if(last == null || !last.equals(status)) {
102                        last = status;
103                        System.out.println(status);
104                    }
105                }
106                if(results.isFinished()) {
107                    if(results.isFailed()) {
108                        System.err.println("Installation FAILED: "+results.getFailure().getMessage());
109                    }
110                    return results;
111                }
112                try {
113                    Thread.sleep(1000);
114                } catch (InterruptedException e) {
115                    return results;
116                }
117            }
118        }
119    }