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.BufferedReader;
20  import java.io.IOException;
21  import java.io.InputStreamReader;
22  import java.io.PrintWriter;
23  import java.net.URL;
24  import java.util.ArrayList;
25  import java.util.HashMap;
26  import java.util.Iterator;
27  import java.util.List;
28  import java.util.Map;
29  import javax.enterprise.deploy.spi.DeploymentManager;
30  import javax.security.auth.login.FailedLoginException;
31  import org.apache.geronimo.common.DeploymentException;
32  import org.apache.geronimo.deployment.plugin.GeronimoDeploymentManager;
33  import org.apache.geronimo.kernel.repository.Artifact;
34  import org.apache.geronimo.system.plugin.DownloadResults;
35  import org.apache.geronimo.system.plugin.PluginList;
36  import org.apache.geronimo.system.plugin.PluginMetadata;
37  
38  /**
39   * The CLI deployer logic to start.
40   *
41   * @version $Rev: 410741 $ $Date: 2006-05-31 21:35:48 -0700 (Wed, 31 May 2006) $
42   */
43  public class CommandListConfigurations extends AbstractCommand {
44      public CommandListConfigurations() {
45          super("search-plugins", "3. Geronimo Plugins", "[MavenRepoURL]",
46                  "Lists the Geronimo plugins available in a Maven repository "+
47                  "and lets you select a plugin to download and install.  This "+
48                  "is used to add new functionality to the Geronimo server.  If " +
49                  "no repository is specified the default repositories will be " +
50                  "listed to select from, but this means there must have been " +
51                  "some default repositories set (by hand or by having the " +
52                  "console update to the latest defaults).");
53      }
54  
55      //todo: provide a way to handle a username and password for the remote repo?
56  
57      public CommandListConfigurations(String command, String group, String helpArgumentList, String helpText) {
58          super(command, group, helpArgumentList, helpText);
59      }
60  
61      public void execute(PrintWriter out, ServerConnection connection, String[] args) throws DeploymentException {
62          DeploymentManager dmgr = connection.getDeploymentManager();
63          if(dmgr instanceof GeronimoDeploymentManager) {
64              GeronimoDeploymentManager mgr = (GeronimoDeploymentManager) dmgr;
65              try {
66                  String repo = null;
67                  if(args.length == 1) {
68                      repo = args[0];
69                  } else {
70                      repo = getRepository(out, new BufferedReader(new InputStreamReader(System.in)), mgr);
71                  }
72                  PluginList data;
73                  URL repository;
74                  try {
75                      repository = new URL(repo);
76                      data = mgr.listPlugins(repository, null, null);
77                  } catch (IOException e) {
78                      throw new DeploymentException("Unable to list configurations", e);
79                  } catch (FailedLoginException e) {
80                      throw new DeploymentException("Invalid login for Maven repository '"+repo+"'");
81                  }
82                  Map categories = new HashMap();
83                  List available = new ArrayList();
84                  for (int i = 0; i < data.getPlugins().length; i++) {
85                      PluginMetadata metadata = data.getPlugins()[i];
86                      List list = (List) categories.get(metadata.getCategory());
87                      if(list == null) {
88                          list = new ArrayList();
89                          categories.put(metadata.getCategory(), list);
90                      }
91                      list.add(metadata);
92                  }
93                  for (Iterator it = categories.entrySet().iterator(); it.hasNext();) {
94                      Map.Entry entry = (Map.Entry) it.next();
95                      String category = (String) entry.getKey();
96                      List items = (List) entry.getValue();
97                      out.println();
98                      out.print(DeployUtils.reformat(category, 4, 72));
99                      for (int i = 0; i < items.size(); i++) {
100                         PluginMetadata metadata = (PluginMetadata) items.get(i);
101                         String prefix = "    ";
102                         if(!metadata.isInstalled() && metadata.isEligible()) {
103                             available.add(metadata);
104                             prefix = Integer.toString(available.size());
105                             if(available.size() < 10) {
106                                 prefix += " ";
107                             }
108                             prefix += ": ";
109                         }
110                         out.print(DeployUtils.reformat(prefix+metadata.getName()+" ("+metadata.getVersion()+")", 8, 72));
111                         out.flush();
112                     }
113                 }
114                 if(available.size() == 0) {
115                     out.println();
116                     out.println("No plugins from this site are eligible for installation.");
117                     return;
118                 }
119                 out.println();
120                 out.print("Install Service [enter number or 'q' to quit]: ");
121                 out.flush();
122                 BufferedReader in = new BufferedReader(new InputStreamReader(System.in));
123                 String answer = in.readLine();
124                 if(answer.equalsIgnoreCase("q")) {
125                     return;
126                 }
127                 int selection = Integer.parseInt(answer);
128                 PluginMetadata target = ((PluginMetadata) available.get(selection - 1));
129                 long start = System.currentTimeMillis();
130                 Object key = mgr.startInstall(PluginList.createInstallList(data, target.getModuleId()), null, null);
131                 DownloadResults results = CommandInstallCAR.showProgress(mgr, key);
132                 int time = (int)(System.currentTimeMillis() - start) / 1000;
133                 out.println();
134                 if(!results.isFailed()) {
135                     out.print(DeployUtils.reformat("**** Installation Complete!", 4, 72));
136                     for (int i = 0; i < results.getDependenciesPresent().length; i++) {
137                         Artifact uri = results.getDependenciesPresent()[i];
138                         out.print(DeployUtils.reformat("Used existing: "+uri, 4, 72));
139                     }
140                     for (int i = 0; i < results.getDependenciesInstalled().length; i++) {
141                         Artifact uri = results.getDependenciesInstalled()[i];
142                         out.print(DeployUtils.reformat("Installed new: "+uri, 4, 72));
143                     }
144                     out.println();
145                     out.print(DeployUtils.reformat("Downloaded "+(results.getTotalDownloadBytes()/1024)+" kB in "+time+"s ("+results.getTotalDownloadBytes()/(1024*time)+" kB/s)", 4, 72));
146                 }
147                 if(results.isFinished() && !results.isFailed()) {
148                     out.print(DeployUtils.reformat("Now starting "+target.getModuleId()+"...", 4, 72));
149                     out.flush();
150                     new CommandStart().execute(out, connection, new String[]{target.getModuleId().toString()});
151                 }
152             } catch (IOException e) {
153                 throw new DeploymentException("Unable to install configuration", e);
154             } catch(NumberFormatException e) {
155                 throw new DeploymentException("Invalid response");
156             }
157         } else {
158             throw new DeploymentException("Cannot list repositories when connected to "+connection.getServerURI());
159         }
160     }
161 
162     private String getRepository(PrintWriter out, BufferedReader in, GeronimoDeploymentManager mgr) throws IOException, DeploymentException {
163         URL[] all = mgr.getRepositories();
164         if(all.length == 0) {
165             throw new DeploymentException("No default repositories available.  Please either specify the repository " +
166                     "URL on the command line, or go into the console Plugin page and update the list of available " +
167                     "repositories.");
168         }
169         out.println();
170         out.println("Select repository:");
171         for (int i = 0; i < all.length; i++) {
172             URL url = all[i];
173             out.println("  "+(i+1)+". "+url);
174         }
175         out.println();
176         out.print("Enter Repository Number: ");
177         out.flush();
178         String entry = in.readLine().trim();
179         int index = Integer.parseInt(entry);
180         return all[index-1].toString();
181     }
182 }