You are viewing a plain text version of this content. The canonical link for it is here.
Posted to pluto-scm@portals.apache.org by at...@apache.org on 2008/05/04 04:13:40 UTC

svn commit: r653170 - in /portals/pluto/branches/2.0-spi-refactoring: pluto-container-api/src/main/java/org/apache/pluto/spi/optional/ pluto-container/src/main/java/org/apache/pluto/core/ pluto-container/src/main/java/org/apache/pluto/internal/impl/

Author: ate
Date: Sat May  3 19:13:38 2008
New Revision: 653170

URL: http://svn.apache.org/viewvc?rev=653170&view=rev
Log:
Simplication en code optimization of PortletPreferencesService  by replacing the InternalPortletPreference[] array passed along by a typed Map.
The PortletPreferencesImpl was already managing these internally in a Map.
Sending arrays back and forth really makes no sense as each time these had to be converted from Map to array and visa versa. 

Modified:
    portals/pluto/branches/2.0-spi-refactoring/pluto-container-api/src/main/java/org/apache/pluto/spi/optional/PortletPreferencesService.java
    portals/pluto/branches/2.0-spi-refactoring/pluto-container/src/main/java/org/apache/pluto/core/DefaultPortletPreferencesService.java
    portals/pluto/branches/2.0-spi-refactoring/pluto-container/src/main/java/org/apache/pluto/internal/impl/PortletPreferencesImpl.java

Modified: portals/pluto/branches/2.0-spi-refactoring/pluto-container-api/src/main/java/org/apache/pluto/spi/optional/PortletPreferencesService.java
URL: http://svn.apache.org/viewvc/portals/pluto/branches/2.0-spi-refactoring/pluto-container-api/src/main/java/org/apache/pluto/spi/optional/PortletPreferencesService.java?rev=653170&r1=653169&r2=653170&view=diff
==============================================================================
--- portals/pluto/branches/2.0-spi-refactoring/pluto-container-api/src/main/java/org/apache/pluto/spi/optional/PortletPreferencesService.java (original)
+++ portals/pluto/branches/2.0-spi-refactoring/pluto-container-api/src/main/java/org/apache/pluto/spi/optional/PortletPreferencesService.java Sat May  3 19:13:38 2008
@@ -16,6 +16,8 @@
  */
 package org.apache.pluto.spi.optional;
 
+import java.util.Map;
+
 import javax.portlet.PortletRequest;
 
 import org.apache.pluto.PortletContainerException;
@@ -37,7 +39,7 @@
      * @return the default portlet preferences.
      * @throws PortletContainerException  if fail to get default preferences.
      */
-    public InternalPortletPreference[] getDefaultPreferences(
+    public Map<String, InternalPortletPreference> getDefaultPreferences(
             PortletWindow portletWindow,
             PortletRequest request)
     throws PortletContainerException;
@@ -49,7 +51,7 @@
 	 * @return the stored portlet preferences.
 	 * @throws PortletContainerException  if fail to get stored preferences.
 	 */
-    public InternalPortletPreference[] getStoredPreferences(
+    public Map<String, InternalPortletPreference> getStoredPreferences(
     		PortletWindow portletWindow,
     		PortletRequest request)
     throws PortletContainerException;
@@ -63,7 +65,7 @@
      */
     public void store(PortletWindow portletWindow,
                       PortletRequest request,
-                      InternalPortletPreference[] preferences)
+                      Map<String, InternalPortletPreference> preferences)
     throws PortletContainerException;
 
 }

Modified: portals/pluto/branches/2.0-spi-refactoring/pluto-container/src/main/java/org/apache/pluto/core/DefaultPortletPreferencesService.java
URL: http://svn.apache.org/viewvc/portals/pluto/branches/2.0-spi-refactoring/pluto-container/src/main/java/org/apache/pluto/core/DefaultPortletPreferencesService.java?rev=653170&r1=653169&r2=653170&view=diff
==============================================================================
--- portals/pluto/branches/2.0-spi-refactoring/pluto-container/src/main/java/org/apache/pluto/core/DefaultPortletPreferencesService.java (original)
+++ portals/pluto/branches/2.0-spi-refactoring/pluto-container/src/main/java/org/apache/pluto/core/DefaultPortletPreferencesService.java Sat May  3 19:13:38 2008
@@ -16,6 +16,7 @@
  */
 package org.apache.pluto.core;
 
