View Javadoc

1   /*
2    * Copyright 2002-2005 the original author or authors.
3    *
4    * Licensed under the Apache License, Version 2.0 (the "License");
5    * you may not use this file except in compliance with the License.
6    * You may obtain a copy of the License at
7    *
8    *      http://www.apache.org/licenses/LICENSE-2.0
9    *
10   * Unless required by applicable law or agreed to in writing, software
11   * distributed under the License is distributed on an "AS IS" BASIS,
12   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13   * See the License for the specific language governing permissions and
14   * limitations under the License.
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  }