Author: mstruk
Date: 2011-04-23 16:17:03 -0400 (Sat, 23 Apr 2011)
New Revision: 6336
Added:
sandbox/cdi_support/pc/branches/cdi/cdi/
sandbox/cdi_support/pc/branches/cdi/cdi/pom.xml
sandbox/cdi_support/pc/branches/cdi/cdi/src/
sandbox/cdi_support/pc/branches/cdi/cdi/src/main/
sandbox/cdi_support/pc/branches/cdi/cdi/src/main/java/
sandbox/cdi_support/pc/branches/cdi/cdi/src/main/java/org/
sandbox/cdi_support/pc/branches/cdi/cdi/src/main/java/org/gatein/
sandbox/cdi_support/pc/branches/cdi/cdi/src/main/java/org/gatein/pc/
sandbox/cdi_support/pc/branches/cdi/cdi/src/main/java/org/gatein/pc/cdi/
sandbox/cdi_support/pc/branches/cdi/cdi/src/main/java/org/gatein/pc/cdi/IntegrationService.java
sandbox/cdi_support/pc/branches/cdi/cdi/src/main/java/org/gatein/pc/cdi/portlet/
sandbox/cdi_support/pc/branches/cdi/cdi/src/main/java/org/gatein/pc/cdi/portlet/CDIPortletDecorator.java
sandbox/cdi_support/pc/branches/cdi/portlet/src/main/java/org/gatein/pc/portlet/impl/spi/PortletDecorator.java
Modified:
sandbox/cdi_support/pc/branches/cdi/portlet/src/main/java/org/gatein/pc/portlet/impl/jsr168/PortletContainerImpl.java
Log:
CDI support for portlets - PC container module, and portlet decorator infrastructure
through which CDI support is integrated
Added: sandbox/cdi_support/pc/branches/cdi/cdi/pom.xml
===================================================================
--- sandbox/cdi_support/pc/branches/cdi/cdi/pom.xml (rev 0)
+++ sandbox/cdi_support/pc/branches/cdi/cdi/pom.xml 2011-04-23 20:17:03 UTC (rev 6336)
@@ -0,0 +1,46 @@
+<?xml version="1.0" encoding="UTF-8"?>
+<project
xmlns="http://maven.apache.org/POM/4.0.0"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
+
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0
http://maven.apache.org/maven-v4_0_0.xsd">
+ <modelVersion>4.0.0</modelVersion>
+
+ <parent>
+ <groupId>org.gatein.pc</groupId>
+ <artifactId>pc-parent</artifactId>
+ <version>2.3.0-Beta02-SNAPSHOT</version>
+ </parent>
+
+ <artifactId>pc-cdi</artifactId>
+ <name>GateIn - Portlet Container (CDI support)</name>
+
+ <packaging>jar</packaging>
+
+ <dependencies>
+ <dependency>
+ <groupId>org.gatein.pc</groupId>
+ <artifactId>pc-portlet</artifactId>
+ <version>${project.version}</version>
+ </dependency>
+ <dependency>
+ <groupId>javax.portlet</groupId>
+ <artifactId>portlet-api</artifactId>
+ </dependency>
+ <dependency>
+ <groupId>javax.enterprise</groupId>
+ <artifactId>cdi-api</artifactId>
+ <version>1.0-SNAPSHOT</version>
+ </dependency>
+ <dependency>
+ <groupId>picocontainer</groupId>
+ <artifactId>picocontainer</artifactId>
+ <version>1.1</version>
+ </dependency>
+ <dependency>
+ <groupId>org.gatein.wci</groupId>
+ <artifactId>wci-wci</artifactId>
+ </dependency>
+ <dependency>
+ <groupId>javax.servlet</groupId>
+ <artifactId>servlet-api</artifactId>
+ </dependency>
+ </dependencies>
+</project>
Added:
sandbox/cdi_support/pc/branches/cdi/cdi/src/main/java/org/gatein/pc/cdi/IntegrationService.java
===================================================================
---
sandbox/cdi_support/pc/branches/cdi/cdi/src/main/java/org/gatein/pc/cdi/IntegrationService.java
(rev 0)
+++
sandbox/cdi_support/pc/branches/cdi/cdi/src/main/java/org/gatein/pc/cdi/IntegrationService.java 2011-04-23
20:17:03 UTC (rev 6336)
@@ -0,0 +1,60 @@
+package org.gatein.pc.cdi;
+
+import org.gatein.wci.WebAppEvent;
+import org.gatein.wci.WebAppLifeCycleEvent;
+import org.gatein.wci.WebAppListener;
+import org.gatein.wci.impl.DefaultServletContainerFactory;
+import org.picocontainer.Startable;
+
+import javax.naming.InitialContext;
+import javax.naming.NamingException;
+import javax.servlet.ServletContext;
+
+/**
+ * @author <a href="mailto:mstrukel@redhat.com">Marko Strukelj</a>
+ */
+public class IntegrationService implements Startable, WebAppListener
+{
+ private static final String BEAN_MGR_ATTR =
"javax.enterprise.inject.spi.BeanManager";
+
+ public void start()
+ {
+
DefaultServletContainerFactory.getInstance().getServletContainer().addWebAppListener(this);
+ }
+
+ public void stop()
+ {
+
DefaultServletContainerFactory.getInstance().getServletContainer().removeWebAppListener(this);
+ }
+
+ public void onEvent(WebAppEvent event)
+ {
+ if (event instanceof WebAppLifeCycleEvent)
+ {
+ WebAppLifeCycleEvent lifeCycleEvent = (WebAppLifeCycleEvent)event;
+
+ switch (lifeCycleEvent.getType())
+ {
+ case WebAppLifeCycleEvent.ADDED:
+ try
+ {
+ ServletContext ctx = event.getWebApp().getServletContext();
+ if (ctx.getAttribute(BEAN_MGR_ATTR) == null)
+ {
+ Object beanMgr = new
InitialContext().lookup("java:comp/BeanManager");
+ if (beanMgr != null)
+ {
+ ctx.setAttribute(BEAN_MGR_ATTR, beanMgr);
+ // save TCCL
+ ctx.setAttribute("org.gatein.pc.tccl",
Thread.currentThread().getContextClassLoader());
+ }
+ }
+ }
+ catch (NamingException ignored)
+ {}
+
+ break;
+ }
+ }
+ }
+}
Added:
sandbox/cdi_support/pc/branches/cdi/cdi/src/main/java/org/gatein/pc/cdi/portlet/CDIPortletDecorator.java
===================================================================
---
sandbox/cdi_support/pc/branches/cdi/cdi/src/main/java/org/gatein/pc/cdi/portlet/CDIPortletDecorator.java
(rev 0)
+++
sandbox/cdi_support/pc/branches/cdi/cdi/src/main/java/org/gatein/pc/cdi/portlet/CDIPortletDecorator.java 2011-04-23
20:17:03 UTC (rev 6336)
@@ -0,0 +1,105 @@
+/*
+ * JBoss, a division of Red Hat
+ * Copyright 2010, Red Hat Middleware, LLC, and individual
+ * contributors as indicated by the @authors tag. See the
+ * copyright.txt in the distribution for a full listing of
+ * individual contributors.
+ *
+ * This is free software; you can redistribute it and/or modify it
+ * under the terms of the GNU Lesser General Public License as
+ * published by the Free Software Foundation; either version 2.1 of
+ * the License, or (at your option) any later version.
+ *
+ * This software is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
+ * Lesser General Public License for more details.
+ *
+ * You should have received a copy of the GNU Lesser General Public
+ * License along with this software; if not, write to the Free
+ * Software Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA
+ * 02110-1301 USA, or see the FSF site:
http://www.fsf.org.
+ */
+package org.gatein.pc.cdi.portlet;
+
+import org.gatein.pc.portlet.container.PortletApplication;
+import org.gatein.pc.portlet.impl.spi.PortletDecorator;
+
+import javax.enterprise.context.spi.CreationalContext;
+import javax.enterprise.inject.spi.BeanManager;
+import javax.enterprise.inject.spi.InjectionTarget;
+import javax.portlet.ActionRequest;
+import javax.portlet.ActionResponse;
+import javax.portlet.Portlet;
+import javax.portlet.PortletConfig;
+import javax.portlet.PortletException;
+import javax.portlet.RenderRequest;
+import javax.portlet.RenderResponse;
+import javax.servlet.ServletContext;
+import java.io.IOException;
+
+/**
+ * @author <a href="mailto:mstrukel@redhat.com">Marko Strukelj</a>
+ */
+public class CDIPortletDecorator implements Portlet, PortletDecorator
+{
+ private Portlet delegate;
+
+ public Portlet decorate(Portlet instance, PortletApplication ctx)
+ {
+ this.delegate = instance;
+ if (performInject(delegate, ctx)) return this;
+ return instance;
+ }
+
+ public boolean performInject(Object instance, PortletApplication ctx)
+ {
+ ServletContext sctx = ctx.getContext().getServletContext();
+ BeanManager beanManager = (BeanManager)
sctx.getAttribute("javax.enterprise.inject.spi.BeanManager");
+
+ if (beanManager == null)
+ return false;
+
+ ClassLoader oldCl = null;
+ ClassLoader cl = (ClassLoader) sctx.getAttribute("org.gatein.pc.tccl");
+ if (cl != null)
+ {
+ oldCl = Thread.currentThread().getContextClassLoader();
+ Thread.currentThread().setContextClassLoader(cl);
+ }
+ try
+ {
+ CreationalContext<Object> creationalContext =
beanManager.createCreationalContext(null);
+ InjectionTarget<Object> injectionTarget = (InjectionTarget<Object>)
+
beanManager.createInjectionTarget(beanManager.createAnnotatedType(instance.getClass()));
+ injectionTarget.inject(instance, creationalContext);
+ }
+ finally
+ {
+ if (oldCl != null)
+ Thread.currentThread().setContextClassLoader(oldCl);
+ }
+
+ return true;
+ }
+
+ public void init(PortletConfig config) throws PortletException
+ {
+ delegate.init(config);
+ }
+
+ public void processAction(ActionRequest request, ActionResponse response) throws
PortletException, IOException
+ {
+ delegate.processAction(request, response);
+ }
+
+ public void render(RenderRequest request, RenderResponse response) throws
PortletException, IOException
+ {
+ delegate.render(request, response);
+ }
+
+ public void destroy()
+ {
+ delegate.destroy();
+ }
+}
Modified:
sandbox/cdi_support/pc/branches/cdi/portlet/src/main/java/org/gatein/pc/portlet/impl/jsr168/PortletContainerImpl.java
===================================================================
---
sandbox/cdi_support/pc/branches/cdi/portlet/src/main/java/org/gatein/pc/portlet/impl/jsr168/PortletContainerImpl.java 2011-04-23
20:10:49 UTC (rev 6335)
+++
sandbox/cdi_support/pc/branches/cdi/portlet/src/main/java/org/gatein/pc/portlet/impl/jsr168/PortletContainerImpl.java 2011-04-23
20:17:03 UTC (rev 6336)
@@ -56,6 +56,7 @@
import org.gatein.pc.portlet.impl.jsr168.api.RenderResponseImpl;
import org.gatein.pc.portlet.impl.jsr168.api.ResourceRequestImpl;
import org.gatein.pc.portlet.impl.jsr168.api.ResourceResponseImpl;
+import org.gatein.pc.portlet.impl.spi.PortletDecorator;
import javax.portlet.ActionRequest;
import javax.portlet.ActionResponse;
@@ -252,7 +253,7 @@
log.debug("Loading portlet class " + className);
Class portletClass =
application.getContext().getClassLoader().loadClass(className);
log.debug("Creating portlet object " + className);
- Portlet portlet = (Portlet)portletClass.newInstance();
+ Portlet portlet = getPortletInstance(portletClass);
log.debug("Created portlet object " + className);
initPortlet(portlet, config);
log.debug("Initialized portlet object " + className);
@@ -294,6 +295,37 @@
}
}
+ private Portlet getPortletInstance(Class portletClass) throws IllegalAccessException,
InstantiationException
+ {
+ Portlet portlet = (Portlet) portletClass.newInstance();
+ return applyPortletDecorators(portlet, application);
+ }
+
+ private Portlet applyPortletDecorators(Portlet portlet, PortletApplicationImpl
portletApp)
+ {
+ List<String> decoratorsList = getPortletDecoratorsList();
+ for (String className: decoratorsList)
+ {
+ try
+ {
+ Class clazz =
Thread.currentThread().getContextClassLoader().loadClass(className);
+ PortletDecorator decorator = (PortletDecorator) clazz.newInstance();
+ portlet = decorator.decorate(portlet, portletApp);
+ log.debug("Decorated portlet instance with: " + className);
+ }
+ catch (Exception e)
+ {
+ log.warn("IGNORED: Failed to process decorator " + className +
" for portlet: " + portlet, e);
+ }
+ }
+ return portlet;
+ }
+
+ private List<String> getPortletDecoratorsList()
+ {
+ return
java.util.Arrays.asList("org.gatein.pc.cdi.portlet.CDIPortletDecorator");
+ }
+
private void buildFilterChains()
{
List<ActionFilter> actionFilterList = builderFilterList(ActionFilter.class);
Added:
sandbox/cdi_support/pc/branches/cdi/portlet/src/main/java/org/gatein/pc/portlet/impl/spi/PortletDecorator.java
===================================================================
---
sandbox/cdi_support/pc/branches/cdi/portlet/src/main/java/org/gatein/pc/portlet/impl/spi/PortletDecorator.java
(rev 0)
+++
sandbox/cdi_support/pc/branches/cdi/portlet/src/main/java/org/gatein/pc/portlet/impl/spi/PortletDecorator.java 2011-04-23
20:17:03 UTC (rev 6336)
@@ -0,0 +1,34 @@
+/*
+ * JBoss, Home of Professional Open Source
+ * Copyright 2010, Red Hat, Inc., and individual contributors
+ * by the @authors tag. See the copyright.txt in the distribution for a
+ * full listing of individual contributors.
+ *
+ * This is free software; you can redistribute it and/or modify it
+ * under the terms of the GNU Lesser General Public License as
+ * published by the Free Software Foundation; either version 2.1 of
+ * the License, or (at your option) any later version.
+ *
+ * This software is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
+ * Lesser General Public License for more details.
+ *
+ * You should have received a copy of the GNU Lesser General Public
+ * License along with this software; if not, write to the Free
+ * Software Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA
+ * 02110-1301 USA, or see the FSF site:
http://www.fsf.org.
+ */
+package org.gatein.pc.portlet.impl.spi;
+
+import org.gatein.pc.portlet.container.PortletApplication;
+
+import javax.portlet.Portlet;
+
+/**
+ * @author <a href="mailto:mstrukel@redhat.com">Marko Strukelj</a>
+ */
+public interface PortletDecorator
+{
+ public Portlet decorate(Portlet instance, PortletApplication ctx);
+}