com.sap.cds.services.mt.impl.MtTenantProviderHandler Maven / Gradle / Ivy
Go to download
Show more of this group Show more artifacts with this name
Show all versions of cds-feature-mt Show documentation
Show all versions of cds-feature-mt Show documentation
Multi tenancy feature for CDS Services Java
/**************************************************************************
* (C) 2019-2021 SAP SE or an SAP affiliate company. All rights reserved. *
**************************************************************************/
package com.sap.cds.services.mt.impl;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;
import com.sap.cds.services.handler.EventHandler;
import com.sap.cds.services.handler.annotations.After;
import com.sap.cds.services.handler.annotations.HandlerOrder;
import com.sap.cds.services.handler.annotations.On;
import com.sap.cds.services.handler.annotations.ServiceName;
import com.sap.cds.services.mt.ReadTenantsEventContext;
import com.sap.cds.services.mt.TenantInfo;
import com.sap.cds.services.mt.TenantProviderService;
import com.sap.cds.services.utils.CdsErrorStatuses;
import com.sap.cds.services.utils.ErrorStatusException;
import com.sap.cds.services.utils.OrderConstants;
import com.sap.cloud.mt.subscription.InstanceLifecycleManager;
import com.sap.cloud.mt.subscription.TenantMetadata;
import com.sap.cloud.mt.subscription.exceptions.InternalError;
@ServiceName(value = TenantProviderService.DEFAULT_NAME, type = TenantProviderService.class)
public class MtTenantProviderHandler implements EventHandler {
private static final String DATABASE_ID_ADDED = "database_id_added";
private final InstanceLifecycleManager ilm;
public MtTenantProviderHandler(InstanceLifecycleManager ilm) {
this.ilm = ilm;
}
@On
@HandlerOrder(OrderConstants.On.FEATURE + 1)
public void readTenants(ReadTenantsEventContext context) {
try {
context.put(DATABASE_ID_ADDED, true);
context.setResult(ilm.getAllTenantInfos(true).values().stream().map(this::toTenantInfo).collect(Collectors.toList()));
} catch (InternalError e) {
throw new ErrorStatusException(CdsErrorStatuses.TENANT_READ_FAILED, e);
}
}
/**
* {@link After} handler which is primarily used to enrich an existing list of tenants that has
* been resolved before (e.g. by {@link MtTenantProviderSaasHandler} with additional fields
* (e.g. 'database_id'). The enrichment is skipped in case the event context contains a field
* {@link #DATABASE_ID_ADDED}. This might be the case if the required information has been added
* already before.
*
* @param context {@link ReadTenantsEventContext} which already contains a list of tenants
*/
@After
public void enrichTenants(ReadTenantsEventContext context) {
Object dbIdAdded = context.get(DATABASE_ID_ADDED);
if(Boolean.TRUE.equals(dbIdAdded)) {
return;
}
try {
Map smTenants = ilm.getAllTenantInfos(false);
// data needs to be merged into the original list as this might already contain additional properties like 'subdomain'
List tenantInfo = context.getResult();
tenantInfo.forEach(tenant -> {
if(smTenants.containsKey(tenant.getTenant())) {
addDatabaseId(smTenants.get(tenant.getTenant()), tenant);
}
});
} catch (InternalError e) {
throw new ErrorStatusException(CdsErrorStatuses.TENANT_READ_FAILED, e);
}
}
private static void addDatabaseId(TenantMetadata smTenant, TenantInfo tenant) {
Map props = smTenant.getAdditionalProperties();
if(props!=null && props.containsKey(InstanceLifecycleManager.DATABASE_ID)) {
tenant.put(InstanceLifecycleManager.DATABASE_ID, props.get(InstanceLifecycleManager.DATABASE_ID));
}
}
private TenantInfo toTenantInfo(TenantMetadata tenant) {
TenantInfo info = TenantInfo.create();
info.setTenant(tenant.getTenantId());
addDatabaseId(tenant, info);
return info;
}
}
© 2015 - 2025 Weber Informatics LLC | Privacy Policy