Package: InstallCompleter

InstallCompleter

nameinstructionbranchcomplexitylinemethod
InstallCompleter()
M: 18 C: 0
0%
M: 0 C: 0
100%
M: 1 C: 0
0%
M: 5 C: 0
0%
M: 1 C: 0
0%
file(String)
M: 52 C: 0
0%
M: 4 C: 0
0%
M: 3 C: 0
0%
M: 9 C: 0
0%
M: 1 C: 0
0%
getCompletionCandidates(String, String[])
M: 63 C: 0
0%
M: 10 C: 0
0%
M: 6 C: 0
0%
M: 15 C: 0
0%
M: 1 C: 0
0%
getRepositories()
M: 65 C: 0
0%
M: 6 C: 0
0%
M: 4 C: 0
0%
M: 9 C: 0
0%
M: 1 C: 0
0%
getUriParts(String)
M: 21 C: 0
0%
M: 2 C: 0
0%
M: 2 C: 0
0%
M: 4 C: 0
0%
M: 1 C: 0
0%
name(String, String)
M: 57 C: 0
0%
M: 8 C: 0
0%
M: 5 C: 0
0%
M: 6 C: 0
0%
M: 1 C: 0
0%
repository(String)
M: 51 C: 0
0%
M: 6 C: 0
0%
M: 4 C: 0
0%
M: 8 C: 0
0%
M: 1 C: 0
0%
static {...}
M: 12 C: 0
0%
M: 0 C: 0
100%
M: 1 C: 0
0%
M: 1 C: 0
0%
M: 1 C: 0
0%
type(String)
M: 47 C: 0
0%
M: 6 C: 0
0%
M: 4 C: 0
0%
M: 6 C: 0
0%
M: 1 C: 0
0%
version(String, String, String)
M: 67 C: 0
0%
M: 10 C: 0
0%
M: 6 C: 0
0%
M: 6 C: 0
0%
M: 1 C: 0
0%

