001/*
002 * Copyright 2002-2017 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.cci.connection;
018
019import javax.naming.NamingException;
020import javax.naming.Reference;
021import javax.resource.ResourceException;
022import javax.resource.cci.Connection;
023import javax.resource.cci.ConnectionFactory;
024import javax.resource.cci.ConnectionSpec;
025import javax.resource.cci.RecordFactory;
026import javax.resource.cci.ResourceAdapterMetaData;
027
028import org.springframework.beans.factory.InitializingBean;
029import org.springframework.lang.Nullable;
030import org.springframework.util.Assert;
031
032/**
033 * CCI {@link ConnectionFactory} implementation that delegates all calls
034 * to a given target {@link ConnectionFactory}.
035 *
036 * <p>This class is meant to be subclassed, with subclasses overriding only
037 * those methods (such as {@link #getConnection()}) that should not simply
038 * delegate to the target {@link ConnectionFactory}.
039 *
040 * @author Juergen Hoeller
041 * @since 1.2
042 * @see #getConnection
043 */
044@SuppressWarnings("serial")
045public class DelegatingConnectionFactory implements ConnectionFactory, InitializingBean {
046
047        @Nullable
048        private ConnectionFactory targetConnectionFactory;
049
050
051        /**
052         * Set the target ConnectionFactory that this ConnectionFactory should delegate to.
053         */
054        public void setTargetConnectionFactory(@Nullable ConnectionFactory targetConnectionFactory) {
055                this.targetConnectionFactory = targetConnectionFactory;
056        }
057
058        /**
059         * Return the target ConnectionFactory that this ConnectionFactory should delegate to.
060         */
061        @Nullable
062        public ConnectionFactory getTargetConnectionFactory() {
063                return this.targetConnectionFactory;
064        }
065
066        /**
067         * Obtain the target {@code ConnectionFactory} for actual use (never {@code null}).
068         * @since 5.0
069         */
070        protected ConnectionFactory obtainTargetConnectionFactory() {
071                ConnectionFactory connectionFactory = getTargetConnectionFactory();
072                Assert.state(connectionFactory != null, "No 'targetConnectionFactory' set");
073                return connectionFactory;
074        }
075
076
077        @Override
078        public void afterPropertiesSet() {
079                if (getTargetConnectionFactory() == null) {
080                        throw new IllegalArgumentException("Property 'targetConnectionFactory' is required");
081                }
082        }
083
084
085        @Override
086        public Connection getConnection() throws ResourceException {
087                return obtainTargetConnectionFactory().getConnection();
088        }
089
090        @Override
091        public Connection getConnection(ConnectionSpec connectionSpec) throws ResourceException {
092                return obtainTargetConnectionFactory().getConnection(connectionSpec);
093        }
094
095        @Override
096        public RecordFactory getRecordFactory() throws ResourceException {
097                return obtainTargetConnectionFactory().getRecordFactory();
098        }
099
100        @Override
101        public ResourceAdapterMetaData getMetaData() throws ResourceException {
102                return obtainTargetConnectionFactory().getMetaData();
103        }
104
105        @Override
106        public Reference getReference() throws NamingException {
107                return obtainTargetConnectionFactory().getReference();
108        }
109
110        @Override
111        public void setReference(Reference reference) {
112                obtainTargetConnectionFactory().setReference(reference);
113        }
114
115}