From 64d194bfea72d6a5f55ac20f41f1c8f92d13d401 Mon Sep 17 00:00:00 2001 From: rajitha Date: Mon, 10 Apr 2023 03:29:10 +0530 Subject: [PATCH 1/5] add tenant-mgt module --- .../DeviceManagementServiceComponent.java | 4 +- .../io.entgra.tenant.mgt.common/pom.xml | 49 +++++++ .../common/exception/TenantMgtException.java | 11 ++ .../mgt/common/spi/TenantManagerService.java | 10 ++ .../io.entgra.tenant.mgt.core/pom.xml | 115 +++++++++++++++++ .../entgra/tenant/mgt/core/TenantManager.java | 10 ++ .../mgt/core/impl/TenantManagerImpl.java | 122 ++++++++++++++++++ .../core/impl/TenantManagerServiceImpl.java | 27 ++++ .../core/internal/TenantMgtDataHolder.java | 53 ++++++++ .../internal/TenantMgtServiceComponent.java | 74 +++++++++++ .../listener/DeviceMgtTenantListener.java | 74 +++++++++++ components/tenant-mgt/pom.xml | 23 ++++ .../pom.xml | 94 ++++++++++++++ .../src/main/resources/build.properties | 1 + .../src/main/resources/p2.inf | 1 + features/tenant-mgt/pom.xml | 19 +++ pom.xml | 15 ++- 17 files changed, 699 insertions(+), 3 deletions(-) create mode 100644 components/tenant-mgt/io.entgra.tenant.mgt.common/pom.xml create mode 100644 components/tenant-mgt/io.entgra.tenant.mgt.common/src/main/java/io/entgra/tenant/mgt/common/exception/TenantMgtException.java create mode 100644 components/tenant-mgt/io.entgra.tenant.mgt.common/src/main/java/io/entgra/tenant/mgt/common/spi/TenantManagerService.java create mode 100644 components/tenant-mgt/io.entgra.tenant.mgt.core/pom.xml create mode 100644 components/tenant-mgt/io.entgra.tenant.mgt.core/src/main/java/io/entgra/tenant/mgt/core/TenantManager.java create mode 100644 components/tenant-mgt/io.entgra.tenant.mgt.core/src/main/java/io/entgra/tenant/mgt/core/impl/TenantManagerImpl.java create mode 100644 components/tenant-mgt/io.entgra.tenant.mgt.core/src/main/java/io/entgra/tenant/mgt/core/impl/TenantManagerServiceImpl.java create mode 100644 components/tenant-mgt/io.entgra.tenant.mgt.core/src/main/java/io/entgra/tenant/mgt/core/internal/TenantMgtDataHolder.java create mode 100644 components/tenant-mgt/io.entgra.tenant.mgt.core/src/main/java/io/entgra/tenant/mgt/core/internal/TenantMgtServiceComponent.java create mode 100644 components/tenant-mgt/io.entgra.tenant.mgt.core/src/main/java/io/entgra/tenant/mgt/core/listener/DeviceMgtTenantListener.java create mode 100644 components/tenant-mgt/pom.xml create mode 100644 features/tenant-mgt/io.entgra.tenant.mgt.server.feature/pom.xml create mode 100644 features/tenant-mgt/io.entgra.tenant.mgt.server.feature/src/main/resources/build.properties create mode 100644 features/tenant-mgt/io.entgra.tenant.mgt.server.feature/src/main/resources/p2.inf create mode 100644 features/tenant-mgt/pom.xml 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 f36def494d..180730dd6c 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 @@ -271,8 +271,8 @@ public class DeviceManagementServiceComponent { componentContext.getBundleContext().registerService(PrivacyComplianceProvider.class.getName(), privacyComplianceProvider, null); - componentContext.getBundleContext() - .registerService(TenantMgtListener.class.getName(), new DeviceMgtTenantMgtListener(), 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/tenant-mgt/io.entgra.tenant.mgt.common/pom.xml b/components/tenant-mgt/io.entgra.tenant.mgt.common/pom.xml new file mode 100644 index 0000000000..2c81fb58c6 --- /dev/null +++ b/components/tenant-mgt/io.entgra.tenant.mgt.common/pom.xml @@ -0,0 +1,49 @@ + + + + tenant-mgt + org.wso2.carbon.devicemgt + 5.0.25-SNAPSHOT + ../pom.xml + + + 4.0.0 + io.entgra.tenant.mgt.common + Entgra IoT - Tenant Manager Common + Entgra IoT - Tenant Manager Common + bundle + + + + + org.apache.felix + maven-bundle-plugin + true + + + ${project.artifactId} + ${project.artifactId} + ${carbon.device.mgt.version} + Tenant Management Common Bundle + + org.apache.commons.logging, + org.wso2.carbon.stratos.common.beans + + io.entgra.tenant.mgt.common.* + + + + + + + + + org.wso2.carbon.commons + org.wso2.carbon.tenant.common + ${carbon.commons.version} + + + + \ No newline at end of file diff --git a/components/tenant-mgt/io.entgra.tenant.mgt.common/src/main/java/io/entgra/tenant/mgt/common/exception/TenantMgtException.java b/components/tenant-mgt/io.entgra.tenant.mgt.common/src/main/java/io/entgra/tenant/mgt/common/exception/TenantMgtException.java new file mode 100644 index 0000000000..c0e278bd2b --- /dev/null +++ b/components/tenant-mgt/io.entgra.tenant.mgt.common/src/main/java/io/entgra/tenant/mgt/common/exception/TenantMgtException.java @@ -0,0 +1,11 @@ +package io.entgra.tenant.mgt.common.exception; + +public class TenantMgtException extends Exception { + public TenantMgtException(String msg, Throwable ex) { + super(msg, ex); + } + + public TenantMgtException(String msg) { + super(msg); + } +} diff --git a/components/tenant-mgt/io.entgra.tenant.mgt.common/src/main/java/io/entgra/tenant/mgt/common/spi/TenantManagerService.java b/components/tenant-mgt/io.entgra.tenant.mgt.common/src/main/java/io/entgra/tenant/mgt/common/spi/TenantManagerService.java new file mode 100644 index 0000000000..a721e03029 --- /dev/null +++ b/components/tenant-mgt/io.entgra.tenant.mgt.common/src/main/java/io/entgra/tenant/mgt/common/spi/TenantManagerService.java @@ -0,0 +1,10 @@ +package io.entgra.tenant.mgt.common.spi; + +import io.entgra.tenant.mgt.common.exception.TenantMgtException; +import org.wso2.carbon.stratos.common.beans.TenantInfoBean; + +public interface TenantManagerService { + void addDefaultRoles(TenantInfoBean tenantInfoBean) throws TenantMgtException; + + void addDefaultAppCategories(TenantInfoBean tenantInfoBean) throws TenantMgtException; +} \ No newline at end of file diff --git a/components/tenant-mgt/io.entgra.tenant.mgt.core/pom.xml b/components/tenant-mgt/io.entgra.tenant.mgt.core/pom.xml new file mode 100644 index 0000000000..76284fea49 --- /dev/null +++ b/components/tenant-mgt/io.entgra.tenant.mgt.core/pom.xml @@ -0,0 +1,115 @@ + + + + tenant-mgt + org.wso2.carbon.devicemgt + 5.0.25-SNAPSHOT + ../pom.xml + + + 4.0.0 + io.entgra.tenant.mgt.core + Entgra IoT - Tenant Manager Core + Entgra IoT - Tenant Manager Core + bundle + + + + + org.apache.felix + maven-scr-plugin + + + org.apache.felix + maven-bundle-plugin + true + + + ${project.artifactId} + ${project.artifactId} + ${carbon.device.mgt.version} + Tenant Management Core Bundle + io.entgra.tenant.mgt.core.internal + + org.osgi.framework.*;version="${imp.package.version.osgi.framework}", + org.osgi.service.*;version="${imp.package.version.osgi.service}", + org.apache.commons.logging, + io.entgra.application.mgt.common.*, + io.entgra.application.mgt.core.config, + org.wso2.carbon.device.mgt.core, + org.wso2.carbon.user.api, + org.wso2.carbon.registry.core.exceptions, + io.entgra.tenant.mgt.common.*, + org.wso2.carbon.stratos.common.beans, + org.wso2.carbon.stratos.common.exception, + org.wso2.carbon.stratos.common.listeners, + org.wso2.carbon.device.mgt.common.metadata.mgt, + org.wso2.carbon.device.mgt.core.metadata.mgt, + org.wso2.carbon.user.core.service, + org.wso2.carbon.context + + + !io.entgra.tenant.mgt.core.internal, + io.entgra.tenant.mgt.core.* + + + + + + + + + + org.eclipse.osgi + org.eclipse.osgi + + + org.eclipse.osgi + org.eclipse.osgi.services + + + org.wso2.carbon.commons + org.wso2.carbon.tenant.common + ${carbon.commons.version} + + + org.wso2.carbon + org.wso2.carbon.registry.core + + + org.wso2.carbon + org.wso2.carbon.user.core + + + org.wso2.carbon + org.wso2.carbon.user.api + + + org.wso2.carbon.devicemgt + org.wso2.carbon.device.mgt.core + + + org.wso2.carbon.devicemgt + org.wso2.carbon.device.mgt.common + + + org.wso2.carbon.devicemgt + io.entgra.application.mgt.core + + + org.wso2.carbon.devicemgt + io.entgra.application.mgt.common + + + org.wso2.carbon.devicemgt + io.entgra.tenant.mgt.common + + + org.wso2.carbon + org.wso2.carbon.utils + + + + \ No newline at end of file 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 new file mode 100644 index 0000000000..2f9b63281b --- /dev/null +++ b/components/tenant-mgt/io.entgra.tenant.mgt.core/src/main/java/io/entgra/tenant/mgt/core/TenantManager.java @@ -0,0 +1,10 @@ +package io.entgra.tenant.mgt.core; + +import io.entgra.tenant.mgt.common.exception.TenantMgtException; +import org.wso2.carbon.stratos.common.beans.TenantInfoBean; + +public interface TenantManager { + void addDefaultRoles(TenantInfoBean tenantInfoBean) throws TenantMgtException; + + 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 new file mode 100644 index 0000000000..8782f7ca1a --- /dev/null +++ b/components/tenant-mgt/io.entgra.tenant.mgt.core/src/main/java/io/entgra/tenant/mgt/core/impl/TenantManagerImpl.java @@ -0,0 +1,122 @@ +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; +import io.entgra.tenant.mgt.common.exception.TenantMgtException; +import io.entgra.tenant.mgt.core.internal.TenantMgtDataHolder; +import org.apache.commons.logging.Log; +import org.apache.commons.logging.LogFactory; +import org.wso2.carbon.context.PrivilegedCarbonContext; +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.permission.mgt.PermissionUtils; +import org.wso2.carbon.registry.core.exceptions.RegistryException; +import org.wso2.carbon.stratos.common.beans.TenantInfoBean; +import org.wso2.carbon.user.api.Permission; +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.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 TenantManagerImpl implements TenantManager { + private static final Log log = LogFactory.getLog(TenantManagerImpl.class); + private static final String PERMISSION_ACTION = "ui.execute"; + + @Override + public void addDefaultRoles(TenantInfoBean tenantInfoBean) throws TenantMgtException { + initTenantFlow(tenantInfoBean); + DeviceManagementConfig config = DeviceConfigurationManager.getInstance().getDeviceManagementConfig(); + if (config.getDefaultRoles().isEnabled()) { + Map> roleMap = getValidRoleMap(config); + try { + UserStoreManager userStoreManager = TenantMgtDataHolder.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) { + String msg = "Error occurred while getting user store manager"; + log.error(msg, e); + throw new TenantMgtException(msg, e); + } + } + try { + TenantMgtDataHolder.getInstance().getWhiteLabelManagementService(). + addDefaultWhiteLabelThemeIfNotExist(tenantInfoBean.getTenantId()); + } catch (MetadataManagementException e) { + String msg = "Error occurred while adding default white label theme to created tenant - id "+tenantInfoBean.getTenantId(); + log.error(msg, e); + throw new TenantMgtException(msg, e); + } finally { + endTenantFlow(); + } + } + + @Override + public void addDefaultAppCategories(TenantInfoBean tenantInfoBean) throws TenantMgtException { + initTenantFlow(tenantInfoBean); + try { + 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); + throw new TenantMgtException(msg, e); + } finally { + endTenantFlow(); + } + + } + + private void initTenantFlow(TenantInfoBean tenantInfoBean) { + PrivilegedCarbonContext.startTenantFlow(); + PrivilegedCarbonContext privilegedCarbonContext = PrivilegedCarbonContext.getThreadLocalCarbonContext(); + privilegedCarbonContext.setTenantId(tenantInfoBean.getTenantId()); + privilegedCarbonContext.setTenantDomain(tenantInfoBean.getTenantDomain()); + } + + private void endTenantFlow() { + PrivilegedCarbonContext.endTenantFlow(); + } + + 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/impl/TenantManagerServiceImpl.java b/components/tenant-mgt/io.entgra.tenant.mgt.core/src/main/java/io/entgra/tenant/mgt/core/impl/TenantManagerServiceImpl.java new file mode 100644 index 0000000000..760e49f4e8 --- /dev/null +++ b/components/tenant-mgt/io.entgra.tenant.mgt.core/src/main/java/io/entgra/tenant/mgt/core/impl/TenantManagerServiceImpl.java @@ -0,0 +1,27 @@ +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); + } + + @Override + public void addDefaultAppCategories(TenantInfoBean tenantInfoBean) throws TenantMgtException { + tenantManager.addDefaultAppCategories(tenantInfoBean); + } +} diff --git a/components/tenant-mgt/io.entgra.tenant.mgt.core/src/main/java/io/entgra/tenant/mgt/core/internal/TenantMgtDataHolder.java b/components/tenant-mgt/io.entgra.tenant.mgt.core/src/main/java/io/entgra/tenant/mgt/core/internal/TenantMgtDataHolder.java new file mode 100644 index 0000000000..6f9d603a3f --- /dev/null +++ b/components/tenant-mgt/io.entgra.tenant.mgt.core/src/main/java/io/entgra/tenant/mgt/core/internal/TenantMgtDataHolder.java @@ -0,0 +1,53 @@ +package io.entgra.tenant.mgt.core.internal; + +import io.entgra.application.mgt.common.services.ApplicationManager; +import io.entgra.tenant.mgt.core.TenantManager; +import org.wso2.carbon.device.mgt.common.metadata.mgt.WhiteLabelManagementService; +import org.wso2.carbon.user.core.service.RealmService; + +public class TenantMgtDataHolder { + private static final TenantMgtDataHolder instance = new TenantMgtDataHolder(); + private TenantManager tenantManager; + + private ApplicationManager applicationManager; + + private WhiteLabelManagementService whiteLabelManagementService; + + private RealmService realmService; + + public RealmService getRealmService() { + return realmService; + } + + public void setRealmService(RealmService realmService) { + this.realmService = realmService; + } + + public ApplicationManager getApplicationManager() { + return applicationManager; + } + + public void setApplicationManager(ApplicationManager applicationManager) { + this.applicationManager = applicationManager; + } + + public WhiteLabelManagementService getWhiteLabelManagementService() { + return whiteLabelManagementService; + } + + public void setWhiteLabelManagementService(WhiteLabelManagementService whiteLabelManagementService) { + this.whiteLabelManagementService = whiteLabelManagementService; + } + + public TenantManager getTenantManager() { + return tenantManager; + } + + public void setTenantManager(TenantManager tenantManager) { + this.tenantManager = tenantManager; + } + + public static TenantMgtDataHolder getInstance() { + return instance; + } +} 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 new file mode 100644 index 0000000000..ac2b0be69b --- /dev/null +++ b/components/tenant-mgt/io.entgra.tenant.mgt.core/src/main/java/io/entgra/tenant/mgt/core/internal/TenantMgtServiceComponent.java @@ -0,0 +1,74 @@ +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.impl.TenantManagerServiceImpl; +import io.entgra.tenant.mgt.core.listener.DeviceMgtTenantListener; +import org.apache.commons.logging.Log; +import org.apache.commons.logging.LogFactory; +import org.osgi.service.component.ComponentContext; +import org.wso2.carbon.device.mgt.common.metadata.mgt.WhiteLabelManagementService; +import org.wso2.carbon.device.mgt.core.metadata.mgt.WhiteLabelManagementServiceImpl; +import org.wso2.carbon.user.core.service.RealmService; + +/** + * @scr.component name="org.wso2.carbon.devicemgt.tenant.manager" immediate="true" + * @scr.reference name="org.wso2.carbon.application.mgt.service" + * interface="io.entgra.application.mgt.common.services.ApplicationManager" + * cardinality="1..1" + * policy="dynamic" + * bind="setApplicationManager" + * unbind="unsetApplicationManager" + * @scr.reference name="user.realmservice.default" + * interface="org.wso2.carbon.user.core.service.RealmService" + * cardinality="1..1" + * policy="dynamic" + * bind="setRealmService" + * unbind="unsetRealmService" + */ + +public class TenantMgtServiceComponent { + + private static final Log log = LogFactory.getLog(TenantManagerService.class); + + @SuppressWarnings("unused") + protected void activate(ComponentContext componentContext) { + try { + TenantManagerService tenantManagerService = new TenantManagerServiceImpl(); + componentContext.getBundleContext(). + registerService(TenantManagerServiceImpl.class.getName(), tenantManagerService, null); + WhiteLabelManagementService whiteLabelManagementService = new WhiteLabelManagementServiceImpl(); + componentContext.getBundleContext().registerService(WhiteLabelManagementServiceImpl.class.getName(), + whiteLabelManagementService, null); + TenantMgtDataHolder.getInstance().setWhiteLabelManagementService(whiteLabelManagementService); + DeviceMgtTenantListener deviceMgtTenantListener = new DeviceMgtTenantListener(); + componentContext.getBundleContext(). + registerService(DeviceMgtTenantListener.class.getName(), deviceMgtTenantListener, null); + log.info("Tenant management service activated"); + } catch (Throwable t) { + String msg = "Error occurred while activating tenant management service"; + log.error(msg, t); + } + } + + @SuppressWarnings("unused") + protected void deactivate(ComponentContext componentContext) { + // nothing to do + } + + protected void setApplicationManager(ApplicationManager applicationManager) { + TenantMgtDataHolder.getInstance().setApplicationManager(applicationManager); + } + + protected void unsetApplicationManager(ApplicationManager applicationManager) { + TenantMgtDataHolder.getInstance().setApplicationManager(null); + } + + protected void setRealmService(RealmService realmService) { + TenantMgtDataHolder.getInstance().setRealmService(realmService); + } + + protected void unsetRealmService(RealmService realmService) { + TenantMgtDataHolder.getInstance().setRealmService(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 new file mode 100644 index 0000000000..43e31cf4e0 --- /dev/null +++ b/components/tenant-mgt/io.entgra.tenant.mgt.core/src/main/java/io/entgra/tenant/mgt/core/listener/DeviceMgtTenantListener.java @@ -0,0 +1,74 @@ +package io.entgra.tenant.mgt.core.listener; + +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.apache.commons.logging.Log; +import org.apache.commons.logging.LogFactory; +import org.wso2.carbon.stratos.common.beans.TenantInfoBean; +import org.wso2.carbon.stratos.common.exception.StratosException; +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; + + @Override + public void onTenantCreate(TenantInfoBean tenantInfoBean) { + // Any work to be performed after a tenant creation + TenantManager tenantManager = TenantMgtDataHolder.getInstance().getTenantManager(); + try { + tenantManager.addDefaultRoles(tenantInfoBean); + tenantManager.addDefaultAppCategories(tenantInfoBean); + } catch (TenantMgtException e) { + String msg = "Error occurred while executing tenant creation flow"; + log.error(msg, e); + } + } + + @Override + public void onTenantUpdate(TenantInfoBean tenantInfoBean) throws StratosException { + // Any work to be performed after a tenant information update happens + } + + @Override + public void onTenantDelete(int i) { + // Any work to be performed after a tenant deletion + } + + @Override + public void onTenantRename(int i, String s, String s1) throws StratosException { + // Any work to be performed after a tenant rename happens + } + + @Override + public void onTenantInitialActivation(int i) throws StratosException { + // Any work to be performed after a tenant's initial activation happens + } + + @Override + public void onTenantActivation(int i) throws StratosException { + // Any work to be performed after a tenant activation + } + + @Override + public void onTenantDeactivation(int i) throws StratosException { + // Any work to be performed after a tenant deactivation + } + + @Override + public void onSubscriptionPlanChange(int i, String s, String s1) throws StratosException { + // Any work to be performed after subscription plan change + } + + @Override + public int getListenerOrder() { + return LISTENER_EXECUTION_ORDER; + } + + @Override + public void onPreDelete(int i) throws StratosException { + // Any work to be performed before a tenant is deleted + } +} diff --git a/components/tenant-mgt/pom.xml b/components/tenant-mgt/pom.xml new file mode 100644 index 0000000000..58b3ebceb0 --- /dev/null +++ b/components/tenant-mgt/pom.xml @@ -0,0 +1,23 @@ + + + + carbon-devicemgt + org.wso2.carbon.devicemgt + 5.0.25-SNAPSHOT + ../../pom.xml + + + 4.0.0 + tenant-mgt + pom + Entgra IoT - Tenant Management Component + Entgra IoT - Tenant Management Component + + + io.entgra.tenant.mgt.core + io.entgra.tenant.mgt.common + + + \ No newline at end of file diff --git a/features/tenant-mgt/io.entgra.tenant.mgt.server.feature/pom.xml b/features/tenant-mgt/io.entgra.tenant.mgt.server.feature/pom.xml new file mode 100644 index 0000000000..04cf1bb3e5 --- /dev/null +++ b/features/tenant-mgt/io.entgra.tenant.mgt.server.feature/pom.xml @@ -0,0 +1,94 @@ + + + + tenant-mgt-feature + org.wso2.carbon.devicemgt + 5.0.25-SNAPSHOT + ../pom.xml + + + 4.0.0 + io.entgra.tenant.mgt.server.feature + Entgra IoT - Tenant Management Server Feature + Entgra IoT - Tenant Management Server Feature + pom + + + + org.wso2.carbon.devicemgt + io.entgra.tenant.mgt.core + + + org.wso2.carbon.devicemgt + io.entgra.tenant.mgt.common + + + + + + + maven-resources-plugin + 2.6 + + + copy-resources + generate-resources + + copy-resources + + + src/main/resources + + + resources + + build.properties + p2.inf + + + + + + + + + org.wso2.maven + carbon-p2-plugin + ${carbon.p2.plugin.version} + + + p2-feature-generation + package + + p2-feature-gen + + + io.entgra.tenant.mgt.server + ../../etc/feature.properties + + + org.wso2.carbon.p2.category.type:server + + org.eclipse.equinox.p2.type.group:true + + + + + + + org.wso2.carbon.devicemgt:io.entgra.tenant.mgt.core:${carbon.device.mgt.version} + + + org.wso2.carbon.devicemgt:io.entgra.tenant.mgt.common:${carbon.device.mgt.version} + + + + + + + + + + \ No newline at end of file diff --git a/features/tenant-mgt/io.entgra.tenant.mgt.server.feature/src/main/resources/build.properties b/features/tenant-mgt/io.entgra.tenant.mgt.server.feature/src/main/resources/build.properties new file mode 100644 index 0000000000..920f577888 --- /dev/null +++ b/features/tenant-mgt/io.entgra.tenant.mgt.server.feature/src/main/resources/build.properties @@ -0,0 +1 @@ +custom=true \ No newline at end of file diff --git a/features/tenant-mgt/io.entgra.tenant.mgt.server.feature/src/main/resources/p2.inf b/features/tenant-mgt/io.entgra.tenant.mgt.server.feature/src/main/resources/p2.inf new file mode 100644 index 0000000000..7ab37b9d7d --- /dev/null +++ b/features/tenant-mgt/io.entgra.tenant.mgt.server.feature/src/main/resources/p2.inf @@ -0,0 +1 @@ +instructions.configure = \ \ No newline at end of file diff --git a/features/tenant-mgt/pom.xml b/features/tenant-mgt/pom.xml new file mode 100644 index 0000000000..d55378a8cb --- /dev/null +++ b/features/tenant-mgt/pom.xml @@ -0,0 +1,19 @@ + + + + + carbon-devicemgt + org.wso2.carbon.devicemgt + 5.0.25-SNAPSHOT + ../../pom.xml + + + 4.0.0 + tenant-mgt-feature + pom + Entgra IoT - Tenant Management Feature + + + io.entgra.tenant.mgt.server.feature + + \ No newline at end of file diff --git a/pom.xml b/pom.xml index 2851e5865f..7eee718846 100644 --- a/pom.xml +++ b/pom.xml @@ -48,6 +48,7 @@ components/webapp-authenticator-framework components/logger components/task-mgt + components/tenant-mgt features/device-mgt features/apimgt-extensions features/application-mgt @@ -62,7 +63,7 @@ features/webapp-authenticator-framework features/logger features/task-mgt - + features/tenant-mgt @@ -370,6 +371,18 @@ + + + org.wso2.carbon.devicemgt + io.entgra.tenant.mgt.common + ${carbon.device.mgt.version} + + + org.wso2.carbon.devicemgt + io.entgra.tenant.mgt.core + ${carbon.device.mgt.version} + + org.wso2.carbon.devicemgt From c33f402fd13e5176c72c25875a9655229a845d57 Mon Sep 17 00:00:00 2001 From: rajitha Date: Tue, 18 Apr 2023 15:34:07 +0530 Subject: [PATCH 2/5] Fix bundling issues --- components/tenant-mgt/io.entgra.tenant.mgt.core/pom.xml | 4 ++++ .../tenant/mgt/core/internal/TenantMgtServiceComponent.java | 4 +++- 2 files changed, 7 insertions(+), 1 deletion(-) diff --git a/components/tenant-mgt/io.entgra.tenant.mgt.core/pom.xml b/components/tenant-mgt/io.entgra.tenant.mgt.core/pom.xml index 76284fea49..5ec9904eb3 100644 --- a/components/tenant-mgt/io.entgra.tenant.mgt.core/pom.xml +++ b/components/tenant-mgt/io.entgra.tenant.mgt.core/pom.xml @@ -46,7 +46,11 @@ org.wso2.carbon.stratos.common.exception, org.wso2.carbon.stratos.common.listeners, org.wso2.carbon.device.mgt.common.metadata.mgt, + org.wso2.carbon.device.mgt.common.exceptions, + org.wso2.carbon.device.mgt.common.permission.mgt, + org.wso2.carbon.device.mgt.common.roles.config, org.wso2.carbon.device.mgt.core.metadata.mgt, + org.wso2.carbon.device.mgt.core.config, org.wso2.carbon.user.core.service, org.wso2.carbon.context 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 ac2b0be69b..534c50c59d 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 @@ -9,6 +9,7 @@ import org.apache.commons.logging.LogFactory; import org.osgi.service.component.ComponentContext; import org.wso2.carbon.device.mgt.common.metadata.mgt.WhiteLabelManagementService; import org.wso2.carbon.device.mgt.core.metadata.mgt.WhiteLabelManagementServiceImpl; +import org.wso2.carbon.stratos.common.listeners.TenantMgtListener; import org.wso2.carbon.user.core.service.RealmService; /** @@ -27,6 +28,7 @@ import org.wso2.carbon.user.core.service.RealmService; * unbind="unsetRealmService" */ +@SuppressWarnings("unused") public class TenantMgtServiceComponent { private static final Log log = LogFactory.getLog(TenantManagerService.class); @@ -43,7 +45,7 @@ public class TenantMgtServiceComponent { TenantMgtDataHolder.getInstance().setWhiteLabelManagementService(whiteLabelManagementService); DeviceMgtTenantListener deviceMgtTenantListener = new DeviceMgtTenantListener(); componentContext.getBundleContext(). - registerService(DeviceMgtTenantListener.class.getName(), deviceMgtTenantListener, null); + registerService(TenantMgtListener.class.getName(), deviceMgtTenantListener, null); log.info("Tenant management service activated"); } catch (Throwable t) { String msg = "Error occurred while activating tenant management service"; From 4c2a02fdf03a3cb352611af039dd645f0d726d93 Mon Sep 17 00:00:00 2001 From: rajitha Date: Tue, 18 Apr 2023 15:40:28 +0530 Subject: [PATCH 3/5] Add license --- .../io.entgra.tenant.mgt.common/pom.xml | 17 +++++++++++++++++ .../common/exception/TenantMgtException.java | 17 +++++++++++++++++ .../mgt/common/spi/TenantManagerService.java | 17 +++++++++++++++++ .../io.entgra.tenant.mgt.core/pom.xml | 17 +++++++++++++++++ .../entgra/tenant/mgt/core/TenantManager.java | 17 +++++++++++++++++ .../tenant/mgt/core/impl/TenantManagerImpl.java | 17 +++++++++++++++++ .../mgt/core/impl/TenantManagerServiceImpl.java | 17 +++++++++++++++++ .../mgt/core/internal/TenantMgtDataHolder.java | 17 +++++++++++++++++ .../internal/TenantMgtServiceComponent.java | 17 +++++++++++++++++ components/tenant-mgt/pom.xml | 17 +++++++++++++++++ .../io.entgra.tenant.mgt.server.feature/pom.xml | 17 +++++++++++++++++ features/tenant-mgt/pom.xml | 17 +++++++++++++++++ 12 files changed, 204 insertions(+) diff --git a/components/tenant-mgt/io.entgra.tenant.mgt.common/pom.xml b/components/tenant-mgt/io.entgra.tenant.mgt.common/pom.xml index 2c81fb58c6..b69c2bb112 100644 --- a/components/tenant-mgt/io.entgra.tenant.mgt.common/pom.xml +++ b/components/tenant-mgt/io.entgra.tenant.mgt.common/pom.xml @@ -1,4 +1,21 @@ + diff --git a/components/tenant-mgt/io.entgra.tenant.mgt.common/src/main/java/io/entgra/tenant/mgt/common/exception/TenantMgtException.java b/components/tenant-mgt/io.entgra.tenant.mgt.common/src/main/java/io/entgra/tenant/mgt/common/exception/TenantMgtException.java index c0e278bd2b..f9b8c22e62 100644 --- a/components/tenant-mgt/io.entgra.tenant.mgt.common/src/main/java/io/entgra/tenant/mgt/common/exception/TenantMgtException.java +++ b/components/tenant-mgt/io.entgra.tenant.mgt.common/src/main/java/io/entgra/tenant/mgt/common/exception/TenantMgtException.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.common.exception; public class TenantMgtException extends Exception { diff --git a/components/tenant-mgt/io.entgra.tenant.mgt.common/src/main/java/io/entgra/tenant/mgt/common/spi/TenantManagerService.java b/components/tenant-mgt/io.entgra.tenant.mgt.common/src/main/java/io/entgra/tenant/mgt/common/spi/TenantManagerService.java index a721e03029..472b1b7cb1 100644 --- a/components/tenant-mgt/io.entgra.tenant.mgt.common/src/main/java/io/entgra/tenant/mgt/common/spi/TenantManagerService.java +++ b/components/tenant-mgt/io.entgra.tenant.mgt.common/src/main/java/io/entgra/tenant/mgt/common/spi/TenantManagerService.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.common.spi; import io.entgra.tenant.mgt.common.exception.TenantMgtException; diff --git a/components/tenant-mgt/io.entgra.tenant.mgt.core/pom.xml b/components/tenant-mgt/io.entgra.tenant.mgt.core/pom.xml index 5ec9904eb3..a91e831ed5 100644 --- a/components/tenant-mgt/io.entgra.tenant.mgt.core/pom.xml +++ b/components/tenant-mgt/io.entgra.tenant.mgt.core/pom.xml @@ -1,4 +1,21 @@ + 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 2f9b63281b..6919f520c4 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 @@ -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; import io.entgra.tenant.mgt.common.exception.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 8782f7ca1a..fe5b16f2f3 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 @@ -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.impl; import io.entgra.application.mgt.common.exception.ApplicationManagementException; 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 760e49f4e8..b8a82be1da 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 @@ -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.impl; import io.entgra.tenant.mgt.common.spi.TenantManagerService; diff --git a/components/tenant-mgt/io.entgra.tenant.mgt.core/src/main/java/io/entgra/tenant/mgt/core/internal/TenantMgtDataHolder.java b/components/tenant-mgt/io.entgra.tenant.mgt.core/src/main/java/io/entgra/tenant/mgt/core/internal/TenantMgtDataHolder.java index 6f9d603a3f..d28862d410 100644 --- a/components/tenant-mgt/io.entgra.tenant.mgt.core/src/main/java/io/entgra/tenant/mgt/core/internal/TenantMgtDataHolder.java +++ b/components/tenant-mgt/io.entgra.tenant.mgt.core/src/main/java/io/entgra/tenant/mgt/core/internal/TenantMgtDataHolder.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.internal; import io.entgra.application.mgt.common.services.ApplicationManager; 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 534c50c59d..0f4480e1b9 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 @@ -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.internal; import io.entgra.application.mgt.common.services.ApplicationManager; diff --git a/components/tenant-mgt/pom.xml b/components/tenant-mgt/pom.xml index 58b3ebceb0..7d13f340c8 100644 --- a/components/tenant-mgt/pom.xml +++ b/components/tenant-mgt/pom.xml @@ -1,4 +1,21 @@ + diff --git a/features/tenant-mgt/io.entgra.tenant.mgt.server.feature/pom.xml b/features/tenant-mgt/io.entgra.tenant.mgt.server.feature/pom.xml index 04cf1bb3e5..87e3214a43 100644 --- a/features/tenant-mgt/io.entgra.tenant.mgt.server.feature/pom.xml +++ b/features/tenant-mgt/io.entgra.tenant.mgt.server.feature/pom.xml @@ -1,4 +1,21 @@ + diff --git a/features/tenant-mgt/pom.xml b/features/tenant-mgt/pom.xml index d55378a8cb..44befb4240 100644 --- a/features/tenant-mgt/pom.xml +++ b/features/tenant-mgt/pom.xml @@ -1,4 +1,21 @@ + From 9c9c391fd4d1268e9464a3647c91c57e2455b4d0 Mon Sep 17 00:00:00 2001 From: rajitha Date: Tue, 18 Apr 2023 15:51:02 +0530 Subject: [PATCH 4/5] Code refactor --- .../common/exception/TenantMgtException.java | 3 +++ .../internal/TenantMgtServiceComponent.java | 19 ++++++++++++++++++- 2 files changed, 21 insertions(+), 1 deletion(-) diff --git a/components/tenant-mgt/io.entgra.tenant.mgt.common/src/main/java/io/entgra/tenant/mgt/common/exception/TenantMgtException.java b/components/tenant-mgt/io.entgra.tenant.mgt.common/src/main/java/io/entgra/tenant/mgt/common/exception/TenantMgtException.java index f9b8c22e62..1470f07f63 100644 --- a/components/tenant-mgt/io.entgra.tenant.mgt.common/src/main/java/io/entgra/tenant/mgt/common/exception/TenantMgtException.java +++ b/components/tenant-mgt/io.entgra.tenant.mgt.common/src/main/java/io/entgra/tenant/mgt/common/exception/TenantMgtException.java @@ -18,6 +18,9 @@ package io.entgra.tenant.mgt.common.exception; public class TenantMgtException extends Exception { + + private static final long serialVersionUID = 4304028531230841553L; + public TenantMgtException(String msg, Throwable ex) { super(msg, ex); } 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 0f4480e1b9..fafdbbbea7 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 @@ -61,9 +61,14 @@ public class TenantMgtServiceComponent { whiteLabelManagementService, null); TenantMgtDataHolder.getInstance().setWhiteLabelManagementService(whiteLabelManagementService); DeviceMgtTenantListener deviceMgtTenantListener = new DeviceMgtTenantListener(); + if(log.isDebugEnabled()) { + log.info("Tenant management listener is registering"); + } componentContext.getBundleContext(). registerService(TenantMgtListener.class.getName(), deviceMgtTenantListener, null); - log.info("Tenant management service activated"); + if(log.isDebugEnabled()) { + log.info("Tenant management service activated"); + } } catch (Throwable t) { String msg = "Error occurred while activating tenant management service"; log.error(msg, t); @@ -76,18 +81,30 @@ public class TenantMgtServiceComponent { } protected void setApplicationManager(ApplicationManager applicationManager) { + if(log.isDebugEnabled()) { + log.info("Application manager service is binding"); + } TenantMgtDataHolder.getInstance().setApplicationManager(applicationManager); } protected void unsetApplicationManager(ApplicationManager applicationManager) { + if(log.isDebugEnabled()) { + log.info("Application manager service is unbinding"); + } TenantMgtDataHolder.getInstance().setApplicationManager(null); } protected void setRealmService(RealmService realmService) { + if(log.isDebugEnabled()) { + log.info("Realm Service service is binding"); + } TenantMgtDataHolder.getInstance().setRealmService(realmService); } protected void unsetRealmService(RealmService realmService) { + if(log.isDebugEnabled()) { + log.info("Realm Service service is unbinding"); + } TenantMgtDataHolder.getInstance().setRealmService(null); } } From e383f3b456474b25ece6bf248c42ae78f6e43056 Mon Sep 17 00:00:00 2001 From: rajitha Date: Wed, 19 Apr 2023 14:14:44 +0530 Subject: [PATCH 5/5] 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 180730dd6c..0d24f1125c 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 79f58e82a1..0000000000 --- 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 6919f520c4..ab9b8b2290 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 fe5b16f2f3..4089b4bffa 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 b8a82be1da..e15d105fbf 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 fafdbbbea7..cab03921c0 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 43e31cf4e0..a272585bf7 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) {