From 5d02746a5ef8625e73b3688c7238a6ee42a9c1f8 Mon Sep 17 00:00:00 2001 From: prabathabey Date: Mon, 30 Mar 2015 20:45:34 +0530 Subject: [PATCH] Fixing API publishing related issues --- .../carbon/device/mgt/core/api/mgt/APIConfig.java | 11 +++++++++-- .../mgt/core/api/mgt/APIPublisherServiceImpl.java | 13 +++++++++---- .../api/mgt/APIRegistrationStartupObserver.java | 13 ++++++++++--- 3 files changed, 28 insertions(+), 9 deletions(-) diff --git a/components/device-mgt/org.wso2.carbon.device.mgt.core/src/main/java/org/wso2/carbon/device/mgt/core/api/mgt/APIConfig.java b/components/device-mgt/org.wso2.carbon.device.mgt.core/src/main/java/org/wso2/carbon/device/mgt/core/api/mgt/APIConfig.java index 20530959d0..03d18c0397 100644 --- a/components/device-mgt/org.wso2.carbon.device.mgt.core/src/main/java/org/wso2/carbon/device/mgt/core/api/mgt/APIConfig.java +++ b/components/device-mgt/org.wso2.carbon.device.mgt.core/src/main/java/org/wso2/carbon/device/mgt/core/api/mgt/APIConfig.java @@ -18,7 +18,10 @@ */ package org.wso2.carbon.device.mgt.core.api.mgt; +import org.wso2.carbon.apimgt.api.APIManagementException; import org.wso2.carbon.apimgt.api.APIProvider; +import org.wso2.carbon.apimgt.impl.APIManagerFactory; +import org.wso2.carbon.device.mgt.common.DeviceManagementException; import javax.xml.bind.annotation.XmlElement; import javax.xml.bind.annotation.XmlRootElement; @@ -51,8 +54,12 @@ public class APIConfig { private String transports; private APIProvider provider; - public void init(APIProvider provider) { - this.provider = provider; + public void init() throws DeviceManagementException { + try { + this.provider = APIManagerFactory.getInstance().getAPIProvider(this.getOwner()); + } catch (APIManagementException e) { + throw new DeviceManagementException("Error occurred while initializing API provider", e); + } } @XmlTransient diff --git a/components/device-mgt/org.wso2.carbon.device.mgt.core/src/main/java/org/wso2/carbon/device/mgt/core/api/mgt/APIPublisherServiceImpl.java b/components/device-mgt/org.wso2.carbon.device.mgt.core/src/main/java/org/wso2/carbon/device/mgt/core/api/mgt/APIPublisherServiceImpl.java index 76f108b6d6..904fdcd8f9 100644 --- a/components/device-mgt/org.wso2.carbon.device.mgt.core/src/main/java/org/wso2/carbon/device/mgt/core/api/mgt/APIPublisherServiceImpl.java +++ b/components/device-mgt/org.wso2.carbon.device.mgt.core/src/main/java/org/wso2/carbon/device/mgt/core/api/mgt/APIPublisherServiceImpl.java @@ -42,10 +42,15 @@ public class APIPublisherServiceImpl implements APIPublisherService { log.debug("Publishing API '" + api.getId() + "'"); } APIProvider provider = APIManagerFactory.getInstance().getAPIProvider(api.getApiOwner()); - provider.addAPI(api); - if (log.isDebugEnabled()) { - log.debug("Successfully published API '" + api.getId() + "' with the context '" + - api.getContext() + "'"); + if (provider != null) { + provider.addAPI(api); + if (log.isDebugEnabled()) { + log.debug("Successfully published API '" + api.getId() + "' with the context '" + + api.getContext() + "'"); + } + } else { + log.error("API provider configured for the given API configuration is null. Thus, the API is not " + + "published"); } } diff --git a/components/device-mgt/org.wso2.carbon.device.mgt.core/src/main/java/org/wso2/carbon/device/mgt/core/api/mgt/APIRegistrationStartupObserver.java b/components/device-mgt/org.wso2.carbon.device.mgt.core/src/main/java/org/wso2/carbon/device/mgt/core/api/mgt/APIRegistrationStartupObserver.java index c85deff8e4..af1fc351a1 100644 --- a/components/device-mgt/org.wso2.carbon.device.mgt.core/src/main/java/org/wso2/carbon/device/mgt/core/api/mgt/APIRegistrationStartupObserver.java +++ b/components/device-mgt/org.wso2.carbon.device.mgt.core/src/main/java/org/wso2/carbon/device/mgt/core/api/mgt/APIRegistrationStartupObserver.java @@ -60,14 +60,21 @@ public class APIRegistrationStartupObserver implements ServerStartupObserver { List apiConfigs = APIPublisherConfig.getInstance().getApiConfigs(); for (APIConfig apiConfig : apiConfigs) { try { + /* API Config is initialized at this point in order to avoid OSGi declarative services which + the APIManagerComponent depend on, are deployed and initialized before invoking methods in + APIManagerFactory */ + apiConfig.init(); + API api = DeviceManagerUtil.getAPI(apiConfig); DeviceManagementDataHolder.getInstance().getApiPublisherService().publishAPI(api); log.info("Successfully published API '" + apiConfig.getName() + "' with the context '" + - apiConfig.getContext() + "' and version '" + apiConfig.getVersion() + "'"); - } catch (APIManagementException e) { - log.error("Error occurred while publishing API '" + apiConfig.getName() + "' with the context '" + apiConfig.getContext() + "' and version '" + apiConfig.getVersion() + "'"); + } catch (Throwable e) { + /* Throwable is caught as none of the RuntimeExceptions that can potentially occur at this point + does not seem to be logged anywhere else within the framework */ + log.error("Error occurred while publishing API '" + apiConfig.getName() + "' with the context '" + + apiConfig.getContext() + "' and version '" + apiConfig.getVersion() + "'", e); } } if (log.isDebugEnabled()) {