removed fix for web app publishing start up issue when synapse gateway is used

revert-70aa11f8
ayyoob 9 years ago
parent 12db341d33
commit 64bd0fc061

@ -29,7 +29,6 @@ import org.wso2.carbon.apimgt.api.model.APIIdentifier;
import org.wso2.carbon.apimgt.api.model.APIStatus; import org.wso2.carbon.apimgt.api.model.APIStatus;
import org.wso2.carbon.apimgt.api.model.URITemplate; import org.wso2.carbon.apimgt.api.model.URITemplate;
import org.wso2.carbon.apimgt.impl.APIManagerFactory; import org.wso2.carbon.apimgt.impl.APIManagerFactory;
import org.wso2.carbon.apimgt.webapp.publisher.exception.APIPublisherException;
import org.wso2.carbon.apimgt.webapp.publisher.internal.APIPublisherDataHolder; import org.wso2.carbon.apimgt.webapp.publisher.internal.APIPublisherDataHolder;
import org.wso2.carbon.context.PrivilegedCarbonContext; import org.wso2.carbon.context.PrivilegedCarbonContext;
import org.wso2.carbon.governance.lcm.util.CommonUtil; import org.wso2.carbon.governance.lcm.util.CommonUtil;
@ -55,92 +54,54 @@ public class APIPublisherServiceImpl implements APIPublisherService {
@Override @Override
public void publishAPI(final API api) throws APIManagementException, FaultGatewaysException { public void publishAPI(final API api) throws APIManagementException, FaultGatewaysException {
// A thread is initialized because we cannot guarantee that the gateway is initialized before publishing. String tenantDomain = MultitenantUtils.getTenantDomain(api.getApiOwner());
// A better solution is needs to be implemented - should check in apimanager whether PrivilegedCarbonContext.startTenantFlow();
// the gateway is local and if so then publish it through OSGI PrivilegedCarbonContext.getThreadLocalCarbonContext().setTenantDomain(tenantDomain, true);
Runnable connector = new Runnable() { try {
public void run() { int tenantId = PrivilegedCarbonContext.getThreadLocalCarbonContext().getTenantId();
if (waitForServerStartup()) { // Below code snippet is added to load API Lifecycle in tenant mode.
return; RegistryService registryService = APIPublisherDataHolder.getInstance().getRegistryService();
CommonUtil.addDefaultLifecyclesIfNotAvailable(registryService.getConfigSystemRegistry(tenantId),
CommonUtil.getRootSystemRegistry(tenantId));
APIProvider provider = APIManagerFactory.getInstance().getAPIProvider(api.getApiOwner());
MultitenantUtils.getTenantDomain(api.getApiOwner());
if (provider != null) {
if (provider.isDuplicateContextTemplate(api.getContext())) {
throw new APIManagementException(
"Error occurred while adding the API. A duplicate API" +
" context already exists for " + api.getContext());
} }
if (log.isDebugEnabled()) { if (!provider.isAPIAvailable(api.getId())) {
log.debug("Publishing API '" + api.getId() + ""); provider.addAPI(api);
} provider.changeLifeCycleStatus(api.getId(), PUBLISH_ACTION);
try { if (log.isDebugEnabled()) {
Thread.sleep(5000); log.debug("Successfully published API '" + api.getId().getApiName() +
} catch (InterruptedException e) { "' with context '" + api.getContext() + "' and version '"
Thread.interrupted(); + api.getId().getVersion() + "'");
} }
synchronized (APIPublisherServiceImpl.class) { } else {
String tenantDomain = MultitenantUtils.getTenantDomain(api.getApiOwner()); api.setStatus(APIStatus.PUBLISHED);
PrivilegedCarbonContext.startTenantFlow(); provider.updateAPI(api);
PrivilegedCarbonContext.getThreadLocalCarbonContext().setTenantDomain(tenantDomain, true); if (log.isDebugEnabled()) {
try { log.debug("An API already exists with the name '" + api.getId().getApiName() +
int tenantId = PrivilegedCarbonContext.getThreadLocalCarbonContext().getTenantId(); "', context '" + api.getContext() + "' and version '"
// Below code snippet is added to load API Lifecycle in tenant mode. + api.getId().getVersion() + "'. Thus, the API config is updated");
RegistryService registryService = APIPublisherDataHolder.getInstance().getRegistryService();
CommonUtil.addDefaultLifecyclesIfNotAvailable(registryService.getConfigSystemRegistry(tenantId),
CommonUtil.getRootSystemRegistry(tenantId));
APIProvider provider = APIManagerFactory.getInstance().getAPIProvider(api.getApiOwner());
MultitenantUtils.getTenantDomain(api.getApiOwner());
if (provider != null) {
if (provider.isDuplicateContextTemplate(api.getContext())) {
throw new APIManagementException(
"Error occurred while adding the API. A duplicate API" +
" context already exists for " + api.getContext());
}
if (!provider.isAPIAvailable(api.getId())) {
provider.addAPI(api);
provider.changeLifeCycleStatus(api.getId(), PUBLISH_ACTION);
if (log.isDebugEnabled()) {
log.debug("Successfully published API '" + api.getId().getApiName() +
"' with context '" + api.getContext() + "' and version '"
+ api.getId().getVersion() + "'");
}
} else {
api.setStatus(APIStatus.PUBLISHED);
provider.updateAPI(api);
if (log.isDebugEnabled()) {
log.debug("An API already exists with the name '" + api.getId().getApiName() +
"', context '" + api.getContext() + "' and version '"
+ api.getId().getVersion() + "'. Thus, the API config is updated");
}
}
provider.saveSwagger20Definition(api.getId(), createSwaggerDefinition(api));
} else {
throw new APIManagementException("API provider configured for the given API configuration " +
"is null. Thus, the API is not published");
}
} catch (FileNotFoundException e) {
throw new APIPublisherException("Failed to retrieve life cycle file ", e);
} catch (RegistryException e) {
throw new APIPublisherException("Failed to access the registry ", e);
} catch (XMLStreamException e) {
throw new APIPublisherException("Failed parsing the lifecycle xml.", e);
} catch (FaultGatewaysException e) {
throw new APIPublisherException("Failed when publishing to the gateway", e);
} catch (APIManagementException e) {
throw new APIPublisherException("Failed publishing the API " + api.getId().getApiName(), e);
} finally {
PrivilegedCarbonContext.endTenantFlow();
} }
} }
provider.saveSwagger20Definition(api.getId(), createSwaggerDefinition(api));
} else {
throw new APIManagementException("API provider configured for the given API configuration " +
"is null. Thus, the API is not published");
} }
}; } catch (FileNotFoundException e) {
Thread connectorThread = new Thread(connector); throw new APIManagementException("Failed to retrieve life cycle file ", e);
connectorThread.setDaemon(true); } catch (RegistryException e) {
connectorThread.start(); throw new APIManagementException("Failed to access the registry ", e);
} } catch (XMLStreamException e) {
throw new APIManagementException("Failed parsing the lifecycle xml.", e);
private boolean waitForServerStartup() { } finally {
while (!APIPublisherDataHolder.getInstance().getServerStartupListener().isServerReady()) { PrivilegedCarbonContext.endTenantFlow();
try {
Thread.sleep(1000);
} catch (InterruptedException e) {
Thread.interrupted();
}
} }
return false;
} }
private String createSwaggerDefinition(API api) { private String createSwaggerDefinition(API api) {

@ -1,56 +0,0 @@
/*
* Copyright (c) 2015, 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.exception;
public class APIPublisherException extends RuntimeException {
private String errorMessage;
public String getErrorMessage() {
return errorMessage;
}
public void setErrorMessage(String errorMessage) {
this.errorMessage = errorMessage;
}
public APIPublisherException(String msg, Exception nestedEx) {
super(msg, nestedEx);
setErrorMessage(msg);
}
public APIPublisherException(String message, Throwable cause) {
super(message, cause);
setErrorMessage(message);
}
public APIPublisherException(String msg) {
super(msg);
setErrorMessage(msg);
}
public APIPublisherException() {
super();
}
public APIPublisherException(Throwable cause) {
super(cause);
}
}

@ -20,8 +20,6 @@ package org.wso2.carbon.apimgt.webapp.publisher.internal;
import org.wso2.carbon.apimgt.webapp.publisher.APIPublisherService; import org.wso2.carbon.apimgt.webapp.publisher.APIPublisherService;
import org.wso2.carbon.apimgt.webapp.publisher.lifecycle.util.ServerStartupListener;
import org.wso2.carbon.core.ServerStartupObserver;
import org.wso2.carbon.registry.core.service.RegistryService; import org.wso2.carbon.registry.core.service.RegistryService;
import org.wso2.carbon.user.core.service.RealmService; import org.wso2.carbon.user.core.service.RealmService;
import org.wso2.carbon.user.core.tenant.TenantManager; import org.wso2.carbon.user.core.tenant.TenantManager;
@ -34,7 +32,6 @@ public class APIPublisherDataHolder {
private RealmService realmService; private RealmService realmService;
private TenantManager tenantManager; private TenantManager tenantManager;
private RegistryService registryService; private RegistryService registryService;
private ServerStartupListener serverStartupListener;
private static APIPublisherDataHolder thisInstance = new APIPublisherDataHolder(); private static APIPublisherDataHolder thisInstance = new APIPublisherDataHolder();
@ -97,12 +94,4 @@ public class APIPublisherDataHolder {
public void setRegistryService(RegistryService registryService) { public void setRegistryService(RegistryService registryService) {
this.registryService = registryService; this.registryService = registryService;
} }
public ServerStartupListener getServerStartupListener() {
return serverStartupListener;
}
public void setServerStartupListener(ServerStartupListener serverStartupListener) {
this.serverStartupListener = serverStartupListener;
}
} }

@ -25,8 +25,6 @@ import org.osgi.service.component.ComponentContext;
import org.wso2.carbon.apimgt.impl.APIManagerConfigurationService; import org.wso2.carbon.apimgt.impl.APIManagerConfigurationService;
import org.wso2.carbon.apimgt.webapp.publisher.APIPublisherService; import org.wso2.carbon.apimgt.webapp.publisher.APIPublisherService;
import org.wso2.carbon.apimgt.webapp.publisher.APIPublisherServiceImpl; import org.wso2.carbon.apimgt.webapp.publisher.APIPublisherServiceImpl;
import org.wso2.carbon.apimgt.webapp.publisher.lifecycle.util.ServerStartupListener;
import org.wso2.carbon.core.ServerStartupObserver;
import org.wso2.carbon.registry.core.service.RegistryService; import org.wso2.carbon.registry.core.service.RegistryService;
import org.wso2.carbon.user.core.service.RealmService; import org.wso2.carbon.user.core.service.RealmService;
import org.wso2.carbon.utils.ConfigurationContextService; import org.wso2.carbon.utils.ConfigurationContextService;
@ -61,10 +59,6 @@ public class APIPublisherServiceComponent {
if (log.isDebugEnabled()) { if (log.isDebugEnabled()) {
log.debug("Initializing device management core bundle"); log.debug("Initializing device management core bundle");
} }
BundleContext bundleContext = componentContext.getBundleContext();
ServerStartupListener serverStartupListener = new ServerStartupListener();
bundleContext.registerService(ServerStartupObserver.class.getName(), serverStartupListener, null);
APIPublisherDataHolder.getInstance().setServerStartupListener(serverStartupListener);
/* Registering declarative service instances exposed by DeviceManagementServiceComponent */ /* Registering declarative service instances exposed by DeviceManagementServiceComponent */
this.registerServices(componentContext); this.registerServices(componentContext);

@ -1,44 +0,0 @@
/*
* 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.lifecycle.util;
import org.wso2.carbon.core.ServerStartupObserver;
/**
* This is added to make sure that the apis are publishsed after gateway services are intiialized.
*/
public class ServerStartupListener implements ServerStartupObserver {
private static volatile boolean serverReady = false;
@Override
public void completingServerStartup() {
}
@Override
public void completedServerStartup() {
ServerStartupListener.setServerReady(true);
}
public static boolean isServerReady() {
return ServerStartupListener.serverReady;
}
public static void setServerReady(boolean serverReady) {
ServerStartupListener.serverReady = serverReady;
}
}
Loading…
Cancel
Save