This commit is contained in:
Rinjae
2025-09-05 18:57:45 +09:00
commit aacc1a389e
1229 changed files with 167963 additions and 0 deletions
@@ -0,0 +1,11 @@
package com.eactive.eai.apim.apigroup;
import lombok.Data;
@Data
public class ApiGroupApiVO {
private String apiGroupId;
private String apiId;
private Integer displayOrder;
}
@@ -0,0 +1,42 @@
package com.eactive.eai.apim.apigroup;
import com.eactive.eai.apim.apigroup.mapper.ApiGroupMapper;
import com.eactive.eai.data.entity.onl.apim.apigroup.ApiGroup;
import com.eactive.eai.apim.apigroup.loader.ApiGroupLoader;
import com.eactive.eai.common.dao.BaseDAO;
import com.eactive.eai.common.dao.DAOException;
import com.eactive.eai.common.exception.ExceptionUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import java.util.List;
import java.util.stream.Collectors;
@Service
@Transactional
public class ApiGroupDao extends BaseDAO {
@Autowired
ApiGroupLoader apiGroupLoader;
@Autowired
ApiGroupMapper apiGroupMapper;
public List<ApiGroupVO> getAllApiGroups() throws DAOException {
try {
List<ApiGroup> adapterGroups = apiGroupLoader.selectAllGroups();
return adapterGroups.stream().map(apiGroupMapper::toVo).collect(Collectors.toList());
} catch (Exception e) {
throw new DAOException(ExceptionUtil.getErrorCode(e, "RECEAIAAC001"));
}
}
public ApiGroupVO getApiGroup(String apiGroupId) throws DAOException {
try {
ApiGroup apiGroup = apiGroupLoader.getById(apiGroupId);
return apiGroupMapper.toVo(apiGroup);
} catch (Exception e) {
throw new DAOException(ExceptionUtil.getErrorCode(e, "RECEAIAAC001"));
}
}
}
@@ -0,0 +1,154 @@
package com.eactive.eai.apim.apigroup;
import com.eactive.eai.common.dao.DAOException;
import com.eactive.eai.common.exception.ExceptionUtil;
import com.eactive.eai.common.lifecycle.Lifecycle;
import com.eactive.eai.common.lifecycle.LifecycleException;
import com.eactive.eai.common.lifecycle.LifecycleListener;
import com.eactive.eai.common.lifecycle.LifecycleSupport;
import com.eactive.eai.common.util.ApplicationContextProvider;
import com.eactive.eai.common.util.Logger;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import java.util.*;
import java.util.stream.Collectors;
@Component
public class ApiGroupManager implements Lifecycle {
static Logger logger = Logger.getLogger(Logger.LOGGER_DEFAULT);
private boolean started;
@Autowired
private ApiGroupDao dao;
private Map<String, ApiGroupVO> apiGroupMap = new HashMap<>();
private Map<String, Set<String>> apiGroupApiIndex = new HashMap<>();
private LifecycleSupport lifecycle = new LifecycleSupport(this);
public static ApiGroupManager getInstance() {
return ApplicationContextProvider.getContext().getBean(ApiGroupManager.class);
}
@Override
public void addLifecycleListener(LifecycleListener listener) {
this.lifecycle.addLifecycleListener(listener);
}
@Override
public LifecycleListener[] findLifecycleListeners() {
return this.lifecycle.findLifecycleListeners();
}
@Override
public void removeLifecycleListener(LifecycleListener listener) {
this.lifecycle.removeLifecycleListener(listener);
}
public void reloadApiGroup(String apiGroupId) throws DAOException {
removeApiGroup(apiGroupId);
ApiGroupVO apiGroupVO = dao.getApiGroup(apiGroupId);
addApiGroup(apiGroupVO);
}
protected void addApiGroup(ApiGroupVO apiGroupVO){
apiGroupMap.put(apiGroupVO.getId(), apiGroupVO);
List<ApiGroupApiVO> apiList = apiGroupVO.getApiGroupApiList();
if(apiList != null){
for (ApiGroupApiVO apiGroupApiVO : apiList){
String apiId = apiGroupApiVO.getApiId();
String apiGroupId = apiGroupApiVO.getApiGroupId();
apiGroupApiIndex.computeIfAbsent(apiId, k -> new HashSet<>()).add(apiGroupId);
}
}
}
public void removeApiGroup(String apiGroupId) {
// apiGroupMap에서 ApiGroupVO 제거
ApiGroupVO removedApiGroup = apiGroupMap.remove(apiGroupId);
// 제거된 ApiGroupVO가 null이 아닌지 확인
if (removedApiGroup != null) {
// 제거된 ApiGroupVO의 ApiGroupApiVO 리스트 가져오기
List<ApiGroupApiVO> apiList = removedApiGroup.getApiGroupApiList();
// apiList가 null이 아닌 경우
if (apiList != null) {
for (ApiGroupApiVO apiGroupApiVO : apiList) {
String apiId = apiGroupApiVO.getApiId();
// apiGroupApiIndex에서 apiId에 해당하는 Set 가져오기
Set<String> apiGroupIds = apiGroupApiIndex.get(apiId);
// Set이 null이 아닌 경우 apiGroupId 제거
if (apiGroupIds != null) {
apiGroupIds.remove(apiGroupId);
// Set이 비어 있으면 apiGroupApiIndex에서 해당 apiId 삭제
if (apiGroupIds.isEmpty()) {
apiGroupApiIndex.remove(apiId);
}
}
}
}
}
}
public List<ApiGroupVO> findApiGroup(String apiId) {
return Optional.ofNullable(apiGroupApiIndex.get(apiId)).orElse(Collections.emptySet())
.stream()
.map(apiGroupMap::get)
.collect(Collectors.toList());
}
private void init() throws Exception {
try {
List<ApiGroupVO> apiGroups = dao.getAllApiGroups();
for(ApiGroupVO apiGroupVO : apiGroups){
addApiGroup(apiGroupVO);
}
} catch (DAOException e) {
logger.error(e);
throw new LifecycleException(ExceptionUtil.getErrorCode(e, "RECEAICPM201"));
}
}
@Override
public void start() throws LifecycleException {
if (started)
throw new LifecycleException("RECEAICCM201");
this.lifecycle.fireLifecycleEvent(STARTING_EVENT, this);
try {
init();
} catch (Exception e) {
throw new LifecycleException(ExceptionUtil.getErrorCode(e, "RECEAICCM202"));
}
this.started = true;
this.lifecycle.fireLifecycleEvent(STARTED_EVENT, this);
}
@Override
public void stop() throws LifecycleException {
if (!started)
throw new LifecycleException("RECEAIAAC020");
// Notify our interested LifecycleListeners
this.lifecycle.fireLifecycleEvent(STOPING_EVENT, this);
this.apiGroupMap = new HashMap<>();
this.started = false;
// Notify our interested LifecycleListeners
this.lifecycle.fireLifecycleEvent(STOPPED_EVENT, this);
}
@Override
public boolean isStarted() {
return this.started;
}
}
@@ -0,0 +1,21 @@
package com.eactive.eai.apim.apigroup;
import com.fasterxml.jackson.annotation.JsonFormat;
import lombok.Data;
import java.time.LocalDateTime;
import java.util.List;
/**
* GROUP 등록 정보로 거래제한, 유량제어등을 하기 위해 로딩
* 개발자 포털에 노출할 CONTENT를 메모리에 로딩할 필요가 없어 해당 부분은 생략
*/
@Data
public class ApiGroupVO {
private String id;
private String groupName;
private String groupDesc;
private String useYn;
private List<ApiGroupApiVO> apiGroupApiList;
}
@@ -0,0 +1,43 @@
package com.eactive.eai.apim.apigroup.command;
import com.eactive.eai.apim.apigroup.ApiGroupManager;
import com.eactive.eai.adapter.AdapterManager;
import com.eactive.eai.agent.command.Command;
import com.eactive.eai.agent.command.CommandException;
import com.eactive.eai.common.dao.DAOException;
import com.eactive.eai.common.util.Logger;
public class ReloadApiGroupCommand extends Command {
Logger logger = Logger.getLogger(Logger.LOGGER_DEFAULT);
public ReloadApiGroupCommand() { super("ReloadApiGroupCommand"); }
@Override
public Object execute() throws CommandException {
if (logger.isInfo())
logger.info(this.name + " is execute");
if (!(args instanceof String)) {
String rspErrorCode = "RECEAIMCM001";
String msg = makeException(rspErrorCode, null);
if (logger.isError())
logger.error(msg);
throw new CommandException(msg);
}
ApiGroupManager manager = ApiGroupManager.getInstance();
String apiGroupId = (String) args;
try {
manager.reloadApiGroup(apiGroupId);
} catch (Exception e) {
String rspErrorCode = "RECEAIMCM002";
String msg = makeException(rspErrorCode, e);
if (logger.isError())
logger.error(msg, e);
throw new CommandException(msg);
}
return "success";
}
}
@@ -0,0 +1,41 @@
package com.eactive.eai.apim.apigroup.command;
import com.eactive.eai.apim.apigroup.ApiGroupManager;
import com.eactive.eai.agent.command.Command;
import com.eactive.eai.agent.command.CommandException;
import com.eactive.eai.common.util.Logger;
public class RemoveApiGroupCommand extends Command {
Logger logger = Logger.getLogger(Logger.LOGGER_DEFAULT);
public RemoveApiGroupCommand() { super("RemoveApiGroupCommand"); }
@Override
public Object execute() throws CommandException {
if (logger.isInfo())
logger.info(this.name + " is execute");
if (!(args instanceof String)) {
String rspErrorCode = "RECEAIMCM001";
String msg = makeException(rspErrorCode, null);
if (logger.isError())
logger.error(msg);
throw new CommandException(msg);
}
ApiGroupManager manager = ApiGroupManager.getInstance();
String apiGroupId = (String) args;
try {
manager.removeApiGroup(apiGroupId);
} catch (Exception e) {
String rspErrorCode = "RECEAIMCM002";
String msg = makeException(rspErrorCode, e);
if (logger.isError())
logger.error(msg, e);
throw new CommandException(msg);
}
return "success";
}
}
@@ -0,0 +1,18 @@
package com.eactive.eai.apim.apigroup.mapper;
import com.eactive.eai.apim.apigroup.ApiGroupApiVO;
import com.eactive.eai.data.entity.onl.apim.apigroup.ApiGroupApi;
import com.eactive.eai.data.mapper.BaseMapperConfig;
import com.eactive.eai.data.mapper.GenericMapper;
import org.mapstruct.Mapper;
import org.mapstruct.Mapping;
@Mapper(config = BaseMapperConfig.class, uses = {})
public interface ApiGroupApiMapper extends GenericMapper<ApiGroupApiVO, ApiGroupApi> {
@Mapping(source = "id.apiGroupId", target = "apiGroupId")
@Mapping(source = "id.apiId", target = "apiId")
@Mapping(source = "displayOrder", target = "displayOrder")
ApiGroupApiVO toVo(ApiGroupApi apiGroupApi);
}
@@ -0,0 +1,16 @@
package com.eactive.eai.apim.apigroup.mapper;
import com.eactive.eai.apim.apigroup.ApiGroupVO;
import com.eactive.eai.data.entity.onl.apim.apigroup.ApiGroup;
import com.eactive.eai.data.mapper.BaseMapperConfig;
import com.eactive.eai.data.mapper.GenericMapper;
import org.mapstruct.Mapper;
import org.mapstruct.Mapping;
@Mapper(config = BaseMapperConfig.class, uses = { ApiGroupApiMapper.class })
public interface ApiGroupMapper extends GenericMapper<ApiGroupVO, ApiGroup> {
@Mapping(source = "apiGroupApiList", target = "apiGroupApiList")
ApiGroupVO toVo(ApiGroup apiGroup);
}