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.support;
018
019import java.util.Timer;
020import javax.resource.spi.BootstrapContext;
021import javax.resource.spi.UnavailableException;
022import javax.resource.spi.XATerminator;
023import javax.resource.spi.work.WorkManager;
024
025/**
026 * Simple implementation of the JCA 1.5 {@link javax.resource.spi.BootstrapContext}
027 * interface, used for bootstrapping a JCA ResourceAdapter in a local environment.
028 *
029 * <p>Delegates to the given WorkManager and XATerminator, if any. Creates simple
030 * local instances of {@code java.util.Timer}.
031 *
032 * @author Juergen Hoeller
033 * @since 2.0.3
034 * @see javax.resource.spi.ResourceAdapter#start(javax.resource.spi.BootstrapContext)
035 * @see ResourceAdapterFactoryBean
036 */
037public class SimpleBootstrapContext implements BootstrapContext {
038
039        private WorkManager workManager;
040
041        private XATerminator xaTerminator;
042
043
044        /**
045         * Create a new SimpleBootstrapContext for the given WorkManager,
046         * with no XATerminator available.
047         * @param workManager the JCA WorkManager to use (may be {@code null})
048         */
049        public SimpleBootstrapContext(WorkManager workManager) {
050                this.workManager = workManager;
051        }
052
053        /**
054         * Create a new SimpleBootstrapContext for the given WorkManager and XATerminator.
055         * @param workManager the JCA WorkManager to use (may be {@code null})
056         * @param xaTerminator the JCA XATerminator to use (may be {@code null})
057         */
058        public SimpleBootstrapContext(WorkManager workManager, XATerminator xaTerminator) {
059                this.workManager = workManager;
060                this.xaTerminator = xaTerminator;
061        }
062
063
064        @Override
065        public WorkManager getWorkManager() {
066                if (this.workManager == null) {
067                        throw new IllegalStateException("No WorkManager available");
068                }
069                return this.workManager;
070        }
071
072        @Override
073        public XATerminator getXATerminator() {
074                return this.xaTerminator;
075        }
076
077        @Override
078        public Timer createTimer() throws UnavailableException {
079                return new Timer();
080        }
081
082}