Skip to content

Commit

Permalink
address review comment
Browse files Browse the repository at this point in the history
Signed-off-by: Jan N. Klug <github@klug.nrw>
  • Loading branch information
J-N-K committed May 4, 2022
1 parent ba3cad4 commit a84abbe
Show file tree
Hide file tree
Showing 5 changed files with 28 additions and 28 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -76,7 +76,7 @@ public String getComponent() {
/**
* Sets the type of the component.
*
* @return the component type
* @param component the component type
*/
public void setComponent(String component) {
this.component = component;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@
* @author Łukasz Dywicki - Initial contribution
*/
@NonNullByDefault
public interface UIProvider extends Provider<RootUIComponent> {
public interface UIComponentProvider extends Provider<RootUIComponent> {
String CONFIG_NAMESPACE = "ui.namespace";

String getNamespace();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@
import org.openhab.core.storage.Storage;
import org.openhab.core.storage.StorageService;
import org.openhab.core.ui.components.RootUIComponent;
import org.openhab.core.ui.components.UIProvider;
import org.openhab.core.ui.components.UIComponentProvider;
import org.osgi.service.component.annotations.Activate;
import org.osgi.service.component.annotations.Component;
import org.osgi.service.component.annotations.Reference;
Expand All @@ -38,14 +38,14 @@
*/
@NonNullByDefault
@Component(factory = "org.openhab.core.ui.component.provider.factory")
public class UIComponentProvider extends AbstractProvider<RootUIComponent>
implements ManagedProvider<RootUIComponent, String>, UIProvider {
public class ManagedUIComponentProvider extends AbstractProvider<RootUIComponent>
implements ManagedProvider<RootUIComponent, String>, UIComponentProvider {

private final String namespace;
private final Storage<RootUIComponent> storage;

@Activate
public UIComponentProvider(@Reference StorageService storageService, Map<String, Object> config) {
public ManagedUIComponentProvider(@Reference StorageService storageService, Map<String, Object> config) {
String namespace = ConfigParser.valueAs(config.get(CONFIG_NAMESPACE), String.class);
if (namespace == null) {
throw new IllegalStateException("'ui.namespace' must not be null in service configuration");
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,8 +23,8 @@

import org.eclipse.jdt.annotation.NonNullByDefault;
import org.openhab.core.common.registry.ManagedProvider;
import org.openhab.core.ui.components.UIComponentProvider;
import org.openhab.core.ui.components.UIComponentRegistryFactory;
import org.openhab.core.ui.components.UIProvider;
import org.osgi.framework.BundleContext;
import org.osgi.framework.InvalidSyntaxException;
import org.osgi.service.component.ComponentFactory;
Expand All @@ -39,7 +39,7 @@
import org.slf4j.LoggerFactory;

/**
* Implementation for a {@link UIComponentRegistryFactory} using a set of {@link UIComponentProvider}.
* Implementation for a {@link UIComponentRegistryFactory} using a set of {@link ManagedUIComponentProvider}.
*
* @author Yannick Schaus - Initial contribution
* @author Łukasz Dywicki - Removed explicit dependency on storage providers.
Expand All @@ -50,16 +50,16 @@
public class UIComponentRegistryFactoryImpl implements UIComponentRegistryFactory {
private final Logger logger = LoggerFactory.getLogger(UIComponentRegistryFactoryImpl.class);

private final ComponentFactory<UIComponentProvider> providerFactory;
private final ComponentFactory<ManagedUIComponentProvider> providerFactory;
private final BundleContext bc;

Map<String, UIComponentRegistryImpl> registries = new ConcurrentHashMap<>();
Set<ComponentInstance<UIComponentProvider>> createdProviders = new CopyOnWriteArraySet<>();
Map<String, Set<UIProvider>> providers = new ConcurrentHashMap<>();
Set<ComponentInstance<ManagedUIComponentProvider>> createdProviders = new CopyOnWriteArraySet<>();
Map<String, Set<UIComponentProvider>> providers = new ConcurrentHashMap<>();

@Activate
public UIComponentRegistryFactoryImpl(
@Reference(target = "(component.factory=org.openhab.core.ui.component.provider.factory)") ComponentFactory<UIComponentProvider> factory,
@Reference(target = "(component.factory=org.openhab.core.ui.component.provider.factory)") ComponentFactory<ManagedUIComponentProvider> factory,
BundleContext bc) {
this.providerFactory = factory;
this.bc = bc;
Expand All @@ -72,11 +72,11 @@ public UIComponentRegistryImpl getRegistry(String namespace) {
if (!managedProviderAvailable(namespace)) {
logger.debug("Creating managed provider for '{}'", namespace);
Dictionary<String, Object> properties = new Hashtable<>();
properties.put(UIProvider.CONFIG_NAMESPACE, namespace);
ComponentInstance<UIComponentProvider> instance = this.providerFactory.newInstance(properties);
properties.put(UIComponentProvider.CONFIG_NAMESPACE, namespace);
ComponentInstance<ManagedUIComponentProvider> instance = this.providerFactory.newInstance(properties);
createdProviders.add(instance);
}
Set<UIProvider> namespaceProviders = this.providers.get(namespace);
Set<UIComponentProvider> namespaceProviders = this.providers.get(namespace);
registry = new UIComponentRegistryImpl(namespace, namespaceProviders);
registries.put(namespace, registry);
}
Expand All @@ -90,47 +90,47 @@ public void deactivate() {

private boolean managedProviderAvailable(String namespace) {
try {
return bc.getServiceReferences(UIProvider.class, null).stream().map(bc::getService)
return bc.getServiceReferences(UIComponentProvider.class, null).stream().map(bc::getService)
.anyMatch(s -> namespace.equals(s.getNamespace()) && s instanceof ManagedProvider<?, ?>);
} catch (InvalidSyntaxException e) {
return false;
}
}

@Reference(cardinality = ReferenceCardinality.MULTIPLE, policy = ReferencePolicy.DYNAMIC)
void addProvider(UIProvider provider) {
void addProvider(UIComponentProvider provider) {
UIComponentRegistryImpl registry = registries.get(provider.getNamespace());
if (registry != null) {
registry.addProvider(provider);
}
registerProvider(provider);
}

void removeProvider(UIProvider provider) {
void removeProvider(UIComponentProvider provider) {
UIComponentRegistryImpl registry = registries.get(provider.getNamespace());
if (registry != null) {
registry.removeProvider(provider);
}
unregisterProvider(provider);
}

private void registerProvider(UIProvider provider) {
Set<UIProvider> existing = providers.get(provider.getNamespace());
private void registerProvider(UIComponentProvider provider) {
Set<UIComponentProvider> existing = providers.get(provider.getNamespace());

if (existing == null) {
existing = Collections.emptySet();
}

Set<UIProvider> updated = new HashSet<>(existing);
Set<UIComponentProvider> updated = new HashSet<>(existing);
updated.add(provider);
providers.put(provider.getNamespace(), Set.copyOf(updated));
}

private void unregisterProvider(UIProvider provider) {
Set<UIProvider> existing = providers.get(provider.getNamespace());
private void unregisterProvider(UIComponentProvider provider) {
Set<UIComponentProvider> existing = providers.get(provider.getNamespace());

if (existing != null && !existing.isEmpty()) {
Set<UIProvider> updated = new HashSet<>(existing);
Set<UIComponentProvider> updated = new HashSet<>(existing);
updated.remove(provider);
providers.put(provider.getNamespace(), Set.copyOf(updated));
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,26 +20,26 @@
import org.openhab.core.common.registry.ManagedProvider;
import org.openhab.core.common.registry.Provider;
import org.openhab.core.ui.components.RootUIComponent;
import org.openhab.core.ui.components.UIComponentProvider;
import org.openhab.core.ui.components.UIComponentRegistry;
import org.openhab.core.ui.components.UIProvider;

/**
* Implementation of a {@link UIComponentRegistry} using a {@link UIComponentProvider}.
* Implementation of a {@link UIComponentRegistry} using a {@link ManagedUIComponentProvider}.
* It is instantiated by the {@link UIComponentRegistryFactoryImpl}.
*
* @author Yannick Schaus - Initial contribution
* @author Łukasz Dywicki - Support for dynamic registration of providers
*/
@NonNullByDefault
public class UIComponentRegistryImpl extends AbstractRegistry<RootUIComponent, String, UIComponentProvider>
public class UIComponentRegistryImpl extends AbstractRegistry<RootUIComponent, String, ManagedUIComponentProvider>
implements UIComponentRegistry {

/**
* Constructs a UI component registry for the specified namespace.
*
* @param namespace UI components namespace of this registry
*/
public UIComponentRegistryImpl(String namespace, @Nullable Set<UIProvider> providers) {
public UIComponentRegistryImpl(String namespace, @Nullable Set<UIComponentProvider> providers) {
super(null);
if (providers != null && !providers.isEmpty()) {
for (Provider<RootUIComponent> provider : providers) {
Expand Down

0 comments on commit a84abbe

Please sign in to comment.