Adding Axis2Oberserver and improving the databse queries

4.x.x
megala21 7 years ago
parent 2ab43c89ac
commit 9bc069a957

@ -60,8 +60,8 @@ import javax.ws.rs.core.Response;
} }
), ),
tags = { tags = {
@Tag(name = "application_management", description = "Platform Management APIS related with " @Tag(name = "device_management, application_management", description = "Platform Management APIS "
+ "Application Management") + "related with Application Management")
} }
) )
@Scopes ( @Scopes (
@ -279,13 +279,10 @@ public interface PlatformManagementAPI {
value = { value = {
@ApiResponse( @ApiResponse(
code = 200, code = 200,
message = "OK. \n Successfully updated the platform"), message = "OK. \n Successfully deleted the platform"),
@ApiResponse(
code = 400,
message = "Bad Request. \n Invalid request parameters passed."),
@ApiResponse( @ApiResponse(
code = 500, 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 = ErrorResponse.class)
}) })
Response removePlatform( Response removePlatform(
@ -297,4 +294,50 @@ public interface PlatformManagementAPI {
String identifier 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
);
} }

@ -166,4 +166,24 @@ public class PlatformManagementAPIImpl implements PlatformManagementAPI {
return APIUtil.getResponse(e, Response.Status.INTERNAL_SERVER_ERROR); 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);
}
}
} }

@ -49,4 +49,9 @@ public interface PlatformManager {
void removeMapping(int tenantId, String platformIdentifier) throws PlatformManagementException; void removeMapping(int tenantId, String platformIdentifier) throws PlatformManagementException;
void updatePlatformStatus(int tenantId, String platformIdentifier, String status)
throws PlatformManagementException;
void removePlatforms(int tenantId) throws PlatformManagementException;
} }

@ -46,4 +46,6 @@ public interface PlatformDAO {
Platform getPlatform(int tenantId, String identifier) throws PlatformManagementDAOException; Platform getPlatform(int tenantId, String identifier) throws PlatformManagementDAOException;
void removePlatforms(int tenantId) throws PlatformManagementDAOException;
} }

