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 20530959d0b..03d18c03979 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 76f108b6d6f..904fdcd8f9d 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 c85deff8e4c..af1fc351a10 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()) {