package com.yd.oss.service.config;

import com.aliyun.oss.OSS;
import com.yd.oss.service.service.OssService;

import java.lang.reflect.InvocationHandler;
import java.lang.reflect.Method;
import java.lang.reflect.Proxy;

public class DynamicOssClientProxy implements InvocationHandler {
    private final OssService ossService;

    public DynamicOssClientProxy(OssService ossService) {
        this.ossService = ossService;
    }

    public static OSS createProxy(OssService ossService) {
        return (OSS) Proxy.newProxyInstance(
                DynamicOssClientProxy.class.getClassLoader(),
                new Class[]{OSS.class},
                new DynamicOssClientProxy(ossService)
        );
    }

    @Override
    public Object invoke(Object proxy, Method method, Object[] args) throws Throwable {
        // 每次方法调用都从 OssService 获取当前客户端
        OSS currentClient = ossService.getOssClient();
        return method.invoke(currentClient, args);
    }
}