Skip to main content
aboutsummaryrefslogtreecommitdiffstats
blob: 84d959e9bdddd7531be571082b47288c5fe7ea98 (plain) (blame)
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
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
/*******************************************************************************
 * Copyright (c) 2007, 2013 Wind River Systems, 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
 *
 * Contributors:
 *     Wind River Systems - initial API and implementation
 *******************************************************************************/
package org.eclipse.tcf;

import org.eclipse.core.runtime.CoreException;
import org.eclipse.core.runtime.IConfigurationElement;
import org.eclipse.core.runtime.IExtension;
import org.eclipse.core.runtime.IExtensionPoint;
import org.eclipse.core.runtime.IExtensionRegistry;
import org.eclipse.core.runtime.ILog;
import org.eclipse.core.runtime.IProgressMonitor;
import org.eclipse.core.runtime.IStatus;
import org.eclipse.core.runtime.Platform;
import org.eclipse.core.runtime.Plugin;
import org.eclipse.core.runtime.Status;
import org.eclipse.core.runtime.jobs.Job;
import org.eclipse.osgi.util.NLS;
import org.eclipse.tcf.core.ChannelTCP;
import org.eclipse.tcf.internal.nls.TcfPluginMessages;
import org.eclipse.tcf.protocol.ILogger;
import org.eclipse.tcf.protocol.IServiceProvider;
import org.eclipse.tcf.protocol.Protocol;
import org.eclipse.tcf.ssl.TCFSecurityManager;
import org.osgi.framework.Bundle;
import org.osgi.framework.BundleContext;
import org.osgi.framework.BundleEvent;
import org.osgi.framework.BundleListener;


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

    public static final String PLUGIN_ID = "org.eclipse.tcf"; //$NON-NLS-1$

    private static Activator plugin;
    private static boolean debug;
    private static final EventQueue queue = new EventQueue();
    private static final BundleListener bundle_listener = new BundleListener() {
        private boolean started = false;
        public void bundleChanged(BundleEvent event) {
            if (plugin != null && !started &&
                    event.getBundle() == plugin.getBundle() &&
                    plugin.getBundle().getState() == Bundle.ACTIVE) {
                queue.start();
                started = true;
            }
        }
    };

    /** Eclipse tracing option, plug-in wide */
    private static boolean TRACE;

    /**
     * Constructor.
     */
    public Activator() {
        plugin = this;
    }

    /**
     * Returns the shared instance
     *
     * @return the shared instance
     */
    public static Activator getDefault() {
        return plugin;
    }

    /* (non-Javadoc)
     * @see org.eclipse.core.runtime.Plugin#start(org.osgi.framework.BundleContext)
     */
    @Override
    public void start(BundleContext context) throws Exception {
        super.start(context);
        debug = Platform.inDebugMode();

        TRACE = "true".equals(Platform.getDebugOption("org.eclipse.tcf/debug")); //$NON-NLS-1$
        if (TRACE && "true".equals(Platform.getDebugOption("org.eclipse.tcf/debug/discovery"))) {
            System.setProperty("org.eclipse.tcf.core.tracing.discovery", "true");
        }
        if (TRACE && "true".equals(Platform.getDebugOption("org.eclipse.tcf/debug/channel"))) {
            System.setProperty("org.eclipse.tcf.core.tracing.channel", "true");
        }

        ChannelTCP.setSSLContext(TCFSecurityManager.createSSLContext());
        Protocol.setLogger(new ILogger() {

            public void log(final String msg, final Throwable x) {
                // Normally, we hook the TCF logging service (ILogger) to the
                // Plug-in logger. Trace hooks in the code use the TCF logger.
                // The Plug-in logger isn't really designed for large amounts of
                // trace data, though, so redirect to stdout when tracing is
                // enabled.
                if (TRACE) {
                    System.out.println(msg);
                    if (x != null) x.printStackTrace();
                }
                else {
                    if (debug) {
                        System.err.println(msg);
                        if (x != null) x.printStackTrace();
                    }
                    if (plugin != null) {
                        final ILog logger = getLog();
                        if (logger != null) {
                            // Do not call logger on TCF thread - it can cause deadlock,
                            // because Eclipse log listeners (e.g. IDEWorkbenchErrorHandler)
                            // can call Display.syncExec(), which is not allowed
                            // on the TCF dispatch thread.
                            Job job = new Job("TCF Log") {
                                @Override
                                protected IStatus run(IProgressMonitor monitor) {
                                    logger.log(new Status(IStatus.ERROR,
                                            getBundle().getSymbolicName(), IStatus.OK, msg, x));
                                    return Status.OK_STATUS;
                                }
                            };
                            job.setPriority(Job.SHORT);
                            job.setSystem(true);
                            job.schedule();
                        }
                    }
                }
            }
        });
        /*
         * Starts the timer_queue and sets the event_queue
         */
        Protocol.setEventQueue(queue);
        Protocol.invokeLater(new Runnable() {
            public void run() {
                runTCFStartup();
            }
        });
        context.addBundleListener(bundle_listener);
    }

    /* (non-Javadoc)
     * @see org.eclipse.core.runtime.Plugin#stop(org.osgi.framework.BundleContext)
     */
    @Override
    public void stop(BundleContext context) throws Exception {
        context.removeBundleListener(bundle_listener);
        queue.shutdown();
        plugin = null;
        super.stop(context);
    }

    private void runTCFStartup() {
        try {
            IExtensionPoint point = Platform.getExtensionRegistry().getExtensionPoint(PLUGIN_ID, "startup"); //$NON-NLS-1$
            IExtension[] extensions = point.getExtensions();
            for (int i = 0; i < extensions.length; i++) {
                try {
                    Bundle bundle = Platform.getBundle(extensions[i].getNamespaceIdentifier());
                    bundle.start(Bundle.START_TRANSIENT);
                    IConfigurationElement[] e = extensions[i].getConfigurationElements();
                    for (int j = 0; j < e.length; j++) {
                        String nm = e[j].getName();
                        if (nm.equals("class")) { //$NON-NLS-1$
                            Class<?> c = bundle.loadClass(e[j].getAttribute("name")); //$NON-NLS-1$
                            Class.forName(c.getName(), true, c.getClassLoader());
                        }
                    }
                }
                catch (Throwable x) {
                    Protocol.log("TCF startup error", x); //$NON-NLS-1$
                }
            }
        }
        catch (Exception x) {
            Protocol.log("TCF startup error", x); //$NON-NLS-1$
        }

        // Register service providers contributed via Eclipse extension point
        IExtensionRegistry registry = Platform.getExtensionRegistry();
        IExtensionPoint point = registry.getExtensionPoint("org.eclipse.tcf.serviceProviders"); //$NON-NLS-1$
        if (point != null) {
            IExtension[] extensions = point.getExtensions();
            for (IExtension extension : extensions) {
                IConfigurationElement[] elements = extension.getConfigurationElements();
                for (IConfigurationElement element : elements) {
                    if ("serviceProvider".equals(element.getName())) { //$NON-NLS-1$
                        try {
                            // Create the service provider instance
                            IServiceProvider provider = (IServiceProvider)element.createExecutableExtension("class"); //$NON-NLS-1$
                            if (provider != null) Protocol.addServiceProvider(provider);
                        } catch (CoreException e) {
                            IStatus status = new Status(IStatus.ERROR, Activator.PLUGIN_ID,
                                                        NLS.bind(TcfPluginMessages.Extension_error_invalidExtensionPoint, element.getDeclaringExtension().getUniqueIdentifier()),
                                                        e);
                            Activator.getDefault().getLog().log(status);
                        }
                    }
                }
            }
        }
    }
}

Back to the top