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

org.springframework.boot.autoconfigure.jdbc.CustomDataSourceProperties 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;

import java.lang.reflect.Field;

import org.springframework.util.ReflectionUtils;
import org.springframework.util.StringUtils;

import lombok.Data;
import lombok.EqualsAndHashCode;

/**
 * Custom {@link DataSourceProperties}
 */
@EqualsAndHashCode(callSuper = true)
@Data
public class CustomDataSourceProperties extends DataSourceProperties {
	
	@Override
	public String getDriverClassName() {
		
		// Try custom detection
		if (!StringUtils.hasText(this.getFieldValue("driverClassName")) && StringUtils.hasText(this.getUrl())) {
			
			String driverClassName = CustomDatabaseDriver.detectDriverClassName(this.getUrl());
			
			if (StringUtils.hasText(driverClassName)) {
				
				return driverClassName;
			}
		}
		
		return super.getDriverClassName();
	}
	
	@Override
	public String getUsername() {
		
		// Enable empty value
		String username = this.getFieldValue("username");
		
		if (username != null) {
			
			return username;
		}
		
		return super.getUsername();
	}
	
	@Override
	public String getPassword() {
		
		// Enable empty value
		String password = this.getFieldValue("password");
		
		if (password != null) {
			
			return password;
		}
		
		return super.getPassword();
	}
	
	/**
	 * Get field value
	 * 
	 * @param name field name
	 * @param  field type
	 * @return field value
	 */
	private  T getFieldValue(String name) {
		
		Field field = ReflectionUtils.findField(DataSourceProperties.class, name);
		ReflectionUtils.makeAccessible(field);
		
		@SuppressWarnings("unchecked")
		T value = (T) ReflectionUtils.getField(field, this);
		
		return value;
	}
}




© 2015 - 2025 Weber Informatics LLC | Privacy Policy