Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse files

created non-static version of bean container

  • Loading branch information...
commit 79f9766ab7676ece13c05c7a32a7590a623f49d5 1 parent 7aeeb6f
@cdiadvocate cdiadvocate authored
View
69 beancontainer/api/src/main/java/org/cdisource/beancontainer/BeanContainerManager.java
@@ -2,11 +2,16 @@
import java.io.PrintWriter;
import java.io.StringWriter;
+import java.lang.ref.WeakReference;
+import java.util.Collections;
+import java.util.Map;
import java.util.Properties;
import java.util.ServiceLoader;
+import java.util.WeakHashMap;
import org.cdisource.logging.Logger;
+
import static org.cdisource.logging.LogFactoryManager.logger;
@@ -54,10 +59,15 @@
/** Property name that we use to look up the bean container override. */
public static String PROP_NAME = "org.cdisource.beancontainer.BeanContainer";
- /**
- * Holds a singleton instance of a {@link BeanContainer}
- */
- private static BeanContainer instance;
+ private static Map<ClassLoader, WeakReference<BeanContainer>> map = Collections
+ .synchronizedMap(new WeakHashMap<ClassLoader, WeakReference<BeanContainer>>());
+
+ private static boolean useStatic = false;
+ private static BeanContainer beanContainer;
+
+ public static void testEnv() {
+ useStatic=true;
+ }
/**
* Thread safe method to create and initialize the {@link BeanContainer}
@@ -78,10 +88,6 @@ public static void initialize() {
*/
public synchronized static void initialize(Properties properties) {
log.trace("initialize(properties)");
-
- if (instance != null) {
- return;
- }
startUpInstance(properties);
}
@@ -101,15 +107,33 @@ public synchronized static void initialize(Properties properties) {
* @see {@link BeanContainerManager#initialize()}
* */
public static BeanContainer getInstance() {
+
+ if (useStatic && beanContainer!=null) {
+ return beanContainer;
+ }
log.trace("getInstance() called");
- if (instance == null) {
- log.debug("getInstance():: instance was null");
- initialize();
+ synchronized (map) {
+ WeakReference<BeanContainer> weakReference = map.get(Thread.currentThread().getContextClassLoader());
+ if (weakReference==null || weakReference.get()==null) {
+ log.debug("getInstance():: instance was null");
+ initialize();
+ }
+ return beanContainer();
+ }
+ }
+
+ private static BeanContainer beanContainer() {
+ if (useStatic && beanContainer!=null) {
+ return beanContainer;
+ }
+ WeakReference<BeanContainer> weakReference = map.get(Thread.currentThread().getContextClassLoader());
+ if (weakReference== null || weakReference.get() == null) {
+ throw new IllegalStateException("WeakReference<BeanContainer> is not set");
}
- log.debug("getInstance():: instance is null? %s", instance == null ? "yes" : "no");
- return instance;
+ return weakReference.get();
}
+
/**
* Create the bean container instance using system properties. If this method
* is called before one of the initialize methods is called, then the
@@ -140,15 +164,16 @@ public static BeanContainer createInstance() {
*/
private synchronized static void startUpInstance(Properties properties) {
log.trace("startUpInstance(properties)");
- // double check that the instance is null, someone might have created
- // it while we were entering this method.
- if (instance != null) {
- return;
- }
- instance = generateInstance(properties);
+ BeanContainer instance = generateInstance(properties);
if (instance != null) {
instance.start();
}
+
+ if (useStatic) {
+ beanContainer= instance;
+ } else {
+ map.put(Thread.currentThread().getContextClassLoader(), new WeakReference<BeanContainer>(instance));
+ }
}
/**
@@ -213,9 +238,9 @@ private static BeanContainer generateInstance(Properties properties) {
* a new instance.
*/
public static synchronized void shutdown() {
- if (instance != null) {
- instance.stop();
- instance = null;
+ BeanContainer beanContainer = beanContainer();
+ if (beanContainer!=null) {
+ beanContainer.stop();
}
}
}
View
2  beancontainer/testing/src/main/java/org/cdisource/testing/AbstractBeanContainerTest.java
@@ -23,7 +23,7 @@
*/
public class AbstractBeanContainerTest {
- private BeanContainer beanContainer;
+ private static BeanContainer beanContainer;
public BeanContainer getBeanContainer() {
return beanContainer;
View
17 spring/pom.xml
@@ -85,11 +85,24 @@
</dependency>
<dependency>
<groupId>org.springframework</groupId>
+ <artifactId>spring-web</artifactId>
+ <version>${spring.version}</version>
+ </dependency>
+
+ <dependency>
+ <groupId>org.springframework</groupId>
<artifactId>spring-tx</artifactId>
<version>${spring.version}</version>
</dependency>
<dependency>
+ <groupId>org.apache.geronimo.specs</groupId>
+ <artifactId>geronimo-servlet_3.0_spec</artifactId>
+ <version>1.0</version>
+ <scope>provided</scope>
+ </dependency>
+
+ <dependency>
<groupId>org.cdisource.beancontainer</groupId>
<artifactId>beancontainer-api</artifactId>
<version>1.0-SNAPSHOT</version>
@@ -228,10 +241,6 @@
<configuration>
<useSystemClassLoader>true</useSystemClassLoader>
<useManifestOnlyJar>false</useManifestOnlyJar>
- <excludes>
- <exclude>**/*SpringIntegrationExtentionTest*</exclude>
- <exclude>**/*CdiBeanThatHasSpringInjection*</exclude>
- </excludes>
</configuration>
</plugin>
</plugins>
View
30 .../java/org/cdisource/springintegration/servletsupport/ApplicationContextFinderServletContextListener.java
@@ -0,0 +1,30 @@
+package org.cdisource.springintegration.servletsupport;
+
+import javax.servlet.ServletContextEvent;
+import javax.servlet.ServletContextListener;
+import javax.servlet.annotation.WebListener;
+
+import org.cdisource.springintegration.springsupport.ApplicationContextLocator;
+import org.cdisource.springintegration.springsupport.ApplicationContextLocatorImpl;
+import org.cdisource.springintegration.springsupport.ApplicationContextLocatorManager;
+import org.springframework.web.context.WebApplicationContext;
+import org.springframework.web.context.support.WebApplicationContextUtils;
+
+@WebListener
+public class ApplicationContextFinderServletContextListener implements ServletContextListener {
+
+
+ @Override
+ public void contextInitialized(ServletContextEvent sce) {
+ WebApplicationContext requiredWebApplicationContext = WebApplicationContextUtils.getRequiredWebApplicationContext(sce.getServletContext());
+ ApplicationContextLocatorImpl.putContext(requiredWebApplicationContext);
+ ApplicationContextLocator applicationContextLocator = ApplicationContextLocatorManager.getInstance();
+ sce.getServletContext().setAttribute(ApplicationContextLocator.class.getName(), applicationContextLocator);
+ }
+
+ @Override
+ public void contextDestroyed(ServletContextEvent sce) {
+ sce.getServletContext().setAttribute(ApplicationContextLocator.class.getName(), null);
+ }
+
+}
View
17 spring/src/main/resources/META-INF/web-fragment.xml
@@ -0,0 +1,17 @@
+<?xml version="1.0" encoding="UTF-8"?>
+
+<web-fragment version="3.0" xmlns="http://java.sun.com/xml/ns/javaee" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-fragment_3_0.xsd">
+
+ <name>Spring CDI Bridge</name>
+
+ <ordering><after><others/></after></ordering>
+
+ <listener>
+ <display-name></display-name>
+
+ <listener-class>org.cdisource.springintegration.servletsupport.ApplicationContextFinderServletContextListener</listener-class>
+
+ </listener>
+
+
+</web-fragment>
View
8 testing/junit/src/main/java/org/cdisource/testing/junit/CdiTestRunner.java
@@ -4,6 +4,7 @@
import javax.inject.Inject;
import org.cdisource.beancontainer.BeanContainer;
+import org.cdisource.beancontainer.BeanContainerImpl;
import org.cdisource.beancontainer.BeanContainerManager;
import org.junit.runner.RunWith;
import org.junit.runner.notification.RunNotifier;
@@ -48,7 +49,6 @@ public CdiTestRunner(Class<?> klass) throws InitializationError {
private static BeanContainer beanContainer;
- private RunConfig runConfig;
/**
* Lazy initializes a bean container that exists on the classpath.
@@ -56,17 +56,19 @@ public CdiTestRunner(Class<?> klass) throws InitializationError {
*
* @return instance of the CDI Bean container
*/
- public static BeanContainer getBeanContainer() {
+ public static BeanContainer getBeanContainer() {
+ BeanContainerManager.testEnv();
if (beanContainer == null) {
beanContainer = BeanContainerManager.getInstance();
}
+
return beanContainer;
}
@Override
protected Object createTest() throws Exception {
Class<?> clazz = getTestClass().getJavaClass();
- this.runConfig = clazz.getAnnotation(RunConfig.class);
+ //this.runConfig = clazz.getAnnotation(RunConfig.class);
Object result = getBeanContainer().getBeanByType(clazz);
if (result == null) {
View
7 web/pom.xml
@@ -58,6 +58,13 @@
<scope>test</scope>
<version>1.0-SNAPSHOT</version>
</dependency>
+
+ <dependency>
+ <groupId>org.apache.geronimo.specs</groupId>
+ <artifactId>geronimo-servlet_3.0_spec</artifactId>
+ <version>1.0</version>
+ <scope>provided</scope>
+ </dependency>
</dependencies>
<build>
View
23 web/src/main/java/org/cdisource/web/CDIServletContextListener.java
@@ -0,0 +1,23 @@
+package org.cdisource.web;
+
+import javax.servlet.ServletContextEvent;
+import javax.servlet.ServletContextListener;
+import javax.servlet.annotation.WebListener;
+
+import org.cdisource.beancontainer.BeanContainer;
+import org.cdisource.beancontainer.BeanContainerManager;
+
+@WebListener
+public class CDIServletContextListener implements ServletContextListener {
+
+ @Override
+ public void contextInitialized(ServletContextEvent sce) {
+ sce.getServletContext().setAttribute(BeanContainer.class.getName(), BeanContainerManager.getInstance());
+ }
+
+ @Override
+ public void contextDestroyed(ServletContextEvent sce) {
+ sce.getServletContext().setAttribute(BeanContainer.class.getName(), null);
+ }
+
+}
Please sign in to comment.
Something went wrong with that request. Please try again.