]> gerrit.simantics Code Review - simantics/platform.git/blob - bundles/org.simantics.platform.ui/src/org/simantics/platform/ui/internal/Activator.java
Fix SimanticsConsole activation invalid thread access problem
[simantics/platform.git] / bundles / org.simantics.platform.ui / src / org / simantics / platform / ui / internal / Activator.java
1 /*******************************************************************************
2  * Copyright (c) 2019 Association for Decentralized Information Management
3  * in Industry THTH ry.
4  * All rights reserved. This program and the accompanying materials
5  * are made available under the terms of the Eclipse Public License v1.0
6  * which accompanies this distribution, and is available at
7  * http://www.eclipse.org/legal/epl-v10.html
8  *
9  * Contributors:
10  *     Semantum Oy - initial API and implementation
11  *******************************************************************************/
12 package org.simantics.platform.ui.internal;
13 import java.io.PrintWriter;
14 import java.io.StringWriter;
15
16 import org.eclipse.ui.PlatformUI;
17 import org.eclipse.ui.plugin.AbstractUIPlugin;
18 import org.osgi.framework.BundleContext;
19 import org.osgi.framework.ServiceReference;
20 import org.simantics.UnhandledExceptionService;
21 import org.simantics.platform.ui.SimanticsConsole;
22 import org.slf4j.Logger;
23 import org.slf4j.LoggerFactory;
24
25 public class Activator extends AbstractUIPlugin {
26
27     private BundleContext context;
28
29     public Activator() {
30     }
31
32     @Override
33     public void start(BundleContext context) throws Exception {
34         super.start(context);
35         this.context = context;
36
37         if (PlatformUI.isWorkbenchRunning())
38             PlatformUI.getWorkbench().getDisplay().asyncExec(this::initConsole);
39     }
40
41     @Override
42     public void stop(BundleContext context) throws Exception {
43         context = null;
44         super.stop(context);
45     }
46
47     private void initConsole() {
48         if (PlatformUI.getWorkbench().getDisplay().isDisposed())
49              return;
50
51         SimanticsConsole console = SimanticsConsole.findConsole();
52         if (console != null) {
53             ServiceReference<?> ref = context.getServiceReference(UnhandledExceptionService.class.getName());
54             if (ref != null) {
55                 UnhandledExceptionService service = (UnhandledExceptionService) context.getService(ref);
56                 service.registerHandler(t -> {
57                     StringWriter sw = new StringWriter();
58                     PrintWriter pw = new PrintWriter(sw);
59                     t.printStackTrace(pw);
60                     console.write(sw.toString());
61                 });
62             }
63             ConsoleAppender ca = new ConsoleAppender(console);
64             ch.qos.logback.classic.Logger logbackLogger =
65                     (ch.qos.logback.classic.Logger) LoggerFactory.getLogger(Logger.ROOT_LOGGER_NAME);
66             logbackLogger.addAppender(ca);
67             ca.start();
68         }
69     }
70
71 }