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 b177930
Show file tree
Hide file tree
Showing 6 changed files with 137 additions and 42 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,18 @@
* 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.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,17 +41,20 @@ public class ConfigurationAdminFactory implements ServiceFactory<ConfigurationAd
private final ManagedServiceTracker managedServiceTracker;
private final ManagedServiceFactoryTracker managedServiceFactoryTracker;
private final ConfigurationStore configurationStore;
private final ServiceTracker<Coordinator, Coordinator> coordinationServiceTracker;

public ConfigurationAdminFactory(BundleContext context, LogTracker log) {
this.log = log;
configurationStore = new ConfigurationStore(this, context);
eventDispatcher = new EventDispatcher(context, log);
eventDispatcher = new EventDispatcher(context, log, this);
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 +66,7 @@ void stop() {
managedServiceFactoryTracker.close();
eventDispatcher.stop();
pluginManager.stop();
coordinationServiceTracker.close();
}

@Override
Expand Down Expand Up @@ -161,4 +169,56 @@ void notifyLocationChanged(ConfigurationImpl config, String oldLocation, boolean
Dictionary<String, Object> modifyConfiguration(ServiceReference<?> reference, ConfigurationImpl config) {
return pluginManager.modifyConfiguration(reference, config);
}

ConfigurationAdminParticipant coordinationParticipant(Coordination coordination) {
return coordination.getParticipants().stream().filter(ConfigurationAdminParticipant.class::isInstance)
.map(ConfigurationAdminParticipant.class::cast).findFirst()
.orElseGet(() -> new ConfigurationAdminParticipant(coordination));
}

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 LinkedHashMap<>();

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
Expand Up @@ -21,6 +21,7 @@
import java.util.concurrent.locks.ReentrantLock;
import org.osgi.framework.*;
import org.osgi.service.cm.*;
import org.osgi.service.coordinator.Coordination;

/**
* ConfigurationImpl provides the Configuration implementation. The lock and
Expand Down Expand Up @@ -53,6 +54,7 @@ class ConfigurationImpl implements Configuration {
/** @GuardedBy lock */
private boolean readOnly = false;
private final ReentrantLock lock = new ReentrantLock();
private final long coordinationId;

public ConfigurationImpl(ConfigurationAdminFactory configurationAdminFactory, ConfigurationStore configurationStore,
String factoryPid, String pid, String bundleLocation, boolean bind) {
Expand All @@ -63,6 +65,7 @@ public ConfigurationImpl(ConfigurationAdminFactory configurationAdminFactory, Co
this.bundleLocation = bundleLocation;
this.changeCount = 0;
this.bound = bind;
this.coordinationId = configurationAdminFactory.coordinate().map(Coordination::getId).orElse(-1l);
}

public ConfigurationImpl(ConfigurationAdminFactory configurationAdminFactory, ConfigurationStore configurationStore,
Expand All @@ -83,6 +86,11 @@ public ConfigurationImpl(ConfigurationAdminFactory configurationAdminFactory, Co
updateDictionary(dictionary);
}
this.storageToken = storageToken;
this.coordinationId = -1;
}

long getCoordinationId() {
return coordinationId;
}

void lock() {
Expand Down
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 All @@ -33,9 +34,11 @@ public class EventDispatcher {
/** @GuardedBy this */
private ServiceReference<ConfigurationAdmin> configAdminReference;
final LogTracker log;
private ConfigurationAdminFactory configurationAdminFactory;

public EventDispatcher(BundleContext context, LogTracker log) {
public EventDispatcher(BundleContext context, LogTracker log, ConfigurationAdminFactory configurationAdminFactory) {
this.log = log;
this.configurationAdminFactory = configurationAdminFactory;
tracker = new ServiceTracker<>(context, ConfigurationListener.class, null);
syncTracker = new ServiceTracker<>(context, SynchronousConfigurationListener.class, null);
}
Expand Down Expand Up @@ -81,27 +84,32 @@ public void dispatchEvent(int type, String factoryPid, String pid) {
return;

for (final ServiceReference<ConfigurationListener> ref : refs) {
queue.put(new Runnable() {
@Override
public void run() {
ConfigurationListener listener = tracker.getService(ref);
if (listener == null) {
return;
}
try {
listener.configurationEvent(event);
} catch (Throwable t) {
log.error(t.getMessage(), t);
}
}
});
configurationAdminFactory.executeCoordinated(new Object(), () -> enqueue(event, ref));
}
}

protected void enqueue(final ConfigurationEvent event, final ServiceReference<ConfigurationListener> ref) {
queue.put(new Runnable() {
@Override
public void run() {
ConfigurationListener listener = tracker.getService(ref);
if (listener == null) {
return;
}
try {
listener.configurationEvent(event);
} catch (Throwable t) {
log.error(t.getMessage(), t);
}
}
});
}

private synchronized ConfigurationEvent createConfigurationEvent(int type, String factoryPid, String pid) {
if (configAdminReference == null)
return null;

return new ConfigurationEvent(configAdminReference, type, factoryPid, pid);
}

}
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,12 +11,14 @@
* 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.util.*;
import org.osgi.framework.*;
import org.osgi.service.cm.*;
import org.osgi.service.coordinator.Coordination;
import org.osgi.util.tracker.ServiceTracker;

/**
Expand Down Expand Up @@ -60,13 +62,29 @@ void notifyDeleted(ConfigurationImpl config) {
synchronized (targets) {
qualifiedPidLists = targets.getQualifiedPids(ref);
}
updateManagedService(qualifiedPidLists, ref, service);
if (isDeletedInCoordination(config)) {
// the configuration was created in a coordination but deleted before it was
// finished, the update was never delivered to the managed service so we do not
// need to propagate the delete either
configurationAdminFactory.cancelExecuteCoordinated(service);
} else {
updateManagedService(qualifiedPidLists, ref, service);
}
}
}
}
}
}

private boolean isDeletedInCoordination(ConfigurationImpl config) {
long coordinationId = config.getCoordinationId();
if (coordinationId > 0) {
long currentId = configurationAdminFactory.coordinate().map(Coordination::getId).orElse(-1l);
return currentId == coordinationId;
}
return false;
}

void notifyUpdated(ConfigurationImpl config) {
config.checkLocked();
String configLoc = config.getLocation();
Expand Down Expand Up @@ -202,13 +220,14 @@ public void modifiedService(ServiceReference<ManagedService> reference, ManagedS
@Override
public void removedService(ServiceReference<ManagedService> reference, ManagedService service) {
untrackManagedService(reference);

configurationAdminFactory.cancelExecuteCoordinated(service);
context.ungetService(reference);
}

private void addReference(ServiceReference<ManagedService> reference, ManagedService service) {
List<List<String>> qualifiedPidLists = trackManagedService(reference);
updateManagedService(qualifiedPidLists, reference, service);
configurationAdminFactory.executeCoordinated(service,
() -> updateManagedService(qualifiedPidLists, reference, service));
}

private void updateManagedService(List<List<String>> qualifiedPidLists, ServiceReference<ManagedService> reference,
Expand Down Expand Up @@ -283,19 +302,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 b177930

Please sign in to comment.