portals-jetspeed-dev mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From tay...@apache.org
Subject svn commit: r1816790 - in /portals/jetspeed-2/portal/trunk: components/jetspeed-portlet-factory/src/main/java/org/apache/jetspeed/factory/ jetspeed-portal-resources/src/main/resources/assembly/ jetspeed-portal-resources/src/main/resources/conf/jetspeed/
Date Fri, 01 Dec 2017 02:52:44 GMT
Author: taylor
Date: Fri Dec  1 02:52:44 2017
New Revision: 1816790

URL: http://svn.apache.org/viewvc?rev=1816790&view=rev
Log:
JS2-1363: Jetspeed Portlet Factory extension to provide factories for creating portlets, filters
and validators

Modified:
    portals/jetspeed-2/portal/trunk/components/jetspeed-portlet-factory/src/main/java/org/apache/jetspeed/factory/JetspeedPortletFactory.java
    portals/jetspeed-2/portal/trunk/jetspeed-portal-resources/src/main/resources/assembly/registry.xml
    portals/jetspeed-2/portal/trunk/jetspeed-portal-resources/src/main/resources/conf/jetspeed/jetspeed.properties

Modified: portals/jetspeed-2/portal/trunk/components/jetspeed-portlet-factory/src/main/java/org/apache/jetspeed/factory/JetspeedPortletFactory.java
URL: http://svn.apache.org/viewvc/portals/jetspeed-2/portal/trunk/components/jetspeed-portlet-factory/src/main/java/org/apache/jetspeed/factory/JetspeedPortletFactory.java?rev=1816790&r1=1816789&r2=1816790&view=diff
==============================================================================
--- portals/jetspeed-2/portal/trunk/components/jetspeed-portlet-factory/src/main/java/org/apache/jetspeed/factory/JetspeedPortletFactory.java
(original)
+++ portals/jetspeed-2/portal/trunk/components/jetspeed-portlet-factory/src/main/java/org/apache/jetspeed/factory/JetspeedPortletFactory.java
Fri Dec  1 02:52:44 2017
@@ -16,28 +16,6 @@
  */
 package org.apache.jetspeed.factory;
 
-import java.io.IOException;
-import java.lang.reflect.Method;
-import java.util.ArrayList;
-import java.util.Collections;
-import java.util.HashMap;
-import java.util.List;
-import java.util.Locale;
-import java.util.Map;
-import java.util.MissingResourceException;
-import java.util.PropertyResourceBundle;
-import java.util.ResourceBundle;
-
-import javax.portlet.GenericPortlet;
-import javax.portlet.Portlet;
-import javax.portlet.PortletException;
-import javax.portlet.PortletMode;
-import javax.portlet.PortletURLGenerationListener;
-import javax.portlet.PreferencesValidator;
-import javax.portlet.UnavailableException;
-import javax.portlet.filter.PortletFilter;
-import javax.servlet.ServletContext;
-
 import org.apache.jetspeed.PortalContext;
 import org.apache.jetspeed.components.portletregistry.RegistryEventListener;
 import org.apache.jetspeed.container.ContainerInfo;
@@ -58,6 +36,28 @@ import org.apache.portals.bridges.common
 import org.slf4j.Logger;
 import org.slf4j.LoggerFactory;
 
+import javax.portlet.GenericPortlet;
+import javax.portlet.Portlet;
+import javax.portlet.PortletException;
+import javax.portlet.PortletMode;
+import javax.portlet.PortletURLGenerationListener;
+import javax.portlet.PreferencesValidator;
+import javax.portlet.UnavailableException;
+import javax.portlet.filter.PortletFilter;
+import javax.servlet.ServletContext;
+import java.io.IOException;
+import java.lang.reflect.InvocationTargetException;
+import java.lang.reflect.Method;
+import java.util.ArrayList;
+import java.util.Collections;
+import java.util.HashMap;
+import java.util.List;
+import java.util.Locale;
+import java.util.Map;
+import java.util.MissingResourceException;
+import java.util.PropertyResourceBundle;
+import java.util.ResourceBundle;
+
 /**
  * <p>
  * JetspeedPortletFactory
@@ -70,10 +70,9 @@ import org.slf4j.LoggerFactory;
  * @version $Id$
  * 
  */
