Back/Spring Java

Spring class 동적 로딩 (Singleton)

밍꿔 2021. 4. 5. 18:52


반응형

 

 

package com.mk.common.web.security;

import org.springframework.web.multipart.MultipartFile;
import java.io.File;
import java.io.FileOutputStream;


public interface FileSecurityInterface {

    /**
     * 파일 업로드 암호화
     *
     * @param multi
     * @return
     */
    MultipartFile encryptUpload(MultipartFile multi);

    /**
     * 파일 업로드 암호화
     *
     * @param file
     * @return
     */
    File encryptUpload(File file);

    /**
     * Excel 파일 업로드 암호화
     *
     * @param multi
     * @return
     */
    MultipartFile encryptExcelUpload(MultipartFile multi);

    /**
     * 파일 다운로드 복호화
     *
     * @param file
     * @return
     */
    File decryptDownload(File file);

    /**
     * Excel 파일 다운로드 복호화
     *
     * @param file
     * @return
     */
    FileOutputStream decryptDownload(FileOutputStream file);
}

 

 

package com.mk.common.web.security;

import org.springframework.beans.factory.annotation.Value;
import org.springframework.web.multipart.MultipartFile;

import java.io.File;
import java.io.FileOutputStream;

public class FileSecurity implements FileSecurityInterface {

    private static FileSecurityInterface instance;

    public FileSecurity(){}

    public static FileSecurityInterface getInstance(String libPath){
        if(instance == null) {
            try {
                instance = (FileSecurityInterface) Class.forName(libPath).newInstance();
            } catch (ClassNotFoundException e) {
                instance = new FileSecurityException();
            }  catch (InstantiationException e) {
                e.printStackTrace();
            } catch (IllegalAccessException e) {
                e.printStackTrace();
            }
        }
        return instance;
    }

    @Override
    public MultipartFile encryptUpload(MultipartFile multi) {
        return multi;
    }

    @Override
    public File encryptUpload(File file) {
        return file;
    }

    @Override
    public MultipartFile encryptExcelUpload(MultipartFile multi) {
        return multi;
    }

    @Override
    public File decryptDownload(File file) {
        return file;
    }

    @Override
    public FileOutputStream decryptDownload(FileOutputStream file) {
        return file;
    }
}

 

 

package com.mk.common.web.security;

import org.apache.log4j.Logger;
import org.springframework.web.multipart.MultipartFile;

import java.io.File;
import java.io.FileOutputStream;

public class FileSecurityException implements FileSecurityInterface{

    public Logger logger = Logger.getLogger(this.getClass());

    public FileSecurityException(){}

    @Override
    public MultipartFile encryptUpload(MultipartFile multi) { return multi; }

    @Override
    public File encryptUpload(File file) { return file; }

    @Override
    public MultipartFile encryptExcelUpload(MultipartFile multi) { return multi; }

    @Override
    public File decryptDownload(File file) { return file; }

    @Override
    public FileOutputStream decryptDownload(FileOutputStream file) { return file; }
}

 

반응형