001    /*
002     * Copyright 2002-2005 the original author or authors.
003     *
004     * Licensed under the Apache License, Version 2.0 (the "License");
005     * you may not use this file except in compliance with the License.
006     * You may obtain a copy of the License at
007     *
008     *      http://www.apache.org/licenses/LICENSE-2.0
009     *
010     * Unless required by applicable law or agreed to in writing, software
011     * distributed under the License is distributed on an "AS IS" BASIS,
012     * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
013     * See the License for the specific language governing permissions and
014     * limitations under the License.
015     */
016    
017    package org.jencks.interceptor;
018    
019    import org.aopalliance.intercept.MethodInterceptor;
020    import org.aopalliance.intercept.MethodInvocation;
021    import org.apache.geronimo.transaction.DefaultInstanceContext;
022    import org.apache.geronimo.transaction.InstanceContext;
023    import org.apache.geronimo.transaction.TrackedConnectionAssociator;
024    
025    import java.util.HashSet;
026    import java.util.Set;
027    
028    /**
029     * This servlet filter is used to enter in a transactional context
030     * automtically at every servlet call and exit of it when the response
031     * is sent back to the client.
032     *
033     * @author Thierry Templier
034     * @see TrackedConnectionAssociator#enter(InstanceContext)
035     * @see TrackedConnectionAssociator#exit(InstanceContext)
036     * @see InstanceContext
037     * @see DefaultInstanceContext
038     */
039    public class TransactionContextInterceptor implements MethodInterceptor {
040    
041        private TrackedConnectionAssociator associator;
042    
043        /**
044         * This is the central method of the filter which allows the
045         * request to enter in a transactionnal context and exit when
046         * the request is sent back to the client.
047         *
048         * @see #enterContext(Set, Set)
049         * @see #exitContext(InstanceContext)
050         */
051        public Object invoke(MethodInvocation invocation) throws Throwable {
052            Set unshareableResources = new HashSet();
053            Set applicationManagedSecurityResources = new HashSet();
054            InstanceContext context = associator.enter(
055                    new DefaultInstanceContext(unshareableResources, applicationManagedSecurityResources));
056            Object returnValue = invocation.proceed();
057            associator.exit(context);
058            return returnValue;
059        }
060    
061        /**
062         * Set the TrackedConnectionAssociator instance to allow the bean
063         * to enter and exit a transactional context.
064         */
065        public void setAssociator(TrackedConnectionAssociator associator) {
066            this.associator = associator;
067        }
068    
069    }