Package: CommandRegistry

CommandRegistry

nameinstructionbranchcomplexitylinemethod
CommandRegistry(CommandResolver, BundleContext)
M: 31 C: 0
0%
M: 0 C: 0
100%
M: 1 C: 0
0%
M: 8 C: 0
0%
M: 1 C: 0
0%
getCommandDescriptors()
M: 13 C: 0
0%
M: 0 C: 0
100%
M: 1 C: 0
0%
M: 2 C: 0
0%
M: 1 C: 0
0%
initialize()
M: 41 C: 0
0%
M: 4 C: 0
0%
M: 3 C: 0
0%
M: 8 C: 0
0%
M: 1 C: 0
0%
serviceRegistered(ServiceReference)
M: 36 C: 0
0%
M: 4 C: 0
0%
M: 3 C: 0
0%
M: 8 C: 0
0%
M: 1 C: 0
0%
serviceUnregistering(ServiceReference)
M: 22 C: 0
0%
M: 2 C: 0
0%
M: 2 C: 0
0%
M: 5 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;
13:
14: import java.util.ArrayList;
15: import java.util.HashMap;
16: import java.util.List;
17: import java.util.Map;
18:
19: import org.osgi.framework.BundleContext;
20: import org.osgi.framework.InvalidSyntaxException;
21: import org.osgi.framework.ServiceEvent;
22: import org.osgi.framework.ServiceListener;
23: import org.osgi.framework.ServiceReference;
24:
25: /**
26: * A <code>CommandRegistry</code> maintains a list of {@link CommandDescriptor CommandDescriptors} based on the contents
27: * of the OSGi service registry. Descriptors are created using a {@link CommandResolver}.
28: * <p />
29: *
30: * <strong>Concurrent Semantics</strong><br />
31: *
32: * Thread-safe.
33: *
34: */
35: public final class CommandRegistry {
36:
37: private final CommandResolver commandResolver;
38:
39: private final BundleContext bundleContext;
40:
41: private final List<CommandDescriptor> commandDescriptors = new ArrayList<CommandDescriptor>();
42:
43: private final Map<ServiceReference<?>, List<CommandDescriptor>> commandDescriptorsByService = new HashMap<ServiceReference<?>, List<CommandDescriptor>>();
44:
45: private final Object monitor = new Object();
46:
47: private final CommandRegistryServiceListener commandRegistryServiceListener = new CommandRegistryServiceListener();
48:
49: /**
50: * @param commandResolver
51: * @param bundleContext
52: */
53: public CommandRegistry(CommandResolver commandResolver, BundleContext bundleContext) {
54: this.commandResolver = commandResolver;
55: this.bundleContext = bundleContext;
56: }
57:
58: void initialize() {
59: // TODO Limit with a filter
60: this.bundleContext.addServiceListener(this.commandRegistryServiceListener);
61: try {
62: // TODO Limit with a filter
63: ServiceReference<?>[] serviceReferences = this.bundleContext.getServiceReferences((String)null, null);
64:• if (serviceReferences != null) {
65:• for (ServiceReference<?> serviceReference : serviceReferences) {
66: serviceRegistered(serviceReference);
67: }
68: }
69: } catch (InvalidSyntaxException e) {
70: throw new RuntimeException("Unexpected InvalidSyntaxException", e);
71: }
72: }
73:
74: public List<CommandDescriptor> getCommandDescriptors() {
75: synchronized(this.monitor) {
76: return new ArrayList<CommandDescriptor>(this.commandDescriptors);
77: }
78: }
79:
80:
81: private void serviceRegistered(ServiceReference<?> serviceReference) {
82: Object service = bundleContext.getService(serviceReference);
83:• if (service != null) {
84: List<CommandDescriptor> commands = commandResolver.resolveCommands(serviceReference, service);
85:• if (!commands.isEmpty()) {
86: synchronized (this.monitor) {
87: this.commandDescriptors.addAll(commands);
88: this.commandDescriptorsByService.put(serviceReference, commands);
89: }
90: }
91: }
92: }
93:
94: private void serviceUnregistering(ServiceReference<?> serviceReference) {
95: synchronized (this.monitor) {
96: List<CommandDescriptor> commandDescriptorsForService = this.commandDescriptorsByService.remove(serviceReference);
97:• if (commandDescriptorsForService != null) {
98: this.commandDescriptors.removeAll(commandDescriptorsForService);
99: }
100: }
101: }
102:
103: private final class CommandRegistryServiceListener implements ServiceListener {
104:
105: /**
106: * {@inheritDoc}
107: */
108: public void serviceChanged(ServiceEvent event) {
109: if (ServiceEvent.REGISTERED == event.getType()) {
110: serviceRegistered(event.getServiceReference());
111: } else if (ServiceEvent.UNREGISTERING == event.getType()) {
112: serviceUnregistering(event.getServiceReference());
113: }
114: }
115: }
116: }