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

org.cattleframework.cloud.config.initialize.ConfigInitialize Maven / Gradle / Ivy

The newest version!
/*
 * Copyright (C) 2018 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.cattleframework.cloud.config.initialize;

import java.util.Set;

import org.apache.commons.lang3.StringUtils;
import org.cattleframework.aop.event.ContextInitializeEvent;
import org.cattleframework.aop.processor.SortedBeanPostProcessor;
import org.cattleframework.aop.utils.ServiceLoaderUtils;
import org.cattleframework.cloud.config.processor.ConfigPropertiesProcessor;
import org.cattleframework.cloud.config.properties.BaseCloudConfigProperties;
import org.cattleframework.exception.CommonException;
import org.cattleframework.exception.CommonRuntimeException;
import org.cattleframework.exception.ExceptionWrapUtils;
import org.springframework.beans.BeansException;
import org.springframework.beans.factory.config.ConfigurableListableBeanFactory;
import org.springframework.core.Ordered;
import org.springframework.core.env.Environment;

/**
 * 服务配置数据源初始化
 * 
 * @author orange
 *
 */
public class ConfigInitialize implements ContextInitializeEvent {

    @Override
    public void execute(ConfigurableListableBeanFactory beanFactory, Environment environment) throws Throwable {
	Set configPropertiesProcessors;
	try {
	    configPropertiesProcessors = ServiceLoaderUtils.getService(ConfigPropertiesProcessor.class);
	} catch (CommonException e) {
	    throw ExceptionWrapUtils.wrapRuntime(e);
	}
	String processorName = StringUtils.uncapitalize(getClass().getSimpleName()) + "Processor";
	beanFactory.registerSingleton(processorName, new SortedBeanPostProcessor() {

	    @Override
	    public Object postProcessAfterInitialization(Object bean, String beanName) throws BeansException {
		ConfigPropertiesProcessor configPropertiesProcessor = configPropertiesProcessors.stream()
			.filter(p -> p.needProcess(bean.getClass())).findFirst().orElse(null);
		if (configPropertiesProcessor != null) {
		    BaseCloudConfigProperties sourcePropertiesBean = beanFactory
			    .getBean(configPropertiesProcessor.getSourcePropertiesClass());
		    if (configPropertiesProcessor.getType() != sourcePropertiesBean.getType()) {
			throw new CommonRuntimeException("服务配置数据源属性配置适配器的类型与源属性配置的类型不一致");
		    }
		    configPropertiesProcessor.process(beanFactory, environment, bean, sourcePropertiesBean);
		}
		return bean;
	    }

	    @Override
	    public int getOrder() {
		return Ordered.HIGHEST_PRECEDENCE;
	    }
	});
    }
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy