Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add support for Coordinator to Equinox Configuration Admin #533

Merged
merged 1 commit into from
Feb 18, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
@@ -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,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 @@ -202,13 +203,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 +285,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);
}
});
}));
}
}
Loading