View Javadoc
1   package io.github.magwas.inez.parse;
2   
3   import java.util.Hashtable;
4   
5   import org.osgi.framework.Bundle;
6   import org.osgi.framework.BundleActivator;
7   import org.osgi.framework.BundleContext;
8   import org.osgi.framework.ServiceRegistration;
9   import org.osgi.framework.wiring.BundleWiring;
10  import org.springframework.beans.factory.annotation.Autowired;
11  import org.springframework.boot.SpringApplication;
12  import org.springframework.boot.autoconfigure.SpringBootApplication;
13  import org.springframework.context.ConfigurableApplicationContext;
14  import org.springframework.context.annotation.AnnotationConfigApplicationContext;
15  import org.springframework.context.annotation.ComponentScan;
16  import org.springframework.util.Assert;
17  
18  @SpringBootApplication
19  @ComponentScan(basePackages = { "io.github.magwas" })
20  public class ParserBundleActivator implements BundleActivator {
21  
22  	ConfigurableApplicationContext appContext;
23  
24  	@Autowired
25  	ParseTextService parseText;
26  
27  	@Override
28  	public void start(BundleContext bundleContext) {
29  		Bundle bundle = bundleContext.getBundle();
30  		BundleWiring bundleWiring = bundle.adapt(BundleWiring.class);
31  		ClassLoader classLoader = bundleWiring.getClassLoader();
32  		Thread.currentThread().setContextClassLoader(classLoader);
33  		appContext = new AnnotationConfigApplicationContext(
34  				ParserBundleActivator.class);
35  		parseText = appContext.getBean(ParseTextService.class);
36  		Assert.notNull(parseText, "parseText is null");
37  		ServiceRegistration<ParseTextService> registration = bundleContext
38  				.registerService(ParseTextService.class, parseText,
39  						new Hashtable<String, String>());
40  		System.err.println("registered ParseTextService:" + registration);
41  	}
42  
43  	@Override
44  	public void stop(BundleContext bundleContext) {
45  		SpringApplication.exit(appContext, () -> 0);
46  	}
47  
48  }