package net.northking.cctp.se.component;

import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import java.util.List;
import java.util.Map;

/**
 * ComponentManager
 *
 * @author fengxiang.zhang
 * @since 1.0
 */
@Slf4j
@Component
public class ComponentManager implements ComponentManagementService {

    @Autowired
    private LibraryDefinitionManager definitionManager;


    @Override
    public ComponentLibrary newInstance(String libCom) throws Exception {
        return definitionManager.newInstance(libCom);
    }

    @Override
    public Map<String, String> getExistMap() {
        return definitionManager.getLoadedLibraryJarInfo();
    }

    @Override
    public void reloadLibrary(List<ComLibraryInfoDto> updateList) {
        for (ComLibraryInfoDto r : updateList) {
            try {
                log.info("------ 准备载入组件库资源：[name={}, version={}, path={}] ------", r.getLibraryName(), r.getLibraryVersion(), r.getLocalPath());
                definitionManager.reload(r.getLocalPath());
            } catch (Exception e) {
                log.error(String.format("初始化组件库失败：[name=%s, version=%s, path=%s]", r.getLibraryName(), r.getLibraryVersion(), r.getLocalPath()), e);
            }
        }
    }

}
