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.aopalliance.intercept.MethodInterceptor;
20  import org.aopalliance.intercept.MethodInvocation;
21  import org.apache.geronimo.transaction.DefaultInstanceContext;
22  import org.apache.geronimo.transaction.InstanceContext;
23  import org.apache.geronimo.transaction.TrackedConnectionAssociator;
24  
25  import java.util.HashSet;
26  import java.util.Set;
27  
28  /***
29   * This servlet filter is used to enter in a transactional context
30   * automtically at every servlet call and exit of it when the response
31   * is sent back to the client.
32   *
33   * @author Thierry Templier
34   * @see TrackedConnectionAssociator#enter(InstanceContext)
35   * @see TrackedConnectionAssociator#exit(InstanceContext)
36   * @see InstanceContext
37   * @see DefaultInstanceContext
38   */
39  public class TransactionContextInterceptor implements MethodInterceptor {
40  
41      private TrackedConnectionAssociator associator;
42  
43      /***
44       * This is the central method of the filter which allows the
45       * request to enter in a transactionnal context and exit when
46       * the request is sent back to the client.
47       *
48       * @see #enterContext(Set, Set)
49       * @see #exitContext(InstanceContext)
50       */
51      public Object invoke(MethodInvocation invocation) throws Throwable {
52          Set unshareableResources = new HashSet();
53          Set applicationManagedSecurityResources = new HashSet();
54          InstanceContext context = associator.enter(
55                  new DefaultInstanceContext(unshareableResources, applicationManagedSecurityResources));
56          Object returnValue = invocation.proceed();
57          associator.exit(context);
58          return returnValue;
59      }
60  
61      /***
62       * Set the TrackedConnectionAssociator instance to allow the bean
63       * to enter and exit a transactional context.
64       */
65      public void setAssociator(TrackedConnectionAssociator associator) {
66          this.associator = associator;
67      }
68  
69  }