]> git.argeo.org Git - gpl/argeo-slc.git/commitdiff
Centralize Spring initialization
authorMathieu Baudier <mbaudier@argeo.org>
Fri, 26 Sep 2008 14:40:56 +0000 (14:40 +0000)
committerMathieu Baudier <mbaudier@argeo.org>
Fri, 26 Sep 2008 14:40:56 +0000 (14:40 +0000)
git-svn-id: https://svn.argeo.org/slc/trunk@1640 4cfe0d0a-d680-48aa-b62c-e0a02a3f76cc

org.argeo.slc.autoui/pom.xml
org.argeo.slc.autoui/src/main/java/org/argeo/slc/autoui/AbstractAutoActivator.java [new file with mode: 0644]

index cbb20cc5251c8b4489eff831404e7921dc8f8c46..4bf4354928a42d7180af1ff8b9988f0e27a5f4b1 100644 (file)
                        <artifactId>log4j</artifactId>
                        </dependency>
                -->
-               <dependency>
-                       <groupId>org.argeo.dep.jemmy</groupId>
-                       <artifactId>org.argeo.dep.jemmy.nb61</artifactId>
-               </dependency>
 
                <!-- OSGi 
                        <dependency>
                        <artifactId>osgi</artifactId>
                        <version>3.3.0-v20070530</version>
                </dependency>
+
+               <dependency>
+                       <groupId>org.springframework</groupId>
+                       <artifactId>spring-context</artifactId>
+                       <version>2.0.8</version>
+               </dependency>
+
+
        </dependencies>
 </project>
\ No newline at end of file
diff --git a/org.argeo.slc.autoui/src/main/java/org/argeo/slc/autoui/AbstractAutoActivator.java b/org.argeo.slc.autoui/src/main/java/org/argeo/slc/autoui/AbstractAutoActivator.java
new file mode 100644 (file)
index 0000000..59c9746
--- /dev/null
@@ -0,0 +1,74 @@
+package org.argeo.slc.autoui;
+
+import java.net.URL;
+
+import org.osgi.framework.Bundle;
+import org.osgi.framework.BundleActivator;
+import org.osgi.framework.BundleContext;
+import org.springframework.beans.factory.support.BeanDefinitionRegistry;
+import org.springframework.beans.factory.xml.XmlBeanDefinitionReader;
+import org.springframework.context.support.AbstractApplicationContext;
+import org.springframework.context.support.GenericApplicationContext;
+import org.springframework.core.io.UrlResource;
+
+public class AbstractAutoActivator implements BundleActivator {
+       private AbstractApplicationContext applicationContext;
+
+       public final void start(BundleContext context) throws Exception {
+               ClassLoader classLoader = getClass().getClassLoader();
+
+               Thread cur = Thread.currentThread();
+               ClassLoader save = cur.getContextClassLoader();
+               cur.setContextClassLoader(classLoader);
+
+               try {
+                       // applicationContext = new ClassPathXmlApplicationContext(
+                       // "/slc/conf/applicationContext.xml");
+
+                       applicationContext = new GenericApplicationContext();
+                       XmlBeanDefinitionReader xmlReader = new XmlBeanDefinitionReader(
+                                       (BeanDefinitionRegistry) applicationContext);
+                       Bundle bundle = context.getBundle();
+
+                       URL url = bundle
+                                       .getResource("META-INF/slc/conf/applicationContext.xml");
+                       if (url != null) {
+                               System.out.println("Loads application context from bundle "
+                                               + bundle.getSymbolicName() + " (url=" + url + ")");
+                               xmlReader.loadBeanDefinitions(new UrlResource(url));
+                       }
+
+               } catch (Exception e) {
+                       e.printStackTrace();
+                       throw new Exception("Could not initialize application context");
+               } finally {
+                       cur.setContextClassLoader(save);
+               }
+
+               startAutoBundle(context);
+       }
+
+       /** Does nothing by default. */
+       protected void startAutoBundle(BundleContext context) throws Exception {
+
+       }
+
+       public final void stop(BundleContext context) throws Exception {
+               stopAutoBundle(context);
+
+               if (applicationContext != null) {
+                       applicationContext.close();
+               }
+
+       }
+
+       /** Does nothing by default. */
+       protected void stopAutoBundle(BundleContext context) throws Exception {
+
+       }
+
+       public Object getStaticRef(String id) {
+               return applicationContext.getBean(id);
+       }
+
+}