package com.huashe.park.infrastructure.socket.cfg;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.aop.framework.ProxyFactory;
import org.springframework.beans.factory.FactoryBean;

public class SocketServerFactoryBean<T> implements FactoryBean<T> {
    private static final Logger log = LoggerFactory.getLogger(SocketServerFactoryBean.class);

    private Class socketServerClz;

    public SocketServerFactoryBean() {
    }

    public SocketServerFactoryBean(Class socketServerClz) {
        this.socketServerClz = socketServerClz;
    }

    public Class getLogClz() {
        return socketServerClz;
    }

    public void setLogClz(Class socketServerClz) {
        this.socketServerClz = socketServerClz;
    }

    @Override
    public T getObject() throws Exception {
        ProxyFactory proxyFactory = new ProxyFactory();
        try {
            proxyFactory.setTarget(socketServerClz.newInstance());
        } catch (InstantiationException | IllegalAccessException e) {
            log.error(e.getMessage(), e);
        }
        return (T) proxyFactory.getProxy();
    }

    @Override
    public Class<?> getObjectType() {
        return socketServerClz;
    }
}