Skip to main content
summaryrefslogtreecommitdiffstats
blob: 6cbe2d57c1c18c823e87e7e955f8528be60c1bfb (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
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
/*******************************************************************************
 * Copyright (c) 2012 Oracle. 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:
 *     Oracle - initial API and implementation
 ******************************************************************************/
package org.eclipse.jpt.common.ui.internal;

import org.eclipse.core.runtime.IStatus;
import org.eclipse.core.runtime.Platform;
import org.eclipse.core.runtime.Status;
import org.eclipse.jpt.common.utility.internal.StringTools;
import org.eclipse.osgi.service.debug.DebugOptions;
import org.eclipse.ui.plugin.AbstractUIPlugin;
import org.osgi.framework.BundleContext;
import org.osgi.util.tracker.ServiceTracker;

/**
 * Common Dali UI plug-in behavior.
 * See JptPlugin
 */
public class JptUIPlugin
	extends AbstractUIPlugin
{
	protected BundleContext bundleContext;
	protected ServiceTracker<DebugOptions, DebugOptions> debugOptionsServiceTracker;


	protected JptUIPlugin() {
		super();
	}


	// ********** logging **********

	/**
	 * Log the specified message with a severity of
	 * {@link IStatus#ERROR ERROR}.
	 * @see #log(int, String)
	 * @see IStatus
	 */
	public void logError(String message) {
        this.log(IStatus.ERROR, message);
    }

	/**
	 * Log the specified message with the specified severity.
	 * @see IStatus#getSeverity()
	 */
	public void log(int severity, String message) {
        this.log(severity, message, null);
    }

	/**
	 * Log the specified exception or error with a severity of
	 * {@link IStatus#ERROR ERROR}.
	 * @see #log(int, Throwable)
	 * @see IStatus
	 */
	public void logError(Throwable throwable) {
        this.log(IStatus.ERROR, throwable);
	}

	/**
	 * Log the specified exception or error with the specified severity.
	 * @see IStatus#getSeverity()
	 */
	public void log(int severity, Throwable throwable) {
		this.log(severity, throwable.getLocalizedMessage(), throwable);
	}

	/**
	 * Log the specified message and exception or error with a severity of
	 * {@link IStatus#ERROR ERROR}.
	 * @see #log(int, String, Throwable)
	 * @see IStatus
	 */
	public void logError(String msg, Throwable throwable) {
		this.log(IStatus.ERROR, msg, throwable);
	}

	/**
	 * Log the specified message and exception or error
	 * with the specified severity.
	 * @see IStatus#getSeverity()
	 * @see IStatus#getCode()
	 */
	public void log(int severity, String msg, Throwable throwable) {
		this.log(severity, IStatus.OK, msg, throwable);
	}

	/**
	 * Log the specified message and exception or error
	 * with the specified severity and code.
	 * @see IStatus#getSeverity()
	 * @see IStatus#getCode()
	 */
	public void log(int severity, int code, String msg, Throwable throwable) {
		this.getLog().log(new Status(severity, this.getPluginID(), code, msg, throwable));
	}


	// ********** debug options **********

	/**
	 * Return the specified debug option as a <code>boolean</code> value.
	 * Return <code>false</code> if no such option is found.
	 * <p>
	 * The debug option is within the scope of the plug-in's debug options
	 * (e.g. for the plug-in <code>"org.eclipse.jpt.common.core"</code>,
	 * the specified option <code>"foo"</code> will be mapped to
	 * the {@link Platform} option
	 * <code>"org.eclipse.jpt.common.core/debug/foo"</code>).
	 */
	public boolean getBooleanDebugOption(String option) {
		return this.getBooleanDebugOption(option, false);
	}

	/**
	 * Return the specified debug option as a <code>boolean</code> value.
	 * Return the specified default value if no such option is found.
	 * <p>
	 * The debug option is within the scope of the plug-in's debug options
	 * (e.g. for the plug-in <code>"org.eclipse.jpt.common.core"</code>,
	 * the specified option <code>"foo"</code> will be mapped to
	 * the {@link Platform} option
	 * <code>"org.eclipse.jpt.common.core/debug/foo"</code>).
	 */
	public boolean getBooleanDebugOption(String option, boolean defaultValue) {
		String value = this.getDebugOption(option);
		return (value == null) ? defaultValue : Boolean.parseBoolean(value.trim());
	}

	/**
	 * Return the specified debug option as an <code>int</code> value.
	 * Return <code>-1</code> if no such option is found.
	 * <p>
	 * The debug option is within the scope of the plug-in's debug options
	 * (e.g. for the plug-in <code>"org.eclipse.jpt.common.core"</code>,
	 * the specified option <code>"foo"</code> will be mapped to
	 * the {@link Platform} option
	 * <code>"org.eclipse.jpt.common.core/debug/foo"</code>).
	 */
	public int getIntegerDebugOption(String option) {
		return this.getIntegerDebugOption(option, -1);
	}

	/**
	 * Return the specified debug option as an <code>int</code> value.
	 * Return the specified default value if no such option is found.
	 * <p>
	 * The debug option is within the scope of the plug-in's debug options
	 * (e.g. for the plug-in <code>"org.eclipse.jpt.common.core"</code>,
	 * the specified option <code>"foo"</code> will be mapped to
	 * the {@link Platform} option
	 * <code>"org.eclipse.jpt.common.core/debug/foo"</code>).
	 */
	public int getIntegerDebugOption(String option, int defaultValue) {
		String value = this.getDebugOption(option);
		return (value == null) ? defaultValue : Integer.parseInt(value.trim());
	}

	/**
	 * Return the specified debug option.
	 * Return <code>null</code> if no such option is found.
	 * <p>
	 * The debug option is within the scope of the plug-in's debug options
	 * (e.g. for the plug-in <code>"org.eclipse.jpt.common.core"</code>,
	 * the specified option <code>"foo"</code> will be mapped to
	 * the {@link Platform} option
	 * <code>"org.eclipse.jpt.common.core/debug/foo"</code>).
	 */
	public String getDebugOption(String option) {
		return this.getDebugOption(option, null);
	}

	/**
	 * Return the specified debug option.
	 * Return the specified default value if no such option is found.
	 * <p>
	 * The debug option is within the scope of the plug-in's debug options
	 * (e.g. for the plug-in <code>"org.eclipse.jpt.common.core"</code>,
	 * the specified option <code>"foo"</code> will be mapped to
	 * the {@link Platform} option
	 * <code>"org.eclipse.jpt.common.core/debug/foo"</code>).
	 */
	public String getDebugOption(String option, String defaultValue) {
		return this.isDebugging() ? this.getDebugOption_(option, defaultValue) : defaultValue;
	}

	protected String getDebugOption_(String option, String defaultValue) {
		if (StringTools.stringIsEmpty(option)) {
			throw new IllegalArgumentException("debug option cannot be blank"); //$NON-NLS-1$
		}
		String value = this.getDebugOption_(option);
		return (value != null) ? value : defaultValue;
	}

	protected String getDebugOption_(String option) {
		DebugOptions debugOptions = this.getDebugOptions();
		return (debugOptions == null) ? null : debugOptions.getOption(this.getPluginDebugOption() + option);
	}

	protected DebugOptions getDebugOptions() {
		ServiceTracker<DebugOptions, DebugOptions> tracker = this.getDebugOptionsServiceTracker();
		return (tracker == null) ? null : tracker.getService();
	}

	private synchronized ServiceTracker<DebugOptions, DebugOptions> getDebugOptionsServiceTracker() {
		if (this.isActive() && (this.debugOptionsServiceTracker == null)) {
			this.debugOptionsServiceTracker = this.buildDebugOptionsServiceTracker();
			this.debugOptionsServiceTracker.open();
		}
		return this.debugOptionsServiceTracker;
	}

	private ServiceTracker<DebugOptions, DebugOptions> buildDebugOptionsServiceTracker() {
		return new ServiceTracker<DebugOptions, DebugOptions>(this.bundleContext, DebugOptions.class, null);
	}

	/**
	 * Return the plug-in's debug option path.
	 */
	protected String getPluginDebugOption() {
		return this.getPluginID() + DEBUG_OPTION_SCOPE;
	}
	protected static final String DEBUG_OPTION_SCOPE = "/debug/"; //$NON-NLS-1$


	// ********** plug-in lifecycle **********

	@Override
	public synchronized void start(BundleContext context) throws Exception {
		super.start(context);
		this.bundleContext = context;
	}

	@Override
	public synchronized void stop(BundleContext context) throws Exception {
		try {
			this.stop_();
		} finally {
			this.bundleContext = null;
			super.stop(context);
		}
	}

	protected void stop_() throws Exception {
		if (this.debugOptionsServiceTracker != null) {
			this.debugOptionsServiceTracker.close();
			this.debugOptionsServiceTracker = null;
		}
	}


	// ********** misc **********

	public String getPluginID() {
		return this.getBundle().getSymbolicName();
	}

	/**
	 * Return whether the plug-in is active; i.e. it has been
	 * {@link #start(BundleContext) started}.
	 */
	public synchronized boolean isActive() {
		return this.bundleContext != null;
	}

	/**
	 * Return whether the plug-in is inactive; i.e. it has been
	 * {@link #stop(BundleContext) stopped} or not yet
	 * {@link #start(BundleContext) started}.
	 */
	public boolean isInactive() {
		return ! this.isActive();
	}
}

Back to the top