From 9bc069a95794cdff3a5d63ee28101e199f04ff60 Mon Sep 17 00:00:00 2001 From: megala21 Date: Wed, 2 Aug 2017 17:12:07 +0530 Subject: [PATCH] Adding Axis2Oberserver and improving the databse queries --- .../api/services/PlatformManagementAPI.java | 57 +++++- .../impl/PlatformManagementAPIImpl.java | 20 ++ .../mgt/common/services/PlatformManager.java | 5 + .../application/mgt/core/dao/PlatformDAO.java | 2 + .../impl/platform/GenericPlatformDAOImpl.java | 80 +++++--- .../mgt/core/deployer/PlatformDeployer.java | 2 +- .../mgt/core/impl/PlatformManagerImpl.java | 174 ++++++++++++++---- ...agementAxis2ConfigurationObserverImpl.java | 63 +++++++ .../mgt/core/internal/ServiceComponent.java | 12 +- .../util/ApplicationMgtDatabaseCreator.java | 6 +- .../mgt/core/util/ConnectionManagerUtil.java | 33 +++- .../dbscripts/cdm/application-mgt/h2.sql | 4 +- 12 files changed, 368 insertions(+), 90 deletions(-) create mode 100644 components/application-mgt/org.wso2.carbon.device.application.mgt.core/src/main/java/org/wso2/carbon/device/application/mgt/core/internal/PlatformManagementAxis2ConfigurationObserverImpl.java diff --git a/components/application-mgt/org.wso2.carbon.device.application.mgt.api/src/main/java/org/wso2/carbon/device/application/mgt/api/services/PlatformManagementAPI.java b/components/application-mgt/org.wso2.carbon.device.application.mgt.api/src/main/java/org/wso2/carbon/device/application/mgt/api/services/PlatformManagementAPI.java index 3721326a8d..ce7ecc88fd 100644 --- a/components/application-mgt/org.wso2.carbon.device.application.mgt.api/src/main/java/org/wso2/carbon/device/application/mgt/api/services/PlatformManagementAPI.java +++ b/components/application-mgt/org.wso2.carbon.device.application.mgt.api/src/main/java/org/wso2/carbon/device/application/mgt/api/services/PlatformManagementAPI.java @@ -60,8 +60,8 @@ import javax.ws.rs.core.Response; } ), tags = { - @Tag(name = "application_management", description = "Platform Management APIS related with " - + "Application Management") + @Tag(name = "device_management, application_management", description = "Platform Management APIS " + + "related with Application Management") } ) @Scopes ( @@ -279,13 +279,10 @@ public interface PlatformManagementAPI { value = { @ApiResponse( code = 200, - message = "OK. \n Successfully updated the platform"), - @ApiResponse( - code = 400, - message = "Bad Request. \n Invalid request parameters passed."), + message = "OK. \n Successfully deleted the platform"), @ApiResponse( code = 500, - message = "Internal Server Error. \n Error occurred while getting the platform list.", + message = "Internal Server Error. \n Error occurred while deleting the platform.", response = ErrorResponse.class) }) Response removePlatform( @@ -297,4 +294,50 @@ public interface PlatformManagementAPI { String identifier ); + @PUT + @Path("update-status/{identifier}") + @Produces(MediaType.APPLICATION_JSON) + @Consumes(MediaType.APPLICATION_JSON) + @ApiOperation( + consumes = MediaType.APPLICATION_JSON, + produces = MediaType.APPLICATION_JSON, + httpMethod = "PUT", + value = "Update Platform status", + notes = "This will update the platform status for the tenant space", + tags = "Platform Management", + extensions = { + @Extension(properties = { + @ExtensionProperty(name = SCOPE, value = "perm:platform:update") + }) + } + ) + @ApiResponses( + value = { + @ApiResponse( + code = 200, + message = "OK. \n Successfully updated the platform."), + @ApiResponse( + code = 404, + message = "Not found. \n Non-file based platform not found to update."), + @ApiResponse( + code = 500, + message = "Internal Server Error. \n Error occurred while getting the platform list.", + response = ErrorResponse.class) + }) + Response updatePlatformStatus( + @ApiParam( + name = "identifier", + required = true) + @PathParam("identifier") + @Size(max = 45) + String identifier, + @ApiParam(name = "status", allowableValues = "ENABLED, DISABLED", value = + "Provide the status of platform for that tenant:\n" + + "- ENABLED: The platforms that are currently enabled for the tenant\n" + + "- DISABLED: The platforms that currently disabled " + + "to be used for tenant\n", required = true) + @QueryParam("status") + String status + ); + } diff --git a/components/application-mgt/org.wso2.carbon.device.application.mgt.api/src/main/java/org/wso2/carbon/device/application/mgt/api/services/impl/PlatformManagementAPIImpl.java b/components/application-mgt/org.wso2.carbon.device.application.mgt.api/src/main/java/org/wso2/carbon/device/application/mgt/api/services/impl/PlatformManagementAPIImpl.java index 9db2273ce4..7d342b55a2 100644 --- a/components/application-mgt/org.wso2.carbon.device.application.mgt.api/src/main/java/org/wso2/carbon/device/application/mgt/api/services/impl/PlatformManagementAPIImpl.java +++ b/components/application-mgt/org.wso2.carbon.device.application.mgt.api/src/main/java/org/wso2/carbon/device/application/mgt/api/services/impl/PlatformManagementAPIImpl.java @@ -166,4 +166,24 @@ public class PlatformManagementAPIImpl implements PlatformManagementAPI { return APIUtil.getResponse(e, Response.Status.INTERNAL_SERVER_ERROR); } } + + @PUT + @Path("update-status/{identifier}") + @Override + public Response updatePlatformStatus(@PathParam("identifier") @Size(max = 45) String id, @QueryParam("status") + String status) { + int tenantId = PrivilegedCarbonContext.getThreadLocalCarbonContext().getTenantId(true); + try { + APIUtil.getPlatformManager().updatePlatformStatus(tenantId, id, status); + return Response.status(Response.Status.OK).build(); + } catch (PlatformManagementDAOException e) { + log.error("Platform Management Database Exception while trying to update the status of the platform with " + + "the identifier : " + id + " for the tenant : " + tenantId, e); + return APIUtil.getResponse(e, Response.Status.INTERNAL_SERVER_ERROR); + } catch (PlatformManagementException e) { + log.error("Platform Management Exception while trying to update the status of the platform with the " + + "identifier : " + id + " for the tenant : " + tenantId, e); + return APIUtil.getResponse(e, Response.Status.NOT_FOUND); + } + } } diff --git a/components/application-mgt/org.wso2.carbon.device.application.mgt.common/src/main/java/org/wso2/carbon/device/application/mgt/common/services/PlatformManager.java b/components/application-mgt/org.wso2.carbon.device.application.mgt.common/src/main/java/org/wso2/carbon/device/application/mgt/common/services/PlatformManager.java index d1edbef02f..58c12e558b 100644 --- a/components/application-mgt/org.wso2.carbon.device.application.mgt.common/src/main/java/org/wso2/carbon/device/application/mgt/common/services/PlatformManager.java +++ b/components/application-mgt/org.wso2.carbon.device.application.mgt.common/src/main/java/org/wso2/carbon/device/application/mgt/common/services/PlatformManager.java @@ -49,4 +49,9 @@ public interface PlatformManager { void removeMapping(int tenantId, String platformIdentifier) throws PlatformManagementException; + void updatePlatformStatus(int tenantId, String platformIdentifier, String status) + throws PlatformManagementException; + + void removePlatforms(int tenantId) throws PlatformManagementException; + } diff --git a/components/application-mgt/org.wso2.carbon.device.application.mgt.core/src/main/java/org/wso2/carbon/device/application/mgt/core/dao/PlatformDAO.java b/components/application-mgt/org.wso2.carbon.device.application.mgt.core/src/main/java/org/wso2/carbon/device/application/mgt/core/dao/PlatformDAO.java index 7311d74025..770f4cd7fe 100644 --- a/components/application-mgt/org.wso2.carbon.device.application.mgt.core/src/main/java/org/wso2/carbon/device/application/mgt/core/dao/PlatformDAO.java +++ b/components/application-mgt/org.wso2.carbon.device.application.mgt.core/src/main/java/org/wso2/carbon/device/application/mgt/core/dao/PlatformDAO.java @@ -46,4 +46,6 @@ public interface PlatformDAO { Platform getPlatform(int tenantId, String identifier) throws PlatformManagementDAOException; + void removePlatforms(int tenantId) throws PlatformManagementDAOException; + } 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 dc8d866b15..954d203294 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 @@ -22,12 +22,10 @@ import org.apache.commons.logging.Log; import org.apache.commons.logging.LogFactory; import org.wso2.carbon.device.application.mgt.common.Platform; import org.wso2.carbon.device.application.mgt.common.exception.DBConnectionException; -import org.wso2.carbon.device.application.mgt.common.exception.TransactionManagementException; import org.wso2.carbon.device.application.mgt.core.dao.PlatformDAO; import org.wso2.carbon.device.application.mgt.core.dao.common.Util; import org.wso2.carbon.device.application.mgt.core.dao.impl.AbstractDAOImpl; import org.wso2.carbon.device.application.mgt.core.exception.PlatformManagementDAOException; -import org.wso2.carbon.device.application.mgt.core.util.ConnectionManagerUtil; import org.wso2.carbon.utils.multitenancy.MultitenantConstants; import java.sql.Connection; @@ -49,7 +47,7 @@ public class GenericPlatformDAOImpl extends AbstractDAOImpl implements PlatformD try { int platformId = getPlatformId(tenantId, platform.getIdentifier()); if (platformId == -1) { - Connection connection = this.getConnection(); + Connection connection = this.getDBConnection(); if (!platform.isFileBased()) { String insertToPlatform = "INSERT INTO APPM_PLATFORM (IDENTIFIER, TENANT_ID, NAME, FILE_BASED, " + "DESCRIPTION, IS_SHARED, ICON_NAME)" + " VALUES (?, ?, ?, ?, ?, ?, ?)"; @@ -121,7 +119,7 @@ public class GenericPlatformDAOImpl extends AbstractDAOImpl implements PlatformD boolean isNameNull = platform.getName() == null; if (platformId != -1) { - Connection connection = this.getConnection(); + Connection connection = this.getDBConnection(); if (!platform.isFileBased()) { String insertToPlatform = "UPDATE APPM_PLATFORM SET DESCRIPTION=?, IS_SHARED=?, ICON_NAME=?"; if (!isIdentifierNull) { @@ -200,7 +198,7 @@ public class GenericPlatformDAOImpl extends AbstractDAOImpl implements PlatformD String query = "SELECT ID FROM APPM_PLATFORM WHERE (TENANT_ID=? AND IDENTIFIER=?) OR (IS_SHARED = TRUE AND " + "IDENTIFIER=?)"; try { - Connection connection = this.getConnection(); + Connection connection = this.getDBConnection(); preparedStatement = connection.prepareStatement(query); preparedStatement.setInt(1, tenantId); preparedStatement.setString(2, platformIdentifier); @@ -229,7 +227,7 @@ public class GenericPlatformDAOImpl extends AbstractDAOImpl implements PlatformD if (platform != null) { if (isFileBased == platform.isFileBased()) { - Connection connection = this.getConnection(); + Connection connection = this.getDBConnection(); String deletePlatform = "DELETE FROM APPM_PLATFORM WHERE ID = ?"; preparedStatement = connection.prepareStatement(deletePlatform); preparedStatement.setInt(1, platform.getId()); @@ -265,7 +263,7 @@ public class GenericPlatformDAOImpl extends AbstractDAOImpl implements PlatformD for (String platformIdentifier : platformIdentifiers) { if (getTenantPlatformMapping(tenantId, platformIdentifier) == -1) { int platformId = getPlatformId(tenantId, platformIdentifier); - Connection connection = this.getConnection(); + Connection connection = this.getDBConnection(); preparedStatement = connection.prepareStatement(insertMapping); preparedStatement.setInt(1, tenantId); preparedStatement.setInt(2, platformId); @@ -294,7 +292,7 @@ public class GenericPlatformDAOImpl extends AbstractDAOImpl implements PlatformD + "WHERE TENANT_ID=?) MAPPING JOIN (SELECT ID FROM APPM_PLATFORM WHERE APPM_PLATFORM.IDENTIFIER=?) " + "PLATFORM ON MAPPING.PLATFORM_ID=PLATFORM.ID"; try { - Connection connection = this.getConnection(); + Connection connection = this.getDBConnection(); preparedStatement = connection.prepareStatement(getMapping); preparedStatement.setInt(1, tenantId); preparedStatement.setString(2, platformIdentifier); @@ -321,7 +319,7 @@ public class GenericPlatformDAOImpl extends AbstractDAOImpl implements PlatformD try { int mappingId = getTenantPlatformMapping(tenantId, platformIdentifier); if (mappingId != -1) { - Connection connection = this.getConnection(); + Connection connection = this.getDBConnection(); preparedStatement = connection.prepareStatement(deleteMapping); preparedStatement.setInt(1, mappingId); preparedStatement.execute(); @@ -346,7 +344,7 @@ public class GenericPlatformDAOImpl extends AbstractDAOImpl implements PlatformD int platformId = getPlatformId(MultitenantConstants.SUPER_TENANT_ID, platformIdentifier); String getMapping = "DELETE FROM APPM_PLATFORM_TENANT_MAPPING WHERE TENANT_ID != ? AND PLATFORM_ID=?"; try { - Connection connection = this.getConnection(); + Connection connection = this.getDBConnection(); preparedStatement = connection.prepareStatement(getMapping); preparedStatement.setInt(1, MultitenantConstants.SUPER_TENANT_ID); preparedStatement.setInt(2, platformId); @@ -376,7 +374,7 @@ public class GenericPlatformDAOImpl extends AbstractDAOImpl implements PlatformD + "IS_SHARED = TRUE AND FILE_BASED = FALSE) PLATFORM LEFT JOIN APPM_PLATFORM_TENANT_MAPPING " + "MAPPING ON PLATFORM.ID = MAPPING.PLATFORM_ID"; try { - Connection connection = this.getConnection(); + Connection connection = this.getDBConnection(); preparedStatement = connection.prepareStatement(selectQuery); preparedStatement.setInt(1, tenantId); resultSet = preparedStatement.executeQuery(); @@ -385,8 +383,8 @@ public class GenericPlatformDAOImpl extends AbstractDAOImpl implements PlatformD log.debug("Platform retrieved for the tenant Id " + tenantId); } while (resultSet.next()) { - String identifier = resultSet.getString("PLATFORM.IDENTIFIER"); - int mappingID = resultSet.getInt("MAPPING.ID"); + int mappingID = resultSet.getInt(1); + String identifier = resultSet.getString(2); Platform platform = getPlatform(tenantId, identifier); if (mappingID != 0) { platform.setEnabled(true); @@ -399,8 +397,7 @@ public class GenericPlatformDAOImpl extends AbstractDAOImpl implements PlatformD } } if (log.isDebugEnabled()) { - log.debug("Number of platforms available for the tenant ID - " + tenantId + " :" + platforms - .size()); + log.debug("Number of platforms available for the tenant ID - " + tenantId + " :" + platforms.size()); } return platforms; } catch (DBConnectionException e) { @@ -409,7 +406,7 @@ public class GenericPlatformDAOImpl extends AbstractDAOImpl implements PlatformD } catch (SQLException e) { throw new PlatformManagementDAOException("Error occurred when executing query - " + selectQuery, e); } finally { - Util.cleanupResources(preparedStatement,resultSet); + Util.cleanupResources(preparedStatement, resultSet); } } @@ -420,7 +417,7 @@ public class GenericPlatformDAOImpl extends AbstractDAOImpl implements PlatformD "OR (IS_SHARED = TRUE AND IDENTIFIER=?) AND FILE_BASED = FALSE ) PLATFORM " + "LEFT JOIN APPM_PLATFORM_PROPERTIES PROPS ON PLATFORM.ID = PROPS.PLATFORM_ID"; try { - Connection connection = this.getConnection(); + Connection connection = this.getDBConnection(); preparedStatement = connection.prepareStatement(platformQuery); preparedStatement.setString(1, tenantDomain); preparedStatement.setString(2, platformIdentifier); @@ -460,6 +457,7 @@ public class GenericPlatformDAOImpl extends AbstractDAOImpl implements PlatformD } } + @Override public Platform getPlatform(int tenantId, String identifier) throws PlatformManagementDAOException { Connection conn; PreparedStatement stmt = null; @@ -467,8 +465,11 @@ public class GenericPlatformDAOImpl extends AbstractDAOImpl implements PlatformD String sql = ""; try { - conn = this.getConnection(); - sql = "SELECT * FROM APPM_PLATFORM WHERE IDENTIFIER = ? AND TENANT_ID = ?"; + conn = this.getDBConnection(); + sql = "SELECT MAPPING.ID, PLATFORM.IDENTIFIER, PLATFORM.FILE_BASED, PLATFORM.ID, PLATFORM.NAME, PLATFORM" + + ".DESCRIPTION, PLATFORM.ICON_NAME, PLATFORM.IS_SHARED FROM (SELECT * FROM APPM_PLATFORM WHERE " + + "IDENTIFIER= ? AND (TENANT_ID=? OR IS_SHARED = TRUE AND FILE_BASED = FALSE)) PLATFORM LEFT JOIN " + + "APPM_PLATFORM_TENANT_MAPPING MAPPING ON PLATFORM.ID = MAPPING.PLATFORM_ID"; stmt = conn.prepareStatement(sql); stmt.setString(1, identifier); @@ -479,15 +480,21 @@ public class GenericPlatformDAOImpl extends AbstractDAOImpl implements PlatformD if (rs.next()) { platform = new Platform(); - platform.setFileBased(rs.getBoolean("FILE_BASED")); + platform.setFileBased(rs.getBoolean(3)); - platform.setIdentifier(rs.getString("IDENTIFIER")); + platform.setIdentifier(rs.getString(2)); if (!platform.isFileBased()) { - platform.setId(rs.getInt("ID")); - platform.setName(rs.getString("NAME")); - platform.setDescription(rs.getString("DESCRIPTION")); - platform.setIconName(rs.getString("ICON_NAME")); - platform.setShared(rs.getBoolean("IS_SHARED")); + platform.setId(rs.getInt(4)); + platform.setName(rs.getString(5)); + platform.setDescription(rs.getString(6)); + platform.setIconName(rs.getString(7)); + platform.setShared(rs.getBoolean(8)); + + if (rs.getInt(1) != 0) { + platform.setEnabled(true); + } else { + platform.setEnabled(false); + } } } return platform; @@ -500,4 +507,25 @@ public class GenericPlatformDAOImpl extends AbstractDAOImpl implements PlatformD Util.cleanupResources(stmt, rs); } } + + @Override + public void removePlatforms(int tenantId) throws PlatformManagementDAOException { + PreparedStatement preparedStatement = null; + String sql = "DELETE FROM APPM_PLATFORM WHERE TENANT_ID = ?"; + + try { + Connection connection = this.getDBConnection(); + preparedStatement = connection.prepareStatement(sql); + preparedStatement.setInt(1, tenantId); + preparedStatement.executeUpdate(); + } catch (DBConnectionException e) { + throw new PlatformManagementDAOException("Database connection error while removing the platforms for the " + + "tenant - " + tenantId); + } catch (SQLException e) { + throw new PlatformManagementDAOException("SQL exception while executing the query " + sql + " for " + + "the tenant : " + tenantId); + } finally { + Util.cleanupResources(preparedStatement, null); + } + } } 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 67ec78f448..d03d3e82bd 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 @@ -74,7 +74,7 @@ public class PlatformDeployer extends AbstractDeployer { 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); + platformManager.update(tenantID, platformConf.getId(), platform); log.info("Platform configuration : " + deploymentFile.getName() + " updated successfully"); } else { platformManager.register(CarbonContext.getThreadLocalCarbonContext().getTenantId(), platform); 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 83c85cb006..31ab5552b3 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 @@ -52,7 +52,7 @@ public class PlatformManagerImpl implements PlatformManager { @Override public void initialize(int tenantId) throws PlatformManagementException { try { - ConnectionManagerUtil.beginTransaction(); + ConnectionManagerUtil.beginDBTransaction(); List platforms = DAOFactory.getPlatformDAO().getPlatforms(tenantId); List platformIdentifiers = new ArrayList<>(); for (Platform platform : platforms) { @@ -61,19 +61,22 @@ public class PlatformManagerImpl implements PlatformManager { } } DAOFactory.getPlatformDAO().addMapping(tenantId, platformIdentifiers); - ConnectionManagerUtil.commitTransaction(); + ConnectionManagerUtil.commitDBTransaction(); } catch (TransactionManagementException e) { - ConnectionManagerUtil.rollbackTransaction(); + ConnectionManagerUtil.rollbackDBTransaction(); throw new PlatformManagementDAOException( "Transaction Management Exception while initializing the " + "platforms for the tenant : " + tenantId, e); } catch (DBConnectionException e) { - ConnectionManagerUtil.rollbackTransaction(); + ConnectionManagerUtil.rollbackDBTransaction(); throw new PlatformManagementDAOException( "Database Connection Exception while initializing the " + "platforms for the tenant : " + tenantId, e); + } catch (PlatformManagementDAOException e) { + ConnectionManagerUtil.rollbackDBTransaction(); + throw e; } finally { - ConnectionManagerUtil.closeConnection(); + ConnectionManagerUtil.closeDBConnection(); } } @@ -86,13 +89,18 @@ public class PlatformManagerImpl implements PlatformManager { + "PlatformManager level"); } try { - ConnectionManagerUtil.openConnection(); + ConnectionManagerUtil.beginDBTransaction(); platforms = DAOFactory.getPlatformDAO().getPlatforms(tenantId); - } catch (DBConnectionException e) { + ConnectionManagerUtil.commitDBTransaction(); + } catch (DBConnectionException | TransactionManagementException e) { + ConnectionManagerUtil.rollbackDBTransaction(); throw new PlatformManagementDAOException( "Database Connection Exception while getting the platforms for the tenant : " + tenantId, e); + } catch (PlatformManagementDAOException e) { + ConnectionManagerUtil.rollbackDBTransaction(); + throw e; } finally { - ConnectionManagerUtil.closeConnection(); + ConnectionManagerUtil.closeDBConnection(); } if (log.isDebugEnabled()) { log.debug("Number of platforms received from DAO layer is " + platforms.size() + " for the tenant " @@ -128,17 +136,22 @@ public class PlatformManagerImpl implements PlatformManager { Platform platform = getPlatformFromInMemory(tenantId, identifier); if (platform == null) { try { - ConnectionManagerUtil.openConnection(); + ConnectionManagerUtil.beginDBTransaction(); platform = DAOFactory.getPlatformDAO().getPlatform(tenantId, identifier); + ConnectionManagerUtil.commitDBTransaction(); if (platform != null) { return platform; } - } catch (DBConnectionException e) { + } catch (DBConnectionException | TransactionManagementException e) { + ConnectionManagerUtil.rollbackDBTransaction(); throw new PlatformManagementDAOException( "Database Connection Exception while trying to get the " + "platform with the id :" + identifier + " for the tenant : " + tenantId, e); + } catch (PlatformManagementDAOException e) { + ConnectionManagerUtil.rollbackDBTransaction(); + throw e; } finally { - ConnectionManagerUtil.closeConnection(); + ConnectionManagerUtil.closeDBConnection(); } } else { return new Platform(platform); @@ -175,7 +188,7 @@ public class PlatformManagerImpl implements PlatformManager { + " cannot be shared by the tenant domain - " + tenantId); } try { - ConnectionManagerUtil.beginTransaction(); + ConnectionManagerUtil.beginDBTransaction(); int platformId = DAOFactory.getPlatformDAO().register(tenantId, platform); if (platform.isFileBased()) { platform.setId(platformId); @@ -187,7 +200,7 @@ public class PlatformManagerImpl implements PlatformManager { if (tenantPlatforms.get(platform.getIdentifier()) == null) { tenantPlatforms.put(platform.getIdentifier(), platform); } else { - ConnectionManagerUtil.rollbackTransaction(); + ConnectionManagerUtil.rollbackDBTransaction(); throw new PlatformManagementException( "Platform - " + platform.getIdentifier() + " is already registered!"); } @@ -204,24 +217,27 @@ public class PlatformManagerImpl implements PlatformManager { } DAOFactory.getPlatformDAO().addMapping(tenantId, getListOfString(platform.getIdentifier())); } catch (UserStoreException e) { - ConnectionManagerUtil.rollbackTransaction(); + ConnectionManagerUtil.rollbackDBTransaction(); throw new PlatformManagementException("Error occurred while assigning the platforms for tenants!", e); } } - ConnectionManagerUtil.commitTransaction(); + ConnectionManagerUtil.commitDBTransaction(); } catch (TransactionManagementException e) { - ConnectionManagerUtil.rollbackTransaction(); + ConnectionManagerUtil.rollbackDBTransaction(); throw new PlatformManagementDAOException( "Transaction Management Exception while trying to register a " + "platform with id " + platform .getIdentifier() + " for tenant " + tenantId); } catch (DBConnectionException e) { - ConnectionManagerUtil.rollbackTransaction(); + ConnectionManagerUtil.rollbackDBTransaction(); throw new PlatformManagementDAOException( "Database Connection Exception while trying to register a " + "platform with id " + platform .getIdentifier() + " for tenant " + tenantId); + } catch (PlatformManagementDAOException e) { + ConnectionManagerUtil.rollbackDBTransaction(); + throw e; } finally { - ConnectionManagerUtil.closeConnection(); + ConnectionManagerUtil.closeDBConnection(); } } @@ -234,11 +250,11 @@ public class PlatformManagerImpl implements PlatformManager { + " cannot be shared by the tenant domain - " + tenantId); } try { - ConnectionManagerUtil.beginTransaction(); + ConnectionManagerUtil.beginDBTransaction(); Platform oldPlatform = DAOFactory.getPlatformDAO().getPlatform(tenantId, oldPlatformIdentifier); if (oldPlatform == null) { - ConnectionManagerUtil.rollbackTransaction(); + ConnectionManagerUtil.commitDBTransaction(); throw new PlatformManagementException( "Cannot update platform. Platform with identifier : " + oldPlatformIdentifier + " does not exist."); @@ -247,7 +263,7 @@ public class PlatformManagerImpl implements PlatformManager { Platform existingPlatform = DAOFactory.getPlatformDAO().getPlatform(tenantId, platform.getIdentifier()); if (existingPlatform != null) { - ConnectionManagerUtil.rollbackTransaction(); + ConnectionManagerUtil.commitDBTransaction(); throw new PlatformManagementException( "Cannot update the identifier of the platform from '" + oldPlatformIdentifier + "' to '" + platform.getIdentifier() + "'. Another platform exists " @@ -288,7 +304,7 @@ public class PlatformManagerImpl implements PlatformManager { } DAOFactory.getPlatformDAO().addMapping(tenantId, getListOfString(platform.getIdentifier())); } catch (UserStoreException e) { - ConnectionManagerUtil.rollbackTransaction(); + ConnectionManagerUtil.rollbackDBTransaction(); throw new PlatformManagementException("Error occurred while assigning the platforms for tenants!", e); } @@ -296,19 +312,22 @@ public class PlatformManagerImpl implements PlatformManager { if (!platform.isShared() && oldPlatform.isShared()) { DAOFactory.getPlatformDAO().removeMappingTenants(platform.getIdentifier()); } - ConnectionManagerUtil.commitTransaction(); + ConnectionManagerUtil.commitDBTransaction(); } catch (TransactionManagementException e) { - ConnectionManagerUtil.rollbackTransaction(); + ConnectionManagerUtil.rollbackDBTransaction(); throw new PlatformManagementDAOException( "Transaction Management Exception while trying to update " + "platform : " + oldPlatformIdentifier + " of tenant :" + tenantId); } catch (DBConnectionException e) { - ConnectionManagerUtil.rollbackTransaction(); + ConnectionManagerUtil.rollbackDBTransaction(); throw new PlatformManagementDAOException( "Database Connection Exception while trying to update " + "platform : " + oldPlatformIdentifier + " of tenant :" + tenantId); + } catch (PlatformManagementDAOException e) { + ConnectionManagerUtil.rollbackDBTransaction(); + throw e; } finally { - ConnectionManagerUtil.closeConnection(); + ConnectionManagerUtil.closeDBConnection(); } } @@ -321,7 +340,7 @@ public class PlatformManagerImpl implements PlatformManager { @Override public void unregister(int tenantId, String identifier, boolean isFileBased) throws PlatformManagementException { try { - ConnectionManagerUtil.beginTransaction(); + ConnectionManagerUtil.beginDBTransaction(); DAOFactory.getPlatformDAO().unregister(tenantId, identifier, isFileBased); if (isFileBased) { @@ -330,33 +349,41 @@ public class PlatformManagerImpl implements PlatformManager { tenantPlatforms.remove(identifier); } } - ConnectionManagerUtil.commitTransaction(); + ConnectionManagerUtil.commitDBTransaction(); } catch (TransactionManagementException e) { - ConnectionManagerUtil.rollbackTransaction(); + ConnectionManagerUtil.rollbackDBTransaction(); throw new PlatformManagementDAOException( "Transaction Management Exception while trying to un-register " + "the platform with identifier : " + identifier + " tenant :" + tenantId, e); } catch (DBConnectionException e) { - ConnectionManagerUtil.rollbackTransaction(); + ConnectionManagerUtil.rollbackDBTransaction(); throw new PlatformManagementDAOException( "Database Connection Exception while trying to un-register " + "the platform with identifier : " + identifier + " tenant :" + tenantId, e); + } catch (PlatformManagementDAOException e) { + ConnectionManagerUtil.rollbackDBTransaction(); + throw e; } finally { - ConnectionManagerUtil.closeConnection(); + ConnectionManagerUtil.closeDBConnection(); } } @Override public void addMapping(int tenantId, List platformIdentifiers) throws PlatformManagementException { try { - ConnectionManagerUtil.openConnection(); + ConnectionManagerUtil.beginDBTransaction(); DAOFactory.getPlatformDAO().addMapping(tenantId, platformIdentifiers); - } catch (DBConnectionException e) { + ConnectionManagerUtil.commitDBTransaction(); + } catch (DBConnectionException | TransactionManagementException e) { + ConnectionManagerUtil.rollbackDBTransaction(); throw new PlatformManagementDAOException( "Database Connection Exception while trying to add tenant " + "mapping for tenant ID : " + tenantId); + } catch (PlatformManagementDAOException e) { + ConnectionManagerUtil.rollbackDBTransaction(); + throw e; } finally { - ConnectionManagerUtil.closeConnection(); + ConnectionManagerUtil.closeDBConnection(); } } @@ -370,13 +397,86 @@ public class PlatformManagerImpl implements PlatformManager { @Override public void removeMapping(int tenantId, String platformIdentifier) throws PlatformManagementException { try { - ConnectionManagerUtil.openConnection(); + ConnectionManagerUtil.beginDBTransaction(); DAOFactory.getPlatformDAO().removeMapping(tenantId, platformIdentifier); - } catch (DBConnectionException e) { + ConnectionManagerUtil.commitDBTransaction(); + } catch (DBConnectionException | TransactionManagementException e) { + ConnectionManagerUtil.rollbackDBTransaction(); throw new PlatformManagementDAOException( "Database Connection Exception while trying to remove tenant mapping for tenant ID : " + tenantId); + } catch (PlatformManagementDAOException e) { + ConnectionManagerUtil.rollbackDBTransaction(); + throw e; } finally { - ConnectionManagerUtil.closeConnection(); + ConnectionManagerUtil.closeDBConnection(); } } + + @Override + public void updatePlatformStatus(int tenantId, String platformIdentifier, String status) + throws PlatformManagementException { + try { + ConnectionManagerUtil.beginDBTransaction(); + Platform platform = DAOFactory.getPlatformDAO().getPlatform(tenantId, platformIdentifier); + + if (platform == null) { + ConnectionManagerUtil.commitDBTransaction(); + throw new PlatformManagementException("Platform with identifier : " + platformIdentifier + " does not" + + " exist for the tenant with id " + tenantId); + } else { + boolean isEnabledNewStatus = status.equalsIgnoreCase("ENABLED"); + + // If the platform is already in the same status. No need to enable the platform again + if (isEnabledNewStatus == platform.isEnabled()) { + if (log.isDebugEnabled()) { + log.debug("Platform with identifier : " + platformIdentifier + " is already in " + + (isEnabledNewStatus ? "Enabled" : "Disabled") + " status. No need to update."); + } + ConnectionManagerUtil.commitDBTransaction(); + return; + } else { + if (isEnabledNewStatus) { + DAOFactory.getPlatformDAO().addMapping(tenantId, getListOfString(platform.getIdentifier())); + } else { + DAOFactory.getPlatformDAO().removeMapping(tenantId, platform.getIdentifier()); + } + + if (log.isDebugEnabled()) { + log.debug("Platform with identifier : " + platformIdentifier + " successfully " + + (isEnabledNewStatus ? "Enabled" : "Disabled")); + } + } + } + ConnectionManagerUtil.commitDBTransaction(); + } catch (TransactionManagementException | DBConnectionException ex) { + ConnectionManagerUtil.rollbackDBTransaction(); + throw new PlatformManagementDAOException("Database exception while trying to update the status of platform " + + "with identifier '" + platformIdentifier + "' for the tenant" + tenantId); + } catch (PlatformManagementDAOException e) { + ConnectionManagerUtil.rollbackDBTransaction(); + throw e; + } finally { + ConnectionManagerUtil.closeDBConnection(); + } + + } + + @Override + public void removePlatforms(int tenantId) throws PlatformManagementException { + try { + ConnectionManagerUtil.beginDBTransaction(); + DAOFactory.getPlatformDAO().removePlatforms(tenantId); + ConnectionManagerUtil.commitDBTransaction(); + } catch (TransactionManagementException | DBConnectionException e) { + ConnectionManagerUtil.rollbackDBTransaction(); + throw new PlatformManagementDAOException("Database exception while trying to remove all the platforms for" + + " the tenant " + tenantId); + } catch (PlatformManagementDAOException e) { + ConnectionManagerUtil.rollbackDBTransaction(); + throw e; + } finally { + ConnectionManagerUtil.closeDBConnection(); + } + } + } diff --git a/components/application-mgt/org.wso2.carbon.device.application.mgt.core/src/main/java/org/wso2/carbon/device/application/mgt/core/internal/PlatformManagementAxis2ConfigurationObserverImpl.java b/components/application-mgt/org.wso2.carbon.device.application.mgt.core/src/main/java/org/wso2/carbon/device/application/mgt/core/internal/PlatformManagementAxis2ConfigurationObserverImpl.java new file mode 100644 index 0000000000..3c6e9cc92a --- /dev/null +++ b/components/application-mgt/org.wso2.carbon.device.application.mgt.core/src/main/java/org/wso2/carbon/device/application/mgt/core/internal/PlatformManagementAxis2ConfigurationObserverImpl.java @@ -0,0 +1,63 @@ +/* + * Copyright (c) 2017, 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.device.application.mgt.core.internal; + +import org.apache.axis2.context.ConfigurationContext; +import org.apache.commons.logging.Log; +import org.apache.commons.logging.LogFactory; +import org.wso2.carbon.context.PrivilegedCarbonContext; +import org.wso2.carbon.device.application.mgt.common.exception.PlatformManagementException; +import org.wso2.carbon.utils.AbstractAxis2ConfigurationContextObserver; + +/** + * PlatformManagementAxis2ConfigurationObserverImpl is responsible for adding relevant platform mapping of shared + * platforms during the tenant creation time. + */ +public class PlatformManagementAxis2ConfigurationObserverImpl extends AbstractAxis2ConfigurationContextObserver { + private static Log log = LogFactory.getLog(PlatformManagementAxis2ConfigurationObserverImpl.class); + + /** + * Whenever a new tenant creation happens, shared platforms need to be added for the relevant tenant. + * @param tenantId Id of the tenant that is being created + */ + @Override + public void creatingConfigurationContext(int tenantId) { + try { + DataHolder.getInstance().getPlatformManager().initialize(tenantId); + } catch (PlatformManagementException e) { + log.error("Error while trying add platforms to the newly created tenant " + tenantId, e); + } + } + + /** + * Whenever terminating a tenant,the platforms added by the tenant need to be removed. + * @param configContext Configuration context. + */ + @Override + public void terminatingConfigurationContext(ConfigurationContext configContext) { + PrivilegedCarbonContext carbonContext = PrivilegedCarbonContext.getThreadLocalCarbonContext(); + int tenantId = carbonContext.getTenantId(); + try { + DataHolder.getInstance().getPlatformManager().removePlatforms(tenantId); + } catch (PlatformManagementException e) { + log.error("Error while removing shared platforms while removing the tenant: " + tenantId, e); + } + } +} diff --git a/components/application-mgt/org.wso2.carbon.device.application.mgt.core/src/main/java/org/wso2/carbon/device/application/mgt/core/internal/ServiceComponent.java b/components/application-mgt/org.wso2.carbon.device.application.mgt.core/src/main/java/org/wso2/carbon/device/application/mgt/core/internal/ServiceComponent.java index e729af9a6a..1a5884852d 100644 --- a/components/application-mgt/org.wso2.carbon.device.application.mgt.core/src/main/java/org/wso2/carbon/device/application/mgt/core/internal/ServiceComponent.java +++ b/components/application-mgt/org.wso2.carbon.device.application.mgt.core/src/main/java/org/wso2/carbon/device/application/mgt/core/internal/ServiceComponent.java @@ -40,6 +40,7 @@ import org.wso2.carbon.device.application.mgt.core.util.ApplicationManagementUti import org.wso2.carbon.device.mgt.core.service.DeviceManagementProviderService; import org.wso2.carbon.ndatasource.core.DataSourceService; import org.wso2.carbon.user.core.service.RealmService; +import org.wso2.carbon.utils.Axis2ConfigurationContextObserver; import javax.naming.NamingException; @@ -117,6 +118,9 @@ public class ServiceComponent { DataHolder.getInstance().setApplicationUploadManager(uploadManager); bundleContext.registerService(ApplicationUploadManager.class.getName(), uploadManager, null); + bundleContext.registerService(Axis2ConfigurationContextObserver.class.getName(), + new PlatformManagementAxis2ConfigurationObserverImpl(), null); + DAOFactory.init(datasourceName); DAOFactory.initDatabases(); log.info("ApplicationManagement core bundle has been successfully initialized"); @@ -154,12 +158,12 @@ public class ServiceComponent { } protected void setDataSourceService(DataSourceService dataSourceService) { - //Not implemented. Not needed but to make sure the datasource service are registered, as it is needed create - // databases. + /*Not implemented. Not needed but to make sure the datasource service are registered, as it is needed create + databases. */ } protected void unsetDataSourceService(DataSourceService dataSourceService) { - //Not implemented. Not needed but to make sure the datasource service are registered, as it is needed to create - // databases. + /*Not implemented. Not needed but to make sure the datasource service are registered, as it is needed to create + databases.*/ } } diff --git a/components/application-mgt/org.wso2.carbon.device.application.mgt.core/src/main/java/org/wso2/carbon/device/application/mgt/core/util/ApplicationMgtDatabaseCreator.java b/components/application-mgt/org.wso2.carbon.device.application.mgt.core/src/main/java/org/wso2/carbon/device/application/mgt/core/util/ApplicationMgtDatabaseCreator.java index 1fe8961a5b..7b9478e17f 100644 --- a/components/application-mgt/org.wso2.carbon.device.application.mgt.core/src/main/java/org/wso2/carbon/device/application/mgt/core/util/ApplicationMgtDatabaseCreator.java +++ b/components/application-mgt/org.wso2.carbon.device.application.mgt.core/src/main/java/org/wso2/carbon/device/application/mgt/core/util/ApplicationMgtDatabaseCreator.java @@ -19,13 +19,11 @@ package org.wso2.carbon.device.application.mgt.core.util; import java.io.File; - +import org.apache.commons.logging.Log; +import org.apache.commons.logging.LogFactory; import org.wso2.carbon.utils.CarbonUtils; import org.wso2.carbon.utils.dbcreator.DatabaseCreator; -import org.apache.commons.logging.LogFactory; -import org.apache.commons.logging.Log; - /** * ApplicationMgtDatabaseCreator is responsible for creating the Application Management related tables. */ diff --git a/components/application-mgt/org.wso2.carbon.device.application.mgt.core/src/main/java/org/wso2/carbon/device/application/mgt/core/util/ConnectionManagerUtil.java b/components/application-mgt/org.wso2.carbon.device.application.mgt.core/src/main/java/org/wso2/carbon/device/application/mgt/core/util/ConnectionManagerUtil.java index 9235715616..d96854bb9f 100644 --- a/components/application-mgt/org.wso2.carbon.device.application.mgt.core/src/main/java/org/wso2/carbon/device/application/mgt/core/util/ConnectionManagerUtil.java +++ b/components/application-mgt/org.wso2.carbon.device.application.mgt.core/src/main/java/org/wso2/carbon/device/application/mgt/core/util/ConnectionManagerUtil.java @@ -24,11 +24,14 @@ import org.wso2.carbon.device.application.mgt.common.exception.DBConnectionExcep import org.wso2.carbon.device.application.mgt.common.exception.IllegalTransactionStateException; import org.wso2.carbon.device.application.mgt.common.exception.TransactionManagementException; -import javax.naming.InitialContext; -import javax.sql.DataSource; import java.sql.Connection; import java.sql.SQLException; +import javax.naming.InitialContext; +import javax.sql.DataSource; +/** + * ConnectionManagerUtil is responsible for handling all the datasource connections utilities. + */ public class ConnectionManagerUtil { private static final Log log = LogFactory.getLog(ConnectionManagerUtil.class); @@ -58,9 +61,7 @@ public class ConnectionManagerUtil { Connection conn = currentConnection.get(); if (conn == null) { conn = getDBConnection(); - } - - if (inTransaction(conn)) { + } else if (inTransaction(conn)) { throw new IllegalTransactionStateException("Transaction has already been started."); } @@ -244,7 +245,7 @@ public class ConnectionManagerUtil { @Deprecated public static void closeConnection() { - if(currentTxState != null) { + if (currentTxState != null) { TxState txState = currentTxState.get(); if (TxState.CONNECTION_NOT_BORROWED == txState) { @@ -259,9 +260,9 @@ public class ConnectionManagerUtil { Connection conn = currentConnection.get(); if (conn == null) { - throw new IllegalTransactionStateException("No connection is associated with the current transaction. " + - "This might have ideally been caused by not properly initiating the transaction via " + - "'beginTransaction'/'openConnection' methods"); + throw new IllegalTransactionStateException("No connection is associated with the current transaction. " + + "This might have ideally been caused by not properly initiating the transaction via " + + "'beginTransaction'/'openConnection' methods"); } try { conn.close(); @@ -298,4 +299,18 @@ public class ConnectionManagerUtil { return null; } + /** + * To check whether particular database that is used for application management supports batch query execution. + * + * @return true if batch query is supported, otherwise false. + */ + public static boolean isBatchQuerySupported() { + try { + return dataSource.getConnection().getMetaData().supportsBatchUpdates(); + } catch (SQLException e) { + log.error("Error occurred while checking whether database supports batch updates", e); + } + return false; + } + } diff --git a/features/application-mgt/org.wso2.carbon.device.application.mgt.server.feature/src/main/resources/dbscripts/cdm/application-mgt/h2.sql b/features/application-mgt/org.wso2.carbon.device.application.mgt.server.feature/src/main/resources/dbscripts/cdm/application-mgt/h2.sql index 6ff557d534..c882ce90b3 100644 --- a/features/application-mgt/org.wso2.carbon.device.application.mgt.server.feature/src/main/resources/dbscripts/cdm/application-mgt/h2.sql +++ b/features/application-mgt/org.wso2.carbon.device.application.mgt.server.feature/src/main/resources/dbscripts/cdm/application-mgt/h2.sql @@ -30,8 +30,8 @@ PRIMARY KEY (ID, PLATFORM_ID, PROP_NAME) CREATE TABLE IF NOT EXISTS APPM_PLATFORM_TENANT_MAPPING ( ID INT NOT NULL AUTO_INCREMENT, TENANT_ID INT NOT NULL , -PLATFORM_ID VARCHAR (100) NOT NULL, -FOREIGN KEY(PLATFORM_ID) REFERENCES APPM_PLATFORM(IDENTIFIER) ON DELETE CASCADE, +PLATFORM_ID INT NOT NULL, +FOREIGN KEY(PLATFORM_ID) REFERENCES APPM_PLATFORM(ID) ON DELETE CASCADE, PRIMARY KEY (ID, TENANT_ID, PLATFORM_ID) );