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.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;
029
030/**
031 * CCI {@link ConnectionFactory} implementation that delegates all calls
032 * to a given target {@link ConnectionFactory}.
033 *
034 * <p>This class is meant to be subclassed, with subclasses overriding only
035 * those methods (such as {@link #getConnection()}) that should not simply
036 * delegate to the target {@link ConnectionFactory}.
037 *
038 * @author Juergen Hoeller
039 * @since 1.2
040 * @see #getConnection
041 */
042@SuppressWarnings("serial")
043public class DelegatingConnectionFactory implements ConnectionFactory, InitializingBean {
044
045        private ConnectionFactory targetConnectionFactory;
046
047
048        /**
049         * Set the target ConnectionFactory that this ConnectionFactory should delegate to.
050         */
051        public void setTargetConnectionFactory(ConnectionFactory targetConnectionFactory) {
052                this.targetConnectionFactory = targetConnectionFactory;
053        }
054
055        /**
056         * Return the target ConnectionFactory that this ConnectionFactory should delegate to.
057         */
058        public ConnectionFactory getTargetConnectionFactory() {
059                return this.targetConnectionFactory;
060        }
061
062
063        @Override
064        public void afterPropertiesSet() {
065                if (getTargetConnectionFactory() == null) {
066                        throw new IllegalArgumentException("Property 'targetConnectionFactory' is required");
067                }
068        }
069
070
071        @Override
072        public Connection getConnection() throws ResourceException {
073                return getTargetConnectionFactory().getConnection();
074        }
075
076        @Override
077        public Connection getConnection(ConnectionSpec connectionSpec) throws ResourceException {
078                return getTargetConnectionFactory().getConnection(connectionSpec);
079        }
080
081        @Override
082        public RecordFactory getRecordFactory() throws ResourceException {
083                return getTargetConnectionFactory().getRecordFactory();
084        }
085
086        @Override
087        public ResourceAdapterMetaData getMetaData() throws ResourceException {
088                return getTargetConnectionFactory().getMetaData();
089        }
090
091        @Override
092        public Reference getReference() throws NamingException {
093                return getTargetConnectionFactory().getReference();
094        }
095
096        @Override
097        public void setReference(Reference reference) {
098                getTargetConnectionFactory().setReference(reference);
099        }
100
101}