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