diff options
Diffstat (limited to 'build/org.eclipse.cdt.meson.core/src/org/eclipse/cdt/meson/core/Activator.java')
-rw-r--r-- | build/org.eclipse.cdt.meson.core/src/org/eclipse/cdt/meson/core/Activator.java | 172 |
1 files changed, 172 insertions, 0 deletions
diff --git a/build/org.eclipse.cdt.meson.core/src/org/eclipse/cdt/meson/core/Activator.java b/build/org.eclipse.cdt.meson.core/src/org/eclipse/cdt/meson/core/Activator.java new file mode 100644 index 00000000000..31e9b18f715 --- /dev/null +++ b/build/org.eclipse.cdt.meson.core/src/org/eclipse/cdt/meson/core/Activator.java @@ -0,0 +1,172 @@ +/******************************************************************************* + * Copyright (c) 2018 Red Hat Inc. and others. + * 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 + * + * 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; + } + + /* + * (non-Javadoc) + * @see org.osgi.framework.BundleActivator#start(org.osgi.framework.BundleContext) + */ + public void start(BundleContext bundleContext) throws Exception { + Activator.context = bundleContext; + } + + /* + * (non-Javadoc) + * @see org.osgi.framework.BundleActivator#stop(org.osgi.framework.BundleContext) + */ + public void stop(BundleContext bundleContext) throws Exception { + Activator.context = null; + plugin = null; + } + +} |