Package: ServiceRegistryCommandCompleterRegistry$ConverterRegistryServiceListener

ServiceRegistryCommandCompleterRegistry$ConverterRegistryServiceListener

nameinstructionbranchcomplexitylinemethod
ServiceRegistryCommandCompleterRegistry.ConverterRegistryServiceListener(ServiceRegistryCommandCompleterRegistry)
M: 6 C: 0
0%
M: 0 C: 0
100%
M: 1 C: 0
0%
M: 1 C: 0
0%
M: 1 C: 0
0%
serviceChanged(ServiceEvent)
M: 20 C: 0
0%
M: 4 C: 0
0%
M: 3 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.completers;
13:
14: import java.util.HashMap;
15: import java.util.Map;
16:
17: import org.eclipse.virgo.shell.CommandCompleter;
18: import org.osgi.framework.BundleContext;
19: import org.osgi.framework.InvalidSyntaxException;
20: import org.osgi.framework.ServiceEvent;
21: import org.osgi.framework.ServiceListener;
22: import org.osgi.framework.ServiceReference;
23:
24:
25:
26: /**
27: * A dynamic registry of {@link CommandCompleter CommandCompleters} backed by the OSGi
28: * service registry.
29: * <p />
30: *
31: * <strong>Concurrent Semantics</strong><br />
32: *
33: * Thread-safe.
34: *
35: */
36: final class ServiceRegistryCommandCompleterRegistry implements CommandCompleterRegistry {
37:
38: private final ServiceListener commandCompleterRegistryServiceListener = new ConverterRegistryServiceListener();
39:
40: private final Map<String, CommandCompleter> completers = new HashMap<String, CommandCompleter>();
41:
42: private final Object monitor = new Object();
43:
44: private final BundleContext bundleContext;
45:
46: ServiceRegistryCommandCompleterRegistry(BundleContext bundleContext) {
47: this.bundleContext = bundleContext;
48: }
49:
50: /**
51: * {@inheritDoc}
52: */
53: public CommandCompleter getCommandCompleter(String commandName) {
54: synchronized(this.monitor) {
55: return this.completers.get(commandName);
56: }
57: }
58:
59: void initialize() {
60: try {
61: this.bundleContext.addServiceListener(this.commandCompleterRegistryServiceListener, "(objectClass=" + CommandCompleter.class.getName() + ")");
62: ServiceReference<?>[] serviceReferences = this.bundleContext.getServiceReferences(CommandCompleter.class.getName(), null);
63: if (serviceReferences != null) {
64: for (ServiceReference<?> serviceReference : serviceReferences) {
65: serviceRegistered(serviceReference);
66: }
67: }
68: } catch (InvalidSyntaxException e) {
69: throw new RuntimeException("Unexpected InvalidSyntaxException", e);
70: }
71: }
72:
73: private void serviceRegistered(ServiceReference<?> serviceReference) {
74: CommandCompleter completer = (CommandCompleter)bundleContext.getService(serviceReference);
75: if (completer != null) {
76: String[] commandNames = getCommandNames(serviceReference);
77: for (String commandName : commandNames) {
78: this.completers.put(commandName, completer);
79: }
80: }
81: }
82:
83: private String[] getCommandNames(ServiceReference<?> serviceReference) {
84: Object commandNamesProperty = serviceReference.getProperty(CommandCompleter.SERVICE_PROPERTY_COMPLETER_COMMAND_NAMES);
85: String[] commandNames;
86:
87: if (commandNamesProperty instanceof String[]) {
88: commandNames = (String[])commandNamesProperty;
89: } else if (commandNamesProperty instanceof String) {
90: commandNames = new String[] {(String)commandNamesProperty};
91: } else {
92: commandNames = new String[0];
93: }
94: return commandNames;
95: }
96:
97: private void serviceUnregistering(ServiceReference<?> serviceReference) {
98: Object converter = this.bundleContext.getService(serviceReference);
99: if (converter != null) {
100: String[] commandNames = getCommandNames(serviceReference);
101: synchronized (monitor) {
102: for (String commandName : commandNames) {
103: this.completers.remove(commandName);
104: }
105: }
106: }
107: }
108:
109: private final class ConverterRegistryServiceListener implements ServiceListener {
110:
111: /**
112: * {@inheritDoc}
113: */
114: public void serviceChanged(ServiceEvent event) {
115:• if (ServiceEvent.REGISTERED == event.getType()) {
116: serviceRegistered(event.getServiceReference());
117:• } else if (ServiceEvent.UNREGISTERING == event.getType()) {
118: serviceUnregistering(event.getServiceReference());
119: }
120: }
121: }
122: }