[jboss-cvs] JBossAS SVN: r67237 - in projects/security/security-jboss-sx/trunk/identity/src/main/org/jboss/security: identity/plugins and 1 other directory.

jboss-cvs-commits at lists.jboss.org jboss-cvs-commits at lists.jboss.org
Sun Nov 18 19:34:47 EST 2007


Author: anil.saldhana at jboss.com
Date: 2007-11-18 19:34:47 -0500 (Sun, 18 Nov 2007)
New Revision: 67237

Added:
   projects/security/security-jboss-sx/trunk/identity/src/main/org/jboss/security/identity/plugins/IdentityFactory.java
   projects/security/security-jboss-sx/trunk/identity/src/main/org/jboss/security/identity/plugins/SecurityActions.java
Removed:
   projects/security/security-jboss-sx/trunk/identity/src/main/org/jboss/security/AnybodyPrincipal.java
   projects/security/security-jboss-sx/trunk/identity/src/main/org/jboss/security/NobodyPrincipal.java
   projects/security/security-jboss-sx/trunk/identity/src/main/org/jboss/security/SimpleGroup.java
   projects/security/security-jboss-sx/trunk/identity/src/main/org/jboss/security/SimplePrincipal.java
Modified:
   projects/security/security-jboss-sx/trunk/identity/src/main/org/jboss/security/identity/plugins/SimpleIdentity.java
Log:
introduce a factory to get rid of implementation details on principal and group

Deleted: projects/security/security-jboss-sx/trunk/identity/src/main/org/jboss/security/AnybodyPrincipal.java
===================================================================
--- projects/security/security-jboss-sx/trunk/identity/src/main/org/jboss/security/AnybodyPrincipal.java	2007-11-18 22:44:33 UTC (rev 67236)
+++ projects/security/security-jboss-sx/trunk/identity/src/main/org/jboss/security/AnybodyPrincipal.java	2007-11-19 00:34:47 UTC (rev 67237)
@@ -1,82 +0,0 @@
-/*
-* 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;
-
-import java.security.Principal;
-
-/** An implementation of Principal and Comparable that represents any role.
-Any Principal or name of a Principal when compared to an AnybodyPrincipal
-using {@link #equals(Object) equals} or {@link #compareTo(Object) compareTo} 
-will always be found equals to the AnybodyPrincipal.
-
-Note that this class is not likely to operate correctly in a collection
-since the hashCode() and equals() methods are not correlated.
-
- at author Scott.Stark at jboss.org
- at version $Revision: 40069 $
-*/
-public class AnybodyPrincipal implements Comparable, Principal
-{
-    public static final String ANYBODY = "<ANYBODY>";
-    public static final AnybodyPrincipal ANYBODY_PRINCIPAL = new AnybodyPrincipal();
-
-    public int hashCode()
-    {
-        return ANYBODY.hashCode();
-    }
-
-    /**
-    @return "<ANYBODY>"
-    */
-    public String getName()
-    {
-        return ANYBODY;
-    }
-
-    public String toString()
-    {
-        return ANYBODY;
-    }
-    
-    /** This method always returns 0 to indicate equality for any argument.
-    This is only meaningful when comparing against other Principal objects
-     or names of Principals.
-
-    @return true to indicate equality for any argument.
-    */
-    public boolean equals(Object another)
-    {
-        return true;
-    }
-
-    /** This method always returns 0 to indicate equality for any argument.
-    This is only meaningful when comparing against other Principal objects
-     or names of Principals.
-
-    @return 0 to indicate equality for any argument.
-    */
-    public int compareTo(Object o)
-    {
-        return 0;
-    }
-
-}

