From 7d191d63c5bd5d5956a66a8a42ce030c4bd53aac Mon Sep 17 00:00:00 2001 From: megala21 Date: Tue, 1 Aug 2017 15:55:46 +0530 Subject: [PATCH] Fixing minor issues in PlatformDeployment --- .../impl/platform/GenericPlatformDAOImpl.java | 5 +- .../mgt/core/deployer/PlatformDeployer.java | 19 +++++-- .../mgt/core/impl/PlatformManagerImpl.java | 49 +++++++++---------- 3 files changed, 41 insertions(+), 32 deletions(-) diff --git a/components/application-mgt/org.wso2.carbon.device.application.mgt.core/src/main/java/org/wso2/carbon/device/application/mgt/core/dao/impl/platform/GenericPlatformDAOImpl.java b/components/application-mgt/org.wso2.carbon.device.application.mgt.core/src/main/java/org/wso2/carbon/device/application/mgt/core/dao/impl/platform/GenericPlatformDAOImpl.java index 7fad43a452c..dc8d866b15d 100644 --- a/components/application-mgt/org.wso2.carbon.device.application.mgt.core/src/main/java/org/wso2/carbon/device/application/mgt/core/dao/impl/platform/GenericPlatformDAOImpl.java +++ b/components/application-mgt/org.wso2.carbon.device.application.mgt.core/src/main/java/org/wso2/carbon/device/application/mgt/core/dao/impl/platform/GenericPlatformDAOImpl.java @@ -171,10 +171,11 @@ public class GenericPlatformDAOImpl extends AbstractDAOImpl implements PlatformD preparedStatement.execute(); } } - } else { + } else if (!isIdentifierNull) { String insertToPlatform = "UPDATE APPM_PLATFORM SET IDENTIFIER = ? WHERE ID = ?"; preparedStatement = connection.prepareStatement(insertToPlatform); - preparedStatement.setInt(1, platformId); + preparedStatement.setString(1, platform.getIdentifier()); + preparedStatement.setInt(2, platformId); preparedStatement.execute(); } } else { diff --git a/components/application-mgt/org.wso2.carbon.device.application.mgt.core/src/main/java/org/wso2/carbon/device/application/mgt/core/deployer/PlatformDeployer.java b/components/application-mgt/org.wso2.carbon.device.application.mgt.core/src/main/java/org/wso2/carbon/device/application/mgt/core/deployer/PlatformDeployer.java index 27e34a693dc..67ec78f4488 100644 --- a/components/application-mgt/org.wso2.carbon.device.application.mgt.core/src/main/java/org/wso2/carbon/device/application/mgt/core/deployer/PlatformDeployer.java +++ b/components/application-mgt/org.wso2.carbon.device.application.mgt.core/src/main/java/org/wso2/carbon/device/application/mgt/core/deployer/PlatformDeployer.java @@ -24,7 +24,9 @@ import org.apache.axis2.deployment.repository.util.DeploymentFileData; import org.apache.commons.logging.Log; import org.apache.commons.logging.LogFactory; import org.wso2.carbon.context.CarbonContext; +import org.wso2.carbon.context.PrivilegedCarbonContext; import org.wso2.carbon.device.application.mgt.common.exception.PlatformManagementException; +import org.wso2.carbon.device.application.mgt.common.services.PlatformManager; import org.wso2.carbon.device.application.mgt.core.internal.DataHolder; import org.wso2.carbon.device.application.mgt.core.util.Constants; import org.wso2.carbon.utils.multitenancy.MultitenantUtils; @@ -66,9 +68,18 @@ public class PlatformDeployer extends AbstractDeployer { Platform platformConf = (Platform) unmarshaller.unmarshal(deploymentFile); if (platformConf.getId().contentEquals(getPlatformID(deploymentFile.getName()))) { org.wso2.carbon.device.application.mgt.common.Platform platform = convert(platformConf); - DataHolder.getInstance().getPlatformManager() - .register(CarbonContext.getThreadLocalCarbonContext().getTenantId(), platform); - log.info("Platform configuration : " + deploymentFile.getName() + " deployed successfully"); + PlatformManager platformManager = DataHolder.getInstance().getPlatformManager(); + int tenantID = PrivilegedCarbonContext.getThreadLocalCarbonContext().getTenantId(); + + org.wso2.carbon.device.application.mgt.common.Platform existingPlatform = platformManager + .getPlatform(tenantID, platform.getIdentifier()); + if (existingPlatform != null && existingPlatform.isFileBased()) { + platformManager.update(tenantID, platformConf.getId(),platform); + log.info("Platform configuration : " + deploymentFile.getName() + " updated successfully"); + } else { + platformManager.register(CarbonContext.getThreadLocalCarbonContext().getTenantId(), platform); + log.info("Platform configuration : " + deploymentFile.getName() + " deployed successfully"); + } } else { log.error("Unable to deploy the platform - " + deploymentFile.getAbsolutePath() + "!. Platform config file name - " + deploymentFile.getName() @@ -89,7 +100,7 @@ public class PlatformDeployer extends AbstractDeployer { .unregister(CarbonContext.getThreadLocalCarbonContext().getTenantId(), platformId, true); log.info("Platform configuration : " + fileName + " un-deployed successfully"); } catch (PlatformManagementException e) { - log.error("Error occurred while un-deploying the platform - " + fileName); + log.error("Error occurred while un-deploying the platform - " + fileName, e); } } diff --git a/components/application-mgt/org.wso2.carbon.device.application.mgt.core/src/main/java/org/wso2/carbon/device/application/mgt/core/impl/PlatformManagerImpl.java b/components/application-mgt/org.wso2.carbon.device.application.mgt.core/src/main/java/org/wso2/carbon/device/application/mgt/core/impl/PlatformManagerImpl.java index 2e4551eb867..83c85cb006d 100644 --- a/components/application-mgt/org.wso2.carbon.device.application.mgt.core/src/main/java/org/wso2/carbon/device/application/mgt/core/impl/PlatformManagerImpl.java +++ b/components/application-mgt/org.wso2.carbon.device.application.mgt.core/src/main/java/org/wso2/carbon/device/application/mgt/core/impl/PlatformManagerImpl.java @@ -233,50 +233,46 @@ public class PlatformManagerImpl implements PlatformManager { "Platform sharing is a restricted operation, therefore Platform - " + platform.getIdentifier() + " cannot be shared by the tenant domain - " + tenantId); } - Platform oldPlatform; + try { + ConnectionManagerUtil.beginTransaction(); + Platform oldPlatform = DAOFactory.getPlatformDAO().getPlatform(tenantId, oldPlatformIdentifier); - if (platform.getIdentifier() != null && !platform.getIdentifier().equals(oldPlatformIdentifier)) { - try { - ConnectionManagerUtil.openConnection(); + if (oldPlatform == null) { + ConnectionManagerUtil.rollbackTransaction(); + throw new PlatformManagementException( + "Cannot update platform. Platform with identifier : " + oldPlatformIdentifier + + " does not exist."); + } + if (platform.getIdentifier() != null && !platform.getIdentifier().equals(oldPlatformIdentifier)) { Platform existingPlatform = DAOFactory.getPlatformDAO().getPlatform(tenantId, platform.getIdentifier()); if (existingPlatform != null) { + ConnectionManagerUtil.rollbackTransaction(); throw new PlatformManagementException( "Cannot update the identifier of the platform from '" + oldPlatformIdentifier + "' to '" + platform.getIdentifier() + "'. Another platform exists " + "already with the identifier '" + platform.getIdentifier() + "' for the tenant : " + tenantId); } - } catch (DBConnectionException e) { - throw new PlatformManagementException( - "Database Connection Exception while trying to update the " + "platform for the tenant : " - + tenantId, e); - } finally { - ConnectionManagerUtil.closeConnection(); } - } - try { + if (platform.isFileBased()) { Map tenantPlatforms = this.inMemoryStore.get(tenantId); + + // File based configurations will be updated in the server start-up as well.So in that case, cache, + // will be empty. if (tenantPlatforms == null) { - throw new PlatformManagementException( - "No platforms registered for the tenant - " + tenantId + " with platform identifier - " - + platform.getIdentifier()); + tenantPlatforms = new HashMap<>(); + this.inMemoryStore.put(tenantId, tenantPlatforms); } - oldPlatform = tenantPlatforms.get(oldPlatformIdentifier); - if (oldPlatform == null) { - throw new PlatformManagementException( - "No platforms registered for the tenant - " + tenantId + " with platform identifier - " - + platform.getIdentifier()); - } else { - ConnectionManagerUtil.beginTransaction(); - DAOFactory.getPlatformDAO().update(tenantId, oldPlatformIdentifier, platform); - platform.setId(oldPlatform.getId()); + if (tenantPlatforms.get(platform.getIdentifier()) == null) { tenantPlatforms.put(platform.getIdentifier(), platform); } + DAOFactory.getPlatformDAO().update(tenantId, oldPlatformIdentifier, platform); + platform.setId(oldPlatform.getId()); + tenantPlatforms.put(platform.getIdentifier(), platform); + } else { - ConnectionManagerUtil.beginTransaction(); - oldPlatform = DAOFactory.getPlatformDAO().getPlatform(tenantId, oldPlatformIdentifier); DAOFactory.getPlatformDAO().update(tenantId, oldPlatformIdentifier, platform); } if (platform.isDefaultTenantMapping() && !oldPlatform.isDefaultTenantMapping()) { @@ -292,6 +288,7 @@ public class PlatformManagerImpl implements PlatformManager { } DAOFactory.getPlatformDAO().addMapping(tenantId, getListOfString(platform.getIdentifier())); } catch (UserStoreException e) { + ConnectionManagerUtil.rollbackTransaction(); throw new PlatformManagementException("Error occurred while assigning the platforms for tenants!", e); }