Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Refactor the code to utilize the application management service for retrieving the main application ID #267

Merged
merged 11 commits into from
Oct 14, 2024
Merged
22 changes: 16 additions & 6 deletions components/email-mgt/org.wso2.carbon.email.mgt/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -52,10 +52,6 @@
<groupId>org.wso2.carbon</groupId>
<artifactId>org.wso2.carbon.user.core</artifactId>
</dependency>
<dependency>
<groupId>org.wso2.carbon.identity.organization.management</groupId>
<artifactId>org.wso2.carbon.identity.organization.management.application</artifactId>
</dependency>
<dependency>
<groupId>org.wso2.carbon</groupId>
<artifactId>javax.cache.wso2</artifactId>
Expand All @@ -80,6 +76,14 @@
<groupId>org.wso2.carbon.identity.framework</groupId>
<artifactId>org.wso2.carbon.identity.core</artifactId>
</dependency>
<dependency>
<groupId>org.wso2.carbon.identity.framework</groupId>
<artifactId>org.wso2.carbon.identity.application.mgt</artifactId>
</dependency>
<dependency>
<groupId>org.wso2.carbon.identity.framework</groupId>
<artifactId>org.wso2.carbon.identity.application.common</artifactId>
</dependency>
<dependency>
<groupId>org.wso2.carbon.commons</groupId>
<artifactId>org.wso2.carbon.tenant.common</artifactId>
Expand Down Expand Up @@ -160,6 +164,8 @@
org.wso2.carbon.identity.core.*; version="${carbon.identity.framework.imp.pkg.version.range}",
org.wso2.carbon.registry.core.*;version="${carbon.kernel.registry.imp.pkg.version}",
org.wso2.carbon.identity.base; version="${carbon.identity.framework.imp.pkg.version.range}",
org.wso2.carbon.identity.application.mgt; version="${carbon.identity.framework.imp.pkg.version.range}",
org.wso2.carbon.identity.application.common; version="${carbon.identity.framework.imp.pkg.version.range}",
org.wso2.carbon.identity.governance.*;version="${identity.governance.imp.pkg.version.range}",
org.wso2.carbon.database.utils.*;version="${org.wso2.carbon.database.utils.version.range}",
org.wso2.carbon.identity.organization.management.service;
Expand All @@ -168,8 +174,6 @@
version="${org.wso2.identity.organization.mgt.core.imp.pkg.version.range}",
org.wso2.carbon.identity.organization.management.service.exception;
version="${org.wso2.identity.organization.mgt.core.imp.pkg.version.range}",
org.wso2.carbon.identity.organization.management.application.*;
version="${org.wso2.identity.organization.mgt.imp.pkg.version.range}",
</Import-Package>
</instructions>
</configuration>
Expand Down Expand Up @@ -253,6 +257,12 @@
<groupId>org.jacoco</groupId>
<artifactId>jacoco-maven-plugin</artifactId>
<version>${jacoco.version}</version>
<configuration>
<excludes>
<exclude>org/wso2/carbon/email/mgt/internal/I18nMgtServiceComponent.class</exclude>
<exclude>org/wso2/carbon/email/mgt/internal/I18nMgtDataHolder.class</exclude>
</excludes>
</configuration>
<executions>
<execution>
<goals>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,7 @@
import org.wso2.carbon.email.mgt.internal.I18nMgtDataHolder;
import org.wso2.carbon.email.mgt.model.EmailTemplate;
import org.wso2.carbon.email.mgt.util.I18nEmailUtil;
import org.wso2.carbon.identity.application.common.IdentityApplicationManagementServerException;
import org.wso2.carbon.identity.base.IdentityValidationUtil;
import org.wso2.carbon.identity.governance.IdentityGovernanceUtil;
import org.wso2.carbon.identity.governance.IdentityMgtConstants;
Expand All @@ -43,7 +44,6 @@
import org.wso2.carbon.identity.governance.model.NotificationTemplate;
import org.wso2.carbon.identity.governance.service.notification.NotificationChannels;
import org.wso2.carbon.identity.governance.service.notification.NotificationTemplateManager;
import org.wso2.carbon.identity.organization.management.application.OrgApplicationManager;
import org.wso2.carbon.identity.organization.management.service.OrganizationManager;
import org.wso2.carbon.identity.organization.management.service.exception.OrganizationManagementException;
import org.wso2.carbon.identity.organization.management.service.util.OrganizationManagementUtil;
Expand All @@ -60,6 +60,7 @@
import static org.wso2.carbon.email.mgt.util.I18nEmailUtil.buildNotificationTemplateFromEmailTemplate;
import static org.wso2.carbon.email.mgt.util.I18nEmailUtil.normalizeLocaleFormat;
import static org.wso2.carbon.identity.base.IdentityValidationUtil.ValidatorPattern.REGISTRY_INVALID_CHARS_EXISTS;
import static org.wso2.carbon.identity.organization.management.service.constant.OrganizationManagementConstants.ErrorMessages.ERROR_CODE_ERROR_RESOLVING_MAIN_APPLICATION;

