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

org.springframework.boot.autoconfigure.jdbc.plus.MultipleDataSourceProperties Maven / Gradle / Ivy

/*
 * Copyright 2016 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
 *
 *     http://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.boot.autoconfigure.jdbc.plus;

import java.util.LinkedHashMap;
import java.util.Map;
import java.util.regex.Pattern;

import org.springframework.beans.BeanUtils;
import org.springframework.boot.autoconfigure.jdbc.CustomDataSourceProperties;
import org.springframework.boot.autoconfigure.jdbc.DataSourceProperties;
import org.springframework.util.StringUtils;

import lombok.Data;
import lombok.EqualsAndHashCode;

/**
 * Multiple {@link CustomDataSourceProperties}
 */
@EqualsAndHashCode(callSuper = true)
@Data
public class MultipleDataSourceProperties extends CustomDataSourceProperties {
	
	/**
	 * Database name placeholder
	 */
	public static final String DATABASE_NAME_PLACEHOLDER = "{name}";
	
	/**
	 * {@link Multiple} map
	 */
	private Map multiple = new LinkedHashMap<>();
	
	/**
	 * Get single {@link DataSourceProperties}
	 * 
	 * @param name database name
	 * @return {@link DataSourceProperties}
	 */
	public DataSourceProperties getSingleProperties(String name) {
		
		// Copy from default
		DataSourceProperties result = new MultipleDataSourceProperties();
		
		BeanUtils.copyProperties(this, result);
		
		// Copy from multiple
		Multiple multiple = this.multiple.getOrDefault(name, new Multiple());
		
		if (StringUtils.hasText(multiple.getDriverClassName())) {
			
			result.setDriverClassName(multiple.getDriverClassName());
		}
		
		if (StringUtils.hasText(multiple.getUrl())) {
			
			result.setUrl(multiple.getUrl());
		}
		
		if (multiple.getUsername() != null) {
			
			result.setUsername(multiple.getUsername());
		}
		
		if (multiple.getPassword() != null) {
			
			result.setPassword(multiple.getPassword());
		}
		
		// Replace placeholder
		result.setUrl(result.getUrl().replaceAll(Pattern.quote(DATABASE_NAME_PLACEHOLDER), name));
		
		return result;
	}
	
	/**
	 * Multiple
	 */
	@Data
	public static class Multiple {
		
		/**
		 * Driver class name
		 */
		private String driverClassName;
		
		/**
		 * URL
		 */
		private String url;
		
		/**
		 * User name
		 */
		private String username;
		
		/**
		 * Password
		 */
		private String password;
	}
}




© 2015 - 2025 Weber Informatics LLC | Privacy Policy