Author: bdaw
Date: 2007-03-12 20:03:58 -0400 (Mon, 12 Mar 2007)
New Revision: 6646
Added:
trunk/core/src/main/org/jboss/portal/core/controller/portlet/CachedUserImpl.java
Modified:
trunk/core/src/main/org/jboss/portal/core/aspects/server/UserInterceptor.java
trunk/core/src/main/org/jboss/portal/core/controller/portlet/ControllerUserContext.java
trunk/core/src/resources/portal-core-sar/META-INF/jboss-service.xml
Log:
- cache user principal in UserInterceptor
Modified: trunk/core/src/main/org/jboss/portal/core/aspects/server/UserInterceptor.java
===================================================================
---
trunk/core/src/main/org/jboss/portal/core/aspects/server/UserInterceptor.java 2007-03-12
20:19:07 UTC (rev 6645)
+++
trunk/core/src/main/org/jboss/portal/core/aspects/server/UserInterceptor.java 2007-03-13
00:03:58 UTC (rev 6646)
@@ -25,6 +25,7 @@
import org.jboss.logging.Logger;
import org.jboss.portal.common.invocation.InvocationException;
import org.jboss.portal.core.CoreConstants;
+import org.jboss.portal.core.controller.portlet.CachedUserImpl;
import org.jboss.portal.identity.NoSuchUserException;
import org.jboss.portal.identity.User;
import org.jboss.portal.identity.UserModule;
@@ -42,6 +43,7 @@
import java.io.Serializable;
import java.security.Principal;
import java.util.Date;
+import java.util.Map;
/**
* The interceptor is responsible for managing the user identity lifecycle based on the
principal name returned by the
@@ -62,6 +64,11 @@
/** User. */
protected UserModule userModule = null;
+ /** UserProfile */
+ protected UserProfileModule userProfileModule = null;
+
+ protected boolean cacheUser = true;
+
public UserModule getUserModule()
{
if (userModule == null)
@@ -78,6 +85,23 @@
return userModule;
}
+ public UserProfileModule getUserProfileModule()
+ {
+ if (userProfileModule == null)
+ {
+ try
+ {
+ userProfileModule = (UserProfileModule)new
InitialContext().lookup("java:portal/UserProfileModule");
+ }
+ catch (NamingException e)
+ {
+ log.error("could not obtain UserProfileModule: ", e);
+ }
+ }
+ return userProfileModule;
+ }
+
+
public void setUserModule(UserModule userModule)
{
this.userModule = userModule;
@@ -100,6 +124,8 @@
{
String userName = userPrincipal.getName();
+
+
//
try
{
@@ -108,9 +134,27 @@
log.trace("About to fetch user=" + userName);
}
- // Lookup
- user = getUserModule().findUserByUserName(userName);
+ if (isCacheUser())
+ {
+ //try to obtain cached user
+ user =
(User)invocation.getContext().getAttributeResolver(ServerInvocation.PRINCIPAL_SCOPE).getAttribute("cachedUser");
+ //create new cached one
+ if (user == null)
+ {
+ user = getUserModule().findUserByUserName(userName);
+ Map props = getUserProfileModule().getProperties(user);
+ user = new CachedUserImpl(user.getId(), user.getUserName(), props);
+
+ //cache user
+ invocation.getContext().setAttribute(ServerInvocation.PRINCIPAL_SCOPE,
"cachedUser", user);
+ }
+ }
+ else
+ {
+ // Lookup
+ user = getUserModule().findUserByUserName(userName);
+ }
//
if (trace)
{
@@ -194,6 +238,17 @@
}
}
+
+ public boolean isCacheUser()
+ {
+ return cacheUser;
+ }
+
+ public void setCacheUser(boolean cacheUser)
+ {
+ this.cacheUser = cacheUser;
+ }
+
/** Implement the last login date feature. */
private static class UserFinalizer implements Serializable,
HttpSessionBindingListener
{
@@ -244,7 +299,6 @@
UserModule userModule = null;
UserProfileModule userProfileModule = null;
- //TODO: identity - code change
try
{
userModule = (UserModule)new
InitialContext().lookup("java:portal/UserModule");
Added: trunk/core/src/main/org/jboss/portal/core/controller/portlet/CachedUserImpl.java
===================================================================
--- trunk/core/src/main/org/jboss/portal/core/controller/portlet/CachedUserImpl.java
(rev 0)
+++
trunk/core/src/main/org/jboss/portal/core/controller/portlet/CachedUserImpl.java 2007-03-13
00:03:58 UTC (rev 6646)
@@ -0,0 +1,82 @@
+/*
+* JBoss, a division of Red Hat
+* Copyright 2006, Red Hat Middleware, LLC, and individual contributors as indicated
+* by the @authors tag. See the copyright.txt in the distribution for a
+* full listing of individual contributors.
+*
+* This is free software; you can redistribute it and/or modify it
+* under the terms of the GNU Lesser General Public License as
+* published by the Free Software Foundation; either version 2.1 of
+* the License, or (at your option) any later version.
+*
+* This software is distributed in the hope that it will be useful,
+* but WITHOUT ANY WARRANTY; without even the implied warranty of
+* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
+* Lesser General Public License for more details.
+*
+* You should have received a copy of the GNU Lesser General Public
+* License along with this software; if not, write to the Free
+* Software Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA
+* 02110-1301 USA, or see the FSF site:
http://www.fsf.org.
+*/
+
+package org.jboss.portal.core.controller.portlet;
+
+import org.jboss.portal.identity.User;
+
+import java.util.Map;
+
+/**
+ * Simple POJO to cache user data
+ *
+ * @author <a href="mailto:boleslaw dot dawidowicz at redhat anotherdot
com">Boleslaw Dawidowicz</a>
+ * @version $Revision: 0.1 $
+ */
+public class CachedUserImpl implements User
+{
+ private static final org.jboss.logging.Logger log =
org.jboss.logging.Logger.getLogger(CachedUserImpl.class);
+
+ private Object id;
+
+ private String name;
+
+ private Map profile;
+
+ //restrict constructor
+ private CachedUserImpl()
+ {
+ }
+
+ public CachedUserImpl(Object id, String name, Map profile)
+ {
+ this.id = id;
+ this.name = name;
+ this.profile = profile;
+ }
+
+ public Object getId()
+ {
+ return id;
+ }
+
+ public String getUserName()
+ {
+ return name;
+ }
+
+ public void updatePassword(String password)
+ {
+ log.error("Cached user. Password cannot be updated using this object. Obtain
User using UserModule.");
+ }
+
+ public boolean validatePassword(String password)
+ {
+ log.error("Cached user. Password cannot be validated using this object. Obtain
User using UserModule.");
+ return false;
+ }
+
+ public Map getProfile()
+ {
+ return profile;
+ }
+}
Modified:
trunk/core/src/main/org/jboss/portal/core/controller/portlet/ControllerUserContext.java
===================================================================
---
trunk/core/src/main/org/jboss/portal/core/controller/portlet/ControllerUserContext.java 2007-03-12
20:19:07 UTC (rev 6645)
+++
trunk/core/src/main/org/jboss/portal/core/controller/portlet/ControllerUserContext.java 2007-03-13
00:03:58 UTC (rev 6646)
@@ -67,6 +67,14 @@
{
User user = getUser();
+ //check if user has been cached and obtain profile from it.
+ if (user instanceof CachedUserImpl)
+ {
+ return ((CachedUserImpl)user).getProfile();
+ }
+
+ //if user was not cached - obtain profile in traditional way.
+
UserProfileModule userProfileModule = null;
//MARK: identity code change
Modified: trunk/core/src/resources/portal-core-sar/META-INF/jboss-service.xml
===================================================================
--- trunk/core/src/resources/portal-core-sar/META-INF/jboss-service.xml 2007-03-12
20:19:07 UTC (rev 6645)
+++ trunk/core/src/resources/portal-core-sar/META-INF/jboss-service.xml 2007-03-13
00:03:58 UTC (rev 6646)
@@ -103,6 +103,7 @@
xmbean-dd=""
xmbean-code="org.jboss.portal.jems.as.system.JBossServiceModelMBean">
<xmbean/>
+ <!--<attribute name="cacheUser">true</attribute>-->
<depends>portal:service=Module,type=IdentityServiceController</depends>
</mbean>
<mbean