[seam-commits] Seam SVN: r7990 - in trunk/src/main/org/jboss/seam: security/management and 2 other directories.

seam-commits at lists.jboss.org seam-commits at lists.jboss.org
Sun Apr 20 22:56:06 EDT 2008


Author: shane.bryzak at jboss.com
Date: 2008-04-20 22:56:06 -0400 (Sun, 20 Apr 2008)
New Revision: 7990

Added:
   trunk/src/main/org/jboss/seam/util/AnnotatedBeanProperty.java
Removed:
   trunk/src/main/org/jboss/seam/security/management/BeanProperty.java
Modified:
   trunk/src/main/org/jboss/seam/core/ConversationPropagation.java
   trunk/src/main/org/jboss/seam/security/management/JpaIdentityStore.java
   trunk/src/main/org/jboss/seam/security/permission/dynamic/JpaDynamicPermissionStore.java
Log:
implemented listPermissions()

Modified: trunk/src/main/org/jboss/seam/core/ConversationPropagation.java
===================================================================
--- trunk/src/main/org/jboss/seam/core/ConversationPropagation.java	2008-04-21 02:30:29 UTC (rev 7989)
+++ trunk/src/main/org/jboss/seam/core/ConversationPropagation.java	2008-04-21 02:56:06 UTC (rev 7990)
@@ -101,35 +101,25 @@
 
    private void restoreNaturalConversationId(Map parameters)
    {
-      //First, try to get the conversation id from the request parameter defined for the page
       String viewId = Pages.getCurrentViewId();
       if ( viewId!=null )
       {
-         Page page = Pages.instance().getPage(viewId);
+         Page page = Pages.instance().getPage(viewId);                           
          
          if(conversationName != null)
          {
-             ConversationIdParameter currentConversationIdParameter = Pages.instance().getConversationIdParameter(conversationName);
-             
+             ConversationIdParameter currentConversationIdParameter = Pages.instance().getConversationIdParameter(conversationName);            
              if(currentConversationIdParameter == null)
              {
                  throw new IllegalStateException("The conversationName specified: " + conversationName + ", does not exist.");
              }
-             // Try to restore the conversation from parameters (the user has specified the exact conversation to restore using f:param)
-             conversationId = currentConversationIdParameter.getRequestConversationId(parameters);
-             if (conversationId == null)
-             {
-                // Try to restore the conversation from the EL expression on the conversation definition
-                conversationId = currentConversationIdParameter.getConversationId();
-             }
+             conversationId = currentConversationIdParameter.getRequestConversationId(parameters);                  
          }
          else
          {
              conversationId = page.getConversationIdParameter().getRequestConversationId(parameters);
-         }         
-         //TODO: how about the parent conversation id?
+         }
       }
-
    }
 
    private void restoreSyntheticConversationId(Map parameters)