Deleted: projects/security/security-jboss-sx/trunk/identity/src/main/org/jboss/security/NobodyPrincipal.java
===================================================================
--- projects/security/security-jboss-sx/trunk/identity/src/main/org/jboss/security/NobodyPrincipal.java	2007-11-18 22:44:33 UTC (rev 67236)
+++ projects/security/security-jboss-sx/trunk/identity/src/main/org/jboss/security/NobodyPrincipal.java	2007-11-19 00:34:47 UTC (rev 67237)
@@ -1,82 +0,0 @@
-/*
-* 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;
-
-import java.security.Principal;
-
-/** An implementation of Principal and Comparable that represents no role.
-Any Principal or name of a Principal when compared to an NobodyPrincipal
-using {@link #equals(Object) equals} or {@link #compareTo(Object) compareTo} 
-will always be found not equal to the NobodyPrincipal.
-
-Note that this class is not likely to operate correctly in a collection
-since the hashCode() and equals() methods are not correlated.
-
- at author Scott.Stark at jboss.org
- at version $Revision: 40069 $
-*/
-public class NobodyPrincipal implements Comparable, Principal
-{
-    public static final String NOBODY = "<NOBODY>";
-    public static final NobodyPrincipal NOBODY_PRINCIPAL = new NobodyPrincipal();
-
-    public int hashCode()
-    {
-        return NOBODY.hashCode();
-    }
-
-    /**
-    @return "<NOBODY>"
-    */
-    public String getName()
-    {
-        return NOBODY;
-    }
-
-    public String toString()
-    {
-        return NOBODY;
-    }
-    
-    /** This method always returns 0 to indicate equality for any argument.
-    This is only meaningful when comparing against other Principal objects
-     or names of Principals.
-
-    @return false to indicate inequality for any argument.
-    */
-    public boolean equals(Object another)
-    {
-        return false;
-    }
-
-    /** This method always returns 1 to indicate inequality for any argument.
-    This is only meaningful when comparing against other Principal objects
-     or names of Principals.
-
-    @return 1 to indicate inequality for any argument.
-    */
-    public int compareTo(Object o)
-    {
-        return 1;
-    }
-
-}

Deleted: projects/security/security-jboss-sx/trunk/identity/src/main/org/jboss/security/SimpleGroup.java
===================================================================
--- projects/security/security-jboss-sx/trunk/identity/src/main/org/jboss/security/SimpleGroup.java	2007-11-18 22:44:33 UTC (rev 67236)
+++ projects/security/security-jboss-sx/trunk/identity/src/main/org/jboss/security/SimpleGroup.java	2007-11-19 00:34:47 UTC (rev 67237)
@@ -1,147 +0,0 @@
-/*
-* 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;
-
-import java.security.Principal;
-import java.security.acl.Group;
-import java.util.Collection;
-import java.util.Collections;
-import java.util.Enumeration;
-import java.util.Iterator;
-import java.util.HashMap;
-
-/** An implementation of Group that manages a collection of Principal
-objects based on their hashCode() and equals() methods. This class
-is not thread safe.
-
- at author Scott.Stark at jboss.org
- at version $Revision: 67182 $
-*/
-public class SimpleGroup extends SimplePrincipal implements Group, Cloneable
-{
-    /** The serialVersionUID */
-   private static final long serialVersionUID = 6051859639378507247L;
-   
-   private HashMap members;
-
-    public SimpleGroup(String groupName)
-    {
-        super(groupName);
-        members = new HashMap(3);
-    }
-
-    /** Adds the specified member to the group.
-     @param user the principal to add to this group.
-     @return true if the member was successfully added,
-         false if the principal was already a member.
-     */
-    public boolean addMember(Principal user)
-    {
-        boolean isMember = members.containsKey(user);
-        if( isMember == false )
-            members.put(user, user);
-        return isMember == false;
-    }
-    /** Returns true if the passed principal is a member of the group.
-        This method does a recursive search, so if a principal belongs to a
-        group which is a member of this group, true is returned.
-
-        A special check is made to see if the member is an instance of
-        org.jboss.security.AnybodyPrincipal or org.jboss.security.NobodyPrincipal
-        since these classes do not hash to meaningful values.
-    @param member the principal whose membership is to be checked.
-    @return true if the principal is a member of this group,
-        false otherwise.
-    */
-    public boolean isMember(Principal member)
-    {
-        // First see if there is a key with the member name
-        boolean isMember = members.containsKey(member);
-        if( isMember == false )
-        {   // Check the AnybodyPrincipal & NobodyPrincipal special cases
-            isMember = (member instanceof org.jboss.security.AnybodyPrincipal);
-            if( isMember == false )
-            {
-                if( member instanceof org.jboss.security.NobodyPrincipal )
-                return false;
-            }
-        }
-        if( isMember == false )
-        {   // Check any Groups for membership
-            Collection values = members.values();
-            Iterator iter = values.iterator();
-            while( isMember == false && iter.hasNext() )
-            {
-                Object next = iter.next();
-                if( next instanceof Group )
-                {
-                    Group group = (Group) next;
-                    isMember = group.isMember(member);
-                }
-            }
-        }
-        return isMember;
-    }
-
-    /** Returns an enumeration of the members in the group.
-        The returned objects can be instances of either Principal
-        or Group (which is a subinterface of Principal).
-    @return an enumeration of the group members.
-    */
-    public Enumeration<Principal> members()
-    {
-        return Collections.enumeration(members.values());
-    }
-
-    /** Removes the specified member from the group.
-    @param user the principal to remove from this group.
-    @return true if the principal was removed, or
-        false if the principal was not a member.
-    */
-    public boolean removeMember(Principal user)
-    {
-        Object prev = members.remove(user);
-        return prev != null;
-    }
-
-   public String toString()
-   {
-      StringBuffer tmp = new StringBuffer(getName());
-      tmp.append("(members:");
-      Iterator iter = members.keySet().iterator();
-      while( iter.hasNext() )
-      {
-         tmp.append(iter.next());
-         tmp.append(',');
-      }
-      tmp.setCharAt(tmp.length()-1, ')');
-      return tmp.toString();
-   }
-   
-   public synchronized Object clone() throws CloneNotSupportedException  
-   {  
-      SimpleGroup clone = (SimpleGroup) super.clone();  
-      if(clone != null) 
-        clone.members = (HashMap)this.members.clone();   
-      return clone;  
-   } 
-}
\ No newline at end of file

