Skip to main content
aboutsummaryrefslogtreecommitdiffstats
blob: 19f2c43e598691285ec8b80ac73a8d9bab9728dc (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
/*******************************************************************************
 * Copyright (c) 2004, 2008, 2009 Red Hat, Inc.
 * 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:
 *    Keith Seitz <keiths@redhat.com> - initial API and implementation
 *    Kent Sebastian <ksebasti@redhat.com> -
 *******************************************************************************/

package org.eclipse.linuxtools.internal.oprofile.core;

import java.io.IOException;
import java.net.URL;

import org.eclipse.core.runtime.CoreException;
import org.eclipse.core.runtime.FileLocator;
import org.eclipse.core.runtime.IStatus;
import org.eclipse.core.runtime.Path;
import org.eclipse.core.runtime.Platform;
import org.eclipse.core.runtime.Plugin;
import org.eclipse.core.runtime.Status;
import org.eclipse.jface.dialogs.ErrorDialog;
import org.eclipse.linuxtools.internal.oprofile.core.linux.LinuxOpxmlProvider;
import org.eclipse.swt.widgets.Display;
import org.osgi.framework.Bundle;
import org.osgi.framework.BundleContext;

/**
 * The main plugin class to be used in the desktop.
 */
public class OprofileCorePlugin extends Plugin {
	private static final String PLUGIN_ID = "org.eclipse.linuxtools.oprofile.core"; //$NON-NLS-1$

	// The shared instance.
	private static OprofileCorePlugin plugin;

	public static final String DEBUG_PRINT_PREFIX = "DEBUG: "; //$NON-NLS-1$

	/**
	 * The constructor.
	 */
	public OprofileCorePlugin() {
		plugin = this;
	}

	/**
	 * This method is called when the plug-in is stopped
	 */
	@Override
	public void stop(BundleContext context) throws Exception {
		super.stop(context);
		plugin = null;
	}

	/**
	 * Returns the shared instance.
	 */
	public static OprofileCorePlugin getDefault() {
		return plugin;
	}

	/**
	 * Returns the unique id of this plugin. Should match plugin.xml!
	 */
	public static String getId() {
		return PLUGIN_ID;
	}

	/**
	 * Returns the OpxmlProvider registered with the plugin or throws an exception
	 * 
	 * @return the OpxmlProvider
	 */
	public IOpxmlProvider getOpxmlProvider() {
		return new LinuxOpxmlProvider();
	}

	/**
	 * Creates an error status object
	 * 
	 * @param errorClassString
	 *            A string of the error class
	 * @param e
	 *            The type of exception
	 * @return the status object of the error
	 */
	public static IStatus createErrorStatus(String errorClassString, Exception e) {
		String statusMessage = OprofileProperties.getString(errorClassString + ".error.statusMessage"); //$NON-NLS-1$

		if (e == null) {
			return new Status(IStatus.ERROR, getId(), IStatus.OK, statusMessage, null);
		} else {
			return new Status(IStatus.ERROR, getId(), IStatus.OK, statusMessage, e);
		}
	}

	/**
	 * Shows an error Dialog
	 * 
	 * @param errorClassString
	 *            A string of the error class
	 * @param ex
	 *            The type of exception
	 */
	public static void showErrorDialog(String errorClassString, CoreException ex) {
		final IStatus status;
		final String dialogTitle = OprofileProperties.getString(errorClassString + ".error.dialog.title"); //$NON-NLS-1$
		final String errorMessage = OprofileProperties.getString(errorClassString + ".error.dialog.message"); //$NON-NLS-1$

		if (ex == null) {
			status = createErrorStatus(errorClassString, null);
		} else {
			status = ex.getStatus();
		}

		// needs to be run in the ui thread otherwise swt throws invalid thread access
		Display.getDefault().syncExec(() -> ErrorDialog.openError(null, dialogTitle, errorMessage, status));

	}

	/**
	 *
	 * @return {@code true} when platform was started in debug mode ({@code -debug}
	 *         switch) and
	 *         {@code org.eclipse.linuxtools.internal.oprofile.core/debug} is set in
	 *         some .options file either in $HOME/.options or $(pwd)/.options.
	 */
	public static boolean isDebugMode() {
		return Platform.inDebugMode() && Platform.getDebugOption(OprofileCorePlugin.getId() + "/debug") != null; //$NON-NLS-1$
	}

	/**
	 * Log a string message with the given severity in the error log.
	 *
	 * @param severity
	 *            the severity of this exception
	 * @param msg
	 *            the string message to be logged
	 */
	public static void log(int severity, String msg) {
		plugin.getLog().log(new Status(severity, PLUGIN_ID, IStatus.OK, msg, null));
	}

	/**
	 * Returns the location of the plugin by checking the path of the bundle's
	 * locationURL.
	 *
	 * @return An absolute path representing the location of this plugin
	 */
	public String getPluginLocation() {
		Bundle bundle = getBundle();

		URL locationUrl = FileLocator.find(bundle, new Path("/"), null); //$NON-NLS-1$
		URL fileUrl = null;
		try {
			fileUrl = FileLocator.toFileURL(locationUrl);
		} catch (IOException e) {
			e.printStackTrace();
		}
		return fileUrl.getFile();
	}

}

Back to the top