diff --git a/bundles/org.eclipse.equinox.weaving.hook/src/org/eclipse/equinox/service/weaving/CacheEntry.java b/bundles/org.eclipse.equinox.weaving.hook/src/org/eclipse/equinox/service/weaving/CacheEntry.java index 04868824930..bf36bfc3f11 100644 --- a/bundles/org.eclipse.equinox.weaving.hook/src/org/eclipse/equinox/service/weaving/CacheEntry.java +++ b/bundles/org.eclipse.equinox.weaving.hook/src/org/eclipse/equinox/service/weaving/CacheEntry.java @@ -28,41 +28,41 @@ */ public class CacheEntry { - private final byte[] cachedBytes; + private final byte[] cachedBytes; - private final boolean dontWeave; + private final boolean dontWeave; - /** - * Creates a new cache entry. This item can tell the basic hook mechanism to - * use the given cached bytes for the class definition or if the original - * class bytes needs weaving or not - * - * @param dontWeave A flag that indicates whether this item needs to be - * woven or not - * @param cachedBytes The bytes for the class read from the cache - */ - public CacheEntry(final boolean dontWeave, final byte[] cachedBytes) { - this.dontWeave = dontWeave; - this.cachedBytes = cachedBytes; - } + /** + * Creates a new cache entry. This item can tell the basic hook mechanism to use + * the given cached bytes for the class definition or if the original class + * bytes needs weaving or not + * + * @param dontWeave A flag that indicates whether this item needs to be woven + * or not + * @param cachedBytes The bytes for the class read from the cache + */ + public CacheEntry(final boolean dontWeave, final byte[] cachedBytes) { + this.dontWeave = dontWeave; + this.cachedBytes = cachedBytes; + } - /** - * Tell the hook mechanism to weave a class or not to weave a class - * - * @return true, if the class doesn't need any weaving, otherwise false - */ - public boolean dontWeave() { - return dontWeave; - } + /** + * Tell the hook mechanism to weave a class or not to weave a class + * + * @return true, if the class doesn't need any weaving, otherwise false + */ + public boolean dontWeave() { + return dontWeave; + } - /** - * Returns the bytes that are read from the cache. These bytes should be - * used for defining the class instead of the original ones. - * - * @return The cached bytes for the class - */ - public byte[] getCachedBytes() { - return cachedBytes; - } + /** + * Returns the bytes that are read from the cache. These bytes should be used + * for defining the class instead of the original ones. + * + * @return The cached bytes for the class + */ + public byte[] getCachedBytes() { + return cachedBytes; + } } diff --git a/bundles/org.eclipse.equinox.weaving.hook/src/org/eclipse/equinox/service/weaving/ICachingService.java b/bundles/org.eclipse.equinox.weaving.hook/src/org/eclipse/equinox/service/weaving/ICachingService.java index 01822e1acc6..69bf2f160a6 100644 --- a/bundles/org.eclipse.equinox.weaving.hook/src/org/eclipse/equinox/service/weaving/ICachingService.java +++ b/bundles/org.eclipse.equinox.weaving.hook/src/org/eclipse/equinox/service/weaving/ICachingService.java @@ -22,18 +22,15 @@ public interface ICachingService { - public boolean canCacheGeneratedClasses(); + public boolean canCacheGeneratedClasses(); - public CacheEntry findStoredClass(String namespace, URL sourceFileURL, - String name); + public CacheEntry findStoredClass(String namespace, URL sourceFileURL, String name); - public void stop(); + public void stop(); - public boolean storeClass(String namespace, URL sourceFileURL, - Class clazz, byte[] classbytes); + public boolean storeClass(String namespace, URL sourceFileURL, Class clazz, byte[] classbytes); - public boolean storeClassAndGeneratedClasses(String namespace, - URL sourceFileURL, Class clazz, byte[] classbytes, - Map generatedClasses); + public boolean storeClassAndGeneratedClasses(String namespace, URL sourceFileURL, Class clazz, byte[] classbytes, + Map generatedClasses); } diff --git a/bundles/org.eclipse.equinox.weaving.hook/src/org/eclipse/equinox/service/weaving/ICachingServiceFactory.java b/bundles/org.eclipse.equinox.weaving.hook/src/org/eclipse/equinox/service/weaving/ICachingServiceFactory.java index 315f2a7092b..7061910694a 100644 --- a/bundles/org.eclipse.equinox.weaving.hook/src/org/eclipse/equinox/service/weaving/ICachingServiceFactory.java +++ b/bundles/org.eclipse.equinox.weaving.hook/src/org/eclipse/equinox/service/weaving/ICachingServiceFactory.java @@ -26,21 +26,20 @@ */ public interface ICachingServiceFactory { - /** - * Create concrete caching service for the given bundle. The caching service - * is then responsible to cache woven bytecode and retrieve those bytecodes - * from the cache. - * - * @param classLoader The classloader if the given bundle - * @param bundle The bundle the caching service should be created for - * @param key A fingerprint that is created by the concrete weavers to - * indicate what the weaving configuration for this bundle is. - * The caching service should be able to handle different keys - * for the same bundle in order not the deliver the wrong cached - * bytes from the cache - * @return The caching service for the given bundle - */ - public ICachingService createCachingService(ClassLoader classLoader, - Bundle bundle, String key); + /** + * Create concrete caching service for the given bundle. The caching service is + * then responsible to cache woven bytecode and retrieve those bytecodes from + * the cache. + * + * @param classLoader The classloader if the given bundle + * @param bundle The bundle the caching service should be created for + * @param key A fingerprint that is created by the concrete weavers to + * indicate what the weaving configuration for this bundle + * is. The caching service should be able to handle different + * keys for the same bundle in order not the deliver the + * wrong cached bytes from the cache + * @return The caching service for the given bundle + */ + public ICachingService createCachingService(ClassLoader classLoader, Bundle bundle, String key); } diff --git a/bundles/org.eclipse.equinox.weaving.hook/src/org/eclipse/equinox/service/weaving/ISupplementerRegistry.java b/bundles/org.eclipse.equinox.weaving.hook/src/org/eclipse/equinox/service/weaving/ISupplementerRegistry.java index e599723b57d..a269701649c 100644 --- a/bundles/org.eclipse.equinox.weaving.hook/src/org/eclipse/equinox/service/weaving/ISupplementerRegistry.java +++ b/bundles/org.eclipse.equinox.weaving.hook/src/org/eclipse/equinox/service/weaving/ISupplementerRegistry.java @@ -23,33 +23,32 @@ public interface ISupplementerRegistry { - public void addBundle(final Bundle bundle); + public void addBundle(final Bundle bundle); - public void addSupplementedBundle(final Bundle bundle); + public void addSupplementedBundle(final Bundle bundle); - public void addSupplementer(final Bundle bundle, final boolean updateBundles); + public void addSupplementer(final Bundle bundle, final boolean updateBundles); - public List getMatchingSupplementers( - final String symbolicName, final ManifestElement[] imports, - final ManifestElement[] exports); + public List getMatchingSupplementers(final String symbolicName, final ManifestElement[] imports, + final ManifestElement[] exports); - public PackageAdmin getPackageAdmin(); + public PackageAdmin getPackageAdmin(); - public Supplementer[] getSupplementers(final Bundle bundle); + public Supplementer[] getSupplementers(final Bundle bundle); - public Supplementer[] getSupplementers(final long bundleID); + public Supplementer[] getSupplementers(final long bundleID); - /** - * Refreshes the given bundles - * - * @param bundles The bundles to refresh - */ - public void refreshBundles(final Bundle[] bundles); + /** + * Refreshes the given bundles + * + * @param bundles The bundles to refresh + */ + public void refreshBundles(final Bundle[] bundles); - public void removeBundle(final Bundle bundle); + public void removeBundle(final Bundle bundle); - public void setBundleContext(final BundleContext context); + public void setBundleContext(final BundleContext context); - public void setPackageAdmin(final PackageAdmin packageAdmin); + public void setPackageAdmin(final PackageAdmin packageAdmin); } diff --git a/bundles/org.eclipse.equinox.weaving.hook/src/org/eclipse/equinox/service/weaving/IWeavingService.java b/bundles/org.eclipse.equinox.weaving.hook/src/org/eclipse/equinox/service/weaving/IWeavingService.java index b1e2efbcf86..2686eae8182 100644 --- a/bundles/org.eclipse.equinox.weaving.hook/src/org/eclipse/equinox/service/weaving/IWeavingService.java +++ b/bundles/org.eclipse.equinox.weaving.hook/src/org/eclipse/equinox/service/weaving/IWeavingService.java @@ -29,64 +29,62 @@ */ public interface IWeavingService { - /** - * Flush all generated classes from the weaving service so that memory kept - * by the weaving service for additional classes can be freed. - * - * @param loader The class loader the weaving service belongs to - */ - public void flushGeneratedClasses(ClassLoader loader); + /** + * Flush all generated classes from the weaving service so that memory kept by + * the weaving service for additional classes can be freed. + * + * @param loader The class loader the weaving service belongs to + */ + public void flushGeneratedClasses(ClassLoader loader); - /** - * Has the weaving service generated new classes on the fly for the given - * class? - * - * @param loader The class loader of the woven class - * @param className The name of the woven class - * @return true, if the weaving service has generated additional classes for - * the woven class (closures, for example) - */ - public boolean generatedClassesExistFor(ClassLoader loader, String className); + /** + * Has the weaving service generated new classes on the fly for the given class? + * + * @param loader The class loader of the woven class + * @param className The name of the woven class + * @return true, if the weaving service has generated additional classes for the + * woven class (closures, for example) + */ + public boolean generatedClassesExistFor(ClassLoader loader, String className); - /** - * Returns a map that contains all generated classes for the given class. - * Implementations of this method should remove those classes from internal - * lists (to free memory). This means also that calling this method a second - * time will return an emptry map. - * - * @param className The name of the class for which additional classes were - * generated - * @return The generated classes (key: generated class name, value: - * generated class bytecode) - */ - public Map getGeneratedClassesFor(String className); + /** + * Returns a map that contains all generated classes for the given class. + * Implementations of this method should remove those classes from internal + * lists (to free memory). This means also that calling this method a second + * time will return an emptry map. + * + * @param className The name of the class for which additional classes were + * generated + * @return The generated classes (key: generated class name, value: generated + * class bytecode) + */ + public Map getGeneratedClassesFor(String className); - /** - * The key of a concrete weaver for a bundle defines the setting in which - * the weaver works. This key typically defines a unique key for the set of - * aspects which are woven into this bundle. The core equinox aspects - * runtime uses this key to feed the caching service. This means, the weaver - * should return different keys for different set of aspects (including - * versions), respectively when the cache should switch its context. - * - * @return A unique key to define the set of aspects that are woven into the - * bundle to which this weaver belongs - */ - public String getKey(); + /** + * The key of a concrete weaver for a bundle defines the setting in which the + * weaver works. This key typically defines a unique key for the set of aspects + * which are woven into this bundle. The core equinox aspects runtime uses this + * key to feed the caching service. This means, the weaver should return + * different keys for different set of aspects (including versions), + * respectively when the cache should switch its context. + * + * @return A unique key to define the set of aspects that are woven into the + * bundle to which this weaver belongs + */ + public String getKey(); - /** - * This method is called for each class which is loaded into the JVM and not - * read from cache to do the actual weaving, if necessary. - * - * @param name The fully qualified name of the class to be loaded - * @param classbytes The original unmodified bytecode of the class read by - * the standard OSGi classloading mechanism - * @param loader The classloader whichi s responsible for loading the class - * @return The modified (woven) bytecode of the class or null, if no - * modification happened - * @throws IOException - */ - public byte[] preProcess(String name, byte[] classbytes, ClassLoader loader) - throws IOException; + /** + * This method is called for each class which is loaded into the JVM and not + * read from cache to do the actual weaving, if necessary. + * + * @param name The fully qualified name of the class to be loaded + * @param classbytes The original unmodified bytecode of the class read by the + * standard OSGi classloading mechanism + * @param loader The classloader whichi s responsible for loading the class + * @return The modified (woven) bytecode of the class or null, if no + * modification happened + * @throws IOException + */ + public byte[] preProcess(String name, byte[] classbytes, ClassLoader loader) throws IOException; } diff --git a/bundles/org.eclipse.equinox.weaving.hook/src/org/eclipse/equinox/service/weaving/IWeavingServiceFactory.java b/bundles/org.eclipse.equinox.weaving.hook/src/org/eclipse/equinox/service/weaving/IWeavingServiceFactory.java index 188c429a497..d9e7a673087 100644 --- a/bundles/org.eclipse.equinox.weaving.hook/src/org/eclipse/equinox/service/weaving/IWeavingServiceFactory.java +++ b/bundles/org.eclipse.equinox.weaving.hook/src/org/eclipse/equinox/service/weaving/IWeavingServiceFactory.java @@ -27,21 +27,21 @@ */ public interface IWeavingServiceFactory { - /** - * Create a concrete weaving implementation for the given bundle. This is - * called by the basic equinox aspects weaving hook mechanism lazily when - * the classloader for the bundle is created. - * - * @param loader The classloader of the bundle for which to create a weaver - * @param bundle The bundle for which to create the weaver - * @param bundleRevision The revision of the bundle for which to create a - * weaver - * @param supplementerRegistry The supplementer registry which is used by - * the core equinox aspects hook - * @return The created weaver for the given bundle - */ - public IWeavingService createWeavingService(ClassLoader loader, - Bundle bundle, BundleRevision bundleRevision, - ISupplementerRegistry supplementerRegistry); + /** + * Create a concrete weaving implementation for the given bundle. This is called + * by the basic equinox aspects weaving hook mechanism lazily when the + * classloader for the bundle is created. + * + * @param loader The classloader of the bundle for which to create + * a weaver + * @param bundle The bundle for which to create the weaver + * @param bundleRevision The revision of the bundle for which to create a + * weaver + * @param supplementerRegistry The supplementer registry which is used by the + * core equinox aspects hook + * @return The created weaver for the given bundle + */ + public IWeavingService createWeavingService(ClassLoader loader, Bundle bundle, BundleRevision bundleRevision, + ISupplementerRegistry supplementerRegistry); } diff --git a/bundles/org.eclipse.equinox.weaving.hook/src/org/eclipse/equinox/service/weaving/Supplementer.java b/bundles/org.eclipse.equinox.weaving.hook/src/org/eclipse/equinox/service/weaving/Supplementer.java index 448a5dc2abb..e7c6851e762 100644 --- a/bundles/org.eclipse.equinox.weaving.hook/src/org/eclipse/equinox/service/weaving/Supplementer.java +++ b/bundles/org.eclipse.equinox.weaving.hook/src/org/eclipse/equinox/service/weaving/Supplementer.java @@ -33,204 +33,199 @@ */ public class Supplementer { - private final ManifestElement[] supplementBundle; - - private final Set supplementedBundles; // elements of type Bundle - - private final Bundle supplementer; - - private final Bundle supplementerHost; - - private final ManifestElement[] supplementExporter; - - private final ManifestElement[] supplementImporter; - - /** - * Creates a supplementer object for the given bundle. - * - * @param bundle The bundle that defines the supplementer headers - * @param bundleHost The host bundle of the supplementer bundle, if the - * bundle is a fragment, otherwise null - * @param supplementBundle The parsed manifest headers defined for - * Eclipse-SupplementBundle - * @param supplementImporter The parsed manifest headers defined for - * Eclipse-SupplementImporter - * @param supplementExporter The parsed manifest headers defined for - * Eclipse-SupplementExporter - */ - public Supplementer(final Bundle bundle, final Bundle bundleHost, - final ManifestElement[] supplementBundle, - final ManifestElement[] supplementImporter, - final ManifestElement[] supplementExporter) { - this.supplementer = bundle; - this.supplementerHost = bundleHost != null ? bundleHost : bundle; - this.supplementBundle = supplementBundle; - this.supplementImporter = supplementImporter; - this.supplementExporter = supplementExporter; - this.supplementedBundles = new HashSet<>(); - } - - /** - * Add a bundle to the list of supplemented bundles - * - * @param supplementedBundle The bundle that is supplemented by this - * supplementer - */ - public void addSupplementedBundle(final Bundle supplementedBundle) { - this.supplementedBundles.add(supplementedBundle); - } - - //knibb - //test if two Strings are equal - //with wild card support - only supports strings ending in * - private boolean equals_wild(final String input, final String match) { - if (input.equals(match)) { - //its a match so just return true - return true; - } - if (input.endsWith("*") == false) { //$NON-NLS-1$ - //no wildcards are being used here - return false; - } - final String wild_in = input.substring(0, input.length() - 1); - if (match.startsWith(wild_in)) return true; - - return false; - } - - /** - * Gives information about which bundles are currently supplemented by this - * supplementer - * - * @return The currently supplemented bundles - */ - public Bundle[] getSupplementedBundles() { - return supplementedBundles.toArray(new Bundle[supplementedBundles - .size()]); - } - - /** - * Returns the bundle that defines the supplementer headers (this - * supplementer object belongs to) - * - * @return The bundle object this supplementer belongs to - */ - public Bundle getSupplementerBundle() { - return supplementer; - } - - /** - * Returns the host of the supplementer bundle, if it is a fragment, - * otherwise this returns the same as getSupplementerBundle() - * - * @return The host bundle this supplementer belongs to - */ - public Bundle getSupplementerHost() { - return supplementerHost; - } - - /** - * The symbolic name of the supplementer bundle - * - * @return The symbolic name of the supplementer bundle - */ - public String getSymbolicName() { - return supplementer.getSymbolicName(); - } - - /** - * Provides information about whether a given bundle is supplemented by this - * supplementer or not - * - * @param bundle The bundle that might be supplemented by this supplementer - * @return true, if the bundle is supplemented by this supplementer, - * otherwise false - */ - public boolean isSupplemented(final Bundle bundle) { - return supplementedBundles.contains(bundle); - } - - /** - * Checks if the given export-package header definitions matches the - * supplement-exporter definitions of this supplementer - * - * @param exports The headers to check for matching against this - * supplementer - * @return true, if this supplementer matches against the given - * export-package headers - */ - public boolean matchesSupplementExporter(final ManifestElement[] exports) { - boolean matches = false; - - if (supplementExporter != null) - for (int i = 0; !matches && i < supplementExporter.length; i++) { - final ManifestElement supplementExport = supplementExporter[i]; - for (int j = 0; !matches && j < exports.length; j++) { - final ManifestElement exportPackage = exports[j]; - if (supplementExport.getValue().equals( - exportPackage.getValue())) matches = true; - } - } - - return matches; - } - - /** - * Checks if the given import-package header definitions matches the - * supplement-importer definitions of this supplementer - * - * @param imports The headers to check for matching against this - * supplementer - * @return true, if this supplementer matches against the given - * import-package headers - */ - public boolean matchesSupplementImporter(final ManifestElement[] imports) { - boolean matches = false; - - if (supplementImporter != null) - for (int i = 0; !matches && i < supplementImporter.length; i++) { - final ManifestElement supplementImport = supplementImporter[i]; - for (int j = 0; !matches && j < imports.length; j++) { - final ManifestElement importPackage = imports[j]; - if (supplementImport.getValue().equals( - importPackage.getValue())) matches = true; - } - } - - return matches; - } - - /** - * Checks if the given bundle symbolic name definition matches the - * supplement-bundle definition of this supplementer - * - * @param symbolicName The symbolic name of the bundle that shoudl be - * checked - * @return true, if this supplementer matches against the given bundle - * symbolic name - */ - public boolean matchSupplementer(final String symbolicName) { - boolean matches = false; - - if (supplementBundle != null) - for (int i = 0; !matches && i < supplementBundle.length; i++) { - final ManifestElement bundle = supplementBundle[i]; - if (equals_wild(bundle.getValue(), symbolicName)) - matches = true; - } - - return matches; - } - - /** - * Removes the given bundle from the set of supplemented bundles (that are - * supplemented by this supplementer) - * - * @param supplementedBundle The bundle that is no longer supplemented by - * this supplementer - */ - public void removeSupplementedBundle(final Bundle supplementedBundle) { - this.supplementedBundles.remove(supplementedBundle); - } + private final ManifestElement[] supplementBundle; + + private final Set supplementedBundles; // elements of type Bundle + + private final Bundle supplementer; + + private final Bundle supplementerHost; + + private final ManifestElement[] supplementExporter; + + private final ManifestElement[] supplementImporter; + + /** + * Creates a supplementer object for the given bundle. + * + * @param bundle The bundle that defines the supplementer headers + * @param bundleHost The host bundle of the supplementer bundle, if the + * bundle is a fragment, otherwise null + * @param supplementBundle The parsed manifest headers defined for + * Eclipse-SupplementBundle + * @param supplementImporter The parsed manifest headers defined for + * Eclipse-SupplementImporter + * @param supplementExporter The parsed manifest headers defined for + * Eclipse-SupplementExporter + */ + public Supplementer(final Bundle bundle, final Bundle bundleHost, final ManifestElement[] supplementBundle, + final ManifestElement[] supplementImporter, final ManifestElement[] supplementExporter) { + this.supplementer = bundle; + this.supplementerHost = bundleHost != null ? bundleHost : bundle; + this.supplementBundle = supplementBundle; + this.supplementImporter = supplementImporter; + this.supplementExporter = supplementExporter; + this.supplementedBundles = new HashSet<>(); + } + + /** + * Add a bundle to the list of supplemented bundles + * + * @param supplementedBundle The bundle that is supplemented by this + * supplementer + */ + public void addSupplementedBundle(final Bundle supplementedBundle) { + this.supplementedBundles.add(supplementedBundle); + } + + // knibb + // test if two Strings are equal + // with wild card support - only supports strings ending in * + private boolean equals_wild(final String input, final String match) { + if (input.equals(match)) { + // its a match so just return true + return true; + } + if (input.endsWith("*") == false) { //$NON-NLS-1$ + // no wildcards are being used here + return false; + } + final String wild_in = input.substring(0, input.length() - 1); + if (match.startsWith(wild_in)) + return true; + + return false; + } + + /** + * Gives information about which bundles are currently supplemented by this + * supplementer + * + * @return The currently supplemented bundles + */ + public Bundle[] getSupplementedBundles() { + return supplementedBundles.toArray(new Bundle[supplementedBundles.size()]); + } + + /** + * Returns the bundle that defines the supplementer headers (this supplementer + * object belongs to) + * + * @return The bundle object this supplementer belongs to + */ + public Bundle getSupplementerBundle() { + return supplementer; + } + + /** + * Returns the host of the supplementer bundle, if it is a fragment, otherwise + * this returns the same as getSupplementerBundle() + * + * @return The host bundle this supplementer belongs to + */ + public Bundle getSupplementerHost() { + return supplementerHost; + } + + /** + * The symbolic name of the supplementer bundle + * + * @return The symbolic name of the supplementer bundle + */ + public String getSymbolicName() { + return supplementer.getSymbolicName(); + } + + /** + * Provides information about whether a given bundle is supplemented by this + * supplementer or not + * + * @param bundle The bundle that might be supplemented by this supplementer + * @return true, if the bundle is supplemented by this supplementer, otherwise + * false + */ + public boolean isSupplemented(final Bundle bundle) { + return supplementedBundles.contains(bundle); + } + + /** + * Checks if the given export-package header definitions matches the + * supplement-exporter definitions of this supplementer + * + * @param exports The headers to check for matching against this supplementer + * @return true, if this supplementer matches against the given export-package + * headers + */ + public boolean matchesSupplementExporter(final ManifestElement[] exports) { + boolean matches = false; + + if (supplementExporter != null) + for (int i = 0; !matches && i < supplementExporter.length; i++) { + final ManifestElement supplementExport = supplementExporter[i]; + for (int j = 0; !matches && j < exports.length; j++) { + final ManifestElement exportPackage = exports[j]; + if (supplementExport.getValue().equals(exportPackage.getValue())) + matches = true; + } + } + + return matches; + } + + /** + * Checks if the given import-package header definitions matches the + * supplement-importer definitions of this supplementer + * + * @param imports The headers to check for matching against this supplementer + * @return true, if this supplementer matches against the given import-package + * headers + */ + public boolean matchesSupplementImporter(final ManifestElement[] imports) { + boolean matches = false; + + if (supplementImporter != null) + for (int i = 0; !matches && i < supplementImporter.length; i++) { + final ManifestElement supplementImport = supplementImporter[i]; + for (int j = 0; !matches && j < imports.length; j++) { + final ManifestElement importPackage = imports[j]; + if (supplementImport.getValue().equals(importPackage.getValue())) + matches = true; + } + } + + return matches; + } + + /** + * Checks if the given bundle symbolic name definition matches the + * supplement-bundle definition of this supplementer + * + * @param symbolicName The symbolic name of the bundle that shoudl be checked + * @return true, if this supplementer matches against the given bundle symbolic + * name + */ + public boolean matchSupplementer(final String symbolicName) { + boolean matches = false; + + if (supplementBundle != null) + for (int i = 0; !matches && i < supplementBundle.length; i++) { + final ManifestElement bundle = supplementBundle[i]; + if (equals_wild(bundle.getValue(), symbolicName)) + matches = true; + } + + return matches; + } + + /** + * Removes the given bundle from the set of supplemented bundles (that are + * supplemented by this supplementer) + * + * @param supplementedBundle The bundle that is no longer supplemented by this + * supplementer + */ + public void removeSupplementedBundle(final Bundle supplementedBundle) { + this.supplementedBundles.remove(supplementedBundle); + } } diff --git a/bundles/org.eclipse.equinox.weaving.hook/src/org/eclipse/equinox/weaving/adaptors/Debug.java b/bundles/org.eclipse.equinox.weaving.hook/src/org/eclipse/equinox/weaving/adaptors/Debug.java index 071240e7f4c..cf4668c2a18 100644 --- a/bundles/org.eclipse.equinox.weaving.hook/src/org/eclipse/equinox/weaving/adaptors/Debug.java +++ b/bundles/org.eclipse.equinox.weaving.hook/src/org/eclipse/equinox/weaving/adaptors/Debug.java @@ -18,56 +18,48 @@ public class Debug { - public static final String ASPECTJ_OSGI = "org.eclipse.equinox.weaving.hook"; //$NON-NLS-1$ + public static final String ASPECTJ_OSGI = "org.eclipse.equinox.weaving.hook"; //$NON-NLS-1$ - public static boolean DEBUG_BUNDLE = false; + public static boolean DEBUG_BUNDLE = false; - public static String DEBUG_BUNDLENAME; + public static String DEBUG_BUNDLENAME; - public static boolean DEBUG_CACHE = false; + public static boolean DEBUG_CACHE = false; - public static boolean DEBUG_GENERAL = false; + public static boolean DEBUG_GENERAL = false; - public static boolean DEBUG_SUPPLEMENTS = false; + public static boolean DEBUG_SUPPLEMENTS = false; - public static boolean DEBUG_WEAVE = false; + public static boolean DEBUG_WEAVE = false; - public static final String OPTION_DEBUG_BUNDLE = ASPECTJ_OSGI - + "/debug/bundle"; //$NON-NLS-1$ + public static final String OPTION_DEBUG_BUNDLE = ASPECTJ_OSGI + "/debug/bundle"; //$NON-NLS-1$ - public static final String OPTION_DEBUG_BUNDLENAME = ASPECTJ_OSGI - + "/debug/bundleName"; //$NON-NLS-1$ + public static final String OPTION_DEBUG_BUNDLENAME = ASPECTJ_OSGI + "/debug/bundleName"; //$NON-NLS-1$ - public static final String OPTION_DEBUG_CACHE = ASPECTJ_OSGI - + "/debug/cache"; //$NON-NLS-1$ + public static final String OPTION_DEBUG_CACHE = ASPECTJ_OSGI + "/debug/cache"; //$NON-NLS-1$ - public static final String OPTION_DEBUG_GENERAL = ASPECTJ_OSGI + "/debug"; //$NON-NLS-1$ + public static final String OPTION_DEBUG_GENERAL = ASPECTJ_OSGI + "/debug"; //$NON-NLS-1$ - public static final String OPTION_DEBUG_SUPPLEMENTS = ASPECTJ_OSGI - + "/debug/supplements"; //$NON-NLS-1$ + public static final String OPTION_DEBUG_SUPPLEMENTS = ASPECTJ_OSGI + "/debug/supplements"; //$NON-NLS-1$ - public static final String OPTION_DEBUG_WEAVE = ASPECTJ_OSGI - + "/debug/weave"; //$NON-NLS-1$ + public static final String OPTION_DEBUG_WEAVE = ASPECTJ_OSGI + "/debug/weave"; //$NON-NLS-1$ - public static boolean bundleNameMatches(final String name) { - return name.equals(DEBUG_BUNDLENAME); - } + public static boolean bundleNameMatches(final String name) { + return name.equals(DEBUG_BUNDLENAME); + } - public static void init(final DebugOptions options) { - if (options != null) { - DEBUG_GENERAL = options.getBooleanOption(OPTION_DEBUG_GENERAL, - false); - DEBUG_BUNDLE = options.getBooleanOption(OPTION_DEBUG_BUNDLE, false); - DEBUG_WEAVE = options.getBooleanOption(OPTION_DEBUG_WEAVE, false); - DEBUG_CACHE = options.getBooleanOption(OPTION_DEBUG_CACHE, false); - DEBUG_BUNDLENAME = options.getOption(OPTION_DEBUG_BUNDLENAME, ""); //$NON-NLS-1$ - DEBUG_SUPPLEMENTS = options.getBooleanOption( - OPTION_DEBUG_SUPPLEMENTS, false); - } - } + public static void init(final DebugOptions options) { + if (options != null) { + DEBUG_GENERAL = options.getBooleanOption(OPTION_DEBUG_GENERAL, false); + DEBUG_BUNDLE = options.getBooleanOption(OPTION_DEBUG_BUNDLE, false); + DEBUG_WEAVE = options.getBooleanOption(OPTION_DEBUG_WEAVE, false); + DEBUG_CACHE = options.getBooleanOption(OPTION_DEBUG_CACHE, false); + DEBUG_BUNDLENAME = options.getOption(OPTION_DEBUG_BUNDLENAME, ""); //$NON-NLS-1$ + DEBUG_SUPPLEMENTS = options.getBooleanOption(OPTION_DEBUG_SUPPLEMENTS, false); + } + } - public static void println(final String s) { - /* if (s.indexOf("org.eclipse.osgi.tests") != -1) */System.err - .println(s); - } + public static void println(final String s) { + /* if (s.indexOf("org.eclipse.osgi.tests") != -1) */System.err.println(s); + } } diff --git a/bundles/org.eclipse.equinox.weaving.hook/src/org/eclipse/equinox/weaving/adaptors/IWeavingAdaptor.java b/bundles/org.eclipse.equinox.weaving.hook/src/org/eclipse/equinox/weaving/adaptors/IWeavingAdaptor.java index f3f4ae8f901..6765dda07a8 100644 --- a/bundles/org.eclipse.equinox.weaving.hook/src/org/eclipse/equinox/weaving/adaptors/IWeavingAdaptor.java +++ b/bundles/org.eclipse.equinox.weaving.hook/src/org/eclipse/equinox/weaving/adaptors/IWeavingAdaptor.java @@ -21,15 +21,14 @@ public interface IWeavingAdaptor { - public CacheEntry findClass(String name, URL sourceFileURL); + public CacheEntry findClass(String name, URL sourceFileURL); - public void initialize(); + public void initialize(); - public boolean isInitialized(); + public boolean isInitialized(); - public boolean storeClass(String name, URL sourceFileURL, Class clazz, - byte[] classbytes); + public boolean storeClass(String name, URL sourceFileURL, Class clazz, byte[] classbytes); - public byte[] weaveClass(String name, byte[] bytes); + public byte[] weaveClass(String name, byte[] bytes); } diff --git a/bundles/org.eclipse.equinox.weaving.hook/src/org/eclipse/equinox/weaving/adaptors/WeavingAdaptor.java b/bundles/org.eclipse.equinox.weaving.hook/src/org/eclipse/equinox/weaving/adaptors/WeavingAdaptor.java index f3b84ee3e5a..0dfb5c96c38 100644 --- a/bundles/org.eclipse.equinox.weaving.hook/src/org/eclipse/equinox/weaving/adaptors/WeavingAdaptor.java +++ b/bundles/org.eclipse.equinox.weaving.hook/src/org/eclipse/equinox/weaving/adaptors/WeavingAdaptor.java @@ -36,217 +36,207 @@ public class WeavingAdaptor implements IWeavingAdaptor { - private static class ThreadLocalSet extends ThreadLocal> { - - public boolean contains(final Object obj) { - final Set set = get(); - return set.contains(obj); - } - - @Override - protected Set initialValue() { - return new HashSet<>(); - } - - public void put(final Object obj) { - final Set set = get(); - if (set.contains(obj)) { - throw new RuntimeException(obj.toString()); - } - set.add(obj); - } - - public void remove(final Object obj) { - final Set set = get(); - if (!set.contains(obj)) { - throw new RuntimeException(obj.toString()); - } - set.remove(obj); - } - } - - private static ThreadLocalSet identifyRecursionSet = new ThreadLocalSet(); - - private Bundle bundle; - - private ICachingService cachingService; - - private final WeavingAdaptorFactory factory; - - private final Generation generation; - - private boolean initialized = false; - - private final ModuleClassLoader moduleLoader; - - private final String symbolicName; - - private IWeavingService weavingService; - - public WeavingAdaptor(final Generation generation, - final WeavingAdaptorFactory serviceFactory, - final IWeavingService weavingService, - final ICachingService cachingService, - final ModuleClassLoader classLoader) { - this.generation = generation; - this.factory = serviceFactory; - this.symbolicName = generation.getRevision().getSymbolicName(); - this.moduleLoader = classLoader; - if (Debug.DEBUG_GENERAL) - Debug.println("- WeavingAdaptor.WeavingAdaptor() bundle=" //$NON-NLS-1$ - + symbolicName); - } - - @Override - public CacheEntry findClass(final String name, final URL sourceFileURL) { - if (Debug.DEBUG_CACHE) - Debug.println("> WeavingAdaptor.findClass() bundle=" + symbolicName //$NON-NLS-1$ - + ", url=" + sourceFileURL + ", name=" + name); //$NON-NLS-1$ //$NON-NLS-2$ - CacheEntry cacheEntry = null; - - initialize(); - if (cachingService != null) { - cacheEntry = cachingService - .findStoredClass("", sourceFileURL, name); //$NON-NLS-1$ - } - - if (Debug.DEBUG_CACHE) - Debug.println("< WeavingAdaptor.findClass() cacheEntry=" //$NON-NLS-1$ - + cacheEntry); - return cacheEntry; - } - - @Override - public void initialize() { - synchronized (this) { - if (initialized) return; - - this.bundle = generation.getRevision().getBundle(); - if (!identifyRecursionSet.contains(this)) { - identifyRecursionSet.put(this); - - if (Debug.DEBUG_GENERAL) - Debug.println("> WeavingAdaptor.initialize() bundle=" //$NON-NLS-1$ - + symbolicName + ", moduleLoader=" + moduleLoader); //$NON-NLS-1$ - - if (symbolicName != null && symbolicName.startsWith("org.aspectj")) { //$NON-NLS-1$ - if (Debug.DEBUG_GENERAL) - Debug.println("- WeavingAdaptor.initialize() symbolicName=" //$NON-NLS-1$ - + symbolicName + ", moduleLoader=" //$NON-NLS-1$ - + moduleLoader); - } else if (moduleLoader != null) { - weavingService = factory.getWeavingService(moduleLoader); - cachingService = factory.getCachingService(moduleLoader, - bundle, weavingService); - } else if ((generation.getRevision().getTypes() & BundleRevision.TYPE_FRAGMENT) != 0) { - - final Bundle host = factory.getHost(bundle); - if (Debug.DEBUG_GENERAL) - Debug.println("- WeavingAdaptor.initialize() symbolicName=" //$NON-NLS-1$ - + symbolicName + ", host=" + host); //$NON-NLS-1$ - - final Generation hostGeneration = (Generation) ((ModuleRevision) host - .adapt(BundleRevision.class)).getRevisionInfo(); - final BundleFile bundleFile = hostGeneration - .getBundleFile(); - if (bundleFile instanceof WeavingBundleFile) { - final WeavingBundleFile hostFile = (WeavingBundleFile) bundleFile; - final WeavingAdaptor hostAdaptor = (WeavingAdaptor) hostFile - .getAdaptor(); - weavingService = hostAdaptor.weavingService; - cachingService = factory.getCachingService( - hostAdaptor.moduleLoader, bundle, - weavingService); - } - } else { - if (Debug.DEBUG_GENERAL) - Debug.println("W WeavingAdaptor.initialize() symbolicName=" //$NON-NLS-1$ - + symbolicName + ", baseLoader=" + moduleLoader); //$NON-NLS-1$ - } - initialized = true; - identifyRecursionSet.remove(this); - } - - if (Debug.DEBUG_GENERAL) - Debug.println("< WeavingAdaptor.initialize() weavingService=" //$NON-NLS-1$ - + (weavingService != null) + ", cachingService=" //$NON-NLS-1$ - + (cachingService != null)); - } - } - - @Override - public boolean isInitialized() { - return initialized; - } - - @Override - public boolean storeClass(final String name, final URL sourceFileURL, - final Class clazz, final byte[] classbytes) { - if (Debug.DEBUG_CACHE) - Debug.println("> WeavingAdaptor.storeClass() bundle=" //$NON-NLS-1$ - + symbolicName + ", url=" + sourceFileURL //$NON-NLS-1$ - + ", name=" //$NON-NLS-1$ - + name + ", clazz=" + clazz); //$NON-NLS-1$ - boolean stored = false; - - initialize(); - if (cachingService != null) { - //have we generated a closure? - if (weavingService != null - && weavingService.generatedClassesExistFor(moduleLoader, - name)) { - //If so we need to ask the cache if its capable of handling generated closures - if (cachingService.canCacheGeneratedClasses()) { - final Map generatedClasses = weavingService - .getGeneratedClassesFor(name); - - stored = cachingService.storeClassAndGeneratedClasses("", //$NON-NLS-1$ - sourceFileURL, clazz, classbytes, generatedClasses); - } else { - weavingService.flushGeneratedClasses(moduleLoader); - if (Debug.DEBUG_CACHE) - Debug.println("- WeavingAdaptor.storeClass() generatedClassesExistFor=true"); //$NON-NLS-1$ - } - } else { - stored = cachingService.storeClass("", sourceFileURL, clazz, //$NON-NLS-1$ - classbytes); - if (!stored) { - if (Debug.DEBUG_CACHE) - Debug.println("E WeavingAdaptor.storeClass() bundle=" //$NON-NLS-1$ - + symbolicName + ", name=" + name); //$NON-NLS-1$ - } - } - } - if (Debug.DEBUG_CACHE) - Debug.println("< WeavingAdaptor.storeClass() stored=" + stored); //$NON-NLS-1$ - return stored; - } - - @Override - public String toString() { - return "WeavingAdaptor[" + symbolicName + "]"; //$NON-NLS-1$ //$NON-NLS-2$ - } - - @Override - public byte[] weaveClass(final String name, final byte[] bytes) { - if (Debug.DEBUG_WEAVE) - Debug.println("> WeavingAdaptor.weaveClass() bundle=" //$NON-NLS-1$ - + symbolicName + ", name=" + name + ", bytes=" //$NON-NLS-1$ //$NON-NLS-2$ - + bytes.length); - byte[] newBytes = null; - - initialize(); - if (/* shouldWeave(bytes) && */weavingService != null) { - try { - newBytes = weavingService.preProcess(name, bytes, moduleLoader); - } catch (final IOException ex) { - throw new ClassFormatError(ex.toString()); - } - } - - if (Debug.DEBUG_WEAVE) - Debug.println("< WeavingAdaptor.weaveClass() newBytes=" + newBytes); //$NON-NLS-1$ - return newBytes; - } + private static class ThreadLocalSet extends ThreadLocal> { + + public boolean contains(final Object obj) { + final Set set = get(); + return set.contains(obj); + } + + @Override + protected Set initialValue() { + return new HashSet<>(); + } + + public void put(final Object obj) { + final Set set = get(); + if (set.contains(obj)) { + throw new RuntimeException(obj.toString()); + } + set.add(obj); + } + + public void remove(final Object obj) { + final Set set = get(); + if (!set.contains(obj)) { + throw new RuntimeException(obj.toString()); + } + set.remove(obj); + } + } + + private static ThreadLocalSet identifyRecursionSet = new ThreadLocalSet(); + + private Bundle bundle; + + private ICachingService cachingService; + + private final WeavingAdaptorFactory factory; + + private final Generation generation; + + private boolean initialized = false; + + private final ModuleClassLoader moduleLoader; + + private final String symbolicName; + + private IWeavingService weavingService; + + public WeavingAdaptor(final Generation generation, final WeavingAdaptorFactory serviceFactory, + final IWeavingService weavingService, final ICachingService cachingService, + final ModuleClassLoader classLoader) { + this.generation = generation; + this.factory = serviceFactory; + this.symbolicName = generation.getRevision().getSymbolicName(); + this.moduleLoader = classLoader; + if (Debug.DEBUG_GENERAL) + Debug.println("- WeavingAdaptor.WeavingAdaptor() bundle=" //$NON-NLS-1$ + + symbolicName); + } + + @Override + public CacheEntry findClass(final String name, final URL sourceFileURL) { + if (Debug.DEBUG_CACHE) + Debug.println("> WeavingAdaptor.findClass() bundle=" + symbolicName //$NON-NLS-1$ + + ", url=" + sourceFileURL + ", name=" + name); //$NON-NLS-1$ //$NON-NLS-2$ + CacheEntry cacheEntry = null; + + initialize(); + if (cachingService != null) { + cacheEntry = cachingService.findStoredClass("", sourceFileURL, name); //$NON-NLS-1$ + } + + if (Debug.DEBUG_CACHE) + Debug.println("< WeavingAdaptor.findClass() cacheEntry=" //$NON-NLS-1$ + + cacheEntry); + return cacheEntry; + } + + @Override + public void initialize() { + synchronized (this) { + if (initialized) + return; + + this.bundle = generation.getRevision().getBundle(); + if (!identifyRecursionSet.contains(this)) { + identifyRecursionSet.put(this); + + if (Debug.DEBUG_GENERAL) + Debug.println("> WeavingAdaptor.initialize() bundle=" //$NON-NLS-1$ + + symbolicName + ", moduleLoader=" + moduleLoader); //$NON-NLS-1$ + + if (symbolicName != null && symbolicName.startsWith("org.aspectj")) { //$NON-NLS-1$ + if (Debug.DEBUG_GENERAL) + Debug.println("- WeavingAdaptor.initialize() symbolicName=" //$NON-NLS-1$ + + symbolicName + ", moduleLoader=" //$NON-NLS-1$ + + moduleLoader); + } else if (moduleLoader != null) { + weavingService = factory.getWeavingService(moduleLoader); + cachingService = factory.getCachingService(moduleLoader, bundle, weavingService); + } else if ((generation.getRevision().getTypes() & BundleRevision.TYPE_FRAGMENT) != 0) { + + final Bundle host = factory.getHost(bundle); + if (Debug.DEBUG_GENERAL) + Debug.println("- WeavingAdaptor.initialize() symbolicName=" //$NON-NLS-1$ + + symbolicName + ", host=" + host); //$NON-NLS-1$ + + final Generation hostGeneration = (Generation) ((ModuleRevision) host.adapt(BundleRevision.class)) + .getRevisionInfo(); + final BundleFile bundleFile = hostGeneration.getBundleFile(); + if (bundleFile instanceof WeavingBundleFile) { + final WeavingBundleFile hostFile = (WeavingBundleFile) bundleFile; + final WeavingAdaptor hostAdaptor = (WeavingAdaptor) hostFile.getAdaptor(); + weavingService = hostAdaptor.weavingService; + cachingService = factory.getCachingService(hostAdaptor.moduleLoader, bundle, weavingService); + } + } else { + if (Debug.DEBUG_GENERAL) + Debug.println("W WeavingAdaptor.initialize() symbolicName=" //$NON-NLS-1$ + + symbolicName + ", baseLoader=" + moduleLoader); //$NON-NLS-1$ + } + initialized = true; + identifyRecursionSet.remove(this); + } + + if (Debug.DEBUG_GENERAL) + Debug.println("< WeavingAdaptor.initialize() weavingService=" //$NON-NLS-1$ + + (weavingService != null) + ", cachingService=" //$NON-NLS-1$ + + (cachingService != null)); + } + } + + @Override + public boolean isInitialized() { + return initialized; + } + + @Override + public boolean storeClass(final String name, final URL sourceFileURL, final Class clazz, + final byte[] classbytes) { + if (Debug.DEBUG_CACHE) + Debug.println("> WeavingAdaptor.storeClass() bundle=" //$NON-NLS-1$ + + symbolicName + ", url=" + sourceFileURL //$NON-NLS-1$ + + ", name=" //$NON-NLS-1$ + + name + ", clazz=" + clazz); //$NON-NLS-1$ + boolean stored = false; + + initialize(); + if (cachingService != null) { + // have we generated a closure? + if (weavingService != null && weavingService.generatedClassesExistFor(moduleLoader, name)) { + // If so we need to ask the cache if its capable of handling generated closures + if (cachingService.canCacheGeneratedClasses()) { + final Map generatedClasses = weavingService.getGeneratedClassesFor(name); + + stored = cachingService.storeClassAndGeneratedClasses("", //$NON-NLS-1$ + sourceFileURL, clazz, classbytes, generatedClasses); + } else { + weavingService.flushGeneratedClasses(moduleLoader); + if (Debug.DEBUG_CACHE) + Debug.println("- WeavingAdaptor.storeClass() generatedClassesExistFor=true"); //$NON-NLS-1$ + } + } else { + stored = cachingService.storeClass("", sourceFileURL, clazz, //$NON-NLS-1$ + classbytes); + if (!stored) { + if (Debug.DEBUG_CACHE) + Debug.println("E WeavingAdaptor.storeClass() bundle=" //$NON-NLS-1$ + + symbolicName + ", name=" + name); //$NON-NLS-1$ + } + } + } + if (Debug.DEBUG_CACHE) + Debug.println("< WeavingAdaptor.storeClass() stored=" + stored); //$NON-NLS-1$ + return stored; + } + + @Override + public String toString() { + return "WeavingAdaptor[" + symbolicName + "]"; //$NON-NLS-1$ //$NON-NLS-2$ + } + + @Override + public byte[] weaveClass(final String name, final byte[] bytes) { + if (Debug.DEBUG_WEAVE) + Debug.println("> WeavingAdaptor.weaveClass() bundle=" //$NON-NLS-1$ + + symbolicName + ", name=" + name + ", bytes=" //$NON-NLS-1$ //$NON-NLS-2$ + + bytes.length); + byte[] newBytes = null; + + initialize(); + if (/* shouldWeave(bytes) && */weavingService != null) { + try { + newBytes = weavingService.preProcess(name, bytes, moduleLoader); + } catch (final IOException ex) { + throw new ClassFormatError(ex.toString()); + } + } + + if (Debug.DEBUG_WEAVE) + Debug.println("< WeavingAdaptor.weaveClass() newBytes=" + newBytes); //$NON-NLS-1$ + return newBytes; + } } diff --git a/bundles/org.eclipse.equinox.weaving.hook/src/org/eclipse/equinox/weaving/adaptors/WeavingAdaptorFactory.java b/bundles/org.eclipse.equinox.weaving.hook/src/org/eclipse/equinox/weaving/adaptors/WeavingAdaptorFactory.java index 57b35d7f297..28dc000e1eb 100644 --- a/bundles/org.eclipse.equinox.weaving.hook/src/org/eclipse/equinox/weaving/adaptors/WeavingAdaptorFactory.java +++ b/bundles/org.eclipse.equinox.weaving.hook/src/org/eclipse/equinox/weaving/adaptors/WeavingAdaptorFactory.java @@ -45,226 +45,211 @@ public class WeavingAdaptorFactory { - private static final Collection IGNORE_WEAVING_SERVICE_BUNDLES = Arrays - .asList(new String[] { "org.eclipse.equinox.weaving.aspectj", //$NON-NLS-1$ - "org.eclipse.equinox.weaving.caching", //$NON-NLS-1$ - "org.eclipse.equinox.weaving.caching.j9", //$NON-NLS-1$ - "org.eclipse.equinox.simpleconfigurator", //$NON-NLS-1$ - "org.eclipse.equinox.common" }); //$NON-NLS-1$ - - private ServiceTracker cachingServiceFactoryTracker; - - private PackageAdmin packageAdminService; - - private StartLevel startLevelService; - - private ISupplementerRegistry supplementerRegistry; - - private ServiceTracker weavingServiceFactoryTracker; - - private ServiceListener weavingServiceListener; - - private final Map weavingServices = new ConcurrentHashMap<>(); - - public WeavingAdaptorFactory() { - } - - public void dispose(final BundleContext context) { - - context.removeServiceListener(weavingServiceListener); - if (Debug.DEBUG_WEAVE) - Debug.println("> Removed service listener for weaving service."); //$NON-NLS-1$ - - weavingServiceFactoryTracker.close(); - if (Debug.DEBUG_WEAVE) - Debug.println("> Closed service tracker for weaving service."); //$NON-NLS-1$ - - cachingServiceFactoryTracker.close(); - if (Debug.DEBUG_CACHE) - Debug.println("> Closed service tracker for caching service."); //$NON-NLS-1$ - } - - protected ICachingService getCachingService(final ModuleClassLoader loader, - final Bundle bundle, final IWeavingService weavingService) { - if (Debug.DEBUG_CACHE) - Debug.println("> WeavingAdaptorFactory.getCachingService() bundle=" //$NON-NLS-1$ - + bundle + ", weavingService=" + weavingService); //$NON-NLS-1$ - ICachingService service = null; - String key = ""; //$NON-NLS-1$ - - if (weavingService != null) { - key = weavingService.getKey(); - } - final ICachingServiceFactory cachingServiceFactory = cachingServiceFactoryTracker - .getService(); - if (cachingServiceFactory != null) { - service = cachingServiceFactory.createCachingService(loader, bundle, - key); - } - if (Debug.DEBUG_CACHE) - Debug.println("< WeavingAdaptorFactory.getCachingService() service=" //$NON-NLS-1$ - + service + ", key='" + key + "'"); //$NON-NLS-1$ //$NON-NLS-2$ - return service; - } - - public Bundle getHost(final Bundle fragment) { - if (Debug.DEBUG_GENERAL) Debug.println( - "> WeavingAdaptorFactory.getHost() fragment=" + fragment); //$NON-NLS-1$ - - Bundle host = null; - if (packageAdminService != null) - host = packageAdminService.getHosts(fragment)[0]; - - if (Debug.DEBUG_GENERAL) - Debug.println("< WeavingAdaptorFactory.getHost() " + host); //$NON-NLS-1$ - return host; - } - - protected IWeavingService getWeavingService( - final ModuleClassLoader loader) { - if (Debug.DEBUG_WEAVE) Debug.println( - "> WeavingAdaptorFactory.getWeavingService() baseClassLoader=" //$NON-NLS-1$ - + loader); - - final Generation generation = loader.getClasspathManager() - .getGeneration(); - final Bundle bundle = loader.getBundle(); - - IWeavingService weavingService = null; - if (!IGNORE_WEAVING_SERVICE_BUNDLES - .contains(bundle.getSymbolicName())) { - final IWeavingServiceFactory weavingServiceFactory = weavingServiceFactoryTracker - .getService(); - if (weavingServiceFactory != null) { - weavingService = weavingServiceFactory.createWeavingService( - loader, bundle, generation.getRevision(), - supplementerRegistry); - - if (weavingService != null) { - weavingServices.put(bundle, weavingService); - } - } - } - if (Debug.DEBUG_WEAVE) - Debug.println("< WeavingAdaptorFactory.getWeavingService() service=" //$NON-NLS-1$ - + weavingService); - return weavingService; - } - - public void initialize(final BundleContext context, - final ISupplementerRegistry supplementerRegistry) { - if (Debug.DEBUG_GENERAL) - Debug.println("> WeavingAdaptorFactory.initialize() context=" //$NON-NLS-1$ - + context); - this.supplementerRegistry = supplementerRegistry; - - initializePackageAdminService(context); - initializeStartLevelService(context); - - // Service tracker for weaving service - weavingServiceFactoryTracker = new ServiceTracker<>( - context, IWeavingServiceFactory.class, null); - weavingServiceFactoryTracker.open(); - if (Debug.DEBUG_WEAVE) - Debug.println("> Opened service tracker for weaving service."); //$NON-NLS-1$ - - // Service listener for weaving service - weavingServiceListener = new ServiceListener() { - - @Override - public void serviceChanged(final ServiceEvent event) { - if (event.getType() == ServiceEvent.REGISTERED) { - final List bundlesToRefresh = new ArrayList<>(); - - synchronized (weavingServices) { - final Iterator bundleEntries = weavingServices - .keySet().iterator(); - while (bundleEntries.hasNext()) { - final Bundle bundle = bundleEntries.next(); - bundleEntries.remove(); - bundlesToRefresh.add(bundle); - if (Debug.DEBUG_WEAVE) - Debug.println("> Updated bundle " //$NON-NLS-1$ - + bundle.getSymbolicName()); - } - } - - if (bundlesToRefresh.size() > 0) { - supplementerRegistry.refreshBundles(bundlesToRefresh - .toArray(new Bundle[bundlesToRefresh.size()])); - } - } - if (event.getType() == ServiceEvent.UNREGISTERING - && startLevelService != null - && startLevelService.getStartLevel() > 0) { - final List bundlesToRefresh = new ArrayList<>(); - - synchronized (weavingServices) { - final Iterator bundleEntries = weavingServices - .keySet().iterator(); - while (bundleEntries.hasNext()) { - final Bundle bundle = bundleEntries.next(); - bundleEntries.remove(); - bundlesToRefresh.add(bundle); - if (Debug.DEBUG_WEAVE) - Debug.println("> Updated bundle " //$NON-NLS-1$ - + bundle.getSymbolicName()); - } - } - if (bundlesToRefresh.size() > 0) { - supplementerRegistry.refreshBundles(bundlesToRefresh - .toArray(new Bundle[bundlesToRefresh.size()])); - } - } - } - }; - - // if (System.getProperty(WEAVING_SERVICE_DYNAMICS_PROPERTY, "false") - // .equals("true")) { - try { - context.addServiceListener(weavingServiceListener, "(" //$NON-NLS-1$ - + Constants.OBJECTCLASS + "=" //$NON-NLS-1$ - + IWeavingServiceFactory.class.getName() + ")"); //$NON-NLS-1$ - } catch (final InvalidSyntaxException e) { // This is correct! - } - - // Service tracker for caching service - cachingServiceFactoryTracker = new ServiceTracker<>( - context, ICachingServiceFactory.class, null); - cachingServiceFactoryTracker.open(); - if (Debug.DEBUG_CACHE) - Debug.println("> Opened service tracker for caching service."); //$NON-NLS-1$ - } - - private void initializePackageAdminService(final BundleContext context) { - if (Debug.DEBUG_GENERAL) Debug.println( - "> AdaptorFactory.initializePackageAdminService() context=" //$NON-NLS-1$ - + context); - - final ServiceReference ref = context - .getServiceReference(PackageAdmin.class); - if (ref != null) { - packageAdminService = context.getService(ref); - } - - if (Debug.DEBUG_GENERAL) - Debug.println("< AdaptorFactory.initializePackageAdminService() " //$NON-NLS-1$ - + packageAdminService); - } - - private void initializeStartLevelService(final BundleContext context) { - if (Debug.DEBUG_GENERAL) Debug.println( - "> AdaptorFactory.initializeStartLevelService() context=" //$NON-NLS-1$ - + context); - - final ServiceReference ref = context - .getServiceReference(StartLevel.class); - if (ref != null) { - startLevelService = context.getService(ref); - } - - if (Debug.DEBUG_GENERAL) - Debug.println("< AdaptorFactory.initializeStartLevelService() " //$NON-NLS-1$ - + startLevelService); - } + private static final Collection IGNORE_WEAVING_SERVICE_BUNDLES = Arrays + .asList(new String[] { "org.eclipse.equinox.weaving.aspectj", //$NON-NLS-1$ + "org.eclipse.equinox.weaving.caching", //$NON-NLS-1$ + "org.eclipse.equinox.weaving.caching.j9", //$NON-NLS-1$ + "org.eclipse.equinox.simpleconfigurator", //$NON-NLS-1$ + "org.eclipse.equinox.common" }); //$NON-NLS-1$ + + private ServiceTracker cachingServiceFactoryTracker; + + private PackageAdmin packageAdminService; + + private StartLevel startLevelService; + + private ISupplementerRegistry supplementerRegistry; + + private ServiceTracker weavingServiceFactoryTracker; + + private ServiceListener weavingServiceListener; + + private final Map weavingServices = new ConcurrentHashMap<>(); + + public WeavingAdaptorFactory() { + } + + public void dispose(final BundleContext context) { + + context.removeServiceListener(weavingServiceListener); + if (Debug.DEBUG_WEAVE) + Debug.println("> Removed service listener for weaving service."); //$NON-NLS-1$ + + weavingServiceFactoryTracker.close(); + if (Debug.DEBUG_WEAVE) + Debug.println("> Closed service tracker for weaving service."); //$NON-NLS-1$ + + cachingServiceFactoryTracker.close(); + if (Debug.DEBUG_CACHE) + Debug.println("> Closed service tracker for caching service."); //$NON-NLS-1$ + } + + protected ICachingService getCachingService(final ModuleClassLoader loader, final Bundle bundle, + final IWeavingService weavingService) { + if (Debug.DEBUG_CACHE) + Debug.println("> WeavingAdaptorFactory.getCachingService() bundle=" //$NON-NLS-1$ + + bundle + ", weavingService=" + weavingService); //$NON-NLS-1$ + ICachingService service = null; + String key = ""; //$NON-NLS-1$ + + if (weavingService != null) { + key = weavingService.getKey(); + } + final ICachingServiceFactory cachingServiceFactory = cachingServiceFactoryTracker.getService(); + if (cachingServiceFactory != null) { + service = cachingServiceFactory.createCachingService(loader, bundle, key); + } + if (Debug.DEBUG_CACHE) + Debug.println("< WeavingAdaptorFactory.getCachingService() service=" //$NON-NLS-1$ + + service + ", key='" + key + "'"); //$NON-NLS-1$ //$NON-NLS-2$ + return service; + } + + public Bundle getHost(final Bundle fragment) { + if (Debug.DEBUG_GENERAL) + Debug.println("> WeavingAdaptorFactory.getHost() fragment=" + fragment); //$NON-NLS-1$ + + Bundle host = null; + if (packageAdminService != null) + host = packageAdminService.getHosts(fragment)[0]; + + if (Debug.DEBUG_GENERAL) + Debug.println("< WeavingAdaptorFactory.getHost() " + host); //$NON-NLS-1$ + return host; + } + + protected IWeavingService getWeavingService(final ModuleClassLoader loader) { + if (Debug.DEBUG_WEAVE) + Debug.println("> WeavingAdaptorFactory.getWeavingService() baseClassLoader=" //$NON-NLS-1$ + + loader); + + final Generation generation = loader.getClasspathManager().getGeneration(); + final Bundle bundle = loader.getBundle(); + + IWeavingService weavingService = null; + if (!IGNORE_WEAVING_SERVICE_BUNDLES.contains(bundle.getSymbolicName())) { + final IWeavingServiceFactory weavingServiceFactory = weavingServiceFactoryTracker.getService(); + if (weavingServiceFactory != null) { + weavingService = weavingServiceFactory.createWeavingService(loader, bundle, generation.getRevision(), + supplementerRegistry); + + if (weavingService != null) { + weavingServices.put(bundle, weavingService); + } + } + } + if (Debug.DEBUG_WEAVE) + Debug.println("< WeavingAdaptorFactory.getWeavingService() service=" //$NON-NLS-1$ + + weavingService); + return weavingService; + } + + public void initialize(final BundleContext context, final ISupplementerRegistry supplementerRegistry) { + if (Debug.DEBUG_GENERAL) + Debug.println("> WeavingAdaptorFactory.initialize() context=" //$NON-NLS-1$ + + context); + this.supplementerRegistry = supplementerRegistry; + + initializePackageAdminService(context); + initializeStartLevelService(context); + + // Service tracker for weaving service + weavingServiceFactoryTracker = new ServiceTracker<>(context, IWeavingServiceFactory.class, null); + weavingServiceFactoryTracker.open(); + if (Debug.DEBUG_WEAVE) + Debug.println("> Opened service tracker for weaving service."); //$NON-NLS-1$ + + // Service listener for weaving service + weavingServiceListener = new ServiceListener() { + + @Override + public void serviceChanged(final ServiceEvent event) { + if (event.getType() == ServiceEvent.REGISTERED) { + final List bundlesToRefresh = new ArrayList<>(); + + synchronized (weavingServices) { + final Iterator bundleEntries = weavingServices.keySet().iterator(); + while (bundleEntries.hasNext()) { + final Bundle bundle = bundleEntries.next(); + bundleEntries.remove(); + bundlesToRefresh.add(bundle); + if (Debug.DEBUG_WEAVE) + Debug.println("> Updated bundle " //$NON-NLS-1$ + + bundle.getSymbolicName()); + } + } + + if (bundlesToRefresh.size() > 0) { + supplementerRegistry + .refreshBundles(bundlesToRefresh.toArray(new Bundle[bundlesToRefresh.size()])); + } + } + if (event.getType() == ServiceEvent.UNREGISTERING && startLevelService != null + && startLevelService.getStartLevel() > 0) { + final List bundlesToRefresh = new ArrayList<>(); + + synchronized (weavingServices) { + final Iterator bundleEntries = weavingServices.keySet().iterator(); + while (bundleEntries.hasNext()) { + final Bundle bundle = bundleEntries.next(); + bundleEntries.remove(); + bundlesToRefresh.add(bundle); + if (Debug.DEBUG_WEAVE) + Debug.println("> Updated bundle " //$NON-NLS-1$ + + bundle.getSymbolicName()); + } + } + if (bundlesToRefresh.size() > 0) { + supplementerRegistry + .refreshBundles(bundlesToRefresh.toArray(new Bundle[bundlesToRefresh.size()])); + } + } + } + }; + + // if (System.getProperty(WEAVING_SERVICE_DYNAMICS_PROPERTY, "false") + // .equals("true")) { + try { + context.addServiceListener(weavingServiceListener, "(" //$NON-NLS-1$ + + Constants.OBJECTCLASS + "=" //$NON-NLS-1$ + + IWeavingServiceFactory.class.getName() + ")"); //$NON-NLS-1$ + } catch (final InvalidSyntaxException e) { // This is correct! + } + + // Service tracker for caching service + cachingServiceFactoryTracker = new ServiceTracker<>(context, ICachingServiceFactory.class, null); + cachingServiceFactoryTracker.open(); + if (Debug.DEBUG_CACHE) + Debug.println("> Opened service tracker for caching service."); //$NON-NLS-1$ + } + + private void initializePackageAdminService(final BundleContext context) { + if (Debug.DEBUG_GENERAL) + Debug.println("> AdaptorFactory.initializePackageAdminService() context=" //$NON-NLS-1$ + + context); + + final ServiceReference ref = context.getServiceReference(PackageAdmin.class); + if (ref != null) { + packageAdminService = context.getService(ref); + } + + if (Debug.DEBUG_GENERAL) + Debug.println("< AdaptorFactory.initializePackageAdminService() " //$NON-NLS-1$ + + packageAdminService); + } + + private void initializeStartLevelService(final BundleContext context) { + if (Debug.DEBUG_GENERAL) + Debug.println("> AdaptorFactory.initializeStartLevelService() context=" //$NON-NLS-1$ + + context); + + final ServiceReference ref = context.getServiceReference(StartLevel.class); + if (ref != null) { + startLevelService = context.getService(ref); + } + + if (Debug.DEBUG_GENERAL) + Debug.println("< AdaptorFactory.initializeStartLevelService() " //$NON-NLS-1$ + + startLevelService); + } } diff --git a/bundles/org.eclipse.equinox.weaving.hook/src/org/eclipse/equinox/weaving/hooks/AbstractWeavingBundleFile.java b/bundles/org.eclipse.equinox.weaving.hook/src/org/eclipse/equinox/weaving/hooks/AbstractWeavingBundleFile.java index 48a4fe19474..bd5026060e0 100644 --- a/bundles/org.eclipse.equinox.weaving.hook/src/org/eclipse/equinox/weaving/hooks/AbstractWeavingBundleFile.java +++ b/bundles/org.eclipse.equinox.weaving.hook/src/org/eclipse/equinox/weaving/hooks/AbstractWeavingBundleFile.java @@ -22,22 +22,20 @@ public abstract class AbstractWeavingBundleFile extends BundleFileWrapper { - private final BundleAdaptorProvider adaptorProvider; + private final BundleAdaptorProvider adaptorProvider; - protected BundleFile delegate; + protected BundleFile delegate; - public AbstractWeavingBundleFile( - final BundleAdaptorProvider adaptorProvider, - final BundleFile bundleFile) { - super(bundleFile); - this.adaptorProvider = adaptorProvider; - this.delegate = bundleFile; - } + public AbstractWeavingBundleFile(final BundleAdaptorProvider adaptorProvider, final BundleFile bundleFile) { + super(bundleFile); + this.adaptorProvider = adaptorProvider; + this.delegate = bundleFile; + } - /** - * @return - */ - public IWeavingAdaptor getAdaptor() { - return this.adaptorProvider.getAdaptor(); - } + /** + * @return + */ + public IWeavingAdaptor getAdaptor() { + return this.adaptorProvider.getAdaptor(); + } } diff --git a/bundles/org.eclipse.equinox.weaving.hook/src/org/eclipse/equinox/weaving/hooks/AbstractWeavingHook.java b/bundles/org.eclipse.equinox.weaving.hook/src/org/eclipse/equinox/weaving/hooks/AbstractWeavingHook.java index a3589dea86f..24a279302a8 100644 --- a/bundles/org.eclipse.equinox.weaving.hook/src/org/eclipse/equinox/weaving/hooks/AbstractWeavingHook.java +++ b/bundles/org.eclipse.equinox.weaving.hook/src/org/eclipse/equinox/weaving/hooks/AbstractWeavingHook.java @@ -31,51 +31,47 @@ * @author Matthew Webster * @author Martin Lippert */ -public abstract class AbstractWeavingHook extends ClassLoaderHook implements - HookConfigurator, BundleFileWrapperFactoryHook, IAdaptorProvider, - ActivatorHookFactory, BundleActivator { +public abstract class AbstractWeavingHook extends ClassLoaderHook implements HookConfigurator, + BundleFileWrapperFactoryHook, IAdaptorProvider, ActivatorHookFactory, BundleActivator { - /** - * flag to indicate whether to print out detailed information or not - */ - public static boolean verbose = Boolean - .getBoolean("org.aspectj.osgi.verbose"); //$NON-NLS-1$ + /** + * flag to indicate whether to print out detailed information or not + */ + public static boolean verbose = Boolean.getBoolean("org.aspectj.osgi.verbose"); //$NON-NLS-1$ - private ISupplementerRegistry supplementerRegistry; + private ISupplementerRegistry supplementerRegistry; - /** - * @see org.eclipse.osgi.baseadaptor.HookConfigurator#addHooks(org.eclipse.osgi.baseadaptor.HookRegistry) - */ - public void addHooks(final HookRegistry hooks) { - if (verbose) - System.err - .println("[org.eclipse.equinox.weaving.hook] info adding AspectJ hooks ..."); //$NON-NLS-1$ + /** + * @see org.eclipse.osgi.baseadaptor.HookConfigurator#addHooks(org.eclipse.osgi.baseadaptor.HookRegistry) + */ + public void addHooks(final HookRegistry hooks) { + if (verbose) + System.err.println("[org.eclipse.equinox.weaving.hook] info adding AspectJ hooks ..."); //$NON-NLS-1$ - Debug.init(hooks.getConfiguration().getDebugOptions()); - supplementerRegistry = new SupplementerRegistry(this); + Debug.init(hooks.getConfiguration().getDebugOptions()); + supplementerRegistry = new SupplementerRegistry(this); - hooks.addClassLoaderHook(this); - hooks.addBundleFileWrapperFactoryHook(this); - hooks.addActivatorHookFactory(this); - hooks.addClassLoaderHook(new WeavingLoaderDelegateHook( - supplementerRegistry)); - } + hooks.addClassLoaderHook(this); + hooks.addBundleFileWrapperFactoryHook(this); + hooks.addActivatorHookFactory(this); + hooks.addClassLoaderHook(new WeavingLoaderDelegateHook(supplementerRegistry)); + } - /** - * @see org.eclipse.osgi.internal.hookregistry.ActivatorHookFactory#createActivator() - */ - public BundleActivator createActivator() { - return this; - } + /** + * @see org.eclipse.osgi.internal.hookregistry.ActivatorHookFactory#createActivator() + */ + public BundleActivator createActivator() { + return this; + } - /** - * Their is only one registry for dealing with supplementers available via - * this accessor method. - * - * @return The supplementer registry, guaranteed to be not null - */ - public ISupplementerRegistry getSupplementerRegistry() { - return this.supplementerRegistry; - } + /** + * Their is only one registry for dealing with supplementers available via this + * accessor method. + * + * @return The supplementer registry, guaranteed to be not null + */ + public ISupplementerRegistry getSupplementerRegistry() { + return this.supplementerRegistry; + } } diff --git a/bundles/org.eclipse.equinox.weaving.hook/src/org/eclipse/equinox/weaving/hooks/BaseWeavingBundleFile.java b/bundles/org.eclipse.equinox.weaving.hook/src/org/eclipse/equinox/weaving/hooks/BaseWeavingBundleFile.java index 56d69cf46f8..21670dded1f 100644 --- a/bundles/org.eclipse.equinox.weaving.hook/src/org/eclipse/equinox/weaving/hooks/BaseWeavingBundleFile.java +++ b/bundles/org.eclipse.equinox.weaving.hook/src/org/eclipse/equinox/weaving/hooks/BaseWeavingBundleFile.java @@ -19,8 +19,7 @@ public class BaseWeavingBundleFile extends WeavingBundleFile { - public BaseWeavingBundleFile(final BundleAdaptorProvider adaptorProvider, - final BundleFile bundleFile) { - super(adaptorProvider, bundleFile); - } + public BaseWeavingBundleFile(final BundleAdaptorProvider adaptorProvider, final BundleFile bundleFile) { + super(adaptorProvider, bundleFile); + } } diff --git a/bundles/org.eclipse.equinox.weaving.hook/src/org/eclipse/equinox/weaving/hooks/BundleAdaptorProvider.java b/bundles/org.eclipse.equinox.weaving.hook/src/org/eclipse/equinox/weaving/hooks/BundleAdaptorProvider.java index dfcc883d8d6..3ba12d27258 100644 --- a/bundles/org.eclipse.equinox.weaving.hook/src/org/eclipse/equinox/weaving/hooks/BundleAdaptorProvider.java +++ b/bundles/org.eclipse.equinox.weaving.hook/src/org/eclipse/equinox/weaving/hooks/BundleAdaptorProvider.java @@ -20,25 +20,22 @@ public class BundleAdaptorProvider { - private final IAdaptorProvider adaptorProvider; - - private final Generation generation; - - public BundleAdaptorProvider(final Generation generation, - final IAdaptorProvider adaptorProvider) { - this.generation = generation; - this.adaptorProvider = adaptorProvider; - } - - public IWeavingAdaptor getAdaptor() { - - if ((generation.getRevision().getTypes() & BundleRevision.TYPE_FRAGMENT) != 0) { - return this.adaptorProvider.getHostBundleAdaptor(this.generation - .getBundleInfo().getBundleId()); - } else { - return this.adaptorProvider.getAdaptor(this.generation - .getBundleInfo().getBundleId()); - } - } + private final IAdaptorProvider adaptorProvider; + + private final Generation generation; + + public BundleAdaptorProvider(final Generation generation, final IAdaptorProvider adaptorProvider) { + this.generation = generation; + this.adaptorProvider = adaptorProvider; + } + + public IWeavingAdaptor getAdaptor() { + + if ((generation.getRevision().getTypes() & BundleRevision.TYPE_FRAGMENT) != 0) { + return this.adaptorProvider.getHostBundleAdaptor(this.generation.getBundleInfo().getBundleId()); + } else { + return this.adaptorProvider.getAdaptor(this.generation.getBundleInfo().getBundleId()); + } + } } diff --git a/bundles/org.eclipse.equinox.weaving.hook/src/org/eclipse/equinox/weaving/hooks/CachedClassBundleEntry.java b/bundles/org.eclipse.equinox.weaving.hook/src/org/eclipse/equinox/weaving/hooks/CachedClassBundleEntry.java index b0c991efd4b..75881af80a9 100644 --- a/bundles/org.eclipse.equinox.weaving.hook/src/org/eclipse/equinox/weaving/hooks/CachedClassBundleEntry.java +++ b/bundles/org.eclipse.equinox.weaving.hook/src/org/eclipse/equinox/weaving/hooks/CachedClassBundleEntry.java @@ -23,74 +23,73 @@ public class CachedClassBundleEntry extends BundleEntry { - private final IWeavingAdaptor adaptor; - - private final URL bundleFileURL; - - private final byte[] bytes; - - private final BundleEntry delegate; - - private final String name; - - public CachedClassBundleEntry(final IWeavingAdaptor aspectjAdaptor, - final BundleEntry delegate, final String name, final byte[] bytes, - final URL url) { - this.adaptor = aspectjAdaptor; - this.bundleFileURL = url; - this.delegate = delegate; - this.name = name; - this.bytes = bytes; - } - - public boolean dontWeave() { - return true; - } - - public IWeavingAdaptor getAdaptor() { - return adaptor; - } - - public URL getBundleFileURL() { - return bundleFileURL; - } - - @Override - public byte[] getBytes() throws IOException { - return bytes; - } - - @Override - public URL getFileURL() { - return null; - } - - @Override - public InputStream getInputStream() throws IOException { - if (delegate == null) { - System.err.println("error in: " + name); - } - return delegate.getInputStream(); - } - - @Override - public URL getLocalURL() { - return null; - } - - @Override - public String getName() { - return name; - } - - @Override - public long getSize() { - return bytes.length; - } - - @Override - public long getTime() { - return 0; - } + private final IWeavingAdaptor adaptor; + + private final URL bundleFileURL; + + private final byte[] bytes; + + private final BundleEntry delegate; + + private final String name; + + public CachedClassBundleEntry(final IWeavingAdaptor aspectjAdaptor, final BundleEntry delegate, final String name, + final byte[] bytes, final URL url) { + this.adaptor = aspectjAdaptor; + this.bundleFileURL = url; + this.delegate = delegate; + this.name = name; + this.bytes = bytes; + } + + public boolean dontWeave() { + return true; + } + + public IWeavingAdaptor getAdaptor() { + return adaptor; + } + + public URL getBundleFileURL() { + return bundleFileURL; + } + + @Override + public byte[] getBytes() throws IOException { + return bytes; + } + + @Override + public URL getFileURL() { + return null; + } + + @Override + public InputStream getInputStream() throws IOException { + if (delegate == null) { + System.err.println("error in: " + name); + } + return delegate.getInputStream(); + } + + @Override + public URL getLocalURL() { + return null; + } + + @Override + public String getName() { + return name; + } + + @Override + public long getSize() { + return bytes.length; + } + + @Override + public long getTime() { + return 0; + } } diff --git a/bundles/org.eclipse.equinox.weaving.hook/src/org/eclipse/equinox/weaving/hooks/CachedGeneratedClassBundleEntry.java b/bundles/org.eclipse.equinox.weaving.hook/src/org/eclipse/equinox/weaving/hooks/CachedGeneratedClassBundleEntry.java index c987e85a063..496d5748ef2 100644 --- a/bundles/org.eclipse.equinox.weaving.hook/src/org/eclipse/equinox/weaving/hooks/CachedGeneratedClassBundleEntry.java +++ b/bundles/org.eclipse.equinox.weaving.hook/src/org/eclipse/equinox/weaving/hooks/CachedGeneratedClassBundleEntry.java @@ -24,68 +24,68 @@ public class CachedGeneratedClassBundleEntry extends BundleEntry { - private final IWeavingAdaptor adaptor; - - private final URL bundleFileURL; - - private final byte[] bytes; - - private final String name; - - public CachedGeneratedClassBundleEntry(final IWeavingAdaptor adaptor, - final String path, final byte[] cachedBytes, final URL url) { - this.adaptor = adaptor; - this.bundleFileURL = url; - this.bytes = cachedBytes; - this.name = path; - } - - public boolean dontWeave() { - return true; - } - - public IWeavingAdaptor getAdaptor() { - return adaptor; - } - - public URL getBundleFileURL() { - return bundleFileURL; - } - - @Override - public byte[] getBytes() throws IOException { - return bytes; - } - - @Override - public URL getFileURL() { - return null; - } - - @Override - public InputStream getInputStream() throws IOException { - final ByteArrayInputStream result = new ByteArrayInputStream(bytes); - return result; - } - - @Override - public URL getLocalURL() { - return null; - } - - @Override - public String getName() { - return name; - } - - @Override - public long getSize() { - return bytes.length; - } - - @Override - public long getTime() { - return 0; - } + private final IWeavingAdaptor adaptor; + + private final URL bundleFileURL; + + private final byte[] bytes; + + private final String name; + + public CachedGeneratedClassBundleEntry(final IWeavingAdaptor adaptor, final String path, final byte[] cachedBytes, + final URL url) { + this.adaptor = adaptor; + this.bundleFileURL = url; + this.bytes = cachedBytes; + this.name = path; + } + + public boolean dontWeave() { + return true; + } + + public IWeavingAdaptor getAdaptor() { + return adaptor; + } + + public URL getBundleFileURL() { + return bundleFileURL; + } + + @Override + public byte[] getBytes() throws IOException { + return bytes; + } + + @Override + public URL getFileURL() { + return null; + } + + @Override + public InputStream getInputStream() throws IOException { + final ByteArrayInputStream result = new ByteArrayInputStream(bytes); + return result; + } + + @Override + public URL getLocalURL() { + return null; + } + + @Override + public String getName() { + return name; + } + + @Override + public long getSize() { + return bytes.length; + } + + @Override + public long getTime() { + return 0; + } } diff --git a/bundles/org.eclipse.equinox.weaving.hook/src/org/eclipse/equinox/weaving/hooks/IAdaptorProvider.java b/bundles/org.eclipse.equinox.weaving.hook/src/org/eclipse/equinox/weaving/hooks/IAdaptorProvider.java index a44d0a29ca5..47e3351bd17 100644 --- a/bundles/org.eclipse.equinox.weaving.hook/src/org/eclipse/equinox/weaving/hooks/IAdaptorProvider.java +++ b/bundles/org.eclipse.equinox.weaving.hook/src/org/eclipse/equinox/weaving/hooks/IAdaptorProvider.java @@ -18,10 +18,10 @@ public interface IAdaptorProvider { - public IWeavingAdaptor getAdaptor(long bundleID); + public IWeavingAdaptor getAdaptor(long bundleID); - public IWeavingAdaptor getHostBundleAdaptor(long bundleID); + public IWeavingAdaptor getHostBundleAdaptor(long bundleID); - public void resetAdaptor(long bundleID); + public void resetAdaptor(long bundleID); } diff --git a/bundles/org.eclipse.equinox.weaving.hook/src/org/eclipse/equinox/weaving/hooks/SupplementBundleListener.java b/bundles/org.eclipse.equinox.weaving.hook/src/org/eclipse/equinox/weaving/hooks/SupplementBundleListener.java index ddf4a927d1d..ddc344283d7 100644 --- a/bundles/org.eclipse.equinox.weaving.hook/src/org/eclipse/equinox/weaving/hooks/SupplementBundleListener.java +++ b/bundles/org.eclipse.equinox.weaving.hook/src/org/eclipse/equinox/weaving/hooks/SupplementBundleListener.java @@ -21,20 +21,19 @@ public class SupplementBundleListener implements SynchronousBundleListener { - private final ISupplementerRegistry supplementerRegistry; + private final ISupplementerRegistry supplementerRegistry; - public SupplementBundleListener( - final ISupplementerRegistry supplementerRegistry) { - this.supplementerRegistry = supplementerRegistry; - } + public SupplementBundleListener(final ISupplementerRegistry supplementerRegistry) { + this.supplementerRegistry = supplementerRegistry; + } - public void bundleChanged(final BundleEvent event) { - final Bundle bundle = event.getBundle(); - if (event.getType() == BundleEvent.RESOLVED) { - supplementerRegistry.addBundle(bundle); - } else if (event.getType() == BundleEvent.UNRESOLVED) { - supplementerRegistry.removeBundle(bundle); - } - } + public void bundleChanged(final BundleEvent event) { + final Bundle bundle = event.getBundle(); + if (event.getType() == BundleEvent.RESOLVED) { + supplementerRegistry.addBundle(bundle); + } else if (event.getType() == BundleEvent.UNRESOLVED) { + supplementerRegistry.removeBundle(bundle); + } + } } diff --git a/bundles/org.eclipse.equinox.weaving.hook/src/org/eclipse/equinox/weaving/hooks/SupplementerRegistry.java b/bundles/org.eclipse.equinox.weaving.hook/src/org/eclipse/equinox/weaving/hooks/SupplementerRegistry.java index 5718e3e4279..dbc80ac4205 100644 --- a/bundles/org.eclipse.equinox.weaving.hook/src/org/eclipse/equinox/weaving/hooks/SupplementerRegistry.java +++ b/bundles/org.eclipse.equinox.weaving.hook/src/org/eclipse/equinox/weaving/hooks/SupplementerRegistry.java @@ -46,385 +46,351 @@ */ public class SupplementerRegistry implements ISupplementerRegistry { - /** - * Manifest header (named "Supplement-Bundle") identifying the - * names (and optionally, version numbers) of any bundles supplemented by - * this bundle. All supplemented bundles will have all the exported packages - * of this bundle added to their imports list - * - *

- * The attribute value may be retrieved from the Dictionary - * object returned by the Bundle.getHeaders method. - */ - public static final String SUPPLEMENT_BUNDLE = "Eclipse-SupplementBundle"; //$NON-NLS-1$ - - /** - * Manifest header (named "Supplement-Exporter") identifying the - * names (and optionally, version numbers) of the packages that the bundle - * supplements. All exporters of one of these packages will have the - * exported packages of this bundle added to their imports list. - * - *

- * The attribute value may be retrieved from the Dictionary - * object returned by the Bundle.getHeaders method. - */ - public static final String SUPPLEMENT_EXPORTER = "Eclipse-SupplementExporter"; //$NON-NLS-1$ - - /** - * Manifest header (named "Supplement-Importer") identifying the - * names (and optionally, version numbers) of the packages that the bundle - * supplements. All importers of one of these packages will have the - * exported packages of this bundle added to their imports in addition. - * - *

- * The attribute value may be retrieved from the Dictionary - * object returned by the Bundle.getHeaders method. - */ - public static final String SUPPLEMENT_IMPORTER = "Eclipse-SupplementImporter"; //$NON-NLS-1$ - - private final IAdaptorProvider adaptorProvider; - - private BundleContext context; - - private final Set dontWeaveTheseBundles; // elements of type String (symbolic name of bundle) - - private PackageAdmin packageAdmin; - - private final Map supplementers; // keys of type String (symbolic name of supplementer bundle) - - private final Map supplementersByBundle; - - public SupplementerRegistry(final IAdaptorProvider adaptorProvider) { - this.adaptorProvider = adaptorProvider; - - this.supplementers = new HashMap<>(); - this.supplementersByBundle = new HashMap<>(); - this.dontWeaveTheseBundles = new HashSet<>(); - - this.dontWeaveTheseBundles.add("org.eclipse.osgi"); - this.dontWeaveTheseBundles.add("org.eclipse.core.runtime"); - this.dontWeaveTheseBundles.add("org.eclipse.equinox.common"); - this.dontWeaveTheseBundles.add("org.eclipse.equinox.weaving.hook"); - this.dontWeaveTheseBundles.add("org.eclipse.equinox.weaving.aspectj"); - this.dontWeaveTheseBundles.add("org.eclipse.equinox.weaving.caching"); - this.dontWeaveTheseBundles - .add("org.eclipse.equinox.weaving.caching.j9"); - this.dontWeaveTheseBundles.add("org.aspectj.runtime"); - this.dontWeaveTheseBundles.add("org.aspectj.weaver"); - this.dontWeaveTheseBundles - .add("org.eclipse.equinox.simpleconfigurator"); - } - - /** - * @see org.eclipse.equinox.service.weaving.ISupplementerRegistry#addBundle(org.osgi.framework.Bundle) - */ - @Override - public void addBundle(final Bundle bundle) { - // First analyze which supplementers already exists for this bundle - addSupplementedBundle(bundle); - - // Second analyze if this bundle itself is a supplementer - addSupplementer(bundle, true); - } - - /** - * @see org.eclipse.equinox.service.weaving.ISupplementerRegistry#addSupplementedBundle(org.osgi.framework.Bundle) - */ - @Override - public void addSupplementedBundle(final Bundle bundle) { - try { - final Dictionary manifest = bundle.getHeaders(""); //$NON-NLS-1$ - final ManifestElement[] imports = ManifestElement.parseHeader( - Constants.IMPORT_PACKAGE, - (String) manifest.get(Constants.IMPORT_PACKAGE)); - final ManifestElement[] exports = ManifestElement.parseHeader( - Constants.EXPORT_PACKAGE, - (String) manifest.get(Constants.EXPORT_PACKAGE)); - final List supplementers = getMatchingSupplementers( - bundle.getSymbolicName(), imports, exports); - if (supplementers.size() > 0) { - this.addSupplementedBundle(bundle, supplementers); - } - - this.supplementersByBundle.put(bundle.getBundleId(), supplementers - .toArray(new Supplementer[supplementers.size()])); - } catch (final BundleException e) { - } - } - - private void addSupplementedBundle(final Bundle supplementedBundle, - final List supplementers) { - for (final Supplementer supplementer : supplementers) { - supplementer.addSupplementedBundle(supplementedBundle); - } - } - - /** - * @see org.eclipse.equinox.service.weaving.ISupplementerRegistry#addSupplementer(org.osgi.framework.Bundle, - * boolean) - */ - @Override - public void addSupplementer(final Bundle bundle, - final boolean updateBundles) { - try { - final Dictionary manifest = bundle.getHeaders(""); //$NON-NLS-1$ - final ManifestElement[] supplementBundle = ManifestElement - .parseHeader(SUPPLEMENT_BUNDLE, - (String) manifest.get(SUPPLEMENT_BUNDLE)); - final ManifestElement[] supplementImporter = ManifestElement - .parseHeader(SUPPLEMENT_IMPORTER, - (String) manifest.get(SUPPLEMENT_IMPORTER)); - final ManifestElement[] supplementExporter = ManifestElement - .parseHeader(SUPPLEMENT_EXPORTER, - (String) manifest.get(SUPPLEMENT_EXPORTER)); - - if (supplementBundle != null || supplementImporter != null - || supplementExporter != null) { - - final Bundle[] hosts = this.packageAdmin.getHosts(bundle); - final Bundle host = hosts != null && hosts.length == 1 - ? hosts[0] - : null; - - final Supplementer newSupplementer = new Supplementer(bundle, - host, supplementBundle, supplementImporter, - supplementExporter); - - this.supplementers.put(bundle.getSymbolicName(), - newSupplementer); - if (updateBundles) { - resupplementInstalledBundles(newSupplementer); - } - } - } catch (final BundleException e) { - } - } - - /** - * @see org.eclipse.equinox.service.weaving.ISupplementerRegistry#getMatchingSupplementers(java.lang.String, - * org.eclipse.osgi.util.ManifestElement[], - * org.eclipse.osgi.util.ManifestElement[]) - */ - @Override - public List getMatchingSupplementers( - final String symbolicName, final ManifestElement[] imports, - final ManifestElement[] exports) { - List result = Collections.emptyList(); - - if (supplementers.size() > 0 - && !this.dontWeaveTheseBundles.contains(symbolicName)) { - result = new LinkedList<>(); - for (Supplementer supplementer : supplementers.values()) { - if (isSupplementerMatching(symbolicName, imports, exports, - supplementer)) { - result.add(supplementer); - } - } - } - - return result; - } - - /** - * @see org.eclipse.equinox.service.weaving.ISupplementerRegistry#getPackageAdmin() - */ - @Override - public PackageAdmin getPackageAdmin() { - return packageAdmin; - } - - /** - * @see org.eclipse.equinox.service.weaving.ISupplementerRegistry#getSupplementers(org.osgi.framework.Bundle) - */ - @Override - public Supplementer[] getSupplementers(final Bundle bundle) { - return getSupplementers(bundle.getBundleId()); - } - - /** - * @see org.eclipse.equinox.service.weaving.ISupplementerRegistry#getSupplementers(long) - */ - @Override - public Supplementer[] getSupplementers(final long bundleID) { - if (supplementersByBundle.containsKey(bundleID)) { - return supplementersByBundle.get(bundleID); - } else { - return new Supplementer[0]; - } - } - - private boolean isSupplementerMatching(final String symbolicName, - final ManifestElement[] imports, final ManifestElement[] exports, - final Supplementer supplementer) { - final String supplementerName = supplementer.getSymbolicName(); - if (!supplementerName.equals(symbolicName)) { - if (supplementer.matchSupplementer(symbolicName) - || (imports != null - && supplementer.matchesSupplementImporter(imports)) - || (exports != null && supplementer - .matchesSupplementExporter(exports))) { - return true; - } - } - return false; - } - - /** - * Refreshes the given bundles - * - * @param bundles The bundles to refresh - */ - @Override - public void refreshBundles(final Bundle[] bundles) { - // if (this.packageAdmin != null) { - // if (AbstractWeavingHook.verbose) { - // for (int i = 0; i < bundles.length; i++) { - // System.out.println("refresh bundle: " - // + bundles[i].getSymbolicName()); - // } - // } - // - // this.packageAdmin.refreshPackages(bundles); - // } - } - - /** - * @see org.eclipse.equinox.service.weaving.ISupplementerRegistry#removeBundle(org.osgi.framework.Bundle) - */ - @Override - public void removeBundle(final Bundle bundle) { - // if this bundle is itself supplemented by others, remove the bundle from those lists - removeSupplementedBundle(bundle); - this.supplementersByBundle.remove(bundle.getBundleId()); - - this.adaptorProvider.resetAdaptor(bundle.getBundleId()); - - // if this bundle supplements other bundles, remove this supplementer and update the other bundles - if (supplementers.containsKey(bundle.getSymbolicName())) { - - // remove the supplementer from the list of supplementers - final Supplementer supplementer = supplementers - .get(bundle.getSymbolicName()); - supplementers.remove(bundle.getSymbolicName()); - if (AbstractWeavingHook.verbose) System.err.println( - "[org.eclipse.equinox.weaving.hook] info removing supplementer " //$NON-NLS-1$ - + bundle.getSymbolicName()); - - // refresh bundles that where supplemented by this bundle - final Bundle[] supplementedBundles = supplementer - .getSupplementedBundles(); - if (supplementedBundles != null && supplementedBundles.length > 0) { - final List bundlesToRefresh = new ArrayList<>( - supplementedBundles.length); - for (final Bundle bundleToRefresh : supplementedBundles) { - if (this.adaptorProvider.getAdaptor( - bundleToRefresh.getBundleId()) != null) { - bundlesToRefresh.add(bundleToRefresh); - } - } - - if (bundlesToRefresh.size() > 0) { - refreshBundles(bundlesToRefresh - .toArray(new Bundle[bundlesToRefresh.size()])); - } - } - - // remove this supplementer from the list of supplementers per other bundle - for (Bundle supplementedBundle : supplementedBundles) { - final long bundleId = supplementedBundle.getBundleId(); - final List supplementerList = new ArrayList<>( - Arrays.asList( - this.supplementersByBundle.get(bundleId))); - supplementerList.remove(supplementer); - this.supplementersByBundle.put(bundleId, - supplementerList.toArray(new Supplementer[0])); - } - } - } - - private void removeSupplementedBundle(final Bundle bundle) { - for (final Supplementer supplementer : this.supplementers.values()) { - supplementer.removeSupplementedBundle(bundle); - } - } - - private void resupplementInstalledBundles(final Supplementer supplementer) { - final Bundle[] installedBundles = context.getBundles(); - - final List bundlesToRefresh = new ArrayList<>(); - - for (Bundle installedBundle : installedBundles) { - try { - final Bundle bundle = installedBundle; - // skip the bundle itself, just resupplement already installed bundles - if (bundle.getSymbolicName() - .equals(supplementer.getSymbolicName())) { - continue; - } - // skip bundles that should not be woven - if (dontWeaveTheseBundles.contains(bundle.getSymbolicName())) { - continue; - } - // find out which of the installed bundles matches the new supplementer - final Dictionary manifest = bundle.getHeaders(""); //$NON-NLS-1$ - final ManifestElement[] imports = ManifestElement.parseHeader( - Constants.IMPORT_PACKAGE, - (String) manifest.get(Constants.IMPORT_PACKAGE)); - final ManifestElement[] exports = ManifestElement.parseHeader( - Constants.EXPORT_PACKAGE, - (String) manifest.get(Constants.EXPORT_PACKAGE)); - if (isSupplementerMatching(bundle.getSymbolicName(), imports, - exports, supplementer)) { - final IWeavingAdaptor adaptor = this.adaptorProvider - .getAdaptor(bundle.getBundleId()); - if (adaptor != null && adaptor.isInitialized()) { - bundlesToRefresh.add(bundle); - } else { - supplementer.addSupplementedBundle(bundle); - final Supplementer[] existingSupplementers = supplementersByBundle - .get(bundle.getBundleId()); - List enhancedSupplementerList = null; - if (existingSupplementers != null) { - enhancedSupplementerList = new ArrayList<>( - Arrays.asList(existingSupplementers)); - } else { - enhancedSupplementerList = new ArrayList<>(); - } - if (!enhancedSupplementerList.contains(supplementer)) { - enhancedSupplementerList.add(supplementer); - } - - this.supplementersByBundle.put(bundle.getBundleId(), - enhancedSupplementerList - .toArray(new Supplementer[0])); - } - } - } catch (final BundleException e) { - e.printStackTrace(); - } - } - - if (bundlesToRefresh.size() > 0) { - final Bundle[] bundles = bundlesToRefresh - .toArray(new Bundle[bundlesToRefresh.size()]); - - refreshBundles(bundles); - } - } - - /** - * @see org.eclipse.equinox.service.weaving.ISupplementerRegistry#setBundleContext(org.osgi.framework.BundleContext) - */ - @Override - public void setBundleContext(final BundleContext context) { - this.context = context; - } - - /** - * @see org.eclipse.equinox.service.weaving.ISupplementerRegistry#setPackageAdmin(org.osgi.service.packageadmin.PackageAdmin) - */ - @Override - public void setPackageAdmin(final PackageAdmin packageAdmin) { - this.packageAdmin = packageAdmin; - } + /** + * Manifest header (named "Supplement-Bundle") identifying the names + * (and optionally, version numbers) of any bundles supplemented by this bundle. + * All supplemented bundles will have all the exported packages of this bundle + * added to their imports list + * + *

+ * The attribute value may be retrieved from the Dictionary object + * returned by the Bundle.getHeaders method. + */ + public static final String SUPPLEMENT_BUNDLE = "Eclipse-SupplementBundle"; //$NON-NLS-1$ + + /** + * Manifest header (named "Supplement-Exporter") identifying the names + * (and optionally, version numbers) of the packages that the bundle + * supplements. All exporters of one of these packages will have the exported + * packages of this bundle added to their imports list. + * + *

+ * The attribute value may be retrieved from the Dictionary object + * returned by the Bundle.getHeaders method. + */ + public static final String SUPPLEMENT_EXPORTER = "Eclipse-SupplementExporter"; //$NON-NLS-1$ + + /** + * Manifest header (named "Supplement-Importer") identifying the names + * (and optionally, version numbers) of the packages that the bundle + * supplements. All importers of one of these packages will have the exported + * packages of this bundle added to their imports in addition. + * + *

+ * The attribute value may be retrieved from the Dictionary object + * returned by the Bundle.getHeaders method. + */ + public static final String SUPPLEMENT_IMPORTER = "Eclipse-SupplementImporter"; //$NON-NLS-1$ + + private final IAdaptorProvider adaptorProvider; + + private BundleContext context; + + private final Set dontWeaveTheseBundles; // elements of type String (symbolic name of bundle) + + private PackageAdmin packageAdmin; + + private final Map supplementers; // keys of type String (symbolic name of supplementer bundle) + + private final Map supplementersByBundle; + + public SupplementerRegistry(final IAdaptorProvider adaptorProvider) { + this.adaptorProvider = adaptorProvider; + + this.supplementers = new HashMap<>(); + this.supplementersByBundle = new HashMap<>(); + this.dontWeaveTheseBundles = new HashSet<>(); + + this.dontWeaveTheseBundles.add("org.eclipse.osgi"); + this.dontWeaveTheseBundles.add("org.eclipse.core.runtime"); + this.dontWeaveTheseBundles.add("org.eclipse.equinox.common"); + this.dontWeaveTheseBundles.add("org.eclipse.equinox.weaving.hook"); + this.dontWeaveTheseBundles.add("org.eclipse.equinox.weaving.aspectj"); + this.dontWeaveTheseBundles.add("org.eclipse.equinox.weaving.caching"); + this.dontWeaveTheseBundles.add("org.eclipse.equinox.weaving.caching.j9"); + this.dontWeaveTheseBundles.add("org.aspectj.runtime"); + this.dontWeaveTheseBundles.add("org.aspectj.weaver"); + this.dontWeaveTheseBundles.add("org.eclipse.equinox.simpleconfigurator"); + } + + /** + * @see org.eclipse.equinox.service.weaving.ISupplementerRegistry#addBundle(org.osgi.framework.Bundle) + */ + @Override + public void addBundle(final Bundle bundle) { + // First analyze which supplementers already exists for this bundle + addSupplementedBundle(bundle); + + // Second analyze if this bundle itself is a supplementer + addSupplementer(bundle, true); + } + + /** + * @see org.eclipse.equinox.service.weaving.ISupplementerRegistry#addSupplementedBundle(org.osgi.framework.Bundle) + */ + @Override + public void addSupplementedBundle(final Bundle bundle) { + try { + final Dictionary manifest = bundle.getHeaders(""); //$NON-NLS-1$ + final ManifestElement[] imports = ManifestElement.parseHeader(Constants.IMPORT_PACKAGE, + (String) manifest.get(Constants.IMPORT_PACKAGE)); + final ManifestElement[] exports = ManifestElement.parseHeader(Constants.EXPORT_PACKAGE, + (String) manifest.get(Constants.EXPORT_PACKAGE)); + final List supplementers = getMatchingSupplementers(bundle.getSymbolicName(), imports, + exports); + if (supplementers.size() > 0) { + this.addSupplementedBundle(bundle, supplementers); + } + + this.supplementersByBundle.put(bundle.getBundleId(), + supplementers.toArray(new Supplementer[supplementers.size()])); + } catch (final BundleException e) { + } + } + + private void addSupplementedBundle(final Bundle supplementedBundle, final List supplementers) { + for (final Supplementer supplementer : supplementers) { + supplementer.addSupplementedBundle(supplementedBundle); + } + } + + /** + * @see org.eclipse.equinox.service.weaving.ISupplementerRegistry#addSupplementer(org.osgi.framework.Bundle, + * boolean) + */ + @Override + public void addSupplementer(final Bundle bundle, final boolean updateBundles) { + try { + final Dictionary manifest = bundle.getHeaders(""); //$NON-NLS-1$ + final ManifestElement[] supplementBundle = ManifestElement.parseHeader(SUPPLEMENT_BUNDLE, + (String) manifest.get(SUPPLEMENT_BUNDLE)); + final ManifestElement[] supplementImporter = ManifestElement.parseHeader(SUPPLEMENT_IMPORTER, + (String) manifest.get(SUPPLEMENT_IMPORTER)); + final ManifestElement[] supplementExporter = ManifestElement.parseHeader(SUPPLEMENT_EXPORTER, + (String) manifest.get(SUPPLEMENT_EXPORTER)); + + if (supplementBundle != null || supplementImporter != null || supplementExporter != null) { + + final Bundle[] hosts = this.packageAdmin.getHosts(bundle); + final Bundle host = hosts != null && hosts.length == 1 ? hosts[0] : null; + + final Supplementer newSupplementer = new Supplementer(bundle, host, supplementBundle, + supplementImporter, supplementExporter); + + this.supplementers.put(bundle.getSymbolicName(), newSupplementer); + if (updateBundles) { + resupplementInstalledBundles(newSupplementer); + } + } + } catch (final BundleException e) { + } + } + + /** + * @see org.eclipse.equinox.service.weaving.ISupplementerRegistry#getMatchingSupplementers(java.lang.String, + * org.eclipse.osgi.util.ManifestElement[], + * org.eclipse.osgi.util.ManifestElement[]) + */ + @Override + public List getMatchingSupplementers(final String symbolicName, final ManifestElement[] imports, + final ManifestElement[] exports) { + List result = Collections.emptyList(); + + if (supplementers.size() > 0 && !this.dontWeaveTheseBundles.contains(symbolicName)) { + result = new LinkedList<>(); + for (Supplementer supplementer : supplementers.values()) { + if (isSupplementerMatching(symbolicName, imports, exports, supplementer)) { + result.add(supplementer); + } + } + } + + return result; + } + + /** + * @see org.eclipse.equinox.service.weaving.ISupplementerRegistry#getPackageAdmin() + */ + @Override + public PackageAdmin getPackageAdmin() { + return packageAdmin; + } + + /** + * @see org.eclipse.equinox.service.weaving.ISupplementerRegistry#getSupplementers(org.osgi.framework.Bundle) + */ + @Override + public Supplementer[] getSupplementers(final Bundle bundle) { + return getSupplementers(bundle.getBundleId()); + } + + /** + * @see org.eclipse.equinox.service.weaving.ISupplementerRegistry#getSupplementers(long) + */ + @Override + public Supplementer[] getSupplementers(final long bundleID) { + if (supplementersByBundle.containsKey(bundleID)) { + return supplementersByBundle.get(bundleID); + } else { + return new Supplementer[0]; + } + } + + private boolean isSupplementerMatching(final String symbolicName, final ManifestElement[] imports, + final ManifestElement[] exports, final Supplementer supplementer) { + final String supplementerName = supplementer.getSymbolicName(); + if (!supplementerName.equals(symbolicName)) { + if (supplementer.matchSupplementer(symbolicName) + || (imports != null && supplementer.matchesSupplementImporter(imports)) + || (exports != null && supplementer.matchesSupplementExporter(exports))) { + return true; + } + } + return false; + } + + /** + * Refreshes the given bundles + * + * @param bundles The bundles to refresh + */ + @Override + public void refreshBundles(final Bundle[] bundles) { + // if (this.packageAdmin != null) { + // if (AbstractWeavingHook.verbose) { + // for (int i = 0; i < bundles.length; i++) { + // System.out.println("refresh bundle: " + // + bundles[i].getSymbolicName()); + // } + // } + // + // this.packageAdmin.refreshPackages(bundles); + // } + } + + /** + * @see org.eclipse.equinox.service.weaving.ISupplementerRegistry#removeBundle(org.osgi.framework.Bundle) + */ + @Override + public void removeBundle(final Bundle bundle) { + // if this bundle is itself supplemented by others, remove the bundle from those + // lists + removeSupplementedBundle(bundle); + this.supplementersByBundle.remove(bundle.getBundleId()); + + this.adaptorProvider.resetAdaptor(bundle.getBundleId()); + + // if this bundle supplements other bundles, remove this supplementer and update + // the other bundles + if (supplementers.containsKey(bundle.getSymbolicName())) { + + // remove the supplementer from the list of supplementers + final Supplementer supplementer = supplementers.get(bundle.getSymbolicName()); + supplementers.remove(bundle.getSymbolicName()); + if (AbstractWeavingHook.verbose) + System.err.println("[org.eclipse.equinox.weaving.hook] info removing supplementer " //$NON-NLS-1$ + + bundle.getSymbolicName()); + + // refresh bundles that where supplemented by this bundle + final Bundle[] supplementedBundles = supplementer.getSupplementedBundles(); + if (supplementedBundles != null && supplementedBundles.length > 0) { + final List bundlesToRefresh = new ArrayList<>(supplementedBundles.length); + for (final Bundle bundleToRefresh : supplementedBundles) { + if (this.adaptorProvider.getAdaptor(bundleToRefresh.getBundleId()) != null) { + bundlesToRefresh.add(bundleToRefresh); + } + } + + if (bundlesToRefresh.size() > 0) { + refreshBundles(bundlesToRefresh.toArray(new Bundle[bundlesToRefresh.size()])); + } + } + + // remove this supplementer from the list of supplementers per other bundle + for (Bundle supplementedBundle : supplementedBundles) { + final long bundleId = supplementedBundle.getBundleId(); + final List supplementerList = new ArrayList<>( + Arrays.asList(this.supplementersByBundle.get(bundleId))); + supplementerList.remove(supplementer); + this.supplementersByBundle.put(bundleId, supplementerList.toArray(new Supplementer[0])); + } + } + } + + private void removeSupplementedBundle(final Bundle bundle) { + for (final Supplementer supplementer : this.supplementers.values()) { + supplementer.removeSupplementedBundle(bundle); + } + } + + private void resupplementInstalledBundles(final Supplementer supplementer) { + final Bundle[] installedBundles = context.getBundles(); + + final List bundlesToRefresh = new ArrayList<>(); + + for (Bundle installedBundle : installedBundles) { + try { + final Bundle bundle = installedBundle; + // skip the bundle itself, just resupplement already installed bundles + if (bundle.getSymbolicName().equals(supplementer.getSymbolicName())) { + continue; + } + // skip bundles that should not be woven + if (dontWeaveTheseBundles.contains(bundle.getSymbolicName())) { + continue; + } + // find out which of the installed bundles matches the new supplementer + final Dictionary manifest = bundle.getHeaders(""); //$NON-NLS-1$ + final ManifestElement[] imports = ManifestElement.parseHeader(Constants.IMPORT_PACKAGE, + (String) manifest.get(Constants.IMPORT_PACKAGE)); + final ManifestElement[] exports = ManifestElement.parseHeader(Constants.EXPORT_PACKAGE, + (String) manifest.get(Constants.EXPORT_PACKAGE)); + if (isSupplementerMatching(bundle.getSymbolicName(), imports, exports, supplementer)) { + final IWeavingAdaptor adaptor = this.adaptorProvider.getAdaptor(bundle.getBundleId()); + if (adaptor != null && adaptor.isInitialized()) { + bundlesToRefresh.add(bundle); + } else { + supplementer.addSupplementedBundle(bundle); + final Supplementer[] existingSupplementers = supplementersByBundle.get(bundle.getBundleId()); + List enhancedSupplementerList = null; + if (existingSupplementers != null) { + enhancedSupplementerList = new ArrayList<>(Arrays.asList(existingSupplementers)); + } else { + enhancedSupplementerList = new ArrayList<>(); + } + if (!enhancedSupplementerList.contains(supplementer)) { + enhancedSupplementerList.add(supplementer); + } + + this.supplementersByBundle.put(bundle.getBundleId(), + enhancedSupplementerList.toArray(new Supplementer[0])); + } + } + } catch (final BundleException e) { + e.printStackTrace(); + } + } + + if (bundlesToRefresh.size() > 0) { + final Bundle[] bundles = bundlesToRefresh.toArray(new Bundle[bundlesToRefresh.size()]); + + refreshBundles(bundles); + } + } + + /** + * @see org.eclipse.equinox.service.weaving.ISupplementerRegistry#setBundleContext(org.osgi.framework.BundleContext) + */ + @Override + public void setBundleContext(final BundleContext context) { + this.context = context; + } + + /** + * @see org.eclipse.equinox.service.weaving.ISupplementerRegistry#setPackageAdmin(org.osgi.service.packageadmin.PackageAdmin) + */ + @Override + public void setPackageAdmin(final PackageAdmin packageAdmin) { + this.packageAdmin = packageAdmin; + } } diff --git a/bundles/org.eclipse.equinox.weaving.hook/src/org/eclipse/equinox/weaving/hooks/WeavingBundleEntry.java b/bundles/org.eclipse.equinox.weaving.hook/src/org/eclipse/equinox/weaving/hooks/WeavingBundleEntry.java index f2a8934ac6b..054ff36f5ec 100644 --- a/bundles/org.eclipse.equinox.weaving.hook/src/org/eclipse/equinox/weaving/hooks/WeavingBundleEntry.java +++ b/bundles/org.eclipse.equinox.weaving.hook/src/org/eclipse/equinox/weaving/hooks/WeavingBundleEntry.java @@ -26,67 +26,67 @@ public class WeavingBundleEntry extends BundleEntry { - private final IWeavingAdaptor adaptor; - - private final URL bundleFileURL; - - private final BundleEntry delegate; - - private final boolean dontWeave; - - public WeavingBundleEntry(final IWeavingAdaptor aspectjAdaptor, - final BundleEntry delegate, final URL url, final boolean dontWeave) { - this.adaptor = aspectjAdaptor; - this.bundleFileURL = url; - this.delegate = delegate; - this.dontWeave = dontWeave; - } - - public boolean dontWeave() { - return dontWeave; - } - - public IWeavingAdaptor getAdaptor() { - return adaptor; - } - - public URL getBundleFileURL() { - return bundleFileURL; - } - - @Override - public byte[] getBytes() throws IOException { - return delegate.getBytes(); - } - - @Override - public URL getFileURL() { - return delegate.getFileURL(); - } - - @Override - public InputStream getInputStream() throws IOException { - return delegate.getInputStream(); - } - - @Override - public URL getLocalURL() { - return delegate.getLocalURL(); - } - - @Override - public String getName() { - return delegate.getName(); - } - - @Override - public long getSize() { - return delegate.getSize(); - } - - @Override - public long getTime() { - return delegate.getTime(); - } + private final IWeavingAdaptor adaptor; + + private final URL bundleFileURL; + + private final BundleEntry delegate; + + private final boolean dontWeave; + + public WeavingBundleEntry(final IWeavingAdaptor aspectjAdaptor, final BundleEntry delegate, final URL url, + final boolean dontWeave) { + this.adaptor = aspectjAdaptor; + this.bundleFileURL = url; + this.delegate = delegate; + this.dontWeave = dontWeave; + } + + public boolean dontWeave() { + return dontWeave; + } + + public IWeavingAdaptor getAdaptor() { + return adaptor; + } + + public URL getBundleFileURL() { + return bundleFileURL; + } + + @Override + public byte[] getBytes() throws IOException { + return delegate.getBytes(); + } + + @Override + public URL getFileURL() { + return delegate.getFileURL(); + } + + @Override + public InputStream getInputStream() throws IOException { + return delegate.getInputStream(); + } + + @Override + public URL getLocalURL() { + return delegate.getLocalURL(); + } + + @Override + public String getName() { + return delegate.getName(); + } + + @Override + public long getSize() { + return delegate.getSize(); + } + + @Override + public long getTime() { + return delegate.getTime(); + } } diff --git a/bundles/org.eclipse.equinox.weaving.hook/src/org/eclipse/equinox/weaving/hooks/WeavingBundleFile.java b/bundles/org.eclipse.equinox.weaving.hook/src/org/eclipse/equinox/weaving/hooks/WeavingBundleFile.java index bf4fb0cdcfc..069bbe77afc 100644 --- a/bundles/org.eclipse.equinox.weaving.hook/src/org/eclipse/equinox/weaving/hooks/WeavingBundleFile.java +++ b/bundles/org.eclipse.equinox.weaving.hook/src/org/eclipse/equinox/weaving/hooks/WeavingBundleFile.java @@ -36,69 +36,62 @@ */ public class WeavingBundleFile extends AbstractWeavingBundleFile { - private final URL url; + private final URL url; - /** - * Create a new wrapper for a bundle file - * - * @param adaptorProvider A provider that allows this wrapper to gain access - * to the adaptor of this bundle - * @param bundleFile The wrapped bundle file - * @throws IOException - */ - public WeavingBundleFile(final BundleAdaptorProvider adaptorProvider, - final BundleFile bundleFile) { - super(adaptorProvider, bundleFile); - try { - this.url = delegate.getBaseFile().toURL(); - } catch (final MalformedURLException e) { - throw new RuntimeException( - "Unexpected error getting bundle file URL.", e); - } - } + /** + * Create a new wrapper for a bundle file + * + * @param adaptorProvider A provider that allows this wrapper to gain access to + * the adaptor of this bundle + * @param bundleFile The wrapped bundle file + * @throws IOException + */ + public WeavingBundleFile(final BundleAdaptorProvider adaptorProvider, final BundleFile bundleFile) { + super(adaptorProvider, bundleFile); + try { + this.url = delegate.getBaseFile().toURL(); + } catch (final MalformedURLException e) { + throw new RuntimeException("Unexpected error getting bundle file URL.", e); + } + } - @Override - public BundleEntry getEntry(final String path) { - if (Debug.DEBUG_BUNDLE) - Debug.println("> AspectJBundleFile.getEntry() path=" + path - + ", url=" + url); - BundleEntry entry = delegate.getEntry(path); + @Override + public BundleEntry getEntry(final String path) { + if (Debug.DEBUG_BUNDLE) + Debug.println("> AspectJBundleFile.getEntry() path=" + path + ", url=" + url); + BundleEntry entry = delegate.getEntry(path); - if (path.endsWith(".class") && entry != null) { - final int offset = path.lastIndexOf('.'); - final String name = path.substring(0, offset).replace('/', '.'); - final IWeavingAdaptor adaptor = getAdaptor(); - if (adaptor != null) { - final CacheEntry cacheEntry = adaptor.findClass(name, url); - if (cacheEntry == null) { - entry = new WeavingBundleEntry(adaptor, entry, url, false); - if (Debug.DEBUG_BUNDLE) - Debug.println("- AspectJBundleFile.getEntry() path=" - + path + ", entry=" + entry); - } else if (cacheEntry.getCachedBytes() != null) { - entry = new CachedClassBundleEntry(adaptor, entry, path, - cacheEntry.getCachedBytes(), url); - } else { - entry = new WeavingBundleEntry(adaptor, entry, url, - cacheEntry.dontWeave()); - } - } - } else if (path.endsWith(".class") && entry == null) { - final int offset = path.lastIndexOf('.'); - final String name = path.substring(0, offset).replace('/', '.'); - final IWeavingAdaptor adaptor = getAdaptor(); - if (adaptor != null) { - final CacheEntry cacheEntry = adaptor.findClass(name, url); - if (cacheEntry != null && cacheEntry.getCachedBytes() != null) { - entry = new CachedGeneratedClassBundleEntry(adaptor, path, - cacheEntry.getCachedBytes(), url); - } - } - } + if (path.endsWith(".class") && entry != null) { + final int offset = path.lastIndexOf('.'); + final String name = path.substring(0, offset).replace('/', '.'); + final IWeavingAdaptor adaptor = getAdaptor(); + if (adaptor != null) { + final CacheEntry cacheEntry = adaptor.findClass(name, url); + if (cacheEntry == null) { + entry = new WeavingBundleEntry(adaptor, entry, url, false); + if (Debug.DEBUG_BUNDLE) + Debug.println("- AspectJBundleFile.getEntry() path=" + path + ", entry=" + entry); + } else if (cacheEntry.getCachedBytes() != null) { + entry = new CachedClassBundleEntry(adaptor, entry, path, cacheEntry.getCachedBytes(), url); + } else { + entry = new WeavingBundleEntry(adaptor, entry, url, cacheEntry.dontWeave()); + } + } + } else if (path.endsWith(".class") && entry == null) { + final int offset = path.lastIndexOf('.'); + final String name = path.substring(0, offset).replace('/', '.'); + final IWeavingAdaptor adaptor = getAdaptor(); + if (adaptor != null) { + final CacheEntry cacheEntry = adaptor.findClass(name, url); + if (cacheEntry != null && cacheEntry.getCachedBytes() != null) { + entry = new CachedGeneratedClassBundleEntry(adaptor, path, cacheEntry.getCachedBytes(), url); + } + } + } - if (Debug.DEBUG_BUNDLE) - Debug.println("< AspectJBundleFile.getEntry() entry=" + entry); - return entry; - } + if (Debug.DEBUG_BUNDLE) + Debug.println("< AspectJBundleFile.getEntry() entry=" + entry); + return entry; + } } diff --git a/bundles/org.eclipse.equinox.weaving.hook/src/org/eclipse/equinox/weaving/hooks/WeavingHook.java b/bundles/org.eclipse.equinox.weaving.hook/src/org/eclipse/equinox/weaving/hooks/WeavingHook.java index bd36881b4ae..60925e14609 100644 --- a/bundles/org.eclipse.equinox.weaving.hook/src/org/eclipse/equinox/weaving/hooks/WeavingHook.java +++ b/bundles/org.eclipse.equinox.weaving.hook/src/org/eclipse/equinox/weaving/hooks/WeavingHook.java @@ -40,212 +40,195 @@ public class WeavingHook extends AbstractWeavingHook { - private final WeavingAdaptorFactory adaptorFactory; - - private final Map adaptors; - - private BundleContext bundleContext; - - public WeavingHook() { - if (Debug.DEBUG_GENERAL) Debug.println("- WeavingHook.()"); - - this.adaptorFactory = new WeavingAdaptorFactory(); - this.adaptors = new HashMap<>(); - } - - @Override - public void classLoaderCreated(final ModuleClassLoader classLoader) { - if (Debug.DEBUG_GENERAL) - Debug.println("> WeavingHook.initializedClassLoader() bundle=" - + classLoader.getBundle().getSymbolicName() - + ", loader=" - + classLoader - + ", bundleFile=" - + classLoader.getClasspathManager().getGeneration() - .getBundleFile()); - - final IWeavingAdaptor adaptor = createWeavingAdaptor(classLoader - .getClasspathManager().getGeneration(), classLoader); - this.adaptors.put(classLoader.getBundle().getBundleId(), adaptor); - - if (Debug.DEBUG_GENERAL) - Debug.println("< WeavingHook.initializedClassLoader() adaptor=" - + adaptor); - } - - private IWeavingAdaptor createWeavingAdaptor(final Generation generation, - final ModuleClassLoader classLoader) { - if (Debug.DEBUG_GENERAL) - Debug.println("> WeavingHook.createAspectJAdaptor() location=" - + generation.getRevision().getRevisions().getModule() - .getLocation()); - IWeavingAdaptor adaptor = null; - - if (adaptorFactory != null) { - adaptor = new WeavingAdaptor(generation, adaptorFactory, null, - null, classLoader); - } else { - if (Debug.DEBUG_GENERAL) - Debug.println("- WeavingHook.createAspectJAdaptor() factory=" - + adaptorFactory); - } - - if (Debug.DEBUG_GENERAL) - Debug.println("< WeavingHook.createAspectJAdaptor() adaptor=" - + adaptor); - return adaptor; - } - - @Override - public IWeavingAdaptor getAdaptor(final long bundleID) { - return this.adaptors.get(bundleID); - } - - @Override - public IWeavingAdaptor getHostBundleAdaptor(final long bundleID) { - final Bundle bundle = this.bundleContext.getBundle(bundleID); - if (bundle != null) { - final Bundle host = adaptorFactory.getHost(bundle); - if (host != null) { - final long hostBundleID = host.getBundleId(); - return this.adaptors.get(hostBundleID); - } - } - return null; - } - - private void initialize(final BundleContext context) { - if (Debug.DEBUG_GENERAL) - Debug.println("> WeavingHook.initialize() context=" + context); - - this.bundleContext = context; - - final ISupplementerRegistry supplementerRegistry = getSupplementerRegistry(); - adaptorFactory.initialize(context, supplementerRegistry); - - final ServiceReference serviceReference = context - .getServiceReference(PackageAdmin.class); - final PackageAdmin packageAdmin = context.getService(serviceReference); - - supplementerRegistry.setBundleContext(context); - supplementerRegistry.setPackageAdmin(packageAdmin); - context.addBundleListener(new SupplementBundleListener( - supplementerRegistry)); - - // re-build supplementer registry state for installed bundles - final Bundle[] installedBundles = context.getBundles(); - for (Bundle installedBundle : installedBundles) { - if (installedBundle.getState() != Bundle.UNINSTALLED) { - supplementerRegistry.addSupplementer(installedBundle, false); - } - } - for (Bundle installedBundle : installedBundles) { - if (installedBundle.getState() != Bundle.UNINSTALLED) { - supplementerRegistry.addSupplementedBundle(installedBundle); - } - } - - if (Debug.DEBUG_GENERAL) - Debug.println("< WeavingHook.initialize() adaptorFactory=" - + adaptorFactory); - } - - /** - * @see org.eclipse.equinox.weaving.hooks.AbstractWeavingHook#processClass(java.lang.String, - * byte[], org.eclipse.osgi.baseadaptor.loader.ClasspathEntry, - * org.eclipse.osgi.baseadaptor.bundlefile.BundleEntry, - * org.eclipse.osgi.baseadaptor.loader.ClasspathManager) - */ - @Override - public byte[] processClass(final String name, final byte[] classbytes, - final ClasspathEntry classpathEntry, final BundleEntry entry, - final ClasspathManager manager) { - byte[] newClassytes = null; - if (entry instanceof WeavingBundleEntry) { - final WeavingBundleEntry ajBundleEntry = (WeavingBundleEntry) entry; - if (!ajBundleEntry.dontWeave()) { - final IWeavingAdaptor adaptor = ajBundleEntry.getAdaptor(); - newClassytes = adaptor.weaveClass(name, classbytes); - } - } - return newClassytes; - } - - /** - * @see org.eclipse.equinox.weaving.hooks.AbstractWeavingHook#recordClassDefine(java.lang.String, - * java.lang.Class, byte[], - * org.eclipse.osgi.baseadaptor.loader.ClasspathEntry, - * org.eclipse.osgi.baseadaptor.bundlefile.BundleEntry, - * org.eclipse.osgi.baseadaptor.loader.ClasspathManager) - */ - @Override - public void recordClassDefine(final String name, final Class clazz, - final byte[] classbytes, final ClasspathEntry classpathEntry, - final BundleEntry entry, final ClasspathManager manager) { - if (clazz == null) { - if (Debug.DEBUG_GENERAL) { - Debug.println("Error in defining class: " + name); //$NON-NLS-1$ - } - return; - } - if (entry instanceof WeavingBundleEntry) { - final WeavingBundleEntry ajBundleEntry = (WeavingBundleEntry) entry; - if (!ajBundleEntry.dontWeave()) { - final IWeavingAdaptor adaptor = ajBundleEntry.getAdaptor(); - final URL sourceFileURL = ajBundleEntry.getBundleFileURL(); - adaptor.storeClass(name, sourceFileURL, clazz, classbytes); - } - } - } - - @Override - public void resetAdaptor(final long bundleID) { - this.adaptors.remove(bundleID); - } - - /** - * @see org.eclipse.equinox.weaving.hooks.AbstractWeavingHook#frameworkStart(org.osgi.framework.BundleContext) - */ - public void start(final BundleContext context) throws BundleException { - initialize(context); - } - - /** - * @see org.eclipse.equinox.weaving.hooks.AbstractWeavingHook#frameworkStop(org.osgi.framework.BundleContext) - */ - public void stop(final BundleContext context) throws BundleException { - adaptorFactory.dispose(context); - } - - /** - * @see org.eclipse.osgi.internal.hookregistry.BundleFileWrapperFactoryHook#wrapBundleFile(org.eclipse.osgi.storage.bundlefile.BundleFile, - * org.eclipse.osgi.storage.BundleInfo.Generation, boolean) - */ - public BundleFileWrapper wrapBundleFile(final BundleFile bundleFile, - final Generation generation, final boolean base) { - if (bundleFile.getBaseFile() == null) { - // must have a base file to work - return null; - } - BundleFileWrapper wrapped = null; - if (Debug.DEBUG_BUNDLE) - Debug.println("> WeavingHook.wrapBundleFile() bundle=" - + (generation.getRevision() != null ? generation - .getRevision().getSymbolicName() : generation - .getBundleInfo().getBundleId()) + " bundleFile=" - + bundleFile + ", generation=" + generation + ", base=" - + base + ", baseFile=" + bundleFile.getBaseFile()); - - if (base) { - wrapped = new BaseWeavingBundleFile(new BundleAdaptorProvider( - generation, this), bundleFile); - } else { - wrapped = new WeavingBundleFile(new BundleAdaptorProvider( - generation, this), bundleFile); - } - if (Debug.DEBUG_BUNDLE) - Debug.println("< WeavingHook.wrapBundleFile() wrapped=" + wrapped); - return wrapped; - } + private final WeavingAdaptorFactory adaptorFactory; + + private final Map adaptors; + + private BundleContext bundleContext; + + public WeavingHook() { + if (Debug.DEBUG_GENERAL) + Debug.println("- WeavingHook.()"); + + this.adaptorFactory = new WeavingAdaptorFactory(); + this.adaptors = new HashMap<>(); + } + + @Override + public void classLoaderCreated(final ModuleClassLoader classLoader) { + if (Debug.DEBUG_GENERAL) + Debug.println("> WeavingHook.initializedClassLoader() bundle=" + classLoader.getBundle().getSymbolicName() + + ", loader=" + classLoader + ", bundleFile=" + + classLoader.getClasspathManager().getGeneration().getBundleFile()); + + final IWeavingAdaptor adaptor = createWeavingAdaptor(classLoader.getClasspathManager().getGeneration(), + classLoader); + this.adaptors.put(classLoader.getBundle().getBundleId(), adaptor); + + if (Debug.DEBUG_GENERAL) + Debug.println("< WeavingHook.initializedClassLoader() adaptor=" + adaptor); + } + + private IWeavingAdaptor createWeavingAdaptor(final Generation generation, final ModuleClassLoader classLoader) { + if (Debug.DEBUG_GENERAL) + Debug.println("> WeavingHook.createAspectJAdaptor() location=" + + generation.getRevision().getRevisions().getModule().getLocation()); + IWeavingAdaptor adaptor = null; + + if (adaptorFactory != null) { + adaptor = new WeavingAdaptor(generation, adaptorFactory, null, null, classLoader); + } else { + if (Debug.DEBUG_GENERAL) + Debug.println("- WeavingHook.createAspectJAdaptor() factory=" + adaptorFactory); + } + + if (Debug.DEBUG_GENERAL) + Debug.println("< WeavingHook.createAspectJAdaptor() adaptor=" + adaptor); + return adaptor; + } + + @Override + public IWeavingAdaptor getAdaptor(final long bundleID) { + return this.adaptors.get(bundleID); + } + + @Override + public IWeavingAdaptor getHostBundleAdaptor(final long bundleID) { + final Bundle bundle = this.bundleContext.getBundle(bundleID); + if (bundle != null) { + final Bundle host = adaptorFactory.getHost(bundle); + if (host != null) { + final long hostBundleID = host.getBundleId(); + return this.adaptors.get(hostBundleID); + } + } + return null; + } + + private void initialize(final BundleContext context) { + if (Debug.DEBUG_GENERAL) + Debug.println("> WeavingHook.initialize() context=" + context); + + this.bundleContext = context; + + final ISupplementerRegistry supplementerRegistry = getSupplementerRegistry(); + adaptorFactory.initialize(context, supplementerRegistry); + + final ServiceReference serviceReference = context.getServiceReference(PackageAdmin.class); + final PackageAdmin packageAdmin = context.getService(serviceReference); + + supplementerRegistry.setBundleContext(context); + supplementerRegistry.setPackageAdmin(packageAdmin); + context.addBundleListener(new SupplementBundleListener(supplementerRegistry)); + + // re-build supplementer registry state for installed bundles + final Bundle[] installedBundles = context.getBundles(); + for (Bundle installedBundle : installedBundles) { + if (installedBundle.getState() != Bundle.UNINSTALLED) { + supplementerRegistry.addSupplementer(installedBundle, false); + } + } + for (Bundle installedBundle : installedBundles) { + if (installedBundle.getState() != Bundle.UNINSTALLED) { + supplementerRegistry.addSupplementedBundle(installedBundle); + } + } + + if (Debug.DEBUG_GENERAL) + Debug.println("< WeavingHook.initialize() adaptorFactory=" + adaptorFactory); + } + + /** + * @see org.eclipse.equinox.weaving.hooks.AbstractWeavingHook#processClass(java.lang.String, + * byte[], org.eclipse.osgi.baseadaptor.loader.ClasspathEntry, + * org.eclipse.osgi.baseadaptor.bundlefile.BundleEntry, + * org.eclipse.osgi.baseadaptor.loader.ClasspathManager) + */ + @Override + public byte[] processClass(final String name, final byte[] classbytes, final ClasspathEntry classpathEntry, + final BundleEntry entry, final ClasspathManager manager) { + byte[] newClassytes = null; + if (entry instanceof WeavingBundleEntry) { + final WeavingBundleEntry ajBundleEntry = (WeavingBundleEntry) entry; + if (!ajBundleEntry.dontWeave()) { + final IWeavingAdaptor adaptor = ajBundleEntry.getAdaptor(); + newClassytes = adaptor.weaveClass(name, classbytes); + } + } + return newClassytes; + } + + /** + * @see org.eclipse.equinox.weaving.hooks.AbstractWeavingHook#recordClassDefine(java.lang.String, + * java.lang.Class, byte[], + * org.eclipse.osgi.baseadaptor.loader.ClasspathEntry, + * org.eclipse.osgi.baseadaptor.bundlefile.BundleEntry, + * org.eclipse.osgi.baseadaptor.loader.ClasspathManager) + */ + @Override + public void recordClassDefine(final String name, final Class clazz, final byte[] classbytes, + final ClasspathEntry classpathEntry, final BundleEntry entry, final ClasspathManager manager) { + if (clazz == null) { + if (Debug.DEBUG_GENERAL) { + Debug.println("Error in defining class: " + name); //$NON-NLS-1$ + } + return; + } + if (entry instanceof WeavingBundleEntry) { + final WeavingBundleEntry ajBundleEntry = (WeavingBundleEntry) entry; + if (!ajBundleEntry.dontWeave()) { + final IWeavingAdaptor adaptor = ajBundleEntry.getAdaptor(); + final URL sourceFileURL = ajBundleEntry.getBundleFileURL(); + adaptor.storeClass(name, sourceFileURL, clazz, classbytes); + } + } + } + + @Override + public void resetAdaptor(final long bundleID) { + this.adaptors.remove(bundleID); + } + + /** + * @see org.eclipse.equinox.weaving.hooks.AbstractWeavingHook#frameworkStart(org.osgi.framework.BundleContext) + */ + public void start(final BundleContext context) throws BundleException { + initialize(context); + } + + /** + * @see org.eclipse.equinox.weaving.hooks.AbstractWeavingHook#frameworkStop(org.osgi.framework.BundleContext) + */ + public void stop(final BundleContext context) throws BundleException { + adaptorFactory.dispose(context); + } + + /** + * @see org.eclipse.osgi.internal.hookregistry.BundleFileWrapperFactoryHook#wrapBundleFile(org.eclipse.osgi.storage.bundlefile.BundleFile, + * org.eclipse.osgi.storage.BundleInfo.Generation, boolean) + */ + public BundleFileWrapper wrapBundleFile(final BundleFile bundleFile, final Generation generation, + final boolean base) { + if (bundleFile.getBaseFile() == null) { + // must have a base file to work + return null; + } + BundleFileWrapper wrapped = null; + if (Debug.DEBUG_BUNDLE) + Debug.println("> WeavingHook.wrapBundleFile() bundle=" + + (generation.getRevision() != null ? generation.getRevision().getSymbolicName() + : generation.getBundleInfo().getBundleId()) + + " bundleFile=" + bundleFile + ", generation=" + generation + ", base=" + base + ", baseFile=" + + bundleFile.getBaseFile()); + + if (base) { + wrapped = new BaseWeavingBundleFile(new BundleAdaptorProvider(generation, this), bundleFile); + } else { + wrapped = new WeavingBundleFile(new BundleAdaptorProvider(generation, this), bundleFile); + } + if (Debug.DEBUG_BUNDLE) + Debug.println("< WeavingHook.wrapBundleFile() wrapped=" + wrapped); + return wrapped; + } } diff --git a/bundles/org.eclipse.equinox.weaving.hook/src/org/eclipse/equinox/weaving/hooks/WeavingLoaderDelegateHook.java b/bundles/org.eclipse.equinox.weaving.hook/src/org/eclipse/equinox/weaving/hooks/WeavingLoaderDelegateHook.java index ad0f4e1f430..e32c36a15eb 100644 --- a/bundles/org.eclipse.equinox.weaving.hook/src/org/eclipse/equinox/weaving/hooks/WeavingLoaderDelegateHook.java +++ b/bundles/org.eclipse.equinox.weaving.hook/src/org/eclipse/equinox/weaving/hooks/WeavingLoaderDelegateHook.java @@ -40,159 +40,155 @@ */ public class WeavingLoaderDelegateHook extends ClassLoaderHook { - private final ThreadLocal> postFindClassCalls = new ThreadLocal>() { - - @Override - protected Set initialValue() { - return new HashSet<>(); - } - }; - - private final ThreadLocal> postFindResourceCalls = new ThreadLocal>() { - - @Override - protected Set initialValue() { - return new HashSet<>(); - } - }; - - private final ThreadLocal> postFindResourcesCalls = new ThreadLocal>() { - - @Override - protected Set initialValue() { - return new HashSet<>(); - } - }; - - private final ISupplementerRegistry supplementerRegistry; - - /** - * Create the hook instance for broaden the visibility according to the - * supplementing mechansism. - * - * @param supplementerRegistry The supplementer registry to be used by this - * hook for information retrieval which bundles are supplemented - * by which other bundles (needs to not be null) - */ - public WeavingLoaderDelegateHook( - final ISupplementerRegistry supplementerRegistry) { - this.supplementerRegistry = supplementerRegistry; - } - - /** - * - * @see org.eclipse.osgi.internal.hookregistry.ClassLoaderHook#postFindClass(java.lang.String, - * org.eclipse.osgi.internal.loader.ModuleClassLoader) - */ - @Override - public Class postFindClass(final String name, - final ModuleClassLoader classLoader) throws ClassNotFoundException { - final long bundleID = classLoader.getBundle().getBundleId(); - - final String callKey = bundleID + name; - if (postFindClassCalls.get().contains(callKey)) { - return null; - } - - postFindClassCalls.get().add(callKey); - try { - final Supplementer[] supplementers = supplementerRegistry - .getSupplementers(bundleID); - if (supplementers != null) { - for (Supplementer supplementer : supplementers) { - try { - final Bundle bundle = supplementer.getSupplementerHost(); - if (bundle.getState() != Bundle.UNINSTALLED) { - final Class clazz = bundle.loadClass(name); - if (clazz != null) { - return clazz; - } - } - }catch (final ClassNotFoundException e) { - } - } - } - } finally { - postFindClassCalls.get().remove(callKey); - } - - return null; - } - - /** - * - * @see org.eclipse.osgi.internal.hookregistry.ClassLoaderHook#postFindResource(java.lang.String, - * org.eclipse.osgi.internal.loader.ModuleClassLoader) - */ - @Override - public URL postFindResource(final String name, - final ModuleClassLoader classLoader) throws FileNotFoundException { - final long bundleID = classLoader.getBundle().getBundleId(); - - final String callKey = bundleID + name; - if (postFindResourceCalls.get().contains(callKey)) { - return null; - } - - postFindResourceCalls.get().add(callKey); - try { - final Supplementer[] supplementers = supplementerRegistry - .getSupplementers(bundleID); - if (supplementers != null) { - for (Supplementer supplementer : supplementers) { - try { - final URL resource = supplementer.getSupplementerHost().getResource(name); - if (resource != null) { - return resource; - } - }catch (final Exception e) { - e.printStackTrace(); - } - } - } - } finally { - postFindResourceCalls.get().remove(callKey); - } - - return null; - } - - /** - * - * @see org.eclipse.osgi.internal.hookregistry.ClassLoaderHook#postFindResources(java.lang.String, - * org.eclipse.osgi.internal.loader.ModuleClassLoader) - */ - @Override - public Enumeration postFindResources(final String name, - final ModuleClassLoader classLoader) throws FileNotFoundException { - final long bundleID = classLoader.getBundle().getBundleId(); - - final String callKey = bundleID + name; - if (postFindResourcesCalls.get().contains(callKey)) { - return null; - } - - postFindResourcesCalls.get().add(callKey); - try { - final Supplementer[] supplementers = supplementerRegistry - .getSupplementers(bundleID); - if (supplementers != null) { - for (Supplementer supplementer : supplementers) { - try { - final Enumeration resource = supplementer.getSupplementerHost().getResources(name); - if (resource != null) { - // TODO: if more than one enumeration is found, we should return all items - return resource; - } - }catch (final Exception e) { - e.printStackTrace(); - } - } - } - } finally { - postFindResourcesCalls.get().remove(callKey); - } - - return null; - } + private final ThreadLocal> postFindClassCalls = new ThreadLocal>() { + + @Override + protected Set initialValue() { + return new HashSet<>(); + } + }; + + private final ThreadLocal> postFindResourceCalls = new ThreadLocal>() { + + @Override + protected Set initialValue() { + return new HashSet<>(); + } + }; + + private final ThreadLocal> postFindResourcesCalls = new ThreadLocal>() { + + @Override + protected Set initialValue() { + return new HashSet<>(); + } + }; + + private final ISupplementerRegistry supplementerRegistry; + + /** + * Create the hook instance for broaden the visibility according to the + * supplementing mechansism. + * + * @param supplementerRegistry The supplementer registry to be used by this hook + * for information retrieval which bundles are + * supplemented by which other bundles (needs to not + * be null) + */ + public WeavingLoaderDelegateHook(final ISupplementerRegistry supplementerRegistry) { + this.supplementerRegistry = supplementerRegistry; + } + + /** + * + * @see org.eclipse.osgi.internal.hookregistry.ClassLoaderHook#postFindClass(java.lang.String, + * org.eclipse.osgi.internal.loader.ModuleClassLoader) + */ + @Override + public Class postFindClass(final String name, final ModuleClassLoader classLoader) + throws ClassNotFoundException { + final long bundleID = classLoader.getBundle().getBundleId(); + + final String callKey = bundleID + name; + if (postFindClassCalls.get().contains(callKey)) { + return null; + } + + postFindClassCalls.get().add(callKey); + try { + final Supplementer[] supplementers = supplementerRegistry.getSupplementers(bundleID); + if (supplementers != null) { + for (Supplementer supplementer : supplementers) { + try { + final Bundle bundle = supplementer.getSupplementerHost(); + if (bundle.getState() != Bundle.UNINSTALLED) { + final Class clazz = bundle.loadClass(name); + if (clazz != null) { + return clazz; + } + } + } catch (final ClassNotFoundException e) { + } + } + } + } finally { + postFindClassCalls.get().remove(callKey); + } + + return null; + } + + /** + * + * @see org.eclipse.osgi.internal.hookregistry.ClassLoaderHook#postFindResource(java.lang.String, + * org.eclipse.osgi.internal.loader.ModuleClassLoader) + */ + @Override + public URL postFindResource(final String name, final ModuleClassLoader classLoader) throws FileNotFoundException { + final long bundleID = classLoader.getBundle().getBundleId(); + + final String callKey = bundleID + name; + if (postFindResourceCalls.get().contains(callKey)) { + return null; + } + + postFindResourceCalls.get().add(callKey); + try { + final Supplementer[] supplementers = supplementerRegistry.getSupplementers(bundleID); + if (supplementers != null) { + for (Supplementer supplementer : supplementers) { + try { + final URL resource = supplementer.getSupplementerHost().getResource(name); + if (resource != null) { + return resource; + } + } catch (final Exception e) { + e.printStackTrace(); + } + } + } + } finally { + postFindResourceCalls.get().remove(callKey); + } + + return null; + } + + /** + * + * @see org.eclipse.osgi.internal.hookregistry.ClassLoaderHook#postFindResources(java.lang.String, + * org.eclipse.osgi.internal.loader.ModuleClassLoader) + */ + @Override + public Enumeration postFindResources(final String name, final ModuleClassLoader classLoader) + throws FileNotFoundException { + final long bundleID = classLoader.getBundle().getBundleId(); + + final String callKey = bundleID + name; + if (postFindResourcesCalls.get().contains(callKey)) { + return null; + } + + postFindResourcesCalls.get().add(callKey); + try { + final Supplementer[] supplementers = supplementerRegistry.getSupplementers(bundleID); + if (supplementers != null) { + for (Supplementer supplementer : supplementers) { + try { + final Enumeration resource = supplementer.getSupplementerHost().getResources(name); + if (resource != null) { + // TODO: if more than one enumeration is found, we should return all items + return resource; + } + } catch (final Exception e) { + e.printStackTrace(); + } + } + } + } finally { + postFindResourcesCalls.get().remove(callKey); + } + + return null; + } }