Author: chris.laprun(a)jboss.com
Date: 2009-05-15 10:45:29 -0400 (Fri, 15 May 2009)
New Revision: 13377
Modified:
modules/identity/trunk/identity/src/main/java/org/jboss/portal/identity/RoleModule.java
modules/identity/trunk/identity/src/main/java/org/jboss/portal/identity/db/HibernateRoleModuleImpl.java
modules/identity/trunk/identity/src/main/java/org/jboss/portal/identity/ldap/LDAPRoleModuleImpl.java
Log:
- Added findRoleByDisplayName method (needed for JBPORTAL-2091).
Modified:
modules/identity/trunk/identity/src/main/java/org/jboss/portal/identity/RoleModule.java
===================================================================
---
modules/identity/trunk/identity/src/main/java/org/jboss/portal/identity/RoleModule.java 2009-05-15
12:10:05 UTC (rev 13376)
+++
modules/identity/trunk/identity/src/main/java/org/jboss/portal/identity/RoleModule.java 2009-05-15
14:45:29 UTC (rev 13377)
@@ -22,9 +22,6 @@
******************************************************************************/
package org.jboss.portal.identity;
-import org.jboss.portal.identity.IdentityException;
-import org.jboss.portal.identity.Role;
-
import java.util.Set;
/**
@@ -44,6 +41,14 @@
Role findRoleByName(String name) throws IdentityException, IllegalArgumentException;
/**
+ * Retrieves a role by its display name
+ *
+ * @param displayName the display name of the Role to be retrieved
+ * @return the role associated to the specified display name
+ */
+ Role findRoleByDisplayName(String displayName) throws IdentityException,
IllegalArgumentException;
+
+ /**
* Retrieve a collection of role from the role names.
*
* @param names the role names
@@ -97,7 +102,7 @@
* @return the roles
*/
Set findRoles() throws IdentityException;
-
+
/**
* Get all the administrator roles
*
Modified:
modules/identity/trunk/identity/src/main/java/org/jboss/portal/identity/db/HibernateRoleModuleImpl.java
===================================================================
---
modules/identity/trunk/identity/src/main/java/org/jboss/portal/identity/db/HibernateRoleModuleImpl.java 2009-05-15
12:10:05 UTC (rev 13376)
+++
modules/identity/trunk/identity/src/main/java/org/jboss/portal/identity/db/HibernateRoleModuleImpl.java 2009-05-15
14:45:29 UTC (rev 13377)
@@ -21,25 +21,23 @@
*/
package org.jboss.portal.identity.db;
-import org.jboss.portal.identity.Role;
-import org.jboss.portal.identity.IdentityException;
-import org.jboss.portal.identity.db.HibernateRoleImpl;
-import org.jboss.portal.identity.User;
-import org.jboss.portal.identity.db.HibernateUserImpl;
-import org.jboss.portal.common.util.Tools;
-import org.jboss.portal.identity.service.RoleModuleService;
-import org.hibernate.SessionFactory;
-import org.hibernate.Session;
import org.hibernate.Criteria;
import org.hibernate.HibernateException;
import org.hibernate.Query;
+import org.hibernate.Session;
+import org.hibernate.SessionFactory;
import org.hibernate.criterion.Restrictions;
+import org.jboss.portal.common.util.Tools;
+import org.jboss.portal.identity.IdentityException;
+import org.jboss.portal.identity.Role;
+import org.jboss.portal.identity.User;
+import org.jboss.portal.identity.service.RoleModuleService;
import javax.naming.InitialContext;
+import java.util.Collections;
+import java.util.HashSet;
+import java.util.Iterator;
import java.util.Set;
-import java.util.Iterator;
-import java.util.HashSet;
-import java.util.Collections;
/**
* @author <a href="mailto:julien@jboss.org">Julien Viet </a>
@@ -52,44 +50,44 @@
{
/** . */
- private static final org.jboss.logging.Logger log =
org.jboss.logging.Logger.getLogger(HibernateRoleModuleImpl.class);
- /** . */
- protected SessionFactory sessionFactory;
+ private static final org.jboss.logging.Logger log =
org.jboss.logging.Logger.getLogger(HibernateRoleModuleImpl.class);
+ /** . */
+ protected SessionFactory sessionFactory;
- /** . */
- protected String sessionFactoryJNDIName;
+ /** . */
+ protected String sessionFactoryJNDIName;
- public void start() throws Exception
- {
- //
- sessionFactory = (SessionFactory)new
InitialContext().lookup(sessionFactoryJNDIName);
+ public void start() throws Exception
+ {
+ //
+ sessionFactory = (SessionFactory)new
InitialContext().lookup(sessionFactoryJNDIName);
- super.start();
- }
+ super.start();
+ }
- public void stop() throws Exception
- {
+ public void stop() throws Exception
+ {
- //
- sessionFactory = null;
+ //
+ sessionFactory = null;
- super.stop();
- }
+ super.stop();
+ }
// public SessionFactory getSessionFactory()
// {
// return sessionFactory;
// }
- public String getSessionFactoryJNDIName()
- {
- return sessionFactoryJNDIName;
- }
+ public String getSessionFactoryJNDIName()
+ {
+ return sessionFactoryJNDIName;
+ }
- public void setSessionFactoryJNDIName(String sessionFactoryJNDIName)
- {
- this.sessionFactoryJNDIName = sessionFactoryJNDIName;
- }
+ public void setSessionFactoryJNDIName(String sessionFactoryJNDIName)
+ {
+ this.sessionFactoryJNDIName = sessionFactoryJNDIName;
+ }
public Role findRoleByName(String name) throws IdentityException
@@ -122,6 +120,36 @@
}
}
+ public Role findRoleByDisplayName(String displayName) throws IdentityException
+ {
+ if (displayName != null)
+ {
+ try
+ {
+ Session session = getCurrentSession();
+ Criteria criteria = session.createCriteria(HibernateRoleImpl.class);
+ criteria.add(Restrictions.eq("displayName", displayName));
+ criteria.setCacheable(true);
+ HibernateRoleImpl role = (HibernateRoleImpl)criteria.uniqueResult();
+ if (role == null)
+ {
+ throw new IdentityException("No such role " + displayName);
+ }
+ return role;
+ }
+ catch (HibernateException e)
+ {
+ String message = "Cannot find role by display name " +
displayName;
+ log.error(message, e);
+ throw new IdentityException(message, e);
+ }
+ }
+ else
+ {
+ throw new IllegalArgumentException("name cannot be null");
+ }
+ }
+
public Set findRolesByNames(String[] names) throws IdentityException
{
if (names != null)
Modified:
modules/identity/trunk/identity/src/main/java/org/jboss/portal/identity/ldap/LDAPRoleModuleImpl.java
===================================================================
---
modules/identity/trunk/identity/src/main/java/org/jboss/portal/identity/ldap/LDAPRoleModuleImpl.java 2009-05-15
12:10:05 UTC (rev 13376)
+++
modules/identity/trunk/identity/src/main/java/org/jboss/portal/identity/ldap/LDAPRoleModuleImpl.java 2009-05-15
14:45:29 UTC (rev 13377)
@@ -22,12 +22,10 @@
******************************************************************************/
package org.jboss.portal.identity.ldap;
-import java.util.HashSet;
-import java.util.Iterator;
-import java.util.List;
-import java.util.Map;
-import java.util.NoSuchElementException;
-import java.util.Set;
+import org.jboss.portal.common.util.Tools;
+import org.jboss.portal.identity.IdentityConfiguration;
+import org.jboss.portal.identity.IdentityException;
+import org.jboss.portal.identity.Role;
import javax.naming.NamingEnumeration;
import javax.naming.NamingException;
@@ -39,12 +37,13 @@
import javax.naming.directory.SearchControls;
import javax.naming.directory.SearchResult;
import javax.naming.ldap.LdapContext;
+import java.util.HashSet;
+import java.util.Iterator;
+import java.util.List;
+import java.util.Map;
+import java.util.NoSuchElementException;
+import java.util.Set;
-import org.jboss.portal.common.util.Tools;
-import org.jboss.portal.identity.IdentityConfiguration;
-import org.jboss.portal.identity.IdentityException;
-import org.jboss.portal.identity.Role;
-
/**
* @author <a href="mailto:boleslaw.dawidowicz@jboss.org">Boleslaw
Dawidowicz</a>
* @version $Revision: 1.1 $
@@ -79,8 +78,8 @@
throw new IdentityException("No such role " + name);
}
SearchResult res = (SearchResult)sr.iterator().next();
- DirContext ctx = (DirContext)res.getObject();
- return createRoleInstance(res.getAttributes(),ctx.getNameInNamespace());
+ DirContext ctx = (DirContext)res.getObject();
+ return createRoleInstance(res.getAttributes(), ctx.getNameInNamespace());
}
catch (NoSuchElementException e)
@@ -94,6 +93,47 @@
throw new IdentityException("No role found with name: " + name);
}
+ public Role findRoleByDisplayName(String displayName) throws IdentityException,
IllegalArgumentException
+ {
+ try
+ {
+ log.debug("findRoleByDisplayName(): display name = " + displayName);
+
+ if (displayName == null)
+ {
+ throw new IdentityException("Role name canot be null");
+ }
+
+
+ String filter =
"(".concat(getDisplayNameAttributeID()).concat("=").concat(displayName).concat(")");
+ log.debug("Search filter: " + filter);
+
+ List sr = searchRoles(filter, null);
+ if (sr.size() > 1)
+ {
+ throw new IdentityException("Found more than one role with display name:
" + displayName + "" +
+ "Posible data inconsistency");
+ }
+ if (sr.size() == 0)
+ {
+ throw new IdentityException("No such role " + displayName);
+ }
+ SearchResult res = (SearchResult)sr.iterator().next();
+ DirContext ctx = (DirContext)res.getObject();
+ return createRoleInstance(res.getAttributes(), ctx.getNameInNamespace());
+
+ }
+ catch (NoSuchElementException e)
+ {
+ log.debug("No role found with display name: " + displayName, e);
+ }
+ catch (NamingException e)
+ {
+ throw new IdentityException("Role search failed.", e);
+ }
+ throw new IdentityException("No role found with display name: " +
displayName);
+ }
+
public Set findRolesByNames(String[] names) throws IdentityException,
IllegalArgumentException
{
if (names == null)
@@ -110,11 +150,11 @@
{
String name = names[i];
filter.append("(")
- .append(getRidAttributeID())
- .append("=")
- .append(name)
- .append(") ");
- }
+ .append(getRidAttributeID())
+ .append("=")
+ .append(name)
+ .append(") ");
+ }
filter.append(")");
List sr = searchRoles(filter.toString(), null);
@@ -122,8 +162,8 @@
for (Iterator iterator = sr.iterator(); iterator.hasNext();)
{
SearchResult res = (SearchResult)iterator.next();
- DirContext ctx = (DirContext)res.getObject();
- roles.add(createRoleInstance(res.getAttributes(),ctx.getNameInNamespace()));
+ DirContext ctx = (DirContext)res.getObject();
+ roles.add(createRoleInstance(res.getAttributes(),
ctx.getNameInNamespace()));
}
}
catch (Exception e)
@@ -222,7 +262,7 @@
}
}
- Role resultRole = findRoleByName(name);
+ Role resultRole = findRoleByName(name);
fireRoleCreatedEvent(resultRole.getId(), resultRole.getName());
@@ -308,8 +348,8 @@
while (iter.hasNext())
{
SearchResult res = (SearchResult)iter.next();
- DirContext ctx = (DirContext)res.getObject();
- rf.add(createRoleInstance(res.getAttributes(),ctx.getNameInNamespace()));
+ DirContext ctx = (DirContext)res.getObject();
+ rf.add(createRoleInstance(res.getAttributes(), ctx.getNameInNamespace()));
}
}
catch (NoSuchElementException e)
@@ -324,8 +364,8 @@
}
/**
- * This method should be used by over modules to perform searches. It will allow role
module
- * implementation to apply proper filter and search scope from the configuration
+ * This method should be used by over modules to perform searches. It will allow role
module implementation to apply
+ * proper filter and search scope from the configuration
*
* @param filter that will be concatenated with proper role search filter from the
module
* @return
@@ -377,5 +417,4 @@
}
-
}