1 package org
.argeo
.slc
.detached
;
4 import java
.util
.Properties
;
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
;
17 public class AbstractDetachedActivator
implements BundleActivator
{
18 private final Log log
= LogFactory
.getLog(getClass());
20 private SpringStaticRefProvider staticRefProvider
;
22 public final void start(BundleContext context
) throws Exception
{
24 Bundle bundle
= context
.getBundle();
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
);
33 AbstractApplicationContext applicationContext
= new GenericApplicationContext();
34 XmlBeanDefinitionReader xmlReader
= new XmlBeanDefinitionReader(
35 (BeanDefinitionRegistry
) applicationContext
);
37 URL url
= bundle
.getResource("META-INF/spring/slc-detached.xml");
39 System
.out
.println("Loads application context from bundle "
40 + bundle
.getSymbolicName() + " (url=" + url
+ ")");
41 xmlReader
.loadBeanDefinitions(new UrlResource(url
));
43 // Register static ref provider
44 staticRefProvider
= new SpringStaticRefProvider(
46 Properties properties
= new Properties();
47 properties
.setProperty("slc.detached.bundle", bundle
49 context
.registerService(StaticRefProvider
.class.getName(),
50 staticRefProvider
, properties
);
54 } catch (Exception e
) {
56 throw new Exception("Could not initialize application context");
58 cur
.setContextClassLoader(save
);
61 startAutoBundle(context
);
63 log
.info("SLC Detached bundle " + bundle
.getSymbolicName() + " ("
64 + bundle
.getBundleId() + ") started");
67 /** Does nothing by default. */
68 protected void startAutoBundle(BundleContext context
) throws Exception
{
72 public final void stop(BundleContext context
) throws Exception
{
73 stopAutoBundle(context
);
75 if (staticRefProvider
!= null) {
76 staticRefProvider
.close();
81 /** Does nothing by default. */
82 protected void stopAutoBundle(BundleContext context
) throws Exception
{
86 protected StaticRefProvider
getStaticRefProvider() {
87 return staticRefProvider
;