Skip to content

Commit

Permalink
WELD-202, and use it in integration code
Browse files Browse the repository at this point in the history
  • Loading branch information
pmuir committed Nov 3, 2009
1 parent 15ba677 commit 85ca957
Show file tree
Hide file tree
Showing 2 changed files with 33 additions and 8 deletions.
15 changes: 7 additions & 8 deletions impl/src/main/java/org/jboss/weld/servlet/ServletHelper.java
Expand Up @@ -16,12 +16,10 @@
*/
package org.jboss.weld.servlet;

import javax.enterprise.inject.spi.BeanManager;
import javax.servlet.ServletContext;

import org.jboss.weld.BeanManagerImpl;
import org.jboss.weld.Container;
import org.jboss.weld.bootstrap.spi.BeanDeploymentArchive;
import org.jboss.weld.servlet.api.ServletServices;

/**
* @author pmuir
Expand All @@ -36,14 +34,15 @@ public static BeanManagerImpl getModuleBeanManager(ServletContext ctx)
{
throw new IllegalArgumentException("ServletContext is null");
}
BeanDeploymentArchive beanDeploymentArchive = Container.instance().deploymentServices().get(ServletServices.class).getBeanDeploymentArchive(ctx);
BeanManagerImpl beanManagerImpl = Container.instance().beanDeploymentArchives().get(beanDeploymentArchive);
BeanManagerImpl beanManagerImpl = (BeanManagerImpl) ctx.getAttribute(BeanManager.class.getName());
if (beanManagerImpl == null)
{
throw new IllegalArgumentException("Unable to find BeanManager. BeanDeploymentArchive: " + beanDeploymentArchive + "; ServletContext: " + ctx);
throw new IllegalArgumentException("Unable to find BeanManager. ServletContext: " + ctx);
}
else
{
return beanManagerImpl;
}
// Actually we need the manager for the current activity
return beanManagerImpl.getCurrent();
}

}
26 changes: 26 additions & 0 deletions impl/src/main/java/org/jboss/weld/servlet/WeldListener.java
Expand Up @@ -22,12 +22,18 @@
*/
package org.jboss.weld.servlet;

import javax.enterprise.inject.spi.BeanManager;
import javax.servlet.ServletContext;
import javax.servlet.ServletContextEvent;
import javax.servlet.ServletRequestEvent;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSessionEvent;

import org.jboss.weld.BeanManagerImpl;
import org.jboss.weld.Container;
import org.jboss.weld.bootstrap.spi.BeanDeploymentArchive;
import org.jboss.weld.context.ContextLifecycle;
import org.jboss.weld.servlet.api.ServletServices;
import org.jboss.weld.servlet.api.helpers.AbstractServletListener;

/**
Expand All @@ -53,6 +59,26 @@ private ServletLifecycle getLifecycle()
}
return lifecycle;
}

private static BeanManagerImpl getBeanManager(ServletContext ctx)
{
BeanDeploymentArchive war = Container.instance().deploymentServices().get(ServletServices.class).getBeanDeploymentArchive(ctx);
return Container.instance().beanDeploymentArchives().get(war);
}

@Override
public void contextInitialized(ServletContextEvent sce)
{
super.contextInitialized(sce);
sce.getServletContext().setAttribute(BeanManager.class.getName(), getBeanManager(sce.getServletContext()));
}

@Override
public void contextDestroyed(ServletContextEvent sce)
{
sce.getServletContext().removeAttribute(BeanManager.class.getName());
super.contextDestroyed(sce);
}

/**
* Called when the session is created
Expand Down

0 comments on commit 85ca957

Please sign in to comment.