001/*
002 * Copyright 2002-2018 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.jms.connection;
018
019import java.util.ArrayList;
020import java.util.List;
021
022import javax.jms.ExceptionListener;
023import javax.jms.JMSException;
024
025import org.springframework.util.Assert;
026
027/**
028 * Implementation of the JMS ExceptionListener interface that supports chaining,
029 * allowing the addition of multiple ExceptionListener instances in order.
030 *
031 * @author Juergen Hoeller
032 * @since 2.0
033 */
034public class ChainedExceptionListener implements ExceptionListener {
035
036        /** List of ExceptionListeners. */
037        private final List<ExceptionListener> delegates = new ArrayList<>(2);
038
039
040        /**
041         * Add an ExceptionListener to the chained delegate list.
042         */
043        public final void addDelegate(ExceptionListener listener) {
044                Assert.notNull(listener, "ExceptionListener must not be null");
045                this.delegates.add(listener);
046        }
047
048        /**
049         * Return all registered ExceptionListener delegates (as array).
050         */
051        public final ExceptionListener[] getDelegates() {
052                return this.delegates.toArray(new ExceptionListener[0]);
053        }
054
055
056        @Override
057        public void onException(JMSException ex) {
058                for (ExceptionListener listener : this.delegates) {
059                        listener.onException(ex);
060                }
061        }
062
063}