From 5ad8d2938e65d9ef562ce82c0beb59b54b73f560 Mon Sep 17 00:00:00 2001 From: mharindu Date: Wed, 11 May 2016 10:50:32 +0530 Subject: [PATCH] Fixed issues of publishing APIs to synapse gateway --- .../publisher/APIPublisherStartupHandler.java | 67 +++++++++++++++++++ .../internal/APIPublisherDataHolder.java | 21 ++++++ .../APIPublisherServiceComponent.java | 3 + .../APIPublisherLifecycleListener.java | 21 ++++-- .../org.wso2.carbon.device.mgt.api/pom.xml | 2 +- .../pom.xml | 4 ++ pom.xml | 39 +++++------ 7 files changed, 129 insertions(+), 28 deletions(-) create mode 100644 components/apimgt-extensions/org.wso2.carbon.apimgt.webapp.publisher/src/main/java/org/wso2/carbon/apimgt/webapp/publisher/APIPublisherStartupHandler.java diff --git a/components/apimgt-extensions/org.wso2.carbon.apimgt.webapp.publisher/src/main/java/org/wso2/carbon/apimgt/webapp/publisher/APIPublisherStartupHandler.java b/components/apimgt-extensions/org.wso2.carbon.apimgt.webapp.publisher/src/main/java/org/wso2/carbon/apimgt/webapp/publisher/APIPublisherStartupHandler.java new file mode 100644 index 00000000000..30f79001535 --- /dev/null +++ b/components/apimgt-extensions/org.wso2.carbon.apimgt.webapp.publisher/src/main/java/org/wso2/carbon/apimgt/webapp/publisher/APIPublisherStartupHandler.java @@ -0,0 +1,67 @@ +/* + * Copyright (c) 2016, WSO2 Inc. (http://www.wso2.org) All Rights Reserved. + * + * WSO2 Inc. licenses this file to you under the Apache License, + * Version 2.0 (the "License"); you may not use this file except + * in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, + * software distributed under the License is distributed on an + * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY + * KIND, either express or implied. See the License for the + * specific language governing permissions and limitations + * under the License. + * + */ + +package org.wso2.carbon.apimgt.webapp.publisher; + +import org.apache.commons.logging.Log; +import org.apache.commons.logging.LogFactory; +import org.wso2.carbon.apimgt.api.model.API; +import org.wso2.carbon.apimgt.webapp.publisher.internal.APIPublisherDataHolder; +import org.wso2.carbon.core.ServerStartupObserver; + +public class APIPublisherStartupHandler implements ServerStartupObserver { + + private static final Log log = LogFactory.getLog(APIPublisherStartupHandler.class); + + @Override + public void completingServerStartup() { + + } + + @Override + public void completedServerStartup() { + // adding temporary due to a bug in the platform + Thread t = new Thread(new Runnable() { + @Override + public void run() { + try { + Thread.sleep(5000); + } catch (InterruptedException e) { + log.error("Error occurred while sleeping", e); + } + APIPublisherDataHolder.getInstance().setServerStarted(true); + log.info("Server has just started, hence started publishing unpublished APIs"); + if (log.isDebugEnabled()) { + log.debug("Total number of unpublished APIs: " + + APIPublisherDataHolder.getInstance().getUnpublishedApis().size()); + } + APIPublisherService publisher = APIPublisherDataHolder.getInstance().getApiPublisherService(); + while (!APIPublisherDataHolder.getInstance().getUnpublishedApis().isEmpty()) { + API api = APIPublisherDataHolder.getInstance().getUnpublishedApis().pop(); + try { + publisher.publishAPI(api); + } catch (java.lang.Exception e) { + log.error("Error occurred while publishing API '" + api.getId().getApiName(), e); + } + } + } + }); + t.start(); + } +} diff --git a/components/apimgt-extensions/org.wso2.carbon.apimgt.webapp.publisher/src/main/java/org/wso2/carbon/apimgt/webapp/publisher/internal/APIPublisherDataHolder.java b/components/apimgt-extensions/org.wso2.carbon.apimgt.webapp.publisher/src/main/java/org/wso2/carbon/apimgt/webapp/publisher/internal/APIPublisherDataHolder.java index bd291ce3be2..b06bc1a4ca6 100644 --- a/components/apimgt-extensions/org.wso2.carbon.apimgt.webapp.publisher/src/main/java/org/wso2/carbon/apimgt/webapp/publisher/internal/APIPublisherDataHolder.java +++ b/components/apimgt-extensions/org.wso2.carbon.apimgt.webapp.publisher/src/main/java/org/wso2/carbon/apimgt/webapp/publisher/internal/APIPublisherDataHolder.java @@ -19,12 +19,15 @@ package org.wso2.carbon.apimgt.webapp.publisher.internal; +import org.wso2.carbon.apimgt.api.model.API; import org.wso2.carbon.apimgt.webapp.publisher.APIPublisherService; import org.wso2.carbon.registry.core.service.RegistryService; import org.wso2.carbon.user.core.service.RealmService; import org.wso2.carbon.user.core.tenant.TenantManager; import org.wso2.carbon.utils.ConfigurationContextService; +import java.util.Stack; + public class APIPublisherDataHolder { private APIPublisherService apiPublisherService; @@ -32,6 +35,8 @@ public class APIPublisherDataHolder { private RealmService realmService; private TenantManager tenantManager; private RegistryService registryService; + private boolean isServerStarted; + private Stack unpublishedApis = new Stack<>(); private static APIPublisherDataHolder thisInstance = new APIPublisherDataHolder(); @@ -94,4 +99,20 @@ public class APIPublisherDataHolder { public void setRegistryService(RegistryService registryService) { this.registryService = registryService; } + + public boolean isServerStarted() { + return isServerStarted; + } + + public void setServerStarted(boolean serverStarted) { + isServerStarted = serverStarted; + } + + public Stack getUnpublishedApis() { + return unpublishedApis; + } + + public void setUnpublishedApis(Stack unpublishedApis) { + this.unpublishedApis = unpublishedApis; + } } diff --git a/components/apimgt-extensions/org.wso2.carbon.apimgt.webapp.publisher/src/main/java/org/wso2/carbon/apimgt/webapp/publisher/internal/APIPublisherServiceComponent.java b/components/apimgt-extensions/org.wso2.carbon.apimgt.webapp.publisher/src/main/java/org/wso2/carbon/apimgt/webapp/publisher/internal/APIPublisherServiceComponent.java index 2a67c78713d..ed9e4377c18 100644 --- a/components/apimgt-extensions/org.wso2.carbon.apimgt.webapp.publisher/src/main/java/org/wso2/carbon/apimgt/webapp/publisher/internal/APIPublisherServiceComponent.java +++ b/components/apimgt-extensions/org.wso2.carbon.apimgt.webapp.publisher/src/main/java/org/wso2/carbon/apimgt/webapp/publisher/internal/APIPublisherServiceComponent.java @@ -25,6 +25,8 @@ import org.osgi.service.component.ComponentContext; import org.wso2.carbon.apimgt.impl.APIManagerConfigurationService; import org.wso2.carbon.apimgt.webapp.publisher.APIPublisherService; import org.wso2.carbon.apimgt.webapp.publisher.APIPublisherServiceImpl; +import org.wso2.carbon.apimgt.webapp.publisher.APIPublisherStartupHandler; +import org.wso2.carbon.core.ServerStartupObserver; import org.wso2.carbon.registry.core.service.RegistryService; import org.wso2.carbon.user.core.service.RealmService; import org.wso2.carbon.utils.ConfigurationContextService; @@ -84,6 +86,7 @@ public class APIPublisherServiceComponent { APIPublisherService publisher = new APIPublisherServiceImpl(); APIPublisherDataHolder.getInstance().setApiPublisherService(publisher); bundleContext.registerService(APIPublisherService.class, publisher, null); + bundleContext.registerService(ServerStartupObserver.class, new APIPublisherStartupHandler(), null); } protected void setAPIManagerConfigurationService(APIManagerConfigurationService service) { diff --git a/components/apimgt-extensions/org.wso2.carbon.apimgt.webapp.publisher/src/main/java/org/wso2/carbon/apimgt/webapp/publisher/lifecycle/listener/APIPublisherLifecycleListener.java b/components/apimgt-extensions/org.wso2.carbon.apimgt.webapp.publisher/src/main/java/org/wso2/carbon/apimgt/webapp/publisher/lifecycle/listener/APIPublisherLifecycleListener.java index f3ecf346207..afff4d71882 100644 --- a/components/apimgt-extensions/org.wso2.carbon.apimgt.webapp.publisher/src/main/java/org/wso2/carbon/apimgt/webapp/publisher/lifecycle/listener/APIPublisherLifecycleListener.java +++ b/components/apimgt-extensions/org.wso2.carbon.apimgt.webapp.publisher/src/main/java/org/wso2/carbon/apimgt/webapp/publisher/lifecycle/listener/APIPublisherLifecycleListener.java @@ -74,13 +74,22 @@ public class APIPublisherLifecycleListener implements LifecycleListener { if (isTenantActive) { apiConfig.init(); API api = APIPublisherUtil.getAPI(apiConfig); - APIPublisherService apiPublisherService = - APIPublisherDataHolder.getInstance().getApiPublisherService(); - if (apiPublisherService == null) { - throw new IllegalStateException( - "API Publisher service is not initialized properly"); + boolean isServerStarted = APIPublisherDataHolder.getInstance().isServerStarted(); + if (isServerStarted) { + APIPublisherService apiPublisherService = + APIPublisherDataHolder.getInstance().getApiPublisherService(); + if (apiPublisherService == null) { + throw new IllegalStateException( + "API Publisher service is not initialized properly"); + } + apiPublisherService.publishAPI(api); + } else { + if (log.isDebugEnabled()) { + log.debug("Server has not started yet. Hence adding API '" + + api.getId().getApiName() + "' to the queue"); + } + APIPublisherDataHolder.getInstance().getUnpublishedApis().push(api); } - apiPublisherService.publishAPI(api); } else { log.error("No tenant [" + apiConfig.getTenantDomain() + "] " + "found when publishing the Web app"); diff --git a/components/device-mgt/org.wso2.carbon.device.mgt.api/pom.xml b/components/device-mgt/org.wso2.carbon.device.mgt.api/pom.xml index f6c02925293..195e9ed8534 100644 --- a/components/device-mgt/org.wso2.carbon.device.mgt.api/pom.xml +++ b/components/device-mgt/org.wso2.carbon.device.mgt.api/pom.xml @@ -146,7 +146,7 @@ provided - org.wso2.carbon.commons + org.wso2.carbon.identity org.wso2.carbon.user.mgt provided diff --git a/features/device-mgt/org.wso2.carbon.device.mgt.server.feature/pom.xml b/features/device-mgt/org.wso2.carbon.device.mgt.server.feature/pom.xml index ec7962494d4..e54f7542e17 100644 --- a/features/device-mgt/org.wso2.carbon.device.mgt.server.feature/pom.xml +++ b/features/device-mgt/org.wso2.carbon.device.mgt.server.feature/pom.xml @@ -120,6 +120,10 @@ org.wso2.carbon.identity:org.wso2.carbon.identity.oauth.stub:${carbon.identity.version} + + + org.apache.axiom.om + org.wso2.carbon.core.server:${carbon.kernel.version} diff --git a/pom.xml b/pom.xml index c71293b0494..f120f6e7c95 100644 --- a/pom.xml +++ b/pom.xml @@ -1375,16 +1375,16 @@ org.wso2.carbon.databridge.core ${carbon.analytics.common.version} - - org.wso2.carbon.commons - org.wso2.carbon.databridge.commons - ${carbon.commons.version} - - - org.wso2.carbon.commons - org.wso2.carbon.databridge.commons.thrift - ${carbon.commons.version} - + + + + + + + + + + org.wso2.carbon.analytics-common org.wso2.carbon.databridge.commons @@ -1445,9 +1445,9 @@ - org.wso2.carbon.commons + org.wso2.carbon.identity org.wso2.carbon.user.mgt - ${carbon.commons.version} + ${carbon.identity.version} provided @@ -1672,8 +1672,8 @@ 6.1.1 - 4.4.3 - [4.4.0, 4.5.0) + 4.4.5 + [4.4.0, 5.0.0) 1.5.4 1.3 @@ -1715,14 +1715,11 @@ 4.6.0 - 5.0.7 + 5.0.9-SNAPSHOT 4.5.0 - - 4.4.8 - 4.5.8 @@ -1735,7 +1732,7 @@ 1.1.0-SNAPSHOT - 4.4.8 + 4.5.2 1.4.0.wso2v1 2.4.0.wso2v1 2.6.0.wso2v1 @@ -1749,8 +1746,8 @@ [5.0.11,6.0.0) - 4.4.8 - [4.4.8, 5.0.0) + 4.5.2 + [4.5.2, 5.0.0) 2.7.16