View Javadoc

1   /**
2    *
3    * Copyright 2005 The Apache Software Foundation
4    *
5    *  Licensed under the Apache License, Version 2.0 (the "License");
6    *  you may not use this file except in compliance with the License.
7    *  You may obtain a copy of the License at
8    *
9    *     http://www.apache.org/licenses/LICENSE-2.0
10   *
11   *  Unless required by applicable law or agreed to in writing, software
12   *  distributed under the License is distributed on an "AS IS" BASIS,
13   *  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14   *  See the License for the specific language governing permissions and
15   *  limitations under the License.
16   */
17  package org.apache.geronimo.deployment.cli;
18  
19  import java.io.File;
20  import java.io.PrintWriter;
21  import javax.enterprise.deploy.spi.DeploymentManager;
22  import org.apache.geronimo.common.DeploymentException;
23  import org.apache.geronimo.deployment.plugin.GeronimoDeploymentManager;
24  import org.apache.geronimo.kernel.repository.Artifact;
25  import org.apache.geronimo.system.plugin.DownloadResults;
26  
27  /**
28   * The CLI deployer logic to start.
29   *
30   * @version $Rev: 410741 $ $Date: 2006-05-31 21:35:48 -0700 (Wed, 31 May 2006) $
31   */
32  public class CommandInstallCAR extends AbstractCommand {
33      public CommandInstallCAR() {
34          super("install-plugin", "3. Geronimo Plugins", "PluginFile",
35                  "Installs a Geronimo plugin you've exported from a Geronimo server " +
36                  "or downloaded from an external repository.  The file must be a " +
37                  "properly configured Geronimo CAR file.  This is used to add new " +
38                  "functionality to the Geronimo server.");
39      }
40  
41      //todo: provide a way to handle a username and password for the remote repo?
42  
43      public CommandInstallCAR(String command, String group, String helpArgumentList, String helpText) {
44          super(command, group, helpArgumentList, helpText);
45      }
46  
47      public void execute(PrintWriter out, ServerConnection connection, String[] args) throws DeploymentException {
48          if(args.length == 0) {
49              throw new DeploymentSyntaxException("Must specify Plugin CAR file");
50          }
51          DeploymentManager dmgr = connection.getDeploymentManager();
52          if(dmgr instanceof GeronimoDeploymentManager) {
53              GeronimoDeploymentManager mgr = (GeronimoDeploymentManager) dmgr;
54              File carFile = new File(args[0]);
55              carFile = carFile.getAbsoluteFile();
56              if(!carFile.exists() || !carFile.canRead()) {
57                  throw new DeploymentException("CAR file cannot be read: "+carFile.getAbsolutePath());
58              }
59              Object key = mgr.startInstall(carFile, null, null);
60              long start = System.currentTimeMillis();
61              DownloadResults results = showProgress(mgr, key);
62              int time = (int)(System.currentTimeMillis() - start) / 1000;
63              System.out.println();
64              if(!results.isFailed()) {
65                  System.out.print(DeployUtils.reformat("**** Installation Complete!", 4, 72));
66                  for (int i = 0; i < results.getDependenciesPresent().length; i++) {
67                      Artifact uri = results.getDependenciesPresent()[i];
68                      System.out.print(DeployUtils.reformat("Used existing: "+uri, 4, 72));
69                  }
70                  for (int i = 0; i < results.getDependenciesInstalled().length; i++) {
71                      Artifact uri = results.getDependenciesInstalled()[i];
72                      System.out.print(DeployUtils.reformat("Installed new: "+uri, 4, 72));
73                  }
74                  if(results.getTotalDownloadBytes() > 0 && time > 0) {
75                      System.out.println();
76                      System.out.print(DeployUtils.reformat("Downloaded "+(results.getTotalDownloadBytes()/1024)+" kB in "+time+"s ("+results.getTotalDownloadBytes()/(1024*time)+" kB/s)", 4, 72));
77                  }
78              }
79              if(results.isFinished() && !results.isFailed() && results.getInstalledConfigIDs().length == 1) {
80                  Artifact target = results.getInstalledConfigIDs()[0];
81                  System.out.print(DeployUtils.reformat("Now starting "+target+"...", 4, 72));
82                  System.out.flush();
83                  new CommandStart().execute(out, connection, new String[]{target.toString()});
84              }
85          } else {
86              throw new DeploymentException("Cannot install plugins when connected to "+connection.getServerURI());
87          }
88      }
89  
90      static DownloadResults showProgress(GeronimoDeploymentManager mgr, Object key) {
91          System.out.println("Checking for status every 1000ms:");
92          String last = null, status;
93          while(true) {
94              DownloadResults results = mgr.checkOnInstall(key);
95              if(results.getCurrentFile() != null) {
96                  if(results.getCurrentFilePercent() > -1) {
97                      status = results.getCurrentMessage()+" ("+results.getCurrentFilePercent()+"%)";
98                  } else {
99                      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 }