1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17 package org.jencks.interceptor;
18
19 import org.apache.commons.logging.Log;
20 import org.apache.commons.logging.LogFactory;
21 import org.apache.geronimo.transaction.DefaultInstanceContext;
22 import org.apache.geronimo.transaction.InstanceContext;
23 import org.apache.geronimo.transaction.TrackedConnectionAssociator;
24 import org.springframework.beans.factory.DisposableBean;
25 import org.springframework.beans.factory.InitializingBean;
26
27 import java.util.HashSet;
28 import java.util.Set;
29
30 /***
31 * This bean is used to configure a transactional context for
32 * applications that work only within a single thread.
33 * <p/>
34 * At the beginning, it makes enter the application in a new
35 * context and, at the end, exit of it.
36 *
37 * @author Thierry Templier
38 * @see TrackedConnectionAssociator#enter(InstanceContext)
39 * @see TrackedConnectionAssociator#exit(InstanceContext)
40 * @see InstanceContext
41 * @see DefaultInstanceContext
42 */
43 public class TransactionContextInitializer implements InitializingBean, DisposableBean {
44
45 private TrackedConnectionAssociator associator;
46 private InstanceContext oldContext;
47
48 protected transient Log logger = LogFactory.getLog(getClass());
49
50 public void afterPropertiesSet() throws Exception {
51 Set unshareableResources = new HashSet();
52 Set applicationManagedSecurityResources = new HashSet();
53 this.oldContext = associator.enter(
54 new DefaultInstanceContext(unshareableResources, applicationManagedSecurityResources));
55 logger.info("Geronimo transaction context set.");
56 }
57
58 public void destroy() throws Exception {
59 associator.exit(oldContext);
60 logger.info("Geronimo transaction context unset.");
61 }
62
63 /***
64 * Set the TrackedConnectionAssociator instance to allow the bean
65 * to enter and exit a transactional context.
66 */
67 public void setAssociator(TrackedConnectionAssociator associator) {
68 this.associator = associator;
69 }
70
71 }