a14be489618d659c6bf394405bce1e2d8868ac04
[simantics/platform.git] / bundles / org.simantics.scl.reflection / src / org / simantics / scl / reflection / internal / Activator.java
1 package org.simantics.scl.reflection.internal;
2
3 import java.util.concurrent.ForkJoinPool;
4
5 import org.eclipse.core.runtime.ILog;
6 import org.eclipse.core.runtime.Platform;
7 import org.eclipse.core.runtime.Status;
8 import org.osgi.framework.Bundle;
9 import org.osgi.framework.BundleActivator;
10 import org.osgi.framework.BundleContext;
11 import org.simantics.scl.reflection.internal.registry.BindingRegistry;
12 import org.slf4j.Logger;
13 import org.slf4j.LoggerFactory;
14
15 public class Activator implements BundleActivator {
16
17     private static final Logger LOGGER = LoggerFactory.getLogger(Activator.class);
18     private BundleContext context;  
19     
20     private static Activator instance;
21     
22     @Override
23     public void start(BundleContext context) throws Exception {
24         this.context = context;
25         instance = this;
26         
27         primeBindingRegistry();
28     }
29
30     private static void primeBindingRegistry() {
31         LOGGER.info("Priming BindingRegistry");
32         ForkJoinPool.commonPool().submit(() -> {
33             try {
34                 // this forces static initialzation of the registry in advance
35                 BindingRegistry.primeBindingRegistry();
36             } catch (Exception e) {
37                 LOGGER.error("Could not prime binding registry", e);
38             }
39             LOGGER.info("Priming done");
40         });
41     }
42
43     @Override
44     public void stop(BundleContext context) throws Exception {
45         instance = null;
46         this.context = null;
47     }
48     
49     public BundleContext getContext() {
50         return context;
51     }
52     
53     public static Activator getInstance() {
54         return instance;
55     }
56         
57     public static void logError(String description, Exception e) {
58         LOGGER.error(description);
59         if(e != null)
60             e.printStackTrace();
61         Bundle bundle = getInstance().getContext().getBundle();
62         ILog log = Platform.getLog(bundle);
63         log.log(new Status(Status.ERROR, bundle.getSymbolicName(), description, e));
64     }
65     
66     public static void logError(String description) {
67         logError(description, null);
68     }
69
70 }