Deleted: projects/security/security-jboss-sx/trunk/identity/src/main/org/jboss/security/SimplePrincipal.java
===================================================================
--- projects/security/security-jboss-sx/trunk/identity/src/main/org/jboss/security/SimplePrincipal.java	2007-11-18 22:44:33 UTC (rev 67236)
+++ projects/security/security-jboss-sx/trunk/identity/src/main/org/jboss/security/SimplePrincipal.java	2007-11-19 00:34:47 UTC (rev 67237)
@@ -1,73 +0,0 @@
-/*
-* 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;
-
-import java.security.Principal;
-
-/** A simple String based implementation of Principal. Typically
-a SimplePrincipal is created given a userID which is used
-as the Principal name.
-
- at author <a href="on at ibis.odessa.ua">Oleg Nitz</a>
- at author Scott.Stark at jboss.org
-*/
-public class SimplePrincipal implements Principal, java.io.Serializable
-{ 
-   private static final long serialVersionUID = 1L;
-   private String name;
-
-  public SimplePrincipal(String name)
-  {
-    this.name = name;
-  }
-
-  /** Compare this SimplePrincipal's name against another Principal
-  @return true if name equals another.getName();
-   */
-  public boolean equals(Object another)
-  {
-    if( !(another instanceof Principal) )
-      return false;
-    String anotherName = ((Principal)another).getName();
-    boolean equals = false;
-    if( name == null )
-      equals = anotherName == null;
-    else
-      equals = name.equals(anotherName);
-    return equals;
-  }
-
-  public int hashCode()
-  {
-    return (name == null ? 0 : name.hashCode());
-  }
-
-  public String toString()
-  {
-    return name;
-  }
-
-  public String getName()
-  {
-    return name;
-  }
-} 
\ No newline at end of file

