1 /* 2 * Copyright (C) 2011 The Android Open Source Project 3 * 4 * Licensed under the Eclipse Public License, Version 1.0 (the "License"); 5 * you may not use this file except in compliance with the License. 6 * You may obtain a copy of the License at 7 * 8 * http://www.eclipse.org/org/documents/epl-v10.php 9 * 10 * Unless required by applicable law or agreed to in writing, software 11 * distributed under the License is distributed on an "AS IS" BASIS, 12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 * See the License for the specific language governing permissions and 14 * limitations under the License. 15 */ 16 17 package com.android.ide.eclipse.ndk.internal; 18 19 import org.eclipse.core.runtime.FileLocator; 20 import org.eclipse.core.runtime.IPath; 21 import org.eclipse.core.runtime.IStatus; 22 import org.eclipse.core.runtime.Status; 23 import org.eclipse.ui.plugin.AbstractUIPlugin; 24 import org.osgi.framework.Bundle; 25 import org.osgi.framework.BundleContext; 26 import org.osgi.framework.ServiceReference; 27 28 import java.net.URL; 29 30 /** 31 * The activator class controls the plug-in life cycle 32 */ 33 public class Activator extends AbstractUIPlugin { 34 35 // The plug-in ID 36 public static final String PLUGIN_ID = "com.android.ide.eclipse.ndk"; //$NON-NLS-1$ 37 38 // The shared instance 39 private static Activator mPlugin; 40 41 @Override start(BundleContext context)42 public void start(BundleContext context) throws Exception { 43 super.start(context); 44 mPlugin = this; 45 } 46 47 @Override stop(BundleContext context)48 public void stop(BundleContext context) throws Exception { 49 mPlugin = null; 50 super.stop(context); 51 } 52 getDefault()53 public static Activator getDefault() { 54 return mPlugin; 55 } 56 57 @SuppressWarnings("unchecked") getService(Class<T> clazz)58 public static <T> T getService(Class<T> clazz) { 59 BundleContext context = mPlugin.getBundle().getBundleContext(); 60 ServiceReference ref = context.getServiceReference(clazz.getName()); 61 return (ref != null) ? (T) context.getService(ref) : null; 62 } 63 getBundle(String id)64 public static Bundle getBundle(String id) { 65 for (Bundle bundle : mPlugin.getBundle().getBundleContext().getBundles()) { 66 if (bundle.getSymbolicName().equals(id)) { 67 return bundle; 68 } 69 } 70 return null; 71 } 72 newStatus(Exception e)73 public static IStatus newStatus(Exception e) { 74 return new Status(IStatus.ERROR, PLUGIN_ID, e.getMessage(), e); 75 } 76 log(Exception e)77 public static void log(Exception e) { 78 mPlugin.getLog().log(newStatus(e)); 79 } 80 findFile(IPath path)81 public static URL findFile(IPath path) { 82 return FileLocator.find(mPlugin.getBundle(), path, null); 83 } 84 85 } 86