portals-pluto-scm mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From msnickl...@apache.org
Subject [3/5] portals-pluto git commit: More refactoring towards removal of cdi dependency. Separated method recognition from custom scoped bean recognition. extracted portlet initialization out of the configuration processing. Moved portlet instantiation from t
Date Wed, 08 Jun 2016 13:12:59 GMT
More refactoring towards removal of cdi dependency. Separated method
recognition from custom scoped bean recognition. extracted portlet
initialization out of the configuration processing. Moved portlet
instantiation from the servlet container initializer to the portlet servlet
itself in order to decouple the portlet container initializer from the
servlet container initializer that starts the Weld CDI container. Began
addition of alternative class scanning technology that will be used to find
the portlet method annotations when cdi is not present.


Project: http://git-wip-us.apache.org/repos/asf/portals-pluto/repo
Commit: http://git-wip-us.apache.org/repos/asf/portals-pluto/commit/842cc562
Tree: http://git-wip-us.apache.org/repos/asf/portals-pluto/tree/842cc562
Diff: http://git-wip-us.apache.org/repos/asf/portals-pluto/diff/842cc562

Branch: refs/heads/master
Commit: 842cc562df479df72d07050dc77b5e8861b2c9ce
Parents: 5371e33
Author: Scott Nicklous <msnicklous@apache.org>
Authored: Tue Jun 7 16:05:20 2016 +0200
Committer: Scott Nicklous <msnicklous@apache.org>
Committed: Tue Jun 7 16:05:20 2016 +0200

----------------------------------------------------------------------
 .../driver/PortletContainerInitializer.java     |   2 +-
 .../pluto/container/driver/PortletServlet3.java |   3 +-
 pluto-container/pom.xml                         |   7 +
 .../bean/processor/AnnotatedMethod.java         |  23 +++
 .../bean/processor/AnnotatedMethodStore.java    |   2 +-
 .../bean/processor/AnnotationRecognizer.java    | 170 ++++++++++--------
 .../bean/processor/MethodAnnoRecognizer.java    |  69 +++++++
 .../processor/PortletAnnotationRecognizer.java  |  21 ++-
 .../bean/processor/PortletCDIExtension.java     |   9 +-
 .../om/portlet/impl/ConfigurationHolder.java    |  21 +++
 .../om/portlet/impl/ConfigurationProcessor.java | 180 ++++++++++++-------
 11 files changed, 352 insertions(+), 155 deletions(-)
----------------------------------------------------------------------


http://git-wip-us.apache.org/repos/asf/portals-pluto/blob/842cc562/pluto-container-driver-api/src/main/java/org/apache/pluto/container/driver/PortletContainerInitializer.java
----------------------------------------------------------------------
diff --git a/pluto-container-driver-api/src/main/java/org/apache/pluto/container/driver/PortletContainerInitializer.java
b/pluto-container-driver-api/src/main/java/org/apache/pluto/container/driver/PortletContainerInitializer.java
index cf02393..9100208 100644
--- a/pluto-container-driver-api/src/main/java/org/apache/pluto/container/driver/PortletContainerInitializer.java
+++ b/pluto-container-driver-api/src/main/java/org/apache/pluto/container/driver/PortletContainerInitializer.java
@@ -25,9 +25,9 @@ import java.util.Set;
 import javax.portlet.annotations.PortletApplication;
 import javax.portlet.annotations.PortletConfiguration;
 import javax.portlet.annotations.PortletConfigurations;
+import javax.portlet.annotations.PortletLifecycleFilter;
 import javax.portlet.annotations.PortletListener;
 import javax.portlet.annotations.PortletPreferencesValidator;
-import javax.portlet.annotations.PortletLifecycleFilter;
 import javax.servlet.DispatcherType;
 import javax.servlet.FilterRegistration;
 import javax.servlet.MultipartConfigElement;