Coverage

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.lang.management.ManagementFactory;
15: import java.util.ArrayList;
16: import java.util.Arrays;
17: import java.util.Collections;
18: import java.util.HashSet;
19: import java.util.List;
20: import java.util.Set;
21:
22: import javax.management.JMX;
23: import javax.management.MBeanServer;
24: import javax.management.MalformedObjectNameException;
25: import javax.management.ObjectName;
26:
27: import org.eclipse.virgo.repository.management.ArtifactDescriptorSummary;
28: import org.eclipse.virgo.repository.management.RepositoryInfo;
29: import org.eclipse.virgo.shell.CommandCompleter;
30:
31: public class InstallCompleter implements CommandCompleter {
32:
33: private static final String FILE_PREFIX = "file:";
34:
35: private static final String FILE_PATH_CANDIDATE_FORMAT = FILE_PREFIX + "%s%s";
36:
37: private static final String REPOSITORY_PREFIX = "repository:";
38:
39: private static final String REPOSITORY_TYPE_NAME_VERSION_FORMAT = REPOSITORY_PREFIX + "%s/%s/%s";
40:
41: private static final String REPOSITORY_TYPE_NAME_FORMAT = REPOSITORY_PREFIX + "%s/%s/";
42:
43: private static final String REPOSITORY_TYPE_FORMAT = REPOSITORY_PREFIX + "%s/";
44:
45: private static final String[] SCHEMES = { FILE_PREFIX, REPOSITORY_PREFIX };
46:
47: private final MBeanServer server = ManagementFactory.getPlatformMBeanServer();
48:
49: private final ObjectName repositoryQuery;
50:
51: private final ObjectName hostedRepositoryQuery;
52:
53: public InstallCompleter() throws MalformedObjectNameException, NullPointerException {
54: this.repositoryQuery = new ObjectName("org.eclipse.virgo.kernel:type=Repository,*");
55: this.hostedRepositoryQuery = new ObjectName("org.eclipse.virgo.server:type=HostedRepository,*");
56: }
57:
58: public List<String> getCompletionCandidates(String subcommand, String... arguments) {
59: Set<String> candidates;
60:• if (arguments.length == 0) {
61:• if (subcommand.startsWith(REPOSITORY_PREFIX)) {
62: candidates = repository(subcommand);
63:• } else if (subcommand.startsWith(FILE_PREFIX)) {
64: candidates = file(subcommand);
65: } else {
66: candidates = new HashSet<String>();
67:• for (String scheme : SCHEMES) {
68:• if (scheme.startsWith(subcommand)) {
69: candidates.add(scheme);
70: }
71: }
72: }
73: } else {
74: // We do not complete anything after the 'subcommand' argument
75: candidates = new HashSet<String>();
76: }
77:
78: List<String> candidateList = new ArrayList<String>(candidates);
79: Collections.sort(candidateList);
80: return candidateList;
81: }
82:
83: private Set<String> file(String subcommand) {
84: List<String> candidates = new ArrayList<String>();
85:
86: String path = subcommand.substring(FILE_PREFIX.length());
87: int completionIndex = 0; //new FileNameCompletor().complete(path, path.length(), candidates);
88:
89: Set<String> candidateSet = new HashSet<String>(candidates.size());
90:
91:• if (completionIndex >= 0) {
92: String completablePath = path.substring(0, completionIndex);
93:
94:• for (String candidate : candidates) {
95: candidateSet.add(String.format(FILE_PATH_CANDIDATE_FORMAT, completablePath, candidate));
96: }
97: }
98:
99: return candidateSet;
100: }
101:
102: private Set<String> repository(String subcommand) {
103: List<String> uriParts = getUriParts(subcommand);
104:
105:• if (uriParts.size() == 1) {
106: return type(uriParts.get(0));
107:• } else if (uriParts.size() == 2) {
108: return name(uriParts.get(0), uriParts.get(1));
109:• } else if (uriParts.size() == 3) {
110: return version(uriParts.get(0), uriParts.get(1), uriParts.get(2));
111: }
112: return Collections.emptySet();
113: }
114:
115: private List<String> getUriParts(String subcommand) {
116: List<String> uriParts = new ArrayList<String>(Arrays.asList(subcommand.substring(REPOSITORY_PREFIX.length()).split("/")));
117:• if (subcommand.endsWith("/")) {
118: uriParts.add("");
119: }
120: return uriParts;
121: }
122:
123: private Set<String> type(String type) {
124: Set<String> types = new HashSet<String>();
125:
126:• for (RepositoryInfo repository : getRepositories()) {
127:• for (ArtifactDescriptorSummary artifact : repository.getAllArtifactDescriptorSummaries()) {
128:• if (artifact.getType().startsWith(type)) {
129: types.add(String.format(REPOSITORY_TYPE_FORMAT, artifact.getType()));
130: }
131: }
132: }
133:
134: return types;
135: }
136:
137: private Set<String> name(String type, String name) {
138: Set<String> names = new HashSet<String>();
139:
140:• for (RepositoryInfo repository : getRepositories()) {
141:• for (ArtifactDescriptorSummary artifact : repository.getAllArtifactDescriptorSummaries()) {
142:• if (artifact.getType().equals(type) && artifact.getName().startsWith(name)) {
143: names.add(String.format(REPOSITORY_TYPE_NAME_FORMAT, artifact.getType(), artifact.getName()));
144: }
145: }
146: }
147:
148: return names;
149: }
150:
151: private Set<String> version(String type, String name, String version) {
152: Set<String> versions = new HashSet<String>();
153:
154:• for (RepositoryInfo repository : getRepositories()) {
155:• for (ArtifactDescriptorSummary artifact : repository.getAllArtifactDescriptorSummaries()) {
156:• if (artifact.getType().equals(type) && artifact.getName().equals(name) && artifact.getVersion().startsWith(version)) {
157: versions.add(String.format(REPOSITORY_TYPE_NAME_VERSION_FORMAT, artifact.getType(), artifact.getName(), artifact.getVersion()));
158: }
159: }
160: }
161:
162: return versions;
163: }
164:
165: private Set<RepositoryInfo> getRepositories() {
166: Set<String> hostedRepositoryNames = new HashSet<String>();
167:• for (ObjectName objectName : this.server.queryNames(this.hostedRepositoryQuery, null)) {
168: hostedRepositoryNames.add(objectName.getKeyProperty("name"));
169: }
170:
171: Set<RepositoryInfo> repositories = new HashSet<RepositoryInfo>();
172:• for (ObjectName objectName : this.server.queryNames(this.repositoryQuery, null)) {
173: String name = objectName.getKeyProperty("name");
174:• if (!hostedRepositoryNames.contains(name)) {
175: repositories.add(JMX.newMXBeanProxy(this.server, objectName, RepositoryInfo.class));
176: }
177: }
178: return repositories;
179: }
180: }