sysperfana/memspyext/com.nokia.s60tools.memspy/src/com/nokia/s60tools/memspy/plugin/MemSpyPlugin.java
changeset 7 8e12a575a9b5
equal deleted inserted replaced
6:f65f740e69f9 7:8e12a575a9b5
       
     1 /*
       
     2 * Copyright (c) 2009 Nokia Corporation and/or its subsidiary(-ies).
       
     3 * All rights reserved.
       
     4 * This component and the accompanying materials are made available
       
     5 * under the terms of "Eclipse Public License v1.0"
       
     6 * which accompanies this distribution, and is available
       
     7 * at the URL "http://www.eclipse.org/legal/epl-v10.html".
       
     8 *
       
     9 * Initial Contributors:
       
    10 * Nokia Corporation - initial contribution.
       
    11 *
       
    12 * Contributors:
       
    13 *
       
    14 * Description: 
       
    15 *
       
    16 */
       
    17 
       
    18 
       
    19 
       
    20 package com.nokia.s60tools.memspy.plugin;
       
    21 
       
    22 import java.io.File;
       
    23 import java.io.IOException;
       
    24 import java.net.URL;
       
    25 
       
    26 import org.eclipse.core.runtime.FileLocator;
       
    27 import org.eclipse.core.runtime.IConfigurationElement;
       
    28 import org.eclipse.core.runtime.IExtension;
       
    29 import org.eclipse.core.runtime.IExtensionPoint;
       
    30 import org.eclipse.core.runtime.IExtensionRegistry;
       
    31 import org.eclipse.core.runtime.Platform;
       
    32 import org.eclipse.jface.preference.IPreferenceStore;
       
    33 import org.eclipse.swt.widgets.Shell;
       
    34 import org.eclipse.ui.IWorkbenchPage;
       
    35 import org.eclipse.ui.plugin.AbstractUIPlugin;
       
    36 import org.osgi.framework.Bundle;
       
    37 import org.osgi.framework.BundleContext;
       
    38 
       
    39 import com.nokia.s60tools.memspy.export.ITraceClientNotificationsIf;
       
    40 import com.nokia.s60tools.memspy.export.ITraceDataProcessor;
       
    41 import com.nokia.s60tools.memspy.export.ITraceProvider;
       
    42 import com.nokia.s60tools.memspy.resources.ImageResourceManager;
       
    43 import com.nokia.s60tools.memspy.util.MemSpyConsole;
       
    44 import com.nokia.s60tools.util.debug.DbgUtility;
       
    45 
       
    46 
       
    47 /**
       
    48  * The activator class controls the plug-in life cycle.
       
    49  */
       
    50 public class MemSpyPlugin extends AbstractUIPlugin {
       
    51 
       
    52 	/**
       
    53 	 * Plug-in ID for Launcher plug-in.
       
    54 	 */
       
    55 	public static final String MEMSPY_TRACE_PLUGIN_ID = "com.nokia.s60tools.memspy.trace";//$NON-NLS-1$
       
    56 	/**
       
    57 	 * Launcher plug-in binaries directory name.
       
    58 	 */
       
    59 	public static final String LAUNCHER_BINARIES_DIR_NAME = "Launcher.binaries";//$NON-NLS-1$
       
    60 	
       
    61 	/**
       
    62 	 * Trace provider extension name.
       
    63 	 */
       
    64 	final String EXTENSION_TRACE_PROVIDER = "traceprovider"; //$NON-NLS-1$
       
    65 	
       
    66 	/**
       
    67 	 * Plug-in ID constant.
       
    68 	 */
       
    69 	public static final String PLUGIN_ID = "com.nokia.s60tools.memspy";
       
    70 	
       
    71 	/**
       
    72 	 * Member for storing plug-in install path.
       
    73 	 */
       
    74 	private String pluginInstallPath = "";
       
    75 	
       
    76 	/**
       
    77 	 * Shared plug-in instance
       
    78 	 */
       
    79 	private static MemSpyPlugin plugin;
       
    80 	
       
    81 	/**
       
    82 	 * Storing reference to possibly installed trace provider plug-in. 
       
    83 	 */
       
    84 	private static ITraceProvider traceProvider;
       
    85 	
       
    86 	/**
       
    87 	 * Preferences store instance reference.
       
    88 	 */
       
    89 	private static IPreferenceStore prefsStore;
       
    90 	
       
    91 	/**
       
    92 	 * SWMT category setting feature is enabled by default, but may be disabled in case device does not support it.
       
    93 	 */
       
    94 	boolean isSWMTCategorySettingFeatureEnabled = true;	
       
    95 
       
    96 	/**
       
    97 	 * The constructor
       
    98 	 */
       
    99 	public MemSpyPlugin() {
       
   100 		plugin = this;
       
   101 	}
       
   102 	
       
   103 	/* (non-Javadoc)
       
   104 	 * @see org.eclipse.ui.plugin.AbstractUIPlugin#start(org.osgi.framework.BundleContext)
       
   105 	 */
       
   106 	public void start(BundleContext context) throws Exception {
       
   107 		super.start(context);
       
   108 		plugin = this;
       
   109 		
       
   110 		String pluginInstallLocation = getPluginInstallPath();
       
   111 		String imagesPath = getImagesPath(pluginInstallLocation);
       
   112 		
       
   113 		// Loading images required by this plug-in
       
   114 		ImageResourceManager.loadImages(imagesPath);
       
   115 		
       
   116 		// Getting installed trace provider plug-in if available
       
   117 		traceProvider = findTraceProviderExtension();
       
   118 	}
       
   119 
       
   120 	/*
       
   121 	 * (non-Javadoc)
       
   122 	 * @see org.eclipse.ui.plugin.AbstractUIPlugin#stop(org.osgi.framework.BundleContext)
       
   123 	 */
       
   124 	public void stop(BundleContext context) throws Exception {
       
   125 		plugin = null;
       
   126 		super.stop(context);
       
   127 	}
       
   128 
       
   129 	/**
       
   130 	 * Returns the shared instance
       
   131 	 *
       
   132 	 * @return the shared instance
       
   133 	 */
       
   134 	public static MemSpyPlugin getDefault() {
       
   135 		return plugin;
       
   136 	}
       
   137 
       
   138 	/**
       
   139 	 * Get plugin installation path
       
   140 	 * @return the path where this plugin is installed
       
   141 	 */
       
   142 	public static String getPluginInstallPath() {
       
   143 		try {
       
   144 			if ( plugin.pluginInstallPath.equals("") ) { //$NON-NLS-1$
       
   145 				 // URL to the plugin's root ("/")
       
   146 				URL relativeURL = plugin.getBundle().getEntry("/"); //$NON-NLS-1$
       
   147 				//	Converting into local path
       
   148 				URL localURL = FileLocator.toFileURL(relativeURL);
       
   149 				//	Getting install location in correct form
       
   150 				File f = new File(localURL.getPath());
       
   151 				plugin.pluginInstallPath = f.getAbsolutePath();
       
   152 			}
       
   153 			return plugin.pluginInstallPath;
       
   154 		} catch (Exception e) {
       
   155 			return ""; //$NON-NLS-1$
       
   156 		}
       
   157 	}
       
   158 	
       
   159 	/**
       
   160 	 * Gets images path relative to given plugin install path.
       
   161 	 * @param pluginInstallPath Plugin installation path.
       
   162 	 * @return Path were image resources are located.
       
   163 	 * @throws IOException
       
   164 	 */
       
   165 	private String getImagesPath(String pluginInstallPath) throws IOException{
       
   166 		return pluginInstallPath
       
   167 				+ File.separatorChar + "icons"; //$NON-NLS-1$
       
   168 	}
       
   169 
       
   170 	/**
       
   171 	 * This must be called from UI thread. If called
       
   172 	 * from non-ui thread this returns <code>null</code>.
       
   173 	 * @return Currently active workbench page.
       
   174 	 */
       
   175 	public static IWorkbenchPage getCurrentlyActivePage(){
       
   176 		return getDefault().getWorkbench().getActiveWorkbenchWindow().getActivePage();
       
   177 	}
       
   178 	
       
   179 	/**
       
   180 	 * This must be called from UI thread. If called
       
   181 	 * from non-UI thread this returns <code>null</code>.
       
   182 	 * @return The shell of the currently active workbench window..
       
   183 	 */
       
   184 	public static Shell getCurrentlyActiveWbWindowShell(){
       
   185 		IWorkbenchPage page = getCurrentlyActivePage();
       
   186 		if(page != null){
       
   187 			return page.getWorkbenchWindow().getShell();
       
   188 		}
       
   189 		return null;
       
   190 	}
       
   191 	
       
   192 	/**
       
   193 	 * Checks if SWMT category setting feature is enabled.
       
   194 	 * @return <code>true</code> if enabled, otherwise <code>false</code>.
       
   195 	 */
       
   196 	public boolean isSWMTCategorySettingFeatureEnabled() {
       
   197 		return isSWMTCategorySettingFeatureEnabled;
       
   198 	}
       
   199 
       
   200 	/**
       
   201 	 * Sets if SWMT category setting feature is enabled.
       
   202 	 * @param isSWMTCategorySettingFeatureEnabled <code>true</code> if enabled, otherwise <code>false</code>.
       
   203 	 */
       
   204 	public void setSWMTCategorySettingFeatureEnabled(
       
   205 			boolean isSWMTCategorySettingFeatureEnabled) {
       
   206 		this.isSWMTCategorySettingFeatureEnabled = isSWMTCategorySettingFeatureEnabled;
       
   207 	}
       
   208 	
       
   209 	/**
       
   210 	 * Gets MemSpy Launcher plugin's binaries directory path
       
   211 	 * @return path were Launcher binaries are located.
       
   212 	 */
       
   213 	public String getMemspyLauncherBinDir(){
       
   214 				
       
   215 		Bundle bundle = Platform
       
   216 			.getBundle(MEMSPY_TRACE_PLUGIN_ID); //$NON-NLS-1$
       
   217 		
       
   218 		String launcherBinDir = null;
       
   219 		try {
       
   220 			 // URL to the Launcher binaries
       
   221 			URL relativeURL = bundle.getEntry(LAUNCHER_BINARIES_DIR_NAME); //$NON-NLS-1$
       
   222 			//	Converting into local path
       
   223 			URL localURL = FileLocator.toFileURL(relativeURL);
       
   224 			//	Getting install location in correct form
       
   225 			File file = new File(localURL.getPath());
       
   226 			launcherBinDir = file.getAbsolutePath();
       
   227 			MemSpyConsole.getInstance().println("MemSpyLauncher binaries dir detected: " +launcherBinDir);
       
   228 			
       
   229 		} catch (IOException e) {
       
   230 			MemSpyConsole.getInstance().println("MemSpyLauncher binaries dir detection failed, reason: " +e);
       
   231 			e.printStackTrace();
       
   232 		}
       
   233 		return launcherBinDir;
       
   234 		
       
   235 	}	
       
   236 
       
   237 	/**
       
   238 	 * Returns PreferenceStore where plugin preferences are stored.
       
   239 	 * @return PreferenceStore where plugin preferences are stored
       
   240 	 */
       
   241 	public static IPreferenceStore getPrefsStore() {
       
   242 		if (prefsStore == null){
       
   243 			prefsStore = getDefault().getPreferenceStore();
       
   244 		}
       
   245 		
       
   246 		return prefsStore;
       
   247 	}
       
   248 
       
   249 	/**
       
   250 	 * Gets trace provider interface instance if available.
       
   251 	 * @return trace provider interface instance or <code>null</code> if not available
       
   252 	 */
       
   253 	public static ITraceProvider getTraceProvider(){
       
   254 		if(isTraceProviderAvailable()){
       
   255 			return traceProvider;			
       
   256 		}
       
   257 		// Otherwise returning a dummy provider instance
       
   258 		return createDummyTraceProviderInstance();
       
   259 	}
       
   260 	
       
   261 	/**
       
   262 	 * Creates a dummy trace provider instance.
       
   263 	 * Client can safely reference to this dummy instance without any <code>NullPointerException</code>
       
   264 	 * problems etc. However the corresponding functionalities from UI should be disables.
       
   265 	 * @return dummy trace provider instance.
       
   266 	 */
       
   267 	private static ITraceProvider createDummyTraceProviderInstance() {
       
   268 		// Creating  just a dummy provider instance that does not do anything
       
   269 		return new ITraceProvider() {
       
   270 			
       
   271 			public void stopListenTraceData() {
       
   272 				// dummy provider does not take any actions
       
   273 			}
       
   274 			
       
   275 			public boolean startListenTraceData(ITraceDataProcessor dataProcessor) {
       
   276 				// dummy provider does not take any actions
       
   277 				return false;
       
   278 			}
       
   279 			
       
   280 			public boolean sendStringData(String stringData) {
       
   281 				// dummy provider does not take any actions
       
   282 				return false;
       
   283 			}
       
   284 			
       
   285 			public boolean sendIntData(int integerData) {
       
   286 				// dummy provider does not take any actions
       
   287 				return false;
       
   288 			}
       
   289 			
       
   290 			public String getTraceSourcePreferencePageId() {
       
   291 				// dummy provider does not take any actions
       
   292 				return null;
       
   293 			}
       
   294 			
       
   295 			public String getDisplayNameForCurrentConnection() {
       
   296 				return "<no trace plug-in installed>";
       
   297 			}
       
   298 			
       
   299 			public void disconnectTraceSource() {
       
   300 				// dummy provider does not take any actions
       
   301 			}
       
   302 			
       
   303 			public boolean connectTraceSource(
       
   304 					ITraceClientNotificationsIf traceClient) {
       
   305 				// dummy provider does not take any actions
       
   306 				return false;
       
   307 			}
       
   308 			
       
   309 			public boolean activateTrace(String traceGroupID) {
       
   310 				// dummy provider does not take any actions
       
   311 				return false;
       
   312 			}
       
   313 
       
   314 		};
       
   315 	}
       
   316 
       
   317 	/**
       
   318 	 * Checks if trace provider plug-in is available.
       
   319 	 * @return <code>true</code> if trace provider interface available, otherwise <code>false</code>
       
   320 	 */
       
   321 	public static boolean isTraceProviderAvailable(){
       
   322 		return (traceProvider != null);
       
   323 	}
       
   324 	
       
   325 	/**
       
   326 	 * Tries to find trace provider plug-ins. Selecting the first found one.
       
   327 	 * @return reference to trace provider instance if found, otherwise <code>null</code>
       
   328 	 */
       
   329 	ITraceProvider findTraceProviderExtension() {
       
   330 		try {
       
   331 			IExtensionRegistry er = Platform.getExtensionRegistry();
       
   332 			IExtensionPoint ep = er.getExtensionPoint(PLUGIN_ID, EXTENSION_TRACE_PROVIDER);
       
   333 			String uniqueIdentifier = ep.getUniqueIdentifier();
       
   334 			DbgUtility.println(DbgUtility.PRIORITY_OPERATION, "Found extension point: " + uniqueIdentifier); //$NON-NLS-1$
       
   335 			IExtension[] extensions = ep.getExtensions();
       
   336 			
       
   337 			// if plug-ins were found.
       
   338 			if (extensions != null && extensions.length > 0) {
       
   339 				
       
   340 				// read all found trace providers
       
   341 				for (int i = 0; i < extensions.length; i++) {
       
   342 					IConfigurationElement[] ce = extensions[i].getConfigurationElements();
       
   343 					if (ce != null && ce.length > 0) {
       
   344 						try {
       
   345 							ITraceProvider provider = (ITraceProvider)ce[0].createExecutableExtension("class"); //$NON-NLS-1$ 
       
   346 							// We support only one trace provider
       
   347 							if (provider != null) {
       
   348 								return provider;
       
   349 							}
       
   350 						} catch (Exception e) {
       
   351 							e.printStackTrace();
       
   352 						}
       
   353 					}
       
   354 				}
       
   355 			}
       
   356 		} catch (Exception e) {
       
   357 			e.printStackTrace();
       
   358 		}
       
   359 		
       
   360 		return null;
       
   361 	}	
       
   362 	
       
   363 }