Bug 406518 - migrate OT/Equinox to the standard OSGi WeavingHook
- migrate from DS to manual registration at start level 1
diff --git a/plugins/org.eclipse.objectteams.otequinox/META-INF/MANIFEST.MF b/plugins/org.eclipse.objectteams.otequinox/META-INF/MANIFEST.MF
index d67ec69..e12d8be 100644
--- a/plugins/org.eclipse.objectteams.otequinox/META-INF/MANIFEST.MF
+++ b/plugins/org.eclipse.objectteams.otequinox/META-INF/MANIFEST.MF
@@ -13,5 +13,4 @@
 Import-Package: org.osgi.service.component;version="1.2.0"
 Bundle-RequiredExecutionEnvironment: JavaSE-1.7
 Bundle-ActivationPolicy: lazy
-Service-Component: OSGI-INF/weavinghook.xml
 Export-Package: org.eclipse.objectteams.otequinox
diff --git a/plugins/org.eclipse.objectteams.otequinox/META-INF/p2.inf b/plugins/org.eclipse.objectteams.otequinox/META-INF/p2.inf
new file mode 100644
index 0000000..f6e8e6a
--- /dev/null
+++ b/plugins/org.eclipse.objectteams.otequinox/META-INF/p2.inf
@@ -0,0 +1,7 @@
+instructions.configure=\
+	org.eclipse.equinox.p2.touchpoint.eclipse.setStartLevel(startLevel:1); \
+    org.eclipse.equinox.p2.touchpoint.eclipse.markStarted(started:true);
+instructions.unconfigure=\
+	org.eclipse.equinox.p2.touchpoint.eclipse.setStartLevel(startLevel:-1); \
+    org.eclipse.equinox.p2.touchpoint.eclipse.markStarted(started:false);
+    
\ No newline at end of file
diff --git a/plugins/org.eclipse.objectteams.otequinox/OSGI-INF/weavinghook.xml b/plugins/org.eclipse.objectteams.otequinox/OSGI-INF/weavinghook.xml
deleted file mode 100644
index 9253e26..0000000
--- a/plugins/org.eclipse.objectteams.otequinox/OSGI-INF/weavinghook.xml
+++ /dev/null
@@ -1,8 +0,0 @@
-<?xml version="1.0"?>
-<component name="weavinghook">
-	<implementation class="org.eclipse.objectteams.internal.osgi.weaving.OTWeavingHook"/>
-	<service>
-		<provide interface="org.osgi.framework.hooks.weaving.WeavingHook"/>
-		<provide interface="org.osgi.framework.hooks.weaving.WovenClassListener"/>
-	</service>
-</component>
\ No newline at end of file
diff --git a/plugins/org.eclipse.objectteams.otequinox/build.properties b/plugins/org.eclipse.objectteams.otequinox/build.properties
index 63628dd..9034c45 100644
--- a/plugins/org.eclipse.objectteams.otequinox/build.properties
+++ b/plugins/org.eclipse.objectteams.otequinox/build.properties
@@ -8,7 +8,6 @@
                ote_32n.png,\
                plugin.properties,\
                about.html,\
-               OSGI-INF/,\
                bcelpatch/
 src.includes = schema/,\
                about.ini
diff --git a/plugins/org.eclipse.objectteams.otequinox/src/org/eclipse/objectteams/internal/osgi/weaving/AspectBindingRegistry.java b/plugins/org.eclipse.objectteams.otequinox/src/org/eclipse/objectteams/internal/osgi/weaving/AspectBindingRegistry.java
index b01b500..eaecfc2 100644
--- a/plugins/org.eclipse.objectteams.otequinox/src/org/eclipse/objectteams/internal/osgi/weaving/AspectBindingRegistry.java
+++ b/plugins/org.eclipse.objectteams.otequinox/src/org/eclipse/objectteams/internal/osgi/weaving/AspectBindingRegistry.java
@@ -16,7 +16,6 @@
  **********************************************************************/
 package org.eclipse.objectteams.internal.osgi.weaving;
 
-import static org.eclipse.objectteams.otequinox.TransformerPlugin.log;
 import static org.eclipse.objectteams.otequinox.Constants.ACTIVATION;
 import static org.eclipse.objectteams.otequinox.Constants.ASPECT_BINDING_EXTPOINT_ID;
 import static org.eclipse.objectteams.otequinox.Constants.BASE_PLUGIN;
