1,106 → 1,106 |
package ak.hostcaptain.core.servlet; |
|
import java.util.List; |
import java.util.ArrayList; |
import java.util.StringTokenizer; |
import java.io.IOException; |
import java.net.URLEncoder; |
import java.security.Principal; |
import java.security.AccessControlException; |
|
import javax.servlet.Filter; |
import javax.servlet.FilterChain; |
import javax.servlet.FilterConfig; |
import javax.servlet.ServletException; |
import javax.servlet.ServletRequest; |
import javax.servlet.ServletResponse; |
import javax.servlet.http.HttpSession; |
import javax.servlet.http.HttpServletRequest; |
import javax.servlet.http.HttpServletResponse; |
|
import org.apache.log4j.Logger; |
|
import net.sf.hibernate.HibernateException; |
|
import ak.hostcaptain.util.HibernateUtil; |
import ak.hostcaptain.util.ModelException; |
|
public class HibernateFilter |
implements Filter |
{ |
private static final Logger logger = Logger.getLogger(HibernateFilter.class); |
|
private FilterConfig filterConfig; |
|
public void init(FilterConfig filterConfig) |
throws ServletException |
{ |
// get config |
this.filterConfig = filterConfig; |
|
if(filterConfig != null) { |
// register hibernate classes |
String toRegister = filterConfig.getInitParameter("register"); |
if(toRegister != null) { |
String[] registers = toRegister.split("\\s*;\\s*"); |
|
for(int i = 0; i < registers.length; i++) { |
try { |
String name = registers[i].trim(); |
if(name.equals("")) continue; |
|
Class cl = Class.forName(name); |
} |
catch(Exception ex) { |
logger.error("cannot register class", ex); |
} |
} |
} |
} |
} |
|
public void doFilter(ServletRequest request, ServletResponse response, |
FilterChain chain) |
throws IOException, ServletException |
{ |
try { |
logger.info("begin transaction"); |
HibernateUtil.beginTransaction(); |
|
chain.doFilter(request, response); |
|
if(HibernateUtil.isTransactionOpen()) { |
logger.info("commit transaction"); |
HibernateUtil.commitTransaction(); |
} |
} |
catch(Exception ex) { |
logger.error("exception by program execution", ex); |
try { |
if(HibernateUtil.isTransactionOpen()) { |
logger.info("rollback transaction"); |
HibernateUtil.rollbackTransaction(); |
} |
} |
catch(Exception ex2) { |
logger.error("cannot rollback transaction", ex2); |
} |
|
if(ex instanceof ServletException) |
throw (ServletException)ex; |
else |
throw new ServletException("Internal server error"); |
} |
|
try { |
HibernateUtil.closeSession(); |
} |
catch(Exception ex) { |
logger.error("cannot close session", ex); |
} |
} |
|
public void destroy() |
{ |
} |
} |
package ak.hostcaptain.core.servlet; |
|
import java.util.List; |
import java.util.ArrayList; |
import java.util.StringTokenizer; |
import java.io.IOException; |
import java.net.URLEncoder; |
import java.security.Principal; |
import java.security.AccessControlException; |
|
import javax.servlet.Filter; |
import javax.servlet.FilterChain; |
import javax.servlet.FilterConfig; |
import javax.servlet.ServletException; |
import javax.servlet.ServletRequest; |
import javax.servlet.ServletResponse; |
import javax.servlet.http.HttpSession; |
import javax.servlet.http.HttpServletRequest; |
import javax.servlet.http.HttpServletResponse; |
|
import org.apache.log4j.Logger; |
|
import net.sf.hibernate.HibernateException; |
|
import ak.hostcaptain.util.HibernateUtil; |
import ak.hostcaptain.util.ModelException; |
|
public class HibernateFilter |
implements Filter |
{ |
private static final Logger logger = Logger.getLogger(HibernateFilter.class); |
|
private FilterConfig filterConfig; |
|
public void init(FilterConfig filterConfig) |
throws ServletException |
{ |
// get config |
this.filterConfig = filterConfig; |
|
if(filterConfig != null) { |
// register hibernate classes |
String toRegister = filterConfig.getInitParameter("register"); |
if(toRegister != null) { |
String[] registers = toRegister.split("\\s*;\\s*"); |
|
for(int i = 0; i < registers.length; i++) { |
try { |
String name = registers[i].trim(); |
if(name.equals("")) continue; |
|
Class cl = Class.forName(name); |
} |
catch(Exception ex) { |
logger.error("cannot register class", ex); |
} |
} |
} |
} |
} |
|
public void doFilter(ServletRequest request, ServletResponse response, |
FilterChain chain) |
throws IOException, ServletException |
{ |
try { |
logger.info("begin transaction"); |
HibernateUtil.beginTransaction(); |
|
chain.doFilter(request, response); |
|
if(HibernateUtil.isTransactionOpen()) { |
logger.info("commit transaction"); |
HibernateUtil.commitTransaction(); |
} |
} |
catch(Exception ex) { |
logger.error("exception by program execution", ex); |
try { |
if(HibernateUtil.isTransactionOpen()) { |
logger.info("rollback transaction"); |
HibernateUtil.rollbackTransaction(); |
} |
} |
catch(Exception ex2) { |
logger.error("cannot rollback transaction", ex2); |
} |
|
if(ex instanceof ServletException) |
throw (ServletException)ex; |
else |
throw new ServletException("Internal server error"); |
} |
|
try { |
HibernateUtil.closeSession(); |
} |
catch(Exception ex) { |
logger.error("cannot close session", ex); |
} |
} |
|
public void destroy() |
{ |
} |
} |