/**
* Provides functionality to manage email templates used in notification emails.
Expand Down Expand Up @@ -289,11 +290,14 @@ public NotificationTemplate getNotificationTemplate(String notificationChannel,
tenantDomain = getRootOrgTenantDomain(tenantDomain);
// If it's application specific template is required, get the root organization's application.
if (StringUtils.isNotBlank(applicationUuid)) {
applicationUuid = getMainApplicationIdForGivenSharedApp(applicationUuid, tenantDomain);
applicationUuid = I18nMgtDataHolder.getInstance().getApplicationManagementService().getMainAppId(applicationUuid);
}
}
} catch (OrganizationManagementException e) {
throw new NotificationTemplateManagerException(e.getMessage(), e);
} catch (IdentityApplicationManagementServerException e) {
throw new NotificationTemplateManagerException(ERROR_CODE_ERROR_RESOLVING_MAIN_APPLICATION.getCode(),
ERROR_CODE_ERROR_RESOLVING_MAIN_APPLICATION.getMessage(), e);
}
// Resolve channel to either SMS or EMAIL.
notificationChannel = resolveNotificationChannel(notificationChannel);
Expand Down Expand Up @@ -825,21 +829,4 @@ private String getRootOrgTenantDomain(String tenantDomain) throws OrganizationMa
String primaryOrgId = organizationManager.getPrimaryOrganizationId(orgId);
return organizationManager.resolveTenantDomain(primaryOrgId);
}

/**
* Get the main application id for the given shared application.
*
* @param applicationUuid Shared application id.
* @param tenantDomain Shared app's tenant domain.
* @return Main application id.
* @throws OrganizationManagementException If an error occurred while getting the main application id.
*/
private String getMainApplicationIdForGivenSharedApp(String applicationUuid, String tenantDomain)
throws OrganizationManagementException {

OrganizationManager organizationManager = I18nMgtDataHolder.getInstance().getOrganizationManager();
String sharedOrgId = organizationManager.resolveOrganizationId(tenantDomain);
OrgApplicationManager sharedAppManager = I18nMgtDataHolder.getInstance().getSharedAppManager();
return sharedAppManager.getMainApplicationIdForGivenSharedApp(applicationUuid, sharedOrgId);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -18,9 +18,9 @@

package org.wso2.carbon.email.mgt.internal;

import org.wso2.carbon.identity.application.mgt.ApplicationManagementService;
import org.wso2.carbon.identity.core.persistence.registry.RegistryResourceMgtService;
import org.wso2.carbon.identity.governance.model.NotificationTemplate;
import org.wso2.carbon.identity.organization.management.application.OrgApplicationManager;
import org.wso2.carbon.identity.organization.management.service.OrganizationManager;
import org.wso2.carbon.registry.core.service.RegistryService;
import org.wso2.carbon.user.core.service.RealmService;
Expand All @@ -33,7 +33,7 @@ public class I18nMgtDataHolder{
private RegistryService registryService;
private RegistryResourceMgtService registryResourceMgtService;
private OrganizationManager organizationManager;
private OrgApplicationManager sharedAppManager;
private ApplicationManagementService applicationManagementService;
private List<NotificationTemplate> defaultEmailTemplates = new ArrayList<>();
private List<NotificationTemplate> defaultSMSTemplates = new ArrayList<>();
private List<String> legacyTenants = new ArrayList<>();
Expand All @@ -47,6 +47,26 @@ public static I18nMgtDataHolder getInstance() {
return instance;
}

/**
* Get the application management service.
*
* @return Application management service.
*/
public ApplicationManagementService getApplicationManagementService() {

return applicationManagementService;
}

/**
* Set the application management service.
*
* @param applicationManagementService Application management service instance.
*/
public void setApplicationManagementService(ApplicationManagementService applicationManagementService) {

this.applicationManagementService = applicationManagementService;
}

public RealmService getRealmService() {
if (realmService == null) {
throw new RuntimeException("Realm Service has not been set. Component has not initialized properly.");
Expand Down Expand Up @@ -121,26 +141,6 @@ public OrganizationManager getOrganizationManager() {
return organizationManager;
}

/**
* Get the shared application manager.
*
* @return Shared application manager.
*/
public OrgApplicationManager getSharedAppManager() {

return sharedAppManager;
}

/**
* Set the shared application manager.
*
* @param sharedAppManager Shared application manager.
*/
public void setSharedAppManager(OrgApplicationManager sharedAppManager) {

this.sharedAppManager = sharedAppManager;
}

/**
* Set the list of legacy tenants.
*
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -33,13 +33,13 @@
import org.wso2.carbon.email.mgt.constants.I18nMgtConstants;
import org.wso2.carbon.email.mgt.exceptions.I18nEmailMgtException;
import org.wso2.carbon.email.mgt.model.SMSProviderTemplate;
import org.wso2.carbon.identity.application.mgt.ApplicationManagementService;
import org.wso2.carbon.identity.core.persistence.registry.RegistryResourceMgtService;
import org.wso2.carbon.identity.governance.exceptions.notiification.NotificationTemplateManagerException;
import org.wso2.carbon.identity.core.util.IdentityUtil;
import org.wso2.carbon.identity.governance.model.NotificationTemplate;
import org.wso2.carbon.identity.governance.service.notification.NotificationChannels;
import org.wso2.carbon.identity.governance.service.notification.NotificationTemplateManager;
import org.wso2.carbon.identity.organization.management.application.OrgApplicationManager;
import org.wso2.carbon.identity.organization.management.service.OrganizationManager;
import org.wso2.carbon.registry.core.service.RegistryService;
import org.wso2.carbon.stratos.common.listeners.TenantMgtListener;
Expand Down Expand Up @@ -310,6 +310,24 @@ protected void deactivate(ComponentContext context) {
}
}

@Reference(
name = "org.wso2.carbon.identity.application.mgt",
service = ApplicationManagementService.class,
cardinality = ReferenceCardinality.MANDATORY,
policy = ReferencePolicy.DYNAMIC,
unbind = "unsetApplicationManagementService")
protected void setApplicationManagementService(ApplicationManagementService applicationManagementService) {

log.debug("Setting Application Management Service.");
dataHolder.setApplicationManagementService(applicationManagementService);
}

protected void unsetApplicationManagementService(ApplicationManagementService applicationManagementService) {

log.debug("Unsetting Application Management Service.");
dataHolder.setApplicationManagementService(null);
}

@Reference(
name = "realm.service",
service = org.wso2.carbon.user.core.service.RealmService.class,
Expand Down Expand Up @@ -385,21 +403,5 @@ protected void unsetOrganizationManager(OrganizationManager organizationManager)

dataHolder.getInstance().setOrganizationManager(null);
}

@Reference(
name = "organization.application.management.service",
service = OrgApplicationManager.class,
cardinality = ReferenceCardinality.MANDATORY,
policy = ReferencePolicy.DYNAMIC,
unbind = "unsetOrganizationApplicationManager")
protected void setOrganizationApplicationManager(OrgApplicationManager orgApplicationManager) {

dataHolder.getInstance().setSharedAppManager(orgApplicationManager);
}

protected void unsetOrganizationApplicationManager(OrgApplicationManager orgApplicationManager) {

dataHolder.getInstance().setSharedAppManager(null);
}
}

Loading
Loading