001: /**
002: * Licensed to the Apache Software Foundation (ASF) under one or more
003: * contributor license agreements. See the NOTICE file distributed with
004: * this work for additional information regarding copyright ownership.
005: * The ASF licenses this file to You under the Apache License, Version 2.0
006: * (the "License"); you may not use this file except in compliance with
007: * the License. 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: */package org.apache.geronimo.deployment.cli;
017:
018: import java.io.IOException;
019: import java.net.URL;
020: import java.util.ArrayList;
021: import java.util.Collection;
022: import java.util.Comparator;
023: import java.util.List;
024: import java.util.Map;
025: import java.util.TreeMap;
026: import java.util.TreeSet;
027:
028: import javax.enterprise.deploy.spi.DeploymentManager;
029: import javax.security.auth.login.FailedLoginException;
030:
031: import jline.ConsoleReader;
032: import org.apache.geronimo.cli.deployer.CommandArgs;
033: import org.apache.geronimo.common.DeploymentException;
034: import org.apache.geronimo.deployment.plugin.GeronimoDeploymentManager;
035: import org.apache.geronimo.kernel.config.NoSuchStoreException;
036: import org.apache.geronimo.system.plugin.DownloadResults;
037: import org.apache.geronimo.system.plugin.PluginInstallerGBean;
038: import org.apache.geronimo.system.plugin.model.PluginArtifactType;
039: import org.apache.geronimo.system.plugin.model.PluginListType;
040: import org.apache.geronimo.system.plugin.model.PluginType;
041:
042: /**
043: * The CLI deployer logic to start.
044: *
045: * @version $Rev: 615057 $ $Date: 2008-01-24 14:22:29 -0800 (Thu, 24 Jan 2008) $
046: */
047: public class CommandListConfigurations extends AbstractCommand {
048:
049: //todo: provide a way to handle a username and password for the remote repo?
050:
051: public void execute(ConsoleReader consoleReader,
052: ServerConnection connection, CommandArgs commandArgs)
053: throws DeploymentException {
054: DeploymentManager dmgr = connection.getDeploymentManager();
055: if (dmgr instanceof GeronimoDeploymentManager) {
056: GeronimoDeploymentManager mgr = (GeronimoDeploymentManager) dmgr;
057: try {
058: String repo;
059: if (commandArgs.getArgs().length == 1) {
060: repo = commandArgs.getArgs()[0];
061: } else {
062: repo = getRepository(consoleReader, mgr);
063: }
064: PluginListType plugins = getPluginCategories(repo, mgr,
065: consoleReader);
066: if (plugins == null) {
067: return;
068: }
069:
070: PluginListType list = getInstallList(plugins,
071: consoleReader, repo);
072: if (list == null) {
073: return;
074: }
075:
076: installPlugins(mgr, list, repo, consoleReader,
077: connection);
078: } catch (IOException e) {
079: throw new DeploymentException(
080: "Unable to install configuration", e);
081: } catch (NumberFormatException e) {
082: throw new DeploymentException("Invalid response");
083: }
084: } else {
085: throw new DeploymentException(
086: "Cannot list repositories when connected to "
087: + connection.getServerURI());
088: }
089: }
090:
091: public String getRepository(ConsoleReader consoleReader,
092: GeronimoDeploymentManager mgr) throws IOException,
093: DeploymentException {
094: URL[] all = mgr.getRepositories();
095: if (all.length == 0) {
096: throw new DeploymentException(
097: "No default repositories available. Please either specify the repository "
098: + "URL on the command line, or go into the console Plugin page and update the list of available "
099: + "repositories.");
100: }
101: // no need to ask for input if only one repo exists
102: if (all.length == 1) {
103: String repo = all[0].toString();
104: consoleReader.printNewline();
105: consoleReader.printString("Selected repository: " + repo);
106: consoleReader.printNewline();
107: return repo;
108: }
109:
110: consoleReader.printNewline();
111: consoleReader.printString("Select repository:");
112: consoleReader.printNewline();
113: for (int i = 0; i < all.length; i++) {
114: URL url = all[i];
115: DeployUtils
116: .printTo(" " + (i + 1) + ". ", 8, consoleReader);
117: DeployUtils.println(url.toString(), 0, consoleReader);
118: }
119: String entry = consoleReader.readLine(
120: "Enter Repository Number: ").trim();
121: if (entry.length() == 0) {
122: return null;
123: }
124: try {
125: int index = Integer.parseInt(entry);
126: return all[index - 1].toString();
127: } catch (NumberFormatException e) {
128: throw new DeploymentException("Invalid selection");
129: } catch (ArrayIndexOutOfBoundsException e) {
130: throw new DeploymentException("Invalid selection");
131: }
132: }
133:
134: public PluginListType getPluginCategories(String repo,
135: GeronimoDeploymentManager mgr, ConsoleReader consoleReader)
136: throws DeploymentException, IOException {
137: if (repo == null) {
138: return null;
139: }
140: PluginListType data;
141: URL repository;
142: try {
143: repository = new URL(repo);
144: data = mgr.listPlugins(repository, null, null);
145: } catch (IOException e) {
146: throw new DeploymentException(
147: "Unable to list configurations", e);
148: } catch (FailedLoginException e) {
149: throw new DeploymentException(
150: "Invalid login for Maven repository '" + repo + "'",
151: e);
152: }
153: if (data == null || data.getPlugin().size() == 0) {
154: return null;
155: }
156: return data;
157: }
158:
159: public PluginListType getLocalPluginCategories(
160: GeronimoDeploymentManager mgr, ConsoleReader consoleReader)
161: throws DeploymentException, IOException {
162: PluginListType data;
163: try {
164: data = mgr.createPluginListForRepositories(null);
165: } catch (NoSuchStoreException e) {
166: throw new DeploymentException(
167: "Unable to list configurations", e);
168: }
169: if (data == null || data.getPlugin().size() == 0) {
170: return null;
171: }
172: return data;
173: }
174:
175: private Map<String, Collection<PluginType>> writePluginList(
176: PluginListType data, ConsoleReader consoleReader)
177: throws IOException {
178: if (data == null) {
179: consoleReader.printNewline();
180: consoleReader
181: .printString("No plugins were returned from this site.");
182: consoleReader.printNewline();
183: consoleReader.flushConsole();
184: return null;
185: }
186: Map<String, Collection<PluginType>> categories = new TreeMap<String, Collection<PluginType>>();
187: Comparator<PluginType> comp = new Comparator<PluginType>() {
188:
189: public int compare(PluginType o1, PluginType o2) {
190: return o1.getName().compareTo(o2.getName());
191: }
192: };
193: for (PluginType metadata : data.getPlugin()) {
194: String category = metadata.getCategory();
195: if (category == null) {
196: category = "<no category>";
197: }
198: Collection<PluginType> list = categories.get(category);
199: if (list == null) {
200: list = new TreeSet<PluginType>(comp);
201: categories.put(category, list);
202: }
203: list.add(metadata);
204: }
205: return categories;
206: }
207:
208: public PluginListType getInstallList(PluginListType plugins,
209: ConsoleReader consoleReader, String repo)
210: throws IOException {
211: Map<String, Collection<PluginType>> categories = writePluginList(
212: plugins, consoleReader);
213: if (categories == null) {
214: return null;
215: }
216: List<PluginType> available = new ArrayList<PluginType>();
217: for (Map.Entry<String, Collection<PluginType>> entry : categories
218: .entrySet()) {
219: String category = entry.getKey();
220: Collection<PluginType> items = entry.getValue();
221: consoleReader.printString(category);
222: consoleReader.printNewline();
223: for (PluginType metadata : items) {
224: for (PluginArtifactType instance : metadata
225: .getPluginArtifact()) {
226: PluginType copy = PluginInstallerGBean.copy(
227: metadata, instance);
228: available.add(copy);
229: DeployUtils.printTo(
230: " " + available.size() + ": ", 10,
231: consoleReader);
232: DeployUtils.println(
233: metadata.getName()
234: + " ("
235: + instance.getModuleId()
236: .getVersion() + ")", 0,
237: consoleReader);
238: }
239: }
240: }
241: if (available.size() == 0) {
242: consoleReader.printNewline();
243: consoleReader
244: .printString("No plugins from this site are eligible for installation.");
245: consoleReader.printNewline();
246: return null;
247: }
248: consoleReader.printNewline();
249: consoleReader.flushConsole();
250: String answer = consoleReader
251: .readLine(
252: "Install Services [enter a comma separated list of numbers or 'q' to quit]: ")
253: .trim();
254: if (answer.equalsIgnoreCase("q")) {
255: return null;
256: }
257: PluginListType list = new PluginListType();
258: for (String instance : answer.split(",")) {
259: int selection = Integer.parseInt(instance.trim());
260: PluginType target = available.get(selection - 1);
261: list.getPlugin().add(target);
262: }
263: if (repo != null) {
264: list.getDefaultRepository().add(repo);
265: }
266: return list;
267: }
268:
269: public void installPlugins(GeronimoDeploymentManager mgr,
270: PluginListType list, String defaultRepository,
271: ConsoleReader consoleReader, ServerConnection connection)
272: throws IOException, DeploymentException {
273: long start = System.currentTimeMillis();
274: Object key = mgr.startInstall(list, defaultRepository, false,
275: null, null);
276: DownloadResults results = CommandInstallCAR.showProgress(
277: consoleReader, mgr, key);
278: int time = (int) (System.currentTimeMillis() - start) / 1000;
279: CommandInstallCAR.printResults(consoleReader, results, time);
280: }
281:
282: public void installPlugins(GeronimoDeploymentManager mgr,
283: List<String> list, PluginListType all,
284: String defaultRepository, ConsoleReader consoleReader,
285: ServerConnection connection) throws IOException,
286: DeploymentException {
287: PluginListType selected = getPluginsFromIds(list, all);
288: installPlugins(mgr, selected, defaultRepository, consoleReader,
289: connection);
290: }
291:
292: private static PluginListType getPluginsFromIds(
293: List<String> configIds, PluginListType list)
294: throws IllegalStateException {
295: PluginListType installList = new PluginListType();
296: for (String configId : configIds) {
297: PluginType plugin = null;
298: for (PluginType metadata : list.getPlugin()) {
299: for (PluginArtifactType testInstance : metadata
300: .getPluginArtifact()) {
301: if (PluginInstallerGBean.toArtifact(
302: testInstance.getModuleId()).toString()
303: .equals(configId)) {
304: plugin = PluginInstallerGBean.copy(metadata,
305: testInstance);
306: installList.getPlugin().add(plugin);
307: break;
308: }
309: }
310: }
311: if (plugin == null) {
312: throw new IllegalStateException(
313: "No configuration found for '" + configId + "'");
314: }
315: }
316: return installList;
317: }
318:
319: public void assembleServer(GeronimoDeploymentManager mgr,
320: PluginListType list, String repositoryPath,
321: String relativeServerPath, ConsoleReader consoleReader)
322: throws Exception {
323: long start = System.currentTimeMillis();
324: DownloadResults results = mgr.installPluginList(repositoryPath,
325: relativeServerPath, list);
326: int time = (int) (System.currentTimeMillis() - start) / 1000;
327: CommandInstallCAR.printResults(consoleReader, results, time);
328: }
329:
330: public void assembleServer(GeronimoDeploymentManager mgr,
331: List<String> list, PluginListType all,
332: String repositoryPath, String relativeServerPath,
333: ConsoleReader consoleReader) throws Exception {
334: PluginListType selected = getPluginsFromIds(list, all);
335: assembleServer(mgr, selected, repositoryPath,
336: relativeServerPath, consoleReader);
337: }
338:
339: }
|