package com.njcn.utils;

import org.springframework.beans.BeansException;
import org.springframework.beans.factory.BeanInitializationException;
import org.springframework.beans.factory.config.ConfigurableListableBeanFactory;
import org.springframework.beans.factory.config.PropertyPlaceholderConfigurer;

import java.util.Properties;

/**
 * @author hongawen
 * @Date: 2018/9/13 10:27
 */
public class EncryptPropertyPlaceholderConfigurer extends
        PropertyPlaceholderConfigurer {

    @Override
    protected void processProperties(
            ConfigurableListableBeanFactory beanFactory, Properties props)
            throws BeansException {

        String[] encryptPropNames = {"mariadb.user", "mariadb.password", "oracle.user", "oracle.password"};
        try {
            for (int i = 0; i < encryptPropNames.length; i++) {
                String value = props.getProperty(encryptPropNames[i]);

                if (value != null) {
                    props.setProperty(encryptPropNames[i], ThreeDes.decryptThreeDes(value));
                }
            }
            super.processProperties(beanFactory, props);
        } catch (Exception e) {
            logger.error(e.getMessage());
            throw new BeanInitializationException(e.getMessage());
        }
    }

    public static void main(String[] args) {
        System.out.println(ThreeDes.encryptThreeDes("pqsadmin_jb"));
    }
}
