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.apache.commons.logging.Log; 020 import org.apache.commons.logging.LogFactory; 021 import org.apache.geronimo.transaction.DefaultInstanceContext; 022 import org.apache.geronimo.transaction.InstanceContext; 023 import org.apache.geronimo.transaction.TrackedConnectionAssociator; 024 import org.springframework.beans.factory.DisposableBean; 025 import org.springframework.beans.factory.InitializingBean; 026 027 import java.util.HashSet; 028 import java.util.Set; 029 030 /** 031 * This bean is used to configure a transactional context for 032 * applications that work only within a single thread. 033 * <p/> 034 * At the beginning, it makes enter the application in a new 035 * context and, at the end, exit of it. 036 * 037 * @author Thierry Templier 038 * @see TrackedConnectionAssociator#enter(InstanceContext) 039 * @see TrackedConnectionAssociator#exit(InstanceContext) 040 * @see InstanceContext 041 * @see DefaultInstanceContext 042 */ 043 public class TransactionContextInitializer implements InitializingBean, DisposableBean { 044 045 private TrackedConnectionAssociator associator; 046 private InstanceContext oldContext; 047 048 protected transient Log logger = LogFactory.getLog(getClass()); 049 050 public void afterPropertiesSet() throws Exception { 051 Set unshareableResources = new HashSet(); 052 Set applicationManagedSecurityResources = new HashSet(); 053 this.oldContext = associator.enter( 054 new DefaultInstanceContext(unshareableResources, applicationManagedSecurityResources)); 055 logger.info("Geronimo transaction context set."); 056 } 057 058 public void destroy() throws Exception { 059 associator.exit(oldContext); 060 logger.info("Geronimo transaction context unset."); 061 } 062 063 /** 064 * Set the TrackedConnectionAssociator instance to allow the bean 065 * to enter and exit a transactional context. 066 */ 067 public void setAssociator(TrackedConnectionAssociator associator) { 068 this.associator = associator; 069 } 070 071 }