portals-jetspeed-dev mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From a..@apache.org
Subject svn commit: r887871 - in /portals/jetspeed-2/portal/trunk/components/jetspeed-portal/src/main/java/org/apache/jetspeed/userinfo/impl: AbstractUserInfoManagerImpl.java MultiSourceUserInfoManagerImpl.java UserInfoManagerImpl.java
Date Mon, 07 Dec 2009 09:53:17 GMT
Author: ate
Date: Mon Dec  7 09:53:16 2009
New Revision: 887871

URL: http://svn.apache.org/viewvc?rev=887871&view=rev
Log:
JS2-1088: Improvements for the mapped user attributes resolving and the UserInfoManager cache
handling

Modified:
    portals/jetspeed-2/portal/trunk/components/jetspeed-portal/src/main/java/org/apache/jetspeed/userinfo/impl/AbstractUserInfoManagerImpl.java
    portals/jetspeed-2/portal/trunk/components/jetspeed-portal/src/main/java/org/apache/jetspeed/userinfo/impl/MultiSourceUserInfoManagerImpl.java
    portals/jetspeed-2/portal/trunk/components/jetspeed-portal/src/main/java/org/apache/jetspeed/userinfo/impl/UserInfoManagerImpl.java

Modified: portals/jetspeed-2/portal/trunk/components/jetspeed-portal/src/main/java/org/apache/jetspeed/userinfo/impl/AbstractUserInfoManagerImpl.java
URL: http://svn.apache.org/viewvc/portals/jetspeed-2/portal/trunk/components/jetspeed-portal/src/main/java/org/apache/jetspeed/userinfo/impl/AbstractUserInfoManagerImpl.java?rev=887871&r1=887870&r2=887871&view=diff
==============================================================================
--- portals/jetspeed-2/portal/trunk/components/jetspeed-portal/src/main/java/org/apache/jetspeed/userinfo/impl/AbstractUserInfoManagerImpl.java
(original)
+++ portals/jetspeed-2/portal/trunk/components/jetspeed-portal/src/main/java/org/apache/jetspeed/userinfo/impl/AbstractUserInfoManagerImpl.java
Mon Dec  7 09:53:16 2009
@@ -17,8 +17,7 @@
 package org.apache.jetspeed.userinfo.impl;
 
 import java.util.ArrayList;
-import java.util.Collection;
-import java.util.Iterator;
+import java.util.List;
 import java.util.Map;
 
 import javax.portlet.PortletRequest;
@@ -29,7 +28,6 @@
 import org.apache.jetspeed.om.portlet.UserAttributeRef;
 import org.apache.jetspeed.om.portlet.impl.UserAttributeRefImpl;
 import org.apache.jetspeed.request.RequestContext;
-import org.apache.pluto.container.PortletContainerException;
 import org.apache.jetspeed.container.PortletWindow;
 
 /**
@@ -55,50 +53,47 @@
      * @param userAttributeRefs
      *            The declarative jetspeed portlet extension user attributes
      *            reference.
-     * @return The collection of linked attributes.
+     * @return The list of linked attributes.
      */
