Skip to content

Commit

Permalink
Add support for Coordinator to Equinox Configuration Admin
Browse files Browse the repository at this point in the history
Chapter 11 of the Configuration Admin Service Specification mandates the
support of the Coordinator service but currently Equinox fails the TCK
tests in that area.

This adds support for participation in the coordinator for the Equinox
Configuration Admin.
  • Loading branch information
laeubi committed Feb 18, 2024
1 parent 841faf8 commit 6f5f6fa
Show file tree
Hide file tree
Showing 4 changed files with 91 additions and 26 deletions.
3 changes: 2 additions & 1 deletion bundles/org.eclipse.equinox.cm/META-INF/MANIFEST.MF
Original file line number Diff line number Diff line change
Expand Up @@ -8,8 +8,9 @@ Bundle-Version: 1.6.0.qualifier
Bundle-Activator: org.eclipse.equinox.internal.cm.Activator
Import-Package: org.osgi.framework;version="1.7.0",
org.osgi.service.cm;version="[1.6,1.7)",
org.osgi.service.coordinator;version="[1.0.0,2.0.0]",
org.osgi.service.event;version="1.0";resolution:=optional,
org.osgi.service.log;version="1.3.0",
org.osgi.service.event;version="1.0"; resolution:=optional,
org.osgi.util.tracker;version="1.3.1"
Bundle-RequiredExecutionEnvironment: JavaSE-17
Provide-Capability:
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*******************************************************************************
* Copyright (c) 2005, 2019 Cognos Incorporated, IBM Corporation and others.
* Copyright (c) 2005, 2024 Cognos Incorporated, IBM Corporation and others.
*
* This program and the accompanying materials
* are made available under the terms of the Eclipse Public License 2.0
Expand All @@ -11,14 +11,19 @@
* Contributors:
* Cognos Incorporated - initial API and implementation
* IBM Corporation - bug fixes and enhancements
* Christoph Läubrich - add support for Coordinator
*******************************************************************************/
package org.eclipse.equinox.internal.cm;

import java.security.Permission;
import java.util.Dictionary;
import java.util.*;
import java.util.concurrent.ConcurrentHashMap;
import java.util.function.Predicate;
import org.osgi.framework.*;
import org.osgi.service.cm.ConfigurationAdmin;
import org.osgi.service.cm.ConfigurationPermission;
import org.osgi.service.coordinator.*;
import org.osgi.util.tracker.ServiceTracker;

/**
* ConfigurationAdminFactory provides a Configuration Admin ServiceFactory but
Expand All @@ -37,6 +42,9 @@ public class ConfigurationAdminFactory implements ServiceFactory<ConfigurationAd
private final ManagedServiceTracker managedServiceTracker;
private final ManagedServiceFactoryTracker managedServiceFactoryTracker;
private final ConfigurationStore configurationStore;
private final ServiceTracker<Coordinator, Coordinator> coordinationServiceTracker;

private final Map<Long, ConfigurationAdminParticipant> coordinationParticipants = new ConcurrentHashMap<>();

public ConfigurationAdminFactory(BundleContext context, LogTracker log) {
this.log = log;
Expand All @@ -45,9 +53,11 @@ public ConfigurationAdminFactory(BundleContext context, LogTracker log) {
pluginManager = new PluginManager(context);
managedServiceTracker = new ManagedServiceTracker(this, configurationStore, context);
managedServiceFactoryTracker = new ManagedServiceFactoryTracker(this, configurationStore, context);
coordinationServiceTracker = new ServiceTracker<>(context, Coordinator.class, null);
}

void start() {
coordinationServiceTracker.open();
eventDispatcher.start();
pluginManager.start();
managedServiceTracker.open();
Expand All @@ -59,6 +69,7 @@ void stop() {
managedServiceFactoryTracker.close();
eventDispatcher.stop();
pluginManager.stop();
coordinationServiceTracker.close();
}

@Override
Expand Down Expand Up @@ -137,17 +148,24 @@ void dispatchEvent(int type, String factoryPid, String pid) {
}

void notifyConfigurationUpdated(ConfigurationImpl config, boolean isFactory) {
if (isFactory)
if (isFactory) {
managedServiceFactoryTracker.notifyUpdated(config);
else
} else {
managedServiceTracker.notifyUpdated(config);
}
}

ConfigurationAdminParticipant coordinationParticipant(Coordination coordination) {
return coordinationParticipants.computeIfAbsent(coordination.getId(),
c -> new ConfigurationAdminParticipant(coordination));
}

void notifyConfigurationDeleted(ConfigurationImpl config, boolean isFactory) {
if (isFactory)
if (isFactory) {
managedServiceFactoryTracker.notifyDeleted(config);
else
} else {
managedServiceTracker.notifyDeleted(config);
}
}

void notifyLocationChanged(ConfigurationImpl config, String oldLocation, boolean isFactory) {
Expand All @@ -161,4 +179,50 @@ void notifyLocationChanged(ConfigurationImpl config, String oldLocation, boolean
Dictionary<String, Object> modifyConfiguration(ServiceReference<?> reference, ConfigurationImpl config) {
return pluginManager.modifyConfiguration(reference, config);
}

Optional<Coordination> coordinate() {
return Optional.ofNullable(coordinationServiceTracker.getService()).map(Coordinator::peek)
.filter(Predicate.not(Coordination::isTerminated));
}

private final class ConfigurationAdminParticipant implements Participant {

private Map<Object, Runnable> tasks = new HashMap<>();

public ConfigurationAdminParticipant(Coordination coordination) {
coordination.addParticipant(this);
}

@Override
public void ended(Coordination coordination) throws Exception {
finish(coordination);
}

@Override
public void failed(Coordination coordination) throws Exception {
finish(coordination);
}

private void finish(Coordination coordination) {
tasks.values().forEach(Runnable::run);
tasks.clear();
}

public void cancelTask(Object key) {
tasks.remove(key);
}

public void addTask(Object key, Runnable runnable) {
tasks.put(key, runnable);
}
}

void executeCoordinated(Object key, Runnable runnable) {
coordinate().ifPresentOrElse(coordination -> coordinationParticipant(coordination).addTask(key, runnable),
() -> runnable.run());
}

void cancelExecuteCoordinated(Object key) {
coordinate().ifPresent(coordination -> coordinationParticipant(coordination).cancelTask(key));
}
}
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*******************************************************************************
* Copyright (c) 2005, 2018 Cognos Incorporated, IBM Corporation and others.
* Copyright (c) 2005, 2024 Cognos Incorporated, IBM Corporation and others.
*
* This program and the accompanying materials
* are made available under the terms of the Eclipse Public License 2.0
Expand All @@ -11,6 +11,7 @@
* Contributors:
* Cognos Incorporated - initial API and implementation
* IBM Corporation - bug fixes and enhancements
* Christoph Läubrich - add support for Coordinator
*******************************************************************************/
package org.eclipse.equinox.internal.cm;

