connectivity/com.nokia.carbide.remoteConnections/src/com/nokia/carbide/remoteconnections/RemoteConnectionsActivator.java
branchRCL_2_4
changeset 857 d66843399035
parent 421 7bf2b8a16445
child 887 f3e6336d8edb
--- a/connectivity/com.nokia.carbide.remoteConnections/src/com/nokia/carbide/remoteconnections/RemoteConnectionsActivator.java	Mon Feb 01 12:38:42 2010 -0600
+++ b/connectivity/com.nokia.carbide.remoteConnections/src/com/nokia/carbide/remoteconnections/RemoteConnectionsActivator.java	Mon Feb 01 13:27:18 2010 -0600
@@ -16,18 +16,34 @@
 */
 package com.nokia.carbide.remoteconnections;
 
-import com.nokia.carbide.remoteconnections.interfaces.IConnectionTypeProvider;
-import com.nokia.carbide.remoteconnections.interfaces.IConnectionsManager;
-import com.nokia.carbide.remoteconnections.internal.registry.Registry;
-import com.nokia.cpp.internal.api.utils.core.Logging;
+import java.util.ArrayList;
+import java.util.Collection;
+import java.util.HashMap;
+import java.util.Map;
+import java.util.Map.Entry;
 
-import org.eclipse.core.net.proxy.IProxyService;
+import org.eclipse.core.runtime.CoreException;
+import org.eclipse.core.runtime.IConfigurationElement;
+import org.eclipse.core.runtime.IStatus;
+import org.eclipse.core.runtime.Platform;
+import org.eclipse.core.runtime.preferences.InstanceScope;
 import org.eclipse.jface.resource.ImageDescriptor;
+import org.eclipse.jface.viewers.IFilter;
 import org.eclipse.swt.widgets.Control;
 import org.eclipse.ui.PlatformUI;
 import org.eclipse.ui.plugin.AbstractUIPlugin;
 import org.osgi.framework.BundleContext;
-import org.osgi.util.tracker.ServiceTracker;
+import org.osgi.service.prefs.BackingStoreException;
+
+import com.nokia.carbide.remoteconnections.interfaces.IConnectionTypeProvider;
+import com.nokia.carbide.remoteconnections.interfaces.IConnectionsManager;
+import com.nokia.carbide.remoteconnections.internal.api.IDeviceDiscoveryAgent;
+import com.nokia.carbide.remoteconnections.internal.api.IDeviceDiscoveryAgent.IPrerequisiteStatus;
+import com.nokia.carbide.remoteconnections.internal.registry.Registry;
+import com.nokia.carbide.remoteconnections.internal.ui.DeviceDiscoveryPrequisiteErrorDialog;
+import com.nokia.cpp.internal.api.utils.core.Logging;
+import com.nokia.cpp.internal.api.utils.ui.RunRunnableWhenWorkbenchVisibleJob;
+import com.nokia.cpp.internal.api.utils.ui.WorkbenchUtils;
 
 /**
  * The activator class controls the plug-in life cycle
@@ -37,51 +53,84 @@
 	// The plug-in ID
 	public static final String PLUGIN_ID = "com.nokia.carbide.remoteConnections"; //$NON-NLS-1$
 
+	private static final String DISCOVERY_AGENT_EXTENSION = PLUGIN_ID + ".deviceDiscoveryAgent"; //$NON-NLS-1$
+
 	// The shared instance
 	private static RemoteConnectionsActivator plugin;
-	
-	// A service tracker for the proxy service
-	private ServiceTracker proxyServiceTracker;
-	
+
+	private Collection<IDeviceDiscoveryAgent> discoveryAgents;
+	private static final String IGNORE_AGENT_LOAD_ERRORS_KEY = "ignoreAgentLoadErrors"; //$NON-NLS-1$
+
 	/**
 	 * The constructor
 	 */
 	public RemoteConnectionsActivator() {
 	}
-
  
- 	/**
-	 * Returns IProxyService.
-	 * @deprecated
-	 */
-	public IProxyService getProxyService() {
-		return (IProxyService) proxyServiceTracker.getService();
-	}
-	
-	/*
-	 * (non-Javadoc)
-	 * @see org.eclipse.ui.plugin.AbstractUIPlugin#start(org.osgi.framework.BundleContext)
-	 */
 	public void start(BundleContext context) throws Exception {
 		super.start(context);
 		plugin = this;
 		Registry instance = Registry.instance();
 		instance.loadExtensions();
 		instance.loadConnections();
-		this.proxyServiceTracker = new ServiceTracker(context, IProxyService.class.getName(), null);
-		this.proxyServiceTracker.open();
+
+		RunRunnableWhenWorkbenchVisibleJob.start(new Runnable() {
+			public void run() {
+				if (!ignoreAgentLoadErrors())
+					checkPrerequisites();
+
+				loadAndStartDeviceDiscoveryAgents();
+			}
+		});
+	}
+
+	private boolean ignoreAgentLoadErrors() {
+		return getPreferenceStore().getBoolean(IGNORE_AGENT_LOAD_ERRORS_KEY);
 	}
 