Deleted: trunk/src/main/org/jboss/seam/security/management/BeanProperty.java
===================================================================
--- trunk/src/main/org/jboss/seam/security/management/BeanProperty.java	2008-04-21 02:30:29 UTC (rev 7989)
+++ trunk/src/main/org/jboss/seam/security/management/BeanProperty.java	2008-04-21 02:56:06 UTC (rev 7990)
@@ -1,173 +0,0 @@
-package org.jboss.seam.security.management;
-
-import java.lang.annotation.Annotation;
-import java.lang.reflect.Field;
-import java.lang.reflect.Method;
-
-/**
- * A convenience class for working with an annotated property (either a field or method) of
- * a JavaBean class.
- *  
- * @author Shane Bryzak
- */
-public class BeanProperty
-{
-   private Field propertyField;
-   private Method propertyGetter;
-   private Method propertySetter;
-   private Annotation annotation;
-   private String name;
-   private Class propertyClass;
-   
-   private boolean isFieldProperty;
-   
-   private BeanProperty(Field propertyField, Annotation annotation)
-   {
-      this.propertyField = propertyField;
-      isFieldProperty = true;
-      this.annotation = annotation;
-      this.name = propertyField.getName();
-      this.propertyClass = propertyField.getDeclaringClass();
-   }
-   
-   private BeanProperty(Method propertyMethod, Annotation annotation)
-   {
-      if (!(propertyMethod.getName().startsWith("get") || (propertyMethod.getName().startsWith("is"))))
-      {
-         throw new IllegalArgumentException("Bean property method name " + propertyMethod.getClass().getName() +
-               "." + propertyMethod.getName() + "() must start with \"get\" or \"is\".");
-      }
-      
-      if (propertyMethod.getReturnType().equals(void.class) || propertyMethod.getParameterTypes().length > 0)
-      {
-         throw new IllegalArgumentException("Bean property method " + propertyMethod.getClass().getName() +
-               "." + propertyMethod.getName() + "() must return a value and take no parameters");
-      }
-      
-      this.propertyGetter = propertyMethod;
-      this.propertyClass = propertyMethod.getReturnType();
-      
-      String methodName = propertyMethod.getName();
-      
-      this.name = methodName.startsWith("get") ?
-            (methodName.substring(3,4).toLowerCase() + methodName.substring(4)) :
-            (methodName.substring(2,3).toLowerCase() + methodName.substring(3));
-      
-      String setterName = propertyMethod.getName().startsWith("get") ?
-            ("set" + methodName.substring(3)) : ("set" + methodName.substring(2));
-            
-      try
-      {
-         propertySetter = propertyMethod.getDeclaringClass().getMethod(setterName, new Class[] {propertyMethod.getReturnType()});
-      }
-      catch (NoSuchMethodException ex)
-      {
-         throw new IllegalArgumentException("Bean property method " + propertyMethod.getClass().getName() +
-               "." + propertyMethod.getName() + "() must have a corresponding setter method.");                  
-      }
-      
-      isFieldProperty = false;
-      this.annotation = annotation;
-   }
-   
-   public void setValue(Object bean, Object value)
-   {
-      if (isFieldProperty)
-      {
-         boolean accessible = propertyField.isAccessible();
-         try
-         {
-            propertyField.setAccessible(true);
-            propertyField.set(bean, value);   
-         }
-         catch (IllegalAccessException ex)
-         {
-            throw new RuntimeException("Exception setting bean property", ex);
-         }
-         finally
-         {
-            propertyField.setAccessible(accessible);
-         }            
-      }
-      else
-      {
-         try
-         {
-            propertySetter.invoke(bean, value);
-         }
-         catch (Exception ex)
-         {
-            throw new RuntimeException("Exception setting bean property", ex);
-         }
-      }
-   }
-   
-   public Object getValue(Object bean)
-   {
-      if (isFieldProperty)
-      {
-         boolean accessible = propertyField.isAccessible();
-         try
-         {
-            propertyField.setAccessible(true);
-            return propertyField.get(bean);
-         }
-         catch (IllegalAccessException ex)
-         {
-            throw new RuntimeException("Exception getting bean property", ex);
-         }
-         finally
-         {
-            propertyField.setAccessible(accessible);
-         }
-      }
-      else
-      {
-         try
-         {
-            return propertyGetter.invoke(bean);
-         }
-         catch (Exception ex)
-         {
-            throw new RuntimeException("Exception getting bean property", ex);
-         }
-      }
-   }
-   
-   public Annotation getAnnotation()
-   {
-      return annotation;
-   }
-   
-   public String getName()
-   {
-      return name;
-   }
-   
-   public Class getPropertyClass()
-   {
-      return propertyClass;
-   }
-   
-   
-   public static BeanProperty scanForProperty(Class cls, Class<? extends Annotation> annotation)
-   {
-      for (Field f : cls.getFields())
-      {
-         if (f.isAnnotationPresent(annotation)) 
-         {
-            return new BeanProperty(f, f.getAnnotation(annotation));
-         }
-      }
-      
-      for (Method m : cls.getMethods())
-      {
-         if (m.isAnnotationPresent(annotation))
-         {
-            return new BeanProperty(m, m.getAnnotation(annotation));
-         }
-      }
-      
-      return null;
-   }   
-}
\ No newline at end of file

