From 25daa38e58bc7c852e8ed93c16e44f60c0fad3cd Mon Sep 17 00:00:00 2001 From: pasindu Date: Wed, 25 Oct 2023 15:01:47 +0530 Subject: [PATCH] Fix OSGI services --- .../publisher/APIPublisherServiceImpl.java | 29 ++++----- .../internal/APIPublisherDataHolder.java | 21 ++++++- .../APIPublisherServiceComponent.java | 62 +++++++++++++++++++ 3 files changed, 95 insertions(+), 17 deletions(-) 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 c6c9003b07..0839b10003 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,11 +18,8 @@ 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; -import io.entgra.device.mgt.core.apimgt.extension.rest.api.PublisherRESTAPIServicesImpl; import io.entgra.device.mgt.core.apimgt.extension.rest.api.constants.Constants; import io.entgra.device.mgt.core.apimgt.extension.rest.api.dto.APIApplicationKey; import io.entgra.device.mgt.core.apimgt.extension.rest.api.dto.APIInfo.*; @@ -43,7 +40,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; @@ -100,7 +96,9 @@ 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 { @@ -156,7 +154,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++) { @@ -420,17 +417,16 @@ 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( "ClientForPublisherRestCalls", "client_credentials password refresh_token" ); - AccessTokenInfo accessTokenInfo = - apiApplicationServices.generateAccessTokenFromRegisteredApplication( - apiApplicationKey.getClientId(), apiApplicationKey.getClientSecret()); - - PublisherRESTAPIServices publisherRESTAPIServices = new PublisherRESTAPIServicesImpl(); + AccessTokenInfo accessTokenInfo = apiApplicationServices.generateAccessTokenFromRegisteredApplication( + apiApplicationKey.getClientId(), apiApplicationKey.getClientSecret()); Scope scope = new Scope(); for (DefaultPermission defaultPermission: defaultPermissions.getDefaultPermissions()) { @@ -460,7 +456,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 { @@ -481,7 +479,6 @@ public class APIPublisherServiceImpl implements APIPublisherService { for (String tenantDomain : tenants) { PrivilegedCarbonContext.startTenantFlow(); PrivilegedCarbonContext.getThreadLocalCarbonContext().setTenantDomain(tenantDomain, true); - PublisherRESTAPIServices publisherRESTAPIServices = new PublisherRESTAPIServicesImpl(); try { String fileName = @@ -597,7 +594,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 { @@ -614,7 +612,6 @@ 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..322abdc321 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(); @@ -156,4 +159,20 @@ public class APIPublisherDataHolder { public void setMetadataManagementService(MetadataManagementService metadataManagementService) { this.metadataManagementService = metadataManagementService; } + + public APIApplicationServices getApiApplicationServices() { + return apiApplicationServices; + } + + public void setApiApplicationServices(APIApplicationServices apiApplicationServices) { + this.apiApplicationServices = apiApplicationServices; + } + + 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/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 d2aa464136..b377007765 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; @@ -137,4 +139,64 @@ public class APIPublisherServiceComponent { APIPublisherDataHolder.getInstance().setMetadataManagementService(null); } + /** + * Sets DCR REST API service. + * + * @param apiApplicationServices An instance of APIApplicationServices + */ + @Reference( + name = "APIM.application.service", + service = io.entgra.device.mgt.core.apimgt.extension.rest.api.APIApplicationServices.class, + cardinality = ReferenceCardinality.MANDATORY, + policy = ReferencePolicy.DYNAMIC, + unbind = "unsetAPIApplicationServices") + protected void setAPIApplicationServices(APIApplicationServices apiApplicationServices) { + if (log.isDebugEnabled()) { + log.debug("Setting DCR REST API Service"); + } + APIPublisherDataHolder.getInstance().setApiApplicationServices(apiApplicationServices); + } + + /** + * Unset DCR REST API service + * + * @param apiApplicationServices An instance of APIApplicationServices + */ + protected void unsetAPIApplicationServices(APIApplicationServices apiApplicationServices) { + if (log.isDebugEnabled()) { + log.debug("Unsetting DCR REST API Service"); + } + APIPublisherDataHolder.getInstance().setApiApplicationServices(null); + } + + + /** + * Sets APIM Publisher REST API service. + * + * @param publisherRESTAPIServices An instance of PublisherRESTAPIServices + */ + @Reference( + name = "APIM.publisher.service", + service = io.entgra.device.mgt.core.apimgt.extension.rest.api.PublisherRESTAPIServices.class, + cardinality = ReferenceCardinality.MANDATORY, + policy = ReferencePolicy.DYNAMIC, + unbind = "unsetPublisherRESTAPIServices") + protected void setPublisherRESTAPIServices(PublisherRESTAPIServices publisherRESTAPIServices) { + if (log.isDebugEnabled()) { + log.debug("Setting APIM Publisher REST API Service"); + } + APIPublisherDataHolder.getInstance().setPublisherRESTAPIServices(publisherRESTAPIServices); + } + + /** + * Unset APIM Publisher REST API service + * + * @param publisherRESTAPIServices An instance of PublisherRESTAPIServices + */ + protected void unsetPublisherRESTAPIServices(PublisherRESTAPIServices publisherRESTAPIServices) { + if (log.isDebugEnabled()) { + log.debug("Unsetting APIM Publisher REST API Service"); + } + APIPublisherDataHolder.getInstance().setPublisherRESTAPIServices(null); + } }