-	/*
-	 * (non-Javadoc)
-	 * @see org.eclipse.ui.plugin.AbstractUIPlugin#stop(org.osgi.framework.BundleContext)
-	 */
+	private void checkPrerequisites() {
+		final Map<IDeviceDiscoveryAgent, IPrerequisiteStatus> agentToStatusMap = 
+			new HashMap<IDeviceDiscoveryAgent, IPrerequisiteStatus>();
+		
+		// load the extensions just to check statuses
+		Collection<IDeviceDiscoveryAgent> agents = new ArrayList<IDeviceDiscoveryAgent>();
+		loadExtensions(DISCOVERY_AGENT_EXTENSION, null, agents, null);
+		
+		for (IDeviceDiscoveryAgent agent : agents) {
+			IPrerequisiteStatus status = agent.getPrerequisiteStatus();
+			if (!status.isOK()) {
+				agentToStatusMap.put(agent, status);
+			}
+		}
+
+		if (!agentToStatusMap.isEmpty()) {
+			DeviceDiscoveryPrequisiteErrorDialog dlg = new DeviceDiscoveryPrequisiteErrorDialog(WorkbenchUtils.getSafeShell());
+			for (Entry<IDeviceDiscoveryAgent, IPrerequisiteStatus> entry : agentToStatusMap.entrySet()) {
+				IDeviceDiscoveryAgent agent = entry.getKey();
+				IPrerequisiteStatus status = entry.getValue();
+				dlg.addAgentData(agent.getDisplayName(), status.getErrorText(), status.getURL());
+			}
+			dlg.open();
+			if (dlg.isDontAskAgainChecked())
+				storeIgnoreAgentLoadErrorsFlag();
+		}	
+	}
+
 	public void stop(BundleContext context) throws Exception {
+		stopDeviceDiscoveryAgents();
 		Registry.instance().storeConnections();
 		Registry.instance().disposeConnections();
 		plugin = null;
 		super.stop(context);
-		this.proxyServiceTracker.close();
+	}
+
+	private void storeIgnoreAgentLoadErrorsFlag() {
+		getPreferenceStore().setValue(IGNORE_AGENT_LOAD_ERRORS_KEY, true);
+		try {
+			new InstanceScope().getNode(PLUGIN_ID).flush();
+		} catch (BackingStoreException e) {
+			logError(e);
+		}
 	}
 
 	/**
@@ -131,4 +180,63 @@
 	public static void setHelp(Control control, String id) {
 		PlatformUI.getWorkbench().getHelpSystem().setHelp(control, PLUGIN_ID + id);		 //$NON-NLS-1$
 	}
+	
+	private void loadAndStartDeviceDiscoveryAgents() {
+		String loadError = Messages.getString("RemoteConnectionsActivator.DiscoveryAgentLoadError"); //$NON-NLS-1$
+		discoveryAgents = new ArrayList<IDeviceDiscoveryAgent>();
+		loadExtensions(DISCOVERY_AGENT_EXTENSION, loadError, discoveryAgents, new IFilter() {
+			public boolean select(Object toTest) {
+				if (toTest instanceof IDeviceDiscoveryAgent) {
+					try {
+						((IDeviceDiscoveryAgent) toTest).start();
+						return true;
+					} catch (Throwable e) {
+						// since we launch arbitrary code, catch any exception to prevent killing the view
+						logError(e);
+					}
+				}
+				return false;
+			}
+		});
+		
+	}
+
+	private void stopDeviceDiscoveryAgents() {
+		for (IDeviceDiscoveryAgent agent : discoveryAgents) {
+			try {
+				agent.stop();
+			} catch (CoreException e) {
+				logError(e);
+			}
+		}
+		
+	}
+
+	public static void log(String errorStr, Throwable t) {
+		RemoteConnectionsActivator p = RemoteConnectionsActivator.getDefault();
+		String error = errorStr;
+		if (t != null) {
+			error += " : " + t.getLocalizedMessage(); //$NON-NLS-1$
+		}
+		Logging.log(p, Logging.newStatus(p, IStatus.ERROR, error));
+		if (t instanceof CoreException)
+			Logging.log(p, ((CoreException) t).getStatus());
+	}
+	
+	@SuppressWarnings("unchecked")
+	public static <T> void loadExtensions(String extensionId, String loadError, Collection<T> extensionObjects, IFilter filter) {
+		IConfigurationElement[] elements = 
+			Platform.getExtensionRegistry().getConfigurationElementsFor(extensionId);
+		for (IConfigurationElement element : elements) {
+			try {
+				T extObject = (T) element.createExecutableExtension("class"); //$NON-NLS-1$
+				if (filter == null || filter.select(extObject))
+					extensionObjects.add(extObject);
+			} 
+			catch (CoreException e) {
+				if (loadError != null)
+					RemoteConnectionsActivator.log(loadError, e);
+			}
+		}
+	}
 }