[Jboss-cvs] JBossAS SVN: r56352 - in trunk/security/src/main/org/jboss/security: . mapping mapping/config mapping/providers
jboss-cvs-commits at lists.jboss.org
jboss-cvs-commits at lists.jboss.org
Mon Aug 28 16:45:51 EDT 2006
Author: anil.saldhana at jboss.com
Date: 2006-08-28 16:45:50 -0400 (Mon, 28 Aug 2006)
New Revision: 56352
Added:
trunk/security/src/main/org/jboss/security/mapping/
trunk/security/src/main/org/jboss/security/mapping/MappingContext.java
trunk/security/src/main/org/jboss/security/mapping/MappingProvider.java
trunk/security/src/main/org/jboss/security/mapping/config/
trunk/security/src/main/org/jboss/security/mapping/config/MappingConfigEntryHolder.java
trunk/security/src/main/org/jboss/security/mapping/config/MappingModuleEntry.java
trunk/security/src/main/org/jboss/security/mapping/config/RoleMappingConfigContainer.java
trunk/security/src/main/org/jboss/security/mapping/providers/
trunk/security/src/main/org/jboss/security/mapping/providers/OptionsRoleMappingProvider.java
Log:
JBAS-3577: Role Mapping + Mapping Framework
Added: trunk/security/src/main/org/jboss/security/mapping/MappingContext.java
===================================================================
--- trunk/security/src/main/org/jboss/security/mapping/MappingContext.java 2006-08-28 19:07:23 UTC (rev 56351)
+++ trunk/security/src/main/org/jboss/security/mapping/MappingContext.java 2006-08-28 20:45:50 UTC (rev 56352)
@@ -0,0 +1,46 @@
+/*
+ * JBoss, the OpenSource J2EE webOS
+ *
+ * Distributable under LGPL license.
+ * See terms of license at gnu.org.
+ */
+package org.jboss.security.mapping;
+
+import java.util.ArrayList;
+import java.util.List;
+import java.util.Map;
+
+/**
+ * Generic Context used by the Mapping Framework
+ * @author <a href="mailto:Anil.Saldhana at jboss.org">Anil Saldhana</a>
+ * @version $Revision$
+ * @since Aug 24, 2006
+ */
+public class MappingContext
+{
+ private List modules = new ArrayList();
+
+ public MappingContext(List mod)
+ {
+ this.modules = mod;
+ }
+
+ /**
+ * Apply mapping semantics on the passed object
+ * @param obj Generic Object
+ * @return Mapped Object
+ */
+ public Object performMapping(Map obj)
+ {
+ int len = modules.size();
+ Object returnObj = null;
+
+ for(int i = 0 ; i < len; i++)
+ {
+ MappingProvider mp = (MappingProvider)modules.get(i);
+ returnObj = mp.performMapping(obj);
+ }
+
+ return returnObj;
+ }
+}
Added: trunk/security/src/main/org/jboss/security/mapping/MappingProvider.java
===================================================================
--- trunk/security/src/main/org/jboss/security/mapping/MappingProvider.java 2006-08-28 19:07:23 UTC (rev 56351)
+++ trunk/security/src/main/org/jboss/security/mapping/MappingProvider.java 2006-08-28 20:45:50 UTC (rev 56352)
@@ -0,0 +1,31 @@
+/*
+ * JBoss, the OpenSource J2EE webOS
+ *
+ * Distributable under LGPL license.
+ * See terms of license at gnu.org.
+ */
+package org.jboss.security.mapping;
+
+import java.util.Map;
+
+/**
+ * A provider with mapping functionality
+ * @author <a href="mailto:Anil.Saldhana at jboss.org">Anil Saldhana</a>
+ * @version $Revision$
+ * @since Aug 24, 2006
+ */
+public interface MappingProvider
+{
+ /**
+ * Initialize the provider with the configured module options
+ * @param options
+ */
+ void init(Map options);
+
+ /**
+ * Map the passed object
+ * @param map A contextual map that can provide information to the provider
+ * @return mapped result
+ */
+ Object performMapping(Map map);
+}
Added: trunk/security/src/main/org/jboss/security/mapping/config/MappingConfigEntryHolder.java
===================================================================
--- trunk/security/src/main/org/jboss/security/mapping/config/MappingConfigEntryHolder.java 2006-08-28 19:07:23 UTC (rev 56351)
+++ trunk/security/src/main/org/jboss/security/mapping/config/MappingConfigEntryHolder.java 2006-08-28 20:45:50 UTC (rev 56352)
@@ -0,0 +1,79 @@
+/*
+ * JBoss, Home of Professional Open Source
+ * Copyright 2005, JBoss Inc., 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.security.mapping.config;
+
+import java.util.HashMap;
+import java.util.Map;
+
+import javax.xml.namespace.QName;
+
+import org.jboss.security.auth.login.ModuleOption;
+import org.jboss.xb.binding.GenericValueContainer;
+
+//$Id: MappingConfigEntryHolder.java 46201 2006-07-11 17:51:23Z asaldhana $
+
+/**
+ * A container for creating MappingConfigEntry during jbxb parse.
+ * @author <a href="mailto:Anil.Saldhana at jboss.org">Anil Saldhana</a>
+ * @since August 26, 2006
+ * @version $Revision: 46201 $
+ */
+public class MappingConfigEntryHolder implements GenericValueContainer
+{
+ private Map moduleOptions = new HashMap();
+ String moduleName = null;
+
+ public void addChild(QName name, Object value)
+ {
+ if("code".equals(name.getLocalPart()))
+ {
+ moduleName = (String)value;
+ }
+ if(value instanceof ModuleOption)
+ {
+ ModuleOption mo = (ModuleOption)value;
+ moduleOptions.put(mo.getName(),mo.getValue());
+ }
+ }
+
+ public void addOption(ModuleOption option)
+ {
+ moduleOptions.put(option.getName(), option.getValue());
+ }
+
+ public MappingModuleEntry getEntry()
+ {
+ return (MappingModuleEntry)instantiate();
+ }
+
+ public Object instantiate()
+ {
+ MappingModuleEntry entry = new MappingModuleEntry( moduleName,moduleOptions );
+ return entry;
+ }
+
+ public Class getTargetClass()
+ {
+ return MappingModuleEntry.class;
+ }
+
+}
Added: trunk/security/src/main/org/jboss/security/mapping/config/MappingModuleEntry.java
===================================================================
--- trunk/security/src/main/org/jboss/security/mapping/config/MappingModuleEntry.java 2006-08-28 19:07:23 UTC (rev 56351)
+++ trunk/security/src/main/org/jboss/security/mapping/config/MappingModuleEntry.java 2006-08-28 20:45:50 UTC (rev 56352)
@@ -0,0 +1,88 @@
+/*
+ * JBoss, Home of Professional Open Source
+ * Copyright 2005, JBoss Inc., 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.security.mapping.config;
+
+import java.util.HashMap;
+import java.util.Map;
+
+import org.jboss.security.auth.login.ModuleOption;
+
+//$Id: MappingModuleEntry.java 45985 2006-06-29 20:56:57Z asaldhana $
+
+/**
+ * Represents configuration for a single Mapping Module
+ * @author <a href="mailto:Anil.Saldhana at jboss.org">Anil Saldhana</a>
+ * @since August 24, 2006
+ * @version $Revision: 45985 $
+ */
+public class MappingModuleEntry
+{
+ private String mappingModuleName;
+ private Map options = null;
+
+ /**
+ * Create a new MappingModuleEntry.
+ *
+ * @param name Policy Module Name
+ */
+ public MappingModuleEntry(String name)
+ {
+ this.mappingModuleName = name;
+ }
+
+ /**
+ * Create a new MappingModuleEntry.
+ *
+ * @param name Policy Module Name
+ * @param options Options
+ */
+ public MappingModuleEntry(String name, Map options)
+ {
+ this.mappingModuleName = name;
+ this.options = options;
+ }
+
+ public void add(ModuleOption option)
+ {
+ if(options == null)
+ options = new HashMap();
+ options.put(option.getName(), option.getValue());
+ }
+
+ /**
+ * Get the Policy Module Name
+ * @return
+ */
+ public String getMappingModuleName()
+ {
+ return mappingModuleName;
+ }
+
+ /**
+ * Get the options
+ * @return
+ */
+ public Map getOptions()
+ {
+ return options;
+ }
+}
Added: trunk/security/src/main/org/jboss/security/mapping/config/RoleMappingConfigContainer.java
===================================================================
--- trunk/security/src/main/org/jboss/security/mapping/config/RoleMappingConfigContainer.java 2006-08-28 19:07:23 UTC (rev 56351)
+++ trunk/security/src/main/org/jboss/security/mapping/config/RoleMappingConfigContainer.java 2006-08-28 20:45:50 UTC (rev 56352)
@@ -0,0 +1,83 @@
+/*
+* JBoss, Home of Professional Open Source
+* Copyright 2005, JBoss Inc., 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.security.mapping.config;
+
+import java.util.ArrayList;
+import java.util.List;
+
+import javax.xml.namespace.QName;
+
+import org.jboss.logging.Logger;
+import org.jboss.security.config.MappingInfo;
+import org.jboss.security.config.RoleMappingInfo;
+import org.jboss.xb.binding.GenericValueContainer;
+
+//$Id: RoleMappingConfigContainer.java 45942 2006-06-28 02:14:46Z asaldhana $
+
+/**
+ * A container for creating RoleMappingConfig during jbxb parse.
+ *
+ * @author Anil.Saldhana at jboss.org
+ * @version $Revision: 45942 $
+ */
+public class RoleMappingConfigContainer
+ implements GenericValueContainer
+{
+ private static Logger log = Logger.getLogger(RoleMappingConfigContainer.class);
+
+ private List moduleEntries = new ArrayList();
+
+ /**
+ * @see GenericValueContainer#addChild(javax.xml.namespace.QName, java.lang.Object)
+ */
+ public void addChild(QName name, Object value)
+ {
+ if(log.isTraceEnabled())
+ log.trace("addChild:Qname="+name+":value="+value);
+ if(value instanceof MappingModuleEntry)
+ {
+ this.moduleEntries.add(value);
+ }
+ }
+
+ /**
+ * @see GenericValueContainer#instantiate()
+ */
+ public Object instantiate()
+ {
+ /**
+ * Currently we do not have the name of the application policy
+ * This will be rectified in the ApplicationPolicyContainer
+ */
+ MappingInfo ri = new RoleMappingInfo("dummy");
+ ri.add(moduleEntries);
+ return ri;
+ }
+
+ /**
+ * @see GenericValueContainer#getTargetClass()
+ */
+ public Class getTargetClass()
+ {
+ return RoleMappingInfo.class;
+ }
+}
Added: trunk/security/src/main/org/jboss/security/mapping/providers/OptionsRoleMappingProvider.java
===================================================================
--- trunk/security/src/main/org/jboss/security/mapping/providers/OptionsRoleMappingProvider.java 2006-08-28 19:07:23 UTC (rev 56351)
+++ trunk/security/src/main/org/jboss/security/mapping/providers/OptionsRoleMappingProvider.java 2006-08-28 20:45:50 UTC (rev 56352)
@@ -0,0 +1,117 @@
+/*
+ * JBoss, the OpenSource J2EE webOS
+ *
+ * Distributable under LGPL license.
+ * See terms of license at gnu.org.
+ */
+package org.jboss.security.mapping.providers;
+
+import java.lang.reflect.Constructor;
+import java.security.Principal;
+import java.security.acl.Group;
+import java.util.Enumeration;
+import java.util.Map;
+import java.util.Properties;
+import java.util.StringTokenizer;
+
+import org.jboss.logging.Logger;
+import org.jboss.security.SecurityConstants;
+import org.jboss.security.mapping.MappingProvider;
+
+
+/**
+ * Role Mapping Provider that picks up the roles from the
+ * options and then appends them to the passed Group
+ * @author <a href="mailto:Anil.Saldhana at jboss.org">Anil Saldhana</a>
+ * @version $Revision$
+ * @since Aug 24, 2006
+ */
+public class OptionsRoleMappingProvider implements MappingProvider
+{
+ private static final Logger log = Logger.getLogger(OptionsRoleMappingProvider.class);
+
+ //Standard Strings
+ private static final String REPLACE_ROLES_STRING = "replaceRoles";
+ private static final String ROLES_MAP = "rolesMap";
+
+ private Map options = null;
+
+ private Properties roleMapProperties = null;
+
+ /**
+ * Specifies
+ */
+ private boolean REPLACE_ROLES = false;
+
+ public void init(Map opt)
+ {
+ this.options = opt;
+ if(options != null)
+ {
+ if(options.containsKey(REPLACE_ROLES_STRING))
+ {
+ REPLACE_ROLES = "true".equalsIgnoreCase((String)options.get(REPLACE_ROLES_STRING));
+ }
+ if(options.containsKey(ROLES_MAP))
+ {
+ roleMapProperties = (Properties)options.get(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");
+
+ //Enumerate over the members
+ Enumeration enumer = roles.members();
+ while(enumer.hasMoreElements())
+ {
+ Principal p = (Principal)enumer.nextElement();
+ String name = p.getName();
+ String commaSeparatedRoles = roleMapProperties.getProperty(name);
+ if(commaSeparatedRoles != null)
+ {
+ String[] tokens = getRolesFromCommaSeparatedString(commaSeparatedRoles);
+ int len = tokens != null ? tokens.length : 0;
+ for(int i = 0; i < len; i++)
+ {
+ if(this.REPLACE_ROLES)
+ roles.removeMember(p);
+ roles.addMember(instantiatePrincipal(p.getClass(),tokens[i]));
+ }
+ }
+ }
+ return roles;
+ }
+
+ private String[] getRolesFromCommaSeparatedString(String str)
+ {
+ 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