Skip to content

Commit

Permalink
Use a more less intrusive approach...
Browse files Browse the repository at this point in the history
  • Loading branch information
laeubi committed Jul 28, 2022
1 parent 7b737e4 commit f2f34f6
Show file tree
Hide file tree
Showing 6 changed files with 54 additions and 62 deletions.
Original file line number Diff line number Diff line change
@@ -1 +1 @@
org.eclipse.osgi.launch.EquinoxFactory
org.eclipse.osgi.internal.framework.ConnectFrameworkUtilHelper
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
package org.eclipse.osgi.internal.framework;

import java.util.Objects;
import java.util.Optional;
import java.util.Set;
import java.util.concurrent.ConcurrentHashMap;
import org.osgi.framework.Bundle;
import org.osgi.framework.connect.FrameworkUtilHelper;

public class ConnectFrameworkUtilHelper implements FrameworkUtilHelper {
static final Set<FrameworkUtilHelper> connectHelpers = ConcurrentHashMap.newKeySet();

@Override
public Optional<Bundle> getBundle(Class<?> classFromBundle) {
return connectHelpers.stream().filter(Objects::nonNull)
.flatMap(helper -> helper.getBundle(classFromBundle).stream()).findFirst();
}

public static void add(FrameworkUtilHelper moduleConnector) {
connectHelpers.add(moduleConnector);
}

public static void remove(FrameworkUtilHelper moduleConnector) {
connectHelpers.remove(moduleConnector);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@
import java.util.HashSet;
import java.util.List;
import java.util.Map;
import java.util.Optional;
import java.util.Set;
import java.util.WeakHashMap;
import java.util.concurrent.ConcurrentHashMap;
Expand Down Expand Up @@ -57,6 +58,7 @@
import org.osgi.framework.FrameworkUtil;
import org.osgi.framework.connect.ConnectContent;
import org.osgi.framework.connect.ConnectModule;
import org.osgi.framework.connect.FrameworkUtilHelper;
import org.osgi.framework.connect.ModuleConnector;
import org.osgi.util.tracker.ServiceTracker;

Expand Down Expand Up @@ -154,6 +156,9 @@ private static void initConnectFramework(ModuleConnector moduleConnector, Equino
final File fwkStore = new File(configUrl.getPath());
@SuppressWarnings({"rawtypes", "unchecked"})
Map<String, String> config = (Map) equinoxConfig.getInitialConfig();
if (moduleConnector instanceof FrameworkUtilHelper) {
ConnectFrameworkUtilHelper.add((FrameworkUtilHelper) moduleConnector);
}
moduleConnector.initialize(fwkStore, Collections.unmodifiableMap(config));
}

Expand All @@ -174,6 +179,13 @@ public EquinoxLogServices getLogServices() {
}

public Bundle getBundle(Class<?> clazz) {
if (connectModules.moduleConnector instanceof FrameworkUtilHelper) {
FrameworkUtilHelper helper = (FrameworkUtilHelper) connectModules.moduleConnector;
Optional<Bundle> bundle = helper.getBundle(clazz);
if (bundle.isPresent()) {
return bundle.get();
}
}
Bundle b = FrameworkUtil.getBundle(clazz);
if (b != null) {
return b;
Expand Down Expand Up @@ -214,6 +226,10 @@ public boolean isProcessClassRecursionSupportedByAll() {
}

void init() {
if (connectModules.moduleConnector instanceof FrameworkUtilHelper) {
FrameworkUtilHelper helper = (FrameworkUtilHelper) connectModules.moduleConnector;
ConnectFrameworkUtilHelper.add(helper);
}
eventPublisher.init();
synchronized (this.monitor) {
serviceRegistry = new ServiceRegistry(this);
Expand All @@ -240,6 +256,12 @@ void close() {
currentStorage.close();
// Must be done last since it will result in termination of the
// framework active thread.
if (connectModules.moduleConnector instanceof FrameworkUtilHelper) {
FrameworkUtilHelper helper = (FrameworkUtilHelper) connectModules.moduleConnector;
currentExecutor.execute(() -> {
ConnectFrameworkUtilHelper.remove(helper);
});
}
currentExecutor.shutdown();
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,13 +14,7 @@
package org.eclipse.osgi.launch;

import java.util.Map;
import java.util.Optional;
import java.util.Set;
import java.util.concurrent.ConcurrentHashMap;
import org.osgi.framework.Bundle;
import org.osgi.framework.connect.ConnectFramework;
import org.osgi.framework.connect.ConnectFrameworkFactory;
import org.osgi.framework.connect.FrameworkUtilHelper;
import org.osgi.framework.connect.ModuleConnector;
import org.osgi.framework.launch.Framework;
import org.osgi.framework.launch.FrameworkFactory;
Expand All @@ -29,40 +23,15 @@
* The framework factory implementation for the Equinox framework.
* @since 3.5
*/
public class EquinoxFactory implements FrameworkFactory, ConnectFrameworkFactory, FrameworkUtilHelper {

static final Set<FrameworkUtilHelper> connectHelpers = ConcurrentHashMap.newKeySet();
public class EquinoxFactory implements FrameworkFactory, ConnectFrameworkFactory {

@Override
public Framework newFramework(Map<String, String> configuration) {
return new Equinox(configuration);
return newFramework(configuration, null);
}

@Override
public ConnectFramework newFramework(Map<String, String> configuration, ModuleConnector moduleConnector) {
return new EquinoxConnect(configuration, moduleConnector);
}

@Override
public Optional<Bundle> getBundle(Class<?> classFromBundle) {
return connectHelpers.stream().flatMap(helper -> helper.getBundle(classFromBundle).stream()).findFirst();
}

private static final class EquinoxConnect extends Equinox implements ConnectFramework {

public EquinoxConnect(Map<String, String> configuration, ModuleConnector moduleConnector) {
super(configuration, moduleConnector);
}

@Override
public void addFrameworkUtilHelper(FrameworkUtilHelper helper) {
connectHelpers.add(helper);
}

@Override
public void removeFrameworkUtilHelper(FrameworkUtilHelper helper) {
connectHelpers.remove(helper);
}

public Framework newFramework(Map<String, String> configuration, ModuleConnector moduleConnector) {
return new Equinox(configuration, moduleConnector);
}
}

This file was deleted.

Original file line number Diff line number Diff line change
Expand Up @@ -74,6 +74,6 @@ public interface ConnectFrameworkFactory {
* supports permissions.
* @see ModuleConnector
*/
ConnectFramework newFramework(Map<String, String> configuration,
Framework newFramework(Map<String,String> configuration,
ModuleConnector moduleConnector);
}

0 comments on commit f2f34f6

Please sign in to comment.