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 129e141f7f3..76f108b6d6f 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 @@ -67,10 +67,14 @@ public class APIPublisherServiceImpl implements APIPublisherService { log.debug("Publishing a batch of APIs"); } for (API api : apis) { - this.publishAPI(api); + try { + this.publishAPI(api); + } catch (APIManagementException e) { + log.error("Error occurred while publishing API '" + api.getId().getApiName() + "'", e); + } } if (log.isDebugEnabled()) { - log.debug("Successfully published the batch of APIs"); + log.debug("End of publishing the batch of APIs"); } } 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 45677318464..3aed51ae4b2 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 @@ -22,10 +22,12 @@ import org.apache.commons.logging.Log; import org.apache.commons.logging.LogFactory; import org.wso2.carbon.apimgt.api.APIManagementException; import org.wso2.carbon.apimgt.api.APIProvider; +import org.wso2.carbon.apimgt.api.model.API; import org.wso2.carbon.apimgt.impl.APIManagerFactory; import org.wso2.carbon.core.ServerStartupObserver; import org.wso2.carbon.device.mgt.common.DeviceManagementException; import org.wso2.carbon.device.mgt.core.api.mgt.config.APIPublisherConfig; +import org.wso2.carbon.device.mgt.core.internal.DeviceManagementDataHolder; import org.wso2.carbon.device.mgt.core.util.DeviceManagerUtil; import java.util.List; @@ -51,43 +53,26 @@ public class APIRegistrationStartupObserver implements ServerStartupObserver { @Override public void completedServerStartup() { - try { - this.initAPIConfigs(); - /* Publish all mobile device management related JAX-RS services as APIs */ - this.publishAPIs(); - } catch (DeviceManagementException e) { - log.error("Error occurred while publishing Mobile Device Management related APIs", e); - } - } - - private void initAPIConfigs() throws DeviceManagementException { + /* Publish all mobile device management related JAX-RS services as APIs */ if (log.isDebugEnabled()) { - log.debug("Initializing Mobile Device Management related APIs"); + log.debug("Publishing all mobile device management related JAX-RS services as APIs"); } List apiConfigs = APIPublisherConfig.getInstance().getApiConfigs(); for (APIConfig apiConfig : apiConfigs) { try { - APIProvider provider = - APIManagerFactory.getInstance().getAPIProvider(apiConfig.getOwner()); - apiConfig.init(provider); + API api = DeviceManagerUtil.getAPI(apiConfig); + DeviceManagementDataHolder.getInstance().getApiPublisherService().publishAPI(api); + if (log.isDebugEnabled()) { + log.debug("Successfully published API '" + apiConfig.getName() + "' with the context '" + + apiConfig.getContext() + "' and version '" + apiConfig.getVersion() + "'"); + } } catch (APIManagementException e) { - throw new DeviceManagementException("Error occurred while initializing API Config '" + - apiConfig.getName() + "'", e); + log.error("Error occurred while publishing API '" + apiConfig.getName() + "' with the context '" + + apiConfig.getContext() + "' and version '" + apiConfig.getVersion() + "'"); } } - } - - private void publishAPIs() throws DeviceManagementException { if (log.isDebugEnabled()) { - log.debug("Publishing Mobile Device Management related APIs"); - } - List apiConfigs = APIPublisherConfig.getInstance().getApiConfigs(); - for (APIConfig apiConfig : apiConfigs) { - DeviceManagerUtil.publishAPI(apiConfig); - if (log.isDebugEnabled()) { - log.debug("Successfully published API '" + apiConfig.getName() + "' with the context '" + - apiConfig.getContext() + "' and version '" + apiConfig.getVersion() + "'"); - } + log.debug("End of publishing all mobile device management related JAX-RS services as APIs"); } } diff --git a/components/device-mgt/org.wso2.carbon.device.mgt.core/src/main/java/org/wso2/carbon/device/mgt/core/util/DeviceManagerUtil.java b/components/device-mgt/org.wso2.carbon.device.mgt.core/src/main/java/org/wso2/carbon/device/mgt/core/util/DeviceManagerUtil.java index de1460075fa..a0ac6ff2dc2 100644 --- a/components/device-mgt/org.wso2.carbon.device.mgt.core/src/main/java/org/wso2/carbon/device/mgt/core/util/DeviceManagerUtil.java +++ b/components/device-mgt/org.wso2.carbon.device.mgt.core/src/main/java/org/wso2/carbon/device/mgt/core/util/DeviceManagerUtil.java @@ -167,37 +167,22 @@ public final class DeviceManagerUtil { return ctx.getTenantId(); } - public static void publishAPI(APIConfig config) throws DeviceManagementException { + public static API getAPI(APIConfig config) throws APIManagementException { APIProvider provider = config.getProvider(); APIIdentifier id = new APIIdentifier(config.getOwner(), config.getName(), config.getVersion()); API api = new API(id); - try { - if(!provider.isAPIAvailable(id)) { - api.setContext(config.getContext()); - api.setUrl(config.getEndpoint()); - api.setUriTemplates(getURITemplates(config.getEndpoint(), - APIConstants.AUTH_APPLICATION_OR_USER_LEVEL_TOKEN)); - api.setVisibility(APIConstants.API_GLOBAL_VISIBILITY); - api.addAvailableTiers(provider.getTiers()); - api.setEndpointSecured(false); - api.setStatus(APIStatus.PUBLISHED); - api.setTransports(config.getTransports()); - - provider.addAPI(api); - } - } catch (APIManagementException e) { - throw new DeviceManagementException("Error occurred while registering the API", e); - } - } - - public static void removeAPI(APIConfig config) throws DeviceManagementException { - try { - APIProvider provider = config.getProvider(); - APIIdentifier id = new APIIdentifier(config.getOwner(), config.getName(), config.getVersion()); - provider.deleteAPI(id); - } catch (APIManagementException e) { - throw new DeviceManagementException("Error occurred while removing API", e); + if (!provider.isAPIAvailable(id)) { + api.setContext(config.getContext()); + api.setUrl(config.getEndpoint()); + api.setUriTemplates( + getURITemplates(config.getEndpoint(), APIConstants.AUTH_APPLICATION_OR_USER_LEVEL_TOKEN)); + api.setVisibility(APIConstants.API_GLOBAL_VISIBILITY); + api.addAvailableTiers(provider.getTiers()); + api.setEndpointSecured(false); + api.setStatus(APIStatus.PUBLISHED); + api.setTransports(config.getTransports()); } + return api; } private static Set getURITemplates(String endpoint, String authType) {