com.alibaba.csp.ahas.switchcenter.SwitchAcmDatasourceService Maven / Gradle / Ivy
package com.alibaba.csp.ahas.switchcenter;
import com.taobao.csp.ahas.service.api.client.ClientInfoService;
import com.taobao.csp.ahas.service.component.AgwComponentManager;
import com.taobao.csp.ahas.service.component.AgwComponentType;
import com.taobao.csp.switchcenter.acm.AcmDatasource;
import com.taobao.csp.switchcenter.acm.AcmDecryptor;
import com.taobao.csp.switchcenter.log.SwitchRecordLog;
import com.taobao.csp.switchcenter.spi.SpiOrder;
import com.taobao.diamond.client.impl.DiamondEnvRepo;
import com.taobao.diamond.client.impl.TenantUtil;
import com.taobao.diamond.exception.DiamondException;
/**
* @author lixin.lb
*/
@SpiOrder(-1000)
public class SwitchAcmDatasourceService implements AcmDatasource {
public static final String PERSISTENCE_GROUP = System.getProperty("switch.diamond.group", "ahas-switch");
private static ClientInfoService clientInfoService;
private static String userId;
private static String appName = com.taobao.csp.ahas.service.util.AppNameUtil.getAppName();
private static String namespace;
private static String tenantId;
private static DiamondEnvRepo repo;
private AcmDecryptor acmDecryptor = new SimpleAcmDecryptor();
public SwitchAcmDatasourceService() {
super();
}
public SwitchAcmDatasourceService(ClientInfoService clientInfo) {
clientInfoService = clientInfo;
}
public void init() throws DiamondException {
if (clientInfoService != null){
String currentEndPoint = clientInfoService.getAcmEndpoint();
if (currentEndPoint != null && !currentEndPoint.isEmpty()){
System.setProperty("ahas.address.server.domain", currentEndPoint);
SwitchRecordLog.info("[SwitchAcmDatasourceService] ACM endpoint: " + currentEndPoint);
}
namespace = clientInfoService.getNamespace();
userId = clientInfoService.getUserId();
tenantId = clientInfoService.getTid();
appName = clientInfoService.getAppName();
}
repo = new DiamondEnvRepo();
}
@Override
public DiamondEnvRepo getRepo() {
return repo;
}
@Override
public String getAppName() {
return appName;
}
@Override
public String getUserId() {
return userId;
}
@Override
public String getNamespace() {
return namespace;
}
@Override
public String getTenantId() {
return tenantId;
}
@Override
public AcmDecryptor getDecryptor() {
if (AgwComponentManager.isPrivate() || AgwComponentManager.isInner()){
return null;
}
return acmDecryptor;
}
@Override
public String getDataId(String appName) {
if (AgwComponentManager.isInner()){
return appName;
}
return String.format("%s-%s-%s", appName, userId, namespace);
}
@Override
public String getGroupId() {
if (AgwComponentManager.isInner()){
return "asp-switch";
}
return PERSISTENCE_GROUP;
}
}
© 2015 - 2024 Weber Informatics LLC | Privacy Policy