diff options
author | gjoranv <gv@verizonmedia.com> | 2022-05-06 20:05:07 +0200 |
---|---|---|
committer | gjoranv <gv@verizonmedia.com> | 2022-05-08 01:40:30 +0200 |
commit | d79c95b8b64e052a00f5eb069df14788ca655d26 (patch) | |
tree | 9a739fe714f27b74dcf903fc63418e43acb95c87 /container-core | |
parent | a51532b7dcf5974be3d61eeaeb873da8b7d70753 (diff) |
Allow loading classes from jdisc_core as well as container-disc.
- If no bundle is explicitly specified, try container-disc first,
then the system bundle (jdisc_core-with-deps)
Diffstat (limited to 'container-core')
-rw-r--r-- | container-core/src/main/java/com/yahoo/osgi/OsgiImpl.java | 34 |
1 files changed, 28 insertions, 6 deletions
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 4909f9a76eb..761d4057db3 100644 --- a/container-core/src/main/java/com/yahoo/osgi/OsgiImpl.java +++ b/container-core/src/main/java/com/yahoo/osgi/OsgiImpl.java @@ -27,6 +27,8 @@ public class OsgiImpl implements Osgi { // The initial bundles are never scheduled for uninstall private final List<Bundle> initialBundles; + private final Bundle systemBundle; + // An initial bundle that is not the framework, and can hence be used to look up current bundles private final Bundle alwaysCurrentBundle; @@ -37,6 +39,7 @@ public class OsgiImpl implements Osgi { if (initialBundles.isEmpty()) throw new IllegalStateException("No initial bundles!"); + systemBundle = getSystemBundle(initialBundles); alwaysCurrentBundle = firstNonFrameworkBundle(initialBundles); if (alwaysCurrentBundle == null) throw new IllegalStateException("The initial bundles only contained the framework bundle!"); @@ -59,19 +62,30 @@ public class OsgiImpl implements Osgi { if (bundle != null) { return resolveFromBundle(spec, bundle); } else { - return resolveFromClassPath(spec); + return resolveFromThisBundleOrSystemBundle(spec); } } + /** + * Tries to resolve the given class from this class' bundle classloader. + * If unsuccessful, resolves the class from . + */ @SuppressWarnings("unchecked") - private static Class<Object> resolveFromClassPath(BundleInstantiationSpecification spec) { + private Class<Object> resolveFromThisBundleOrSystemBundle(BundleInstantiationSpecification spec) { + log.fine(() -> "Resolving class from container-disc: " + spec.classId.getName()); try { return (Class<Object>) Class.forName(spec.classId.getName()); } catch (ClassNotFoundException e) { - throw new IllegalArgumentException("Could not create a component with id '" + spec.classId.getName() + - "'. Tried to load class directly, since no bundle was found for spec: " + spec.bundle + - ". If a bundle with the same name is installed, there is a either a version mismatch" + - " or the installed bundle's version contains a qualifier string."); + log.fine(() -> "Resolving class from the system bundle (jdisc core): " + spec.classId.getName()); + try { + return (Class<Object>) systemBundle.loadClass(spec.classId.getName()); + } catch (ClassNotFoundException e2) { + throw new IllegalArgumentException( + "Could not create a component with id '" + spec.classId.getName() + + "'. Tried to load class directly, since no bundle was found for spec: " + spec.bundle + + ". If a bundle with the same name is installed, there is a either a version mismatch" + + " or the installed bundle's version contains a qualifier string."); + } } } @@ -157,6 +171,14 @@ public class OsgiImpl implements Osgi { return jdiscOsgi.isFelixFramework(); } + private static Bundle getSystemBundle(List<Bundle> bundles ) { + for (Bundle b : bundles) { + if (b instanceof Framework) + return b; + } + throw new IllegalStateException("No system bundle in " + bundles); + } + private static Bundle firstNonFrameworkBundle(List<Bundle> bundles) { for (Bundle b : bundles) { if (! (b instanceof Framework)) |