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