From e383f3b456474b25ece6bf248c42ae78f6e43056 Mon Sep 17 00:00:00 2001 From: rajitha Date: Wed, 19 Apr 2023 14:14:44 +0530 Subject: [PATCH] Refactor code --- .../DeviceManagementServiceComponent.java | 5 - .../core/util/DeviceMgtTenantMgtListener.java | 155 ------------------ .../entgra/tenant/mgt/core/TenantManager.java | 12 ++ .../mgt/core/impl/TenantManagerImpl.java | 5 - .../core/impl/TenantManagerServiceImpl.java | 12 +- .../internal/TenantMgtServiceComponent.java | 4 + .../listener/DeviceMgtTenantListener.java | 19 ++- 7 files changed, 36 insertions(+), 176 deletions(-) delete mode 100644 components/device-mgt/org.wso2.carbon.device.mgt.core/src/main/java/org/wso2/carbon/device/mgt/core/util/DeviceMgtTenantMgtListener.java diff --git a/components/device-mgt/org.wso2.carbon.device.mgt.core/src/main/java/org/wso2/carbon/device/mgt/core/internal/DeviceManagementServiceComponent.java b/components/device-mgt/org.wso2.carbon.device.mgt.core/src/main/java/org/wso2/carbon/device/mgt/core/internal/DeviceManagementServiceComponent.java index 180730dd6cb..0d24f1125c3 100644 --- a/components/device-mgt/org.wso2.carbon.device.mgt.core/src/main/java/org/wso2/carbon/device/mgt/core/internal/DeviceManagementServiceComponent.java +++ b/components/device-mgt/org.wso2.carbon.device.mgt.core/src/main/java/org/wso2/carbon/device/mgt/core/internal/DeviceManagementServiceComponent.java @@ -86,11 +86,9 @@ import org.wso2.carbon.device.mgt.core.traccar.api.service.DeviceAPIClientServic import org.wso2.carbon.device.mgt.core.traccar.api.service.impl.DeviceAPIClientServiceImpl; import org.wso2.carbon.device.mgt.core.util.DeviceManagementSchemaInitializer; import org.wso2.carbon.device.mgt.core.util.DeviceManagerUtil; -import org.wso2.carbon.device.mgt.core.util.DeviceMgtTenantMgtListener; import org.wso2.carbon.email.sender.core.service.EmailSenderService; import org.wso2.carbon.ndatasource.core.DataSourceService; import org.wso2.carbon.registry.core.service.RegistryService; -import org.wso2.carbon.stratos.common.listeners.TenantMgtListener; import org.wso2.carbon.user.core.service.RealmService; import org.wso2.carbon.utils.Axis2ConfigurationContextObserver; import org.wso2.carbon.utils.ConfigurationContextService; @@ -271,9 +269,6 @@ public class DeviceManagementServiceComponent { componentContext.getBundleContext().registerService(PrivacyComplianceProvider.class.getName(), privacyComplianceProvider, null); -// componentContext.getBundleContext() -// .registerService(TenantMgtListener.class.getName(), new DeviceMgtTenantMgtListener(), null); - if (log.isDebugEnabled()) { log.debug("Device management core bundle has been successfully initialized"); } diff --git a/components/device-mgt/org.wso2.carbon.device.mgt.core/src/main/java/org/wso2/carbon/device/mgt/core/util/DeviceMgtTenantMgtListener.java b/components/device-mgt/org.wso2.carbon.device.mgt.core/src/main/java/org/wso2/carbon/device/mgt/core/util/DeviceMgtTenantMgtListener.java deleted file mode 100644 index 79f58e82a17..00000000000 --- a/components/device-mgt/org.wso2.carbon.device.mgt.core/src/main/java/org/wso2/carbon/device/mgt/core/util/DeviceMgtTenantMgtListener.java +++ /dev/null @@ -1,155 +0,0 @@ -/* Copyright (c) 2020, 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 org.wso2.carbon.device.mgt.core.util; - -import org.apache.commons.logging.Log; -import org.apache.commons.logging.LogFactory; -import org.wso2.carbon.context.PrivilegedCarbonContext; -import org.wso2.carbon.device.mgt.common.exceptions.MetadataManagementException; -import org.wso2.carbon.device.mgt.common.permission.mgt.PermissionManagementException; -import org.wso2.carbon.device.mgt.common.roles.config.Role; -import org.wso2.carbon.device.mgt.core.config.DeviceConfigurationManager; -import org.wso2.carbon.device.mgt.core.config.DeviceManagementConfig; -import org.wso2.carbon.device.mgt.core.internal.DeviceManagementDataHolder; -import org.wso2.carbon.device.mgt.core.permission.mgt.PermissionUtils; -import org.wso2.carbon.registry.core.exceptions.RegistryException; -import org.wso2.carbon.stratos.common.beans.TenantInfoBean; -import org.wso2.carbon.stratos.common.listeners.TenantMgtListener; -import org.wso2.carbon.user.api.Permission; -import org.wso2.carbon.user.api.UserStoreException; -import org.wso2.carbon.user.api.UserStoreManager; - -import java.util.ArrayList; -import java.util.HashMap; -import java.util.List; -import java.util.Map; - -public class DeviceMgtTenantMgtListener implements TenantMgtListener { - private static final Log log = LogFactory.getLog(DeviceMgtTenantMgtListener.class); - private static final int EXEC_ORDER = 10; - private static final String PERMISSION_ACTION = "ui.execute"; - - @Override - public void onTenantCreate(TenantInfoBean tenantInfoBean) { - DeviceManagementConfig config = DeviceConfigurationManager.getInstance().getDeviceManagementConfig(); - if (config.getDefaultRoles().isEnabled()) { - Map> roleMap = getValidRoleMap(config); - try { - PrivilegedCarbonContext.startTenantFlow(); - PrivilegedCarbonContext.getThreadLocalCarbonContext() - .setTenantDomain(tenantInfoBean.getTenantDomain(), true); - UserStoreManager userStoreManager = DeviceManagementDataHolder.getInstance().getRealmService() - .getTenantUserRealm(tenantInfoBean.getTenantId()).getUserStoreManager(); - - roleMap.forEach((key, value) -> { - try { - userStoreManager.addRole(key, null, value.toArray(new Permission[0])); - } catch (UserStoreException e) { - log.error("Error occurred while adding default roles into user store.", e); - } - }); - } catch (UserStoreException e) { - log.error("Error occurred while getting user store manager.", e); - } finally { - PrivilegedCarbonContext.endTenantFlow(); - } - } - try { - DeviceManagementDataHolder.getInstance().getWhiteLabelManagementService(). - addDefaultWhiteLabelThemeIfNotExist(tenantInfoBean.getTenantId()); - } catch (MetadataManagementException e) { - log.error("Error occurred while adding default white label theme to created tenant.", e); - } - } - - @Override - public void onTenantUpdate(TenantInfoBean tenantInfoBean) { - // doing nothing - } - - @Override - public void onTenantDelete(int i) { - // doing nothing - } - - @Override - public void onTenantRename(int i, String s, String s1) { - // doing nothing - } - - @Override - public void onTenantInitialActivation(int i) { - // doing nothing - } - - @Override - public void onTenantActivation(int i) { - // doing nothing - } - - @Override - public void onTenantDeactivation(int i) { - // doing nothing - } - - @Override - public void onSubscriptionPlanChange(int i, String s, String s1) { - // doing nothing - } - - @Override - public int getListenerOrder() { - return EXEC_ORDER; - } - - @Override - public void onPreDelete(int i) { - // doing nothing - } - - /** - * Use the default roles defined in the cdm-config and evaluate the defined permissions. If permissions does not - * exist then exclude them and return role map which contains defined roles in the cdm-config and existing - * permission list as a roleMap - * @param config cdm-config - * @return {@link Map} key is role name and value is list of permissions which needs to be assigned to the role - * defined in the key. - */ - private Map> getValidRoleMap(DeviceManagementConfig config) { - Map> roleMap = new HashMap<>(); - try { - for (Role role : config.getDefaultRoles().getRoles()) { - List permissionList = new ArrayList<>(); - for (String permissionPath : role.getPermissions()) { - if (PermissionUtils.checkResourceExists(permissionPath)) { - Permission permission = new Permission(permissionPath, PERMISSION_ACTION); - - permissionList.add(permission); - } else { - log.warn("Permission " + permissionPath + " does not exist. Hence it will not add to role " - + role.getName()); - } - } - roleMap.put(role.getName(), permissionList); - } - } catch (PermissionManagementException | RegistryException e) { - log.error("Error occurred while checking permission existence.", e); - } - return roleMap; - } -} diff --git a/components/tenant-mgt/io.entgra.tenant.mgt.core/src/main/java/io/entgra/tenant/mgt/core/TenantManager.java b/components/tenant-mgt/io.entgra.tenant.mgt.core/src/main/java/io/entgra/tenant/mgt/core/TenantManager.java index 6919f520c4f..ab9b8b22900 100644 --- a/components/tenant-mgt/io.entgra.tenant.mgt.core/src/main/java/io/entgra/tenant/mgt/core/TenantManager.java +++ b/components/tenant-mgt/io.entgra.tenant.mgt.core/src/main/java/io/entgra/tenant/mgt/core/TenantManager.java @@ -21,7 +21,19 @@ import io.entgra.tenant.mgt.common.exception.TenantMgtException; import org.wso2.carbon.stratos.common.beans.TenantInfoBean; public interface TenantManager { + + /** + * Add default roles to a tenant described by the tenant info bean + * @param tenantInfoBean The info bean that provides tenant info + * @throws TenantMgtException Throws when error occurred while adding + * a role into user store or adding default white label theme to created tenant + */ void addDefaultRoles(TenantInfoBean tenantInfoBean) throws TenantMgtException; + /** + * Add default application categories to a tenant described by the tenant info bean + * @param tenantInfoBean The info bean that provides tenant info + * @throws TenantMgtException Throws when error occurred while adding default application categories + */ void addDefaultAppCategories(TenantInfoBean tenantInfoBean) throws TenantMgtException; } diff --git a/components/tenant-mgt/io.entgra.tenant.mgt.core/src/main/java/io/entgra/tenant/mgt/core/impl/TenantManagerImpl.java b/components/tenant-mgt/io.entgra.tenant.mgt.core/src/main/java/io/entgra/tenant/mgt/core/impl/TenantManagerImpl.java index fe5b16f2f38..4089b4bffaf 100644 --- a/components/tenant-mgt/io.entgra.tenant.mgt.core/src/main/java/io/entgra/tenant/mgt/core/impl/TenantManagerImpl.java +++ b/components/tenant-mgt/io.entgra.tenant.mgt.core/src/main/java/io/entgra/tenant/mgt/core/impl/TenantManagerImpl.java @@ -18,7 +18,6 @@ package io.entgra.tenant.mgt.core.impl; import io.entgra.application.mgt.common.exception.ApplicationManagementException; -import io.entgra.application.mgt.common.exception.InvalidConfigurationException; import io.entgra.application.mgt.core.config.ConfigurationManager; import io.entgra.application.mgt.common.services.ApplicationManager; import io.entgra.tenant.mgt.core.TenantManager; @@ -90,9 +89,6 @@ public class TenantManagerImpl implements TenantManager { ApplicationManager applicationManager = TenantMgtDataHolder.getInstance().getApplicationManager(); applicationManager .addApplicationCategories(ConfigurationManager.getInstance().getConfiguration().getAppCategories()); - } catch (InvalidConfigurationException e) { - String msg = "Error occurred while getting application manager"; - throw new TenantMgtException(msg, e); } catch (ApplicationManagementException e) { String msg = "Error occurred while getting default application categories"; log.error(msg, e); @@ -100,7 +96,6 @@ public class TenantManagerImpl implements TenantManager { } finally { endTenantFlow(); } - } private void initTenantFlow(TenantInfoBean tenantInfoBean) { diff --git a/components/tenant-mgt/io.entgra.tenant.mgt.core/src/main/java/io/entgra/tenant/mgt/core/impl/TenantManagerServiceImpl.java b/components/tenant-mgt/io.entgra.tenant.mgt.core/src/main/java/io/entgra/tenant/mgt/core/impl/TenantManagerServiceImpl.java index b8a82be1da0..e15d105fbf4 100644 --- a/components/tenant-mgt/io.entgra.tenant.mgt.core/src/main/java/io/entgra/tenant/mgt/core/impl/TenantManagerServiceImpl.java +++ b/components/tenant-mgt/io.entgra.tenant.mgt.core/src/main/java/io/entgra/tenant/mgt/core/impl/TenantManagerServiceImpl.java @@ -18,27 +18,19 @@ package io.entgra.tenant.mgt.core.impl; import io.entgra.tenant.mgt.common.spi.TenantManagerService; -import io.entgra.tenant.mgt.core.TenantManager; import io.entgra.tenant.mgt.common.exception.TenantMgtException; import io.entgra.tenant.mgt.core.internal.TenantMgtDataHolder; import org.wso2.carbon.stratos.common.beans.TenantInfoBean; public class TenantManagerServiceImpl implements TenantManagerService { - private final TenantManager tenantManager; - - public TenantManagerServiceImpl() { - tenantManager = new TenantManagerImpl(); - TenantMgtDataHolder.getInstance().setTenantManager(tenantManager); - } - @Override public void addDefaultRoles(TenantInfoBean tenantInfoBean) throws TenantMgtException { - tenantManager.addDefaultRoles(tenantInfoBean); + TenantMgtDataHolder.getInstance().getTenantManager().addDefaultRoles(tenantInfoBean); } @Override public void addDefaultAppCategories(TenantInfoBean tenantInfoBean) throws TenantMgtException { - tenantManager.addDefaultAppCategories(tenantInfoBean); + TenantMgtDataHolder.getInstance().getTenantManager().addDefaultAppCategories(tenantInfoBean); } } diff --git a/components/tenant-mgt/io.entgra.tenant.mgt.core/src/main/java/io/entgra/tenant/mgt/core/internal/TenantMgtServiceComponent.java b/components/tenant-mgt/io.entgra.tenant.mgt.core/src/main/java/io/entgra/tenant/mgt/core/internal/TenantMgtServiceComponent.java index fafdbbbea75..cab03921c06 100644 --- a/components/tenant-mgt/io.entgra.tenant.mgt.core/src/main/java/io/entgra/tenant/mgt/core/internal/TenantMgtServiceComponent.java +++ b/components/tenant-mgt/io.entgra.tenant.mgt.core/src/main/java/io/entgra/tenant/mgt/core/internal/TenantMgtServiceComponent.java @@ -19,6 +19,8 @@ package io.entgra.tenant.mgt.core.internal; import io.entgra.application.mgt.common.services.ApplicationManager; import io.entgra.tenant.mgt.common.spi.TenantManagerService; +import io.entgra.tenant.mgt.core.TenantManager; +import io.entgra.tenant.mgt.core.impl.TenantManagerImpl; import io.entgra.tenant.mgt.core.impl.TenantManagerServiceImpl; import io.entgra.tenant.mgt.core.listener.DeviceMgtTenantListener; import org.apache.commons.logging.Log; @@ -56,6 +58,8 @@ public class TenantMgtServiceComponent { TenantManagerService tenantManagerService = new TenantManagerServiceImpl(); componentContext.getBundleContext(). registerService(TenantManagerServiceImpl.class.getName(), tenantManagerService, null); + TenantManager tenantManager = new TenantManagerImpl(); + TenantMgtDataHolder.getInstance().setTenantManager(tenantManager); WhiteLabelManagementService whiteLabelManagementService = new WhiteLabelManagementServiceImpl(); componentContext.getBundleContext().registerService(WhiteLabelManagementServiceImpl.class.getName(), whiteLabelManagementService, null); diff --git a/components/tenant-mgt/io.entgra.tenant.mgt.core/src/main/java/io/entgra/tenant/mgt/core/listener/DeviceMgtTenantListener.java b/components/tenant-mgt/io.entgra.tenant.mgt.core/src/main/java/io/entgra/tenant/mgt/core/listener/DeviceMgtTenantListener.java index 43e31cf4e08..a272585bf72 100644 --- a/components/tenant-mgt/io.entgra.tenant.mgt.core/src/main/java/io/entgra/tenant/mgt/core/listener/DeviceMgtTenantListener.java +++ b/components/tenant-mgt/io.entgra.tenant.mgt.core/src/main/java/io/entgra/tenant/mgt/core/listener/DeviceMgtTenantListener.java @@ -1,3 +1,20 @@ +/* + * Copyright (c) 2023, Entgra Pvt Ltd. (http://www.wso2.org) 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.tenant.mgt.core.listener; import io.entgra.tenant.mgt.core.TenantManager; @@ -12,7 +29,7 @@ import org.wso2.carbon.stratos.common.listeners.TenantMgtListener; public class DeviceMgtTenantListener implements TenantMgtListener { private static final Log log = LogFactory.getLog(DeviceMgtTenantListener.class); - public static final int LISTENER_EXECUTION_ORDER = 11; + public static final int LISTENER_EXECUTION_ORDER = 10; @Override public void onTenantCreate(TenantInfoBean tenantInfoBean) {