diff options
author | Thomas Watson | 2018-10-19 16:29:35 +0000 |
---|---|---|
committer | Thomas Watson | 2018-10-28 12:59:56 +0000 |
commit | 2bed46ed9077d7483ad568f8c4f5dfbec35e6144 (patch) | |
tree | 70c1cce77c3d3ed5c3cc88a1a05854bf42669fa6 | |
parent | 34ff143fce6643a6a318fd6878b0f5677a78e471 (diff) | |
download | rt.equinox.framework-2bed46ed9077d7483ad568f8c4f5dfbec35e6144.tar.gz rt.equinox.framework-2bed46ed9077d7483ad568f8c4f5dfbec35e6144.tar.xz rt.equinox.framework-2bed46ed9077d7483ad568f8c4f5dfbec35e6144.zip |
Bug 540323 - Add helper support for FrameworkUtilY20181030-1100Y20181030-0940I20181029-1800I20181028-1800
Change-Id: I716d14a47540dbb529500b8d2aa8171846a66904
Signed-off-by: Thomas Watson <tjwatson@us.ibm.com>
7 files changed, 137 insertions, 17 deletions
diff --git a/bundles/org.eclipse.osgi.tests/bundles_src/storage.hooks.a/META-INF/services/org.eclipse.osgi.internal.hookregistry.FrameworkUtilHelper b/bundles/org.eclipse.osgi.tests/bundles_src/storage.hooks.a/META-INF/services/org.eclipse.osgi.internal.hookregistry.FrameworkUtilHelper new file mode 100644 index 000000000..a3365b5b5 --- /dev/null +++ b/bundles/org.eclipse.osgi.tests/bundles_src/storage.hooks.a/META-INF/services/org.eclipse.osgi.internal.hookregistry.FrameworkUtilHelper @@ -0,0 +1 @@ +org.eclipse.osgi.tests.hooks.framework.storage.a.TestHelper
\ No newline at end of file diff --git a/bundles/org.eclipse.osgi.tests/bundles_src/storage.hooks.a/org/eclipse/osgi/tests/hooks/framework/storage/a/TestHelper.java b/bundles/org.eclipse.osgi.tests/bundles_src/storage.hooks.a/org/eclipse/osgi/tests/hooks/framework/storage/a/TestHelper.java new file mode 100644 index 000000000..35b949cc3 --- /dev/null +++ b/bundles/org.eclipse.osgi.tests/bundles_src/storage.hooks.a/org/eclipse/osgi/tests/hooks/framework/storage/a/TestHelper.java @@ -0,0 +1,26 @@ +/******************************************************************************* + * Copyright (c) 2018 IBM Corporation and others. + * + * This program and the accompanying materials + * are made available under the terms of the Eclipse Public License 2.0 + * which accompanies this distribution, and is available at + * https://www.eclipse.org/legal/epl-2.0/ + * + * SPDX-License-Identifier: EPL-2.0 + * + * Contributors: + * IBM Corporation - initial API and implementation + *******************************************************************************/ +package org.eclipse.osgi.tests.hooks.framework.storage.a; + +import org.eclipse.osgi.internal.hookregistry.FrameworkUtilHelper; +import org.osgi.framework.Bundle; + +public class TestHelper extends FrameworkUtilHelper { + volatile static Bundle testBundle = null; + + @Override + public Bundle getBundle(Class<?> classFromBundle) { + return testBundle; + } +} diff --git a/bundles/org.eclipse.osgi.tests/bundles_src/storage.hooks.a/org/eclipse/osgi/tests/hooks/framework/storage/a/TestHookConfigurator.java b/bundles/org.eclipse.osgi.tests/bundles_src/storage.hooks.a/org/eclipse/osgi/tests/hooks/framework/storage/a/TestHookConfigurator.java index 5ca89c08f..db23e19b3 100644 --- a/bundles/org.eclipse.osgi.tests/bundles_src/storage.hooks.a/org/eclipse/osgi/tests/hooks/framework/storage/a/TestHookConfigurator.java +++ b/bundles/org.eclipse.osgi.tests/bundles_src/storage.hooks.a/org/eclipse/osgi/tests/hooks/framework/storage/a/TestHookConfigurator.java @@ -15,13 +15,22 @@ package org.eclipse.osgi.tests.hooks.framework.storage.a; import java.io.DataInputStream; import java.io.DataOutputStream; -import java.util.*; +import java.util.Collections; +import java.util.Dictionary; +import java.util.HashMap; +import java.util.Map; import java.util.concurrent.atomic.AtomicInteger; import org.eclipse.osgi.container.Module; import org.eclipse.osgi.container.ModuleContainerAdaptor.ModuleEvent; import org.eclipse.osgi.container.ModuleRevisionBuilder; -import org.eclipse.osgi.internal.hookregistry.*; +import org.eclipse.osgi.internal.hookregistry.ActivatorHookFactory; +import org.eclipse.osgi.internal.hookregistry.HookConfigurator; +import org.eclipse.osgi.internal.hookregistry.HookRegistry; +import org.eclipse.osgi.internal.hookregistry.StorageHookFactory; import org.eclipse.osgi.storage.BundleInfo.Generation; +import org.osgi.framework.BundleActivator; +import org.osgi.framework.BundleContext; +import org.osgi.framework.Constants; import org.osgi.framework.Version; import org.osgi.framework.namespace.BundleNamespace; import org.osgi.framework.namespace.IdentityNamespace; @@ -121,5 +130,23 @@ public class TestHookConfigurator implements HookConfigurator { public void addHooks(HookRegistry hookRegistry) { hookRegistry.addStorageHookFactory(new TestStorageHookFactory()); + hookRegistry.addActivatorHookFactory(new ActivatorHookFactory() { + + @Override + public BundleActivator createActivator() { + return new BundleActivator() { + + @Override + public void start(BundleContext context) throws Exception { + TestHelper.testBundle = context.getBundle(Constants.SYSTEM_BUNDLE_LOCATION); + } + + @Override + public void stop(BundleContext context) throws Exception { + // nothing + } + }; + } + }); } } diff --git a/bundles/org.eclipse.osgi.tests/src/org/eclipse/osgi/tests/hooks/framework/AbstractFrameworkHookTests.java b/bundles/org.eclipse.osgi.tests/src/org/eclipse/osgi/tests/hooks/framework/AbstractFrameworkHookTests.java index 78cd099fc..cdbaa3fc8 100644 --- a/bundles/org.eclipse.osgi.tests/src/org/eclipse/osgi/tests/hooks/framework/AbstractFrameworkHookTests.java +++ b/bundles/org.eclipse.osgi.tests/src/org/eclipse/osgi/tests/hooks/framework/AbstractFrameworkHookTests.java @@ -56,7 +56,7 @@ public abstract class AbstractFrameworkHookTests extends CoreTest { @Override protected synchronized Class<?> loadClass(String name, boolean resolve) throws ClassNotFoundException { - if (name.startsWith("org.eclipse")) { + if (name.startsWith("org.eclipse") || name.startsWith("org.osgi.framework.FrameworkUtil")) { Class<?> result = findLoadedClass(name); if (result == null) result = findClass(name); diff --git a/bundles/org.eclipse.osgi.tests/src/org/eclipse/osgi/tests/hooks/framework/StorageHookTests.java b/bundles/org.eclipse.osgi.tests/src/org/eclipse/osgi/tests/hooks/framework/StorageHookTests.java index 2303b322b..ddfb5a9ea 100644 --- a/bundles/org.eclipse.osgi.tests/src/org/eclipse/osgi/tests/hooks/framework/StorageHookTests.java +++ b/bundles/org.eclipse.osgi.tests/src/org/eclipse/osgi/tests/hooks/framework/StorageHookTests.java @@ -17,12 +17,17 @@ import static org.junit.Assert.assertNotEquals; import java.io.File; import java.net.URL; -import java.util.*; +import java.util.HashMap; +import java.util.List; +import java.util.Map; import org.eclipse.osgi.container.ModuleContainerAdaptor.ModuleEvent; import org.eclipse.osgi.internal.hookregistry.HookRegistry; import org.eclipse.osgi.tests.OSGiTestsActivator; import org.eclipse.osgi.tests.bundles.SystemBundleTests; -import org.osgi.framework.*; +import org.osgi.framework.Bundle; +import org.osgi.framework.BundleContext; +import org.osgi.framework.BundleException; +import org.osgi.framework.Constants; import org.osgi.framework.launch.Framework; import org.osgi.framework.wiring.BundleRevision; import org.osgi.resource.Capability; @@ -217,6 +222,13 @@ public class StorageHookTests extends AbstractFrameworkHookTests { assertEquals("Wrong number of capabilities.", 1, testCaps.size()); } + public void testFrameworkUtilHelper() throws Exception { + initAndStartFramework(); + Class<?> frameworkUtilClass = classLoader.loadClass("org.osgi.framework.FrameworkUtil"); + Bundle b = (Bundle) frameworkUtilClass.getMethod("getBundle", Class.class).invoke(null, BundleContext.class); + assertEquals("Wrong bundle found.", framework.getBundleContext().getBundle(Constants.SYSTEM_BUNDLE_LOCATION), b); + } + protected void setUp() throws Exception { super.setUp(); String loc = bundleInstaller.getBundleLocation(HOOK_CONFIGURATOR_BUNDLE); diff --git a/bundles/org.eclipse.osgi/container/src/org/eclipse/osgi/internal/hookregistry/FrameworkUtilHelper.java b/bundles/org.eclipse.osgi/container/src/org/eclipse/osgi/internal/hookregistry/FrameworkUtilHelper.java new file mode 100644 index 000000000..80bc1a80c --- /dev/null +++ b/bundles/org.eclipse.osgi/container/src/org/eclipse/osgi/internal/hookregistry/FrameworkUtilHelper.java @@ -0,0 +1,36 @@ +/******************************************************************************* + * Copyright (c) 2018 IBM Corporation and others. + * + * This program and the accompanying materials + * are made available under the terms of the Eclipse Public License 2.0 + * which accompanies this distribution, and is available at + * https://www.eclipse.org/legal/epl-2.0/ + * + * SPDX-License-Identifier: EPL-2.0 + * + * Contributors: + * IBM Corporation - initial API and implementation + *******************************************************************************/ +package org.eclipse.osgi.internal.hookregistry; + +import org.osgi.framework.Bundle; +import org.osgi.framework.FrameworkUtil; + +/** + * The helper provides alternative implementations for methods in the + * {@link FrameworkUtil} class. While this is not a hook, it is possible + * for framework fragments to provide a META-INF/services configuration + * to allow their own implementation to be loaded by the {@link FrameworkUtil} + * class. + */ +public class FrameworkUtilHelper { + /** + * See {@link FrameworkUtil#getBundle(Class)} + * @param classFromBundle a class defined by a bundle class loader. + * @return A Bundle for the specified bundle class or null if the + * specified class was not defined by a bundle class loader. + */ + public Bundle getBundle(Class<?> classFromBundle) { + return null; + } +} diff --git a/bundles/org.eclipse.osgi/osgi/src/org/osgi/framework/FrameworkUtil.java b/bundles/org.eclipse.osgi/osgi/src/org/osgi/framework/FrameworkUtil.java index 558807664..45f0f2dfe 100644 --- a/bundles/org.eclipse.osgi/osgi/src/org/osgi/framework/FrameworkUtil.java +++ b/bundles/org.eclipse.osgi/osgi/src/org/osgi/framework/FrameworkUtil.java @@ -16,25 +16,16 @@ package org.osgi.framework; -import java.lang.reflect.AccessibleObject; -import java.lang.reflect.Constructor; -import java.lang.reflect.InvocationTargetException; -import java.lang.reflect.Method; -import java.lang.reflect.Modifier; import java.security.AccessController; import java.security.PrivilegedAction; -import java.util.AbstractMap; import java.util.ArrayList; -import java.util.Collection; -import java.util.Dictionary; -import java.util.Enumeration; +import java.util.Collections; import java.util.Iterator; import java.util.List; -import java.util.Map; -import java.util.Set; - +import java.util.ServiceLoader; import javax.security.auth.x500.X500Principal; import org.eclipse.osgi.internal.framework.FilterImpl; +import org.eclipse.osgi.internal.hookregistry.FrameworkUtilHelper; /** * Framework Utility class. @@ -194,6 +185,26 @@ public class FrameworkUtil { return DNChainMatching.match(matchPattern, dnChain); } + private final static List<FrameworkUtilHelper> helpers; + static { + List<FrameworkUtilHelper> l = new ArrayList<>(); + try { + ServiceLoader<FrameworkUtilHelper> helperLoader = AccessController.doPrivileged(new PrivilegedAction<ServiceLoader<FrameworkUtilHelper>>() { + @Override + public ServiceLoader<FrameworkUtilHelper> run() { + return ServiceLoader.load(FrameworkUtilHelper.class, FrameworkUtilHelper.class.getClassLoader()); + } + }); + for (Iterator<FrameworkUtilHelper> iHelpers = helperLoader.iterator(); iHelpers.hasNext();) { + l.add(iHelpers.next()); + } + } catch (Exception e) { + // should not fail out of static initializers + e.printStackTrace(); + } + helpers = Collections.unmodifiableList(l); + } + /** * Return a {@code Bundle} for the specified bundle class. The returned * {@code Bundle} is the bundle associated with the bundle class loader @@ -217,6 +228,13 @@ public class FrameworkUtil { if (cl instanceof BundleReference) { return ((BundleReference) cl).getBundle(); } + + for (FrameworkUtilHelper helper : helpers) { + Bundle b = helper.getBundle(classFromBundle); + if (b != null) { + return b; + } + } return null; } |