Package: ConfigCompleter

ConfigCompleter

nameinstructionbranchcomplexitylinemethod
ConfigCompleter(RuntimeArtifactModelObjectNameCreator, RegionDigraph)
M: 24 C: 0
0%
M: 0 C: 0
100%
M: 1 C: 0
0%
M: 6 C: 0
0%
M: 1 C: 0
0%
filter(Set, String, String[])
M: 23 C: 0
0%
M: 6 C: 0
0%
M: 4 C: 0
0%
M: 6 C: 0
0%
M: 1 C: 0
0%
filterNames(Set)
M: 53 C: 0
0%
M: 8 C: 0
0%
M: 5 C: 0
0%
M: 13 C: 0
0%
M: 1 C: 0
0%
filterVersions(String, Set)
M: 30 C: 0
0%
M: 4 C: 0
0%
M: 3 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.Iterator;
16: import java.util.Set;
17:
18: import javax.management.InstanceNotFoundException;
19: import javax.management.MBeanServer;
20: import javax.management.ObjectName;
21:
22: import org.osgi.framework.Version;
23:
24: import org.eclipse.equinox.region.Region;
25: import org.eclipse.equinox.region.RegionDigraph;
26: import org.eclipse.virgo.kernel.model.management.ManageableArtifact;
27: import org.eclipse.virgo.kernel.model.management.RuntimeArtifactModelObjectNameCreator;
28: import org.eclipse.virgo.shell.internal.util.ArtifactRetriever;
29:
30: final class ConfigCompleter extends AbstractInstallArtifactCompleter {
31:
32: private static final String TYPE = "configuration";
33:
34: private static final String COMMAND_EXAMINE = "examine";
35:
36: private static final String STATE_ACTIVE = "ACTIVE";
37:
38: private final Region globalRegion;
39:
40: private final MBeanServer server = ManagementFactory.getPlatformMBeanServer();
41:
42: private final RuntimeArtifactModelObjectNameCreator objectNameCreator;
43:
44: private final ArtifactRetriever<ManageableArtifact> artifactRetriever;
45:
46: public ConfigCompleter(RuntimeArtifactModelObjectNameCreator objectNameCreator, RegionDigraph regionDigraph) {
47: super(TYPE, objectNameCreator);
48: this.objectNameCreator = objectNameCreator;
49: this.globalRegion = regionDigraph.getRegion("global");
50: this.artifactRetriever = new ArtifactRetriever<ManageableArtifact>(TYPE, objectNameCreator, ManageableArtifact.class);
51: }
52:
53: @Override
54: protected void filter(Set<String> candidates, String subcommand, String... tokens) {
55:• if (COMMAND_EXAMINE.equals(subcommand)) {
56:• if (tokens.length == 2) {
57: filterVersions(tokens[0], candidates);
58:• } else if (tokens.length == 1) {
59: filterNames(candidates);
60: }
61: }
62: }
63:
64: private void filterVersions(String name, Set<String> candidates) {
65:• for (Iterator<String> i = candidates.iterator(); i.hasNext();) {
66: try {
67: ManageableArtifact artifact = this.artifactRetriever.getArtifact(name, new Version(i.next()), globalRegion);
68:• if (!STATE_ACTIVE.equals(artifact.getState())) {
69: i.remove();
70: }
71: } catch (InstanceNotFoundException e) {
72: // Swallow to allow others to proceed
73: }
74: }
75:
76: }
77:
78: private void filterNames(Set<String> candidates) {
79:• for (Iterator<String> i = candidates.iterator(); i.hasNext();) {
80: ObjectName createArtifactVersionsQuery = this.objectNameCreator.createArtifactVersionsQuery(TYPE, i.next());
81:                         Set<ObjectName> objectNames = this.server.queryNames(createArtifactVersionsQuery, null);
82: boolean hasActive = false;
83:• for (ObjectName objectName : objectNames) {
84: try {
85: ManageableArtifact artifact = this.artifactRetriever.getArtifact(objectName);
86:• if (STATE_ACTIVE.equals(artifact.getState())) {
87: hasActive = true;
88: break;
89: }
90: } catch (InstanceNotFoundException e) {
91: // Swallow to allow others to proceed
92: }
93: }
94:• if (!hasActive) {
95: i.remove();
96: }
97: }
98:
99: }
100:
101: }