-    protected Collection mapLinkedUserAttributes(Collection userAttributes, Collection userAttributeRefs)
+    protected List<UserAttributeRef> mapLinkedUserAttributes(List<UserAttribute>
userAttributes, List<UserAttributeRef> userAttributeRefs)
     {
-        Collection linkedUserAttributes = new ArrayList();
+        UserAttributeRefImpl impl;
+        List<UserAttributeRef> linkedUserAttributes = new ArrayList<UserAttributeRef>();
         if ((null != userAttributeRefs) && (userAttributeRefs.size() > 0))
         {
-            Iterator attrIter = userAttributes.iterator();
-            while (attrIter.hasNext())
+            for (UserAttribute currentAttribute : userAttributes)
             {
-                UserAttribute currentAttribute = (UserAttribute) attrIter.next();
                 boolean linkedAttribute = false;
-                if (null != currentAttribute)
+                impl = new UserAttributeRefImpl();
+                for (UserAttributeRef currentAttributeRef : userAttributeRefs)
                 {
-                    Iterator attrRefsIter = userAttributeRefs.iterator();
-                    while (attrRefsIter.hasNext())
+                    if ((currentAttribute.getName()).equals(currentAttributeRef.getNameLink()))
                     {
-                        UserAttributeRef currentAttributeRef = (UserAttributeRef) attrRefsIter.next();
-                        if (null != currentAttributeRef)
+                        if (log.isDebugEnabled())
                         {
-                            if ((currentAttribute.getName()).equals(currentAttributeRef.getNameLink()))
-                            {
-                                if (log.isDebugEnabled())
-                                    log.debug("Linking user attribute ref: [[name, " + currentAttribute.getName()
-                                            + "], [linked name, " + currentAttributeRef.getName()
+ "]]");
-                                linkedUserAttributes.add(currentAttributeRef);
-                                linkedAttribute = true;
-                            }
+                            log.debug("Linking user attribute ref: [[name, " + currentAttribute.getName()
+                                    + "], [linked name, " + currentAttributeRef.getName()
+ "]]");
                         }
+                        impl.setName(currentAttributeRef.getName());
+                        impl.setNameLink(currentAttributeRef.getNameLink());
+                        linkedAttribute = true;
+                        break;
                     }
                 }
                 if (!linkedAttribute)
                 {
-                    linkedUserAttributes.add(new UserAttributeRefImpl(currentAttribute));
+                    impl.setName(currentAttribute.getName());
                 }
+                linkedUserAttributes.add(impl);
             }
         }
         else
         {
-            Iterator attrIter = userAttributes.iterator();
-            while (attrIter.hasNext())
+            for (UserAttribute currentAttribute : userAttributes)
             {
-                UserAttribute currentAttribute = (UserAttribute) attrIter.next();
-                linkedUserAttributes.add(new UserAttributeRefImpl(currentAttribute));
+                impl = new UserAttributeRefImpl();
+                impl.setName(currentAttribute.getName());
+                linkedUserAttributes.add(impl);
             }
         }
         return linkedUserAttributes;
@@ -107,10 +102,9 @@
     /**
      * For Pluto 2.0
      */
-    public Map<String, String> getUserInfo(PortletRequest request, org.apache.pluto.container.PortletWindow
window) throws PortletContainerException
+    public Map<String, String> getUserInfo(PortletRequest request, org.apache.pluto.container.PortletWindow
window)
     {
-        String remoteUser = request.getRemoteUser(); 
-        if ( remoteUser == null ) 
+        if ( request.getUserPrincipal() == null ) 
         {
             return null;
         }

Modified: portals/jetspeed-2/portal/trunk/components/jetspeed-portal/src/main/java/org/apache/jetspeed/userinfo/impl/MultiSourceUserInfoManagerImpl.java
URL: http://svn.apache.org/viewvc/portals/jetspeed-2/portal/trunk/components/jetspeed-portal/src/main/java/org/apache/jetspeed/userinfo/impl/MultiSourceUserInfoManagerImpl.java?rev=887871&r1=887870&r2=887871&view=diff
==============================================================================
--- portals/jetspeed-2/portal/trunk/components/jetspeed-portal/src/main/java/org/apache/jetspeed/userinfo/impl/MultiSourceUserInfoManagerImpl.java
(original)
+++ portals/jetspeed-2/portal/trunk/components/jetspeed-portal/src/main/java/org/apache/jetspeed/userinfo/impl/MultiSourceUserInfoManagerImpl.java
Mon Dec  7 09:53:16 2009
@@ -16,23 +16,19 @@
  */
 package org.apache.jetspeed.userinfo.impl;
 
-import java.util.Collection;
 import java.util.HashMap;
-import java.util.Iterator;
 import java.util.List;
 import java.util.Map;
 
-import javax.portlet.PortletRequest;
 import javax.security.auth.Subject;
 
 import org.slf4j.Logger;
 import org.slf4j.LoggerFactory;
 import org.apache.jetspeed.components.portletregistry.PortletRegistry;
-import org.apache.jetspeed.om.portlet.PortletApplication;
+import org.apache.jetspeed.om.portlet.UserAttributeRef;
 import org.apache.jetspeed.request.RequestContext;
 import org.apache.jetspeed.userinfo.UserAttributeRetrievalException;
 import org.apache.jetspeed.userinfo.UserAttributeSource;
-import org.apache.jetspeed.userinfo.UserInfoManager;
 
 /**
  * Multisource User Information manager
@@ -42,57 +38,51 @@
  * @author <a href="mailto:taylor@apache.org">David Sean Taylor </a>
  * @version $Id: $
  */
-public class MultiSourceUserInfoManagerImpl extends AbstractUserInfoManagerImpl
-        implements UserInfoManager
+public class MultiSourceUserInfoManagerImpl extends UserInfoManagerImpl
 {
 
     /** Logger */
     private static final Logger log = LoggerFactory.getLogger(MultiSourceUserInfoManagerImpl.class);
 
-    private List sources;
+    private List<UserAttributeSource> sources;
 
-    private PortletRegistry portletRegistry;
-   
-    public Map getUserInfoMap(String appName, RequestContext context)
+    public Map<String,String> getUserInfoMap(String appName, RequestContext context)
     {
-
+        Map<String,String> userInfoMap = null;
         try
         {
-            Map userInfoMap = new HashMap();
             Subject subject = context.getSubject();
-            PortletApplication pa = portletRegistry.getPortletApplication(appName, true);
-            if (null == pa)
+            if (null != subject)
             {
-                log.debug(PortletRequest.USER_INFO + " is set to null");
-                return null;
+                
             }
-            Collection userAttributes = pa.getUserAttributes();
-            Collection userAttributeRefs = pa.getUserAttributeRefs();
-            Collection linkedUserAttributes = mapLinkedUserAttributes(
-                    userAttributes, userAttributeRefs);
-            for (Iterator iter = sources.iterator(); iter.hasNext();)
+            userInfoMap = new HashMap<String,String>();
+            
+            List<UserAttributeRef> linkedUserAttributes = getLinkedUserAttr(appName);
+            
+            for (UserAttributeSource source : sources)
             {
-                UserAttributeSource source = (UserAttributeSource) iter.next();
-                Map sourceMap;
-
-                sourceMap = source.getUserAttributeMap(subject,
-                        linkedUserAttributes, context);
-                userInfoMap.putAll(sourceMap);
+                Map<String, String> sourceMap = source.getUserAttributeMap(subject,
linkedUserAttributes, context);
+                if (sourceMap != null)
+                {
+                    userInfoMap.putAll(sourceMap);
+                }
             }
-            return userInfoMap;
-        } catch (UserAttributeRetrievalException e)
+        } 
+        catch (UserAttributeRetrievalException e)
         {
             // Until external api is changed return
-            e.printStackTrace();            
+            log.error(e.getMessage(), e);          
             return null;
         }
+        return userInfoMap;
     }
 
     /**
      * @param sources
      *            The sources to set.
      */
-    public void setSources(List sources)
+    public void setSources(List<UserAttributeSource> sources)
     {
         this.sources = sources;
     }
@@ -103,7 +93,7 @@
      */
     public void setPortletRegistry(PortletRegistry portletRegistry)
     {
-        this.portletRegistry = portletRegistry;
+        registry = portletRegistry;
     }
 
 }

Modified: portals/jetspeed-2/portal/trunk/components/jetspeed-portal/src/main/java/org/apache/jetspeed/userinfo/impl/UserInfoManagerImpl.java
URL: http://svn.apache.org/viewvc/portals/jetspeed-2/portal/trunk/components/jetspeed-portal/src/main/java/org/apache/jetspeed/userinfo/impl/UserInfoManagerImpl.java?rev=887871&r1=887870&r2=887871&view=diff
==============================================================================
--- portals/jetspeed-2/portal/trunk/components/jetspeed-portal/src/main/java/org/apache/jetspeed/userinfo/impl/UserInfoManagerImpl.java
(original)
+++ portals/jetspeed-2/portal/trunk/components/jetspeed-portal/src/main/java/org/apache/jetspeed/userinfo/impl/UserInfoManagerImpl.java
Mon Dec  7 09:53:16 2009
@@ -17,10 +17,9 @@
 package org.apache.jetspeed.userinfo.impl;
 
 import java.security.Principal;
-import java.util.Collection;
 import java.util.Collections;
 import java.util.HashMap;
-import java.util.Iterator;
+import java.util.List;
 import java.util.Map;
 
 import javax.portlet.PortletRequest;
@@ -30,9 +29,10 @@
 import org.slf4j.LoggerFactory;
 import org.apache.jetspeed.components.portletregistry.PortletRegistry;
 import org.apache.jetspeed.om.portlet.PortletApplication;
+import org.apache.jetspeed.om.portlet.UserAttribute;
 import org.apache.jetspeed.om.portlet.UserAttributeRef;
 import org.apache.jetspeed.request.RequestContext;
-import org.apache.jetspeed.security.SecurityException;
+import org.apache.jetspeed.security.JetspeedPrincipal;
 import org.apache.jetspeed.security.SubjectHelper;
 import org.apache.jetspeed.security.User;
 import org.apache.jetspeed.security.UserManager;
@@ -53,19 +53,19 @@
     /** Logger */
     private static final Logger log = LoggerFactory.getLogger(UserInfoManagerImpl.class);
 
-    // TODO Same caching issue as usual. We should look into JCS. That wil do
-    // for now.
-    /** Map used to cache user info maps for each mapped portlet application. */
-    private static Map<String, Map<String, String>> userInfoMapCache;
+    // TODO: needs cache invalidation when portlet application user info configuration changes
+    /** Map to cache user info keys for each mapped portlet application. */
+    private static Map<String, List<UserAttributeRef>> appUserInfoAttrCache =
Collections.synchronizedMap(new HashMap<String,List<UserAttributeRef>>());
 
     /** The user manager */
-    UserManager userMgr;
+    protected UserManager userMgr;
 
     /** The portlet registry. */
-    PortletRegistry registry;
-
-    /** The object id of the portlet application being processed. */
-    String oid;
+    protected PortletRegistry registry;
+    
+    protected UserInfoManagerImpl()
+    {
+    }
 
     /**
      * <p>
@@ -79,7 +79,6 @@
     {
         this.userMgr = userMgr;
         this.registry = registry;
-        initUserInfoMapCache();
     }
 
     /**
@@ -97,36 +96,42 @@
     {
         this.userMgr = userMgr;
         this.registry = registry;
-        initUserInfoMapCache();
     }
 
     public Map<String, String> getUserInfoMap(String appName, RequestContext context)
     {
         if (log.isDebugEnabled())
-            log.debug("Getting user info for portlet application: " + oid.toString());
-
-        // Check if user info map is in cache.
-        if (userInfoMapCache.containsKey(appName))
-        {
-            return userInfoMapCache.get(appName);
-        }
-        // Not in cache, map user info.
+            log.debug("Getting user info for portlet application: " + appName);
+        
         Map<String, String> userInfo = getUserInformation(context);
-        if (null == userInfo)
+        if (null == userInfo || userInfo.isEmpty())
         {
-            log.debug(PortletRequest.USER_INFO + " is set to null");
+            log.debug(PortletRequest.USER_INFO + " is null or empty");
             return null;
         }
-
-        PortletApplication pa = registry.getPortletApplication(appName, true);
-        if (null == pa)
+        
+        return mapUserInfo(userInfo, getLinkedUserAttr(appName));
+    }
+    
+    protected List<UserAttributeRef> getLinkedUserAttr(String appName)
+    {
+        // Check if user info map is in cache.
+        List<UserAttributeRef> linkedUserAttr = appUserInfoAttrCache.get(appName);
+        
+        if (linkedUserAttr == null)
         {
-            log.debug(PortletRequest.USER_INFO + " is set to null");
-            return null;
+            PortletApplication pa = registry.getPortletApplication(appName, true);
+            if (null == pa)
+            {
+                log.debug(PortletRequest.USER_INFO + " is set to null");
+                return null;
+            }
+            List<UserAttribute> userAttributes = pa.getUserAttributes();
+            List<UserAttributeRef> userAttributeRefs = pa.getUserAttributeRefs();
+            linkedUserAttr = mapLinkedUserAttributes(userAttributes, userAttributeRefs);
+            appUserInfoAttrCache.put(appName, linkedUserAttr);
         }
-        Collection userAttributes = pa.getUserAttributes();
-        Collection userAttributeRefs = pa.getUserAttributeRefs();
-        return mapUserInfo(userInfo, userAttributes, userAttributeRefs);
+        return linkedUserAttr;
     }
 
     /**
@@ -141,40 +146,22 @@
      *            attributes reference.
      * @return The user info map.
      */
-    private Map<String, String> mapUserInfo(Map<String, String> userInfo, Collection
userAttributes, Collection userAttributeRefs)
+    protected Map<String, String> mapUserInfo(Map<String, String> userInfo, List<UserAttributeRef>
linkedUserAttributes)
     {
         Map<String, String>userInfoMap = new HashMap<String, String>();
-        if ((null == userAttributes) || (userAttributes.size() == 0))
+        for (UserAttributeRef currentAttributeRef : linkedUserAttributes)
         {
-            return null;
-        }
-        Collection linkedUserAttributes = mapLinkedUserAttributes(userAttributes, userAttributeRefs);
-        Iterator iter = linkedUserAttributes.iterator();
-        while (iter.hasNext())
-        {
-            UserAttributeRef currentAttributeRef = (UserAttributeRef) iter.next();
-            if (null != currentAttributeRef)
+            String key = currentAttributeRef.getNameLink();
+            String name = currentAttributeRef.getName();
+            if (key == null)
+            {                
+                key = name;
+            }
+            if (userInfo.containsKey(key))
             {
-                for (String key : userInfo.keySet())
-                {
-                    if (null != currentAttributeRef.getNameLink())
-                    {
-                        if ((currentAttributeRef.getNameLink()).equals(key))
-                        {
-                            userInfoMap.put(currentAttributeRef.getName(), userInfo.get(key));
-                        }
-                    }
-                    else
-                    {
-                        if ((currentAttributeRef.getName()).equals(key))
-                        {
-                            userInfoMap.put(currentAttributeRef.getName(), userInfo.get(key));
-                        }
-                    }
-                }
+                userInfoMap.put(name, userInfo.get(key));
             }
         }
-        userInfoMapCache.put(oid, userInfoMap);
         return userInfoMap;
     }
 
@@ -191,7 +178,7 @@
      */
     private Map<String, String> getUserInformation(RequestContext context)
     {
-        Map<String, String> userInfo = new HashMap<String, String>();
+        Map<String, String> userInfo = null;
         Subject subject = context.getSubject();
         if (null != subject)
         {
@@ -199,29 +186,12 @@
             if (null != userPrincipal)
             {
                 log.debug("Got user principal: " + userPrincipal.getName());
-                try
-                {
-                    if (userMgr.userExists(userPrincipal.getName()))
-                    {
-                        User user = userMgr.getUser(userPrincipal.getName());
-                        userInfo = user.getInfoMap();
-                    }
-                }
-                catch (SecurityException sex)
+                if (userPrincipal instanceof JetspeedPrincipal)
                 {
-                    log.warn("Unexpected SecurityException in UserInfoManager", sex);
+                    return ((JetspeedPrincipal)userPrincipal).getInfoMap();
                 }
             }
         }
         return userInfo;
     }
-
-    private void initUserInfoMapCache()
-    {
-        if (null == userInfoMapCache)
-        {
-            userInfoMapCache = Collections.synchronizedMap(new HashMap());
-        }
-    }
-
 }
\ No newline at end of file



---------------------------------------------------------------------
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