From 9e09cd98e9c331f0805a096bf5ab8a6d8df20617 Mon Sep 17 00:00:00 2001 From: pasindu Date: Thu, 26 Oct 2023 13:02:17 +0530 Subject: [PATCH] Add OSGI services for API manager services --- ...t.java => APIManagerServiceComponent.java} | 16 ++++--- ....java => APIManagerServiceDataHolder.java} | 17 ++++++-- .../publisher/APIPublisherServiceImpl.java | 22 +++++----- .../internal/APIPublisherDataHolder.java | 29 +++++++++++-- .../APIPublisherServiceComponent.java | 42 +++++++++++++++++++ 5 files changed, 103 insertions(+), 23 deletions(-) rename components/apimgt-extensions/io.entgra.device.mgt.core.apimgt.extension.rest.api/src/main/java/io/entgra/device/mgt/core/apimgt/extension/rest/api/internal/{PublisherRESTAPIServiceComponent.java => APIManagerServiceComponent.java} (76%) rename components/apimgt-extensions/io.entgra.device.mgt.core.apimgt.extension.rest.api/src/main/java/io/entgra/device/mgt/core/apimgt/extension/rest/api/internal/{PublisherRESTAPIDataHolder.java => APIManagerServiceDataHolder.java} (73%) diff --git a/components/apimgt-extensions/io.entgra.device.mgt.core.apimgt.extension.rest.api/src/main/java/io/entgra/device/mgt/core/apimgt/extension/rest/api/internal/PublisherRESTAPIServiceComponent.java b/components/apimgt-extensions/io.entgra.device.mgt.core.apimgt.extension.rest.api/src/main/java/io/entgra/device/mgt/core/apimgt/extension/rest/api/internal/APIManagerServiceComponent.java similarity index 76% rename from components/apimgt-extensions/io.entgra.device.mgt.core.apimgt.extension.rest.api/src/main/java/io/entgra/device/mgt/core/apimgt/extension/rest/api/internal/PublisherRESTAPIServiceComponent.java rename to components/apimgt-extensions/io.entgra.device.mgt.core.apimgt.extension.rest.api/src/main/java/io/entgra/device/mgt/core/apimgt/extension/rest/api/internal/APIManagerServiceComponent.java index 799d5ee1c9..fef16ea6ee 100644 --- a/components/apimgt-extensions/io.entgra.device.mgt.core.apimgt.extension.rest.api/src/main/java/io/entgra/device/mgt/core/apimgt/extension/rest/api/internal/PublisherRESTAPIServiceComponent.java +++ b/components/apimgt-extensions/io.entgra.device.mgt.core.apimgt.extension.rest.api/src/main/java/io/entgra/device/mgt/core/apimgt/extension/rest/api/internal/APIManagerServiceComponent.java @@ -20,6 +20,8 @@ package io.entgra.device.mgt.core.apimgt.extension.rest.api.internal; import io.entgra.device.mgt.core.apimgt.extension.rest.api.APIApplicationServices; import io.entgra.device.mgt.core.apimgt.extension.rest.api.APIApplicationServicesImpl; +import io.entgra.device.mgt.core.apimgt.extension.rest.api.PublisherRESTAPIServices; +import io.entgra.device.mgt.core.apimgt.extension.rest.api.PublisherRESTAPIServicesImpl; import org.apache.commons.logging.Log; import org.apache.commons.logging.LogFactory; import org.osgi.framework.BundleContext; @@ -36,9 +38,9 @@ import org.wso2.carbon.apimgt.impl.APIManagerConfigurationService; * bind="setAPIManagerConfigurationService" * unbind="unsetAPIManagerConfigurationService" */ -public class PublisherRESTAPIServiceComponent { +public class APIManagerServiceComponent { - private static Log log = LogFactory.getLog(PublisherRESTAPIServiceComponent.class); + private static Log log = LogFactory.getLog(APIManagerServiceComponent.class); protected void activate(ComponentContext componentContext) { if (log.isDebugEnabled()) { @@ -49,7 +51,11 @@ public class PublisherRESTAPIServiceComponent { APIApplicationServices apiApplicationServices = new APIApplicationServicesImpl(); bundleContext.registerService(APIApplicationServices.class.getName(), apiApplicationServices, null); - PublisherRESTAPIDataHolder.getInstance().setApiApplicationServices(apiApplicationServices); + APIManagerServiceDataHolder.getInstance().setApiApplicationServices(apiApplicationServices); + + PublisherRESTAPIServices publisherRESTAPIServices = new PublisherRESTAPIServicesImpl(); + bundleContext.registerService(PublisherRESTAPIServices.class.getName(), publisherRESTAPIServices, null); + APIManagerServiceDataHolder.getInstance().setPublisherRESTAPIServices(publisherRESTAPIServices); if (log.isDebugEnabled()) { log.debug("API Application bundle has been successfully initialized"); @@ -67,13 +73,13 @@ public class PublisherRESTAPIServiceComponent { if (log.isDebugEnabled()) { log.debug("Setting API Manager Configuration Service"); } - PublisherRESTAPIDataHolder.getInstance().setAPIManagerConfiguration(apiManagerConfigurationService); + APIManagerServiceDataHolder.getInstance().setAPIManagerConfiguration(apiManagerConfigurationService); } protected void unsetAPIManagerConfigurationService(APIManagerConfigurationService apiManagerConfigurationService) { if (log.isDebugEnabled()) { log.debug("Unsetting API Manager Configuration Service"); } - PublisherRESTAPIDataHolder.getInstance().setAPIManagerConfiguration(null); + APIManagerServiceDataHolder.getInstance().setAPIManagerConfiguration(null); } } diff --git a/components/apimgt-extensions/io.entgra.device.mgt.core.apimgt.extension.rest.api/src/main/java/io/entgra/device/mgt/core/apimgt/extension/rest/api/internal/PublisherRESTAPIDataHolder.java b/components/apimgt-extensions/io.entgra.device.mgt.core.apimgt.extension.rest.api/src/main/java/io/entgra/device/mgt/core/apimgt/extension/rest/api/internal/APIManagerServiceDataHolder.java similarity index 73% rename from components/apimgt-extensions/io.entgra.device.mgt.core.apimgt.extension.rest.api/src/main/java/io/entgra/device/mgt/core/apimgt/extension/rest/api/internal/PublisherRESTAPIDataHolder.java rename to components/apimgt-extensions/io.entgra.device.mgt.core.apimgt.extension.rest.api/src/main/java/io/entgra/device/mgt/core/apimgt/extension/rest/api/internal/APIManagerServiceDataHolder.java index 9a53757b0b..2779366b9d 100644 --- a/components/apimgt-extensions/io.entgra.device.mgt.core.apimgt.extension.rest.api/src/main/java/io/entgra/device/mgt/core/apimgt/extension/rest/api/internal/PublisherRESTAPIDataHolder.java +++ b/components/apimgt-extensions/io.entgra.device.mgt.core.apimgt.extension.rest.api/src/main/java/io/entgra/device/mgt/core/apimgt/extension/rest/api/internal/APIManagerServiceDataHolder.java @@ -19,19 +19,21 @@ package io.entgra.device.mgt.core.apimgt.extension.rest.api.internal; import io.entgra.device.mgt.core.apimgt.extension.rest.api.APIApplicationServices; +import io.entgra.device.mgt.core.apimgt.extension.rest.api.PublisherRESTAPIServices; import org.wso2.carbon.apimgt.impl.APIManagerConfigurationService; -public class PublisherRESTAPIDataHolder { +public class APIManagerServiceDataHolder { private APIApplicationServices apiApplicationServices; private APIManagerConfigurationService apiManagerConfigurationService; + private PublisherRESTAPIServices publisherRESTAPIServices; - private static PublisherRESTAPIDataHolder thisInstance = new PublisherRESTAPIDataHolder(); + private static APIManagerServiceDataHolder thisInstance = new APIManagerServiceDataHolder(); - private PublisherRESTAPIDataHolder() { + private APIManagerServiceDataHolder() { } - static PublisherRESTAPIDataHolder getInstance() { + static APIManagerServiceDataHolder getInstance() { return thisInstance; } @@ -54,4 +56,11 @@ public class PublisherRESTAPIDataHolder { return apiManagerConfigurationService; } + public PublisherRESTAPIServices getPublisherRESTAPIServices() { + return publisherRESTAPIServices; + } + + public void setPublisherRESTAPIServices(PublisherRESTAPIServices publisherRESTAPIServices) { + this.publisherRESTAPIServices = publisherRESTAPIServices; + } } diff --git a/components/apimgt-extensions/io.entgra.device.mgt.core.apimgt.webapp.publisher/src/main/java/io/entgra/device/mgt/core/apimgt/webapp/publisher/APIPublisherServiceImpl.java b/components/apimgt-extensions/io.entgra.device.mgt.core.apimgt.webapp.publisher/src/main/java/io/entgra/device/mgt/core/apimgt/webapp/publisher/APIPublisherServiceImpl.java index db7e58a32b..2e971e7370 100644 --- a/components/apimgt-extensions/io.entgra.device.mgt.core.apimgt.webapp.publisher/src/main/java/io/entgra/device/mgt/core/apimgt/webapp/publisher/APIPublisherServiceImpl.java +++ b/components/apimgt-extensions/io.entgra.device.mgt.core.apimgt.webapp.publisher/src/main/java/io/entgra/device/mgt/core/apimgt/webapp/publisher/APIPublisherServiceImpl.java @@ -18,7 +18,6 @@ package io.entgra.device.mgt.core.apimgt.webapp.publisher; import com.google.gson.Gson; -import io.entgra.device.mgt.core.apimgt.annotations.Scopes; import io.entgra.device.mgt.core.apimgt.extension.rest.api.APIApplicationServices; import io.entgra.device.mgt.core.apimgt.extension.rest.api.APIApplicationServicesImpl; import io.entgra.device.mgt.core.apimgt.extension.rest.api.PublisherRESTAPIServices; @@ -43,7 +42,6 @@ import io.entgra.device.mgt.core.device.mgt.core.config.permission.ScopeMapping; import org.apache.commons.lang.StringUtils; import org.apache.commons.logging.Log; import org.apache.commons.logging.LogFactory; -import org.json.JSONArray; import org.json.JSONObject; import org.wso2.carbon.CarbonConstants; import org.wso2.carbon.apimgt.api.APIManagementException; @@ -109,7 +107,8 @@ public class APIPublisherServiceImpl implements APIPublisherService { RealmService realmService = (RealmService) PrivilegedCarbonContext.getThreadLocalCarbonContext() .getOSGiService(RealmService.class, null); - APIApplicationServices apiApplicationServices = new APIApplicationServicesImpl(); + APIApplicationServices apiApplicationServices = APIPublisherDataHolder.getInstance().getApiApplicationServices(); + PublisherRESTAPIServices publisherRESTAPIServices = APIPublisherDataHolder.getInstance().getPublisherRESTAPIServices(); APIApplicationKey apiApplicationKey; AccessTokenInfo accessTokenInfo; try { @@ -162,7 +161,6 @@ public class APIPublisherServiceImpl implements APIPublisherService { APIIdentifier apiIdentifier = new APIIdentifier(APIUtil.replaceEmailDomain(apiConfig.getOwner()), apiConfig.getName(), apiConfig.getVersion()); - PublisherRESTAPIServices publisherRESTAPIServices = new PublisherRESTAPIServicesImpl(); APIInfo[] apiList = publisherRESTAPIServices.getApis(apiApplicationKey, accessTokenInfo); boolean apiFound = false; for (int i = 0; i < apiList.length; i++) { @@ -444,7 +442,9 @@ public class APIPublisherServiceImpl implements APIPublisherService { public void addDefaultScopesIfNotExist() { DeviceManagementConfig deviceManagementConfig = DeviceConfigurationManager.getInstance().getDeviceManagementConfig(); DefaultPermissions defaultPermissions = deviceManagementConfig.getDefaultPermissions(); - APIApplicationServices apiApplicationServices = new APIApplicationServicesImpl(); + APIApplicationServices apiApplicationServices = APIPublisherDataHolder.getInstance().getApiApplicationServices(); + PublisherRESTAPIServices publisherRESTAPIServices = APIPublisherDataHolder.getInstance().getPublisherRESTAPIServices(); + try { APIApplicationKey apiApplicationKey = apiApplicationServices.createAndRetrieveApplicationCredentials(); @@ -452,8 +452,6 @@ public class APIPublisherServiceImpl implements APIPublisherService { apiApplicationServices.generateAccessTokenFromRegisteredApplication( apiApplicationKey.getClientId(), apiApplicationKey.getClientSecret()); - PublisherRESTAPIServices publisherRESTAPIServices = new PublisherRESTAPIServicesImpl(); - Scope scope = new Scope(); for (DefaultPermission defaultPermission: defaultPermissions.getDefaultPermissions()) { //todo check whether scope is available or not @@ -482,7 +480,9 @@ public class APIPublisherServiceImpl implements APIPublisherService { List tenants = new ArrayList<>(Collections.singletonList(APIConstants.SUPER_TENANT_DOMAIN)); tenants.addAll(config.getTenants().getTenant()); - APIApplicationServices apiApplicationServices = new APIApplicationServicesImpl(); + APIApplicationServices apiApplicationServices = APIPublisherDataHolder.getInstance().getApiApplicationServices(); + PublisherRESTAPIServices publisherRESTAPIServices = APIPublisherDataHolder.getInstance().getPublisherRESTAPIServices(); + APIApplicationKey apiApplicationKey; AccessTokenInfo accessTokenInfo; try { @@ -500,7 +500,6 @@ public class APIPublisherServiceImpl implements APIPublisherService { for (String tenantDomain : tenants) { PrivilegedCarbonContext.startTenantFlow(); PrivilegedCarbonContext.getThreadLocalCarbonContext().setTenantDomain(tenantDomain, true); - PublisherRESTAPIServices publisherRESTAPIServices = new PublisherRESTAPIServicesImpl(); try { String fileName = @@ -616,7 +615,8 @@ public class APIPublisherServiceImpl implements APIPublisherService { @Override public void updateScopeRoleMapping(String roleName, String[] permissions, String[] removedPermissions) throws APIManagerPublisherException { - APIApplicationServices apiApplicationServices = new APIApplicationServicesImpl(); + APIApplicationServices apiApplicationServices = APIPublisherDataHolder.getInstance().getApiApplicationServices(); + PublisherRESTAPIServices publisherRESTAPIServices = APIPublisherDataHolder.getInstance().getPublisherRESTAPIServices(); APIApplicationKey apiApplicationKey; AccessTokenInfo accessTokenInfo; try { @@ -630,7 +630,7 @@ public class APIPublisherServiceImpl implements APIPublisherService { } try { - PublisherRESTAPIServices publisherRESTAPIServices = new PublisherRESTAPIServicesImpl(); + Scope[] scopeList = publisherRESTAPIServices.getScopes(apiApplicationKey, accessTokenInfo); Map permScopeMap = APIPublisherDataHolder.getInstance().getPermScopeMapping(); diff --git a/components/apimgt-extensions/io.entgra.device.mgt.core.apimgt.webapp.publisher/src/main/java/io/entgra/device/mgt/core/apimgt/webapp/publisher/internal/APIPublisherDataHolder.java b/components/apimgt-extensions/io.entgra.device.mgt.core.apimgt.webapp.publisher/src/main/java/io/entgra/device/mgt/core/apimgt/webapp/publisher/internal/APIPublisherDataHolder.java index 0d67edc7a9..4f8e5599bf 100644 --- a/components/apimgt-extensions/io.entgra.device.mgt.core.apimgt.webapp.publisher/src/main/java/io/entgra/device/mgt/core/apimgt/webapp/publisher/internal/APIPublisherDataHolder.java +++ b/components/apimgt-extensions/io.entgra.device.mgt.core.apimgt.webapp.publisher/src/main/java/io/entgra/device/mgt/core/apimgt/webapp/publisher/internal/APIPublisherDataHolder.java @@ -17,6 +17,8 @@ */ package io.entgra.device.mgt.core.apimgt.webapp.publisher.internal; +import io.entgra.device.mgt.core.apimgt.extension.rest.api.APIApplicationServices; +import io.entgra.device.mgt.core.apimgt.extension.rest.api.PublisherRESTAPIServices; import io.entgra.device.mgt.core.apimgt.webapp.publisher.APIConfig; import io.entgra.device.mgt.core.apimgt.webapp.publisher.APIPublisherService; import io.entgra.device.mgt.core.device.mgt.common.metadata.mgt.MetadataManagementService; @@ -44,7 +46,8 @@ public class APIPublisherDataHolder { private boolean isServerStarted; private Stack unpublishedApis = new Stack<>(); private Map permScopeMapping; - + private APIApplicationServices apiApplicationServices; + private PublisherRESTAPIServices publisherRESTAPIServices; private MetadataManagementService metadataManagementService; private static APIPublisherDataHolder thisInstance = new APIPublisherDataHolder(); @@ -145,9 +148,29 @@ public class APIPublisherDataHolder { this.unpublishedApis = unpublishedApis; } - public Map getPermScopeMapping() {return permScopeMapping;} + public Map getPermScopeMapping() { + return permScopeMapping; + } + + public void setPermScopeMapping(Map permScopeMapping) { + this.permScopeMapping = permScopeMapping; + } + + public APIApplicationServices getApiApplicationServices() { + return apiApplicationServices; + } - public void setPermScopeMapping(Map permScopeMapping) {this.permScopeMapping = permScopeMapping;} + public void setApiApplicationServices(APIApplicationServices apiApplicationServices) { + this.apiApplicationServices = apiApplicationServices; + } + + public PublisherRESTAPIServices getPublisherRESTAPIServices() { + return publisherRESTAPIServices; + } + + public void setPublisherRESTAPIServices(PublisherRESTAPIServices publisherRESTAPIServices) { + this.publisherRESTAPIServices = publisherRESTAPIServices; + } public MetadataManagementService getMetadataManagementService() { return metadataManagementService; diff --git a/components/apimgt-extensions/io.entgra.device.mgt.core.apimgt.webapp.publisher/src/main/java/io/entgra/device/mgt/core/apimgt/webapp/publisher/internal/APIPublisherServiceComponent.java b/components/apimgt-extensions/io.entgra.device.mgt.core.apimgt.webapp.publisher/src/main/java/io/entgra/device/mgt/core/apimgt/webapp/publisher/internal/APIPublisherServiceComponent.java index 616a1c3987..7427bd930f 100644 --- a/components/apimgt-extensions/io.entgra.device.mgt.core.apimgt.webapp.publisher/src/main/java/io/entgra/device/mgt/core/apimgt/webapp/publisher/internal/APIPublisherServiceComponent.java +++ b/components/apimgt-extensions/io.entgra.device.mgt.core.apimgt.webapp.publisher/src/main/java/io/entgra/device/mgt/core/apimgt/webapp/publisher/internal/APIPublisherServiceComponent.java @@ -17,6 +17,8 @@ */ package io.entgra.device.mgt.core.apimgt.webapp.publisher.internal; +import io.entgra.device.mgt.core.apimgt.extension.rest.api.APIApplicationServices; +import io.entgra.device.mgt.core.apimgt.extension.rest.api.PublisherRESTAPIServices; import io.entgra.device.mgt.core.device.mgt.common.metadata.mgt.MetadataManagementService; import org.apache.commons.logging.Log; import org.apache.commons.logging.LogFactory; @@ -46,6 +48,18 @@ import java.util.HashMap; * policy="dynamic" * bind="setRegistryService" * unbind="unsetRegistryService" + * @scr.reference name="APIM.application.service" + * interface="io.entgra.device.mgt.core.apimgt.extension.rest.api.APIApplicationServices" + * cardinality="1..1" + * policy="dynamic" + * bind="setAPIApplicationServices" + * unbind="unsetAPIApplicationServices" + * @scr.reference name="APIM.publisher.service" + * interface="io.entgra.device.mgt.core.apimgt.extension.rest.api.PublisherRESTAPIServices" + * cardinality="1..1" + * policy="dynamic" + * bind="setPublisherRESTAPIServices" + * unbind="unsetPublisherRESTAPIServices" * @scr.reference name="io.entgra.meta.mgt" * interface="io.entgra.device.mgt.core.device.mgt.common.metadata.mgt.MetadataManagementService" * cardinality="0..1" @@ -123,6 +137,34 @@ public class APIPublisherServiceComponent { APIPublisherDataHolder.getInstance().setRegistryService(null); } + protected void setAPIApplicationServices(APIApplicationServices apiApplicationServices) { + if (log.isDebugEnabled()) { + log.debug("Setting DCR REST API Service"); + } + APIPublisherDataHolder.getInstance().setApiApplicationServices(apiApplicationServices); + } + + protected void unsetAPIApplicationServices(APIApplicationServices apiApplicationServices) { + if (log.isDebugEnabled()) { + log.debug("Unsetting DCR REST API Service"); + } + APIPublisherDataHolder.getInstance().setApiApplicationServices(null); + } + + protected void setPublisherRESTAPIServices(PublisherRESTAPIServices publisherRESTAPIServices) { + if (log.isDebugEnabled()) { + log.debug("Setting APIM Publisher REST API Service"); + } + APIPublisherDataHolder.getInstance().setPublisherRESTAPIServices(publisherRESTAPIServices); + } + + protected void unsetPublisherRESTAPIServices(PublisherRESTAPIServices publisherRESTAPIServices) { + if (log.isDebugEnabled()) { + log.debug("Unsetting APIM Publisher REST API Service"); + } + APIPublisherDataHolder.getInstance().setPublisherRESTAPIServices(null); + } + protected void setMetaDataMgtService(MetadataManagementService metadataManagementService) { if (metadataManagementService != null && log.isDebugEnabled()) { log.debug("Meta data mgt mgt service initialized");