Method: filter(Set, String, String[])

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.Collections;
17: import java.util.HashSet;
18: import java.util.List;
19: import java.util.Set;
20:
21: import javax.management.MBeanServer;
22: import javax.management.ObjectName;
23:
24: import org.eclipse.virgo.kernel.model.management.RuntimeArtifactModelObjectNameCreator;
25: import org.eclipse.virgo.shell.CommandCompleter;
26:
27: class AbstractInstallArtifactCompleter implements CommandCompleter {
28:
29: private static final String SUBCOMMAND_LIST = "list";
30:
31: private final MBeanServer server = ManagementFactory.getPlatformMBeanServer();
32:
33: private final String type;
34:
35: private final RuntimeArtifactModelObjectNameCreator objectNameCreator;
36:
37: public AbstractInstallArtifactCompleter(String type, RuntimeArtifactModelObjectNameCreator objectNameCreator) {
38: this.type = type;
39: this.objectNameCreator = objectNameCreator;
40: }
41:
42: public final List<String> getCompletionCandidates(String subcommand, String... tokens) {
43: Set<String> candidates;
44:
45: if (SUBCOMMAND_LIST.equals(subcommand)) {
46: candidates = Collections.<String> emptySet();
47: } else if (tokens.length == 2) {
48: candidates = versions(tokens[0], tokens[1]);
49: } else if (tokens.length == 1) {
50: candidates = names(tokens[0]);
51: } else {
52: candidates = Collections.<String> emptySet();
53: }
54:
55: filter(candidates, subcommand, tokens);
56: List<String> candidateList = new ArrayList<String>(candidates);
57: Collections.sort(candidateList);
58: return candidateList;
59: }
60:
61: /**
62: * To be over ridden by sub-classes that want to filter the completions to be offered back to the user.
63: *
64: * @param candidates
65: * @param subcommand
66: * @param tokens
67: */
68: protected void filter(Set<String> candidates, String subcommand, String... tokens) {
69: }
70:
71: private Set<String> versions(String name, String version) {
72: Set<String> candidates = new HashSet<String>();
73:
74: Set<ObjectName> objectNames = this.server.queryNames(this.objectNameCreator.createArtifactVersionsQuery(this.type, name), null);
75: for (ObjectName objectName : objectNames) {
76: String candidateVersion = this.objectNameCreator.getVersion(objectName);
77: if (candidateVersion.startsWith(version)) {
78: candidates.add(candidateVersion);
79: }
80: }
81:
82: return candidates;
83: }
84:
85: private Set<String> names(String name) {
86: Set<String> candidates = new HashSet<String>();
87:
88: Set<ObjectName> objectNames = this.server.queryNames(this.objectNameCreator.createArtifactsOfTypeQuery(this.type), null);
89: for (ObjectName objectName : objectNames) {
90: String candidateName = this.objectNameCreator.getName(objectName);
91: if (candidateName.startsWith(name)) {
92: candidates.add(candidateName);
93: }
94: }
95:
96: return candidates;
97: }
98: }