aboutsummaryrefslogtreecommitdiff
path: root/eclipse/plugins/com.android.ide.eclipse.ndk/src/com/android/ide/eclipse/ndk/internal/Activator.java
blob: e165df1c5cad18f697b4f684e645483dad4bdf04 (plain)
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
/*
 * Copyright (C) 2011 The Android Open Source Project
 *
 * Licensed under the Eclipse Public License, Version 1.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *      http://www.eclipse.org/org/documents/epl-v10.php
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

package com.android.ide.eclipse.ndk.internal;

import org.eclipse.core.runtime.FileLocator;
import org.eclipse.core.runtime.IPath;
import org.eclipse.core.runtime.IStatus;
import org.eclipse.core.runtime.Status;
import org.eclipse.ui.plugin.AbstractUIPlugin;
import org.osgi.framework.Bundle;
import org.osgi.framework.BundleContext;
import org.osgi.framework.ServiceReference;

import java.net.URL;

/**
 * The activator class controls the plug-in life cycle
 */
public class Activator extends AbstractUIPlugin {

    // The plug-in ID
    public static final String PLUGIN_ID = "com.android.ide.eclipse.ndk"; //$NON-NLS-1$

    // The shared instance
    private static Activator mPlugin;

    @Override
    public void start(BundleContext context) throws Exception {
        super.start(context);
        mPlugin = this;
    }

    @Override
    public void stop(BundleContext context) throws Exception {
        mPlugin = null;
        super.stop(context);
    }

    public static Activator getDefault() {
        return mPlugin;
    }

    public static <T> T getService(Class<T> clazz) {
        BundleContext context = mPlugin.getBundle().getBundleContext();
        ServiceReference ref = context.getServiceReference(clazz.getName());
        return (ref != null) ? (T) context.getService(ref) : null;
    }

    public static Bundle getBundle(String id) {
        for (Bundle bundle : mPlugin.getBundle().getBundleContext().getBundles()) {
            if (bundle.getSymbolicName().equals(id)) {
                return bundle;
            }
        }
        return null;
    }

    public static IStatus newStatus(Exception e) {
        return new Status(IStatus.ERROR, PLUGIN_ID, e.getMessage(), e);
    }

    public static void log(Exception e) {
        mPlugin.getLog().log(newStatus(e));
    }

    public static URL findFile(IPath path) {
        return FileLocator.find(mPlugin.getBundle(), path, null);
    }

}