Added: projects/security/security-jboss-sx/trunk/identity/src/main/org/jboss/security/identity/plugins/IdentityFactory.java
===================================================================
--- projects/security/security-jboss-sx/trunk/identity/src/main/org/jboss/security/identity/plugins/IdentityFactory.java	                        (rev 0)
+++ projects/security/security-jboss-sx/trunk/identity/src/main/org/jboss/security/identity/plugins/IdentityFactory.java	2007-11-19 00:34:47 UTC (rev 67237)
@@ -0,0 +1,56 @@
+/*
+  * JBoss, Home of Professional Open Source
+  * Copyright 2007, 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.identity.plugins;
+
+import java.lang.reflect.Constructor;
+import java.security.Principal;
+import java.security.acl.Group;
+
+//$Id$
+
+/**
+ *  Factory to create customized principal and group
+ *  instances
+ *  @author Anil.Saldhana at redhat.com
+ *  @since  Nov 18, 2007 
+ *  @version $Revision$
+ */
+public class IdentityFactory
+{
+   public static final String PRINCIPAL_CLASS = "org.jboss.security.SimplePrincipal";
+   
+   public static final String GROUP_CLASS = "org.jboss.security.SimpleGroup";
+
+   public static Principal createPrincipal(String name) throws Exception
+   {
+     Class<Principal> clazz = SecurityActions.getClass(PRINCIPAL_CLASS);
+     Constructor<Principal> ctr = clazz.getConstructor(new Class[]{String.class});
+     return (Principal) ctr.newInstance(new Object[]{name});
+   }
+   
+   public static Group createGroup(String name) throws Exception
+   {
+      Class<Group> clazz = SecurityActions.getClass(GROUP_CLASS);
+      Constructor<Group> ctr = clazz.getConstructor(new Class[]{String.class});
+      return (Group) ctr.newInstance(new Object[]{name});
+   }
+}
\ No newline at end of file

Added: projects/security/security-jboss-sx/trunk/identity/src/main/org/jboss/security/identity/plugins/SecurityActions.java
===================================================================
--- projects/security/security-jboss-sx/trunk/identity/src/main/org/jboss/security/identity/plugins/SecurityActions.java	                        (rev 0)
+++ projects/security/security-jboss-sx/trunk/identity/src/main/org/jboss/security/identity/plugins/SecurityActions.java	2007-11-19 00:34:47 UTC (rev 67237)
@@ -0,0 +1,60 @@
+/*
+  * JBoss, Home of Professional Open Source
+  * Copyright 2007, 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.identity.plugins;
+
+import java.security.AccessController;
+import java.security.PrivilegedAction;
+
+//$Id$
+
+/**
+ *  Privileged Blocks
+ *  @author Anil.Saldhana at redhat.com
+ *  @since  Nov 18, 2007 
+ *  @version $Revision$
+ */
+ at SuppressWarnings("unused")
+public class SecurityActions
+{
+   public static Class getClass(final String FQN)
+   {
+     return (Class) AccessController.doPrivileged(new PrivilegedAction()
+     {
+      public Object run()
+      {
+         try
+         {
+            ClassLoader tcl = Thread.currentThread().getContextClassLoader();
+            return tcl.loadClass(FQN);
+         }
+         catch(Exception e)
+         {
+            throw new RuntimeException(e);
+         }
+         finally
+         {
+            return null;
+         }
+      }
+    });  
+   }
+}
\ No newline at end of file

Modified: projects/security/security-jboss-sx/trunk/identity/src/main/org/jboss/security/identity/plugins/SimpleIdentity.java
===================================================================
--- projects/security/security-jboss-sx/trunk/identity/src/main/org/jboss/security/identity/plugins/SimpleIdentity.java	2007-11-18 22:44:33 UTC (rev 67236)
+++ projects/security/security-jboss-sx/trunk/identity/src/main/org/jboss/security/identity/plugins/SimpleIdentity.java	2007-11-19 00:34:47 UTC (rev 67237)
@@ -24,9 +24,7 @@
 import java.io.Serializable;
 import java.security.Principal;
 import java.security.acl.Group;
-
-import org.jboss.security.SimpleGroup;
-import org.jboss.security.SimplePrincipal;
+ 
 import org.jboss.security.identity.Identity;
 import org.jboss.security.identity.Role;
 
@@ -63,14 +61,28 @@
    
    public Group asGroup()
    {
-      SimpleGroup sg = new SimpleGroup("Roles");
-      sg.addMember(new SimplePrincipal(role.getRoleName()));
-      return sg;
+      try
+      {
+         Group gp = IdentityFactory.createGroup("Roles");
+         gp.addMember(IdentityFactory.createPrincipal(role.getRoleName()));
+         return gp; 
+      }
+      catch(Exception e)
+      {
+         throw new RuntimeException(e);
+      }
    }
 
    public Principal asPrincipal()
    { 
-      return new SimplePrincipal(name);
+      try
+      {
+         return IdentityFactory.createPrincipal(name); 
+      }
+      catch(Exception e)
+      {
+         throw new RuntimeException(e);
+      } 
    }
 
    public String getName()




More information about the jboss-cvs-commits mailing list