Author: shane.bryzak(a)jboss.com
Date: 2008-10-06 01:15:52 -0400 (Mon, 06 Oct 2008)
New Revision: 9205
Modified:
trunk/src/main/org/jboss/seam/security/JpaTokenStore.java
trunk/src/main/org/jboss/seam/security/management/JpaIdentityStore.java
trunk/src/main/org/jboss/seam/security/management/action/UserAction.java
trunk/src/main/org/jboss/seam/security/permission/JpaPermissionStore.java
trunk/src/main/org/jboss/seam/util/AnnotatedBeanProperty.java
Log:
JBSEAM-3511
Modified: trunk/src/main/org/jboss/seam/security/JpaTokenStore.java
===================================================================
--- trunk/src/main/org/jboss/seam/security/JpaTokenStore.java 2008-10-06 05:13:26 UTC (rev
9204)
+++ trunk/src/main/org/jboss/seam/security/JpaTokenStore.java 2008-10-06 05:15:52 UTC (rev
9205)
@@ -47,16 +47,16 @@
entityManager =
Expressions.instance().createValueExpression("#{entityManager}",
EntityManager.class);
}
- tokenUsernameProperty = AnnotatedBeanProperty.scanForProperty(tokenClass,
TokenUsername.class);
- tokenValueProperty = AnnotatedBeanProperty.scanForProperty(tokenClass,
TokenValue.class);
+ tokenUsernameProperty = new AnnotatedBeanProperty<TokenUsername>(tokenClass,
TokenUsername.class);
+ tokenValueProperty = new AnnotatedBeanProperty<TokenValue>(tokenClass,
TokenValue.class);
- if (tokenUsernameProperty == null)
+ if (!tokenUsernameProperty.isSet())
{
throw new IllegalStateException("Invalid tokenClass " +
tokenClass.getName() +
" - required annotation @TokenUsername not found on any Field or
Method.");
}
- if (tokenValueProperty == null)
+ if (!tokenValueProperty.isSet())
{
throw new IllegalStateException("Invalid tokenClass " +
tokenClass.getName() +
" - required annotation @TokenValue not found on any Field or
Method.");
Modified: trunk/src/main/org/jboss/seam/security/management/JpaIdentityStore.java
===================================================================
--- trunk/src/main/org/jboss/seam/security/management/JpaIdentityStore.java 2008-10-06
05:13:26 UTC (rev 9204)
+++ trunk/src/main/org/jboss/seam/security/management/JpaIdentityStore.java 2008-10-06
05:15:52 UTC (rev 9205)
@@ -113,33 +113,33 @@
private void initProperties()
{
- 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);
+ userPrincipalProperty = new AnnotatedBeanProperty(userClass, UserPrincipal.class);
+ userPasswordProperty = new AnnotatedBeanProperty(userClass, UserPassword.class);
+ userRolesProperty = new AnnotatedBeanProperty(userClass, UserRoles.class);
+ userEnabledProperty = new AnnotatedBeanProperty(userClass, UserEnabled.class);
+ userFirstNameProperty = new AnnotatedBeanProperty(userClass, UserFirstName.class);
+ userLastNameProperty = new AnnotatedBeanProperty(userClass, UserLastName.class);
if (roleClass != null)
{
- roleNameProperty = AnnotatedBeanProperty.scanForProperty(roleClass,
RoleName.class);
- roleGroupsProperty = AnnotatedBeanProperty.scanForProperty(roleClass,
RoleGroups.class);
- roleConditionalProperty = AnnotatedBeanProperty.scanForProperty(roleClass,
RoleConditional.class);
+ roleNameProperty = new AnnotatedBeanProperty(roleClass, RoleName.class);
+ roleGroupsProperty = new AnnotatedBeanProperty(roleClass, RoleGroups.class);
+ roleConditionalProperty = new AnnotatedBeanProperty(roleClass,
RoleConditional.class);
}
- if (userPrincipalProperty == null)
+ if (!userPrincipalProperty.isSet())
{
throw new IdentityManagementException("Invalid userClass " +
userClass.getName() +
" - required annotation @UserPrincipal not found on any Field or
Method.");
}
- if (userRolesProperty == null)
+ if (!userRolesProperty.isSet())
{
throw new IdentityManagementException("Invalid userClass " +
userClass.getName() +
" - required annotation @UserRoles not found on any Field or
Method.");
}
- if (roleClass != null && roleNameProperty == null)
+ if (roleClass != null && !roleNameProperty.isSet())
{
throw new IdentityManagementException("Invalid roleClass " +
roleClass.getName() +
" - required annotation @RoleName not found on any Field or Method.");
@@ -164,17 +164,17 @@
userPrincipalProperty.setValue(user, username);
- if (userFirstNameProperty != null) userFirstNameProperty.setValue(user,
firstname);
- if (userLastNameProperty != null) userLastNameProperty.setValue(user,
lastname);
+ if (userFirstNameProperty.isSet()) userFirstNameProperty.setValue(user,
firstname);
+ if (userLastNameProperty.isSet()) userLastNameProperty.setValue(user,
lastname);
if (password == null)
{
- if (userEnabledProperty != null) userEnabledProperty.setValue(user, false);
+ if (userEnabledProperty.isSet()) userEnabledProperty.setValue(user, false);
}
else
{
userPasswordProperty.setValue(user, generatePasswordHash(password,
getUserAccountSalt(user)));
- if (userEnabledProperty != null) userEnabledProperty.setValue(user, true);
+ if (userEnabledProperty.isSet()) userEnabledProperty.setValue(user, true);
}
if (Events.exists()) Events.instance().raiseEvent(EVENT_PRE_PERSIST_USER,
user);
@@ -228,7 +228,7 @@
Object user = lookupUser(username);
if (user == null)
{
- if (userPasswordProperty != null)
+ if (userPasswordProperty.isSet())
{
// If no userPasswordProperty is set, it means that authentication is being
performed
// by another identity store and this one is just managing roles
@@ -303,7 +303,7 @@
public boolean addRoleToGroup(String role, String group)
{
- if (roleGroupsProperty == null) return false;
+ if (!roleGroupsProperty.isSet()) return false;
Object targetRole = lookupRole(role);
if (targetRole == null)
@@ -346,7 +346,7 @@
public boolean removeRoleFromGroup(String role, String group)
{
- if (roleGroupsProperty == null) return false;
+ if (!roleGroupsProperty.isSet()) return false;
Object roleToRemove = lookupRole(role);
if (role == null)
@@ -413,7 +413,7 @@
public boolean enableUser(String name)
{
- if (userEnabledProperty == null)
+ if (!userEnabledProperty.isSet())
{
log.debug("Can not enable user, no @UserEnabled property configured in
userClass " + userClass.getName());
return false;
@@ -438,7 +438,7 @@
public boolean disableUser(String name)
{
- if (userEnabledProperty == null)
+ if (!userEnabledProperty.isSet())
{
log.debug("Can not disable user, no @UserEnabled property configured in
userClass " + userClass.getName());
return false;
@@ -488,7 +488,7 @@
public boolean isUserEnabled(String name)
{
Object user = lookupUser(name);
- return user != null && (userEnabledProperty == null || (((Boolean)
userEnabledProperty.getValue(user))) == true);
+ return user != null && (!userEnabledProperty.isSet() || (((Boolean)
userEnabledProperty.getValue(user))) == true);
}
public List<String> getGrantedRoles(String name)
@@ -564,7 +564,7 @@
{
Object instance = lookupRole(role);
- if (roleGroupsProperty != null)
+ if (roleGroupsProperty.isSet())
{
Collection groups = (Collection) roleGroupsProperty.getValue(instance);
@@ -614,7 +614,7 @@
public boolean authenticate(String username, String password)
{
Object user = lookupUser(username);
- if (user == null || (userEnabledProperty != null && ((Boolean)
userEnabledProperty.getValue(user) == false)))
+ if (user == null || (userEnabledProperty.isSet() && ((Boolean)
userEnabledProperty.getValue(user) == false)))
{
return false;
}
@@ -675,8 +675,8 @@
public boolean isRoleConditional(String role)
{
- return roleConditionalProperty == null ? false : (Boolean)
roleConditionalProperty.getValue(
- lookupRole(role));
+ return roleConditionalProperty.isSet() ? (Boolean)
roleConditionalProperty.getValue(
+ lookupRole(role)) : false;
}
public Object lookupRole(String role)
@@ -730,7 +730,7 @@
roleQuery.append(roleClass.getName());
roleQuery.append(" r");
- if (roleConditionalProperty != null)
+ if (roleConditionalProperty.isSet())
{
roleQuery.append(" where r.");
roleQuery.append(roleConditionalProperty.getName());
Modified: trunk/src/main/org/jboss/seam/security/management/action/UserAction.java
===================================================================
--- trunk/src/main/org/jboss/seam/security/management/action/UserAction.java 2008-10-06
05:13:26 UTC (rev 9204)
+++ trunk/src/main/org/jboss/seam/security/management/action/UserAction.java 2008-10-06
05:15:52 UTC (rev 9205)
@@ -68,7 +68,7 @@
private String saveNewUser()
{
- if (!password.equals(confirm))
+ if (password == null || !password.equals(confirm))
{
StatusMessages.instance().addToControl("password", "Passwords do
not match");
return "failure";
Modified: trunk/src/main/org/jboss/seam/security/permission/JpaPermissionStore.java
===================================================================
--- trunk/src/main/org/jboss/seam/security/permission/JpaPermissionStore.java 2008-10-06
05:13:26 UTC (rev 9204)
+++ trunk/src/main/org/jboss/seam/security/permission/JpaPermissionStore.java 2008-10-06
05:15:52 UTC (rev 9205)
@@ -99,32 +99,32 @@
protected void initProperties()
{
- userProperty = AnnotatedBeanProperty.scanForProperty(userPermissionClass,
PermissionUser.class);
- targetProperty = AnnotatedBeanProperty.scanForProperty(userPermissionClass,
PermissionTarget.class);
- actionProperty = AnnotatedBeanProperty.scanForProperty(userPermissionClass,
PermissionAction.class);
+ userProperty = new AnnotatedBeanProperty<PermissionUser>(userPermissionClass,
PermissionUser.class);
+ targetProperty = new
AnnotatedBeanProperty<PermissionTarget>(userPermissionClass,
PermissionTarget.class);
+ actionProperty = new
AnnotatedBeanProperty<PermissionAction>(userPermissionClass,
PermissionAction.class);
if (rolePermissionClass != null)
{
- roleProperty = AnnotatedBeanProperty.scanForProperty(rolePermissionClass,
PermissionRole.class);
- if (roleProperty != null)
+ roleProperty = new
AnnotatedBeanProperty<PermissionRole>(rolePermissionClass, PermissionRole.class);
+ if (roleProperty.isSet())
{
- roleTargetProperty =
AnnotatedBeanProperty.scanForProperty(rolePermissionClass,
+ roleTargetProperty = new
AnnotatedBeanProperty<PermissionTarget>(rolePermissionClass,
PermissionTarget.class);
- roleActionProperty =
AnnotatedBeanProperty.scanForProperty(rolePermissionClass,
+ roleActionProperty = new
AnnotatedBeanProperty<PermissionAction>(rolePermissionClass,
PermissionAction.class);
}
}
else
{
- roleProperty = AnnotatedBeanProperty.scanForProperty(userPermissionClass,
PermissionRole.class);
- if (roleProperty != null)
+ roleProperty = new
AnnotatedBeanProperty<PermissionRole>(userPermissionClass, PermissionRole.class);
+ if (roleProperty.isSet())
{
- discriminatorProperty =
AnnotatedBeanProperty.scanForProperty(userPermissionClass,
+ discriminatorProperty = new
AnnotatedBeanProperty<PermissionDiscriminator>(userPermissionClass,
PermissionDiscriminator.class);
}
}
- if (userProperty == null)
+ if (!userProperty.isSet())
{
throw new RuntimeException("Invalid userPermissionClass " +
userPermissionClass.getName() +
" - required annotation @PermissionUser not found on any Field or
Method.");
@@ -132,25 +132,25 @@
if (rolePermissionClass != null)
{
- if (roleProperty == null)
+ if (!roleProperty.isSet())
{
throw new RuntimeException("Invalid rolePermissionClass " +
rolePermissionClass.getName() +
" - required annotation @PermissionRole not found on any Field or
Method.");
}
- if (roleTargetProperty == null)
+ if (!roleTargetProperty.isSet())
{
throw new RuntimeException("Invalid rolePermissionClass " +
rolePermissionClass.getName() +
" - required annotation @PermissionTarget not found on any Field
or Method.");
}
- if (roleActionProperty == null)
+ if (!roleActionProperty.isSet())
{
throw new RuntimeException("Invalid rolePermissionClass " +
rolePermissionClass.getName() +
" - required annotation @PermissionAction not found on any Field
or Method.");
}
}
- else if (discriminatorProperty == null)
+ else if (!discriminatorProperty.isSet())
{
throw new RuntimeException("Invalid userPermissionClass " +
rolePermissionClass.getName() +
" - no rolePermissionClass set and @PermissionDiscriminator
annotation not found on " +
@@ -342,7 +342,7 @@
return true;
}
- if (discriminatorProperty == null)
+ if (!discriminatorProperty.isSet())
{
throw new RuntimeException("Could not grant permission,
rolePermissionClass not set");
}
@@ -371,7 +371,7 @@
actionProperty.setValue(instance, actionSet.toString());
userProperty.setValue(instance, resolvePrincipalEntity(recipient));
- if (discriminatorProperty != null)
+ if (discriminatorProperty.isSet())
{
PermissionDiscriminator discriminator =
discriminatorProperty.getAnnotation();
discriminatorProperty.setValue(instance, recipientIsRole ?
discriminator.roleValue() :
@@ -529,7 +529,7 @@
if (identityStore != null)
{
- if (recipientIsRole && roleProperty != null &&
roleProperty.getPropertyClass().equals(identityStore.getRoleClass()))
+ if (recipientIsRole && roleProperty.isSet() &&
roleProperty.getPropertyClass().equals(identityStore.getRoleClass()))
{
return identityStore.lookupRole(recipient.getName());
}
@@ -609,7 +609,7 @@
Map<String,Principal> principalCache = new
HashMap<String,Principal>();
- boolean useDiscriminator = rolePermissionClass == null &&
discriminatorProperty != null;
+ boolean useDiscriminator = rolePermissionClass == null &&
discriminatorProperty.isSet();
Map<String,Object> identifierCache = null;
Modified: trunk/src/main/org/jboss/seam/util/AnnotatedBeanProperty.java
===================================================================
--- trunk/src/main/org/jboss/seam/util/AnnotatedBeanProperty.java 2008-10-06 05:13:26 UTC
(rev 9204)
+++ trunk/src/main/org/jboss/seam/util/AnnotatedBeanProperty.java 2008-10-06 05:15:52 UTC
(rev 9205)
@@ -1,5 +1,6 @@
package org.jboss.seam.util;
+import java.beans.Introspector;
import java.lang.annotation.Annotation;
import java.lang.reflect.Field;
import java.lang.reflect.Method;
@@ -15,90 +16,90 @@
private Field propertyField;
private Method propertyGetter;
private Method propertySetter;
- private T annotation;
private String name;
private Class propertyClass;
+ private T annotation;
private boolean isFieldProperty;
+ private boolean set = false;
- 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"))))
+ public AnnotatedBeanProperty(Class<?> cls, Class<T> annotationClass)
+ {
+ // First check declared fields
+ for (Field f : cls.getDeclaredFields())
{
- throw new IllegalArgumentException("Bean property method name " +
propertyMethod.getClass().getName() +
- "." + propertyMethod.getName() + "() must start with
\"get\" or \"is\".");
- }
+ if (f.isAnnotationPresent(annotationClass))
+ {
+ setupFieldProperty(f);
+ this.annotation = f.getAnnotation(annotationClass);
+ set = true;
+ return;
+ }
+ }
- if (propertyMethod.getReturnType().equals(void.class) ||
propertyMethod.getParameterTypes().length > 0)
+ // Then check public fields, in case it's inherited
+ for (Field f : cls.getFields())
{
- throw new IllegalArgumentException("Bean property method " +
propertyMethod.getClass().getName() +
- "." + propertyMethod.getName() + "() must return a value
and take no parameters");
+ if (f.isAnnotationPresent(annotationClass))
+ {
+ this.annotation = f.getAnnotation(annotationClass);
+ setupFieldProperty(f);
+ set = true;
+ return;
+ }
}
- 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
+ // Then check public methods (we ignore private methods)
+ for (Method m : cls.getMethods())
{
- 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;
+ if (m.isAnnotationPresent(annotationClass))
+ {
+ this.annotation = m.getAnnotation(annotationClass);
+ String methodName = m.getName();
+
+ if ( m.getName().startsWith("get") )
+ {
+ this.name = Introspector.decapitalize( m.getName().substring(3) );
+ }
+ else if ( methodName.startsWith("is") )
+ {
+ this.name = Introspector.decapitalize( m.getName().substring(2) );
+ }
+
+ if (this.name != null)
+ {
+ this.propertyGetter = Reflections.getGetterMethod(cls, this.name);
+ this.propertySetter = Reflections.getSetterMethod(cls, this.name);
+ this.propertyClass = this.propertyGetter.getReturnType();
+ isFieldProperty = false;
+ set = true;
+ }
+ else
+ {
+ throw new IllegalStateException("Invalid accessor method, must start
with 'get' or 'is'. " +
+ "Method: " + m + " in class: " + cls);
+ }
+ }
+ }
}
-
+
+ private void setupFieldProperty(Field propertyField)
+ {
+ this.propertyField = propertyField;
+ isFieldProperty = true;
+ this.name = propertyField.getName();
+ this.propertyClass = propertyField.getDeclaringClass();
+ }
+
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);
- }
+ Reflections.setAndWrap(propertyField, bean, value);
}
else
{
- try
- {
- propertySetter.invoke(bean, value);
- }
- catch (Exception ex)
- {
- throw new RuntimeException("Exception setting bean property", ex);
- }
+ Reflections.invokeAndWrap(propertySetter, bean, value);
}
}
@@ -106,42 +107,22 @@
{
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);
- }
+ return Reflections.getAndWrap(propertyField, bean);
}
else
{
- try
- {
- return propertyGetter.invoke(bean);
- }
- catch (Exception ex)
- {
- throw new RuntimeException("Exception getting bean property", ex);
- }
+ return Reflections.invokeAndWrap(propertyGetter, bean);
}
}
- public T getAnnotation()
+ public String getName()
{
- return annotation;
+ return name;
}
- public String getName()
+ public T getAnnotation()
{
- return name;
+ return annotation;
}
public Class getPropertyClass()
@@ -149,25 +130,8 @@
return propertyClass;
}
-
- public static AnnotatedBeanProperty scanForProperty(Class cls, Class<? extends
Annotation> annotation)
+ public boolean isSet()
{
- 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;
- }
+ return set;
+ }
}
\ No newline at end of file