@@ -27,6 +26,7 @@
 import static org.eclipse.objectteams.otequinox.Constants.SUPERCLASS;
 import static org.eclipse.objectteams.otequinox.Constants.TEAM;
 import static org.eclipse.objectteams.otequinox.Constants.TRANSFORMER_PLUGIN_ID;
+import static org.eclipse.objectteams.otequinox.TransformerPlugin.log;
 
 import java.util.ArrayList;
 import java.util.HashMap;
@@ -36,8 +36,8 @@
 import java.util.Set;
 
 import org.eclipse.core.runtime.IConfigurationElement;
+import org.eclipse.core.runtime.IExtensionRegistry;
 import org.eclipse.core.runtime.IStatus;
-import org.eclipse.core.runtime.RegistryFactory;
 import org.eclipse.jdt.annotation.NonNull;
 import org.eclipse.jdt.annotation.NonNullByDefault;
 import org.eclipse.jdt.annotation.Nullable;
@@ -77,11 +77,12 @@
 
 	/* Load extensions for org.eclipse.objectteams.otequinox.aspectBindings and check aspect permissions. */
 	public void loadAspectBindings(
+			IExtensionRegistry extensionRegistry,
 			@SuppressWarnings("deprecation") @Nullable org.osgi.service.packageadmin.PackageAdmin packageAdmin,
 			OTWeavingHook hook) 
 	{
-		IConfigurationElement[] aspectBindingConfigs = RegistryFactory.getRegistry().getConfigurationElementsFor(
-				TRANSFORMER_PLUGIN_ID, ASPECT_BINDING_EXTPOINT_ID);
+		IConfigurationElement[] aspectBindingConfigs = extensionRegistry
+				.getConfigurationElementsFor(TRANSFORMER_PLUGIN_ID, ASPECT_BINDING_EXTPOINT_ID);
 		Map<String, Set<TeamBinding>> teamLookup = new HashMap<>();
 		Map<String, BaseBundle> baseBundleLookup = new HashMap<>();
 		AspectBinding[] bindings = new AspectBinding[aspectBindingConfigs.length];
diff --git a/plugins/org.eclipse.objectteams.otequinox/src/org/eclipse/objectteams/internal/osgi/weaving/OTWeavingHook.java b/plugins/org.eclipse.objectteams.otequinox/src/org/eclipse/objectteams/internal/osgi/weaving/OTWeavingHook.java
index 7536630..d46edcc 100644
--- a/plugins/org.eclipse.objectteams.otequinox/src/org/eclipse/objectteams/internal/osgi/weaving/OTWeavingHook.java
+++ b/plugins/org.eclipse.objectteams.otequinox/src/org/eclipse/objectteams/internal/osgi/weaving/OTWeavingHook.java
@@ -26,6 +26,7 @@
 import java.util.List;
 import java.util.Set;
 
+import org.eclipse.core.runtime.IExtensionRegistry;
 import org.eclipse.core.runtime.IStatus;
 import org.eclipse.jdt.annotation.NonNull;
 import org.eclipse.jdt.annotation.Nullable;
@@ -42,7 +43,6 @@
 import org.osgi.framework.namespace.PackageNamespace;
 import org.osgi.framework.wiring.BundleWiring;
 import org.osgi.resource.Wire;
-import org.osgi.service.component.ComponentContext;
 
 
 /**
@@ -75,16 +75,16 @@
 	private @NonNull List<WaitingTeamRecord> deferredTeams = new ArrayList<>();
 
 	
-	/** Call-back from DS framework. */
-	public void activate(ComponentContext context) {
-		loadAspectBindingRegistry(context.getBundleContext());
+	/** Call-back once the extension registry is up and running. */
+	public void activate(BundleContext bundleContext, ServiceReference<IExtensionRegistry> serviceReference) {
+		loadAspectBindingRegistry(bundleContext, serviceReference);
 		TransformerPlugin.getDefault().registerAspectBindingRegistry(this.aspectBindingRegistry);
 	}
 
 	// ====== Aspect Binding: ======
-	
+
 	@SuppressWarnings("deprecation")
-	private void loadAspectBindingRegistry(BundleContext context) {
+	private void loadAspectBindingRegistry(BundleContext context, ServiceReference<IExtensionRegistry> serviceReference) {
 		org.osgi.service.packageadmin.PackageAdmin packageAdmin = null;
 		
 		ServiceReference<?> ref= context.getServiceReference(org.osgi.service.packageadmin.PackageAdmin.class.getName());
@@ -93,7 +93,11 @@
 		else
 			log(IStatus.ERROR, "Failed to load PackageAdmin service. Will not be able to handle fragments.");
 
-		aspectBindingRegistry.loadAspectBindings(packageAdmin, this);
+		IExtensionRegistry extensionRegistry = context.getService(serviceReference);
+		if (extensionRegistry == null)
+			log(IStatus.ERROR, "Failed to acquire ExtensionRegistry service, cannot load aspect bindings.");
+		else
+			aspectBindingRegistry.loadAspectBindings(extensionRegistry, packageAdmin, this);
 	}
 
 	// ====== Base Bundle Trip Wires: ======
diff --git a/plugins/org.eclipse.objectteams.otequinox/src/org/eclipse/objectteams/otequinox/TransformerPlugin.java b/plugins/org.eclipse.objectteams.otequinox/src/org/eclipse/objectteams/otequinox/TransformerPlugin.java
index 2871c68..8dc45f9 100644
--- a/plugins/org.eclipse.objectteams.otequinox/src/org/eclipse/objectteams/otequinox/TransformerPlugin.java
+++ b/plugins/org.eclipse.objectteams.otequinox/src/org/eclipse/objectteams/otequinox/TransformerPlugin.java
@@ -5,17 +5,24 @@
 import java.util.ArrayList;
 import java.util.List;
 
+import org.eclipse.core.runtime.IExtensionRegistry;
 import org.eclipse.core.runtime.ILog;
 import org.eclipse.core.runtime.IStatus;
 import org.eclipse.core.runtime.Status;
 import org.eclipse.jdt.annotation.NonNull;
 import org.eclipse.objectteams.internal.osgi.weaving.AspectBinding;
 import org.eclipse.objectteams.internal.osgi.weaving.AspectBindingRegistry;
+import org.eclipse.objectteams.internal.osgi.weaving.OTWeavingHook;
 import org.eclipse.objectteams.otre.ClassLoaderAccess;
 import org.objectteams.Team;
 import org.osgi.framework.Bundle;
 import org.osgi.framework.BundleActivator;
 import org.osgi.framework.BundleContext;
+import org.osgi.framework.InvalidSyntaxException;
+import org.osgi.framework.ServiceEvent;
+import org.osgi.framework.ServiceListener;
+import org.osgi.framework.hooks.weaving.WeavingHook;
+import org.osgi.framework.hooks.weaving.WovenClassListener;
 
 public class TransformerPlugin implements BundleActivator, IAspectRegistry {
 
@@ -35,13 +42,33 @@
 	 * (non-Javadoc)
 	 * @see org.osgi.framework.BundleActivator#start(org.osgi.framework.BundleContext)
 	 */
-	public void start(BundleContext bundleContext) throws Exception {
+	public void start(final BundleContext bundleContext) throws Exception {
 		plugin = this;
 		TransformerPlugin.context = bundleContext;
 		
 		acquireLog(bundleContext);
 	
 		OTREInit();
+		
+		// register our weaving service:
+		final OTWeavingHook otWeavingHook = new OTWeavingHook();
+		context.registerService(new String[] { WeavingHook.class.getName(), WovenClassListener.class.getName() },
+				otWeavingHook, null);
+		
+		// but wait until the extension registry is available for reading aspectBindings:
+		try {
+			context.addServiceListener(
+				new ServiceListener() { 
+					public void serviceChanged(ServiceEvent event) {
+						if(event.getType() == ServiceEvent.REGISTERED)
+							otWeavingHook.activate(bundleContext, context.getServiceReference(IExtensionRegistry.class));
+					}
+				},
+				"(objectclass="+IExtensionRegistry.class.getName()+")"); //$NON-NLS-1$ //$NON-NLS-2$
+		}
+		catch (InvalidSyntaxException ex) {
+			log(ex, "Failed to register service listener");
+		}
 	}
 
 	@SuppressWarnings("restriction")