1 package org.jbpm.identity.hibernate; 2 3 import java.io.Serializable ; 4 import java.lang.reflect.Constructor ; 5 import java.security.Permission ; 6 import java.sql.PreparedStatement ; 7 import java.sql.ResultSet ; 8 import java.sql.SQLException ; 9 10 import org.hibernate.Hibernate; 11 import org.hibernate.HibernateException; 12 import org.hibernate.engine.SessionImplementor; 13 import org.hibernate.type.Type; 14 import org.hibernate.usertype.CompositeUserType; 15 16 public class PermissionUserType implements CompositeUserType { 17 18 private static final String [] PROPERTY_NAMES = new String []{"class", "name", "actions"}; 19 public String [] getPropertyNames() { 20 return PROPERTY_NAMES; 21 } 22 23 private static final Type[] PROPERTY_TYPES = new Type[]{Hibernate.STRING, Hibernate.STRING, Hibernate.STRING}; 24 public Type[] getPropertyTypes() { 25 return PROPERTY_TYPES; 26 } 27 28 public Object getPropertyValue(Object component, int property) throws HibernateException { 29 Permission permission = (Permission ) component; 30 if (property==0) { 31 return permission.getClass().getName(); 32 } else if (property==1) { 33 return permission.getName(); 34 } else if (property==2) { 35 return permission.getActions(); 36 } else { 37 throw new IllegalArgumentException ("illegal permission property '"+property+"'"); 38 } 39 } 40 41 public void setPropertyValue(Object arg0, int arg1, Object arg2) throws HibernateException { 42 throw new UnsupportedOperationException ("setting properties on a permission is not allowed"); 43 } 44 45 public Class returnedClass() { 46 return Permission .class; 47 } 48 49 public boolean equals(Object left, Object right) throws HibernateException { 50 return left.equals(right); 51 } 52 53 public int hashCode(Object permission) throws HibernateException { 54 return permission.hashCode(); 55 } 56 57 private static final Class [] NAME_ACTIOS_CONSTRUCTOR_PARAMETER_TYPES = new Class []{String .class, String .class}; 58 public Object nullSafeGet(ResultSet resultSet, String [] names, SessionImplementor session, Object owner) throws HibernateException, SQLException { 59 Object permission = null; 60 String className = resultSet.getString(names[0]); 61 String name = resultSet.getString(names[1]); 62 String actions = resultSet.getString(names[2]); 63 64 try { 65 Class permissionClass = PermissionUserType.class.getClassLoader().loadClass(className); 67 Constructor constructor = permissionClass.getDeclaredConstructor(NAME_ACTIOS_CONSTRUCTOR_PARAMETER_TYPES); 68 permission = constructor.newInstance(new Object []{name, actions}); 69 } catch (Exception e) { 70 throw new HibernateException("couldn't create permission from database record ["+className+"|"+name+"|"+actions+"]. Does the permission class have a (String name,String actions) constructor ?", e); 71 } 72 73 return permission; 74 } 75 76 public void nullSafeSet(PreparedStatement preparedStatement, Object value, int index, SessionImplementor session) throws HibernateException, SQLException { 77 Permission permission = (Permission ) value; 78 preparedStatement.setString(index, permission.getClass().getName()); 79 preparedStatement.setString(index+1, permission.getName()); 80 preparedStatement.setString(index+2, permission.getActions()); 81 } 82 83 public Object deepCopy(Object permission) throws HibernateException { 84 return permission; 85 } 86 87 public boolean isMutable() { 88 return false; 89 } 90 91 public Serializable disassemble(Object value, SessionImplementor session) throws HibernateException { 92 return (Serializable ) value; 93 } 94 95 public Object assemble(Serializable cached, SessionImplementor session, Object owner) throws HibernateException { 96 return cached; 97 } 98 99 public Object replace(Object original, Object target, SessionImplementor session, Object owner) throws HibernateException { 100 return original; 101 } 102 } 103 | Popular Tags |