All Downloads are FREE. Search and download functionalities are using the official Maven repository.

org.springframework.integration.dsl.MessageChannelSpec Maven / Gradle / Ivy

There is a newer version: 6.3.3
Show newest version
/*
 * Copyright 2016-2019 the original author or authors.
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *      https://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

package org.springframework.integration.dsl;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.LinkedHashMap;
import java.util.LinkedList;
import java.util.List;
import java.util.Map;

import org.springframework.integration.channel.AbstractMessageChannel;
import org.springframework.integration.channel.interceptor.WireTap;
import org.springframework.messaging.MessageChannel;
import org.springframework.messaging.converter.MessageConverter;
import org.springframework.messaging.support.ChannelInterceptor;
import org.springframework.util.Assert;

/**
 *
 * @param  the target {@link MessageChannelSpec} implementation type.
 * @param  the target {@link AbstractMessageChannel} implementation type.
 *
 * @author Artem Bilan
 *
 * @since 5.0
 */
public abstract class MessageChannelSpec, C extends AbstractMessageChannel>
		extends IntegrationComponentSpec
		implements ComponentsRegistration {

	private final Map componentsToRegister = new LinkedHashMap<>();

	private final List> datatypes = new ArrayList<>();

	private final List interceptors = new LinkedList<>();

	protected C channel; // NOSONAR

	private MessageConverter messageConverter;

	protected MessageChannelSpec() {
		super();
	}

	public S datatype(Class... types) {
		Assert.notNull(types, "'datatypes' must not be null");
		Assert.noNullElements(types, "'datatypes' must not contain null elements");
		this.datatypes.addAll(Arrays.asList(types));
		return _this();
	}

	public S interceptor(ChannelInterceptor... interceptorArray) {
		Assert.notNull(interceptorArray, "'interceptorArray' must not be null");
		Assert.noNullElements(interceptorArray, "'interceptorArray' must not contain null elements");
		this.interceptors.addAll(Arrays.asList(interceptorArray));
		return _this();
	}

	/**
	 * Populate the {@code Wire Tap} EI Pattern specific
	 * {@link org.springframework.messaging.support.ChannelInterceptor} implementation.
	 * @param wireTapChannel the {@link MessageChannel} bean name to wire-tap.
	 * @return the current {@link MessageChannelSpec}.
	 * @see WireTapSpec
	 */
	public S wireTap(String wireTapChannel) {
		return wireTap(new WireTapSpec(wireTapChannel));
	}

	/**
	 * Populate the {@code Wire Tap} EI Pattern specific
	 * {@link org.springframework.messaging.support.ChannelInterceptor} implementation.
	 * @param wireTapChannel the {@link MessageChannel} instance to wire-tap.
	 * @return the current {@link MessageChannelSpec}.
	 * @see WireTapSpec
	 */
	public S wireTap(MessageChannel wireTapChannel) {
		return wireTap(new WireTapSpec(wireTapChannel));
	}

	/**
	 * Populate the {@code Wire Tap} EI Pattern specific
	 * {@link org.springframework.messaging.support.ChannelInterceptor} implementation.
	 * @param wireTapSpec the {@link WireTapSpec} to build {@link WireTap} instance.
	 * @return the current {@link MessageChannelSpec}.
	 * @see WireTap
	 */
	public S wireTap(WireTapSpec wireTapSpec) {
		WireTap interceptor = wireTapSpec.get();
		this.componentsToRegister.put(interceptor, null);
		return interceptor(interceptor);
	}

	public S messageConverter(MessageConverter converter) {
		this.messageConverter = converter;
		return _this();
	}

	@Override
	public Map getComponentsToRegister() {
		return this.componentsToRegister;
	}

	@Override
	protected C doGet() {
		this.channel.setDatatypes(this.datatypes.toArray(new Class[this.datatypes.size()]));
		this.channel.setBeanName(getId());
		this.channel.setInterceptors(this.interceptors);
		this.channel.setMessageConverter(this.messageConverter);
		return this.channel;
	}

}