]> git.argeo.org Git - gpl/argeo-slc.git/blob - org.argeo.slc.detached/src/main/java/org/argeo/slc/detached/AbstractDetachedActivator.java
Start introducing detached ui
[gpl/argeo-slc.git] / org.argeo.slc.detached / src / main / java / org / argeo / slc / detached / AbstractDetachedActivator.java
1 package org.argeo.slc.detached;
2
3 import java.net.URL;
4 import java.util.Properties;
5
6 import org.apache.commons.logging.Log;
7 import org.apache.commons.logging.LogFactory;
8 import org.osgi.framework.Bundle;
9 import org.osgi.framework.BundleActivator;
10 import org.osgi.framework.BundleContext;
11 import org.springframework.beans.factory.support.BeanDefinitionRegistry;
12 import org.springframework.beans.factory.xml.XmlBeanDefinitionReader;
13 import org.springframework.context.support.AbstractApplicationContext;
14 import org.springframework.context.support.GenericApplicationContext;
15 import org.springframework.core.io.UrlResource;
16
17 public class AbstractDetachedActivator implements BundleActivator {
18 private final Log log = LogFactory.getLog(getClass());
19
20 private SpringStaticRefProvider staticRefProvider;
21
22 public final void start(BundleContext context) throws Exception {
23
24 Bundle bundle = context.getBundle();
25
26 // Creates application context with this class class loader
27 ClassLoader classLoader = getClass().getClassLoader();
28 Thread cur = Thread.currentThread();
29 ClassLoader save = cur.getContextClassLoader();
30 cur.setContextClassLoader(classLoader);
31
32 try {
33 AbstractApplicationContext applicationContext = new GenericApplicationContext();
34 XmlBeanDefinitionReader xmlReader = new XmlBeanDefinitionReader(
35 (BeanDefinitionRegistry) applicationContext);
36
37 URL url = bundle.getResource("META-INF/spring/slc-detached.xml");
38 if (url != null) {
39 System.out.println("Loads application context from bundle "
40 + bundle.getSymbolicName() + " (url=" + url + ")");
41 xmlReader.loadBeanDefinitions(new UrlResource(url));
42
43 // Register static ref provider
44 staticRefProvider = new SpringStaticRefProvider(
45 applicationContext);
46 Properties properties = new Properties();
47 properties.setProperty("slc.detached.bundle", bundle
48 .getSymbolicName());
49 context.registerService(StaticRefProvider.class.getName(),
50 staticRefProvider, properties);
51
52 }
53
54 } catch (Exception e) {
55 e.printStackTrace();
56 throw new Exception("Could not initialize application context");
57 } finally {
58 cur.setContextClassLoader(save);
59 }
60
61 startAutoBundle(context);
62
63 log.info("SLC Detached bundle " + bundle.getSymbolicName() + " ("
64 + bundle.getBundleId() + ") started");
65 }
66
67 /** Does nothing by default. */
68 protected void startAutoBundle(BundleContext context) throws Exception {
69
70 }
71
72 public final void stop(BundleContext context) throws Exception {
73 stopAutoBundle(context);
74
75 if (staticRefProvider != null) {
76 staticRefProvider.close();
77 }
78
79 }
80
81 /** Does nothing by default. */
82 protected void stopAutoBundle(BundleContext context) throws Exception {
83
84 }
85
86 protected StaticRefProvider getStaticRefProvider() {
87 return staticRefProvider;
88 }
89 }