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

com.taotao.boot.data.p6spy.ext.P6spyConfigLoaderBeanPostProcessor Maven / Gradle / Ivy

The newest version!
/*
 * Copyright (c) 2020-2030, Shuigedeng ([email protected] & https://blog.taotaocloud.top/).
 *
 * 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 com.taotao.boot.data.p6spy.ext;

import com.p6spy.engine.spy.P6ModuleManager;
import com.taotao.boot.common.utils.log.LogUtils;
import com.taotao.boot.data.p6spy.properties.P6spyProperties;
import org.springframework.beans.BeansException;
import org.springframework.beans.factory.config.BeanPostProcessor;
import org.springframework.context.ApplicationContext;
import org.springframework.context.ApplicationContextAware;
import org.springframework.core.Ordered;
import org.springframework.util.ReflectionUtils;

import javax.sql.DataSource;
import java.lang.reflect.Field;

/** Before the DataSource is initialized, set the P6spy configuration */
public class P6spyConfigLoaderBeanPostProcessor implements BeanPostProcessor, Ordered, ApplicationContextAware {

    boolean isLoad = false;

    private P6spyProperties p6spyProperties;

    private ApplicationContext applicationContext;

    @Override
    public Object postProcessAfterInitialization(Object bean, String beanName) throws BeansException {
        try {
            if (bean instanceof DataSource && !isLoad) {
                p6spyProperties = getP6spyProperties();
                Field[] fields = P6spyProperties.class.getDeclaredFields();
                for (Field field : fields) {
                    ReflectionUtils.makeAccessible(field);
                    Object val = field.get(this.p6spyProperties);

                    if (val instanceof String valStr) {
                        String propertyKey = String.format("%s.%s", "p6spy.config", field.getName());
                        System.setProperty(propertyKey, valStr);
                    }
                }

                P6ModuleManager.getInstance().reload();
                isLoad = true;
            }
        } catch (IllegalAccessException e) {
			LogUtils.error(e);
        }
        return bean;
    }

    public P6spyProperties getP6spyProperties() {
        if (p6spyProperties == null) {
            p6spyProperties = applicationContext.getBean(P6spyProperties.class);
        }
        return p6spyProperties;
    }

    @Override
    public int getOrder() {
        return 30;
    }

    @Override
    public void setApplicationContext(ApplicationContext applicationContext) throws BeansException {
        this.applicationContext = applicationContext;
    }
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy