KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > jbpm > webapp > filter > PersistenceFilter


1 package org.jbpm.webapp.filter;
2
3 import java.io.IOException JavaDoc;
4
5 import javax.servlet.Filter JavaDoc;
6 import javax.servlet.FilterChain JavaDoc;
7 import javax.servlet.FilterConfig JavaDoc;
8 import javax.servlet.ServletException JavaDoc;
9 import javax.servlet.ServletRequest JavaDoc;
10 import javax.servlet.ServletResponse JavaDoc;
11
12 import org.jbpm.webapp.context.Context;
13 import org.jbpm.webapp.context.PersistenceContext;
14
15 public class PersistenceFilter implements Filter JavaDoc {
16   
17   public void doFilter(ServletRequest JavaDoc servletRequest, ServletResponse JavaDoc servletResponse, FilterChain JavaDoc filterChain) throws IOException JavaDoc, ServletException JavaDoc {
18     // let's suppose there will be an exception and change this value if we know that is not the case
19
boolean exceptionOccurred = true;
20     
21     // fetch a persistence context. it will be created cause this it the first access.
22
PersistenceContext persistenceContext = Context.getPersistenceContext();
23     try {
24       // begin a transaction
25
persistenceContext.beginTransaction();
26
27       // pass to the next in the chain
28
filterChain.doFilter(servletRequest, servletResponse);
29       
30       // if there is an exception, this line is not executed
31
exceptionOccurred = false;
32       
33     } finally {
34       // if an exception is being thrown,
35
if (exceptionOccurred) {
36         // mark the transaction for rollback
37
persistenceContext.setRollbackOnly();
38       }
39       // commit or rollback depending on the isRollbackOnly property
40
persistenceContext.endTransaction();
41     }
42   }
43
44   public void init(FilterConfig JavaDoc filterConfig) throws ServletException JavaDoc {
45   }
46
47   public void destroy() {
48   }
49   
50   // private static final Log log = LogFactory.getLog(PersistenceFilter.class);
51
}
52
Popular Tags