diff options
author | gjoranv <gv@verizonmedia.com> | 2019-11-05 10:53:44 +0100 |
---|---|---|
committer | gjoranv <gv@verizonmedia.com> | 2019-11-05 10:53:44 +0100 |
commit | d75825d503b8566e66d45b0ef726a1d5834970d7 (patch) | |
tree | d6da7f8af45a6bef83823f1ae7068317a5d17e8d /container-core/src/main/java/com/yahoo/osgi | |
parent | 73cbfef0434123f59584f9ed5f5cceac6715adbd (diff) |
Reapply "Gjoranv/allow duplicate bundles"
This reverts commit 2ef1e922a1d845b3cd79e9fb329925e7e9896919.
Diffstat (limited to 'container-core/src/main/java/com/yahoo/osgi')
4 files changed, 70 insertions, 20 deletions
diff --git a/container-core/src/main/java/com/yahoo/osgi/MockOsgi.java b/container-core/src/main/java/com/yahoo/osgi/MockOsgi.java index 45ad02d2cef..d809c493565 100644 --- a/container-core/src/main/java/com/yahoo/osgi/MockOsgi.java +++ b/container-core/src/main/java/com/yahoo/osgi/MockOsgi.java @@ -16,26 +16,28 @@ import java.util.List; public class MockOsgi extends NonWorkingOsgiFramework implements Osgi { @Override - public Bundle[] getBundles() { - return new Bundle[0]; + public List<Bundle> getInitialBundles() { + return Collections.emptyList(); } @Override - public Bundle getBundle(ComponentSpecification bundleId) { - return null; + public Bundle[] getBundles() { + return new Bundle[0]; } @Override - public List<Bundle> install(String absolutePath) { + public List<Bundle> getCurrentBundles() { return Collections.emptyList(); } @Override - public void uninstall(Bundle bundle) { + public Bundle getBundle(ComponentSpecification bundleId) { + return null; } @Override - public void refreshPackages() { + public List<Bundle> install(String absolutePath) { + return Collections.emptyList(); } } diff --git a/container-core/src/main/java/com/yahoo/osgi/Osgi.java b/container-core/src/main/java/com/yahoo/osgi/Osgi.java index c94eaf43deb..8f0acf41f30 100644 --- a/container-core/src/main/java/com/yahoo/osgi/Osgi.java +++ b/container-core/src/main/java/com/yahoo/osgi/Osgi.java @@ -4,6 +4,7 @@ package com.yahoo.osgi; import com.yahoo.component.ComponentSpecification; import org.osgi.framework.Bundle; +import java.util.Collection; import java.util.List; /** @@ -11,14 +12,17 @@ import java.util.List; */ public interface Osgi { + List<Bundle> getInitialBundles(); + Bundle[] getBundles(); + /** Returns all bundles that have not been scheduled for uninstall. */ + List<Bundle> getCurrentBundles(); + Bundle getBundle(ComponentSpecification bundleId); List<Bundle> install(String absolutePath); - void uninstall(Bundle bundle); - - void refreshPackages(); + void allowDuplicateBundles(Collection<Bundle> bundles); } diff --git a/container-core/src/main/java/com/yahoo/osgi/OsgiImpl.java b/container-core/src/main/java/com/yahoo/osgi/OsgiImpl.java index 8b2f20a1c13..ed93d15c975 100644 --- a/container-core/src/main/java/com/yahoo/osgi/OsgiImpl.java +++ b/container-core/src/main/java/com/yahoo/osgi/OsgiImpl.java @@ -7,19 +7,43 @@ import com.yahoo.container.bundle.BundleInstantiationSpecification; import com.yahoo.jdisc.application.OsgiFramework; import org.osgi.framework.Bundle; import org.osgi.framework.BundleException; +import org.osgi.framework.launch.Framework; +import java.util.Collection; import java.util.List; +import java.util.logging.Logger; /** * @author Tony Vaagenes * @author bratseth */ public class OsgiImpl implements Osgi { + private static final Logger log = Logger.getLogger(OsgiImpl.class.getName()); private final OsgiFramework jdiscOsgi; + // The initial bundles are never scheduled for uninstall + private final List<Bundle> initialBundles; + + // An initial bundle that is not the framework, and can hence be used to look up current bundles + private final Bundle alwaysCurrentBundle; + public OsgiImpl(OsgiFramework jdiscOsgi) { this.jdiscOsgi = jdiscOsgi; + + this.initialBundles = jdiscOsgi.bundles(); + if (initialBundles.isEmpty()) + throw new IllegalStateException("No initial bundles!"); + + alwaysCurrentBundle = firstNonFrameworkBundle(initialBundles); + if (alwaysCurrentBundle == null) + throw new IllegalStateException("The initial bundles only contained the framework bundle!"); + log.info("Using " + alwaysCurrentBundle + " to lookup current bundles."); + } + + @Override + public List<Bundle> getInitialBundles() { + return initialBundles; } @Override @@ -28,6 +52,10 @@ public class OsgiImpl implements Osgi { return bundles.toArray(new Bundle[bundles.size()]); } + @Override + public List<Bundle> getCurrentBundles() { + return jdiscOsgi.getBundles(alwaysCurrentBundle); + } public Class<Object> resolveClass(BundleInstantiationSpecification spec) { Bundle bundle = getBundle(spec.bundle); @@ -86,8 +114,9 @@ public class OsgiImpl implements Osgi { * @return the bundle match having the highest version, or null if there was no matches */ public Bundle getBundle(ComponentSpecification id) { + log.fine(() -> "Getting bundle for component " + id + ". Set of current bundles: " + getCurrentBundles()); Bundle highestMatch = null; - for (Bundle bundle : getBundles()) { + for (Bundle bundle : getCurrentBundles()) { assert bundle.getSymbolicName() != null : "ensureHasBundleSymbolicName not called during installation"; if ( ! bundle.getSymbolicName().equals(id.getName())) continue; @@ -122,17 +151,16 @@ public class OsgiImpl implements Osgi { } @Override - public void uninstall(Bundle bundle) { - try { - bundle.uninstall(); - } catch (BundleException e) { - throw new RuntimeException(e); - } + public void allowDuplicateBundles(Collection<Bundle> bundles) { + jdiscOsgi.allowDuplicateBundles(bundles); } - @Override - public void refreshPackages() { - jdiscOsgi.refreshPackages(); + private static Bundle firstNonFrameworkBundle(List<Bundle> bundles) { + for (Bundle b : bundles) { + if (! (b instanceof Framework)) + return b; + } + return null; } } diff --git a/container-core/src/main/java/com/yahoo/osgi/OsgiWrapper.java b/container-core/src/main/java/com/yahoo/osgi/OsgiWrapper.java new file mode 100644 index 00000000000..58e19e52ee3 --- /dev/null +++ b/container-core/src/main/java/com/yahoo/osgi/OsgiWrapper.java @@ -0,0 +1,16 @@ +package com.yahoo.osgi; + +import com.yahoo.component.ComponentSpecification; +import org.osgi.framework.Bundle; + +/** + * @author gjoranv + */ +public interface OsgiWrapper extends Osgi, com.yahoo.container.di.Osgi { + + @Override + default Bundle getBundle(ComponentSpecification bundleId) { + return null; + } + +} |