KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > geronimo > deployment > cli > CommandListConfigurations


1 /**
2  * Licensed to the Apache Software Foundation (ASF) under one or more
3  * contributor license agreements. See the NOTICE file distributed with
4  * this work for additional information regarding copyright ownership.
5  * The ASF licenses this file to You under the Apache License, Version 2.0
6  * (the "License"); you may not use this file except in compliance with
7  * the License. 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 JavaDoc;
20 import java.io.IOException JavaDoc;
21 import java.io.InputStreamReader JavaDoc;
22 import java.io.PrintWriter JavaDoc;
23 import java.net.URL JavaDoc;
24 import java.util.ArrayList JavaDoc;
25 import java.util.HashMap JavaDoc;
26 import java.util.Iterator JavaDoc;
27 import java.util.List JavaDoc;
28 import java.util.Map JavaDoc;
29 import javax.enterprise.deploy.spi.DeploymentManager JavaDoc;
30 import javax.security.auth.login.FailedLoginException JavaDoc;
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: 483670 $ $Date: 2006-12-07 16:23:05 -0500 (Thu, 07 Dec 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 JavaDoc command, String JavaDoc group, String JavaDoc helpArgumentList, String JavaDoc helpText) {
58         super(command, group, helpArgumentList, helpText);
59     }
60
61     public void execute(PrintWriter JavaDoc out, ServerConnection connection, String JavaDoc[] args) throws DeploymentException {
62         DeploymentManager JavaDoc dmgr = connection.getDeploymentManager();
63         if(dmgr instanceof GeronimoDeploymentManager) {
64             GeronimoDeploymentManager mgr = (GeronimoDeploymentManager) dmgr;
65             try {
66                 String JavaDoc repo = null;
67                 if(args.length == 1) {
68                     repo = args[0];
69                 } else {
70                     repo = getRepository(out, new BufferedReader JavaDoc(new InputStreamReader JavaDoc(System.in)), mgr);
71                 }
72                 PluginList data;
73                 URL JavaDoc repository;
74                 try {
75                     repository = new URL JavaDoc(repo);
76                     data = mgr.listPlugins(repository, null, null);
77                 } catch (IOException JavaDoc e) {
78                     throw new DeploymentException("Unable to list configurations", e);
79                 } catch (FailedLoginException JavaDoc e) {
80                     throw new DeploymentException("Invalid login for Maven repository '"+repo+"'");
81                 }
82                 if (data == null) {
83                     out.println();
84                     out.println("No plugins were returned from this site.");
85                     return;
86                 }
87                 Map JavaDoc categories = new HashMap JavaDoc();
88                 List JavaDoc available = new ArrayList JavaDoc();
89                 for (int i = 0; i < data.getPlugins().length; i++) {
90                     PluginMetadata metadata = data.getPlugins()[i];
91                     List JavaDoc list = (List JavaDoc) categories.get(metadata.getCategory());
92                     if(list == null) {
93                         list = new ArrayList JavaDoc();
94                         categories.put(metadata.getCategory(), list);
95                     }
96                     list.add(metadata);
97                 }
98                 for (Iterator JavaDoc it = categories.entrySet().iterator(); it.hasNext();) {
99                     Map.Entry JavaDoc entry = (Map.Entry JavaDoc) it.next();
100                     String JavaDoc category = (String JavaDoc) entry.getKey();
101                     List JavaDoc items = (List JavaDoc) entry.getValue();
102                     out.println();
103                     out.print(DeployUtils.reformat(category, 4, 72));
104                     for (int i = 0; i < items.size(); i++) {
105                         PluginMetadata metadata = (PluginMetadata) items.get(i);
106                         String JavaDoc prefix = " ";
107                         if(!metadata.isInstalled() && metadata.isEligible()) {
108                             available.add(metadata);
109                             prefix = Integer.toString(available.size());
110                             if(available.size() < 10) {
111                                 prefix += " ";
112                             }
113                             prefix += ": ";
114                         }
115                         out.print(DeployUtils.reformat(prefix+metadata.getName()+" ("+metadata.getVersion()+")", 8, 72));
116                         out.flush();
117                     }
118                 }
119                 if(available.size() == 0) {
120                     out.println();
121                     out.println("No plugins from this site are eligible for installation.");
122                     return;
123                 }
124                 out.println();
125                 out.print("Install Service [enter number or 'q' to quit]: ");
126                 out.flush();
127                 BufferedReader JavaDoc in = new BufferedReader JavaDoc(new InputStreamReader JavaDoc(System.in));
128                 String JavaDoc answer = in.readLine();
129                 if(answer.equalsIgnoreCase("q")) {
130                     return;
131                 }
132                 int selection = Integer.parseInt(answer);
133                 PluginMetadata target = ((PluginMetadata) available.get(selection - 1));
134                 long start = System.currentTimeMillis();
135                 Object JavaDoc key = mgr.startInstall(PluginList.createInstallList(data, target.getModuleId()), null, null);
136                 DownloadResults results = CommandInstallCAR.showProgress(mgr, key);
137                 int time = (int)(System.currentTimeMillis() - start) / 1000;
138                 out.println();
139                 if(!results.isFailed()) {
140                     out.print(DeployUtils.reformat("**** Installation Complete!", 4, 72));
141                     for (int i = 0; i < results.getDependenciesPresent().length; i++) {
142                         Artifact uri = results.getDependenciesPresent()[i];
143                         out.print(DeployUtils.reformat("Used existing: "+uri, 4, 72));
144                     }
145                     for (int i = 0; i < results.getDependenciesInstalled().length; i++) {
146                         Artifact uri = results.getDependenciesInstalled()[i];
147                         out.print(DeployUtils.reformat("Installed new: "+uri, 4, 72));
148                     }
149                     out.println();
150                     out.print(DeployUtils.reformat("Downloaded "+(results.getTotalDownloadBytes()/1024)+" kB in "+time+"s ("+results.getTotalDownloadBytes()/(1024*time)+" kB/s)", 4, 72));
151                 }
152                 if(results.isFinished() && !results.isFailed()) {
153                     out.print(DeployUtils.reformat("Now starting "+target.getModuleId()+"...", 4, 72));
154                     out.flush();
155                     new CommandStart().execute(out, connection, new String JavaDoc[]{target.getModuleId().toString()});
156                 }
157             } catch (IOException JavaDoc e) {
158                 throw new DeploymentException("Unable to install configuration", e);
159             } catch(NumberFormatException JavaDoc e) {
160                 throw new DeploymentException("Invalid response");
161             }
162         } else {
163             throw new DeploymentException("Cannot list repositories when connected to "+connection.getServerURI());
164         }
165     }
166
167     private String JavaDoc getRepository(PrintWriter JavaDoc out, BufferedReader JavaDoc in, GeronimoDeploymentManager mgr) throws IOException JavaDoc, DeploymentException {
168         URL JavaDoc[] all = mgr.getRepositories();
169         if(all.length == 0) {
170             throw new DeploymentException("No default repositories available. Please either specify the repository " +
171                     "URL on the command line, or go into the console Plugin page and update the list of available " +
172                     "repositories.");
173         }
174         out.println();
175         out.println("Select repository:");
176         for (int i = 0; i < all.length; i++) {
177             URL JavaDoc url = all[i];
178             out.println(" "+(i+1)+". "+url);
179         }
180         out.println();
181         out.print("Enter Repository Number: ");
182         out.flush();
183         String JavaDoc entry = in.readLine().trim();
184         int index = Integer.parseInt(entry);
185         return all[index-1].toString();
186     }
187 }
188
Popular Tags