[seam-commits] Seam SVN: r8090 - in trunk/src/main/org/jboss/seam: security/permission and 1 other directory.

seam-commits at lists.jboss.org seam-commits at lists.jboss.org
Wed Apr 30 22:37:36 EDT 2008


Author: shane.bryzak at jboss.com
Date: 2008-04-30 22:37:36 -0400 (Wed, 30 Apr 2008)
New Revision: 8090

Modified:
   trunk/src/main/org/jboss/seam/annotations/security/permission/Identifier.java
   trunk/src/main/org/jboss/seam/security/permission/ClassIdentifierStrategy.java
   trunk/src/main/org/jboss/seam/security/permission/EntityIdentifierStrategy.java
Log:
finished permission identifier implementation

Modified: trunk/src/main/org/jboss/seam/annotations/security/permission/Identifier.java
===================================================================
--- trunk/src/main/org/jboss/seam/annotations/security/permission/Identifier.java	2008-05-01 00:08:50 UTC (rev 8089)
+++ trunk/src/main/org/jboss/seam/annotations/security/permission/Identifier.java	2008-05-01 02:37:36 UTC (rev 8090)
@@ -24,4 +24,5 @@
 public @interface Identifier
 {
    Class<? extends IdentifierStrategy> value();
+   String name() default "";
 }

Modified: trunk/src/main/org/jboss/seam/security/permission/ClassIdentifierStrategy.java
===================================================================
--- trunk/src/main/org/jboss/seam/security/permission/ClassIdentifierStrategy.java	2008-05-01 00:08:50 UTC (rev 8089)
+++ trunk/src/main/org/jboss/seam/security/permission/ClassIdentifierStrategy.java	2008-05-01 02:37:36 UTC (rev 8090)
@@ -1,5 +1,11 @@
 package org.jboss.seam.security.permission;
 
+import java.util.Map;
+import java.util.concurrent.ConcurrentHashMap;
+
+import org.jboss.seam.Seam;
+import org.jboss.seam.annotations.security.permission.Identifier;
+
 /**
  * An Identifier strategy for class-based permission checks
  * 
@@ -7,6 +13,8 @@
  */
 public class ClassIdentifierStrategy implements IdentifierStrategy
 {
+   private Map<Class,String> identifierNames = new ConcurrentHashMap<Class,String>();   
+   
    public boolean canIdentify(Class targetClass)
    {
       return Class.class.equals(targetClass);
@@ -14,8 +22,43 @@
 
    public String getIdentifier(Object target)
    {
-      // TODO Auto-generated method stub
-      return null;
+      if (!(target instanceof Class))
+      {
+         throw new IllegalArgumentException("Target [" + target + "] must be instance of Class");
+      }
+      
+      return getIdentifierName((Class) target);
    }
-
+   
+   private String getIdentifierName(Class cls)
+   {
+      if (!identifierNames.containsKey(cls))
+      {   
+         String name = null;
+         
+         if (cls.isAnnotationPresent(Identifier.class))
+         {
+            Identifier identifier = (Identifier) cls.getAnnotation(Identifier.class);
+            if (identifier.name() != null && !"".equals(name.trim()))
+            {
+               name = identifier.name();
+            }
+         }
+         
+         if (name == null)
+         {
+            name = Seam.getComponentName(cls);
+         }
+         
+         if (name == null)
+         {
+            name = cls.getName().substring(cls.getName().lastIndexOf('.') + 1);
+         }
+         
+         identifierNames.put(cls, name);
+         return name;
+      }
+      
+      return identifierNames.get(cls);
+   }
 }

Modified: trunk/src/main/org/jboss/seam/security/permission/EntityIdentifierStrategy.java
===================================================================
--- trunk/src/main/org/jboss/seam/security/permission/EntityIdentifierStrategy.java	2008-05-01 00:08:50 UTC (rev 8089)
+++ trunk/src/main/org/jboss/seam/security/permission/EntityIdentifierStrategy.java	2008-05-01 02:37:36 UTC (rev 8090)
@@ -1,9 +1,14 @@
 package org.jboss.seam.security.permission;
 
+import java.util.Map;
+import java.util.concurrent.ConcurrentHashMap;
+
 import javax.persistence.Entity;
 import javax.persistence.EntityManager;
 
 import org.jboss.seam.Component;
+import org.jboss.seam.Seam;
+import org.jboss.seam.annotations.security.permission.Identifier;
 import org.jboss.seam.core.Expressions;
 import org.jboss.seam.core.Expressions.ValueExpression;
 import org.jboss.seam.persistence.PersistenceProvider;
@@ -19,6 +24,8 @@
    
    private PersistenceProvider persistenceProvider;
    
+   private Map<Class,String> identifierNames = new ConcurrentHashMap<Class,String>();
+   
    public EntityIdentifierStrategy()
    {
       persistenceProvider = (PersistenceProvider) Component.getInstance(PersistenceProvider.class, true);
@@ -37,9 +44,41 @@
 
    public String getIdentifier(Object target)
    {
-      // TODO temporary, need to implement properly
-      return target.getClass().getName() + ":" + persistenceProvider.getId(target, lookupEntityManager());
+      return String.format("%s:%s", getIdentifierName(target.getClass()),  
+        persistenceProvider.getId(target, lookupEntityManager()).toString());
    }
+   
+   private String getIdentifierName(Class cls)
+   {
+      if (!identifierNames.containsKey(cls))
+      {   
+         String name = null;
+         
+         if (cls.isAnnotationPresent(Identifier.class))
+         {
+            Identifier identifier = (Identifier) cls.getAnnotation(Identifier.class);
+            if (identifier.name() != null && !"".equals(name.trim()))
+            {
+               name = identifier.name();
+            }
+         }
+         
+         if (name == null)
+         {
+            name = Seam.getComponentName(cls);
+         }
+         
+         if (name == null)
+         {
+            name = cls.getName().substring(cls.getName().lastIndexOf('.') + 1);
+         }
+         
+         identifierNames.put(cls, name);
+         return name;
+      }
+      
+      return identifierNames.get(cls);
+   }
 
    private EntityManager lookupEntityManager()
    {




More information about the seam-commits mailing list