Expand Down Expand Up @@ -286,12 +287,12 @@ public void run() {

private void asynchUpdated(final ManagedServiceFactory service, final String pid,
final Dictionary<String, Object> properties) {
configurationAdminFactory.cancelExecuteCoordinated(service);
if (properties == null) {
return;
}
queue.put(new Runnable() {
@Override
public void run() {
configurationAdminFactory.executeCoordinated(service, () -> {
queue.put(() -> {
try {
service.updated(pid, properties);
} catch (ConfigurationException e) {
Expand All @@ -301,7 +302,7 @@ public void run() {
} catch (Throwable t) {
configurationAdminFactory.error(t.getMessage(), t);
}
}
});
});
}
}
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*******************************************************************************
* Copyright (c) 2005, 2018 Cognos Incorporated, IBM Corporation and others.
* Copyright (c) 2005, 2024 Cognos Incorporated, IBM Corporation and others.
*
* This program and the accompanying materials
* are made available under the terms of the Eclipse Public License 2.0
Expand All @@ -11,6 +11,7 @@
* Contributors:
* Cognos Incorporated - initial API and implementation
* IBM Corporation - bug fixes and enhancements
* Christoph Läubrich - add support for Coordinator
*******************************************************************************/
package org.eclipse.equinox.internal.cm;

Expand Down Expand Up @@ -283,19 +284,17 @@ private List<ServiceReference<ManagedService>> getManagedServiceReferences(Strin
}

private void asynchUpdated(final ManagedService service, final Dictionary<String, ?> properties) {
queue.put(new Runnable() {
@Override
public void run() {
try {
service.updated(properties);
} catch (ConfigurationException e) {
// we might consider doing more for ConfigurationExceptions
Throwable cause = e.getCause();
configurationAdminFactory.error(e.getMessage(), cause != null ? cause : e);
} catch (Throwable t) {
configurationAdminFactory.error(t.getMessage(), t);
}
configurationAdminFactory.cancelExecuteCoordinated(service);
configurationAdminFactory.executeCoordinated(service, () -> queue.put(() -> {
try {
service.updated(properties);
} catch (ConfigurationException e) {
// we might consider doing more for ConfigurationExceptions
Throwable cause = e.getCause();
configurationAdminFactory.error(e.getMessage(), cause != null ? cause : e);
} catch (Throwable t) {
configurationAdminFactory.error(t.getMessage(), t);
}
});
}));
}
}

0 comments on commit 6f5f6fa

Please sign in to comment.