diff options
author | Thomas Watson | 2020-06-15 16:56:22 +0000 |
---|---|---|
committer | Thomas Watson | 2020-06-15 18:00:20 +0000 |
commit | 3e8fc891e1d7e99caf160f8b37d6c1e5de3895c8 (patch) | |
tree | 076cec172ec4d5198df274d3142feb25f902f536 | |
parent | aa13079427a179248881ea40d86e52e5b3ee67e7 (diff) | |
download | rt.equinox.framework-I20200616-0140.tar.gz rt.equinox.framework-I20200616-0140.tar.xz rt.equinox.framework-I20200616-0140.zip |
Bug 564307 - Make HookContext a functional interface and use lambdasI20200616-0620I20200616-0320I20200616-0140I20200615-1800
where possible
While modifying the weaving hook support I removed the use of
"blacklist" also. The OSGi R8 specification moved to using the term
denied.
Change-Id: I4d44922a32957bc05d2ae8cd7bc92e16538b4d94
Signed-off-by: Thomas Watson <tjwatson@us.ibm.com>
7 files changed, 98 insertions, 259 deletions
diff --git a/bundles/org.eclipse.osgi/container/src/org/eclipse/osgi/internal/framework/BundleContextImpl.java b/bundles/org.eclipse.osgi/container/src/org/eclipse/osgi/internal/framework/BundleContextImpl.java index 29e9fa93b..8c8a8249a 100644 --- a/bundles/org.eclipse.osgi/container/src/org/eclipse/osgi/internal/framework/BundleContextImpl.java +++ b/bundles/org.eclipse.osgi/container/src/org/eclipse/osgi/internal/framework/BundleContextImpl.java @@ -35,7 +35,6 @@ import org.eclipse.osgi.framework.eventmgr.EventDispatcher; import org.eclipse.osgi.internal.debug.Debug; import org.eclipse.osgi.internal.loader.BundleLoader; import org.eclipse.osgi.internal.messages.Msg; -import org.eclipse.osgi.internal.serviceregistry.HookContext; import org.eclipse.osgi.internal.serviceregistry.ServiceReferenceImpl; import org.eclipse.osgi.internal.serviceregistry.ServiceRegistrationImpl; import org.eclipse.osgi.internal.serviceregistry.ServiceRegistry; @@ -259,27 +258,9 @@ public class BundleContextImpl implements BundleContext, EventDispatcher<Object, if (debug.DEBUG_HOOKS) { Debug.println("notifyBundleFindHooks(" + allBundles + ")"); //$NON-NLS-1$ //$NON-NLS-2$ } - container.getServiceRegistry().notifyHooksPrivileged(new HookContext() { - @Override - public void call(Object hook, ServiceRegistration<?> hookRegistration) throws Exception { - if (hook instanceof FindHook) { - ((FindHook) hook).find(context, allBundles); - } - } - - @Override - public String getHookClassName() { - return findHookName; - } - - @Override - public String getHookMethodName() { - return "find"; //$NON-NLS-1$ - } - - @Override - public boolean skipRegistration(ServiceRegistration<?> hookRegistration) { - return false; + container.getServiceRegistry().notifyHooksPrivileged(findHookName, "find", (hook, hookRegistration) -> { //$NON-NLS-1$ + if (hook instanceof FindHook) { + ((FindHook) hook).find(context, allBundles); } }); } diff --git a/bundles/org.eclipse.osgi/container/src/org/eclipse/osgi/internal/framework/EquinoxEventPublisher.java b/bundles/org.eclipse.osgi/container/src/org/eclipse/osgi/internal/framework/EquinoxEventPublisher.java index fcbae3ee6..0e85e7031 100644 --- a/bundles/org.eclipse.osgi/container/src/org/eclipse/osgi/internal/framework/EquinoxEventPublisher.java +++ b/bundles/org.eclipse.osgi/container/src/org/eclipse/osgi/internal/framework/EquinoxEventPublisher.java @@ -28,7 +28,6 @@ import org.eclipse.osgi.framework.eventmgr.EventDispatcher; import org.eclipse.osgi.framework.eventmgr.EventManager; import org.eclipse.osgi.framework.eventmgr.ListenerQueue; import org.eclipse.osgi.internal.debug.Debug; -import org.eclipse.osgi.internal.serviceregistry.HookContext; import org.eclipse.osgi.internal.serviceregistry.ServiceRegistry; import org.eclipse.osgi.internal.serviceregistry.ShrinkableCollection; import org.osgi.framework.AdminPermission; @@ -38,7 +37,6 @@ import org.osgi.framework.BundleEvent; import org.osgi.framework.BundleListener; import org.osgi.framework.FrameworkEvent; import org.osgi.framework.FrameworkListener; -import org.osgi.framework.ServiceRegistration; import org.osgi.framework.SynchronousBundleListener; import org.osgi.framework.hooks.bundle.CollisionHook; import org.osgi.framework.hooks.bundle.EventHook; @@ -46,6 +44,7 @@ import org.osgi.framework.hooks.bundle.EventHook; public class EquinoxEventPublisher { static final String eventHookName = EventHook.class.getName(); static final String collisionHookName = CollisionHook.class.getName(); + @SuppressWarnings("deprecation") static final int FRAMEWORK_STOPPED_MASK = (FrameworkEvent.STOPPED | FrameworkEvent.STOPPED_BOOTCLASSPATH_MODIFIED | FrameworkEvent.STOPPED_UPDATE | FrameworkEvent.STOPPED_SYSTEM_REFRESHED); @@ -256,27 +255,9 @@ public class EquinoxEventPublisher { ServiceRegistry serviceRegistry = container.getServiceRegistry(); if (serviceRegistry != null) { - serviceRegistry.notifyHooksPrivileged(new HookContext() { - @Override - public void call(Object hook, ServiceRegistration<?> hookRegistration) throws Exception { - if (hook instanceof EventHook) { - ((EventHook) hook).event(event, result); - } - } - - @Override - public String getHookClassName() { - return eventHookName; - } - - @Override - public String getHookMethodName() { - return "event"; //$NON-NLS-1$ - } - - @Override - public boolean skipRegistration(ServiceRegistration<?> hookRegistration) { - return false; + serviceRegistry.notifyHooksPrivileged(eventHookName, "event", (hook, hookRegistration) -> { //$NON-NLS-1$ + if (hook instanceof EventHook) { + ((EventHook) hook).event(event, result); } }); } diff --git a/bundles/org.eclipse.osgi/container/src/org/eclipse/osgi/internal/framework/OSGiFrameworkHooks.java b/bundles/org.eclipse.osgi/container/src/org/eclipse/osgi/internal/framework/OSGiFrameworkHooks.java index c1e5b25b2..3dc90dab0 100644 --- a/bundles/org.eclipse.osgi/container/src/org/eclipse/osgi/internal/framework/OSGiFrameworkHooks.java +++ b/bundles/org.eclipse.osgi/container/src/org/eclipse/osgi/internal/framework/OSGiFrameworkHooks.java @@ -27,7 +27,6 @@ import org.eclipse.osgi.container.ModuleContainer; import org.eclipse.osgi.framework.util.ArrayMap; import org.eclipse.osgi.internal.debug.Debug; import org.eclipse.osgi.internal.messages.Msg; -import org.eclipse.osgi.internal.serviceregistry.HookContext; import org.eclipse.osgi.internal.serviceregistry.ServiceReferenceImpl; import org.eclipse.osgi.internal.serviceregistry.ServiceRegistry; import org.eclipse.osgi.internal.serviceregistry.ShrinkableCollection; @@ -37,7 +36,6 @@ import org.eclipse.osgi.util.NLS; import org.osgi.framework.Bundle; import org.osgi.framework.BundleException; import org.osgi.framework.InvalidSyntaxException; -import org.osgi.framework.ServiceRegistration; import org.osgi.framework.hooks.bundle.CollisionHook; import org.osgi.framework.hooks.resolver.ResolverHook; import org.osgi.framework.hooks.resolver.ResolverHookFactory; @@ -119,27 +117,9 @@ class OSGiFrameworkHooks { } ServiceRegistry registry = container.getServiceRegistry(); if (registry != null) { - registry.notifyHooksPrivileged(new HookContext() { - @Override - public void call(Object hook, ServiceRegistration<?> hookRegistration) throws Exception { - if (hook instanceof CollisionHook) { - ((CollisionHook) hook).filterCollisions(operationType, target, collisionCandidates); - } - } - - @Override - public String getHookClassName() { - return collisionHookName; - } - - @Override - public String getHookMethodName() { - return "filterCollisions"; //$NON-NLS-1$ - } - - @Override - public boolean skipRegistration(ServiceRegistration<?> hookRegistration) { - return false; + registry.notifyHooksPrivileged(collisionHookName, "filterCollisions", (hook, hookRegistration) -> { //$NON-NLS-1$ + if (hook instanceof CollisionHook) { + ((CollisionHook) hook).filterCollisions(operationType, target, collisionCandidates); } }); } diff --git a/bundles/org.eclipse.osgi/container/src/org/eclipse/osgi/internal/serviceregistry/HookContext.java b/bundles/org.eclipse.osgi/container/src/org/eclipse/osgi/internal/serviceregistry/HookContext.java index a16d885ec..29bead28a 100644 --- a/bundles/org.eclipse.osgi/container/src/org/eclipse/osgi/internal/serviceregistry/HookContext.java +++ b/bundles/org.eclipse.osgi/container/src/org/eclipse/osgi/internal/serviceregistry/HookContext.java @@ -1,5 +1,5 @@ /******************************************************************************* - * Copyright (c) 2010, 2017 IBM Corporation and others. + * Copyright (c) 2010, 2020 IBM Corporation and others. * * This program and the accompanying materials * are made available under the terms of the Eclipse Public License 2.0 @@ -23,6 +23,7 @@ import org.osgi.framework.ServiceRegistration; * as well as all the parameters which need to be passed to the hook method. * */ +@FunctionalInterface public interface HookContext { /** @@ -37,23 +38,11 @@ public interface HookContext { public void call(Object hook, ServiceRegistration<?> hookRegistration) throws Exception; /** - * Return the class name of the hook type supported by this hook context. - * - * @return The class name of the hook type supported by this hook context. - */ - public String getHookClassName(); - - /** - * Return the hook method name called by this hook context. - * - * @return The hook method name called by this hook context. - */ - public String getHookMethodName(); - - /** * Returns true if the given registration should be skipped. * @param hookRegistration the registration to check * @return true if the given registration should be skipped. */ - public boolean skipRegistration(ServiceRegistration<?> hookRegistration); + public default boolean skipRegistration(ServiceRegistration<?> hookRegistration) { + return false; + } } diff --git a/bundles/org.eclipse.osgi/container/src/org/eclipse/osgi/internal/serviceregistry/ServiceRegistry.java b/bundles/org.eclipse.osgi/container/src/org/eclipse/osgi/internal/serviceregistry/ServiceRegistry.java index 8207c650e..1cb84dc37 100644 --- a/bundles/org.eclipse.osgi/container/src/org/eclipse/osgi/internal/serviceregistry/ServiceRegistry.java +++ b/bundles/org.eclipse.osgi/container/src/org/eclipse/osgi/internal/serviceregistry/ServiceRegistry.java @@ -1245,27 +1245,9 @@ public class ServiceRegistry { if (debug.DEBUG_HOOKS) { Debug.println("notifyServiceFindHooks(" + context.getBundleImpl() + "," + clazz + "," + filterstring + "," + allservices + "," + result + ")"); //$NON-NLS-1$ //$NON-NLS-2$ //$NON-NLS-3$ //$NON-NLS-4$ //$NON-NLS-5$ //$NON-NLS-6$ } - notifyHooksPrivileged(new HookContext() { - @Override - public void call(Object hook, ServiceRegistration<?> hookRegistration) throws Exception { - if (hook instanceof FindHook) { - ((FindHook) hook).find(context, clazz, filterstring, allservices, result); - } - } - - @Override - public String getHookClassName() { - return findHookName; - } - - @Override - public String getHookMethodName() { - return "find"; //$NON-NLS-1$ - } - - @Override - public boolean skipRegistration(ServiceRegistration<?> hookRegistration) { - return false; + notifyHooksPrivileged(findHookName, "find", (hook, hookRegistration) -> { //$NON-NLS-1$ + if (hook instanceof FindHook) { + ((FindHook) hook).find(context, clazz, filterstring, allservices, result); } }); } @@ -1278,32 +1260,14 @@ public class ServiceRegistry { * @param event The service event to be delivered. * @param result The result to return to the caller which may have been shrunk by the EventHooks. */ + @SuppressWarnings("deprecation") private void notifyEventHooksPrivileged(final ServiceEvent event, final Collection<BundleContext> result) { if (debug.DEBUG_HOOKS) { Debug.println("notifyServiceEventHooks(" + event.getType() + ":" + event.getServiceReference() + "," + result + ")"); //$NON-NLS-1$ //$NON-NLS-2$ //$NON-NLS-3$ //$NON-NLS-4$ } - notifyHooksPrivileged(new HookContext() { - @SuppressWarnings("deprecation") - @Override - public void call(Object hook, ServiceRegistration<?> hookRegistration) throws Exception { - if (hook instanceof EventHook) { - ((EventHook) hook).event(event, result); - } - } - - @Override - public String getHookClassName() { - return eventHookName; - } - - @Override - public String getHookMethodName() { - return "event"; //$NON-NLS-1$ - } - - @Override - public boolean skipRegistration(ServiceRegistration<?> hookRegistration) { - return false; + notifyHooksPrivileged(eventHookName, "event", (hook, hookRegistration) -> { //$NON-NLS-1$ + if (hook instanceof EventHook) { + ((EventHook) hook).event(event, result); } }); } @@ -1320,27 +1284,9 @@ public class ServiceRegistry { if (debug.DEBUG_HOOKS) { Debug.println("notifyServiceEventListenerHooks(" + event.getType() + ":" + event.getServiceReference() + "," + result + ")"); //$NON-NLS-1$ //$NON-NLS-2$ //$NON-NLS-3$ //$NON-NLS-4$ } - notifyHooksPrivileged(new HookContext() { - @Override - public void call(Object hook, ServiceRegistration<?> hookRegistration) throws Exception { - if (hook instanceof EventListenerHook) { - ((EventListenerHook) hook).event(event, result); - } - } - - @Override - public String getHookClassName() { - return eventListenerHookName; - } - - @Override - public String getHookMethodName() { - return "event"; //$NON-NLS-1$ - } - - @Override - public boolean skipRegistration(ServiceRegistration<?> hookRegistration) { - return false; + notifyHooksPrivileged(eventListenerHookName, "event", (hook, hookRegistration) -> { //$NON-NLS-1$ + if (hook instanceof EventListenerHook) { + ((EventListenerHook) hook).event(event, result); } }); } @@ -1350,13 +1296,13 @@ public class ServiceRegistry { * * @param hookContext Context to use when calling the hook services. */ - public void notifyHooksPrivileged(HookContext hookContext) { - List<ServiceRegistrationImpl<?>> hooks = lookupServiceRegistrations(hookContext.getHookClassName(), null); + public void notifyHooksPrivileged(String serviceName, String serviceMethod, HookContext hookContext) { + List<ServiceRegistrationImpl<?>> hooks = lookupServiceRegistrations(serviceName, null); // Since the list is already sorted, we don't need to sort the list to call the hooks // in the proper order. for (ServiceRegistrationImpl<?> registration : hooks) { - notifyHookPrivileged(systemBundleContext, registration, hookContext); + notifyHookPrivileged(systemBundleContext, registration, serviceMethod, hookContext); } } @@ -1367,7 +1313,8 @@ public class ServiceRegistry { * @param registration Hook service to call. * @param hookContext Context to use when calling the hook service. */ - private void notifyHookPrivileged(BundleContextImpl context, ServiceRegistrationImpl<?> registration, HookContext hookContext) { + private void notifyHookPrivileged(BundleContextImpl context, ServiceRegistrationImpl<?> registration, + String serviceMethod, HookContext hookContext) { if (hookContext.skipRegistration(registration)) { return; } @@ -1382,12 +1329,13 @@ public class ServiceRegistry { hookContext.call(hook, registration); } catch (Throwable t) { if (debug.DEBUG_HOOKS) { - Debug.println(hook.getClass().getName() + "." + hookContext.getHookMethodName() + "() exception: " + t.getMessage()); //$NON-NLS-1$ //$NON-NLS-2$ + Debug.println(hook.getClass().getName() + "." + serviceMethod + "() exception: " + t.getMessage()); //$NON-NLS-1$ //$NON-NLS-2$ Debug.printStackTrace(t); } // allow the adaptor to handle this unexpected error container.handleRuntimeError(t); - ServiceException se = new ServiceException(NLS.bind(Msg.SERVICE_FACTORY_EXCEPTION, hook.getClass().getName(), hookContext.getHookMethodName()), t); + ServiceException se = new ServiceException( + NLS.bind(Msg.SERVICE_FACTORY_EXCEPTION, hook.getClass().getName(), serviceMethod), t); container.getEventPublisher().publishFrameworkEvent(FrameworkEvent.ERROR, registration.getBundle(), se); } } @@ -1429,27 +1377,9 @@ public class ServiceRegistry { } final Collection<ListenerInfo> listeners = Collections.unmodifiableCollection(addedListeners); - notifyHookPrivileged(systemBundleContext, registration, new HookContext() { - @Override - public void call(Object hook, ServiceRegistration<?> hookRegistration) throws Exception { - if (hook instanceof ListenerHook) { - ((ListenerHook) hook).added(listeners); - } - } - - @Override - public String getHookClassName() { - return listenerHookName; - } - - @Override - public String getHookMethodName() { - return "added"; //$NON-NLS-1$ - } - - @Override - public boolean skipRegistration(ServiceRegistration<?> hookRegistration) { - return false; + notifyHookPrivileged(systemBundleContext, registration, "added", (hook, hookRegistration) -> { //$NON-NLS-1$ + if (hook instanceof ListenerHook) { + ((ListenerHook) hook).added(listeners); } }); } @@ -1485,32 +1415,14 @@ public class ServiceRegistry { Debug.println("notifyServiceListenerHooks(" + listeners + "," + (added ? "added" : "removed") + ")"); //$NON-NLS-1$ //$NON-NLS-2$ //$NON-NLS-3$ //$NON-NLS-4$ //$NON-NLS-5$ } - notifyHooksPrivileged(new HookContext() { - @Override - public void call(Object hook, ServiceRegistration<?> hookRegistration) throws Exception { - if (hook instanceof ListenerHook) { - if (added) { - ((ListenerHook) hook).added(listeners); - } else { - ((ListenerHook) hook).removed(listeners); - } + notifyHooksPrivileged(listenerHookName, added ? "added" : "removed", (hook, hookRegistration) -> { //$NON-NLS-1$ //$NON-NLS-2$ + if (hook instanceof ListenerHook) { + if (added) { + ((ListenerHook) hook).added(listeners); + } else { + ((ListenerHook) hook).removed(listeners); } } - - @Override - public String getHookClassName() { - return listenerHookName; - } - - @Override - public String getHookMethodName() { - return added ? "added" : "removed"; //$NON-NLS-1$ //$NON-NLS-2$ - } - - @Override - public boolean skipRegistration(ServiceRegistration<?> hookRegistration) { - return false; - } }); } diff --git a/bundles/org.eclipse.osgi/container/src/org/eclipse/osgi/internal/weaving/WeavingHookConfigurator.java b/bundles/org.eclipse.osgi/container/src/org/eclipse/osgi/internal/weaving/WeavingHookConfigurator.java index 24acd3b27..8e2adaf8b 100644 --- a/bundles/org.eclipse.osgi/container/src/org/eclipse/osgi/internal/weaving/WeavingHookConfigurator.java +++ b/bundles/org.eclipse.osgi/container/src/org/eclipse/osgi/internal/weaving/WeavingHookConfigurator.java @@ -1,5 +1,5 @@ /******************************************************************************* - * Copyright (c) 2010, 2017 IBM Corporation and others. + * Copyright (c) 2010, 2020 IBM Corporation and others. * * This program and the accompanying materials * are made available under the terms of the Eclipse Public License 2.0 @@ -13,7 +13,11 @@ *******************************************************************************/ package org.eclipse.osgi.internal.weaving; -import java.util.*; +import java.util.ArrayList; +import java.util.Collections; +import java.util.List; +import java.util.Map; +import java.util.WeakHashMap; import org.eclipse.osgi.internal.framework.EquinoxContainer; import org.eclipse.osgi.internal.hookregistry.ClassLoaderHook; import org.eclipse.osgi.internal.loader.BundleLoader; @@ -22,7 +26,9 @@ import org.eclipse.osgi.internal.loader.classpath.ClasspathEntry; import org.eclipse.osgi.internal.loader.classpath.ClasspathManager; import org.eclipse.osgi.internal.serviceregistry.ServiceRegistry; import org.eclipse.osgi.storage.bundlefile.BundleEntry; -import org.osgi.framework.*; +import org.osgi.framework.Bundle; +import org.osgi.framework.FrameworkEvent; +import org.osgi.framework.ServiceRegistration; public class WeavingHookConfigurator extends ClassLoaderHook { static class WovenClassContext { @@ -30,8 +36,10 @@ public class WeavingHookConfigurator extends ClassLoaderHook { List<String> processClassNameStack = new ArrayList<>(6); } - // holds the map of black listed hooks. Use weak map to avoid pinning and simplify cleanup. - private final Map<ServiceRegistration<?>, Boolean> blackList = Collections.synchronizedMap(new WeakHashMap<ServiceRegistration<?>, Boolean>()); + // holds the map of denied hooks. Use weak map to avoid pinning and simplify + // cleanup. + private final Map<ServiceRegistration<?>, Boolean> deniedHooks = Collections + .synchronizedMap(new WeakHashMap<ServiceRegistration<?>, Boolean>()); // holds the stack of WovenClass objects currently being used to define classes private final ThreadLocal<WovenClassContext> wovenClassContext = new ThreadLocal<>(); @@ -53,7 +61,8 @@ public class WeavingHookConfigurator extends ClassLoaderHook { ModuleClassLoader classLoader = manager.getClassLoader(); BundleLoader loader = classLoader.getBundleLoader(); // create a woven class object and add it to the thread local stack - WovenClassImpl wovenClass = new WovenClassImpl(name, classbytes, entry, classpathEntry, loader, container, blackList); + WovenClassImpl wovenClass = new WovenClassImpl(name, classbytes, entry, classpathEntry, loader, container, + deniedHooks); WovenClassContext context = wovenClassContext.get(); if (context == null) { context = new WovenClassContext(); diff --git a/bundles/org.eclipse.osgi/container/src/org/eclipse/osgi/internal/weaving/WovenClassImpl.java b/bundles/org.eclipse.osgi/container/src/org/eclipse/osgi/internal/weaving/WovenClassImpl.java index e6bec24c6..77a8b4307 100644 --- a/bundles/org.eclipse.osgi/container/src/org/eclipse/osgi/internal/weaving/WovenClassImpl.java +++ b/bundles/org.eclipse.osgi/container/src/org/eclipse/osgi/internal/weaving/WovenClassImpl.java @@ -1,5 +1,5 @@ /******************************************************************************* - * Copyright (c) 2010, 2017 IBM Corporation and others. + * Copyright (c) 2010, 2020 IBM Corporation and others. * * This program and the accompanying materials * are made available under the terms of the Eclipse Public License 2.0 @@ -13,8 +13,13 @@ *******************************************************************************/ package org.eclipse.osgi.internal.weaving; -import java.security.*; -import java.util.*; +import java.security.AccessController; +import java.security.PrivilegedActionException; +import java.security.PrivilegedExceptionAction; +import java.security.ProtectionDomain; +import java.util.Collections; +import java.util.List; +import java.util.Map; import org.eclipse.osgi.container.ModuleRevision; import org.eclipse.osgi.internal.framework.EquinoxContainer; import org.eclipse.osgi.internal.hookregistry.ClassLoaderHook; @@ -27,22 +32,31 @@ import org.eclipse.osgi.storage.BundleInfo.Generation; import org.eclipse.osgi.storage.StorageUtil; import org.eclipse.osgi.storage.bundlefile.BundleEntry; import org.eclipse.osgi.util.ManifestElement; -import org.osgi.framework.*; -import org.osgi.framework.hooks.weaving.*; +import org.osgi.framework.AdminPermission; +import org.osgi.framework.BundleException; +import org.osgi.framework.Constants; +import org.osgi.framework.FrameworkEvent; +import org.osgi.framework.PackagePermission; +import org.osgi.framework.ServiceRegistration; +import org.osgi.framework.hooks.weaving.WeavingException; +import org.osgi.framework.hooks.weaving.WeavingHook; +import org.osgi.framework.hooks.weaving.WovenClass; +import org.osgi.framework.hooks.weaving.WovenClassListener; import org.osgi.framework.wiring.BundleWiring; public final class WovenClassImpl implements WovenClass, HookContext { private final static byte FLAG_HOOKCALLED = 0x01; private final static byte FLAG_HOOKSCOMPLETE = 0x02; private final static byte FLAG_WEAVINGCOMPLETE = 0x04; - private final static String weavingHookName = WeavingHook.class.getName(); + final static String weavingHookName = WeavingHook.class.getName(); + final static String wovenClassListenerName = WovenClassListener.class.getName(); private final String className; private final BundleEntry entry; private final List<String> dynamicImports; private final ClasspathEntry classpathEntry; private final BundleLoader loader; final ServiceRegistry registry; - private final Map<ServiceRegistration<?>, Boolean> blackList; + private final Map<ServiceRegistration<?>, Boolean> deniedHooks; private byte[] validBytes; private byte[] resultBytes; private byte hookFlags = 0; @@ -52,7 +66,7 @@ public final class WovenClassImpl implements WovenClass, HookContext { private int state; final EquinoxContainer container; - public WovenClassImpl(String className, byte[] bytes, BundleEntry entry, ClasspathEntry classpathEntry, BundleLoader loader, EquinoxContainer container, Map<ServiceRegistration<?>, Boolean> blacklist) { + public WovenClassImpl(String className, byte[] bytes, BundleEntry entry, ClasspathEntry classpathEntry, BundleLoader loader, EquinoxContainer container, Map<ServiceRegistration<?>, Boolean> deniedHooks) { super(); this.className = className; this.validBytes = this.resultBytes = bytes; @@ -62,7 +76,7 @@ public final class WovenClassImpl implements WovenClass, HookContext { this.loader = loader; this.registry = container.getServiceRegistry(); this.container = container; - this.blackList = blacklist; + this.deniedHooks = deniedHooks; setState(TRANSFORMING); } @@ -160,8 +174,8 @@ public final class WovenClassImpl implements WovenClass, HookContext { return; // do not call any other hooks once an error has occurred. if (hook instanceof WeavingHook) { if (skipRegistration(hookRegistration)) { - // Note we double check blacklist here just - // in case another thread blacklisted since the first check + // Note we double check denied hooks here just + // in case another thread denied the hook since the first check return; } if ((hookFlags & FLAG_HOOKCALLED) == 0) { @@ -176,19 +190,19 @@ public final class WovenClassImpl implements WovenClass, HookContext { } catch (WeavingException e) { error = e; errorHook = hookRegistration; - // do not blacklist on weaving exceptions + // do not deny the hook on weaving exceptions } catch (Throwable t) { error = t; // save the error to fail later errorHook = hookRegistration; - // put the registration on the black list - blackList.put(hookRegistration, Boolean.TRUE); + // deny the registration + deniedHooks.put(hookRegistration, Boolean.TRUE); } } } @Override public boolean skipRegistration(ServiceRegistration<?> hookRegistration) { - return blackList.containsKey(hookRegistration); + return deniedHooks.containsKey(hookRegistration); } private boolean validBytes(byte[] checkBytes) { @@ -205,52 +219,25 @@ public final class WovenClassImpl implements WovenClass, HookContext { return true; } - @Override - public String getHookMethodName() { - return "weave"; //$NON-NLS-1$ - } - - @Override - public String getHookClassName() { - return weavingHookName; - } - private void notifyWovenClassListeners() { - final HookContext context = new HookContext() { - @Override - public void call(Object hook, ServiceRegistration<?> hookRegistration) throws Exception { - if (!(hook instanceof WovenClassListener)) - return; - try { - ((WovenClassListener) hook).modified(WovenClassImpl.this); - } catch (Exception e) { - WovenClassImpl.this.container.getEventPublisher().publishFrameworkEvent(FrameworkEvent.ERROR, hookRegistration.getReference().getBundle(), e); - } - } - - @Override - public String getHookClassName() { - return WovenClassListener.class.getName(); - } - - @Override - public String getHookMethodName() { - return "modified"; //$NON-NLS-1$ - } - - @Override - public boolean skipRegistration(ServiceRegistration<?> hookRegistration) { - return false; + final HookContext context = (hook, hookRegistration) -> { + if (!(hook instanceof WovenClassListener)) + return; + try { + ((WovenClassListener) hook).modified(WovenClassImpl.this); + } catch (Exception e) { + WovenClassImpl.this.container.getEventPublisher().publishFrameworkEvent(FrameworkEvent.ERROR, + hookRegistration.getReference().getBundle(), e); } }; if (System.getSecurityManager() == null) - registry.notifyHooksPrivileged(context); + registry.notifyHooksPrivileged(wovenClassListenerName, "modified", context); //$NON-NLS-1$ else { try { AccessController.doPrivileged(new PrivilegedExceptionAction<Void>() { @Override public Void run() { - registry.notifyHooksPrivileged(context); + registry.notifyHooksPrivileged(wovenClassListenerName, "modified", context); //$NON-NLS-1$ return null; } }); @@ -267,13 +254,13 @@ public final class WovenClassImpl implements WovenClass, HookContext { boolean rejected = false; try { if (sm == null) { - registry.notifyHooksPrivileged(this); + registry.notifyHooksPrivileged(weavingHookName, "weave", this); //$NON-NLS-1$ } else { try { AccessController.doPrivileged(new PrivilegedExceptionAction<Void>() { @Override public Void run() { - registry.notifyHooksPrivileged(WovenClassImpl.this); + registry.notifyHooksPrivileged(weavingHookName, "weave", WovenClassImpl.this); //$NON-NLS-1$ return null; } }); |