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 27, 2022
1 parent 7b737e4 commit 03598c6
Show file tree
Hide file tree
Showing 6 changed files with 40 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,28 @@
package org.eclipse.osgi.internal.framework;

import java.util.Arrays;
import java.util.Collections;
import java.util.Objects;
import java.util.Optional;
import java.util.Set;
import java.util.WeakHashMap;
import org.osgi.framework.Bundle;
import org.osgi.framework.connect.FrameworkUtilHelper;

public class ConnectFrameworkUtilHelper implements FrameworkUtilHelper {
static final Set<FrameworkUtilHelper> connectHelpers = Collections.newSetFromMap(new WeakHashMap<>());

@Override
public Optional<Bundle> getBundle(Class<?> classFromBundle) {
FrameworkUtilHelper[] helpers;
synchronized (connectHelpers) {
helpers = connectHelpers.toArray(FrameworkUtilHelper[]::new);
}
return Arrays.stream(helpers).filter(Objects::nonNull)
.flatMap(helper -> helper.getBundle(classFromBundle).stream()).findFirst();
}

public static synchronized void add(FrameworkUtilHelper moduleConnector) {
connectHelpers.add(moduleConnector);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -57,6 +57,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 +155,11 @@ 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) {
// TODO when would this ever be removed? But the SPI has the same problem so
// maybe it is okay?
ConnectFrameworkUtilHelper.add((FrameworkUtilHelper) moduleConnector);
}
moduleConnector.initialize(fwkStore, Collections.unmodifiableMap(config));
}

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 03598c6

Please sign in to comment.