blob: 1d3b072df67c798d5cbbf63df0a48a843a801a90 [file] [log] [blame]
/*******************************************************************************
* Copyright (c) 2009, 2012 Obeo.
* 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:
* Obeo - initial API and implementation
*******************************************************************************/
package org.eclipse.acceleo.internal.traceability;
import org.eclipse.acceleo.engine.AcceleoEngineMessages;
import org.eclipse.core.runtime.CoreException;
import org.eclipse.core.runtime.IStatus;
import org.eclipse.core.runtime.Plugin;
import org.eclipse.core.runtime.Status;
import org.osgi.framework.BundleContext;
/**
* The activator class controls the plug-in life cycle.
*
* @author <a href="mailto:stephane.begaudeau@obeo.fr">Stephane Begaudeau</a>
*/
public class AcceleoTraceabilityPlugin extends Plugin {
/** The plug-in ID. */
public static final String PLUGIN_ID = "org.eclipse.acceleo.traceability"; //$NON-NLS-1$
/** This plug-in's shared instance. */
private static AcceleoTraceabilityPlugin plugin;
/**
* Default constructor for the plugin.
*/
public AcceleoTraceabilityPlugin() {
// Empty implementation
}
/**
* Returns the shared instance.
*
* @return the shared instance
*/
public static AcceleoTraceabilityPlugin getDefault() {
return plugin;
}
/**
* Trace an Exception in the error log.
*
* @param e
* Exception to log.
* @param blocker
* <code>True</code> if the exception must be logged as error, <code>False</code> to log it as
* a warning.
*/
public static void log(Exception e, boolean blocker) {
if (e == null) {
throw new NullPointerException(AcceleoEngineMessages
.getString("AcceleoTraceabilityPlugin.LogNullException")); //$NON-NLS-1$
}
if (getDefault() == null) {
// We are out of eclipse. Prints the stack trace on standard error.
// CHECKSTYLE:OFF
e.printStackTrace();
// CHECKSTYLE:ON
} else if (e instanceof CoreException) {
log(((CoreException)e).getStatus());
} else if (e instanceof NullPointerException) {
int severity = IStatus.WARNING;
if (blocker) {
severity = IStatus.ERROR;
}
log(new Status(severity, PLUGIN_ID, severity, AcceleoEngineMessages
.getString("AcceleoTraceabilityPlugin.ElementNotFound"), e)); //$NON-NLS-1$
} else {
int severity = IStatus.WARNING;
if (blocker) {
severity = IStatus.ERROR;
}
log(new Status(severity, PLUGIN_ID, severity, e.getMessage(), e));
}
}
/**
* Puts the given status in the error log view.
*
* @param status
* Error Status.
*/
public static void log(IStatus status) {
// Eclipse platform displays NullPointer on standard error instead of throwing it.
// We'll handle this by throwing it ourselves.
if (status == null) {
throw new NullPointerException(AcceleoEngineMessages
.getString("AcceleoTraceabilityPlugin.LogNullStatus")); //$NON-NLS-1$
}
if (getDefault() != null) {
getDefault().getLog().log(status);
} else {
// We are out of eclipse. Prints the message on standard error.
// CHECKSTYLE:OFF
System.err.println(status.getMessage());
status.getException().printStackTrace();
// CHECKSTYLE:ON
}
}
/**
* Puts the given message in the error log view, as error or warning.
*
* @param message
* The message to put in the error log view.
* @param blocker
* <code>True</code> if the message must be logged as error, <code>False</code> to log it as a
* warning.
*/
public static void log(String message, boolean blocker) {
if (getDefault() == null) {
// We are out of eclipse. Prints the message on standard error.
// CHECKSTYLE:OFF
System.err.println(message);
// CHECKSTYLE:ON
} else {
int severity = IStatus.WARNING;
if (blocker) {
severity = IStatus.ERROR;
}
String errorMessage = message;
if (errorMessage == null || "".equals(errorMessage)) { //$NON-NLS-1$
errorMessage = AcceleoTraceabilityMessages
.getString("AcceleoTraceabilityPlugin.UnexpectedException"); //$NON-NLS-1$
}
log(new Status(severity, PLUGIN_ID, errorMessage));
}
}
/**
* {@inheritDoc}
*
* @see org.eclipse.core.runtime.Plugin#start(org.osgi.framework.BundleContext)
*/
@Override
public void start(final BundleContext context) throws Exception {
plugin = this;
super.start(context);
}
/**
* {@inheritDoc}
*
* @see org.eclipse.core.runtime.Plugin#stop(org.osgi.framework.BundleContext)
*/
@Override
public void stop(final BundleContext context) throws Exception {
super.stop(context);
plugin = null;
}
}