Method: PackageCompleter(QuasiFrameworkFactory)

1: /*******************************************************************************
2: * Copyright (c) 2008, 2010 VMware Inc.
3: * All rights reserved. This program and the accompanying materials
4: * are made available under the terms of the Eclipse Public License v1.0
5: * which accompanies this distribution, and is available at
6: * http://www.eclipse.org/legal/epl-v10.html
7: *
8: * Contributors:
9: * VMware Inc. - initial contribution
10: *******************************************************************************/
11:
12: package org.eclipse.virgo.shell.internal.completers;
13:
14: import java.util.ArrayList;
15: import java.util.Collections;
16: import java.util.HashSet;
17: import java.util.List;
18: import java.util.Set;
19:
20: import org.eclipse.virgo.kernel.osgi.quasi.QuasiBundle;
21: import org.eclipse.virgo.kernel.osgi.quasi.QuasiExportPackage;
22: import org.eclipse.virgo.kernel.osgi.quasi.QuasiFrameworkFactory;
23: import org.eclipse.virgo.shell.CommandCompleter;
24:
25: public class PackageCompleter implements CommandCompleter {
26:
27: private static final String SUBCOMMAND_LIST = "list";
28:
29:         private QuasiFrameworkFactory quasiFrameworkFactory;
30:
31: public PackageCompleter(QuasiFrameworkFactory quasiFrameworkFactory) {
32: this.quasiFrameworkFactory = quasiFrameworkFactory;
33: }
34:
35: public List<String> getCompletionCandidates(String subcommand, String... tokens) {
36: Set<String> candidates;
37:
38: if (SUBCOMMAND_LIST.equals(subcommand)) {
39: candidates = Collections.<String> emptySet();
40: } else if (tokens.length == 2) {
41: candidates = versions(tokens[0], tokens[1]);
42: } else if (tokens.length == 1) {
43: candidates = names(tokens[0]);
44: } else {
45: candidates = Collections.<String> emptySet();
46: }
47:
48: List<String> candidateList = new ArrayList<String>(candidates);
49: Collections.sort(candidateList);
50: return candidateList;
51: }
52:
53: private Set<String> versions(String name, String version) {
54: Set<String> versions = new HashSet<String>();
55:
56: for (QuasiExportPackage exportPackage : getAllPackages()) {
57: String packageName = exportPackage.getPackageName();
58: String packageVersion = exportPackage.getVersion().toString();
59: if (packageName.equals(name) && packageVersion.startsWith(version)) {
60: versions.add(packageVersion);
61: }
62: }
63:
64: return versions;
65: }
66:
67: private Set<String> names(String name) {
68: Set<String> names = new HashSet<String>();
69:
70: for (QuasiExportPackage exportPackage : getAllPackages()) {
71: String packageName = exportPackage.getPackageName();
72: if (packageName.startsWith(name)) {
73: names.add(packageName);
74: }
75: }
76:
77: return names;
78: }
79:
80: private List<QuasiExportPackage> getAllPackages() {
81: List<QuasiExportPackage> packages = new ArrayList<QuasiExportPackage>();
82: for (QuasiBundle bundle : this.quasiFrameworkFactory.create().getBundles()) {
83: packages.addAll(bundle.getExportPackages());
84: }
85: return packages;
86: }
87: }