http://git-wip-us.apache.org/repos/asf/portals-pluto/blob/842cc562/pluto-container-driver-api/src/main/java/org/apache/pluto/container/driver/PortletServlet3.java
----------------------------------------------------------------------
diff --git a/pluto-container-driver-api/src/main/java/org/apache/pluto/container/driver/PortletServlet3.java
b/pluto-container-driver-api/src/main/java/org/apache/pluto/container/driver/PortletServlet3.java
index e3b970d..cc4677c 100644
--- a/pluto-container-driver-api/src/main/java/org/apache/pluto/container/driver/PortletServlet3.java
+++ b/pluto-container-driver-api/src/main/java/org/apache/pluto/container/driver/PortletServlet3.java
@@ -145,13 +145,14 @@ public class PortletServlet3 extends HttpServlet {
       // Retrieve portlet name as defined as an initialization parameter.
       portletName = getInitParameter(PORTLET_NAME);
       
-      // Get the config bean and create the invoker
+      // Get the config bean, instantiate the portlets, and create the invoker
       holder = (ConfigurationHolder) config.getServletContext().getAttribute(ConfigurationHolder.ATTRIB_NAME);
       try {
          if (holder == null || holder.getMethodStore() == null) {
             LOG.error("Could not obtain configuration bean for portlet " + portletName +
". Exiting.");
             return;
          } else {
+            holder.instantiatePortlets(beanmgr);
             invoker = new PortletInvoker(holder.getMethodStore(), portletName);
             LOG.debug("Created the portlet invoker for portlet: " + portletName);
          }

http://git-wip-us.apache.org/repos/asf/portals-pluto/blob/842cc562/pluto-container/pom.xml
----------------------------------------------------------------------
diff --git a/pluto-container/pom.xml b/pluto-container/pom.xml
index ed41612..f23d027 100644
--- a/pluto-container/pom.xml
+++ b/pluto-container/pom.xml
@@ -40,6 +40,13 @@
          <scope>provided</scope>
       </dependency>
 
+      <!-- For method annotation scanning ==================================== -->
+      <dependency>
+         <groupId>eu.infomas</groupId>
+         <artifactId>annotation-detector</artifactId>
+         <version>3.0.5</version>
+      </dependency>
+
       <!-- Specification Libraries =========================================== -->
       <dependency>
          <groupId>org.apache.portals.pluto</groupId>

http://git-wip-us.apache.org/repos/asf/portals-pluto/blob/842cc562/pluto-container/src/main/java/org/apache/pluto/container/bean/processor/AnnotatedMethod.java
----------------------------------------------------------------------
diff --git a/pluto-container/src/main/java/org/apache/pluto/container/bean/processor/AnnotatedMethod.java
b/pluto-container/src/main/java/org/apache/pluto/container/bean/processor/AnnotatedMethod.java
index 13cc4d1..e421fcd 100644
--- a/pluto-container/src/main/java/org/apache/pluto/container/bean/processor/AnnotatedMethod.java
+++ b/pluto-container/src/main/java/org/apache/pluto/container/bean/processor/AnnotatedMethod.java
@@ -8,7 +8,13 @@ import java.util.Set;
 import javax.enterprise.inject.spi.Bean;
 import javax.enterprise.inject.spi.BeanManager;
 
+import org.slf4j.Logger;
+import org.slf4j.LoggerFactory;
+
 public class AnnotatedMethod {
+   private static final Logger LOG = LoggerFactory.getLogger(AnnotatedMethod.class);
+   private static final boolean isTrace = LOG.isTraceEnabled();
+   
    
    private final Class<?>           beanClass; 
    private final Method             meth;
@@ -83,6 +89,13 @@ public class AnnotatedMethod {
    }
 
    /**
+    * @return the beanClass
+    */
+   public Class<?> getBeanClass() {
+      return beanClass;
+   }
+
+   /**
     * The annotated method is activated by using the specified BeanManager to
     * obtain a reference to a bean that can be used for method invocation.
     * 
@@ -102,6 +115,15 @@ public class AnnotatedMethod {
     * @param beanInstance
     */
    public void setPortletClassInstance(Object beanInstance) {
+      
+      if (isTrace) {
+         StringBuilder str = new StringBuilder();
+         str.append("Updating class instance.");
+         str.append(", bean class: ").append(beanClass.getCanonicalName());
+         str.append(", instance: ").append((beanInstance == null) ? "null" : "not null");
+         LOG.debug(str.toString());
+      }
+
       this.beanInstance = beanInstance;
       this.isPortletClass = true;
    }
@@ -123,6 +145,7 @@ public class AnnotatedMethod {
          // scope of the bean.
          beanInstance = beanMgr.getReference(bean, bean.getBeanClass(), beanMgr.createCreationalContext(bean));
       }
+
       return meth.invoke(beanInstance, args);
    }
 

http://git-wip-us.apache.org/repos/asf/portals-pluto/blob/842cc562/pluto-container/src/main/java/org/apache/pluto/container/bean/processor/AnnotatedMethodStore.java
----------------------------------------------------------------------
diff --git a/pluto-container/src/main/java/org/apache/pluto/container/bean/processor/AnnotatedMethodStore.java
b/pluto-container/src/main/java/org/apache/pluto/container/bean/processor/AnnotatedMethodStore.java
index 50287d2..60ca476 100644
--- a/pluto-container/src/main/java/org/apache/pluto/container/bean/processor/AnnotatedMethodStore.java
+++ b/pluto-container/src/main/java/org/apache/pluto/container/bean/processor/AnnotatedMethodStore.java
@@ -550,7 +550,7 @@ public class AnnotatedMethodStore {
       for (MethodIdentifier mi : getMethodIDsForPortlet(portletName)) {
          List<AnnotatedMethod> list =  methods.get(mi);
          for (AnnotatedMethod am : list) {
-            if (am.getJavaMethod().getDeclaringClass().equals(cls)) {
+            if (am.getBeanClass().equals(cls)) {
                am.setPortletClassInstance(beanInstance);
             }
          }

http://git-wip-us.apache.org/repos/asf/portals-pluto/blob/842cc562/pluto-container/src/main/java/org/apache/pluto/container/bean/processor/AnnotationRecognizer.java
----------------------------------------------------------------------
diff --git a/pluto-container/src/main/java/org/apache/pluto/container/bean/processor/AnnotationRecognizer.java
b/pluto-container/src/main/java/org/apache/pluto/container/bean/processor/AnnotationRecognizer.java
index 83dcee2..57b344b 100644
--- a/pluto-container/src/main/java/org/apache/pluto/container/bean/processor/AnnotationRecognizer.java
+++ b/pluto-container/src/main/java/org/apache/pluto/container/bean/processor/AnnotationRecognizer.java
@@ -25,7 +25,6 @@ import java.util.List;
 import java.util.Map;
 import java.util.Set;
 
-import javax.enterprise.inject.spi.AnnotatedMethod;
 import javax.enterprise.inject.spi.AnnotatedType;
 import javax.enterprise.inject.spi.BeanManager;
 import javax.enterprise.inject.spi.ProcessAnnotatedType;
@@ -94,7 +93,6 @@ public abstract class AnnotationRecognizer {
    @SuppressWarnings({ "unchecked", "rawtypes" })
    public void checkAnnotatedType(ProcessAnnotatedType pat) throws InvalidAnnotationException
{
       AnnotatedType<?> aType = pat.getAnnotatedType();
-      String typeName = aType.getJavaClass().getCanonicalName();
       try {
          
          // Process the class annotations
@@ -115,78 +113,7 @@ public abstract class AnnotationRecognizer {
             }
          }
          
-         // Get the annotated methods and handle them
-         
-         for (AnnotatedMethod<?> meth : aType.getMethods()) {
-            annos = meth.getAnnotations();
-            for (Annotation anno : annos) {
-               Class<? extends Annotation> keyAnno = 
-                     setContains(descriptions.keySet(), anno);
-               if (keyAnno != null) {
-                  
-                  // Get the valid method descriptions for this annotation
-                  
-                  List<MethodDescription> descs = descriptions.get(keyAnno);
-                  assert descs != null;
-                  assert descs.size() > 0;
-                  
-                  // try to find the matching method description
-                  
-                  MethodDescription matchingDesc = null;
-                  StringBuilder errtxt = new StringBuilder(128);
-                  String sep = "   ";
-                  for (MethodDescription desc : descs) {
-                     if (desc.isMethodMatched(meth.getJavaMember())) {
-                        matchingDesc = desc;
-                        break;
-                     } else {
-                        errtxt.append(sep).append(desc.getExpectedSignature(true));
-                        sep = "\n   ";
-                     }
-                  }
-  
-                  if (isTrace) {
-                     StringBuilder txt = new StringBuilder(128);
-                     txt.append("For method annotation: ").append(anno.annotationType().getSimpleName());
-                     txt.append(" on class: ").append(typeName);
-                     txt.append(", method: ").append(meth.getJavaMember().getName());
-                     if (matchingDesc != null) {
-                        txt.append(", recognized type: ").append(matchingDesc.getType());
-                        txt.append(", signature variant: ").append(matchingDesc.getVariant());
-                     } else {
-                        txt.append(", No match found. Error string:\n");
-                        txt.append(errtxt);
-                     }
-                     LOG.trace(txt.toString());
-                  }
-
-                  if (matchingDesc != null) {
-                     
-                     // Found a matching method, so handle.
-                     
-                     handleMethod(anno, aType.getJavaClass(), meth.getJavaMember(), matchingDesc);
-                  } else {
-                     
-                     // Method doesn't match any of the descriptions. 
-                     // this might occur when someone makes a mistake with configuration,
-                     // so handle gracefully.
-                     
-                     StringBuilder txt = new StringBuilder(128);
-                     txt.append("Unrecognized method annotation: ")
-                        .append(anno.annotationType().getSimpleName());
-                     txt.append(", Class: ").append(typeName);
-                     txt.append(", Method: ").append(meth.getJavaMember().getName());
-                     txt.append("\n").append(errtxt);
-                     LOG.debug(txt.toString());
-                     
-                     // Store the error for each portlet name in array 
-                     for (String n : getDisplayNames(anno)) {
-                        summary.addErrorString(n, txt.toString());
-                     }
-                  }
-               }
-            }
-         }
+         checkForMethodAnnotations(aType.getJavaClass());
          
       } catch (Exception e) {
          
@@ -198,6 +125,88 @@ public abstract class AnnotationRecognizer {
          throw new InvalidAnnotationException(txt.toString(), e); 
       }
    }
+   
+   /**
+    * recognizes and registers all portlet method annotations for the given class.
+    * 
+    * @param aClass  the annotated class 
+    */
+   protected void checkForMethodAnnotations(Class<?> aClass) {
+      String typeName = aClass.getCanonicalName();
+      
+      // Get the annotated methods and handle them
+      
+      for (Method meth : aClass.getMethods()) {
+         for (Annotation anno : meth.getAnnotations()) {
+            Class<? extends Annotation> keyAnno = 
+                  setContains(descriptions.keySet(), anno);
+            if (keyAnno != null) {
+               
+               // Get the valid method descriptions for this annotation
+               
+               List<MethodDescription> descs = descriptions.get(keyAnno);
+               assert descs != null;
+               assert descs.size() > 0;
+               
+               // try to find the matching method description
+               
+               MethodDescription matchingDesc = null;
+               StringBuilder errtxt = new StringBuilder(128);
+               String sep = "   ";
+               for (MethodDescription desc : descs) {
+                  if (desc.isMethodMatched(meth)) {
+                     matchingDesc = desc;
+                     break;
+                  } else {
+                     errtxt.append(sep).append(desc.getExpectedSignature(true));
+                     sep = "\n   ";
+                  }
+               }
+
+               if (isTrace) {
+                  StringBuilder txt = new StringBuilder(128);
+                  txt.append("For method annotation: ").append(anno.annotationType().getSimpleName());
+                  txt.append(" on class: ").append(typeName);
+                  txt.append(", method: ").append(meth.getName());
+                  if (matchingDesc != null) {
+                     txt.append(", recognized type: ").append(matchingDesc.getType());
+                     txt.append(", signature variant: ").append(matchingDesc.getVariant());
+                  } else {
+                     txt.append(", No match found. Error string:\n");
+                     txt.append(errtxt);
+                  }
+                  LOG.trace(txt.toString());
+               }
+
+               if (matchingDesc != null) {
+                  
+                  // Found a matching method, so handle.
+                  
+                  handleMethod(anno, aClass, meth, matchingDesc);
+               } else {
+                  
+                  // Method doesn't match any of the descriptions. 
+                  // this might occur when someone makes a mistake with configuration,
+                  // so handle gracefully.
+                  
+                  StringBuilder txt = new StringBuilder(128);
+                  txt.append("Unrecognized method annotation: ")
+                     .append(anno.annotationType().getSimpleName());
+                  txt.append(", Class: ").append(typeName);
+                  txt.append(", Method: ").append(meth.getName());
+                  txt.append("\n").append(errtxt);
+                  LOG.debug(txt.toString());
+                  
+                  // Store the error for each portlet name in array 
+                  for (String n : getDisplayNames(anno)) {
+                     summary.addErrorString(n, txt.toString());
+                  }
+               }
+            }
+         }
+      }
+
+   }
 
    /**
     * extracts array of portlet names from the annotation
@@ -257,12 +266,15 @@ public abstract class AnnotationRecognizer {
 
    /**
     * To be called by the CDI extension afterDeploymentValidation method to
-    * verify that the stored methods are consistent and to create the bean referenecs.
+    * activate the custom scoped beans by providing a bean manager.
     *  
     * @param bm      BeanManager needed to activate the beans.
-    * @throws InvalidAnnotationException  If the deployment is inconsistent or if the
-    *                                     beans cannot be instantiated.
     */
-   protected abstract void activateDeployment(BeanManager bm)
-         throws InvalidAnnotationException;
+   protected abstract void activateCustomScopes(BeanManager bm);
+
+   /**
+    * To be after the beans have been recognized and CDI is initialized to
+    * verify that the stored methods are consistent.
+    */
+   protected abstract void activateAnnotatedMethods(BeanManager bm);
 }

http://git-wip-us.apache.org/repos/asf/portals-pluto/blob/842cc562/pluto-container/src/main/java/org/apache/pluto/container/bean/processor/MethodAnnoRecognizer.java
----------------------------------------------------------------------
diff --git a/pluto-container/src/main/java/org/apache/pluto/container/bean/processor/MethodAnnoRecognizer.java
b/pluto-container/src/main/java/org/apache/pluto/container/bean/processor/MethodAnnoRecognizer.java
new file mode 100644
index 0000000..05cacaa
--- /dev/null
+++ b/pluto-container/src/main/java/org/apache/pluto/container/bean/processor/MethodAnnoRecognizer.java
@@ -0,0 +1,69 @@
+/*  Licensed to the Apache Software Foundation (ASF) under one
+ *  or more contributor license agreements.  See the NOTICE file
+ *  distributed with this work for additional information
+ *  regarding copyright ownership.  The ASF licenses this file
+ *  to you under the Apache License, Version 2.0 (the
+ *  "License"); you may not use this file except in compliance
+ *  with the License.  You may obtain a copy of the License at
+ *
+ *    http://www.apache.org/licenses/LICENSE-2.0
+ *
+ *  Unless required by applicable law or agreed to in writing,
+ *  software distributed under the License is distributed on an
+ *  "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
+ *  KIND, either express or implied.  See the License for the
+ *  specific language governing permissions and limitations
+ *  under the License.
+ */
+
+package org.apache.pluto.container.bean.processor;
+
+import java.lang.annotation.Annotation;
+import java.util.logging.Level;
+import java.util.logging.Logger;
+
+import javax.portlet.annotations.ActionMethod;
+import javax.portlet.annotations.EventMethod;
+import javax.portlet.annotations.HeaderMethod;
+import javax.portlet.annotations.RenderMethod;
+import javax.portlet.annotations.ServeResourceMethod;
+
+import eu.infomas.annotation.AnnotationDetector;
+
+/**
+ * Uses the annotation detector to find the portlet method annotations.
+ * 
+ * @author Scott Nicklous
+ * 
+ */
+public class MethodAnnoRecognizer implements AnnotationDetector.MethodReporter {
+   private static final Logger  LOGGER  = Logger.getLogger(MethodAnnoRecognizer.class.getName());
+   private static final boolean isDebug = LOGGER.isLoggable(Level.FINE);
+   private static final boolean isTrace = LOGGER.isLoggable(Level.FINEST);
+   
+   AnnotationDetector ad;
+
+
+   public MethodAnnoRecognizer() {
+      ad = new AnnotationDetector(this);
+   }
+
+   @SuppressWarnings("unchecked")
+   @Override
+   public Class<? extends Annotation>[] annotations() {
+      return new Class[] { HeaderMethod.class, ActionMethod.class, EventMethod.class, RenderMethod.class,
+            ServeResourceMethod.class };
+   }
+
+   @Override
+   public void reportMethodAnnotation(Class<? extends Annotation> anno, String className,
String methName) {
+      if (isDebug) {
+         StringBuilder txt = new StringBuilder(128);
+         txt.append("Found: ").append(anno.getSimpleName());
+         txt.append(", class name: ").append(className);
+         txt.append(", method name: ").append(methName);
+         LOGGER.fine(txt.toString());
+      }
+   }
+
+}

http://git-wip-us.apache.org/repos/asf/portals-pluto/blob/842cc562/pluto-container/src/main/java/org/apache/pluto/container/bean/processor/PortletAnnotationRecognizer.java
----------------------------------------------------------------------
diff --git a/pluto-container/src/main/java/org/apache/pluto/container/bean/processor/PortletAnnotationRecognizer.java
b/pluto-container/src/main/java/org/apache/pluto/container/bean/processor/PortletAnnotationRecognizer.java
index 2b30ae3..99e5da9 100644
--- a/pluto-container/src/main/java/org/apache/pluto/container/bean/processor/PortletAnnotationRecognizer.java
+++ b/pluto-container/src/main/java/org/apache/pluto/container/bean/processor/PortletAnnotationRecognizer.java
@@ -362,7 +362,20 @@ public class PortletAnnotationRecognizer extends AnnotationRecognizer
{
     *                                     beans cannot be instantiated.
     */
    @Override
-   protected void activateDeployment(BeanManager bm) {
+   protected void activateCustomScopes(BeanManager bm) {
+      
+      // Activate the custom scoped beans
+      stateScopedConfig.activate(bm);
+      sessionScopedConfig.activate(bm);
+   }
+
+   
+   /**
+    * To be called by the CDI extension afterDeploymentValidation method to
+    * verify that the stored methods are consistent.
+    */
+   @Override
+   protected void activateAnnotatedMethods(BeanManager bm) {
       
       // Verify the portlet names in the proxied method store. It is an error if:
       // 1) There is no method for a given name, but configuration data exists
@@ -402,13 +415,9 @@ public class PortletAnnotationRecognizer extends AnnotationRecognizer
{
          }
       }
       
-      // Now activate the methods by providing a BeanManager to the store.
-      
+      //TODO: remove ams initialization
       ams.activateMethods(bm);
       
-      // Activate the custom scoped beans
-      stateScopedConfig.activate(bm);
-      sessionScopedConfig.activate(bm);
    }
 
    /**

http://git-wip-us.apache.org/repos/asf/portals-pluto/blob/842cc562/pluto-container/src/main/java/org/apache/pluto/container/bean/processor/PortletCDIExtension.java
----------------------------------------------------------------------
diff --git a/pluto-container/src/main/java/org/apache/pluto/container/bean/processor/PortletCDIExtension.java
b/pluto-container/src/main/java/org/apache/pluto/container/bean/processor/PortletCDIExtension.java
index 9eae380..851a208 100644
--- a/pluto-container/src/main/java/org/apache/pluto/container/bean/processor/PortletCDIExtension.java
+++ b/pluto-container/src/main/java/org/apache/pluto/container/bean/processor/PortletCDIExtension.java
@@ -58,6 +58,12 @@ public class PortletCDIExtension implements Extension {
    
    private static AnnotatedConfigBean acb = null;
    
+   /**
+    * This is necessary to allow the portlet container initializer access to the 
+    * config bean, since injection won't work there.
+    * 
+    * @return the annotated config bean.
+    */
    public static AnnotatedConfigBean getConfig() {
       return acb;
    }
@@ -180,7 +186,8 @@ public class PortletCDIExtension implements Extension {
     */
    void afterDeploymentValidation(@Observes AfterDeploymentValidation adv, BeanManager bm)
          throws InvalidAnnotationException {
-      par.activateDeployment(bm);
+      par.activateCustomScopes(bm);
+      par.activateAnnotatedMethods(bm);
       
       // Done processing the annotations, so put the resulting configuration in an
       // application scoped bean to pass it to the servlet

http://git-wip-us.apache.org/repos/asf/portals-pluto/blob/842cc562/pluto-container/src/main/java/org/apache/pluto/container/om/portlet/impl/ConfigurationHolder.java
----------------------------------------------------------------------
diff --git a/pluto-container/src/main/java/org/apache/pluto/container/om/portlet/impl/ConfigurationHolder.java
b/pluto-container/src/main/java/org/apache/pluto/container/om/portlet/impl/ConfigurationHolder.java
index f4fa830..0ec2f34 100644
--- a/pluto-container/src/main/java/org/apache/pluto/container/om/portlet/impl/ConfigurationHolder.java
+++ b/pluto-container/src/main/java/org/apache/pluto/container/om/portlet/impl/ConfigurationHolder.java
@@ -22,6 +22,7 @@ import java.io.IOException;
 import java.io.InputStream;
 import java.util.Set;
 
+import javax.enterprise.inject.spi.BeanManager;
 import javax.portlet.annotations.PortletApplication;
 import javax.portlet.annotations.PortletConfiguration;
 import javax.portlet.annotations.PortletConfigurations;
@@ -64,6 +65,7 @@ public class ConfigurationHolder {
                                                            + "org.apache.pluto.container.om.portlet30.impl";
 
    private AnnotatedMethodStore methodStore;
+   private boolean portletsInstantiated = false;
    
    /**
     * Default constructor
@@ -277,6 +279,24 @@ public class ConfigurationHolder {
    }
 
    /**
+    * Instantiates the portlets with the helpt of the bean manager. If the bean 
+    * manager is <code>null</code>, the classes are instantiated the old-fashioned
way.
+    * 
+    * @param bm   the bean manager; may be <code>null</code>
+    */
+   public void instantiatePortlets(BeanManager bm) {
+      
+      // This method will be called once for each deployed portlet servlet. However,
+      // it's important that the methods only be instantiated once per servlet context 
+      // to avoid losing initialization data.
+      
+      if (!portletsInstantiated) {
+         portletsInstantiated = true;
+         jcp.instantiatePortlets(methodStore, bm);
+      }
+   }
+
+   /**
     * Reconciles the bean configuration with the config from annotations & portlet DD.
     */
    public void reconcileBeanConfig(AnnotatedMethodStore ams) {
@@ -284,6 +304,7 @@ public class ConfigurationHolder {
          jcp = new JSR362ConfigurationProcessor(pad);
       }
       jcp.reconcileBeanConfig(ams);
+      jcp.instantiatePortlets(ams, ams.getBeanMgr());
    }
 
 }

http://git-wip-us.apache.org/repos/asf/portals-pluto/blob/842cc562/pluto-container/src/main/java/org/apache/pluto/container/om/portlet/impl/ConfigurationProcessor.java
----------------------------------------------------------------------
diff --git a/pluto-container/src/main/java/org/apache/pluto/container/om/portlet/impl/ConfigurationProcessor.java
b/pluto-container/src/main/java/org/apache/pluto/container/om/portlet/impl/ConfigurationProcessor.java
index d8250c1..5c0f24c 100644
--- a/pluto-container/src/main/java/org/apache/pluto/container/om/portlet/impl/ConfigurationProcessor.java
+++ b/pluto-container/src/main/java/org/apache/pluto/container/om/portlet/impl/ConfigurationProcessor.java
@@ -60,6 +60,7 @@ public abstract class ConfigurationProcessor {
    /** Logger. */
    private static final Logger LOG = LoggerFactory.getLogger(ConfigurationProcessor.class);
    private static final boolean isDebug = LOG.isDebugEnabled();
+   private static final boolean isTrace = LOG.isTraceEnabled();
 
 
    protected PortletApplicationDefinition pad;
@@ -494,70 +495,15 @@ public abstract class ConfigurationProcessor {
                }
             }
          }
-         
-         Object instance = null;
-         if (cls != null) {
-            
-            // Let CDI instantiate the portlet to allow for injection. 
-            // Get the single bean instance for the portlet class.
-            
-            StringBuilder txt = new StringBuilder(128);
-            BeanManager bm = ams.getBeanMgr();
-            if (bm == null) {
-               txt.append("Could not get portlet bean. Bean manager is null.");
-            } else {
-               Set<Bean<?>> beans = bm.getBeans(cls);
-               if (beans == null || beans.size() == 0) {
-                  txt.append("Could not get portlet bean. No beans found.");
-               } else {
-                  Bean<?> bean = bm.resolve(beans);
-                  if (bean == null) {
-                     txt.append("Could not get portlet bean. Could not resolve bean.");
-                  } else {
-                     instance = bm.getReference(bean, bean.getBeanClass(), bm.createCreationalContext(bean));
-                     if (instance == null) {
-                        txt.append("Could not get portlet bean. Could not get bean instance.");
-                     }
-                  }
-               }
-            }
-            
-            // If the instance is still null, the portlet class might not be in a valid bean

-            // archive, as a JSR 286 portlet might be. Try to get a regular old instance.
-            
-            if (instance == null) {
-               LOG.debug("Could not create bean (possibly not in a valid bean archive). Now
directly instantiating class: " + cls.getCanonicalName());
-               try {
-                  instance = cls.newInstance();
-               } catch(Exception e) {
-                  txt.append(" Exception creating instance of class: ").append(e.toString());
-               }
-            }
-            
-            
-            if ((instance == null) && (txt.length() > 0)) {
-               txt.append(" Portlet name: ").append(pd.getPortletName());
-               txt.append(", portlet class: ").append(cls);
-               LOG.warn(txt.toString());
-            }
-         }
 
-         if (instance != null) {
-            
-            // The annotated method store might contain methods from the configured
-            // portlet class being processed. For example, this may occur when an action
-            // or event method in the portlet class is annotated to specify processing or
-            // publishing event references. Such annotated methods must use the same bean
-            // instance, so fix up the method store.
-            
-            ams.setPortletClassInstance(pd.getPortletName(), cls, instance);
+         if (cls != null) {
 
             // extract the methods from the portlet class and add them to the method store
             // as long there is no corresponding annotated method already present.
             // (annotated methods take precedence over portlet class methods). 
             
             AnnotatedMethod am;
-            am = getMethod(instance, "init", METH_INI);
+            am = getMethod(cls, "init", METH_INI);
             if (am != null) {
                MethodIdentifier mi = new MethodIdentifier(pd.getPortletName(), "", MethodType.INIT);
                if (ams.getMethods(mi).size() == 0) {
@@ -565,7 +511,7 @@ public abstract class ConfigurationProcessor {
                }
             }
             
-            am = getMethod(instance, "destroy", METH_DES);
+            am = getMethod(cls, "destroy", METH_DES);
             if (am != null) {
                MethodIdentifier mi = new MethodIdentifier(pd.getPortletName(), "", MethodType.DESTROY);
                if (ams.getMethods(mi).size() == 0) {
@@ -573,7 +519,7 @@ public abstract class ConfigurationProcessor {
                }
             }
             
-            am = getMethod(instance, "processAction", METH_ACT);
+            am = getMethod(cls, "processAction", METH_ACT);
             if (am != null) {
                MethodIdentifier mi = new MethodIdentifier(pd.getPortletName(), "", MethodType.ACTION);
                if (ams.getMethods(mi).size() == 0) {
@@ -581,7 +527,7 @@ public abstract class ConfigurationProcessor {
                }
             }
             
-            am = getMethod(instance, "processEvent", METH_EVT);
+            am = getMethod(cls, "processEvent", METH_EVT);
             if (am != null) {
                MethodIdentifier mi = new MethodIdentifier(pd.getPortletName(), "", MethodType.EVENT);
                if (ams.getMethods(mi).size() == 0) {
@@ -589,7 +535,7 @@ public abstract class ConfigurationProcessor {
                }
             }
             
-            am = getMethod(instance, "render", METH_REN);
+            am = getMethod(cls, "render", METH_REN);
             if (am != null) {
                MethodIdentifier mi = new MethodIdentifier(pd.getPortletName(), "", MethodType.RENDER);
                if (ams.getMethods(mi).size() == 0) {
@@ -597,7 +543,7 @@ public abstract class ConfigurationProcessor {
                }
             }
             
-            am = getMethod(instance, "renderHeaders", METH_HDR);
+            am = getMethod(cls, "renderHeaders", METH_HDR);
             if (am != null) {
                MethodIdentifier mi = new MethodIdentifier(pd.getPortletName(), "", MethodType.HEADER);
                if (ams.getMethods(mi).size() == 0) {
@@ -605,7 +551,7 @@ public abstract class ConfigurationProcessor {
                }
             }
             
-            am = getMethod(instance, "serveResource", METH_RES);
+            am = getMethod(cls, "serveResource", METH_RES);
             if (am != null) {
                MethodIdentifier mi = new MethodIdentifier(pd.getPortletName(), "", MethodType.RESOURCE);
                if (ams.getMethods(mi).size() == 0) {
@@ -655,18 +601,120 @@ public abstract class ConfigurationProcessor {
    }
    
    /**
+    * Activates the bean methods in the method store. Instantiates any portlet classes and
fixes 
+    * up the method store so that methods of the same class use the same class instance.
+    */
+   public void instantiatePortlets(AnnotatedMethodStore ams, BeanManager bm) {
+      
+      if (isDebug) {
+         StringBuilder txt = new StringBuilder();
+         txt.append("Instantiating the portlets.");
+         txt.append(" bean mgr = null: ").append(bm == null);
+         txt.append(", portlet names: ").append(Arrays.toString(ams.getPortletNames().toArray()));
+         LOG.debug(txt.toString());
+      }
+      
+      ams.activateMethods(bm);
+    
+      for (PortletDefinition pd : pad.getPortlets()) {
+         String clsName = pd.getPortletClass();
+         
+         if (clsName != null) {
+            ClassLoader cl = Thread.currentThread().getContextClassLoader();
+            if (cl == null) {
+               cl = this.getClass().getClassLoader();
+            }
+            try {
+               Class<?> cls = cl.loadClass(clsName);
+               Object instance = null;
+               
+               // Let CDI instantiate the portlet to allow for injection. 
+               // Get the single bean instance for the portlet class.
+               
+               StringBuilder txt = new StringBuilder(128);
+               if (bm == null) {
+                  txt.append("Could not get portlet bean. Bean manager is null.");
+               } else {
+                  Set<Bean<?>> beans = bm.getBeans(cls);
+                  if (beans == null || beans.size() == 0) {
+                     txt.append("Could not get portlet bean. No beans found.");
+                  } else {
+                     Bean<?> bean = bm.resolve(beans);
+                     if (bean == null) {
+                        txt.append("Could not get portlet bean. Could not resolve bean.");
+                     } else {
+                        instance = bm.getReference(bean, bean.getBeanClass(), bm.createCreationalContext(bean));
+                        if (instance == null) {
+                           txt.append("Could not get portlet bean. Could not get bean instance.");
+                        }
+                     }
+                  }
+               }
+               
+               // If the instance is still null, the portlet class might not be in a valid
bean 
+               // archive, as a JSR 286 portlet might be. Try to get a regular old instance.
+               
+               if (instance == null) {
+                  LOG.debug("Could not create bean (possibly not in a valid bean archive).
Now directly instantiating class: " + cls.getCanonicalName());
+                  try {
+                     instance = cls.newInstance();
+                  } catch(Exception e) {
+                     txt.append(" Exception creating instance of class: ").append(e.toString());
+                  }
+               }
+               
+               // If all went well, fix up the method store with the instance
+               
+               if (instance != null) {
+                  
+                  // The annotated method store might contain methods from the configured
+                  // portlet class being processed. For example, this may occur when an action
+                  // or event method in the portlet class is annotated to specify processing
or
+                  // publishing event references. Such annotated methods must use the same
bean
+                  // instance, so fix up the method store.
+
+                  ams.setPortletClassInstance(pd.getPortletName(), cls, instance);
+                  
+                  if (isTrace) {
+                     StringBuilder str = new StringBuilder();
+                     str.append("Updating class instances.");
+                     str.append(" portlet name: ").append(pd.getPortletName());
+                     str.append(", class: ").append(cls.getCanonicalName());
+                     str.append(", instance: ").append((instance == null) ? "null" : "not
null");
+                     LOG.debug(str.toString());
+                  }
+
+               }
+               
+               // handle error situation
+               
+               if (instance == null) {
+                  txt.append(" Portlet name: ").append(pd.getPortletName());
+                  txt.append(", portlet class: ").append(cls);
+                  LOG.warn(txt.toString());
+               }
+
+               
+            } catch (ClassNotFoundException e) {
+               LOG.debug("Could not instantiate portlet class: " + clsName);
+            }
+
+         }
+      }
+   }
+   
+   /**
     * helper method for extracting the portlet methods from the portlet class.
     * @param cls
     * @param name
     * @param md
     * @return
     */
-   private AnnotatedMethod getMethod(Object instance, String name, MethodDescription md)
{
+   private AnnotatedMethod getMethod(Class<?> cls, String name, MethodDescription md)
{
       AnnotatedMethod am = null;
-      Class<?> cls = instance.getClass();
       try {
          Method meth = cls.getMethod(name, md.getArgTypes());
-         am = new AnnotatedMethod(cls, instance, meth, md);
+         am = new AnnotatedMethod(cls, null, meth, md);
       } catch (Exception e) {
          if (isDebug) {
             StringBuilder txt = new StringBuilder();


Mime
View raw message