14 * Description: |
14 * Description: |
15 * |
15 * |
16 */ |
16 */ |
17 package com.nokia.carbide.remoteconnections; |
17 package com.nokia.carbide.remoteconnections; |
18 |
18 |
19 import com.nokia.carbide.remoteconnections.interfaces.IConnectionTypeProvider; |
19 import java.util.ArrayList; |
20 import com.nokia.carbide.remoteconnections.interfaces.IConnectionsManager; |
20 import java.util.Collection; |
21 import com.nokia.carbide.remoteconnections.internal.registry.Registry; |
21 import java.util.HashMap; |
22 import com.nokia.cpp.internal.api.utils.core.Logging; |
22 import java.util.Map; |
23 |
23 import java.util.Map.Entry; |
24 import org.eclipse.core.net.proxy.IProxyService; |
24 |
|
25 import org.eclipse.core.runtime.CoreException; |
|
26 import org.eclipse.core.runtime.IConfigurationElement; |
|
27 import org.eclipse.core.runtime.IStatus; |
|
28 import org.eclipse.core.runtime.Platform; |
|
29 import org.eclipse.core.runtime.preferences.InstanceScope; |
25 import org.eclipse.jface.resource.ImageDescriptor; |
30 import org.eclipse.jface.resource.ImageDescriptor; |
|
31 import org.eclipse.jface.viewers.IFilter; |
26 import org.eclipse.swt.widgets.Control; |
32 import org.eclipse.swt.widgets.Control; |
27 import org.eclipse.ui.PlatformUI; |
33 import org.eclipse.ui.PlatformUI; |
28 import org.eclipse.ui.plugin.AbstractUIPlugin; |
34 import org.eclipse.ui.plugin.AbstractUIPlugin; |
29 import org.osgi.framework.BundleContext; |
35 import org.osgi.framework.BundleContext; |
30 import org.osgi.util.tracker.ServiceTracker; |
36 import org.osgi.service.prefs.BackingStoreException; |
|
37 |
|
38 import com.nokia.carbide.remoteconnections.interfaces.IConnectionTypeProvider; |
|
39 import com.nokia.carbide.remoteconnections.interfaces.IConnectionsManager; |
|
40 import com.nokia.carbide.remoteconnections.internal.api.IDeviceDiscoveryAgent; |
|
41 import com.nokia.carbide.remoteconnections.internal.api.IDeviceDiscoveryAgent.IPrerequisiteStatus; |
|
42 import com.nokia.carbide.remoteconnections.internal.registry.Registry; |
|
43 import com.nokia.carbide.remoteconnections.internal.ui.DeviceDiscoveryPrequisiteErrorDialog; |
|
44 import com.nokia.cpp.internal.api.utils.core.Logging; |
|
45 import com.nokia.cpp.internal.api.utils.ui.RunRunnableWhenWorkbenchVisibleJob; |
|
46 import com.nokia.cpp.internal.api.utils.ui.WorkbenchUtils; |
31 |
47 |
32 /** |
48 /** |
33 * The activator class controls the plug-in life cycle |
49 * The activator class controls the plug-in life cycle |
34 */ |
50 */ |
35 public class RemoteConnectionsActivator extends AbstractUIPlugin { |
51 public class RemoteConnectionsActivator extends AbstractUIPlugin { |
36 |
52 |
37 // The plug-in ID |
53 // The plug-in ID |
38 public static final String PLUGIN_ID = "com.nokia.carbide.remoteConnections"; //$NON-NLS-1$ |
54 public static final String PLUGIN_ID = "com.nokia.carbide.remoteConnections"; //$NON-NLS-1$ |
39 |
55 |
|
56 private static final String DISCOVERY_AGENT_EXTENSION = PLUGIN_ID + ".deviceDiscoveryAgent"; //$NON-NLS-1$ |
|
57 |
40 // The shared instance |
58 // The shared instance |
41 private static RemoteConnectionsActivator plugin; |
59 private static RemoteConnectionsActivator plugin; |
42 |
60 |
43 // A service tracker for the proxy service |
61 private Collection<IDeviceDiscoveryAgent> discoveryAgents; |
44 private ServiceTracker proxyServiceTracker; |
62 private static final String IGNORE_AGENT_LOAD_ERRORS_KEY = "ignoreAgentLoadErrors"; //$NON-NLS-1$ |
45 |
63 |
46 /** |
64 /** |
47 * The constructor |
65 * The constructor |
48 */ |
66 */ |
49 public RemoteConnectionsActivator() { |
67 public RemoteConnectionsActivator() { |
50 } |
68 } |
51 |
|
52 |
69 |
53 /** |
|
54 * Returns IProxyService. |
|
55 * @deprecated |
|
56 */ |
|
57 public IProxyService getProxyService() { |
|
58 return (IProxyService) proxyServiceTracker.getService(); |
|
59 } |
|
60 |
|
61 /* |
|
62 * (non-Javadoc) |
|
63 * @see org.eclipse.ui.plugin.AbstractUIPlugin#start(org.osgi.framework.BundleContext) |
|
64 */ |
|
65 public void start(BundleContext context) throws Exception { |
70 public void start(BundleContext context) throws Exception { |
66 super.start(context); |
71 super.start(context); |
67 plugin = this; |
72 plugin = this; |
68 Registry instance = Registry.instance(); |
73 Registry instance = Registry.instance(); |
69 instance.loadExtensions(); |
74 instance.loadExtensions(); |
70 instance.loadConnections(); |
75 instance.loadConnections(); |
71 this.proxyServiceTracker = new ServiceTracker(context, IProxyService.class.getName(), null); |
76 |
72 this.proxyServiceTracker.open(); |
77 RunRunnableWhenWorkbenchVisibleJob.start(new Runnable() { |
73 } |
78 public void run() { |
74 |
79 if (!ignoreAgentLoadErrors()) |
75 /* |
80 checkPrerequisites(); |
76 * (non-Javadoc) |
81 |
77 * @see org.eclipse.ui.plugin.AbstractUIPlugin#stop(org.osgi.framework.BundleContext) |
82 loadAndStartDeviceDiscoveryAgents(); |
78 */ |
83 } |
|
84 }); |
|
85 } |
|
86 |
|
87 private boolean ignoreAgentLoadErrors() { |
|
88 return getPreferenceStore().getBoolean(IGNORE_AGENT_LOAD_ERRORS_KEY); |
|
89 } |
|
90 |
|
91 private void checkPrerequisites() { |
|
92 final Map<IDeviceDiscoveryAgent, IPrerequisiteStatus> agentToStatusMap = |
|
93 new HashMap<IDeviceDiscoveryAgent, IPrerequisiteStatus>(); |
|
94 |
|
95 // load the extensions just to check statuses |
|
96 Collection<IDeviceDiscoveryAgent> agents = new ArrayList<IDeviceDiscoveryAgent>(); |
|
97 loadExtensions(DISCOVERY_AGENT_EXTENSION, null, agents, null); |
|
98 |
|
99 for (IDeviceDiscoveryAgent agent : agents) { |
|
100 IPrerequisiteStatus status = agent.getPrerequisiteStatus(); |
|
101 if (!status.isOK()) { |
|
102 agentToStatusMap.put(agent, status); |
|
103 } |
|
104 } |
|
105 |
|
106 if (!agentToStatusMap.isEmpty()) { |
|
107 DeviceDiscoveryPrequisiteErrorDialog dlg = new DeviceDiscoveryPrequisiteErrorDialog(WorkbenchUtils.getSafeShell()); |
|
108 for (Entry<IDeviceDiscoveryAgent, IPrerequisiteStatus> entry : agentToStatusMap.entrySet()) { |
|
109 IDeviceDiscoveryAgent agent = entry.getKey(); |
|
110 IPrerequisiteStatus status = entry.getValue(); |
|
111 dlg.addAgentData(agent.getDisplayName(), status.getErrorText(), status.getURL()); |
|
112 } |
|
113 dlg.open(); |
|
114 if (dlg.isDontAskAgainChecked()) |
|
115 storeIgnoreAgentLoadErrorsFlag(); |
|
116 } |
|
117 } |
|
118 |
79 public void stop(BundleContext context) throws Exception { |
119 public void stop(BundleContext context) throws Exception { |
|
120 stopDeviceDiscoveryAgents(); |
80 Registry.instance().storeConnections(); |
121 Registry.instance().storeConnections(); |
81 Registry.instance().disposeConnections(); |
122 Registry.instance().disposeConnections(); |
82 plugin = null; |
123 plugin = null; |
83 super.stop(context); |
124 super.stop(context); |
84 this.proxyServiceTracker.close(); |
125 } |
|
126 |
|
127 private void storeIgnoreAgentLoadErrorsFlag() { |
|
128 getPreferenceStore().setValue(IGNORE_AGENT_LOAD_ERRORS_KEY, true); |
|
129 try { |
|
130 new InstanceScope().getNode(PLUGIN_ID).flush(); |
|
131 } catch (BackingStoreException e) { |
|
132 logError(e); |
|
133 } |
85 } |
134 } |
86 |
135 |
87 /** |
136 /** |
88 * Returns the shared instance |
137 * Returns the shared instance |
89 * |
138 * |
129 } |
178 } |
130 |
179 |
131 public static void setHelp(Control control, String id) { |
180 public static void setHelp(Control control, String id) { |
132 PlatformUI.getWorkbench().getHelpSystem().setHelp(control, PLUGIN_ID + id); //$NON-NLS-1$ |
181 PlatformUI.getWorkbench().getHelpSystem().setHelp(control, PLUGIN_ID + id); //$NON-NLS-1$ |
133 } |
182 } |
|
183 |
|
184 private void loadAndStartDeviceDiscoveryAgents() { |
|
185 String loadError = Messages.getString("RemoteConnectionsActivator.DiscoveryAgentLoadError"); //$NON-NLS-1$ |
|
186 discoveryAgents = new ArrayList<IDeviceDiscoveryAgent>(); |
|
187 loadExtensions(DISCOVERY_AGENT_EXTENSION, loadError, discoveryAgents, new IFilter() { |
|
188 public boolean select(Object toTest) { |
|
189 if (toTest instanceof IDeviceDiscoveryAgent) { |
|
190 try { |
|
191 ((IDeviceDiscoveryAgent) toTest).start(); |
|
192 return true; |
|
193 } catch (Throwable e) { |
|
194 // since we launch arbitrary code, catch any exception to prevent killing the view |
|
195 logError(e); |
|
196 } |
|
197 } |
|
198 return false; |
|
199 } |
|
200 }); |
|
201 |
|
202 } |
|
203 |
|
204 private void stopDeviceDiscoveryAgents() { |
|
205 for (IDeviceDiscoveryAgent agent : discoveryAgents) { |
|
206 try { |
|
207 agent.stop(); |
|
208 } catch (CoreException e) { |
|
209 logError(e); |
|
210 } |
|
211 } |
|
212 |
|
213 } |
|
214 |
|
215 public static void log(String errorStr, Throwable t) { |
|
216 RemoteConnectionsActivator p = RemoteConnectionsActivator.getDefault(); |
|
217 String error = errorStr; |
|
218 if (t != null) { |
|
219 error += " : " + t.getLocalizedMessage(); //$NON-NLS-1$ |
|
220 } |
|
221 Logging.log(p, Logging.newStatus(p, IStatus.ERROR, error)); |
|
222 if (t instanceof CoreException) |
|
223 Logging.log(p, ((CoreException) t).getStatus()); |
|
224 } |
|
225 |
|
226 @SuppressWarnings("unchecked") |
|
227 public static <T> void loadExtensions(String extensionId, String loadError, Collection<T> extensionObjects, IFilter filter) { |
|
228 IConfigurationElement[] elements = |
|
229 Platform.getExtensionRegistry().getConfigurationElementsFor(extensionId); |
|
230 for (IConfigurationElement element : elements) { |
|
231 try { |
|
232 T extObject = (T) element.createExecutableExtension("class"); //$NON-NLS-1$ |
|
233 if (filter == null || filter.select(extObject)) |
|
234 extensionObjects.add(extObject); |
|
235 } |
|
236 catch (CoreException e) { |
|
237 if (loadError != null) |
|
238 RemoteConnectionsActivator.log(loadError, e); |
|
239 } |
|
240 } |
|
241 } |
134 } |
242 } |