Skip to main content
summaryrefslogtreecommitdiffstats
blob: 361438a7411c75f7d86e28541e52dc6eb8fb8954 (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
/*******************************************************************************
 * 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.core.internal;

import org.eclipse.core.runtime.IStatus;
import org.eclipse.core.runtime.Platform;
import org.eclipse.core.runtime.Plugin;
import org.eclipse.core.runtime.Status;
import org.eclipse.jpt.common.utility.internal.StringTools;

/**
 * Common Dali plug-in behavior.
 */
public abstract class JptPlugin
	extends Plugin
{
	protected JptPlugin() {
		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.getPlatformDebugOption(option);
		return (value != null) ? value : defaultValue;
	}

	protected String getPlatformDebugOption(String option) {
		return Platform.getDebugOption(this.getPluginDebugOption() + option);
	}

	/**
	 * 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$


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

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

Back to the top