Modified: trunk/src/main/org/jboss/seam/security/management/JpaIdentityStore.java
===================================================================
--- trunk/src/main/org/jboss/seam/security/management/JpaIdentityStore.java	2008-04-21 02:30:29 UTC (rev 7989)
+++ trunk/src/main/org/jboss/seam/security/management/JpaIdentityStore.java	2008-04-21 02:56:06 UTC (rev 7990)
@@ -34,6 +34,7 @@
 import org.jboss.seam.log.LogProvider;
 import org.jboss.seam.log.Logging;
 import org.jboss.seam.security.Identity;
+import org.jboss.seam.util.AnnotatedBeanProperty;
 
 /**
  * The default identity store implementation, uses JPA as its persistence mechanism.
@@ -61,14 +62,14 @@
    private Class userClass;
    private Class roleClass;   
    
-   private BeanProperty userPrincipalProperty;
-   private BeanProperty userPasswordProperty;
-   private BeanProperty userRolesProperty;
-   private BeanProperty userEnabledProperty;
-   private BeanProperty userFirstNameProperty;
-   private BeanProperty userLastNameProperty;   
-   private BeanProperty roleNameProperty;
-   private BeanProperty roleGroupsProperty;
+   private AnnotatedBeanProperty<UserPrincipal> userPrincipalProperty;
+   private AnnotatedBeanProperty<UserPassword> userPasswordProperty;
+   private AnnotatedBeanProperty<UserRoles> userRolesProperty;
+   private AnnotatedBeanProperty<UserEnabled> userEnabledProperty;
+   private AnnotatedBeanProperty<UserFirstName> userFirstNameProperty;
+   private AnnotatedBeanProperty<UserLastName> userLastNameProperty;   
+   private AnnotatedBeanProperty<RoleName> roleNameProperty;
+   private AnnotatedBeanProperty<RoleGroups> roleGroupsProperty;
    
    public Set<Feature> getFeatures()
    {
@@ -116,15 +117,15 @@
    
    private void initProperties()
    {
-      userPrincipalProperty = BeanProperty.scanForProperty(userClass, UserPrincipal.class);
-      userPasswordProperty = BeanProperty.scanForProperty(userClass, UserPassword.class);
-      userRolesProperty = BeanProperty.scanForProperty(userClass, UserRoles.class);
-      userEnabledProperty = BeanProperty.scanForProperty(userClass, UserEnabled.class);
-      userFirstNameProperty = BeanProperty.scanForProperty(userClass, UserFirstName.class);
-      userLastNameProperty = BeanProperty.scanForProperty(userClass, UserLastName.class);
+      userPrincipalProperty = AnnotatedBeanProperty.scanForProperty(userClass, UserPrincipal.class);
+      userPasswordProperty = AnnotatedBeanProperty.scanForProperty(userClass, UserPassword.class);
+      userRolesProperty = AnnotatedBeanProperty.scanForProperty(userClass, UserRoles.class);
+      userEnabledProperty = AnnotatedBeanProperty.scanForProperty(userClass, UserEnabled.class);
+      userFirstNameProperty = AnnotatedBeanProperty.scanForProperty(userClass, UserFirstName.class);
+      userLastNameProperty = AnnotatedBeanProperty.scanForProperty(userClass, UserLastName.class);
       
-      roleNameProperty = BeanProperty.scanForProperty(roleClass, RoleName.class);
-      roleGroupsProperty = BeanProperty.scanForProperty(roleClass, RoleGroups.class);
+      roleNameProperty = AnnotatedBeanProperty.scanForProperty(roleClass, RoleName.class);
+      roleGroupsProperty = AnnotatedBeanProperty.scanForProperty(roleClass, RoleGroups.class);
       
       if (userPrincipalProperty == null) 
       {
@@ -410,7 +411,7 @@
          throw new NoSuchUserException("Could not enable user, user '" + name + "' does not exist");
       }
       
-      // If it's already enabled return false
+      // Can't enable an already-enabled user, return false
       if (((Boolean) userEnabledProperty.getValue(user)) == true)
       {
          return false;
@@ -435,7 +436,7 @@
          throw new NoSuchUserException("Could not disable user, user '" + name + "' does not exist");
       }
       
-      // If it's already disabled return false
+      // Can't disable an already-disabled user, return false
       if (((Boolean) userEnabledProperty.getValue(user)) == false)
       {
          return false;
@@ -648,6 +649,16 @@
       }      
    }
    
+   public String getUserName(Object user)
+   {
+      return (String) userPrincipalProperty.getValue(user);
+   }
+   
+   public String getRoleName(Object role)
+   {
+      return (String) roleNameProperty.getValue(role);
+   }
+   
    public Object lookupRole(String role)       
    {
       try

Modified: trunk/src/main/org/jboss/seam/security/permission/dynamic/JpaDynamicPermissionStore.java
===================================================================
--- trunk/src/main/org/jboss/seam/security/permission/dynamic/JpaDynamicPermissionStore.java	2008-04-21 02:30:29 UTC (rev 7989)
+++ trunk/src/main/org/jboss/seam/security/permission/dynamic/JpaDynamicPermissionStore.java	2008-04-21 02:56:06 UTC (rev 7990)
@@ -5,7 +5,10 @@
 
 import java.io.Serializable;
 import java.security.Principal;
+import java.util.ArrayList;
+import java.util.HashMap;
 import java.util.List;
+import java.util.Map;
 import java.util.Set;
 
 import javax.persistence.EntityManager;
@@ -28,10 +31,11 @@
 import org.jboss.seam.log.LogProvider;
 import org.jboss.seam.log.Logging;
 import org.jboss.seam.security.Role;
-import org.jboss.seam.security.management.BeanProperty;
+import org.jboss.seam.security.SimplePrincipal;
 import org.jboss.seam.security.management.JpaIdentityStore;
 import org.jboss.seam.security.permission.Permission;
 import org.jboss.seam.security.permission.PermissionStore;
+import org.jboss.seam.util.AnnotatedBeanProperty;
 
 /**
  * A permission store implementation that uses JPA as its persistence mechanism.
@@ -51,15 +55,15 @@
    private Class userPermissionClass;
    private Class rolePermissionClass;
       
-   private BeanProperty userProperty;
-   private BeanProperty roleProperty;
+   private AnnotatedBeanProperty<PermissionUser> userProperty;
+   private AnnotatedBeanProperty<PermissionRole> roleProperty;
    
-   private BeanProperty targetProperty;
-   private BeanProperty actionProperty;   
-   private BeanProperty discriminatorProperty;
+   private AnnotatedBeanProperty<PermissionTarget> targetProperty;
+   private AnnotatedBeanProperty<PermissionAction> actionProperty;   
+   private AnnotatedBeanProperty<PermissionDiscriminator> discriminatorProperty;
    
-   private BeanProperty roleTargetProperty;
-   private BeanProperty roleActionProperty;
+   private AnnotatedBeanProperty<PermissionTarget> roleTargetProperty;
+   private AnnotatedBeanProperty<PermissionAction> roleActionProperty;
    
    private String selectUserPermissionQuery;
    private String selectRolePermissionQuery;
@@ -84,25 +88,25 @@
    
    protected void initProperties()
    {
-      userProperty = BeanProperty.scanForProperty(userPermissionClass, PermissionUser.class);
-      targetProperty = BeanProperty.scanForProperty(userPermissionClass, PermissionTarget.class);
-      actionProperty = BeanProperty.scanForProperty(userPermissionClass, PermissionAction.class);
+      userProperty = AnnotatedBeanProperty.scanForProperty(userPermissionClass, PermissionUser.class);
+      targetProperty = AnnotatedBeanProperty.scanForProperty(userPermissionClass, PermissionTarget.class);
+      actionProperty = AnnotatedBeanProperty.scanForProperty(userPermissionClass, PermissionAction.class);
       
       if (rolePermissionClass != null)
       {
-         roleProperty = BeanProperty.scanForProperty(rolePermissionClass, PermissionRole.class);
+         roleProperty = AnnotatedBeanProperty.scanForProperty(rolePermissionClass, PermissionRole.class);
          if (roleProperty != null)
          {
-            roleTargetProperty = BeanProperty.scanForProperty(rolePermissionClass, PermissionTarget.class);
-            roleActionProperty = BeanProperty.scanForProperty(rolePermissionClass, PermissionAction.class);
+            roleTargetProperty = AnnotatedBeanProperty.scanForProperty(rolePermissionClass, PermissionTarget.class);
+            roleActionProperty = AnnotatedBeanProperty.scanForProperty(rolePermissionClass, PermissionAction.class);
          }
       }
       else
       {
-         roleProperty = BeanProperty.scanForProperty(userPermissionClass, PermissionRole.class);
+         roleProperty = AnnotatedBeanProperty.scanForProperty(userPermissionClass, PermissionRole.class);
          if (roleProperty != null)
          {
-            discriminatorProperty = BeanProperty.scanForProperty(userPermissionClass, PermissionDiscriminator.class);
+            discriminatorProperty = AnnotatedBeanProperty.scanForProperty(userPermissionClass, PermissionDiscriminator.class);
          }
       }
       
@@ -149,16 +153,7 @@
       query.append(targetProperty.getName());
       query.append(" = :target and ");
       query.append(actionProperty.getName());
-      query.append(" = :action and ");
-      query.append(userProperty.getName());
-      query.append(" = :recipient");
-      
-      if (rolePermissionClass == null)
-      {
-         query.append(" and ");
-         query.append(discriminatorProperty.getName());
-         query.append(" = :discriminator");         
-      }
+      query.append(" = :action");
             
       selectUserPermissionQuery = query.toString();
       
@@ -171,9 +166,7 @@
          query.append(roleTargetProperty.getName());
          query.append(" = :target and ");
          query.append(roleActionProperty.getName());
-         query.append(" = :action and ");
-         query.append(roleProperty.getName());
-         query.append(" = :recipient");
+         query.append(" = :action");
          
          selectRolePermissionQuery = query.toString();
       }
@@ -214,8 +207,8 @@
                  
          Object instance = userPermissionClass.newInstance();
          targetProperty.setValue(instance, permission.getTarget().toString());
-         actionProperty.setValue(instance, permission.getAction());
-         userProperty.setValue(instance, permission.getRecipient().getName());
+         actionProperty.setValue(instance, permission.getAction());         
+         userProperty.setValue(instance, resolvePrincipal(permission.getRecipient()));
          
          if (discriminatorProperty != null)
          {
@@ -269,7 +262,8 @@
    
    /**
     * If the user or role properties in the entity class refer to other entities, then this method
-    * uses JpaIdentityStore to lookup that user or role entity. 
+    * uses the JpaIdentityStore (if available) to lookup that user or role entity.  Otherwise it
+    * simply returns the name of the recipient. 
     * 
     * @param recipient
     * @return
@@ -294,23 +288,121 @@
       
       return recipient.getName();
    }
+   
+   protected String resolvePrincipalName(Object principal, boolean isUser
+         )
+   {
+      if (principal instanceof String)
+      {
+         return (String) principal;
+      }
+      
+      JpaIdentityStore identityStore = (JpaIdentityStore) Component.getInstance(JpaIdentityStore.class, true);
+      
+      if (identityStore != null)
+      {
+         if (isUser && identityStore.getUserClass().equals(principal.getClass()))
+         {
+            return identityStore.getUserName(principal);
+         }
+         
+         if (!isUser && identityStore.getRoleClass().equals(principal.getClass()))
+         {
+            return identityStore.getRoleName(principal);
+         }
+      }
+      
+      throw new IllegalArgumentException("Cannot resolve principal name for principal " + principal); 
+   }
 
    public List<Permission> listPermissions(Object target, String action) 
    {
-      return lookupEntityManager().createQuery(
-            "from " + userPermissionClass.getName() + 
-            " where target = :target and action = :action")
-            .setParameter("target", target)
-            .setParameter("action", action)
-            .getResultList();
+      List<Permission> permissions = new ArrayList<Permission>();
+      
+      Query permissionQuery = lookupEntityManager().createQuery(selectUserPermissionQuery)
+         .setParameter("target", target);
+      
+      if (action != null)
+      {
+         permissionQuery.setParameter("action", action);
+      }
+      
+      List userPermissions = permissionQuery.getResultList(); 
+      
+      Map<String,Principal> principalCache = new HashMap<String,Principal>();
+      
+      boolean useDiscriminator = rolePermissionClass == null && discriminatorProperty != null;
+      
+      for (Object permission : userPermissions)
+      {
+         Principal principal;
+         boolean isUser = true;
+         
+         if (useDiscriminator && 
+            discriminatorProperty.getAnnotation().roleValue().equals(discriminatorProperty.getValue(permission)))
+         {
+            isUser = false;
+         }
+
+         String name = resolvePrincipalName(isUser ? userProperty.getValue(permission) :
+            roleProperty.getValue(permission), isUser);
+         
+         String key = (isUser ? "user:" : "role:") + name;
+         
+         if (!principalCache.containsKey(key))
+         {
+            principal = isUser ? new SimplePrincipal(name) : new Role(name);
+            principalCache.put(key, principal);
+         }
+         else
+         {
+            principal = principalCache.get(key);
+         }
+         
+         permissions.add(new Permission(target, (String) (action != null ? action : actionProperty.getValue(permission)), 
+               principal));
+      }
+      
+      if (rolePermissionClass == null)
+      {
+         permissionQuery = lookupEntityManager().createQuery(selectRolePermissionQuery)
+         .setParameter("target", target);         
+         
+         if (action != null)
+         {
+            permissionQuery.setParameter("action", action);
+         }
+         
+         List rolePermissions = permissionQuery.getResultList();
+         
+         for (Object permission : rolePermissions)
+         {
+            Principal principal;
+            
+            String name = resolvePrincipalName(roleProperty.getValue(permission), false);
+            String key = "role:" + name;
+            
+            if (!principalCache.containsKey(key))
+            {
+               principal = new Role(name);
+               principalCache.put(key, principal);
+            }
+            else
+            {
+               principal = principalCache.get(key);
+            }
+            
+            permissions.add(new Permission(target, (String) (action != null ? action : 
+               roleActionProperty.getValue(permission)), principal));
+         }
+      }
+      
+      return permissions;
    }
 
    public List<Permission> listPermissions(Object target) 
    {
-      return lookupEntityManager().createQuery(
-            "from " + userPermissionClass.getName() + " where target = :target")
-            .setParameter("target", target)
-            .getResultList();
+      return listPermissions(target, null);
    }
    
    public List<Permission> listPermissions(Set<Object> targets)

Added: trunk/src/main/org/jboss/seam/util/AnnotatedBeanProperty.java
===================================================================
--- trunk/src/main/org/jboss/seam/util/AnnotatedBeanProperty.java	                        (rev 0)
+++ trunk/src/main/org/jboss/seam/util/AnnotatedBeanProperty.java	2008-04-21 02:56:06 UTC (rev 7990)
@@ -0,0 +1,173 @@
+package org.jboss.seam.util;
+
+import java.lang.annotation.Annotation;
+import java.lang.reflect.Field;
+import java.lang.reflect.Method;
+
+/**
+ * A convenience class for working with an annotated property (either a field or method) of
+ * a JavaBean class.
+ *  
+ * @author Shane Bryzak
+ */
+public class AnnotatedBeanProperty<T extends Annotation>
+{
+   private Field propertyField;
+   private Method propertyGetter;
+   private Method propertySetter;
+   private T annotation;
+   private String name;
+   private Class propertyClass;
+   
+   private boolean isFieldProperty;
+   
+   private AnnotatedBeanProperty(Field propertyField, T annotation)
+   {
+      this.propertyField = propertyField;
+      isFieldProperty = true;
+      this.annotation = annotation;
+      this.name = propertyField.getName();
+      this.propertyClass = propertyField.getDeclaringClass();
+   }
+   
+   private AnnotatedBeanProperty(Method propertyMethod, T annotation)
+   {
+      if (!(propertyMethod.getName().startsWith("get") || (propertyMethod.getName().startsWith("is"))))
+      {
+         throw new IllegalArgumentException("Bean property method name " + propertyMethod.getClass().getName() +
+               "." + propertyMethod.getName() + "() must start with \"get\" or \"is\".");
+      }
+      
+      if (propertyMethod.getReturnType().equals(void.class) || propertyMethod.getParameterTypes().length > 0)
+      {
+         throw new IllegalArgumentException("Bean property method " + propertyMethod.getClass().getName() +
+               "." + propertyMethod.getName() + "() must return a value and take no parameters");
+      }
+      
+      this.propertyGetter = propertyMethod;
+      this.propertyClass = propertyMethod.getReturnType();
+      
+      String methodName = propertyMethod.getName();
+      
+      this.name = methodName.startsWith("get") ?
+            (methodName.substring(3,4).toLowerCase() + methodName.substring(4)) :
+            (methodName.substring(2,3).toLowerCase() + methodName.substring(3));
+      
+      String setterName = propertyMethod.getName().startsWith("get") ?
+            ("set" + methodName.substring(3)) : ("set" + methodName.substring(2));
+            
+      try
+      {
+         propertySetter = propertyMethod.getDeclaringClass().getMethod(setterName, new Class[] {propertyMethod.getReturnType()});
+      }
+      catch (NoSuchMethodException ex)
+      {
+         throw new IllegalArgumentException("Bean property method " + propertyMethod.getClass().getName() +
+               "." + propertyMethod.getName() + "() must have a corresponding setter method.");                  
+      }
+      
+      isFieldProperty = false;
+      this.annotation = annotation;
+   }
+   
+   public void setValue(Object bean, Object value)
+   {
+      if (isFieldProperty)
+      {
+         boolean accessible = propertyField.isAccessible();
+         try
+         {
+            propertyField.setAccessible(true);
+            propertyField.set(bean, value);   
+         }
+         catch (IllegalAccessException ex)
+         {
+            throw new RuntimeException("Exception setting bean property", ex);
+         }
+         finally
+         {
+            propertyField.setAccessible(accessible);
+         }            
+      }
+      else
+      {
+         try
+         {
+            propertySetter.invoke(bean, value);
+         }
+         catch (Exception ex)
+         {
+            throw new RuntimeException("Exception setting bean property", ex);
+         }
+      }
+   }
+   
+   public Object getValue(Object bean)
+   {
+      if (isFieldProperty)
+      {
+         boolean accessible = propertyField.isAccessible();
+         try
+         {
+            propertyField.setAccessible(true);
+            return propertyField.get(bean);
+         }
+         catch (IllegalAccessException ex)
+         {
+            throw new RuntimeException("Exception getting bean property", ex);
+         }
+         finally
+         {
+            propertyField.setAccessible(accessible);
+         }
+      }
+      else
+      {
+         try
+         {
+            return propertyGetter.invoke(bean);
+         }
+         catch (Exception ex)
+         {
+            throw new RuntimeException("Exception getting bean property", ex);
+         }
+      }
+   }
+   
+   public T getAnnotation()
+   {
+      return annotation;
+   }
+   
+   public String getName()
+   {
+      return name;
+   }
+   
+   public Class getPropertyClass()
+   {
+      return propertyClass;
+   }
+   
+   
+   public static AnnotatedBeanProperty scanForProperty(Class cls, Class<? extends Annotation> annotation)
+   {
+      for (Field f : cls.getFields())
+      {
+         if (f.isAnnotationPresent(annotation)) 
+         {
+            return new AnnotatedBeanProperty(f, f.getAnnotation(annotation));
+         }
+      }
+      
+      for (Method m : cls.getMethods())
+      {
+         if (m.isAnnotationPresent(annotation))
+         {
+            return new AnnotatedBeanProperty(m, m.getAnnotation(annotation));
+         }
+      }
+      
+      return null;
+   }   
+}
\ No newline at end of file




More information about the seam-commits mailing list