KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > jbpm > bpel > messager > MessagerSession


1 package org.jbpm.bpel.messager;
2
3 import javax.jms.JMSException JavaDoc;
4 import javax.jms.Session JavaDoc;
5
6 import org.apache.commons.logging.Log;
7 import org.apache.commons.logging.LogFactory;
8
9 import org.jbpm.db.JbpmSession;
10
11 /**
12  * @author Alejandro Guízar
13  * @version $Revision: 1.2 $ $Date: 2005/05/07 00:03:46 $
14  */

15 public class MessagerSession {
16   
17   private Session JavaDoc jmsSession;
18   private MessagerService messagerService;
19   private JbpmSession jbpmSession;
20   
21   private static ThreadLocal JavaDoc sessionThreadLocal = new ThreadLocal JavaDoc();
22   private static final Log log = LogFactory.getLog(MessagerSession.class);
23
24   public MessagerSession(Session JavaDoc jmsSession, MessagerService messagerService) {
25     this.jmsSession = jmsSession;
26     this.messagerService = messagerService;
27   }
28   
29   public Session JavaDoc 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 JavaDoc e) {
54       log.error(e);
55       throw new RuntimeException JavaDoc("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 JavaDoc e) {
67       log.error(e);
68       throw new RuntimeException JavaDoc("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 JavaDoc e) {
80       log.warn(e);
81       throw new RuntimeException JavaDoc("could not close jms session", e);
82     }
83     finally {
84       if (jbpmSession != null) jbpmSession.close();
85     }
86   }
87   
88   protected void finalize() throws Throwable JavaDoc {
89     close();
90   }
91   
92   public void setAsCurrentSession() {
93     Object JavaDoc currentSession = sessionThreadLocal.get();
94     if (currentSession == null) {
95       sessionThreadLocal.set(this);
96     }
97     else if (currentSession != this) {
98       throw new IllegalStateException JavaDoc("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