Injecting MC beans into the servlet context
julien1 Jun 14, 2007 11:10 AM- MC is able to pick beans from a factory.
- MC is able to inject beans into a Map.
given that we can have a bean factory that produces a POJO that has a Map as property. That Map is a wrapper around the ServletContext attributes.
public class ServletContextAttributes
{
private final ServletContext context;
public ServletContextAttributesInj(ServletContext context)
{
this.context = context;
}
public void setAttributes(Map attrs)
{
for (Iterator i = attrs.entrySet().iterator();i.hasNext();)
{
Map.Entry entry = (Map.Entry)i.next();
String name = (String)entry.getKey();
Object value = entry.getValue();
context.setAttribute(name, value);
}
}
}
For the bean factory one already exist in the test module : org.jboss.portal.test.framework.TestBeanFactory
Then at startup of the servlet context listener we would have something like:
public void contextInitialized(ServletContextEvent event)
{
// Create wrapper that configures
ServletContextAttributes attrs = new ServletContextAttributes(event.getServletContext());
// Configure MC
TestRuntimeContext runtimeContext = new TestRuntimeContext("jboss-beans.xml");
runtimeContext.addBean("ServletContextAttributes", attrs);
runtimeContext.start();
}
and in jboss-beans.xml we would have something like :
<deployment ...> ... <!-- Declare all beans that are wired like AuthorizationDomainRegistry, etc... --> <!-- That's one bean that interest us to be injected in the servlet context as an attribute --> <bean name="PortalObjectContainer" class="org.jboss.portal.core.impl.model.portal.PersistentPortalObjectContainer"> <property name="sessionFactoryJNDIName">java:/SessionFactory</property> <property name="authorizationDomainRegistry"><inject bean="AuthorizationDomainRegistry"/></property> <property name="portalAuthorizationManagerFactory"><inject bean="AuthorizationManagerFactory"/></property> <property name="contentProviderRegistry"><inject bean="ContentProviderRegistry"/></property> </bean> <bean name="ServletContextAttributes" class="ServletContextAttributes"> <constructor factoryMethod="getBean"> <factory bean="BeanFactory"/> <parameter>ServletContextAttributes</parameter> </constructor> <property name="attributes"> <map keyClass="java.lang.String"> <entry> <key>PortalObjectContainer</key> <value><inject bean="PortalObjectContainer"/></value> </entry> </map> </property> </bean> </deployment>