@ -22,12 +22,10 @@ import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory; import org.apache.commons.logging.LogFactory;
import org.wso2.carbon.device.application.mgt.common.Platform; 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.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.PlatformDAO;
import org.wso2.carbon.device.application.mgt.core.dao.common.Util; 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.dao.impl.AbstractDAOImpl;
import org.wso2.carbon.device.application.mgt.core.exception.PlatformManagementDAOException; 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 org.wso2.carbon.utils.multitenancy.MultitenantConstants;
import java.sql.Connection; import java.sql.Connection;
@ -49,7 +47,7 @@ public class GenericPlatformDAOImpl extends AbstractDAOImpl implements PlatformD
try { try {
int platformId = getPlatformId(tenantId, platform.getIdentifier()); int platformId = getPlatformId(tenantId, platform.getIdentifier());
if (platformId == -1) { if (platformId == -1) {
Connection connection = this.getConnection(); Connection connection = this.getDBConnection();
if (!platform.isFileBased()) { if (!platform.isFileBased()) {
String insertToPlatform = "INSERT INTO APPM_PLATFORM (IDENTIFIER, TENANT_ID, NAME, FILE_BASED, " String insertToPlatform = "INSERT INTO APPM_PLATFORM (IDENTIFIER, TENANT_ID, NAME, FILE_BASED, "
+ "DESCRIPTION, IS_SHARED, ICON_NAME)" + " VALUES (?, ?, ?, ?, ?, ?, ?)"; + "DESCRIPTION, IS_SHARED, ICON_NAME)" + " VALUES (?, ?, ?, ?, ?, ?, ?)";
@ -121,7 +119,7 @@ public class GenericPlatformDAOImpl extends AbstractDAOImpl implements PlatformD
boolean isNameNull = platform.getName() == null; boolean isNameNull = platform.getName() == null;
if (platformId != -1) { if (platformId != -1) {
Connection connection = this.getConnection(); Connection connection = this.getDBConnection();
if (!platform.isFileBased()) { if (!platform.isFileBased()) {
String insertToPlatform = "UPDATE APPM_PLATFORM SET DESCRIPTION=?, IS_SHARED=?, ICON_NAME=?"; String insertToPlatform = "UPDATE APPM_PLATFORM SET DESCRIPTION=?, IS_SHARED=?, ICON_NAME=?";
if (!isIdentifierNull) { 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 " String query = "SELECT ID FROM APPM_PLATFORM WHERE (TENANT_ID=? AND IDENTIFIER=?) OR (IS_SHARED = TRUE AND "
+ "IDENTIFIER=?)"; + "IDENTIFIER=?)";
try { try {
Connection connection = this.getConnection(); Connection connection = this.getDBConnection();
preparedStatement = connection.prepareStatement(query); preparedStatement = connection.prepareStatement(query);
preparedStatement.setInt(1, tenantId); preparedStatement.setInt(1, tenantId);
preparedStatement.setString(2, platformIdentifier); preparedStatement.setString(2, platformIdentifier);
@ -229,7 +227,7 @@ public class GenericPlatformDAOImpl extends AbstractDAOImpl implements PlatformD
if (platform != null) { if (platform != null) {
if (isFileBased == platform.isFileBased()) { if (isFileBased == platform.isFileBased()) {
Connection connection = this.getConnection(); Connection connection = this.getDBConnection();
String deletePlatform = "DELETE FROM APPM_PLATFORM WHERE ID = ?"; String deletePlatform = "DELETE FROM APPM_PLATFORM WHERE ID = ?";
preparedStatement = connection.prepareStatement(deletePlatform); preparedStatement = connection.prepareStatement(deletePlatform);
preparedStatement.setInt(1, platform.getId()); preparedStatement.setInt(1, platform.getId());
@ -265,7 +263,7 @@ public class GenericPlatformDAOImpl extends AbstractDAOImpl implements PlatformD
for (String platformIdentifier : platformIdentifiers) { for (String platformIdentifier : platformIdentifiers) {
if (getTenantPlatformMapping(tenantId, platformIdentifier) == -1) { if (getTenantPlatformMapping(tenantId, platformIdentifier) == -1) {
int platformId = getPlatformId(tenantId, platformIdentifier); int platformId = getPlatformId(tenantId, platformIdentifier);
Connection connection = this.getConnection(); Connection connection = this.getDBConnection();
preparedStatement = connection.prepareStatement(insertMapping); preparedStatement = connection.prepareStatement(insertMapping);
preparedStatement.setInt(1, tenantId); preparedStatement.setInt(1, tenantId);
preparedStatement.setInt(2, platformId); 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=?) " + "WHERE TENANT_ID=?) MAPPING JOIN (SELECT ID FROM APPM_PLATFORM WHERE APPM_PLATFORM.IDENTIFIER=?) "
+ "PLATFORM ON MAPPING.PLATFORM_ID=PLATFORM.ID"; + "PLATFORM ON MAPPING.PLATFORM_ID=PLATFORM.ID";
try { try {
Connection connection = this.getConnection(); Connection connection = this.getDBConnection();
preparedStatement = connection.prepareStatement(getMapping); preparedStatement = connection.prepareStatement(getMapping);
preparedStatement.setInt(1, tenantId); preparedStatement.setInt(1, tenantId);
preparedStatement.setString(2, platformIdentifier); preparedStatement.setString(2, platformIdentifier);
@ -321,7 +319,7 @@ public class GenericPlatformDAOImpl extends AbstractDAOImpl implements PlatformD
try { try {
int mappingId = getTenantPlatformMapping(tenantId, platformIdentifier); int mappingId = getTenantPlatformMapping(tenantId, platformIdentifier);
if (mappingId != -1) { if (mappingId != -1) {
Connection connection = this.getConnection(); Connection connection = this.getDBConnection();
preparedStatement = connection.prepareStatement(deleteMapping); preparedStatement = connection.prepareStatement(deleteMapping);
preparedStatement.setInt(1, mappingId); preparedStatement.setInt(1, mappingId);
preparedStatement.execute(); preparedStatement.execute();
@ -346,7 +344,7 @@ public class GenericPlatformDAOImpl extends AbstractDAOImpl implements PlatformD
int platformId = getPlatformId(MultitenantConstants.SUPER_TENANT_ID, platformIdentifier); int platformId = getPlatformId(MultitenantConstants.SUPER_TENANT_ID, platformIdentifier);
String getMapping = "DELETE FROM APPM_PLATFORM_TENANT_MAPPING WHERE TENANT_ID != ? AND PLATFORM_ID=?"; String getMapping = "DELETE FROM APPM_PLATFORM_TENANT_MAPPING WHERE TENANT_ID != ? AND PLATFORM_ID=?";
try { try {
Connection connection = this.getConnection(); Connection connection = this.getDBConnection();
preparedStatement = connection.prepareStatement(getMapping); preparedStatement = connection.prepareStatement(getMapping);
preparedStatement.setInt(1, MultitenantConstants.SUPER_TENANT_ID); preparedStatement.setInt(1, MultitenantConstants.SUPER_TENANT_ID);
preparedStatement.setInt(2, platformId); 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 " + "IS_SHARED = TRUE AND FILE_BASED = FALSE) PLATFORM LEFT JOIN APPM_PLATFORM_TENANT_MAPPING "
+ "MAPPING ON PLATFORM.ID = MAPPING.PLATFORM_ID"; + "MAPPING ON PLATFORM.ID = MAPPING.PLATFORM_ID";
try { try {
Connection connection = this.getConnection(); Connection connection = this.getDBConnection();
preparedStatement = connection.prepareStatement(selectQuery); preparedStatement = connection.prepareStatement(selectQuery);
preparedStatement.setInt(1, tenantId); preparedStatement.setInt(1, tenantId);
resultSet = preparedStatement.executeQuery(); resultSet = preparedStatement.executeQuery();
@ -385,8 +383,8 @@ public class GenericPlatformDAOImpl extends AbstractDAOImpl implements PlatformD
log.debug("Platform retrieved for the tenant Id " + tenantId); log.debug("Platform retrieved for the tenant Id " + tenantId);
} }
while (resultSet.next()) { while (resultSet.next()) {
String identifier = resultSet.getString("PLATFORM.IDENTIFIER"); int mappingID = resultSet.getInt(1);
int mappingID = resultSet.getInt("MAPPING.ID"); String identifier = resultSet.getString(2);
Platform platform = getPlatform(tenantId, identifier); Platform platform = getPlatform(tenantId, identifier);
if (mappingID != 0) { if (mappingID != 0) {
platform.setEnabled(true); platform.setEnabled(true);
@ -399,8 +397,7 @@ public class GenericPlatformDAOImpl extends AbstractDAOImpl implements PlatformD
} }
} }
if (log.isDebugEnabled()) { if (log.isDebugEnabled()) {
log.debug("Number of platforms available for the tenant ID - " + tenantId + " :" + platforms log.debug("Number of platforms available for the tenant ID - " + tenantId + " :" + platforms.size());
.size());
} }
return platforms; return platforms;
} catch (DBConnectionException e) { } catch (DBConnectionException e) {
@ -420,7 +417,7 @@ public class GenericPlatformDAOImpl extends AbstractDAOImpl implements PlatformD
"OR (IS_SHARED = TRUE AND IDENTIFIER=?) AND FILE_BASED = FALSE ) PLATFORM " + "OR (IS_SHARED = TRUE AND IDENTIFIER=?) AND FILE_BASED = FALSE ) PLATFORM " +
"LEFT JOIN APPM_PLATFORM_PROPERTIES PROPS ON PLATFORM.ID = PROPS.PLATFORM_ID"; "LEFT JOIN APPM_PLATFORM_PROPERTIES PROPS ON PLATFORM.ID = PROPS.PLATFORM_ID";
try { try {
Connection connection = this.getConnection(); Connection connection = this.getDBConnection();
preparedStatement = connection.prepareStatement(platformQuery); preparedStatement = connection.prepareStatement(platformQuery);
preparedStatement.setString(1, tenantDomain); preparedStatement.setString(1, tenantDomain);
preparedStatement.setString(2, platformIdentifier); 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 { public Platform getPlatform(int tenantId, String identifier) throws PlatformManagementDAOException {
Connection conn; Connection conn;
PreparedStatement stmt = null; PreparedStatement stmt = null;
@ -467,8 +465,11 @@ public class GenericPlatformDAOImpl extends AbstractDAOImpl implements PlatformD
String sql = ""; String sql = "";
try { try {
conn = this.getConnection(); conn = this.getDBConnection();
sql = "SELECT * FROM APPM_PLATFORM WHERE IDENTIFIER = ? AND TENANT_ID = ?"; 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 = conn.prepareStatement(sql);
stmt.setString(1, identifier); stmt.setString(1, identifier);
@ -479,15 +480,21 @@ public class GenericPlatformDAOImpl extends AbstractDAOImpl implements PlatformD
if (rs.next()) { if (rs.next()) {
platform = new Platform(); 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()) { if (!platform.isFileBased()) {
platform.setId(rs.getInt("ID")); platform.setId(rs.getInt(4));
platform.setName(rs.getString("NAME")); platform.setName(rs.getString(5));
platform.setDescription(rs.getString("DESCRIPTION")); platform.setDescription(rs.getString(6));
platform.setIconName(rs.getString("ICON_NAME")); platform.setIconName(rs.getString(7));
platform.setShared(rs.getBoolean("IS_SHARED")); platform.setShared(rs.getBoolean(8));
if (rs.getInt(1) != 0) {
platform.setEnabled(true);
} else {
platform.setEnabled(false);
}
} }
} }
return platform; return platform;
@ -500,4 +507,25 @@ public class GenericPlatformDAOImpl extends AbstractDAOImpl implements PlatformD
Util.cleanupResources(stmt, rs); 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);
}
}
} }

@ -52,7 +52,7 @@ public class PlatformManagerImpl implements PlatformManager {
@Override @Override
public void initialize(int tenantId) throws PlatformManagementException { public void initialize(int tenantId) throws PlatformManagementException {
try { try {
ConnectionManagerUtil.beginTransaction(); ConnectionManagerUtil.beginDBTransaction();
List<Platform> platforms = DAOFactory.getPlatformDAO().getPlatforms(tenantId); List<Platform> platforms = DAOFactory.getPlatformDAO().getPlatforms(tenantId);
List<String> platformIdentifiers = new ArrayList<>(); List<String> platformIdentifiers = new ArrayList<>();
for (Platform platform : platforms) { for (Platform platform : platforms) {
@ -61,19 +61,22 @@ public class PlatformManagerImpl implements PlatformManager {
} }
} }
DAOFactory.getPlatformDAO().addMapping(tenantId, platformIdentifiers); DAOFactory.getPlatformDAO().addMapping(tenantId, platformIdentifiers);
ConnectionManagerUtil.commitTransaction(); ConnectionManagerUtil.commitDBTransaction();
} catch (TransactionManagementException e) { } catch (TransactionManagementException e) {
ConnectionManagerUtil.rollbackTransaction(); ConnectionManagerUtil.rollbackDBTransaction();
throw new PlatformManagementDAOException( throw new PlatformManagementDAOException(
"Transaction Management Exception while initializing the " + "platforms for the tenant : " "Transaction Management Exception while initializing the " + "platforms for the tenant : "
+ tenantId, e); + tenantId, e);
} catch (DBConnectionException e) { } catch (DBConnectionException e) {
ConnectionManagerUtil.rollbackTransaction(); ConnectionManagerUtil.rollbackDBTransaction();
throw new PlatformManagementDAOException( throw new PlatformManagementDAOException(
"Database Connection Exception while initializing the " + "platforms for the tenant : " + tenantId, "Database Connection Exception while initializing the " + "platforms for the tenant : " + tenantId,
e); e);
} catch (PlatformManagementDAOException e) {
ConnectionManagerUtil.rollbackDBTransaction();
throw e;
} finally { } finally {
ConnectionManagerUtil.closeConnection(); ConnectionManagerUtil.closeDBConnection();
} }
} }
@ -86,13 +89,18 @@ public class PlatformManagerImpl implements PlatformManager {
+ "PlatformManager level"); + "PlatformManager level");
} }
try { try {
ConnectionManagerUtil.openConnection(); ConnectionManagerUtil.beginDBTransaction();
platforms = DAOFactory.getPlatformDAO().getPlatforms(tenantId); platforms = DAOFactory.getPlatformDAO().getPlatforms(tenantId);
} catch (DBConnectionException e) { ConnectionManagerUtil.commitDBTransaction();
} catch (DBConnectionException | TransactionManagementException e) {
ConnectionManagerUtil.rollbackDBTransaction();
throw new PlatformManagementDAOException( throw new PlatformManagementDAOException(
"Database Connection Exception while getting the platforms for the tenant : " + tenantId, e); "Database Connection Exception while getting the platforms for the tenant : " + tenantId, e);
} catch (PlatformManagementDAOException e) {
ConnectionManagerUtil.rollbackDBTransaction();
throw e;
} finally { } finally {
ConnectionManagerUtil.closeConnection(); ConnectionManagerUtil.closeDBConnection();
} }
if (log.isDebugEnabled()) { if (log.isDebugEnabled()) {
log.debug("Number of platforms received from DAO layer is " + platforms.size() + " for the tenant " 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); Platform platform = getPlatformFromInMemory(tenantId, identifier);
if (platform == null) { if (platform == null) {
try { try {
ConnectionManagerUtil.openConnection(); ConnectionManagerUtil.beginDBTransaction();
platform = DAOFactory.getPlatformDAO().getPlatform(tenantId, identifier); platform = DAOFactory.getPlatformDAO().getPlatform(tenantId, identifier);
ConnectionManagerUtil.commitDBTransaction();
if (platform != null) { if (platform != null) {
return platform; return platform;
} }
} catch (DBConnectionException e) { } catch (DBConnectionException | TransactionManagementException e) {
ConnectionManagerUtil.rollbackDBTransaction();
throw new PlatformManagementDAOException( throw new PlatformManagementDAOException(
"Database Connection Exception while trying to get the " + "platform with the id :" + identifier "Database Connection Exception while trying to get the " + "platform with the id :" + identifier
+ " for the tenant : " + tenantId, e); + " for the tenant : " + tenantId, e);
} catch (PlatformManagementDAOException e) {
ConnectionManagerUtil.rollbackDBTransaction();
throw e;
} finally { } finally {
ConnectionManagerUtil.closeConnection(); ConnectionManagerUtil.closeDBConnection();
} }
} else { } else {
return new Platform(platform); return new Platform(platform);
@ -175,7 +188,7 @@ public class PlatformManagerImpl implements PlatformManager {
+ " cannot be shared by the tenant domain - " + tenantId); + " cannot be shared by the tenant domain - " + tenantId);
} }
try { try {
ConnectionManagerUtil.beginTransaction(); ConnectionManagerUtil.beginDBTransaction();
int platformId = DAOFactory.getPlatformDAO().register(tenantId, platform); int platformId = DAOFactory.getPlatformDAO().register(tenantId, platform);
if (platform.isFileBased()) { if (platform.isFileBased()) {
platform.setId(platformId); platform.setId(platformId);
@ -187,7 +200,7 @@ public class PlatformManagerImpl implements PlatformManager {
if (tenantPlatforms.get(platform.getIdentifier()) == null) { if (tenantPlatforms.get(platform.getIdentifier()) == null) {
tenantPlatforms.put(platform.getIdentifier(), platform); tenantPlatforms.put(platform.getIdentifier(), platform);
} else { } else {
ConnectionManagerUtil.rollbackTransaction(); ConnectionManagerUtil.rollbackDBTransaction();
throw new PlatformManagementException( throw new PlatformManagementException(
"Platform - " + platform.getIdentifier() + " is already registered!"); "Platform - " + platform.getIdentifier() + " is already registered!");
} }
@ -204,24 +217,27 @@ public class PlatformManagerImpl implements PlatformManager {
} }
DAOFactory.getPlatformDAO().addMapping(tenantId, getListOfString(platform.getIdentifier())); DAOFactory.getPlatformDAO().addMapping(tenantId, getListOfString(platform.getIdentifier()));
} catch (UserStoreException e) { } catch (UserStoreException e) {
ConnectionManagerUtil.rollbackTransaction(); ConnectionManagerUtil.rollbackDBTransaction();
throw new PlatformManagementException("Error occurred while assigning the platforms for tenants!", throw new PlatformManagementException("Error occurred while assigning the platforms for tenants!",
e); e);
} }
} }
ConnectionManagerUtil.commitTransaction(); ConnectionManagerUtil.commitDBTransaction();
} catch (TransactionManagementException e) { } catch (TransactionManagementException e) {
ConnectionManagerUtil.rollbackTransaction(); ConnectionManagerUtil.rollbackDBTransaction();
throw new PlatformManagementDAOException( throw new PlatformManagementDAOException(
"Transaction Management Exception while trying to register a " + "platform with id " + platform "Transaction Management Exception while trying to register a " + "platform with id " + platform
.getIdentifier() + " for tenant " + tenantId); .getIdentifier() + " for tenant " + tenantId);
} catch (DBConnectionException e) { } catch (DBConnectionException e) {
ConnectionManagerUtil.rollbackTransaction(); ConnectionManagerUtil.rollbackDBTransaction();
throw new PlatformManagementDAOException( throw new PlatformManagementDAOException(
"Database Connection Exception while trying to register a " + "platform with id " + platform "Database Connection Exception while trying to register a " + "platform with id " + platform
.getIdentifier() + " for tenant " + tenantId); .getIdentifier() + " for tenant " + tenantId);
} catch (PlatformManagementDAOException e) {
ConnectionManagerUtil.rollbackDBTransaction();
throw e;
} finally { } finally {
ConnectionManagerUtil.closeConnection(); ConnectionManagerUtil.closeDBConnection();
} }
} }
@ -234,11 +250,11 @@ public class PlatformManagerImpl implements PlatformManager {
+ " cannot be shared by the tenant domain - " + tenantId); + " cannot be shared by the tenant domain - " + tenantId);
} }
try { try {
ConnectionManagerUtil.beginTransaction(); ConnectionManagerUtil.beginDBTransaction();
Platform oldPlatform = DAOFactory.getPlatformDAO().getPlatform(tenantId, oldPlatformIdentifier); Platform oldPlatform = DAOFactory.getPlatformDAO().getPlatform(tenantId, oldPlatformIdentifier);
if (oldPlatform == null) { if (oldPlatform == null) {
ConnectionManagerUtil.rollbackTransaction(); ConnectionManagerUtil.commitDBTransaction();
throw new PlatformManagementException( throw new PlatformManagementException(
"Cannot update platform. Platform with identifier : " + oldPlatformIdentifier "Cannot update platform. Platform with identifier : " + oldPlatformIdentifier
+ " does not exist."); + " does not exist.");
@ -247,7 +263,7 @@ public class PlatformManagerImpl implements PlatformManager {
Platform existingPlatform = DAOFactory.getPlatformDAO().getPlatform(tenantId, platform.getIdentifier()); Platform existingPlatform = DAOFactory.getPlatformDAO().getPlatform(tenantId, platform.getIdentifier());
if (existingPlatform != null) { if (existingPlatform != null) {
ConnectionManagerUtil.rollbackTransaction(); ConnectionManagerUtil.commitDBTransaction();
throw new PlatformManagementException( throw new PlatformManagementException(
"Cannot update the identifier of the platform from '" + oldPlatformIdentifier + "' to '" "Cannot update the identifier of the platform from '" + oldPlatformIdentifier + "' to '"
+ platform.getIdentifier() + "'. Another platform exists " + platform.getIdentifier() + "'. Another platform exists "
@ -288,7 +304,7 @@ public class PlatformManagerImpl implements PlatformManager {
} }
DAOFactory.getPlatformDAO().addMapping(tenantId, getListOfString(platform.getIdentifier())); DAOFactory.getPlatformDAO().addMapping(tenantId, getListOfString(platform.getIdentifier()));
} catch (UserStoreException e) { } catch (UserStoreException e) {
ConnectionManagerUtil.rollbackTransaction(); ConnectionManagerUtil.rollbackDBTransaction();
throw new PlatformManagementException("Error occurred while assigning the platforms for tenants!", throw new PlatformManagementException("Error occurred while assigning the platforms for tenants!",
e); e);
} }
@ -296,19 +312,22 @@ public class PlatformManagerImpl implements PlatformManager {
if (!platform.isShared() && oldPlatform.isShared()) { if (!platform.isShared() && oldPlatform.isShared()) {
DAOFactory.getPlatformDAO().removeMappingTenants(platform.getIdentifier()); DAOFactory.getPlatformDAO().removeMappingTenants(platform.getIdentifier());
} }
ConnectionManagerUtil.commitTransaction(); ConnectionManagerUtil.commitDBTransaction();
} catch (TransactionManagementException e) { } catch (TransactionManagementException e) {
ConnectionManagerUtil.rollbackTransaction(); ConnectionManagerUtil.rollbackDBTransaction();
throw new PlatformManagementDAOException( throw new PlatformManagementDAOException(
"Transaction Management Exception while trying to update " + "platform : " + oldPlatformIdentifier "Transaction Management Exception while trying to update " + "platform : " + oldPlatformIdentifier
+ " of tenant :" + tenantId); + " of tenant :" + tenantId);
} catch (DBConnectionException e) { } catch (DBConnectionException e) {
ConnectionManagerUtil.rollbackTransaction(); ConnectionManagerUtil.rollbackDBTransaction();
throw new PlatformManagementDAOException( throw new PlatformManagementDAOException(
"Database Connection Exception while trying to update " + "platform : " + oldPlatformIdentifier "Database Connection Exception while trying to update " + "platform : " + oldPlatformIdentifier
+ " of tenant :" + tenantId); + " of tenant :" + tenantId);
} catch (PlatformManagementDAOException e) {
ConnectionManagerUtil.rollbackDBTransaction();
throw e;
} finally { } finally {
ConnectionManagerUtil.closeConnection(); ConnectionManagerUtil.closeDBConnection();
} }
} }
@ -321,7 +340,7 @@ public class PlatformManagerImpl implements PlatformManager {
@Override @Override
public void unregister(int tenantId, String identifier, boolean isFileBased) throws PlatformManagementException { public void unregister(int tenantId, String identifier, boolean isFileBased) throws PlatformManagementException {
try { try {
ConnectionManagerUtil.beginTransaction(); ConnectionManagerUtil.beginDBTransaction();
DAOFactory.getPlatformDAO().unregister(tenantId, identifier, isFileBased); DAOFactory.getPlatformDAO().unregister(tenantId, identifier, isFileBased);
if (isFileBased) { if (isFileBased) {
@ -330,33 +349,41 @@ public class PlatformManagerImpl implements PlatformManager {
tenantPlatforms.remove(identifier); tenantPlatforms.remove(identifier);
} }
} }
ConnectionManagerUtil.commitTransaction(); ConnectionManagerUtil.commitDBTransaction();
} catch (TransactionManagementException e) { } catch (TransactionManagementException e) {
ConnectionManagerUtil.rollbackTransaction(); ConnectionManagerUtil.rollbackDBTransaction();
throw new PlatformManagementDAOException( throw new PlatformManagementDAOException(
"Transaction Management Exception while trying to un-register " + "the platform with identifier : " "Transaction Management Exception while trying to un-register " + "the platform with identifier : "
+ identifier + " tenant :" + tenantId, e); + identifier + " tenant :" + tenantId, e);
} catch (DBConnectionException e) { } catch (DBConnectionException e) {
ConnectionManagerUtil.rollbackTransaction(); ConnectionManagerUtil.rollbackDBTransaction();
throw new PlatformManagementDAOException( throw new PlatformManagementDAOException(
"Database Connection Exception while trying to un-register " + "the platform with identifier : " "Database Connection Exception while trying to un-register " + "the platform with identifier : "
+ identifier + " tenant :" + tenantId, e); + identifier + " tenant :" + tenantId, e);
} catch (PlatformManagementDAOException e) {
ConnectionManagerUtil.rollbackDBTransaction();
throw e;
} finally { } finally {
ConnectionManagerUtil.closeConnection(); ConnectionManagerUtil.closeDBConnection();
} }
} }
@Override @Override
public void addMapping(int tenantId, List<String> platformIdentifiers) throws PlatformManagementException { public void addMapping(int tenantId, List<String> platformIdentifiers) throws PlatformManagementException {
try { try {
ConnectionManagerUtil.openConnection(); ConnectionManagerUtil.beginDBTransaction();
DAOFactory.getPlatformDAO().addMapping(tenantId, platformIdentifiers); DAOFactory.getPlatformDAO().addMapping(tenantId, platformIdentifiers);
} catch (DBConnectionException e) { ConnectionManagerUtil.commitDBTransaction();
} catch (DBConnectionException | TransactionManagementException e) {
ConnectionManagerUtil.rollbackDBTransaction();
throw new PlatformManagementDAOException( throw new PlatformManagementDAOException(
"Database Connection Exception while trying to add tenant " + "mapping for tenant ID : " "Database Connection Exception while trying to add tenant " + "mapping for tenant ID : "
+ tenantId); + tenantId);
} catch (PlatformManagementDAOException e) {
ConnectionManagerUtil.rollbackDBTransaction();
throw e;
} finally { } finally {
ConnectionManagerUtil.closeConnection(); ConnectionManagerUtil.closeDBConnection();
} }
} }
@ -370,13 +397,86 @@ public class PlatformManagerImpl implements PlatformManager {
@Override @Override
public void removeMapping(int tenantId, String platformIdentifier) throws PlatformManagementException { public void removeMapping(int tenantId, String platformIdentifier) throws PlatformManagementException {
try { try {
ConnectionManagerUtil.openConnection(); ConnectionManagerUtil.beginDBTransaction();
DAOFactory.getPlatformDAO().removeMapping(tenantId, platformIdentifier); DAOFactory.getPlatformDAO().removeMapping(tenantId, platformIdentifier);
} catch (DBConnectionException e) { ConnectionManagerUtil.commitDBTransaction();
} catch (DBConnectionException | TransactionManagementException e) {
ConnectionManagerUtil.rollbackDBTransaction();
throw new PlatformManagementDAOException( throw new PlatformManagementDAOException(
"Database Connection Exception while trying to remove tenant mapping for tenant ID : " + tenantId); "Database Connection Exception while trying to remove tenant mapping for tenant ID : " + tenantId);
} catch (PlatformManagementDAOException e) {
ConnectionManagerUtil.rollbackDBTransaction();
throw e;
} finally { } 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();
}
}
} }

@ -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);
}
}
}

@ -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.device.mgt.core.service.DeviceManagementProviderService;
import org.wso2.carbon.ndatasource.core.DataSourceService; import org.wso2.carbon.ndatasource.core.DataSourceService;
import org.wso2.carbon.user.core.service.RealmService; import org.wso2.carbon.user.core.service.RealmService;
import org.wso2.carbon.utils.Axis2ConfigurationContextObserver;
import javax.naming.NamingException; import javax.naming.NamingException;
@ -117,6 +118,9 @@ public class ServiceComponent {
DataHolder.getInstance().setApplicationUploadManager(uploadManager); DataHolder.getInstance().setApplicationUploadManager(uploadManager);
bundleContext.registerService(ApplicationUploadManager.class.getName(), uploadManager, null); bundleContext.registerService(ApplicationUploadManager.class.getName(), uploadManager, null);
bundleContext.registerService(Axis2ConfigurationContextObserver.class.getName(),
new PlatformManagementAxis2ConfigurationObserverImpl(), null);
DAOFactory.init(datasourceName); DAOFactory.init(datasourceName);
DAOFactory.initDatabases(); DAOFactory.initDatabases();
log.info("ApplicationManagement core bundle has been successfully initialized"); log.info("ApplicationManagement core bundle has been successfully initialized");
@ -154,12 +158,12 @@ public class ServiceComponent {
} }
protected void setDataSourceService(DataSourceService dataSourceService) { protected void setDataSourceService(DataSourceService dataSourceService) {
//Not implemented. Not needed but to make sure the datasource service are registered, as it is needed create /*Not implemented. Not needed but to make sure the datasource service are registered, as it is needed create
// databases. databases. */
} }
protected void unsetDataSourceService(DataSourceService dataSourceService) { protected void unsetDataSourceService(DataSourceService dataSourceService) {
//Not implemented. Not needed but to make sure the datasource service are registered, as it is needed to create /*Not implemented. Not needed but to make sure the datasource service are registered, as it is needed to create
// databases. databases.*/
} }
} }

@ -19,13 +19,11 @@
package org.wso2.carbon.device.application.mgt.core.util; package org.wso2.carbon.device.application.mgt.core.util;
import java.io.File; 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.CarbonUtils;
import org.wso2.carbon.utils.dbcreator.DatabaseCreator; 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. * ApplicationMgtDatabaseCreator is responsible for creating the Application Management related tables.
*/ */

@ -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.IllegalTransactionStateException;
import org.wso2.carbon.device.application.mgt.common.exception.TransactionManagementException; 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.Connection;
import java.sql.SQLException; 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 { public class ConnectionManagerUtil {
private static final Log log = LogFactory.getLog(ConnectionManagerUtil.class); private static final Log log = LogFactory.getLog(ConnectionManagerUtil.class);
@ -58,9 +61,7 @@ public class ConnectionManagerUtil {
Connection conn = currentConnection.get(); Connection conn = currentConnection.get();
if (conn == null) { if (conn == null) {
conn = getDBConnection(); conn = getDBConnection();
} } else if (inTransaction(conn)) {
if (inTransaction(conn)) {
throw new IllegalTransactionStateException("Transaction has already been started."); throw new IllegalTransactionStateException("Transaction has already been started.");
} }
@ -259,9 +260,9 @@ public class ConnectionManagerUtil {
Connection conn = currentConnection.get(); Connection conn = currentConnection.get();
if (conn == null) { if (conn == null) {
throw new IllegalTransactionStateException("No connection is associated with the current transaction. " + throw new IllegalTransactionStateException("No connection is associated with the current transaction. "
"This might have ideally been caused by not properly initiating the transaction via " + + "This might have ideally been caused by not properly initiating the transaction via "
"'beginTransaction'/'openConnection' methods"); + "'beginTransaction'/'openConnection' methods");
} }
try { try {
conn.close(); conn.close();
@ -298,4 +299,18 @@ public class ConnectionManagerUtil {
return null; 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;
}
} }

@ -30,8 +30,8 @@ PRIMARY KEY (ID, PLATFORM_ID, PROP_NAME)
CREATE TABLE IF NOT EXISTS APPM_PLATFORM_TENANT_MAPPING ( CREATE TABLE IF NOT EXISTS APPM_PLATFORM_TENANT_MAPPING (
ID INT NOT NULL AUTO_INCREMENT, ID INT NOT NULL AUTO_INCREMENT,
TENANT_ID INT NOT NULL , TENANT_ID INT NOT NULL ,
PLATFORM_ID VARCHAR (100) NOT NULL, PLATFORM_ID INT NOT NULL,
FOREIGN KEY(PLATFORM_ID) REFERENCES APPM_PLATFORM(IDENTIFIER) ON DELETE CASCADE, FOREIGN KEY(PLATFORM_ID) REFERENCES APPM_PLATFORM(ID) ON DELETE CASCADE,
PRIMARY KEY (ID, TENANT_ID, PLATFORM_ID) PRIMARY KEY (ID, TENANT_ID, PLATFORM_ID)
); );

Loading…
Cancel
Save