From 8e33eebbf6ea7087a519353eb6bf46f1bdfb45a0 Mon Sep 17 00:00:00 2001 From: ashvini Date: Fri, 28 Jun 2024 12:38:35 +0530 Subject: [PATCH 1/2] Add tenant application artifact deletion configuration Fix code errors Remove spaces Refactor code --- .../common/services/ApplicationManager.java | 1 + .../mgt/core/impl/ApplicationManagerImpl.java | 35 ++++++++++++------- .../api/admin/UserManagementAdminService.java | 11 ++++-- .../admin/UserManagementAdminServiceImpl.java | 5 ++- 4 files changed, 35 insertions(+), 17 deletions(-) diff --git a/components/application-mgt/io.entgra.device.mgt.core.application.mgt.common/src/main/java/io/entgra/device/mgt/core/application/mgt/common/services/ApplicationManager.java b/components/application-mgt/io.entgra.device.mgt.core.application.mgt.common/src/main/java/io/entgra/device/mgt/core/application/mgt/common/services/ApplicationManager.java index 4a1be2ea1e7..bf3bdc5d408 100644 --- a/components/application-mgt/io.entgra.device.mgt.core.application.mgt.common/src/main/java/io/entgra/device/mgt/core/application/mgt/common/services/ApplicationManager.java +++ b/components/application-mgt/io.entgra.device.mgt.core.application.mgt.common/src/main/java/io/entgra/device/mgt/core/application/mgt/common/services/ApplicationManager.java @@ -547,4 +547,5 @@ public interface ApplicationManager { */ void deleteApplicationDataOfTenant(int tenantId) throws ApplicationManagementException; void deleteApplicationDataByTenantDomain(String tenantDomain) throws ApplicationManagementException; + void deleteApplicationArtifactsByTenantDomain(String tenantDomain) throws ApplicationManagementException; } diff --git a/components/application-mgt/io.entgra.device.mgt.core.application.mgt.core/src/main/java/io/entgra/device/mgt/core/application/mgt/core/impl/ApplicationManagerImpl.java b/components/application-mgt/io.entgra.device.mgt.core.application.mgt.core/src/main/java/io/entgra/device/mgt/core/application/mgt/core/impl/ApplicationManagerImpl.java index efb55e60cb2..f6d0fc228f6 100644 --- a/components/application-mgt/io.entgra.device.mgt.core.application.mgt.core/src/main/java/io/entgra/device/mgt/core/application/mgt/core/impl/ApplicationManagerImpl.java +++ b/components/application-mgt/io.entgra.device.mgt.core.application.mgt.core/src/main/java/io/entgra/device/mgt/core/application/mgt/core/impl/ApplicationManagerImpl.java @@ -4424,7 +4424,6 @@ public class ApplicationManagerImpl implements ApplicationManager { spApplicationDAO.deleteSPApplicationMappingByTenant(tenantId); spApplicationDAO.deleteIdentityServerByTenant(tenantId); applicationDAO.deleteApplicationsByTenant(tenantId); - APIUtil.getApplicationStorageManager().deleteAppFolderOfTenant(tenantId); ConnectionManagerUtil.commitDBTransaction(); } catch (DBConnectionException e) { @@ -4449,12 +4448,6 @@ public class ApplicationManagerImpl implements ApplicationManager { + " of tenant ID: " + tenantId ; log.error(msg, e); throw new ApplicationManagementException(msg, e); - } catch (ApplicationStorageManagementException e) { - ConnectionManagerUtil.rollbackDBTransaction(); - String msg = "Error occurred while deleting App folder of tenant" - + " of tenant ID: " + tenantId ; - log.error(msg, e); - throw new ApplicationManagementException(msg, e); } finally { ConnectionManagerUtil.closeDBConnection(); } @@ -4499,7 +4492,6 @@ public class ApplicationManagerImpl implements ApplicationManager { spApplicationDAO.deleteSPApplicationMappingByTenant(tenantId); spApplicationDAO.deleteIdentityServerByTenant(tenantId); applicationDAO.deleteApplicationsByTenant(tenantId); - APIUtil.getApplicationStorageManager().deleteAppFolderOfTenant(tenantId); ConnectionManagerUtil.commitDBTransaction(); } catch (DBConnectionException e) { @@ -4524,15 +4516,32 @@ public class ApplicationManagerImpl implements ApplicationManager { + " of tenant ID: " + tenantId ; log.error(msg, e); throw new ApplicationManagementException(msg, e); + } finally { + ConnectionManagerUtil.closeDBConnection(); + } + } + + @Override + public void deleteApplicationArtifactsByTenantDomain(String tenantDomain) throws ApplicationManagementException { + int tenantId; + try{ + TenantMgtAdminService tenantMgtAdminService = new TenantMgtAdminService(); + TenantInfoBean tenantInfoBean = tenantMgtAdminService.getTenant(tenantDomain); + tenantId = tenantInfoBean.getTenantId(); + + } catch (Exception e) { + String msg = "Error getting tenant ID from domain: " + + tenantDomain + "when trying to delete application artifacts of tenant"; + log.error(msg, e); + throw new ApplicationManagementException(msg, e); + } + try { + APIUtil.getApplicationStorageManager().deleteAppFolderOfTenant(tenantId); } catch (ApplicationStorageManagementException e) { - ConnectionManagerUtil.rollbackDBTransaction(); - String msg = "Error occurred while deleting App folder of tenant" + String msg = "Error occurred while deleting Application folders of tenant" + " of tenant ID: " + tenantId ; log.error(msg, e); throw new ApplicationManagementException(msg, e); - } finally { - ConnectionManagerUtil.closeDBConnection(); } - } } diff --git a/components/device-mgt/io.entgra.device.mgt.core.device.mgt.api/src/main/java/io/entgra/device/mgt/core/device/mgt/api/jaxrs/service/api/admin/UserManagementAdminService.java b/components/device-mgt/io.entgra.device.mgt.core.device.mgt.api/src/main/java/io/entgra/device/mgt/core/device/mgt/api/jaxrs/service/api/admin/UserManagementAdminService.java index 5d324aeb02a..08b51ac9c42 100644 --- a/components/device-mgt/io.entgra.device.mgt.core.device.mgt.api/src/main/java/io/entgra/device/mgt/core/device/mgt/api/jaxrs/service/api/admin/UserManagementAdminService.java +++ b/components/device-mgt/io.entgra.device.mgt.core.device.mgt.api/src/main/java/io/entgra/device/mgt/core/device/mgt/api/jaxrs/service/api/admin/UserManagementAdminService.java @@ -298,8 +298,13 @@ public interface UserManagementAdminService { name = "tenantDomain", value = "The domain of the tenant to be deleted.", required = true) - @PathParam("tenantDomain") - String tenantDomain); - + String tenantDomain, + @ApiParam( + name = "deleteAppArtifacts", + value = "Flag to indicate whether to delete application artifacts.", + required = false) + @QueryParam("deleteAppArtifacts") + @DefaultValue("false") + boolean deleteAppArtifacts); } diff --git a/components/device-mgt/io.entgra.device.mgt.core.device.mgt.api/src/main/java/io/entgra/device/mgt/core/device/mgt/api/jaxrs/service/impl/admin/UserManagementAdminServiceImpl.java b/components/device-mgt/io.entgra.device.mgt.core.device.mgt.api/src/main/java/io/entgra/device/mgt/core/device/mgt/api/jaxrs/service/impl/admin/UserManagementAdminServiceImpl.java index d4f34d8e4ab..d0e82d7fb32 100644 --- a/components/device-mgt/io.entgra.device.mgt.core.device.mgt.api/src/main/java/io/entgra/device/mgt/core/device/mgt/api/jaxrs/service/impl/admin/UserManagementAdminServiceImpl.java +++ b/components/device-mgt/io.entgra.device.mgt.core.device.mgt.api/src/main/java/io/entgra/device/mgt/core/device/mgt/api/jaxrs/service/impl/admin/UserManagementAdminServiceImpl.java @@ -91,7 +91,7 @@ public class UserManagementAdminServiceImpl implements UserManagementAdminServic @DELETE @Path("/domain/{tenantDomain}") @Override - public Response deleteTenantByDomain(@PathParam("tenantDomain") String tenantDomain) { + public Response deleteTenantByDomain(@PathParam("tenantDomain") String tenantDomain, @QueryParam("deleteAppArtifacts") boolean deleteAppArtifacts) { try { int tenantId = CarbonContext.getThreadLocalCarbonContext().getTenantId(); if (tenantId != MultitenantConstants.SUPER_TENANT_ID){ @@ -99,6 +99,9 @@ public class UserManagementAdminServiceImpl implements UserManagementAdminServic log.error(msg); return Response.status(Response.Status.UNAUTHORIZED).entity(msg).build(); } else { + if(deleteAppArtifacts){ + DeviceMgtAPIUtils.getApplicationManager().deleteApplicationArtifactsByTenantDomain(tenantDomain); + } DeviceMgtAPIUtils.getApplicationManager().deleteApplicationDataByTenantDomain(tenantDomain); DeviceMgtAPIUtils.getDeviceManagementService().deleteDeviceDataByTenantDomain(tenantDomain); TenantMgtAdminService tenantMgtAdminService = new TenantMgtAdminService(); From e1a47c68fbbf3822cf47669b6aef2197d2d16aad Mon Sep 17 00:00:00 2001 From: ashvini Date: Tue, 2 Jul 2024 14:20:57 +0530 Subject: [PATCH 2/2] Get TenantMgtAdminService as an osgi service Resolve comments Remove source reference Change error messages Make changes in code Add license Resolve comments Add java doc comments Resolve comments Resolve comment --- .../common/services/ApplicationManager.java | 14 +++++ .../pom.xml | 5 ++ .../mgt/core/impl/ApplicationManagerImpl.java | 57 +++++++------------ ...ApplicationManagementServiceComponent.java | 24 ++++++++ .../mgt/core/internal/DataHolder.java | 11 ++++ .../admin/UserManagementAdminServiceImpl.java | 14 ++--- .../mgt/api/jaxrs/util/DeviceMgtAPIUtils.java | 19 +++++++ .../common/spi/TenantManagerAdminService.java | 27 +++++++++ .../pom.xml | 1 + .../core/tenant/mgt/core/TenantManager.java | 1 + .../impl/TenantManagerAdminServiceImpl.java | 57 +++++++++++++++++++ .../internal/TenantMgtServiceComponent.java | 9 ++- .../listener/DeviceMgtTenantListener.java | 8 --- 13 files changed, 195 insertions(+), 52 deletions(-) create mode 100644 components/tenant-mgt/io.entgra.device.mgt.core.tenant.mgt.common/src/main/java/io/entgra/device/mgt/core/tenant/mgt/common/spi/TenantManagerAdminService.java create mode 100644 components/tenant-mgt/io.entgra.device.mgt.core.tenant.mgt.core/src/main/java/io/entgra/device/mgt/core/tenant/mgt/core/impl/TenantManagerAdminServiceImpl.java diff --git a/components/application-mgt/io.entgra.device.mgt.core.application.mgt.common/src/main/java/io/entgra/device/mgt/core/application/mgt/common/services/ApplicationManager.java b/components/application-mgt/io.entgra.device.mgt.core.application.mgt.common/src/main/java/io/entgra/device/mgt/core/application/mgt/common/services/ApplicationManager.java index bf3bdc5d408..7e133539803 100644 --- a/components/application-mgt/io.entgra.device.mgt.core.application.mgt.common/src/main/java/io/entgra/device/mgt/core/application/mgt/common/services/ApplicationManager.java +++ b/components/application-mgt/io.entgra.device.mgt.core.application.mgt.common/src/main/java/io/entgra/device/mgt/core/application/mgt/common/services/ApplicationManager.java @@ -546,6 +546,20 @@ public interface ApplicationManager { * @throws ApplicationManagementException thrown if an error occurs when deleting data */ void deleteApplicationDataOfTenant(int tenantId) throws ApplicationManagementException; + + /** + * Delete all application related data of a tenant by tenant Domain + * + * @param tenantDomain Domain of the Tenant + * @throws ApplicationManagementException thrown if an error occurs when deleting data + */ void deleteApplicationDataByTenantDomain(String tenantDomain) throws ApplicationManagementException; + + /** + * Delete all Application artifacts related to a tenant by Tenant Domain + * + * @param tenantDomain Domain of the Tenant + * @throws ApplicationManagementException thrown if an error occurs when deleting app folders + */ void deleteApplicationArtifactsByTenantDomain(String tenantDomain) throws ApplicationManagementException; } diff --git a/components/application-mgt/io.entgra.device.mgt.core.application.mgt.core/pom.xml b/components/application-mgt/io.entgra.device.mgt.core.application.mgt.core/pom.xml index 057345defca..b273d85a82c 100644 --- a/components/application-mgt/io.entgra.device.mgt.core.application.mgt.core/pom.xml +++ b/components/application-mgt/io.entgra.device.mgt.core.application.mgt.core/pom.xml @@ -81,6 +81,7 @@ com.dd.*, io.entgra.device.mgt.core.identity.jwt.client.extension.*, io.entgra.device.mgt.core.apimgt.application.extension.*, + io.entgra.device.mgt.core.tenant.mgt.common.*, org.apache.commons.httpclient, org.apache.commons.httpclient.methods, org.apache.commons.validator.routines @@ -389,6 +390,10 @@ org.wso2.carbon.tenant.mgt compile + + io.entgra.device.mgt.core + io.entgra.device.mgt.core.tenant.mgt.common + diff --git a/components/application-mgt/io.entgra.device.mgt.core.application.mgt.core/src/main/java/io/entgra/device/mgt/core/application/mgt/core/impl/ApplicationManagerImpl.java b/components/application-mgt/io.entgra.device.mgt.core.application.mgt.core/src/main/java/io/entgra/device/mgt/core/application/mgt/core/impl/ApplicationManagerImpl.java index f6d0fc228f6..5131eeeed85 100644 --- a/components/application-mgt/io.entgra.device.mgt.core.application.mgt.core/src/main/java/io/entgra/device/mgt/core/application/mgt/core/impl/ApplicationManagerImpl.java +++ b/components/application-mgt/io.entgra.device.mgt.core.application.mgt.core/src/main/java/io/entgra/device/mgt/core/application/mgt/core/impl/ApplicationManagerImpl.java @@ -30,6 +30,7 @@ import io.entgra.device.mgt.core.device.mgt.common.PaginationRequest; import io.entgra.device.mgt.core.device.mgt.common.app.mgt.App; import io.entgra.device.mgt.core.device.mgt.common.exceptions.MetadataManagementException; import io.entgra.device.mgt.core.device.mgt.common.metadata.mgt.Metadata; +import io.entgra.device.mgt.core.tenant.mgt.common.exception.TenantMgtException; import org.apache.commons.codec.digest.DigestUtils; import org.apache.commons.io.IOUtils; import org.apache.commons.lang.StringEscapeUtils; @@ -96,8 +97,6 @@ import io.entgra.device.mgt.core.device.mgt.common.exceptions.DeviceManagementEx import io.entgra.device.mgt.core.device.mgt.core.dto.DeviceType; import io.entgra.device.mgt.core.device.mgt.core.service.DeviceManagementProviderService; -import org.wso2.carbon.stratos.common.beans.TenantInfoBean; -import org.wso2.carbon.tenant.mgt.services.TenantMgtAdminService; import org.wso2.carbon.user.api.UserRealm; import org.wso2.carbon.user.api.UserStoreException; @@ -4456,19 +4455,9 @@ public class ApplicationManagerImpl implements ApplicationManager { @Override public void deleteApplicationDataByTenantDomain(String tenantDomain) throws ApplicationManagementException { int tenantId; - try{ - TenantMgtAdminService tenantMgtAdminService = new TenantMgtAdminService(); - TenantInfoBean tenantInfoBean = tenantMgtAdminService.getTenant(tenantDomain); - tenantId = tenantInfoBean.getTenantId(); - - } catch (Exception e) { - String msg = "Error getting tenant ID from domain: " - + tenantDomain; - log.error(msg, e); - throw new ApplicationManagementException(msg, e); - } - try { + tenantId = DataHolder.getInstance().getTenantManagerAdminService().getTenantId(tenantDomain); + ConnectionManagerUtil.beginDBTransaction(); vppApplicationDAO.deleteAssociationByTenant(tenantId); @@ -4495,51 +4484,49 @@ public class ApplicationManagerImpl implements ApplicationManager { ConnectionManagerUtil.commitDBTransaction(); } catch (DBConnectionException e) { - String msg = "Error occurred while observing the database connection to delete applications for tenant with ID: " - + tenantId; + String msg = "Error occurred while observing the database connection to delete applications for tenant with " + + "domain: " + tenantDomain; log.error(msg, e); throw new ApplicationManagementException(msg, e); } catch (ApplicationManagementDAOException e) { ConnectionManagerUtil.rollbackDBTransaction(); - String msg = "Database access error is occurred when getting applications for tenant with ID: " + tenantId; + String msg = "Database access error is occurred when getting applications for tenant with domain: " + + tenantDomain; log.error(msg, e); throw new ApplicationManagementException(msg, e); } catch (LifeCycleManagementDAOException e) { ConnectionManagerUtil.rollbackDBTransaction(); String msg = "Error occurred while deleting life-cycle state data of application releases of the tenant" - + " of ID: " + tenantId ; + + " of domain: " + tenantDomain ; log.error(msg, e); throw new ApplicationManagementException(msg, e); } catch (ReviewManagementDAOException e) { ConnectionManagerUtil.rollbackDBTransaction(); String msg = "Error occurred while deleting reviews of application releases of the applications" - + " of tenant ID: " + tenantId ; + + " of tenant of domain: " + tenantDomain ; + log.error(msg, e); + throw new ApplicationManagementException(msg, e); + } catch (Exception e) { + String msg = "Error getting tenant ID from domain: " + + tenantDomain; log.error(msg, e); throw new ApplicationManagementException(msg, e); - } finally { - ConnectionManagerUtil.closeDBConnection(); } } @Override public void deleteApplicationArtifactsByTenantDomain(String tenantDomain) throws ApplicationManagementException { int tenantId; - try{ - TenantMgtAdminService tenantMgtAdminService = new TenantMgtAdminService(); - TenantInfoBean tenantInfoBean = tenantMgtAdminService.getTenant(tenantDomain); - tenantId = tenantInfoBean.getTenantId(); - - } catch (Exception e) { - String msg = "Error getting tenant ID from domain: " - + tenantDomain + "when trying to delete application artifacts of tenant"; + try { + tenantId = DataHolder.getInstance().getTenantManagerAdminService().getTenantId(tenantDomain); + DataHolder.getInstance().getApplicationStorageManager().deleteAppFolderOfTenant(tenantId); + } catch (ApplicationStorageManagementException e) { + String msg = "Error deleting app artifacts of tenant of domain: " + tenantDomain ; log.error(msg, e); throw new ApplicationManagementException(msg, e); - } - try { - APIUtil.getApplicationStorageManager().deleteAppFolderOfTenant(tenantId); - } catch (ApplicationStorageManagementException e) { - String msg = "Error occurred while deleting Application folders of tenant" - + " of tenant ID: " + tenantId ; + } catch (TenantMgtException e) { + String msg = "Error getting tenant ID from domain: " + + tenantDomain + " when trying to delete application artifacts of tenant"; log.error(msg, e); throw new ApplicationManagementException(msg, e); } diff --git a/components/application-mgt/io.entgra.device.mgt.core.application.mgt.core/src/main/java/io/entgra/device/mgt/core/application/mgt/core/internal/ApplicationManagementServiceComponent.java b/components/application-mgt/io.entgra.device.mgt.core.application.mgt.core/src/main/java/io/entgra/device/mgt/core/application/mgt/core/internal/ApplicationManagementServiceComponent.java index 20cd9febbc2..d1b8ff82889 100644 --- a/components/application-mgt/io.entgra.device.mgt.core.application.mgt.core/src/main/java/io/entgra/device/mgt/core/application/mgt/core/internal/ApplicationManagementServiceComponent.java +++ b/components/application-mgt/io.entgra.device.mgt.core.application.mgt.core/src/main/java/io/entgra/device/mgt/core/application/mgt/core/internal/ApplicationManagementServiceComponent.java @@ -33,7 +33,9 @@ import io.entgra.device.mgt.core.application.mgt.core.impl.FileTransferServiceIm import io.entgra.device.mgt.core.application.mgt.core.lifecycle.LifecycleStateManager; import io.entgra.device.mgt.core.application.mgt.core.task.ScheduledAppSubscriptionTaskManager; import io.entgra.device.mgt.core.application.mgt.core.util.ApplicationManagementUtil; +import io.entgra.device.mgt.core.device.mgt.core.internal.DeviceManagementDataHolder; import io.entgra.device.mgt.core.device.mgt.core.service.DeviceManagementProviderService; +import io.entgra.device.mgt.core.tenant.mgt.common.spi.TenantManagerAdminService; import org.apache.commons.logging.Log; import org.apache.commons.logging.LogFactory; import org.osgi.framework.BundleContext; @@ -71,6 +73,12 @@ import java.util.List; * policy="dynamic" * bind="setTaskService" * unbind="unsetTaskService" + * @scr.reference name="io.entgra.device.mgt.core.tenant.manager" + * interface="io.entgra.device.mgt.core.tenant.mgt.common.spi.TenantManagerAdminService" + * cardinality="0..1" + * policy="dynamic" + * bind="setTenantManagementAdminService" + * unbind="unsetTenantManagementAdminService" */ @SuppressWarnings("unused") public class ApplicationManagementServiceComponent { @@ -196,4 +204,20 @@ public class ApplicationManagementServiceComponent { } DataHolder.getInstance().setTaskService(null); } + + @SuppressWarnings("unused") + protected void setTenantManagementAdminService(TenantManagerAdminService tenantManagerAdminService) { + if (log.isDebugEnabled()) { + log.debug("Setting Tenant management admin Service"); + } + DataHolder.getInstance().setTenantManagerAdminService(tenantManagerAdminService); + } + + @SuppressWarnings("unused") + protected void unsetTenantManagementAdminService(TenantManagerAdminService tenantManagerAdminService) { + if (log.isDebugEnabled()) { + log.debug("Un setting Tenant management admin service"); + } + DataHolder.getInstance().setTenantManagerAdminService(null); + } } diff --git a/components/application-mgt/io.entgra.device.mgt.core.application.mgt.core/src/main/java/io/entgra/device/mgt/core/application/mgt/core/internal/DataHolder.java b/components/application-mgt/io.entgra.device.mgt.core.application.mgt.core/src/main/java/io/entgra/device/mgt/core/application/mgt/core/internal/DataHolder.java index 80416dcd59d..c67db70fb6b 100644 --- a/components/application-mgt/io.entgra.device.mgt.core.application.mgt.core/src/main/java/io/entgra/device/mgt/core/application/mgt/core/internal/DataHolder.java +++ b/components/application-mgt/io.entgra.device.mgt.core.application.mgt.core/src/main/java/io/entgra/device/mgt/core/application/mgt/core/internal/DataHolder.java @@ -27,6 +27,8 @@ import io.entgra.device.mgt.core.application.mgt.common.services.SubscriptionMan import io.entgra.device.mgt.core.application.mgt.common.services.VPPApplicationManager; import io.entgra.device.mgt.core.application.mgt.core.lifecycle.LifecycleStateManager; import io.entgra.device.mgt.core.device.mgt.core.service.DeviceManagementProviderService; +import io.entgra.device.mgt.core.tenant.mgt.common.spi.TenantManagerAdminService; +import org.wso2.carbon.context.PrivilegedCarbonContext; import org.wso2.carbon.ntask.core.service.TaskService; import org.wso2.carbon.user.core.service.RealmService; @@ -57,6 +59,7 @@ public class DataHolder { private TaskService taskService; private FileTransferService fileTransferService; + private TenantManagerAdminService tenantManagerAdminService; private static final DataHolder applicationMgtDataHolder = new DataHolder(); @@ -163,4 +166,12 @@ public class DataHolder { public void setFileTransferService(FileTransferService fileTransferService) { this.fileTransferService = fileTransferService; } + + public TenantManagerAdminService getTenantManagerAdminService() { + return tenantManagerAdminService; + } + + public void setTenantManagerAdminService(TenantManagerAdminService tenantManagerAdminService) { + this.tenantManagerAdminService = tenantManagerAdminService; + } } diff --git a/components/device-mgt/io.entgra.device.mgt.core.device.mgt.api/src/main/java/io/entgra/device/mgt/core/device/mgt/api/jaxrs/service/impl/admin/UserManagementAdminServiceImpl.java b/components/device-mgt/io.entgra.device.mgt.core.device.mgt.api/src/main/java/io/entgra/device/mgt/core/device/mgt/api/jaxrs/service/impl/admin/UserManagementAdminServiceImpl.java index d0e82d7fb32..91ed3f6d420 100644 --- a/components/device-mgt/io.entgra.device.mgt.core.device.mgt.api/src/main/java/io/entgra/device/mgt/core/device/mgt/api/jaxrs/service/impl/admin/UserManagementAdminServiceImpl.java +++ b/components/device-mgt/io.entgra.device.mgt.core.device.mgt.api/src/main/java/io/entgra/device/mgt/core/device/mgt/api/jaxrs/service/impl/admin/UserManagementAdminServiceImpl.java @@ -19,6 +19,7 @@ package io.entgra.device.mgt.core.device.mgt.api.jaxrs.service.impl.admin; import io.entgra.device.mgt.core.application.mgt.common.exception.ApplicationManagementException; import io.entgra.device.mgt.core.device.mgt.common.exceptions.DeviceManagementException; +import io.entgra.device.mgt.core.tenant.mgt.common.exception.TenantMgtException; import org.apache.commons.logging.Log; import org.apache.commons.logging.LogFactory; import io.entgra.device.mgt.core.device.mgt.common.DeviceIdentifier; @@ -29,9 +30,6 @@ import io.entgra.device.mgt.core.device.mgt.api.jaxrs.util.CredentialManagementR import io.entgra.device.mgt.core.device.mgt.api.jaxrs.util.DeviceMgtAPIUtils; import org.wso2.carbon.base.MultitenantConstants; import org.wso2.carbon.context.CarbonContext; -import org.wso2.carbon.stratos.common.exception.StratosException; -import org.wso2.carbon.tenant.mgt.services.TenantMgtAdminService; -import org.wso2.carbon.user.api.UserStoreException; import javax.validation.constraints.Size; import javax.ws.rs.*; @@ -99,18 +97,20 @@ public class UserManagementAdminServiceImpl implements UserManagementAdminServic log.error(msg); return Response.status(Response.Status.UNAUTHORIZED).entity(msg).build(); } else { - if(deleteAppArtifacts){ + if (deleteAppArtifacts) { DeviceMgtAPIUtils.getApplicationManager().deleteApplicationArtifactsByTenantDomain(tenantDomain); } DeviceMgtAPIUtils.getApplicationManager().deleteApplicationDataByTenantDomain(tenantDomain); DeviceMgtAPIUtils.getDeviceManagementService().deleteDeviceDataByTenantDomain(tenantDomain); - TenantMgtAdminService tenantMgtAdminService = new TenantMgtAdminService(); - tenantMgtAdminService.deleteTenant(tenantDomain); + DeviceMgtAPIUtils.getTenantManagerAdminService().deleteTenant(tenantDomain); String msg = "Tenant Deletion process has been initiated for tenant:" + tenantDomain; + if (log.isDebugEnabled()) { + log.debug(msg); + } return Response.status(Response.Status.OK).entity(msg).build(); } - } catch (StratosException | UserStoreException e) { + } catch (TenantMgtException e) { String msg = "Error deleting tenant: " + tenantDomain; log.error(msg, e); return Response.status(Response.Status.INTERNAL_SERVER_ERROR).entity(msg).build(); diff --git a/components/device-mgt/io.entgra.device.mgt.core.device.mgt.api/src/main/java/io/entgra/device/mgt/core/device/mgt/api/jaxrs/util/DeviceMgtAPIUtils.java b/components/device-mgt/io.entgra.device.mgt.core.device.mgt.api/src/main/java/io/entgra/device/mgt/core/device/mgt/api/jaxrs/util/DeviceMgtAPIUtils.java index 885d521389a..54515725193 100644 --- a/components/device-mgt/io.entgra.device.mgt.core.device.mgt.api/src/main/java/io/entgra/device/mgt/core/device/mgt/api/jaxrs/util/DeviceMgtAPIUtils.java +++ b/components/device-mgt/io.entgra.device.mgt.core.device.mgt.api/src/main/java/io/entgra/device/mgt/core/device/mgt/api/jaxrs/util/DeviceMgtAPIUtils.java @@ -24,6 +24,7 @@ import io.entgra.device.mgt.core.application.mgt.common.services.SubscriptionMan import io.entgra.device.mgt.core.device.mgt.common.authorization.GroupAccessAuthorizationService; import io.entgra.device.mgt.core.device.mgt.common.metadata.mgt.DeviceStatusManagementService; import io.entgra.device.mgt.core.device.mgt.core.permission.mgt.PermissionManagerServiceImpl; +import io.entgra.device.mgt.core.tenant.mgt.common.spi.TenantManagerAdminService; import org.apache.axis2.AxisFault; import org.apache.axis2.client.Options; import org.apache.axis2.java.security.SSLProtocolSocketFactory; @@ -163,6 +164,7 @@ public class DeviceMgtAPIUtils { private static volatile ApplicationManager applicationManager; private static volatile APIPublisherService apiPublisher; + private static volatile TenantManagerAdminService tenantManagerAdminService; static { String keyStorePassword = ServerConfiguration.getInstance().getFirstProperty("Security.KeyStore.Password"); @@ -1243,4 +1245,21 @@ public class DeviceMgtAPIUtils { } return isPermitted; } + + public static TenantManagerAdminService getTenantManagerAdminService(){ + if(tenantManagerAdminService == null) { + synchronized (DeviceMgtAPIUtils.class) { + if (tenantManagerAdminService == null) { + tenantManagerAdminService = (TenantManagerAdminService) PrivilegedCarbonContext.getThreadLocalCarbonContext(). + getOSGiService(TenantManagerAdminService.class, null); + if (tenantManagerAdminService == null) { + String msg = "Tenant Manager Admin Service is null"; + log.error(msg); + throw new IllegalStateException(msg); + } + } + } + } + return tenantManagerAdminService; + } } diff --git a/components/tenant-mgt/io.entgra.device.mgt.core.tenant.mgt.common/src/main/java/io/entgra/device/mgt/core/tenant/mgt/common/spi/TenantManagerAdminService.java b/components/tenant-mgt/io.entgra.device.mgt.core.tenant.mgt.common/src/main/java/io/entgra/device/mgt/core/tenant/mgt/common/spi/TenantManagerAdminService.java new file mode 100644 index 00000000000..0b93e7ca5b2 --- /dev/null +++ b/components/tenant-mgt/io.entgra.device.mgt.core.tenant.mgt.common/src/main/java/io/entgra/device/mgt/core/tenant/mgt/common/spi/TenantManagerAdminService.java @@ -0,0 +1,27 @@ +/* + * Copyright (c) 2018 - 2024, Entgra (Pvt) Ltd. (http://www.entgra.io) All Rights Reserved. + * + * Entgra (Pvt) Ltd. licenses this file to you under the Apache License, + * Version 2.0 (the "License"); you may not use this file except + * in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, + * software distributed under the License is distributed on an + * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY + * KIND, either express or implied. See the License for the + * specific language governing permissions and limitations + * under the License. + */ + +package io.entgra.device.mgt.core.tenant.mgt.common.spi; + +import io.entgra.device.mgt.core.tenant.mgt.common.exception.TenantMgtException; + +public interface TenantManagerAdminService { + + void deleteTenant(String tenantDomain) throws TenantMgtException; + int getTenantId(String tenantDomain) throws TenantMgtException; +} diff --git a/components/tenant-mgt/io.entgra.device.mgt.core.tenant.mgt.core/pom.xml b/components/tenant-mgt/io.entgra.device.mgt.core.tenant.mgt.core/pom.xml index 33d4c9969c8..f89aa3cf394 100644 --- a/components/tenant-mgt/io.entgra.device.mgt.core.tenant.mgt.core/pom.xml +++ b/components/tenant-mgt/io.entgra.device.mgt.core.tenant.mgt.core/pom.xml @@ -60,6 +60,7 @@ org.wso2.carbon.stratos.common.beans, org.wso2.carbon.stratos.common.exception, org.wso2.carbon.stratos.common.listeners, + org.wso2.carbon.tenant.mgt.services, io.entgra.device.mgt.core.device.mgt.common.metadata.mgt, io.entgra.device.mgt.core.device.mgt.common.exceptions, io.entgra.device.mgt.core.device.mgt.common.permission.mgt, diff --git a/components/tenant-mgt/io.entgra.device.mgt.core.tenant.mgt.core/src/main/java/io/entgra/device/mgt/core/tenant/mgt/core/TenantManager.java b/components/tenant-mgt/io.entgra.device.mgt.core.tenant.mgt.core/src/main/java/io/entgra/device/mgt/core/tenant/mgt/core/TenantManager.java index ac3ee406f9c..ea26974300b 100644 --- a/components/tenant-mgt/io.entgra.device.mgt.core.tenant.mgt.core/src/main/java/io/entgra/device/mgt/core/tenant/mgt/core/TenantManager.java +++ b/components/tenant-mgt/io.entgra.device.mgt.core.tenant.mgt.core/src/main/java/io/entgra/device/mgt/core/tenant/mgt/core/TenantManager.java @@ -57,4 +57,5 @@ public interface TenantManager { * @throws TenantMgtException Throws when deleting Tenant related device data */ void deleteTenantDeviceData(int tenantId) throws TenantMgtException; + } diff --git a/components/tenant-mgt/io.entgra.device.mgt.core.tenant.mgt.core/src/main/java/io/entgra/device/mgt/core/tenant/mgt/core/impl/TenantManagerAdminServiceImpl.java b/components/tenant-mgt/io.entgra.device.mgt.core.tenant.mgt.core/src/main/java/io/entgra/device/mgt/core/tenant/mgt/core/impl/TenantManagerAdminServiceImpl.java new file mode 100644 index 00000000000..b96535da252 --- /dev/null +++ b/components/tenant-mgt/io.entgra.device.mgt.core.tenant.mgt.core/src/main/java/io/entgra/device/mgt/core/tenant/mgt/core/impl/TenantManagerAdminServiceImpl.java @@ -0,0 +1,57 @@ +/* + * Copyright (c) 2018 - 2024, Entgra (Pvt) Ltd. (http://www.entgra.io) All Rights Reserved. + * + * Entgra (Pvt) Ltd. licenses this file to you under the Apache License, + * Version 2.0 (the "License"); you may not use this file except + * in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, + * software distributed under the License is distributed on an + * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY + * KIND, either express or implied. See the License for the + * specific language governing permissions and limitations + * under the License. + */ + +package io.entgra.device.mgt.core.tenant.mgt.core.impl; + +import io.entgra.device.mgt.core.tenant.mgt.common.exception.TenantMgtException; +import io.entgra.device.mgt.core.tenant.mgt.common.spi.TenantManagerAdminService; +import org.apache.commons.logging.Log; +import org.apache.commons.logging.LogFactory; +import org.wso2.carbon.stratos.common.exception.StratosException; +import org.wso2.carbon.tenant.mgt.services.TenantMgtAdminService; +import org.wso2.carbon.user.api.UserStoreException; + + +public class TenantManagerAdminServiceImpl implements TenantManagerAdminService { + + private static final Log log = LogFactory.getLog(TenantManagerAdminServiceImpl.class); + + private static final TenantMgtAdminService tenantMgtAdminService = new TenantMgtAdminService(); + + @Override + public void deleteTenant(String tenantDomain) throws TenantMgtException { + try { + tenantMgtAdminService.deleteTenant(tenantDomain); + } catch (StratosException | UserStoreException e) { + String msg = "Error occurred while deleting tenant of domain: " + tenantDomain; + log.error(msg, e); + throw new TenantMgtException(msg, e); + } + } + + @Override + public int getTenantId(String tenantDomain) throws TenantMgtException { + try { + return tenantMgtAdminService.getTenant(tenantDomain).getTenantId(); + } catch (Exception e){ + String msg = "Error occurred while getting tenant ID of domain: " + tenantDomain; + log.error(msg, e); + throw new TenantMgtException(msg, e); + } + } +} diff --git a/components/tenant-mgt/io.entgra.device.mgt.core.tenant.mgt.core/src/main/java/io/entgra/device/mgt/core/tenant/mgt/core/internal/TenantMgtServiceComponent.java b/components/tenant-mgt/io.entgra.device.mgt.core.tenant.mgt.core/src/main/java/io/entgra/device/mgt/core/tenant/mgt/core/internal/TenantMgtServiceComponent.java index fa2f26c9729..c6ad710cb8d 100644 --- a/components/tenant-mgt/io.entgra.device.mgt.core.tenant.mgt.core/src/main/java/io/entgra/device/mgt/core/tenant/mgt/core/internal/TenantMgtServiceComponent.java +++ b/components/tenant-mgt/io.entgra.device.mgt.core.tenant.mgt.core/src/main/java/io/entgra/device/mgt/core/tenant/mgt/core/internal/TenantMgtServiceComponent.java @@ -20,8 +20,10 @@ package io.entgra.device.mgt.core.tenant.mgt.core.internal; import io.entgra.device.mgt.core.application.mgt.common.services.ApplicationManager; import io.entgra.device.mgt.core.device.mgt.common.metadata.mgt.DeviceStatusManagementService; import io.entgra.device.mgt.core.device.mgt.core.metadata.mgt.DeviceStatusManagementServiceImpl; +import io.entgra.device.mgt.core.tenant.mgt.common.spi.TenantManagerAdminService; import io.entgra.device.mgt.core.tenant.mgt.common.spi.TenantManagerService; import io.entgra.device.mgt.core.tenant.mgt.core.TenantManager; +import io.entgra.device.mgt.core.tenant.mgt.core.impl.TenantManagerAdminServiceImpl; import io.entgra.device.mgt.core.tenant.mgt.core.impl.TenantManagerImpl; import io.entgra.device.mgt.core.tenant.mgt.core.impl.TenantManagerServiceImpl; import io.entgra.device.mgt.core.tenant.mgt.core.listener.DeviceMgtTenantListener; @@ -59,11 +61,14 @@ public class TenantMgtServiceComponent { try { TenantManagerService tenantManagerService = new TenantManagerServiceImpl(); componentContext.getBundleContext(). - registerService(TenantManagerServiceImpl.class.getName(), tenantManagerService, null); + registerService(TenantManagerService.class.getName(), tenantManagerService, null); + TenantManagerAdminService tenantManagerAdminService = new TenantManagerAdminServiceImpl(); + componentContext.getBundleContext(). + registerService(TenantManagerAdminService.class.getName(), tenantManagerAdminService, null); TenantManager tenantManager = new TenantManagerImpl(); TenantMgtDataHolder.getInstance().setTenantManager(tenantManager); WhiteLabelManagementService whiteLabelManagementService = new WhiteLabelManagementServiceImpl(); - componentContext.getBundleContext().registerService(WhiteLabelManagementServiceImpl.class.getName(), + componentContext.getBundleContext().registerService(WhiteLabelManagementService.class.getName(), whiteLabelManagementService, null); TenantMgtDataHolder.getInstance().setWhiteLabelManagementService(whiteLabelManagementService); DeviceStatusManagementService deviceStatusManagementService = new DeviceStatusManagementServiceImpl(); diff --git a/components/tenant-mgt/io.entgra.device.mgt.core.tenant.mgt.core/src/main/java/io/entgra/device/mgt/core/tenant/mgt/core/listener/DeviceMgtTenantListener.java b/components/tenant-mgt/io.entgra.device.mgt.core.tenant.mgt.core/src/main/java/io/entgra/device/mgt/core/tenant/mgt/core/listener/DeviceMgtTenantListener.java index 257206f84f7..978e69b0e43 100644 --- a/components/tenant-mgt/io.entgra.device.mgt.core.tenant.mgt.core/src/main/java/io/entgra/device/mgt/core/tenant/mgt/core/listener/DeviceMgtTenantListener.java +++ b/components/tenant-mgt/io.entgra.device.mgt.core.tenant.mgt.core/src/main/java/io/entgra/device/mgt/core/tenant/mgt/core/listener/DeviceMgtTenantListener.java @@ -88,13 +88,5 @@ public class DeviceMgtTenantListener implements TenantMgtListener { @Override public void onPreDelete(int i) throws StratosException { // Any work to be performed before a tenant is deleted - TenantManager tenantManager = TenantMgtDataHolder.getInstance().getTenantManager(); - try{ - tenantManager.deleteTenantDeviceData(i); - tenantManager.deleteTenantApplicationData(i); - } catch (TenantMgtException e) { - String msg = "Error occurred while deleting tenant data"; - log.error(msg, e); - } } }