[jboss-cvs] jboss-seam/src/main/org/jboss/seam/security/acl ...
Shane Bryzak
Shane_Bryzak at symantec.com
Wed Nov 8 20:26:30 EST 2006
User: sbryzak2
Date: 06/11/08 20:26:30
Modified: src/main/org/jboss/seam/security/acl
IdentityGenerator.java
Added: src/main/org/jboss/seam/security/acl
JPAIdentityGenerator.java
Removed: src/main/org/jboss/seam/security/acl
DefaultIdentityGenerator.java
Log:
created JPAIdentityGenerator (renamed from DefaultIdentityGenerator)
Revision Changes Path
1.2 +1 -3 jboss-seam/src/main/org/jboss/seam/security/acl/IdentityGenerator.java
(In the diff below, changes in quantity of whitespace are not shown.)
Index: IdentityGenerator.java
===================================================================
RCS file: /cvsroot/jboss/jboss-seam/src/main/org/jboss/seam/security/acl/IdentityGenerator.java,v
retrieving revision 1.1
retrieving revision 1.2
diff -u -b -r1.1 -r1.2
--- IdentityGenerator.java 8 Nov 2006 23:28:38 -0000 1.1
+++ IdentityGenerator.java 9 Nov 2006 01:26:30 -0000 1.2
@@ -1,9 +1,7 @@
package org.jboss.seam.security.acl;
-import java.io.Serializable;
-
/**
- * Identity generator
+ * Classes implementing this interface generate ACL Identities
*
* @author Shane Bryzak
*/
1.1 date: 2006/11/09 01:26:30; author: sbryzak2; state: Exp;jboss-seam/src/main/org/jboss/seam/security/acl/JPAIdentityGenerator.java
Index: JPAIdentityGenerator.java
===================================================================
package org.jboss.seam.security.acl;
import java.io.Serializable;
import java.lang.reflect.Field;
import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;
import java.lang.reflect.Modifier;
import java.util.HashMap;
import java.util.Map;
import javax.persistence.Id;
import org.jboss.seam.annotations.Name;
/**
* Generates ACL identities for JPA entity classes. The toString() method of
* the value returned by the identifier method (or field) should return an
* accurate representation of the identifier's value. In the case of composite
* identifiers, a recommended approach is to return the constituent values in
* delimited form.
*
* @author Shane Bryzak
*/
public class JPAIdentityGenerator implements IdentityGenerator
{
private enum IdentityType {field, method};
/**
* Used to cache identity metadata for a Class
*/
private class IdentityMetadata {
IdentityType idType;
String name;
Field identityField;
Method identityMethod;
public Object getIdentityValue(Object obj)
{
switch (idType)
{
case field:
try
{
return (Serializable) identityField.get(obj);
}
catch (IllegalAccessException ex) { // shouldn't occur
}
case method:
try
{
return identityMethod.invoke(obj);
}
catch (IllegalAccessException ex) {
// won't occur - already checked that method is public
}
catch (InvocationTargetException ex) {
}
default:
throw new IllegalStateException("Invalid identifier type");
}
}
}
/**
* Cache of identity metadata for Classes
*/
private Map<Class,IdentityMetadata> identityMeta = new HashMap<Class,IdentityMetadata>();
/**
* Returns a String representation of an object's identity. If the
* object has no identity, null is returned.
*
* @param obj Object The object to return an identity for.
* @return String The generated identity for the specified object, or null if
* the object has no identity.
*/
public String generateIdentity(Object obj)
{
IdentityMetadata meta = getIdentityMetadata(obj.getClass());
Object val = meta.getIdentityValue(obj);
return val == null ? null : String.format("%s:%s", meta.name, val.toString());
}
/**
* Returns the identity metadata for the specified Class.
*
* @param cls Class
* @return IdentityMetadata
*/
private IdentityMetadata getIdentityMetadata(Class cls)
{
if (!identityMeta.containsKey(cls))
{
synchronized(identityMeta)
{
if (!identityMeta.containsKey(cls))
{
IdentityMetadata meta = new IdentityMetadata();
// Check for a method annotated with @Id
for (Method m : cls.getMethods())
{
if (m.isAnnotationPresent(Id.class))
{
if (m.getParameterTypes().length > 0)
throw new IllegalArgumentException(String.format(
"Specified class [%s] has illegal identifier method - must accept no parameters.",
cls.getName()));
if (!Modifier.isPublic(m.getModifiers()))
throw new IllegalArgumentException(String.format(
"Specified class [%s] has illegal identifier method - must be public.",
cls.getName()));
meta.idType = IdentityType.method;
meta.identityMethod = m;
break;
}
}
// If there is no @Id method, check the fields
if (meta.identityMethod == null)
{
for (Field f : cls.getFields())
{
if (f.isAnnotationPresent(Id.class))
{
meta.idType = IdentityType.field;
meta.identityField = f;
// Have to do this so we can read the field value
f.setAccessible(true);
break;
}
}
}
if (meta.identityField == null && meta.identityMethod == null)
throw new IllegalArgumentException(String.format(
"Specified class [%s] has no identifier method or field.", cls.getName()));
if (cls.isAnnotationPresent(Name.class))
meta.name = ((Name) cls.getAnnotation(Name.class)).value();
else
meta.name = cls.getName();
identityMeta.put(cls, meta);
}
}
}
return identityMeta.get(cls);
}
}
More information about the jboss-cvs-commits
mailing list