1 package org.jbpm.bpel.messager; 2 3 import javax.jms.JMSException ; 4 import javax.jms.Session ; 5 6 import org.apache.commons.logging.Log; 7 import org.apache.commons.logging.LogFactory; 8 9 import org.jbpm.db.JbpmSession; 10 11 15 public class MessagerSession { 16 17 private Session jmsSession; 18 private MessagerService messagerService; 19 private JbpmSession jbpmSession; 20 21 private static ThreadLocal sessionThreadLocal = new ThreadLocal (); 22 private static final Log log = LogFactory.getLog(MessagerSession.class); 23 24 public MessagerSession(Session jmsSession, MessagerService messagerService) { 25 this.jmsSession = jmsSession; 26 this.messagerService = messagerService; 27 } 28 29 public Session getJmsSession() { 30 return jmsSession; 31 } 32 33 public MessagerService getMessagerService() { 34 return messagerService; 35 } 36 37 public JbpmSession getJbpmSession() { 38 return jbpmSession; 39 } 40 41 public void setJbpmSession(JbpmSession jbpmSession) { 42 this.jbpmSession = jbpmSession; 43 } 44 45 public void beginTransaction() { 46 if (jbpmSession != null) jbpmSession.beginTransaction(); 47 } 48 49 public void commitTransaction() { 50 try { 51 jmsSession.commit(); 52 } 53 catch (JMSException e) { 54 log.error(e); 55 throw new RuntimeException ("could not commit jms session", e); 56 } 57 finally { 58 if (jbpmSession != null) jbpmSession.commitTransaction(); 59 } 60 } 61 62 public void rollbackTransaction() { 63 try { 64 jmsSession.rollback(); 65 } 66 catch (JMSException e) { 67 log.error(e); 68 throw new RuntimeException ("could not rollback jms session", e); 69 } 70 finally { 71 if (jbpmSession != null) jbpmSession.rollbackTransaction(); 72 } 73 } 74 75 public void close() { 76 try { 77 jmsSession.close(); 78 } 79 catch (JMSException e) { 80 log.warn(e); 81 throw new RuntimeException ("could not close jms session", e); 82 } 83 finally { 84 if (jbpmSession != null) jbpmSession.close(); 85 } 86 } 87 88 protected void finalize() throws Throwable { 89 close(); 90 } 91 92 public void setAsCurrentSession() { 93 Object currentSession = sessionThreadLocal.get(); 94 if (currentSession == null) { 95 sessionThreadLocal.set(this); 96 } 97 else if (currentSession != this) { 98 throw new IllegalStateException ("a session already exists for the current thread"); 99 } 100 } 101 102 public static MessagerSession getCurrentSession() { 103 return (MessagerSession) sessionThreadLocal.get(); 104 } 105 106 public static MessagerSession unsetCurrentSession() { 107 MessagerSession currentSession = (MessagerSession) sessionThreadLocal.get(); 108 if (currentSession != null) { 109 sessionThreadLocal.set(null); 110 } 111 return currentSession; 112 } 113 } 114 | Popular Tags |