+import java.util.Collections;
 import java.util.HashMap;
 import java.util.Map;
 
@@ -55,7 +56,7 @@
 	 * The in-memory portlet preferences storage: key is the preference name as
 	 * a string, value is an array of PortletPreference objects.
 	 */
-	private Map<String,InternalPortletPreference[]> storage = new HashMap<String,InternalPortletPreference[]>();
+	private Map<String,Map<String,InternalPortletPreference>> storage = new HashMap<String,Map<String,InternalPortletPreference>>();
 
 
 	// Constructor -------------------------------------------------------------
@@ -71,16 +72,16 @@
 	// PortletPreferencesService Impl ------------------------------------------
 
     /**
-     * Returns an array of default preferences for a PortletWindow. The default
+     * Returns a map of default preferences for a PortletWindow. The default
      * preferences are retrieved from the portlet application descriptor.
      * <p>
      * Data retrieved from <code>portlet.xml</code> are injected into the domain
-     * object <code>PortletPreferenceDefinition</code>. This method converts the domain
+     * object <code>PortletPreference</code>. This method converts the domain
      * objects into <code>PortletPreference</code> objects.
      * </p>
      * <p>
      * Note that if no value is bound to a given preference key,
-     * <code>PortletPreferenceDefinition.getValues()</code> will return an empty string
+     * <code>PortletPreference.getValues()</code> will return an empty string
      * list, but the value array of <code>PortletPreference</code> should be set
      * to null (instead of an empty array).
      * </p>
@@ -88,54 +89,53 @@
      * This method never returns null, but the values held by PortletPreference
      * may be null.
      * </p>
-     * @return the preference set
+     * @return the default preferences set
      * 
      * @see org.apache.pluto.descriptors.portlet.PortletPreferenceDD
      */
