001/*
002 * Copyright 2002-2017 the original author or authors.7
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.messaging.simp.config;
018
019import java.util.ArrayList;
020import java.util.Arrays;
021import java.util.List;
022
023import org.springframework.messaging.support.ChannelInterceptor;
024import org.springframework.scheduling.concurrent.ThreadPoolTaskExecutor;
025
026/**
027 * A registration class for customizing the configuration for a
028 * {@link org.springframework.messaging.MessageChannel}.
029 *
030 * @author Rossen Stoyanchev
031 * @author Juergen Hoeller
032 * @since 4.0
033 */
034public class ChannelRegistration {
035
036        private TaskExecutorRegistration registration;
037
038        private final List<ChannelInterceptor> interceptors = new ArrayList<ChannelInterceptor>();
039
040
041        /**
042         * Configure the thread pool backing this message channel.
043         */
044        public TaskExecutorRegistration taskExecutor() {
045                return taskExecutor(null);
046        }
047
048        /**
049         * Configure the thread pool backing this message channel using a custom
050         * ThreadPoolTaskExecutor.
051         * @param taskExecutor the executor to use (or {@code null} for a default executor)
052         */
053        public TaskExecutorRegistration taskExecutor(ThreadPoolTaskExecutor taskExecutor) {
054                if (this.registration == null) {
055                        this.registration = (taskExecutor != null ? new TaskExecutorRegistration(taskExecutor) :
056                                        new TaskExecutorRegistration());
057                }
058                return this.registration;
059        }
060
061        /**
062         * Configure the given interceptors for this message channel,
063         * adding them to the channel's current list of interceptors.
064         * @since 4.3.12
065         */
066        public ChannelRegistration interceptors(ChannelInterceptor... interceptors) {
067                this.interceptors.addAll(Arrays.asList(interceptors));
068                return this;
069        }
070
071        /**
072         * Configure interceptors for the message channel.
073         * @deprecated as of 4.3.12, in favor of {@link #interceptors(ChannelInterceptor...)}
074         */
075        @Deprecated
076        public ChannelRegistration setInterceptors(ChannelInterceptor... interceptors) {
077                if (interceptors != null) {
078                        this.interceptors.addAll(Arrays.asList(interceptors));
079                }
080                return this;
081        }
082
083
084        protected boolean hasTaskExecutor() {
085                return (this.registration != null);
086        }
087
088        /**
089         * @deprecated as of 4.3.12 since it's not used anymore
090         */
091        @Deprecated
092        protected TaskExecutorRegistration getTaskExecRegistration() {
093                return this.registration;
094        }
095
096        /**
097         * @deprecated as of 4.3.12 since it's not used anymore
098         */
099        @Deprecated
100        protected TaskExecutorRegistration getOrCreateTaskExecRegistration() {
101                return taskExecutor();
102        }
103
104        protected boolean hasInterceptors() {
105                return !this.interceptors.isEmpty();
106        }
107
108        protected List<ChannelInterceptor> getInterceptors() {
109                return this.interceptors;
110        }
111
112}