[jboss-cvs] JBossAS SVN: r57240 - projects/security/trunk/src/main/org/jboss/security/mapping/providers
jboss-cvs-commits at lists.jboss.org
jboss-cvs-commits at lists.jboss.org
Wed Sep 27 16:57:08 EDT 2006
Author: anil.saldhana at jboss.com
Date: 2006-09-27 16:57:08 -0400 (Wed, 27 Sep 2006)
New Revision: 57240
Added:
projects/security/trunk/src/main/org/jboss/security/mapping/providers/PrincipalToRoleMappingProvider.java
Log:
SECURITY-11:principal to role mapping provider
Added: projects/security/trunk/src/main/org/jboss/security/mapping/providers/PrincipalToRoleMappingProvider.java
===================================================================
--- projects/security/trunk/src/main/org/jboss/security/mapping/providers/PrincipalToRoleMappingProvider.java 2006-09-27 20:52:04 UTC (rev 57239)
+++ projects/security/trunk/src/main/org/jboss/security/mapping/providers/PrincipalToRoleMappingProvider.java 2006-09-27 20:57:08 UTC (rev 57240)
@@ -0,0 +1,123 @@
+/*
+ * JBoss, Home of Professional Open Source.
+ * Copyright 2006, Red Hat Middleware LLC, and individual contributors
+ * as indicated by the @author tags. See the copyright.txt file 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.security.mapping.providers;
+
+import java.lang.reflect.Constructor;
+import java.security.Principal;
+import java.security.acl.Group;
+import java.util.Map;
+import java.util.Properties;
+import java.util.Set;
+import java.util.StringTokenizer;
+
+import org.jboss.logging.Logger;
+import org.jboss.security.SecurityConstants;
+import org.jboss.security.mapping.MappingProvider;
+
+
+/**
+ * A principal to role mapping provider
+ * @author <a href="mailto:Anil.Saldhana at jboss.org">Anil Saldhana</a>
+ * @version $Revision$
+ * @since Sep 27, 2006
+ */
+public class PrincipalToRoleMappingProvider implements MappingProvider
+{
+ private static final Logger log = Logger.getLogger(PrincipalToRoleMappingProvider.class);
+
+ //Standard Strings
+ private static final String PRINCIPAL_ROLES_MAP = "principalRolesMap";
+
+ private Map options = null;
+
+ private Properties principalRoleMapProperties = null;
+
+ public void init(Map opt)
+ {
+ this.options = opt;
+ if(log.isTraceEnabled())
+ log.trace("Module Options="+options);
+ if(options != null)
+ {
+ if(options.containsKey(PRINCIPAL_ROLES_MAP))
+ {
+ principalRoleMapProperties = (Properties)options.get(PRINCIPAL_ROLES_MAP);
+ }
+ }
+ }
+
+ public Object performMapping(Map contextMap)
+ {
+ Group roles = (Group)contextMap.get(SecurityConstants.ROLES_IDENTIFIER);
+ if(roles == null)
+ throw new IllegalArgumentException("Group called Roles not found");
+
+ Set<Principal> principals = (Set<Principal>) contextMap.get(SecurityConstants.PRINCIPALS_IDENTIFIER);
+ for(Principal p:principals)
+ {
+ String pname = p.getName().trim();
+ boolean containsP = principalRoleMapProperties.containsKey(pname);
+ log.trace("Principal:"+pname+" is contained in options map:"+containsP);
+ if(!containsP) continue;
+ String commaSeparatedRoles = principalRoleMapProperties.getProperty(pname);
+ if(log.isTraceEnabled())
+ log.trace("Principal:"+p.getName()+":roles add="+commaSeparatedRoles);
+ String[] tokens = getRolesFromCommaSeparatedString(commaSeparatedRoles);
+ int len = tokens != null ? tokens.length : 0;
+ for(int i = 0; i < len; i++)
+ {
+ roles.addMember(instantiatePrincipal(p.getClass(),tokens[i]));
+ }
+ }
+ return roles;
+ }
+
+ private String[] getRolesFromCommaSeparatedString(String str)
+ {
+ if(str == null)
+ throw new IllegalArgumentException("str is null");
+ StringTokenizer st = new StringTokenizer(str,",");
+ int numTokens = st != null ? st.countTokens() : 0;
+ String[] tokens = new String[numTokens];
+ for(int i = 0; i < numTokens; i++)
+ {
+ tokens[i] = st.nextToken();
+ }
+ return tokens;
+ }
+
+ private Principal instantiatePrincipal(Class cls, String role)
+ {
+ Principal p = null;
+ try
+ {
+ Constructor ctr = cls.getConstructor(new Class[] {String.class});
+ p = (Principal)ctr.newInstance(new Object[]{role});
+ }
+ catch (Exception e)
+ {
+ if(log.isTraceEnabled())
+ log.trace("Encountered exception in role mapping:",e);
+ }
+ return p;
+ }
+}
More information about the jboss-cvs-commits
mailing list