]> git.argeo.org Git - gpl/argeo-slc.git/blob - org.argeo.slc.autoui/src/main/java/org/argeo/slc/autoui/AbstractDetachedActivator.java
7f18d50a4d0c6c652432bddcc5c64a56459ee7af
[gpl/argeo-slc.git] / org.argeo.slc.autoui / src / main / java / org / argeo / slc / autoui / AbstractDetachedActivator.java
1 package org.argeo.slc.autoui;
2
3 import java.net.URL;
4
5 import org.osgi.framework.Bundle;
6 import org.osgi.framework.BundleActivator;
7 import org.osgi.framework.BundleContext;
8 import org.springframework.beans.factory.support.BeanDefinitionRegistry;
9 import org.springframework.beans.factory.xml.XmlBeanDefinitionReader;
10 import org.springframework.context.support.AbstractApplicationContext;
11 import org.springframework.context.support.GenericApplicationContext;
12 import org.springframework.core.io.UrlResource;
13
14 public class AbstractDetachedActivator implements BundleActivator {
15 private AbstractApplicationContext applicationContext;
16
17 public final void start(BundleContext context) throws Exception {
18 ClassLoader classLoader = getClass().getClassLoader();
19
20 Thread cur = Thread.currentThread();
21 ClassLoader save = cur.getContextClassLoader();
22 cur.setContextClassLoader(classLoader);
23
24 try {
25 // applicationContext = new ClassPathXmlApplicationContext(
26 // "/slc/conf/applicationContext.xml");
27
28 applicationContext = new GenericApplicationContext();
29 XmlBeanDefinitionReader xmlReader = new XmlBeanDefinitionReader(
30 (BeanDefinitionRegistry) applicationContext);
31 Bundle bundle = context.getBundle();
32
33 URL url = bundle
34 .getResource("META-INF/slc/conf/applicationContext.xml");
35 if (url != null) {
36 System.out.println("Loads application context from bundle "
37 + bundle.getSymbolicName() + " (url=" + url + ")");
38 xmlReader.loadBeanDefinitions(new UrlResource(url));
39 }
40
41 } catch (Exception e) {
42 e.printStackTrace();
43 throw new Exception("Could not initialize application context");
44 } finally {
45 cur.setContextClassLoader(save);
46 }
47
48 startAutoBundle(context);
49 }
50
51 /** Does nothing by default. */
52 protected void startAutoBundle(BundleContext context) throws Exception {
53
54 }
55
56 public final void stop(BundleContext context) throws Exception {
57 stopAutoBundle(context);
58
59 if (applicationContext != null) {
60 applicationContext.close();
61 }
62
63 }
64
65 /** Does nothing by default. */
66 protected void stopAutoBundle(BundleContext context) throws Exception {
67
68 }
69
70 public Object getStaticRef(String id) {
71 return applicationContext.getBean(id);
72 }
73
74 }