blob: 974278c3f887d45fba19d69008bf49da9fb249cb [file] [log] [blame]
/*******************************************************************************
* Copyright (c) 2018 Red Hat Inc. and others.
*
* This program and the accompanying materials
* are made available under the terms of the Eclipse Public License 2.0
* which accompanies this distribution, and is available at
* https://www.eclipse.org/legal/epl-2.0/
*
* SPDX-License-Identifier: EPL-2.0
*
* Red Hat Inc. - initial version
*******************************************************************************/
package org.eclipse.cdt.meson.core;
import java.lang.reflect.InvocationTargetException;
import java.text.MessageFormat;
import java.util.MissingResourceException;
import java.util.ResourceBundle;
import org.eclipse.core.resources.ResourcesPlugin;
import org.eclipse.core.runtime.Assert;
import org.eclipse.core.runtime.CoreException;
import org.eclipse.core.runtime.IStatus;
import org.eclipse.core.runtime.Status;
import org.osgi.framework.Bundle;
import org.osgi.framework.BundleActivator;
import org.osgi.framework.BundleContext;
import org.osgi.framework.ServiceReference;
public class Activator implements BundleActivator {
private static BundleContext context;
//The shared instance.
private static Activator plugin;
private ResourceBundle resourceBundle;
public static final String PLUGIN_ID = "org.eclipse.cdt.meson.core"; //$NON-NLS-1$
public Activator() {
Assert.isTrue(plugin == null);
plugin = this;
try {
resourceBundle = ResourceBundle.getBundle(PLUGIN_ID + ".Resources"); //$NON-NLS-1$
} catch (MissingResourceException x) {
resourceBundle = null;
}
}
public static String getPluginId() {
return PLUGIN_ID;
}
public static String getUniqueIdentifier() {
if (getDefault() == null) {
// If the default instance is not yet initialized,
// return a static identifier. This identifier must
// match the plugin id defined in plugin.xml
return PLUGIN_ID;
}
return context.getBundle().getSymbolicName();
}
public Bundle getBundle() {
return context.getBundle();
}
/**
* Returns the shared instance.
*/
public static Activator getDefault() {
return plugin;
}
/**
* Return the OSGi service with the given service interface.
*
* @param service service interface
* @return the specified service or null if it's not registered
* @since 1.5
*/
public static <T> T getService(Class<T> service) {
if (context != null) {
ServiceReference<T> ref = context.getServiceReference(service);
return ref != null ? context.getService(ref) : null;
}
return null;
}
public static void error(String message, Throwable cause) {
log(errorStatus(message, cause));
}
public static IStatus errorStatus(String message, Throwable cause) {
return new Status(IStatus.ERROR, PLUGIN_ID, message, cause);
}
/**
* Returns the string from the plugin's resource bundle,
* or 'key' if not found.
*
* @param key the message key
* @return the resource bundle message
*/
public static String getResourceString(String key) {
ResourceBundle bundle = Activator.getDefault().getResourceBundle();
try {
return bundle.getString(key);
} catch (MissingResourceException e) {
return key;
}
}
/**
* Returns the string from the plugin's resource bundle,
* or 'key' if not found.
*
* @param key the message key
* @param args an array of substituition strings
* @return the resource bundle message
*/
public static String getFormattedString(String key, String[] args) {
return MessageFormat.format(getResourceString(key), (Object[]) args);
}
/**
* Returns the plugin's resource bundle,
*/
public ResourceBundle getResourceBundle() {
return resourceBundle;
}
public static void log(IStatus status) {
ResourcesPlugin.getPlugin().getLog().log(status);
}
public static void logErrorMessage(String message) {
log(new Status(IStatus.ERROR, getUniqueIdentifier(), IStatus.ERROR, message, null));
}
public static void log(Throwable e) {
if (e instanceof InvocationTargetException) {
e = ((InvocationTargetException) e).getTargetException();
}
IStatus status = null;
if (e instanceof CoreException) {
status = ((CoreException) e).getStatus();
} else {
status = new Status(IStatus.ERROR, getUniqueIdentifier(), IStatus.OK, e.getMessage(), e);
}
log(status);
}
static BundleContext getContext() {
return context;
}
@Override
public void start(BundleContext bundleContext) {
Activator.context = bundleContext;
}
@Override
public void stop(BundleContext bundleContext) {
Activator.context = null;
plugin = null;
}
}