-    public InternalPortletPreference[] getDefaultPreferences( PortletWindow portletWindow,
+    public Map<String,InternalPortletPreference> getDefaultPreferences( PortletWindow portletWindow,
                                                               PortletRequest request )
       throws PortletContainerException {
-        InternalPortletPreference[] preferences = null;
-        Portlet portletD = portletWindow.getPortletEntity().getPortletDefinition();
-        PortletPreferences prefsD = portletD.getPortletPreferences();
-        if (prefsD != null && prefsD.getPortletPreferences() != null) {
-            preferences = new InternalPortletPreference[prefsD.getPortletPreferences().size()];
-            int index = 0;
-            for (PortletPreference prefD : prefsD.getPortletPreferences()) {
+        Map<String,InternalPortletPreference> preferences = null;
+        Portlet portlet = portletWindow.getPortletEntity().getPortletDefinition();
+        PortletPreferences prefs = portlet.getPortletPreferences();
+        if (prefs != null && prefs.getPortletPreferences() != null) {
+            preferences = new HashMap<String,InternalPortletPreference>(prefs.getPortletPreferences().size());
+            for (PortletPreference pref : prefs.getPortletPreferences()) {
                 String[] values = null;
-                if (prefD.getValues() != null && prefD.getValues().size() > 0) {
-                    values = prefD.getValues().toArray(new String[prefD.getValues().size()]);
+                if (pref.getValues() != null && pref.getValues().size() > 0) {
+                    values = pref.getValues().toArray(new String[pref.getValues().size()]);
                 }
-                preferences[index++] = new PortletPreferenceImpl(prefD.getName(), values, prefD.isReadOnly());
+                preferences.put(pref.getName(), new PortletPreferenceImpl(pref.getName(), values, pref.isReadOnly()));
             }
         }
         return preferences;
     }
 
 	/**
-	 * Returns the stored portlet preferences array. The preferences managed by
+	 * Returns the stored portlet preferences map. The preferences managed by
 	 * this service should be protected from being directly accessed, so this
 	 * method returns a cloned copy of the stored preferences.
 	 *
 	 * @param portletWindow  the portlet window.
 	 * @param request  the portlet request from which the remote user is retrieved.
-	 * @return a copy of the stored portlet preferences array.
+	 * @return a copy of the stored portlet preferences map.
 	 * @throws PortletContainerException
 	 */
-	public InternalPortletPreference[] getStoredPreferences(
+	public Map<String,InternalPortletPreference> getStoredPreferences(
 			PortletWindow portletWindow,
 			PortletRequest request)
 	throws PortletContainerException {
         String key = getFormattedKey(portletWindow, request);
-        InternalPortletPreference[] preferences = storage.get(key);
+        Map<String,InternalPortletPreference> preferences = storage.get(key);
         if (preferences == null) {
             if (LOG.isDebugEnabled()) {
                 LOG.debug("No portlet preferences found for: " + key);
             }
-            return new InternalPortletPreference[0];
+            return Collections.emptyMap();
         } else {
         	if (LOG.isDebugEnabled()) {
-        		LOG.debug("Got " + preferences.length + " stored preferences.");
+        		LOG.debug("Got " + preferences.size() + " stored preferences.");
         	}
         	return clonePreferences(preferences);
         }
@@ -147,7 +147,7 @@
 	 * preference validator (if defined).
 	 * <p>
 	 * The preferences managed by this service should be protected from being
-	 * directly accessed, so this method clones the passed-in preferences array
+	 * directly accessed, so this method clones the passed-in preferences map
 	 * and saves it.
 	 * </p>
 	 *
@@ -160,7 +160,7 @@
 	 */
     public void store(PortletWindow portletWindow,
                       PortletRequest request,
-                      InternalPortletPreference[] preferences)
+                      Map<String,InternalPortletPreference> preferences)
     throws PortletContainerException {
         String key = getFormattedKey(portletWindow, request);
         storage.put(key, clonePreferences(preferences));
@@ -187,27 +187,23 @@
     }
 
     /**
-     * Clones a PortletPreference array. This method performs a deep clone on
-     * the passed-in portlet preferences array. Every PortletPreference object
-     * in the array are cloned (via the <code>PortletPreference.clone()</code>
-     * method) and injected into the new array.
+     * Clones a PortletPreference map. This method performs a deep clone on
+     * the passed-in portlet preferences map. Every PortletPreference object
+     * in the map are cloned (via the <code>PortletPreference.clone()</code>
+     * method) and injected into the new map.
      *
-     * @param preferences  the portlet preferences array to clone.
-     * @return a deep-cloned copy of the portlet preferences array.
+     * @param preferences  the portlet preferences map to clone.
+     * @return a deep-cloned copy of the portlet preferences map.
      */
-    private InternalPortletPreference[] clonePreferences(
-    		InternalPortletPreference[] preferences) {
+    private Map<String,InternalPortletPreference> clonePreferences(
+    		Map <String,InternalPortletPreference> preferences) {
     	if (preferences == null) {
     		return null;
     	}
-    	InternalPortletPreference[] copy =
-    			new InternalPortletPreference[preferences.length];
-    	for (int i = 0; i < preferences.length; i++) {
-    		if (preferences[i] != null) {
-    			copy[i] = preferences[i].clone();
-    		} else {
-    			copy[i] = null;
-    		}
+    	Map <String,InternalPortletPreference> copy =
+    			new HashMap<String,InternalPortletPreference>(preferences.size());
+    	for (InternalPortletPreference p : preferences.values()) {
+    	    copy.put(p.getName(), p.clone());
     	}
     	return copy;
     }

Modified: portals/pluto/branches/2.0-spi-refactoring/pluto-container/src/main/java/org/apache/pluto/internal/impl/PortletPreferencesImpl.java
URL: http://svn.apache.org/viewvc/portals/pluto/branches/2.0-spi-refactoring/pluto-container/src/main/java/org/apache/pluto/internal/impl/PortletPreferencesImpl.java?rev=653170&r1=653169&r2=653170&view=diff
==============================================================================
--- portals/pluto/branches/2.0-spi-refactoring/pluto-container/src/main/java/org/apache/pluto/internal/impl/PortletPreferencesImpl.java (original)
+++ portals/pluto/branches/2.0-spi-refactoring/pluto-container/src/main/java/org/apache/pluto/internal/impl/PortletPreferencesImpl.java Sat May  3 19:13:38 2008
@@ -70,7 +70,7 @@
      * Default portlet preferences retrieved from portlet.xml, and used for
      * resetting portlet preferences.
      */
-    private InternalPortletPreference[] defaultPreferences;
+    private Map<String,InternalPortletPreference> defaultPreferences;
     
     /**
      * Current portlet preferences: key is the preference name as a string,
@@ -111,7 +111,7 @@
             // Put default portlet preferences into preferences map.
             defaultPreferences = preferencesService.getDefaultPreferences(window, request);
             if (defaultPreferences != null) {
-                for (InternalPortletPreference p : defaultPreferences) {
+                for (InternalPortletPreference p : defaultPreferences.values()) {
                     preferences.put(p.getName(), p.clone());
                 }
             }
@@ -121,20 +121,16 @@
             
             // Merge stored portlet preferences into preferences map.
             
-            InternalPortletPreference[] storedPreferences = preferencesService
+            Map<String,InternalPortletPreference> storedPreferences = preferencesService
             		.getStoredPreferences(window, request);
-            for (InternalPortletPreference p : storedPreferences) {
-                if (LOG.isDebugEnabled()) {
-                    LOG.debug("Merging stored preference: "
-                            + p.getName());
-                }
-                preferences.put(p.getName(), p);
-            }
+            preferences.putAll(storedPreferences);
+
         	// Store the preferences retrieved from portlet.xml.
             //   Portlet preferences are stored everytime when a
             //   PortletPreferencesImpl instance is created.
+            // TODO Ate: why?
             //   So here we do not check the portlet request method ID.
-            if (storedPreferences.length > 0)
+            if (storedPreferences.size() > 0)
             	internalStore();
         	
         } catch (PortletContainerException ex) {
@@ -241,19 +237,15 @@
             		"error.preference.readonly", "Preference key "));
         }
         // Try to reset preference to the default values.
-        boolean resetDone = false;
-        for (InternalPortletPreference p : defaultPreferences) {
-            if (key.equals(p.getName())) {
-                if (LOG.isDebugEnabled()) {
-                    LOG.debug("Resetting preference for key: " + key);
-                }
-                preferences.put(key,p.clone());
-                resetDone = true;
-                break;
+        InternalPortletPreference p = defaultPreferences.get(key);
+        if (p != null) {
+            if (LOG.isDebugEnabled()) {
+                LOG.debug("Resetting preference for key: " + key);
             }
-        }
+            preferences.put(key,p.clone());
+        }       
         // Remove preference if default values are not defined (PLT.14.1).
-        if (!resetDone) {
+        else {
         	if (LOG.isDebugEnabled()) {
         		LOG.debug("Resetting preference to null for key: " + key);
         	}
@@ -305,13 +297,8 @@
         	validator.validate(this);
         }
         // Store the portlet preferences.
-        InternalPortletPreference[] prefs = new InternalPortletPreference[preferences.size()];
-        int index = 0;
-        for (InternalPortletPreference p : preferences.values()) {
-            prefs[index++] = p;
-        }
         try {
-        	preferencesService.store(window, request, prefs);
+        	preferencesService.store(window, request, preferences);
         } catch (PortletContainerException ex) {
             LOG.error("Error storing preferences.", ex);
             throw new IOException("Error storing perferences: " + ex.getMessage());