1 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 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 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 if (data == null) { 83 out.println(); 84 out.println("No plugins were returned from this site."); 85 return; 86 } 87 Map categories = new HashMap (); 88 List available = new ArrayList (); 89 for (int i = 0; i < data.getPlugins().length; i++) { 90 PluginMetadata metadata = data.getPlugins()[i]; 91 List list = (List ) categories.get(metadata.getCategory()); 92 if(list == null) { 93 list = new ArrayList (); 94 categories.put(metadata.getCategory(), list); 95 } 96 list.add(metadata); 97 } 98 for (Iterator it = categories.entrySet().iterator(); it.hasNext();) { 99 Map.Entry entry = (Map.Entry ) it.next(); 100 String category = (String ) entry.getKey(); 101 List items = (List ) 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 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 in = new BufferedReader (new InputStreamReader (System.in)); 128 String 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 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 []{target.getModuleId().toString()}); 156 } 157 } catch (IOException e) { 158 throw new DeploymentException("Unable to install configuration", e); 159 } catch(NumberFormatException 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 getRepository(PrintWriter out, BufferedReader in, GeronimoDeploymentManager mgr) throws IOException , DeploymentException { 168 URL [] 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 url = all[i]; 178 out.println(" "+(i+1)+". "+url); 179 } 180 out.println(); 181 out.print("Enter Repository Number: "); 182 out.flush(); 183 String entry = in.readLine().trim(); 184 int index = Integer.parseInt(entry); 185 return all[index-1].toString(); 186 } 187 } 188 | Popular Tags |