Bug 406518 - migrate OT/Equinox to the standard OSGi WeavingHook
- workaround for bug 253244 using a new system fragment
diff --git a/plugins/org.eclipse.objectteams.otequinox.bug253244workaround/.classpath b/plugins/org.eclipse.objectteams.otequinox.bug253244workaround/.classpath
new file mode 100644
index 0000000..098194c
--- /dev/null
+++ b/plugins/org.eclipse.objectteams.otequinox.bug253244workaround/.classpath
@@ -0,0 +1,7 @@
+<?xml version="1.0" encoding="UTF-8"?>
+<classpath>
+	<classpathentry kind="con" path="org.eclipse.jdt.launching.JRE_CONTAINER/org.eclipse.jdt.internal.debug.ui.launcher.StandardVMType/JavaSE-1.7"/>
+	<classpathentry kind="con" path="org.eclipse.pde.core.requiredPlugins"/>
+	<classpathentry kind="src" path="src"/>
+	<classpathentry kind="output" path="bin"/>
+</classpath>
diff --git a/plugins/org.eclipse.objectteams.otequinox.bug253244workaround/.project b/plugins/org.eclipse.objectteams.otequinox.bug253244workaround/.project
new file mode 100644
index 0000000..eab3f67
--- /dev/null
+++ b/plugins/org.eclipse.objectteams.otequinox.bug253244workaround/.project
@@ -0,0 +1,28 @@
+<?xml version="1.0" encoding="UTF-8"?>
+<projectDescription>
+	<name>org.eclipse.objectteams.otequinox.bug253244workaround</name>
+	<comment></comment>
+	<projects>
+	</projects>
+	<buildSpec>
+		<buildCommand>
+			<name>org.eclipse.jdt.core.javabuilder</name>
+			<arguments>
+			</arguments>
+		</buildCommand>
+		<buildCommand>
+			<name>org.eclipse.pde.ManifestBuilder</name>
+			<arguments>
+			</arguments>
+		</buildCommand>
+		<buildCommand>
+			<name>org.eclipse.pde.SchemaBuilder</name>
+			<arguments>
+			</arguments>
+		</buildCommand>
+	</buildSpec>
+	<natures>
+		<nature>org.eclipse.pde.PluginNature</nature>
+		<nature>org.eclipse.jdt.core.javanature</nature>
+	</natures>
+</projectDescription>
diff --git a/plugins/org.eclipse.objectteams.otequinox.bug253244workaround/.settings/org.eclipse.jdt.core.prefs b/plugins/org.eclipse.objectteams.otequinox.bug253244workaround/.settings/org.eclipse.jdt.core.prefs
new file mode 100644
index 0000000..14d39a9
--- /dev/null
+++ b/plugins/org.eclipse.objectteams.otequinox.bug253244workaround/.settings/org.eclipse.jdt.core.prefs
@@ -0,0 +1,8 @@
+eclipse.preferences.version=1
+org.eclipse.jdt.core.compiler.codegen.inlineJsrBytecode=enabled
+org.eclipse.jdt.core.compiler.codegen.targetPlatform=1.7
+org.eclipse.jdt.core.compiler.compliance=1.7
+org.eclipse.jdt.core.compiler.problem.assertIdentifier=error
+org.eclipse.jdt.core.compiler.problem.enumIdentifier=error
+org.eclipse.jdt.core.compiler.source=1.7
+org.eclipse.objectteams.otdt.compiler.option.pure_java=enabled
diff --git a/plugins/org.eclipse.objectteams.otequinox.bug253244workaround/META-INF/MANIFEST.MF b/plugins/org.eclipse.objectteams.otequinox.bug253244workaround/META-INF/MANIFEST.MF
new file mode 100644
index 0000000..51f2b8d
--- /dev/null
+++ b/plugins/org.eclipse.objectteams.otequinox.bug253244workaround/META-INF/MANIFEST.MF
@@ -0,0 +1,9 @@
+Manifest-Version: 1.0
+Bundle-ManifestVersion: 2
+Bundle-Name: %pluginName
+Bundle-SymbolicName: org.eclipse.objectteams.otequinox.bug253244workaround
+Bundle-Version: 1.0.0.qualifier
+Bundle-Vendor: %providerName
+ExtensionBundle-Activator: org.eclipse.objectteams.otequinox.bug253244workaround.Activator
+Fragment-Host: org.eclipse.osgi;bundle-version="[3.10.0,4.0.0)"
+Bundle-RequiredExecutionEnvironment: JavaSE-1.7
diff --git a/plugins/org.eclipse.objectteams.otequinox.bug253244workaround/about.html b/plugins/org.eclipse.objectteams.otequinox.bug253244workaround/about.html
new file mode 100644
index 0000000..66ef6f6
--- /dev/null
+++ b/plugins/org.eclipse.objectteams.otequinox.bug253244workaround/about.html
@@ -0,0 +1,28 @@
+<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN"
+    "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
+<html xmlns="http://www.w3.org/1999/xhtml">
+<head>
+<meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1"/>
+<title>About</title>
+</head>
+<body lang="EN-US">
+<h2>About This Content</h2>
+ 
+<p>June 15, 2010</p>	
+<h3>License</h3>
+
+<p>The Eclipse Foundation makes available all content in this plug-in (&quot;Content&quot;).  Unless otherwise 
+indicated below, the Content is provided to you under the terms and conditions of the
+Eclipse Public License Version 1.0 (&quot;EPL&quot;).  A copy of the EPL is available 
+at <a href="http://www.eclipse.org/legal/epl-v10.html">http://www.eclipse.org/legal/epl-v10.html</a>.
+For purposes of the EPL, &quot;Program&quot; will mean the Content.</p>
+
+<p>If you did not receive this Content directly from the Eclipse Foundation, the Content is 
+being redistributed by another party (&quot;Redistributor&quot;) and different terms and conditions may
+apply to your use of any object code in the Content.  Check the Redistributor's license that was 
+provided with the Content.  If no such license exists, contact the Redistributor.  Unless otherwise
+indicated below, the terms and conditions of the EPL still apply to any source code in the Content
+and such source code may be obtained at <a href="http://www.eclipse.org">http://www.eclipse.org</a>.</p>
+
+</body>
+</html>
\ No newline at end of file
diff --git a/plugins/org.eclipse.objectteams.otequinox.bug253244workaround/build.properties b/plugins/org.eclipse.objectteams.otequinox.bug253244workaround/build.properties
new file mode 100644
index 0000000..0d18b31
--- /dev/null
+++ b/plugins/org.eclipse.objectteams.otequinox.bug253244workaround/build.properties
@@ -0,0 +1,7 @@
+source.. = src/
+output.. = bin/
+bin.includes = META-INF/,\
+               .,\
+               about.html,\
+               plugin.properties
+javacProjectSettings=true
diff --git a/plugins/org.eclipse.objectteams.otequinox.bug253244workaround/plugin.properties b/plugins/org.eclipse.objectteams.otequinox.bug253244workaround/plugin.properties
new file mode 100644
index 0000000..e33b4e3
--- /dev/null
+++ b/plugins/org.eclipse.objectteams.otequinox.bug253244workaround/plugin.properties
@@ -0,0 +1,2 @@
+pluginName=Workaround for Bug253244 in p2
+providerName=Eclipse.org - Object Teams
diff --git a/plugins/org.eclipse.objectteams.otequinox.bug253244workaround/src/org/eclipse/objectteams/otequinox/bug253244workaround/Activator.java b/plugins/org.eclipse.objectteams.otequinox.bug253244workaround/src/org/eclipse/objectteams/otequinox/bug253244workaround/Activator.java
new file mode 100644
index 0000000..7413471
--- /dev/null
+++ b/plugins/org.eclipse.objectteams.otequinox.bug253244workaround/src/org/eclipse/objectteams/otequinox/bug253244workaround/Activator.java
@@ -0,0 +1,58 @@
+/**********************************************************************
+ * This file is part of "Object Teams Development Tooling"-Software
+ * 
+ * Copyright 2013 GK Software AG
+ *  
+ * All rights reserved. This program and the accompanying materials
+ * are made available under the terms of the Eclipse Public License v1.0
+ * which accompanies this distribution, and is available at
+ * http://www.eclipse.org/legal/epl-v10.html
+ * 
+ * Please visit http://www.objectteams.org for updates and contact.
+ * 
+ * Contributors:
+ * 	Stephan Herrmann - Initial API and implementation
+ **********************************************************************/
+package org.eclipse.objectteams.otequinox.bug253244workaround;
+
+import org.osgi.framework.Bundle;
+import org.osgi.framework.BundleActivator;
+import org.osgi.framework.BundleContext;
+import org.osgi.framework.ServiceReference;
+import org.osgi.framework.startlevel.BundleStartLevel;
+import org.osgi.service.packageadmin.PackageAdmin;
+
+/**
+ * The sole purpose of this fragment is to work around
+ * https://bugs.eclipse.org/bugs/show_bug.cgi?id=253244 in p2.
+ * 
+ * We need to set the start level of org.eclipse.objectteams.otequinox to 1
+ * so that weaving is activate as early as possible.
+ */
+@SuppressWarnings("deprecation") // useing deprecated class PackageAdmin
+public class Activator implements BundleActivator {
+
+	@Override
+	public void start(BundleContext context) throws Exception {
+		PackageAdmin packageAdmin = null;
+		
+		ServiceReference<?> ref= context.getServiceReference(PackageAdmin.class.getName());
+		if (ref!=null) {
+			packageAdmin = (PackageAdmin)context.getService(ref);
+			Bundle[] bundles = packageAdmin.getBundles("org.eclipse.objectteams.otequinox", "2.3.0");
+			if (bundles != null && bundles.length > 0) {
+				BundleStartLevel startLevel = bundles[0].adapt(BundleStartLevel.class);
+				if (startLevel.getStartLevel() < 1) {
+					startLevel.setStartLevel(1);
+				}
+				// this would cause lock problems (and is successfully handled already via p2.inf):
+//				bundles[0].start();
+			}
+		}
+	}
+
+	@Override
+	public void stop(BundleContext context) throws Exception {
+		// nop
+	}
+}
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 9a9217a..b091441 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
@@ -1,3 +1,18 @@
+/**********************************************************************
+ * This file is part of "Object Teams Development Tooling"-Software
+ * 
+ * Copyright 2013 GK Software AG
+ *  
+ * All rights reserved. This program and the accompanying materials
+ * are made available under the terms of the Eclipse Public License v1.0
+ * which accompanies this distribution, and is available at
+ * http://www.eclipse.org/legal/epl-v10.html
+ * 
+ * Please visit http://www.objectteams.org for updates and contact.
+ * 
+ * Contributors:
+ * 	Stephan Herrmann - Initial API and implementation
+ **********************************************************************/
 package org.eclipse.objectteams.otequinox;
 
 import static org.eclipse.objectteams.otequinox.Constants.TRANSFORMER_PLUGIN_ID;
@@ -21,6 +36,7 @@
 import org.osgi.framework.InvalidSyntaxException;
 import org.osgi.framework.ServiceEvent;
 import org.osgi.framework.ServiceListener;
+import org.osgi.framework.ServiceReference;
 import org.osgi.framework.hooks.weaving.WeavingHook;
 import org.osgi.framework.hooks.weaving.WovenClassListener;
 
@@ -57,14 +73,19 @@
 		
 		// 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$
+			ServiceReference<IExtensionRegistry> reference = context.getServiceReference(IExtensionRegistry.class);
+			if (reference != null) {
+				otWeavingHook.activate(bundleContext, reference);
+			} else {
+				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");