KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > hibernate > ce > auction > persistence > audit > AuditLogInterceptor


1 package org.hibernate.ce.auction.persistence.audit;
2
3 import org.hibernate.*;
4 import org.hibernate.type.Type;
5
6 import java.io.Serializable JavaDoc;
7 import java.util.*;
8
9 import org.hibernate.ce.auction.model.*;
10 import org.apache.commons.logging.*;
11
12 public class AuditLogInterceptor implements Interceptor {
13
14     private static Log log = LogFactory.getLog(AuditLogInterceptor.class);
15
16     private Session session;
17     private Long JavaDoc userId;
18
19     private Set inserts = new HashSet();
20     private Set updates = new HashSet();
21
22     public void setSession(Session session) {
23         this.session=session;
24     }
25     public void setUserId(Long JavaDoc userId) {
26         this.userId=userId;
27     }
28
29     public boolean onSave(Object JavaDoc entity,
30                          Serializable JavaDoc id,
31                          Object JavaDoc[] state,
32                          String JavaDoc[] propertyNames,
33                          Type[] types)
34             throws CallbackException {
35
36         if (entity instanceof Auditable)
37             inserts.add(entity);
38
39         return false;
40     }
41
42     public boolean onFlushDirty(Object JavaDoc entity,
43                                 Serializable JavaDoc id,
44                                 Object JavaDoc[] currentState,
45                                 Object JavaDoc[] previousState,
46                                 String JavaDoc[] propertyNames,
47                                 Type[] types)
48             throws CallbackException {
49
50         if (entity instanceof Auditable)
51             updates.add(entity);
52
53         return false;
54     }
55
56     public boolean onLoad(Object JavaDoc o, Serializable JavaDoc serializable, Object JavaDoc[] objects, String JavaDoc[] strings, Type[] types) throws CallbackException {
57         return false;
58     }
59
60     public void onDelete(Object JavaDoc o, Serializable JavaDoc serializable, Object JavaDoc[] objects, String JavaDoc[] strings, Type[] types) throws CallbackException {
61     }
62
63     public void preFlush(Iterator iterator) throws CallbackException {
64     }
65
66     public void postFlush(Iterator iterator) throws CallbackException {
67         try {
68             for (Iterator it = inserts.iterator(); it.hasNext();) {
69                 Auditable entity = (Auditable) it.next();
70                 log.debug("Intercepted creation of : " + entity);
71                 AuditLog.logEvent("create",
72                                   entity,
73                                   userId,
74                                   session.connection());
75             }
76             for (Iterator it = updates.iterator(); it.hasNext();) {
77                 Auditable entity = (Auditable) it.next();
78                 log.debug("Intercepted modification of : " + entity);
79                 AuditLog.logEvent("update",
80                                   entity,
81                                   userId,
82                                   session.connection());
83             }
84         } catch (HibernateException ex) {
85             throw new CallbackException(ex);
86         } finally {
87             inserts.clear();
88             updates.clear();
89         }
90     }
91
92     public int[] findDirty(Object JavaDoc o, Serializable JavaDoc serializable, Object JavaDoc[] objects, Object JavaDoc[] objects1, String JavaDoc[] strings, Type[] types) {
93         return null;
94     }
95
96     public Object JavaDoc instantiate(Class JavaDoc aClass, Serializable JavaDoc serializable) throws CallbackException {
97         return null;
98     }
99
100     public Boolean JavaDoc isTransient(Object JavaDoc o) {
101         return null;
102     }
103
104     public Object JavaDoc instantiate(String JavaDoc s, Serializable JavaDoc serializable) throws CallbackException {
105         return null;
106     }
107
108     public String JavaDoc getEntityName(Object JavaDoc o) throws CallbackException {
109         return null;
110     }
111
112     public Object JavaDoc getEntity(String JavaDoc s, Serializable JavaDoc serializable) throws CallbackException {
113         return null;
114     }
115
116     public Object JavaDoc instantiate(String JavaDoc entityName, EntityMode entityMode, Serializable JavaDoc id) throws CallbackException {
117         return null; //To change body of implemented methods use File | Settings | File Templates.
118
}
119
120     public void afterTransactionBegin(Transaction tx) {
121         //To change body of implemented methods use File | Settings | File Templates.
122
}
123
124     public void beforeTransactionCompletion(Transaction tx) {
125         //To change body of implemented methods use File | Settings | File Templates.
126
}
127
128     public void afterTransactionCompletion(Transaction tx) {
129         //To change body of implemented methods use File | Settings | File Templates.
130
}
131 }
132
Popular Tags