-public class JetspeedPortletFactory implements PortletFactory, RegistryEventListener
-{
+public class JetspeedPortletFactory implements PortletFactory, RegistryEventListener {
     private static final Logger log = LoggerFactory.getLogger(JetspeedPortletFactory.class);
-    
+
     private Map<String, Map<String, PortletInstance>> portletCache;
     private Map<String, Map<String, PreferencesValidator>> validatorCache;
     private Map<String, Map<String, PortletFilterInstance>> portletFilterCache;
@@ -108,29 +107,33 @@ public class JetspeedPortletFactory impl
      * Delegated portlet unique name for config mode
      */
     private String customConfigModePortletUniqueName;
-    
+
     /**
      * Flag whether the instantiated proxy will switch preview mode to built-in
      * preview markup generating portlet or not.
      */
     private boolean autoSwitchPreviewMode;
-    
+
     /**
      * Delegated portlet unique name for preview mode
      */
     private String customPreviewModePortletUniqueName;
 
-    public JetspeedPortletFactory(RequestDispatcherService rdService)
-    {
+    private boolean isFactoryExtensionsEnabled = false;
+
+    public JetspeedPortletFactory(RequestDispatcherService rdService) {
         this(rdService, false, false);
     }
-    
-    public JetspeedPortletFactory(RequestDispatcherService rdService, boolean autoSwitchConfigMode,
boolean autoSwitchEditDefaultsModeToEditMode)
-    {
+
+    public JetspeedPortletFactory(RequestDispatcherService rdService, boolean autoSwitchConfigMode,
boolean autoSwitchEditDefaultsModeToEditMode) {
         this(rdService, autoSwitchConfigMode, autoSwitchEditDefaultsModeToEditMode, false);
     }
-    
-    public JetspeedPortletFactory(RequestDispatcherService rdService, boolean autoSwitchConfigMode,
boolean autoSwitchEditDefaultsModeToEditMode, boolean autoSwitchPreviewMode)
+
+    public JetspeedPortletFactory(RequestDispatcherService rdService, boolean autoSwitchConfigMode,
boolean autoSwitchEditDefaultsModeToEditMode, boolean autoSwitchPreviewMode) {
+        this(rdService, autoSwitchConfigMode, autoSwitchEditDefaultsModeToEditMode, false,
false);
+    }
+
+    public JetspeedPortletFactory(RequestDispatcherService rdService, boolean autoSwitchConfigMode,
boolean autoSwitchEditDefaultsModeToEditMode, boolean autoSwitchPreviewMode, boolean isFactoryExtensionsEnabled)
     {
         this.rdService = rdService;
         this.portletCache = Collections.synchronizedMap(new HashMap<String, Map<String,
PortletInstance>>());
@@ -145,6 +148,7 @@ public class JetspeedPortletFactory impl
         this.autoSwitchPreviewMode = autoSwitchPreviewMode;
         this.portletProxyUsed = (this.autoSwitchConfigMode || this.autoSwitchEditDefaultsModeToEditMode
|| this.autoSwitchPreviewMode);
         this.servletContextProvider = new JetspeedServletContextProviderImpl(rdService);
+        this.isFactoryExtensionsEnabled = isFactoryExtensionsEnabled;
     }
     
     protected ResourceBundle loadResourceBundle( Locale locale, String bundleName, ClassLoader
cl )
@@ -310,8 +314,7 @@ public class JetspeedPortletFactory impl
                         try
                         {
                             Class<?> clazz = paCl.loadClass(className);
-                            validator = (PreferencesValidator) clazz.newInstance();
-                            
+                            validator = (PreferencesValidator) createExtendedFactoryObject(clazz,
"createValidator", pdName, paName);
                             if (instanceCache == null)
                             {
                                 instanceCache = Collections.synchronizedMap(new HashMap<String,
PreferencesValidator>());
@@ -442,7 +445,7 @@ public class JetspeedPortletFactory impl
      * Gets a portlet by either creating it or returning a handle to it from the
      * portlet 'cache'
      * 
-     * @param portletDefinition
+     * @param pd
      *            The definition of the portlet
      * @return PortletInstance
      * @throws PortletException
@@ -456,7 +459,7 @@ public class JetspeedPortletFactory impl
      * Gets a portlet by either creating it or returning a handle to it from the
      * portlet 'cache'
      * 
-     * @param portletDefinition
+     * @param pd
      *            The definition of the portlet
      * @return PortletInstance
      * @throws PortletException
@@ -510,12 +513,12 @@ public class JetspeedPortletFactory impl
                             // complete
                             // and thereby releasing all its ClassLoader locks as
                             // needed for local portlets.
-
+                            Portlet newInstance = (Portlet)createExtendedFactoryObject(clazz,
"createPortlet", pdName, paName);
                             if (proxyUsed)
                             {
                                 portlet = new JetspeedPortletProxyInstance(
                                                                            pd.getPortletName(),
-                                                                           (Portlet) clazz.newInstance(),
+                                                                           newInstance,
                                                                            this.autoSwitchEditDefaultsModeToEditMode,
                                                                            this.autoSwitchConfigMode,
this.customConfigModePortletUniqueName,
                                                                            this.autoSwitchPreviewMode,
this.customPreviewModePortletUniqueName
@@ -523,7 +526,7 @@ public class JetspeedPortletFactory impl
                             }
                             else
                             {
-                                portlet = new JetspeedPortletInstance(pdName, (Portlet)clazz.newInstance());
+                                portlet = new JetspeedPortletInstance(pdName, newInstance);
                             }
                         }
                         finally
@@ -707,7 +710,7 @@ public class JetspeedPortletFactory impl
                     try
                     {
                         Class<? extends Object> clazz = paCl.loadClass(filter.getFilterClass());
-                        PortletFilter portletFilter = (PortletFilter) clazz.newInstance();
+                        PortletFilter portletFilter = (PortletFilter)createExtendedFactoryObject(clazz,
"createFilter", filterName, paName);
                         filterInstance = new JetspeedPortletFilterInstance(filter, portletFilter);
                     }
                     catch (ClassNotFoundException e)
@@ -716,7 +719,7 @@ public class JetspeedPortletFactory impl
                         log.error(message);
                         throw new UnavailableException(message);
                     }
-                    catch (InstantiationException e)
+                    catch (InstantiationException | InvocationTargetException e)
                     {
                         String message = "The filter class instantiation fail: " + filter.getFilterClass();
                         log.error(message);
@@ -931,4 +934,52 @@ public class JetspeedPortletFactory impl
 	{
 		portletUpdated(pd);
 	}
+
+    /**
+     * Provide the portlet provider with the ability to create its own portlets, portlet
filters, and validators
+     *
+     * Three static factory methods supported as
+     *  createPortlet
+     *  createFilter
+     *  createValidator
+     *
+     * @param clazz the portlet class
+     * @param factoryMethod the factory create method to invoke
+     * @param name the name of the portlet
+     * @param paName the portlet application name
+     * @return a new Portlet, PortletFilter, or PreferencesValidator
+     * @throws InvocationTargetException
+     * @throws InstantiationException
+     * @throws IllegalAccessException
+     */
+    protected Object createExtendedFactoryObject(Class clazz, String factoryMethod, String
name, String paName)
+            throws InvocationTargetException, InstantiationException, IllegalAccessException
{
+        if (!isFactoryExtensionsEnabled) {
+            return clazz.newInstance();
+        }
+        Object newInstance = null;
+        Method method = null;
+        try {
+            method = clazz.getMethod(factoryMethod, Class.class, String.class, String.class);
+        }
+        catch (NoSuchMethodException e) {
+            return clazz.newInstance();
+        }
+        if (method != null) {
+            try {
+                newInstance = method.invoke(null, clazz, name, paName);
+                if (newInstance == null) {
+                    newInstance = clazz.newInstance();
+                }
+            }
+            catch(Exception e) {
+                log.error("Failed to invoke class factory method: " + e.getMessage(), e);
+                newInstance = clazz.newInstance();
+            }
+        }
+        else {
+            newInstance = clazz.newInstance();
+        }
+        return newInstance;
+    }
 }

Modified: portals/jetspeed-2/portal/trunk/jetspeed-portal-resources/src/main/resources/assembly/registry.xml
URL: http://svn.apache.org/viewvc/portals/jetspeed-2/portal/trunk/jetspeed-portal-resources/src/main/resources/assembly/registry.xml?rev=1816790&r1=1816789&r2=1816790&view=diff
==============================================================================
--- portals/jetspeed-2/portal/trunk/jetspeed-portal-resources/src/main/resources/assembly/registry.xml
(original)
+++ portals/jetspeed-2/portal/trunk/jetspeed-portal-resources/src/main/resources/assembly/registry.xml
Fri Dec  1 02:52:44 2017
@@ -39,6 +39,9 @@
     <constructor-arg index="3">
       <value>${supported.portletmode.autoswitch.preview}</value>
     </constructor-arg>
+    <constructor-arg index="4">
+      <value>${factory.portlet.extensions}</value>
+    </constructor-arg>
     <!-- Surrogate portlet for config modes. -->
     <property name="customConfigModePortletUniqueName">
       <value>${supported.portletmode.autoswitch.config.surrogate.portlet}</value>

Modified: portals/jetspeed-2/portal/trunk/jetspeed-portal-resources/src/main/resources/conf/jetspeed/jetspeed.properties
URL: http://svn.apache.org/viewvc/portals/jetspeed-2/portal/trunk/jetspeed-portal-resources/src/main/resources/conf/jetspeed/jetspeed.properties?rev=1816790&r1=1816789&r2=1816790&view=diff
==============================================================================
--- portals/jetspeed-2/portal/trunk/jetspeed-portal-resources/src/main/resources/conf/jetspeed/jetspeed.properties
(original)
+++ portals/jetspeed-2/portal/trunk/jetspeed-portal-resources/src/main/resources/conf/jetspeed/jetspeed.properties
Fri Dec  1 02:52:44 2017
@@ -474,5 +474,11 @@ autorefresh.enabled = true
 #-------------------------------------------------------------------------
 resource.loader.filesystem = true
 
+#-------------------------------------------------------------------------
+# Enable loading portlets from factory extensions
+# since 2.3.2
+#-------------------------------------------------------------------------
+factory.portlet.extensions = false
+
 
 



---------------------------------------------------------------------
To unsubscribe, e-mail: jetspeed-dev-unsubscribe@portals.apache.org
For additional commands, e-mail: jetspeed-dev-help@portals.apache.org


Mime
View raw message