001/*
002 * Copyright 2002-2012 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 *      https://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
017package org.springframework.jca.context;
018
019import javax.resource.spi.BootstrapContext;
020import javax.resource.spi.work.WorkManager;
021
022import org.springframework.beans.BeansException;
023import org.springframework.beans.factory.ObjectFactory;
024import org.springframework.beans.factory.config.ConfigurableListableBeanFactory;
025import org.springframework.context.support.GenericApplicationContext;
026import org.springframework.util.Assert;
027
028/**
029 * {@link org.springframework.context.ApplicationContext} implementation
030 * for a JCA ResourceAdapter. Needs to be initialized with the JCA
031 * {@link javax.resource.spi.BootstrapContext}, passing it on to
032 * Spring-managed beans that implement {@link BootstrapContextAware}.
033 *
034 * @author Juergen Hoeller
035 * @since 2.5
036 * @see SpringContextResourceAdapter
037 * @see BootstrapContextAware
038 */
039public class ResourceAdapterApplicationContext extends GenericApplicationContext {
040
041        private final BootstrapContext bootstrapContext;
042
043
044        /**
045         * Create a new ResourceAdapterApplicationContext for the given BootstrapContext.
046         * @param bootstrapContext the JCA BootstrapContext that the ResourceAdapter
047         * has been started with
048         */
049        public ResourceAdapterApplicationContext(BootstrapContext bootstrapContext) {
050                Assert.notNull(bootstrapContext, "BootstrapContext must not be null");
051                this.bootstrapContext = bootstrapContext;
052        }
053
054
055        @Override
056        protected void postProcessBeanFactory(ConfigurableListableBeanFactory beanFactory) throws BeansException {
057                beanFactory.addBeanPostProcessor(new BootstrapContextAwareProcessor(this.bootstrapContext));
058                beanFactory.ignoreDependencyInterface(BootstrapContextAware.class);
059                beanFactory.registerResolvableDependency(BootstrapContext.class, this.bootstrapContext);
060
061                // JCA WorkManager resolved lazily - may not be available.
062                beanFactory.registerResolvableDependency(WorkManager.class, new ObjectFactory<WorkManager>() {
063                        @Override
064                        public WorkManager getObject() {
065                                return bootstrapContext.getWorkManager();
066                        }
067                });
068        }
069
070}