Bug 395632 - Tracing UI bundle declarative service causes the workspace
prompt to not display
diff --git a/ui/org.eclipse.ui.trace/src/org/eclipse/ui/trace/internal/TracingStartupMonitor.java b/ui/org.eclipse.ui.trace/src/org/eclipse/ui/trace/internal/TracingStartupMonitor.java
new file mode 100644
index 0000000..ebb4530
--- /dev/null
+++ b/ui/org.eclipse.ui.trace/src/org/eclipse/ui/trace/internal/TracingStartupMonitor.java
@@ -0,0 +1,50 @@
+/*******************************************************************************
+ * Copyright (c) 2012 IBM Corporation and others.
+ * 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
+ * 
+ * Contributors:
+ *     IBM Corporation - initial API and implementation
+ *******************************************************************************/
+package org.eclipse.ui.trace.internal;
+
+import org.eclipse.osgi.service.runnable.StartupMonitor;
+import org.osgi.framework.ServiceRegistration;
+
+/**
+ * A {@link StartupMonitor} implementation for initializing the tracing preferences after the application has started.
+ */
+public class TracingStartupMonitor implements StartupMonitor {
+
+	private ServiceRegistration<StartupMonitor> registration;
+
+	/* (non-Javadoc)
+	 * @see org.eclipse.osgi.service.runnable.StartupMonitor#update()
+	 */
+	public void update() {
+
+		// empty implementation
+	}
+
+	/* (non-Javadoc)
+	 * @see org.eclipse.osgi.service.runnable.StartupMonitor#applicationRunning()
+	 */
+	public void applicationRunning() {
+
+		// bug 395632: The application is running now so it's safe to initialize the preferences
+		TracingUIActivator.getDefault().initPreferences();
+		// Unregister this service as its purpose is complete
+		registration.unregister();
+
+	}
+
+	/**
+	 * Set the service registration on this monitor so it can unregister itself after {@link #applicationRunning()}
+	 * @param registration the service registration returned when registering a {@link StartupMonitor} service or <code>null</code>
+	 */
+	public void setRegistration(ServiceRegistration<StartupMonitor> registration) {
+		this.registration = registration;
+	}
+}
\ No newline at end of file
diff --git a/ui/org.eclipse.ui.trace/src/org/eclipse/ui/trace/internal/TracingUIActivator.java b/ui/org.eclipse.ui.trace/src/org/eclipse/ui/trace/internal/TracingUIActivator.java
index 28f75bd..4892050 100644
--- a/ui/org.eclipse.ui.trace/src/org/eclipse/ui/trace/internal/TracingUIActivator.java
+++ b/ui/org.eclipse.ui.trace/src/org/eclipse/ui/trace/internal/TracingUIActivator.java
@@ -11,14 +11,13 @@
 package org.eclipse.ui.trace.internal;

 

 import java.io.File;

-import java.util.Hashtable;

-import java.util.Map;

-import org.eclipse.core.runtime.IStatus;

-import org.eclipse.core.runtime.Status;

-import org.eclipse.osgi.service.debug.DebugOptions;

+import java.util.*;

+import org.eclipse.core.runtime.*;

+import org.eclipse.osgi.service.runnable.StartupMonitor;

 import org.eclipse.ui.plugin.AbstractUIPlugin;

 import org.eclipse.ui.trace.internal.utils.*;

 import org.osgi.framework.BundleContext;

+import org.osgi.framework.ServiceRegistration;

 

 /**

  * The activator class controls the plug-in life cycle

@@ -54,20 +53,20 @@
 			// Set option so we know debug mode is set, not preferences

 			DebugOptionsHandler.setLaunchInDebugMode(true);

 

-		} else if (PreferenceHandler.isTracingEnabled()) {

-			// User has previously enabled tracing options

-			DebugOptionsHandler.setDebugEnabled(true);

-			DebugOptionsHandler.getDebugOptions().setFile(new File(PreferenceHandler.getFilePath()));

-			System.setProperty(TracingConstants.PROP_TRACE_SIZE_MAX, String.valueOf(PreferenceHandler.getMaxFileSize()));

-			System.setProperty(TracingConstants.PROP_TRACE_FILE_MAX, String.valueOf(PreferenceHandler.getMaxFileCount()));

-

-			Map<String, String> prefs = PreferenceHandler.getPreferenceProperties();

-			DebugOptionsHandler.getDebugOptions().setOptions(prefs);

+		} else {

+			// bug 395632: see if the instance location is defined.  if not then defer accessing

+			// the preferences until it is defined by being notified via the org.eclipse.osgi.service.runnable.StartupMonitor

+			// service

+			if (!Platform.getInstanceLocation().isSet()) {

+				// register a startup monitor to notify us when the application is running

+				final TracingStartupMonitor startupMonitor = new TracingStartupMonitor();

+				final Dictionary<String, ?> properties = new Hashtable<String, Object>(1);

+				ServiceRegistration<StartupMonitor> registration = context.registerService(StartupMonitor.class, startupMonitor, properties);

+				startupMonitor.setRegistration(registration);

+			} else {

+				this.initPreferences();

+			}

 		}

-

-		final Hashtable<String, String> props = new Hashtable<String, String>(4);

-		props.put(DebugOptions.LISTENER_SYMBOLICNAME, TracingConstants.BUNDLE_ID);

-

 	}

 

 	@Override

@@ -91,6 +90,23 @@
 		}

 	}

 

+	/**

+	 * Initialize the tracing preferences if tracing is enabled.

+	 */

+	protected final void initPreferences() {

+

+		if (PreferenceHandler.isTracingEnabled()) {

+			// User has previously enabled tracing options

+			DebugOptionsHandler.setDebugEnabled(true);

+			DebugOptionsHandler.getDebugOptions().setFile(new File(PreferenceHandler.getFilePath()));

+			System.setProperty(TracingConstants.PROP_TRACE_SIZE_MAX, String.valueOf(PreferenceHandler.getMaxFileSize()));

+			System.setProperty(TracingConstants.PROP_TRACE_FILE_MAX, String.valueOf(PreferenceHandler.getMaxFileCount()));

+

+			Map<String, String> prefs = PreferenceHandler.getPreferenceProperties();

+			DebugOptionsHandler.getDebugOptions().setOptions(prefs);

+		}

+	}

+

 	/** The shared instance */

 	private static TracingUIActivator plugin = null;