KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > jbpm > identity > hibernate > PermissionUserType


1 package org.jbpm.identity.hibernate;
2
3 import java.io.Serializable JavaDoc;
4 import java.lang.reflect.Constructor JavaDoc;
5 import java.security.Permission JavaDoc;
6 import java.sql.PreparedStatement JavaDoc;
7 import java.sql.ResultSet JavaDoc;
8 import java.sql.SQLException JavaDoc;
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 JavaDoc[] PROPERTY_NAMES = new String JavaDoc[]{"class", "name", "actions"};
19   public String JavaDoc[] 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 JavaDoc getPropertyValue(Object JavaDoc component, int property) throws HibernateException {
29     Permission JavaDoc permission = (Permission JavaDoc) 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 JavaDoc("illegal permission property '"+property+"'");
38     }
39   }
40
41   public void setPropertyValue(Object JavaDoc arg0, int arg1, Object JavaDoc arg2) throws HibernateException {
42     throw new UnsupportedOperationException JavaDoc("setting properties on a permission is not allowed");
43   }
44
45   public Class JavaDoc returnedClass() {
46     return Permission JavaDoc.class;
47   }
48
49   public boolean equals(Object JavaDoc left, Object JavaDoc right) throws HibernateException {
50     return left.equals(right);
51   }
52
53   public int hashCode(Object JavaDoc permission) throws HibernateException {
54     return permission.hashCode();
55   }
56   
57   private static final Class JavaDoc[] NAME_ACTIOS_CONSTRUCTOR_PARAMETER_TYPES = new Class JavaDoc[]{String JavaDoc.class, String JavaDoc.class};
58   public Object JavaDoc nullSafeGet(ResultSet JavaDoc resultSet, String JavaDoc[] names, SessionImplementor session, Object JavaDoc owner) throws HibernateException, SQLException JavaDoc {
59     Object JavaDoc permission = null;
60     String JavaDoc className = resultSet.getString(names[0]);
61     String JavaDoc name = resultSet.getString(names[1]);
62     String JavaDoc actions = resultSet.getString(names[2]);
63     
64     try {
65       // TODO optimize performance by caching the constructors
66
Class JavaDoc permissionClass = PermissionUserType.class.getClassLoader().loadClass(className);
67       Constructor JavaDoc constructor = permissionClass.getDeclaredConstructor(NAME_ACTIOS_CONSTRUCTOR_PARAMETER_TYPES);
68       permission = constructor.newInstance(new Object JavaDoc[]{name, actions});
69     } catch (Exception JavaDoc 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 JavaDoc preparedStatement, Object JavaDoc value, int index, SessionImplementor session) throws HibernateException, SQLException JavaDoc {
77     Permission JavaDoc permission = (Permission JavaDoc) 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 JavaDoc deepCopy(Object JavaDoc permission) throws HibernateException {
84     return permission;
85   }
86
87   public boolean isMutable() {
88     return false;
89   }
90
91   public Serializable JavaDoc disassemble(Object JavaDoc value, SessionImplementor session) throws HibernateException {
92     return (Serializable JavaDoc) value;
93   }
94
95   public Object JavaDoc assemble(Serializable JavaDoc cached, SessionImplementor session, Object JavaDoc owner) throws HibernateException {
96     return cached;
97   }
98
99   public Object JavaDoc replace(Object JavaDoc original, Object JavaDoc target, SessionImplementor session, Object JavaDoc owner) throws HibernateException {
100     return original;
101   }
102 }
103
Popular Tags