<commit_before>/*
 * Copyright 2021-2021 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.cloud.stream.binding;

import java.util.ArrayList;
import java.util.Collection;
import java.util.List;
import java.util.Map;
import java.util.stream.Stream;

import com.fasterxml.jackson.databind.ObjectMapper;

import org.springframework.beans.DirectFieldAccessor;
import org.springframework.cloud.stream.binder.Binding;
import org.springframework.util.Assert;

/**
 *
 * Lifecycle controller for the bindings.
 * It is registered as a bean and once injected could be used to control the lifecycle f the bindings.
 *
 * @author <NAME>
 * @since 3.x
 */
public class BindingsLifecycleController {

	private final List<InputBindingLifecycle> inputBindingLifecycles;

	private final List<OutputBindingLifecycle> outputBindingsLifecycles;

	private final ObjectMapper objectMapper;

	public BindingsLifecycleController(List<InputBindingLifecycle> inputBindingLifecycles,
			List<OutputBindingLifecycle> outputBindingsLifecycles) {
		Assert.notEmpty(inputBindingLifecycles,
				"'inputBindingLifecycles' must not be null or empty");
		this.inputBindingLifecycles = inputBindingLifecycles;
		this.outputBindingsLifecycles = outputBindingsLifecycles;
		this.objectMapper = new ObjectMapper(); //see https://github.com/spring-cloud/spring-cloud-stream/issues/2253
		// we need to use ObjectMapper that could not be modified by the user.
	}

	/**
	 * Convenience method to stop the binding with provided `bindingName`.
	 * @param bindingName the name of the binding.
	 */
	public void stop(String bindingName) {
		this.changeState(bindingName, State.STOPPED);
	}

	/**
	 * Convenience method to start the binding with provided `bindingName`.
	 * @param bindingName the name of the binding.
	 */
	public void start(String bindingName) {
		this.changeState(bindingName, State.STARTED);
	}

	/**
	 * Convenience method to pause the binding with provided `bindingName`.
	 * @param bindingName the name of the binding.
	 */<commit_msg>GH-2272 Register JavaTimeModule with BindingsLifecycleController's ObjectMapper<commit_after>/*
 * Copyright 2021-2021 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.cloud.stream.binding;

import java.util.ArrayList;
import java.util.Collection;
import java.util.List;
import java.util.Map;
import java.util.stream.Stream;

import com.fasterxml.jackson.databind.Module;
import com.fasterxml.jackson.databind.ObjectMapper;

import org.springframework.beans.BeanUtils;
import org.springframework.beans.DirectFieldAccessor;
import org.springframework.cloud.stream.binder.Binding;
import org.springframework.util.Assert;
import org.springframework.util.ClassUtils;

/**
 *
 * Lifecycle controller for the bindings.
 * It is registered as a bean and once injected could be used to control the lifecycle f the bindings.
 *
 * @author <NAME>
 * @since 3.x
 */
public class BindingsLifecycleController {

	private final List<InputBindingLifecycle> inputBindingLifecycles;

	private final List<OutputBindingLifecycle> outputBindingsLifecycles;

	private final ObjectMapper objectMapper;

	@SuppressWarnings("unchecked")
	public BindingsLifecycleController(List<InputBindingLifecycle> inputBindingLifecycles,
			List<OutputBindingLifecycle> outputBindingsLifecycles) {
		Assert.notEmpty(inputBindingLifecycles,
				"'inputBindingLifecycles' must not be null or empty");
		this.inputBindingLifecycles = inputBindingLifecycles;
		this.outputBindingsLifecycles = outputBindingsLifecycles;


		this.objectMapper = new ObjectMapper(); //see https://github.com/spring-cloud/spring-cloud-stream/issues/2253
		// we need to use ObjectMapper that could not be modified by the user.

		try {
			Class<? extends Module> javaTimeModuleClass = (Class<? extends Module>)
					ClassUtils.forName("com.fasterxml.jackson.datatype.jsr310.JavaTimeModule", ClassUtils.getDefaultClassLoader());
			Module javaTimeModule = BeanUtils.instantiateClass(javaTimeModuleClass);
			this.objectMapper.registerModule(javaTimeModule);
		}
		catch (ClassNotFoundException ex) {
			// ignore; jackson-datatype-jsr310 not available
		}
	}

	/**
	 * Convenience method to stop the binding with provided `bindingName`.
	 * @param bindingName the name of the binding.
	 */
	public void stop(String bindingName) {
		this.changeState(bindingName, State.STOPPED);
	}

	/**
	 * Convenience method to start the binding with provided `bindingName`.
	 * @param bindingName the name of the binding.
	 */
	public void start(String bindingName) {
		this.changeState(bindingName, State.STARTED);
	}

	/**
	 * Convenience method to pause the binding with provided `bindingName`.
	 * @param bindingName the name of the binding.
	 */
{'fraction_non_alphanumeric': 0.0841329677861549, 'fraction_numerical': 0.0075394105551747775, 'mean_word_length': 4.716944172380019, 'pattern_counts': {'":': 0, '<': 15, '<?xml version=': 0, '>': 15, 'https://': 4, 'lorem ipsum': 0, 'www.': 2, 'xml': 4}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '3962661', 'n_tokens_mistral': 1707, 'n_tokens_neox': 1575, 'n_words': 521}