From b1fc0fcfdbdf806ad8845fd0afa72540b6bb1563 Mon Sep 17 00:00:00 2001 From: megala21 Date: Sun, 30 Jul 2017 15:06:31 +0530 Subject: [PATCH 01/10] Fixing minor issues related with platform --- .../impl/PlatformManagementAPIImpl.java | 23 +-- .../src/main/webapp/WEB-INF/cxf-servlet.xml | 3 +- .../src/main/webapp/WEB-INF/web.xml | 2 +- .../mgt/common/services/PlatformManager.java | 18 +- .../pom.xml | 3 +- .../mgt/core/config/ConfigurationManager.java | 5 - .../application/mgt/core/dao/PlatformDAO.java | 17 +- .../mgt/core/dao/common/DAOFactory.java | 98 +++------- .../impl/platform/GenericPlatformDAOImpl.java | 168 ++++++++++-------- .../mgt/core/deployer/Platform.java | 5 +- .../mgt/core/deployer/PlatformDeployer.java | 29 +-- .../PlatformManagementDAOException.java | 3 + .../mgt/core/impl/PlatformManagerImpl.java | 114 ++++++------ .../mgt/core/internal/DataHolder.java | 10 -- .../mgt/core/internal/ServiceComponent.java | 12 +- .../util/ApplicationMgtDatabaseCreator.java | 4 +- .../mgt/core/util/ConnectionManagerUtil.java | 10 +- components/application-mgt/pom.xml | 6 + .../dbscripts/cdm/application-mgt/h2.sql | 17 +- .../dbscripts/cdm/application-mgt/mysql.sql | 4 +- pom.xml | 30 ---- 21 files changed, 280 insertions(+), 301 deletions(-) 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 fe8ec589ea..b708b388e2 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 @@ -37,6 +37,9 @@ import javax.ws.rs.PathParam; import javax.ws.rs.QueryParam; import javax.ws.rs.core.Response; +/** + * Implementation of PlatformManagement APIs. + */ @Path("/platforms") public class PlatformManagementAPIImpl implements PlatformManagementAPI { @@ -49,13 +52,13 @@ public class PlatformManagementAPIImpl implements PlatformManagementAPI { @GET @Override public Response getPlatforms(@QueryParam("status") String status) { - String tenantDomain = PrivilegedCarbonContext.getThreadLocalCarbonContext().getTenantDomain(true); + int tenantID = PrivilegedCarbonContext.getThreadLocalCarbonContext().getTenantId(true); if (log.isDebugEnabled()) { log.debug("API request received for getting the platforms with the status " + status); } try { - List platforms = APIUtil.getPlatformManager().getPlatforms(tenantDomain); + List platforms = APIUtil.getPlatformManager().getPlatforms(tenantID); List results; if (status != null) { if (status.contentEquals(ALL_STATUS)) { @@ -85,7 +88,7 @@ public class PlatformManagementAPIImpl implements PlatformManagementAPI { } return Response.status(Response.Status.OK).entity(results).build(); } catch (PlatformManagementException e) { - log.error("Error while getting the platforms for tenant - " + tenantDomain, e); + log.error("Error while getting the platforms for tenant - " + tenantID, e); return APIUtil.getResponse(e, Response.Status.INTERNAL_SERVER_ERROR); } } @@ -94,9 +97,9 @@ public class PlatformManagementAPIImpl implements PlatformManagementAPI { @Override @Path("/{identifier}") public Response getPlatform(@PathParam("identifier") String id) { - String tenantDomain = PrivilegedCarbonContext.getThreadLocalCarbonContext().getTenantDomain(true); + int tenantId = PrivilegedCarbonContext.getThreadLocalCarbonContext().getTenantId(true); try { - Platform platform = APIUtil.getPlatformManager().getPlatform(tenantDomain, id); + Platform platform = APIUtil.getPlatformManager().getPlatform(tenantId, id); return Response.status(Response.Status.OK).entity(platform).build(); } catch (PlatformManagementDAOException e) { return APIUtil.getResponse(e, Response.Status.INTERNAL_SERVER_ERROR); @@ -108,11 +111,11 @@ public class PlatformManagementAPIImpl implements PlatformManagementAPI { @POST @Override public Response addPlatform(Platform platform) { - String tenantDomain = PrivilegedCarbonContext.getThreadLocalCarbonContext().getTenantDomain(true); + int tenantId = PrivilegedCarbonContext.getThreadLocalCarbonContext().getTenantId(true); try { if (platform != null) { if (platform.validate()) { - APIUtil.getPlatformManager().register(tenantDomain, platform); + APIUtil.getPlatformManager().register(tenantId, platform); return Response.status(Response.Status.CREATED).build(); } else { return APIUtil.getResponse("Invxalid payload! Platform ID and names are mandatory fields!", @@ -131,12 +134,12 @@ public class PlatformManagementAPIImpl implements PlatformManagementAPI { @Path("/{identifier}") @Override public Response updatePlatform(Platform platform, @PathParam("identifier") @Size(max = 45) String id) { - String tenantDomain = PrivilegedCarbonContext.getThreadLocalCarbonContext().getTenantDomain(true); + int tenantId = PrivilegedCarbonContext.getThreadLocalCarbonContext().getTenantId(true); try { - APIUtil.getPlatformManager().update(tenantDomain, id, platform); + APIUtil.getPlatformManager().update(tenantId, id, platform); return Response.status(Response.Status.OK).build(); } catch (PlatformManagementException e) { - log.error("Error while updating the platform - " + id + " for tenant domain - " + tenantDomain, e); + log.error("Error while updating the platform - " + id + " for tenant domain - " + tenantId, e); return APIUtil.getResponse(e, Response.Status.INTERNAL_SERVER_ERROR); } } diff --git a/components/application-mgt/org.wso2.carbon.device.application.mgt.api/src/main/webapp/WEB-INF/cxf-servlet.xml b/components/application-mgt/org.wso2.carbon.device.application.mgt.api/src/main/webapp/WEB-INF/cxf-servlet.xml index 8d238a363d..1347461c54 100644 --- a/components/application-mgt/org.wso2.carbon.device.application.mgt.api/src/main/webapp/WEB-INF/cxf-servlet.xml +++ b/components/application-mgt/org.wso2.carbon.device.application.mgt.api/src/main/webapp/WEB-INF/cxf-servlet.xml @@ -26,6 +26,7 @@ http://cxf.apache.org/jaxrs http://cxf.apache.org/schemas/jaxrs.xsd"> + @@ -33,7 +34,7 @@ http://cxf.apache.org/jaxrs http://cxf.apache.org/schemas/jaxrs.xsd"> - + diff --git a/components/application-mgt/org.wso2.carbon.device.application.mgt.api/src/main/webapp/WEB-INF/web.xml b/components/application-mgt/org.wso2.carbon.device.application.mgt.api/src/main/webapp/WEB-INF/web.xml index db708d84b9..55fe863d2e 100644 --- a/components/application-mgt/org.wso2.carbon.device.application.mgt.api/src/main/webapp/WEB-INF/web.xml +++ b/components/application-mgt/org.wso2.carbon.device.application.mgt.api/src/main/webapp/WEB-INF/web.xml @@ -37,7 +37,7 @@ doAuthentication - false + true 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 ca2fc379a9..d1edbef02f 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 @@ -29,24 +29,24 @@ import java.util.List; */ public interface PlatformManager { - void initialize(String tenantDomain) throws PlatformManagementException; + void initialize(int tenantId) throws PlatformManagementException; - List getPlatforms(String tenantDomain) throws PlatformManagementException; + List getPlatforms(int tenantId) throws PlatformManagementException; - Platform getPlatform(String tenantDomain, String platformIdentifier) throws PlatformManagementException; + Platform getPlatform(int tenantId, String platformIdentifier) throws PlatformManagementException; - void register(String tenantDomain, Platform platform) throws PlatformManagementException; + void register(int tenantId, Platform platform) throws PlatformManagementException; - void update(String tenantDomain, String oldPlatformIdentifier, Platform platform) + void update(int tenantId, String oldPlatformIdentifier, Platform platform) throws PlatformManagementException; - void unregister(String tenantDomain, String platformIdentifier, boolean isFileBased) + void unregister(int tenantId, String platformIdentifier, boolean isFileBased) throws PlatformManagementException; - void addMapping(String tenantDomain, List platformIdentifiers) throws PlatformManagementException; + void addMapping(int tenantId, List platformIdentifiers) throws PlatformManagementException; - void addMapping(String tenantDomain, String platformIdentifier) throws PlatformManagementException; + void addMapping(int tenantId, String platformIdentifier) throws PlatformManagementException; - void removeMapping(String tenantDomain, String platformIdentifier) throws PlatformManagementException; + void removeMapping(int tenantId, String platformIdentifier) throws PlatformManagementException; } diff --git a/components/application-mgt/org.wso2.carbon.device.application.mgt.core/pom.xml b/components/application-mgt/org.wso2.carbon.device.application.mgt.core/pom.xml index 7687d18861..a971d147e6 100644 --- a/components/application-mgt/org.wso2.carbon.device.application.mgt.core/pom.xml +++ b/components/application-mgt/org.wso2.carbon.device.application.mgt.core/pom.xml @@ -74,8 +74,7 @@ org.apache.axis2.*, org.wso2.carbon.user.core.*, org.wso2.carbon.user.api.*, - org.wso2.carbon.ndatasource.core, - org.apache.axiom.om.*; version="${axiom.osgi.version.range}" + org.wso2.carbon.ndatasource.core !org.wso2.carbon.device.application.mgt.core.internal.*, diff --git a/components/application-mgt/org.wso2.carbon.device.application.mgt.core/src/main/java/org/wso2/carbon/device/application/mgt/core/config/ConfigurationManager.java b/components/application-mgt/org.wso2.carbon.device.application.mgt.core/src/main/java/org/wso2/carbon/device/application/mgt/core/config/ConfigurationManager.java index cf959cb91d..8ef40bdff0 100644 --- a/components/application-mgt/org.wso2.carbon.device.application.mgt.core/src/main/java/org/wso2/carbon/device/application/mgt/core/config/ConfigurationManager.java +++ b/components/application-mgt/org.wso2.carbon.device.application.mgt.core/src/main/java/org/wso2/carbon/device/application/mgt/core/config/ConfigurationManager.java @@ -22,16 +22,11 @@ import org.apache.commons.logging.Log; import org.apache.commons.logging.LogFactory; import org.wso2.carbon.device.application.mgt.common.exception.ApplicationManagementException; import org.wso2.carbon.device.application.mgt.common.exception.InvalidConfigurationException; -import org.wso2.carbon.device.application.mgt.core.deployer.Platform; -import org.wso2.carbon.device.application.mgt.core.deployer.Property; -import org.wso2.carbon.device.application.mgt.core.internal.DataHolder; import org.wso2.carbon.device.application.mgt.core.util.Constants; import javax.xml.bind.JAXBContext; import javax.xml.bind.Unmarshaller; import java.io.File; -import java.util.ArrayList; -import java.util.List; public class ConfigurationManager { 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 8aa524f138..55e95ed17e 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 @@ -19,30 +19,31 @@ package org.wso2.carbon.device.application.mgt.core.dao; import org.wso2.carbon.device.application.mgt.common.Platform; -import org.wso2.carbon.device.application.mgt.core.exception.ApplicationManagementDAOException; import org.wso2.carbon.device.application.mgt.core.exception.PlatformManagementDAOException; import java.util.List; +/** + * PlatformDAO defines set of DAO operations that are needed for Platform Management. + */ public interface PlatformDAO { - int register(String tenantDomain, Platform platform) throws PlatformManagementDAOException; + int register(int tenantId, Platform platform) throws PlatformManagementDAOException; - void update(String tenantDomain, String oldPlatformIdentifier, Platform platform) throws PlatformManagementDAOException; + void update(int tenantId, String oldPlatformIdentifier, Platform platform) throws PlatformManagementDAOException; - void unregister(String tenantDomain, String platformIdentifier) throws PlatformManagementDAOException; + void unregister(int tenantId, String platformIdentifier) throws PlatformManagementDAOException; - void addMapping(String tenantDomain, List platformIdentifiers) throws PlatformManagementDAOException; + void addMapping(int tenantId, List platformIdentifiers) throws PlatformManagementDAOException; - void removeMapping(String tenantDomain, String platformIdentifier) throws PlatformManagementDAOException; + void removeMapping(int tenantId, String platformIdentifier) throws PlatformManagementDAOException; void removeMappingTenants(String platformIdentifier) throws PlatformManagementDAOException; - List getPlatforms(String tenantDomain) throws PlatformManagementDAOException; + List getPlatforms(int tenantId) throws PlatformManagementDAOException; Platform getPlatform(String tenantDomain, String platformIdentifier) throws PlatformManagementDAOException; Platform getPlatform(int tenantId, String identifier) 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/common/DAOFactory.java b/components/application-mgt/org.wso2.carbon.device.application.mgt.core/src/main/java/org/wso2/carbon/device/application/mgt/core/dao/common/DAOFactory.java index 6a63405460..027067a186 100644 --- a/components/application-mgt/org.wso2.carbon.device.application.mgt.core/src/main/java/org/wso2/carbon/device/application/mgt/core/dao/common/DAOFactory.java +++ b/components/application-mgt/org.wso2.carbon.device.application.mgt.core/src/main/java/org/wso2/carbon/device/application/mgt/core/dao/common/DAOFactory.java @@ -18,11 +18,8 @@ */ package org.wso2.carbon.device.application.mgt.core.dao.common; -import org.apache.axiom.om.OMElement; -import org.apache.axiom.om.util.AXIOMUtil; 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.UnsupportedDatabaseEngineException; import org.wso2.carbon.device.application.mgt.core.config.ConfigurationManager; import org.wso2.carbon.device.application.mgt.core.dao.ApplicationDAO; @@ -33,18 +30,11 @@ import org.wso2.carbon.device.application.mgt.core.dao.impl.application.MySQLApp import org.wso2.carbon.device.application.mgt.core.dao.impl.lifecyclestate.GenericLifecycleStateImpl; import org.wso2.carbon.device.application.mgt.core.dao.impl.platform.GenericPlatformDAOImpl; import org.wso2.carbon.device.application.mgt.core.exception.ApplicationManagementDAOException; -import org.wso2.carbon.device.application.mgt.core.internal.DataHolder; import org.wso2.carbon.device.application.mgt.core.util.ApplicationMgtDatabaseCreator; -import org.wso2.carbon.device.application.mgt.core.util.Constants; import org.wso2.carbon.device.application.mgt.core.util.ConnectionManagerUtil; -import org.wso2.carbon.ndatasource.core.CarbonDataSource; -import org.wso2.carbon.ndatasource.core.DataSourceService; +import org.wso2.carbon.device.application.mgt.core.util.Constants; import org.wso2.carbon.utils.dbcreator.DatabaseCreator; -import org.wso2.carbon.utils.multitenancy.MultitenantConstants; -import javax.sql.DataSource; -import javax.xml.namespace.QName; -import javax.xml.stream.XMLStreamException; import java.sql.SQLException; /** @@ -63,41 +53,41 @@ public class DAOFactory { databaseEngine = ConnectionManagerUtil.getDatabaseType(); } - public static ApplicationDAO getApplicationDAO(){ + public static ApplicationDAO getApplicationDAO() { if (databaseEngine != null) { switch (databaseEngine) { - case Constants.DataBaseTypes.DB_TYPE_H2: - return new H2ApplicationDAOImpl(); - case Constants.DataBaseTypes.DB_TYPE_MYSQL: - return new MySQLApplicationDAOImpl(); - default: - throw new UnsupportedDatabaseEngineException("Unsupported database engine : " + databaseEngine); + case Constants.DataBaseTypes.DB_TYPE_H2: + return new H2ApplicationDAOImpl(); + case Constants.DataBaseTypes.DB_TYPE_MYSQL: + return new MySQLApplicationDAOImpl(); + default: + throw new UnsupportedDatabaseEngineException("Unsupported database engine : " + databaseEngine); } } throw new IllegalStateException("Database engine has not initialized properly."); } - public static PlatformDAO getPlatformDAO(){ + public static PlatformDAO getPlatformDAO() { if (databaseEngine != null) { switch (databaseEngine) { - case Constants.DataBaseTypes.DB_TYPE_H2: - case Constants.DataBaseTypes.DB_TYPE_MYSQL: - return new GenericPlatformDAOImpl(); - default: - throw new UnsupportedDatabaseEngineException("Unsupported database engine : " + databaseEngine); + case Constants.DataBaseTypes.DB_TYPE_H2: + case Constants.DataBaseTypes.DB_TYPE_MYSQL: + return new GenericPlatformDAOImpl(); + default: + throw new UnsupportedDatabaseEngineException("Unsupported database engine : " + databaseEngine); } } throw new IllegalStateException("Database engine has not initialized properly."); } - public static LifecycleStateDAO getLifecycleStateDAO(){ + public static LifecycleStateDAO getLifecycleStateDAO() { if (databaseEngine != null) { switch (databaseEngine) { - case Constants.DataBaseTypes.DB_TYPE_H2: - case Constants.DataBaseTypes.DB_TYPE_MYSQL: - return new GenericLifecycleStateImpl(); - default: - throw new UnsupportedDatabaseEngineException("Unsupported database engine : " + databaseEngine); + case Constants.DataBaseTypes.DB_TYPE_H2: + case Constants.DataBaseTypes.DB_TYPE_MYSQL: + return new GenericLifecycleStateImpl(); + default: + throw new UnsupportedDatabaseEngineException("Unsupported database engine : " + databaseEngine); } } throw new IllegalStateException("Database engine has not initialized properly."); @@ -105,29 +95,20 @@ public class DAOFactory { /** * This method initializes the databases by creating the database. + * * @throws ApplicationManagementDAOException Exceptions thrown during the creation of the tables */ public static void initDatabases() throws ApplicationManagementDAOException { - CarbonDataSource carbonDataSource = null; - DataSource dataSource = null; - String dataSourceName = ConfigurationManager.getInstance() - .getConfiguration().getDatasourceName(); - DataSourceService service = DataHolder.getInstance().getDataSourceService(); - PrivilegedCarbonContext.startTenantFlow(); - PrivilegedCarbonContext.getThreadLocalCarbonContext() - .setTenantDomain(MultitenantConstants.SUPER_TENANT_DOMAIN_NAME, true); + String dataSourceName = ConfigurationManager.getInstance().getConfiguration().getDatasourceName(); + String validationQuery = "SELECT * from APPM_PLATFORM"; try { - carbonDataSource = service.getDataSource(dataSourceName); - dataSource = (DataSource) carbonDataSource.getDSObject(); if (System.getProperty("setup") == null) { if (log.isDebugEnabled()) { log.debug("Application Management Database schema initialization check was skipped since " + "\'setup\' variable was not given during startup"); } } else { - DatabaseCreator databaseCreator = new ApplicationMgtDatabaseCreator(dataSource); - String validationQuery = getValidationQuery( - (String) carbonDataSource.getDSMInfo().getDefinition().getDsXMLConfiguration()); + DatabaseCreator databaseCreator = new ApplicationMgtDatabaseCreator(dataSourceName); if (!databaseCreator.isDatabaseStructureCreated(validationQuery)) { databaseCreator.createRegistryDatabase(); if (log.isDebugEnabled()) { @@ -136,34 +117,11 @@ public class DAOFactory { } } } catch (SQLException e) { - throw new ApplicationManagementDAOException("Error while creating application-mgt database during the " - + "startup ", e); + throw new ApplicationManagementDAOException( + "Error while creating application-mgt database during the " + "startup ", e); } catch (Exception e) { - throw new ApplicationManagementDAOException("Error while creating application-mgt database in the " - + "startup ", e); + throw new ApplicationManagementDAOException( + "Error while creating application-mgt database in the " + "startup ", e); } } - - /** - * To get the the validation query to make sure whether the tables exist already in application management databse - * @param dsXMLConfiguration Datasource XML configurations - * @return Validation query - */ - private static String getValidationQuery(String dsXMLConfiguration) { - String DEFAULT_VALIDATION_QUERY = "SELECT 1"; - try { - OMElement omElement = AXIOMUtil.stringToOM(dsXMLConfiguration); - return omElement.getFirstChildWithName(new QName("validationQuery")).getText(); - } catch (XMLStreamException e) { - log.error("Error while reading the validation query from the data source configuration of " - + "application-mgt (application-mgt-datasources.xml", e); - if (log.isDebugEnabled()) { - log.debug("Due to fail to read the validation query from application-mgt datasources, using the " - + "default validation query : " + DEFAULT_VALIDATION_QUERY); - } - return DEFAULT_VALIDATION_QUERY; - } - - } } - 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 2aebb9d32c..ae51b716d2 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 @@ -36,22 +36,25 @@ import java.sql.SQLException; import java.util.ArrayList; import java.util.List; +/** + * Generic Implementation for handling Platform management related database operations. + */ public class GenericPlatformDAOImpl extends AbstractDAOImpl implements PlatformDAO { private static Log log = LogFactory.getLog(GenericPlatformDAOImpl.class); @Override - public int register(String tenantDomain, Platform platform) throws PlatformManagementDAOException { + public int register(int tenantId, Platform platform) throws PlatformManagementDAOException { try { ConnectionManagerUtil.beginTransaction(); - int platformId = getPlatformId(tenantDomain, platform.getIdentifier()); + int platformId = getPlatformId(tenantId, platform.getIdentifier()); if (platformId == -1) { Connection connection = ConnectionManagerUtil.getConnection(); if (!platform.isFileBased()) { - String insertToPlatform = "INSERT INTO APPM_PLATFORM (IDENTIFIER, TENANT_DOMAIN, NAME, FILE_BASED, DESCRIPTION, IS_SHARED, ICON_NAME)" + - " VALUES (?, ?, ?, ?, ?, ?, ?)"; + String insertToPlatform = "INSERT INTO APPM_PLATFORM (IDENTIFIER, TENANT_ID, NAME, FILE_BASED, " + + "DESCRIPTION, IS_SHARED, ICON_NAME)" + " VALUES (?, ?, ?, ?, ?, ?, ?)"; PreparedStatement preparedStatement = connection.prepareStatement(insertToPlatform); preparedStatement.setString(1, platform.getIdentifier()); - preparedStatement.setString(2, tenantDomain); + preparedStatement.setInt(2, tenantId); preparedStatement.setString(3, platform.getName()); preparedStatement.setBoolean(4, false); preparedStatement.setString(5, platform.getDescription()); @@ -59,9 +62,10 @@ public class GenericPlatformDAOImpl extends AbstractDAOImpl implements PlatformD preparedStatement.setString(7, platform.getIconName()); preparedStatement.execute(); - platformId = getPlatformId(tenantDomain, platform.getIdentifier()); - String insertPlatformProps = "INSERT INTO APPM_PLATFORM_PROPERTIES (PLATFORM_ID, PROP_NAME, OPTIONAL, DEFAULT_VALUE) VALUES " + - "( ? , ?, ? , ?)"; + platformId = getPlatformId(tenantId, platform.getIdentifier()); + String insertPlatformProps = + "INSERT INTO APPM_PLATFORM_PROPERTIES (PLATFORM_ID, PROP_NAME, OPTIONAL, " + + "DEFAULT_VALUE) VALUES ( ? , ?, ? , ?)"; for (Platform.Property property : platform.getProperties()) { preparedStatement = connection.prepareStatement(insertPlatformProps); preparedStatement.setInt(1, platformId); @@ -71,16 +75,16 @@ public class GenericPlatformDAOImpl extends AbstractDAOImpl implements PlatformD preparedStatement.execute(); } } else { - String insertToPlatform = "INSERT INTO APPM_PLATFORM (IDENTIFIER, TENANT_DOMAIN, FILE_BASED)" + - " VALUES (?, ?, ?)"; + String insertToPlatform = + "INSERT INTO APPM_PLATFORM (IDENTIFIER, TENANT_ID, FILE_BASED)" + " VALUES (?, ?, ?)"; PreparedStatement preparedStatement = connection.prepareStatement(insertToPlatform); preparedStatement.setString(1, platform.getIdentifier()); - preparedStatement.setString(2, tenantDomain); + preparedStatement.setInt(2, tenantId); preparedStatement.setBoolean(3, true); preparedStatement.execute(); } if (platformId == -1) { - platformId = getPlatformId(tenantDomain, platform.getIdentifier()); + platformId = getPlatformId(tenantId, platform.getIdentifier()); } ConnectionManagerUtil.commitTransaction(); return platformId; @@ -88,7 +92,7 @@ public class GenericPlatformDAOImpl extends AbstractDAOImpl implements PlatformD if (!platform.isFileBased()) { ConnectionManagerUtil.rollbackTransaction(); throw new PlatformManagementDAOException("Platform - " + platform.getIdentifier() - + " is already registered for tenant - " + tenantDomain); + + " is already registered for tenant - " + tenantId); } else { return platformId; } @@ -102,26 +106,27 @@ public class GenericPlatformDAOImpl extends AbstractDAOImpl implements PlatformD } catch (DBConnectionException e) { ConnectionManagerUtil.rollbackTransaction(); throw new PlatformManagementDAOException("Unable to obtain the connection while trying to register the platform - " - + platform.getIdentifier() + " for tenant - " + tenantDomain, e); + + platform.getIdentifier() + " for tenant - " + tenantId, e); } catch (TransactionManagementException e) { ConnectionManagerUtil.rollbackTransaction(); throw new PlatformManagementDAOException("Error occurred while performing the transaction on the database " + - "for adding the platform - " + platform.getIdentifier() + " , tenant domain - " + tenantDomain); + "for adding the platform - " + platform.getIdentifier() + " , tenant domain - " + tenantId); } finally { ConnectionManagerUtil.closeConnection(); } } @Override - public void update(String tenantDomain, String oldPlatformIdentifier, Platform platform) throws PlatformManagementDAOException { + public void update(int tenantId, String oldPlatformIdentifier, Platform platform) throws + PlatformManagementDAOException { try { ConnectionManagerUtil.beginTransaction(); - int platformId = getPlatformId(tenantDomain, oldPlatformIdentifier); + int platformId = getPlatformId(tenantId, oldPlatformIdentifier); if (platformId != -1) { Connection connection = ConnectionManagerUtil.getConnection(); if (!platform.isFileBased()) { - String insertToPlatform = "UPDATE APPM_PLATFORM SET IDENTIFIER = ?, NAME =?, DESCRIPTION=?, " + - "IS_SHARED=?, ICON_NAME=? WHERE ID = ?"; + String insertToPlatform = "UPDATE APPM_PLATFORM SET IDENTIFIER = ?, NAME =?, DESCRIPTION=?, " + + "IS_SHARED=?, ICON_NAME=? WHERE ID = ?"; PreparedStatement preparedStatement = connection.prepareStatement(insertToPlatform); preparedStatement.setString(1, platform.getIdentifier()); preparedStatement.setString(2, platform.getName()); @@ -130,14 +135,15 @@ public class GenericPlatformDAOImpl extends AbstractDAOImpl implements PlatformD preparedStatement.setString(5, platform.getIconName()); preparedStatement.execute(); - platformId = getPlatformId(tenantDomain, platform.getIdentifier()); + platformId = getPlatformId(tenantId, platform.getIdentifier()); String deletePlatformProps = "DELETE FROM APPM_PLATFORM_PROPERTIES WHERE PLATFORM_ID=?"; preparedStatement = connection.prepareStatement(deletePlatformProps); preparedStatement.setInt(1, platformId); preparedStatement.execute(); - String insertPlatformProps = "INSERT INTO APPM_PLATFORM_PROPERTIES (PLATFORM_ID, PROP_NAME, OPTIONAL," + - " DEFAULT_VALUE) VALUES ( ? , ?, ? , ?)"; + String insertPlatformProps = + "INSERT INTO APPM_PLATFORM_PROPERTIES (PLATFORM_ID, PROP_NAME, OPTIONAL," + + " DEFAULT_VALUE) VALUES ( ? , ?, ? , ?)"; for (Platform.Property property : platform.getProperties()) { preparedStatement = connection.prepareStatement(insertPlatformProps); preparedStatement.setInt(1, platformId); @@ -154,8 +160,9 @@ public class GenericPlatformDAOImpl extends AbstractDAOImpl implements PlatformD } ConnectionManagerUtil.commitTransaction(); } else { - throw new PlatformManagementDAOException("Cannot find any platform that was registered with identifier - " - + platform.getIdentifier() + " for tenant - " + tenantDomain); + throw new PlatformManagementDAOException( + "Cannot find any platform that was registered with identifier - " + platform.getIdentifier() + + " for tenant - " + tenantId); } } catch (SQLException e) { ConnectionManagerUtil.rollbackTransaction(); @@ -165,23 +172,26 @@ public class GenericPlatformDAOImpl extends AbstractDAOImpl implements PlatformD throw ex; } catch (DBConnectionException e) { ConnectionManagerUtil.rollbackTransaction(); - throw new PlatformManagementDAOException("Unable to obtain the connection while trying to register the platform - " - + platform.getIdentifier() + " for tenant - " + tenantDomain, e); + throw new PlatformManagementDAOException( + "Unable to obtain the connection while trying to register the platform - " + platform + .getIdentifier() + " for tenant - " + tenantId, e); } catch (TransactionManagementException e) { ConnectionManagerUtil.rollbackTransaction(); - throw new PlatformManagementDAOException("Error occurred while performing the transaction on the database " + - "for adding the platform - " + platform.getIdentifier() + " , tenant domain - " + tenantDomain); + throw new PlatformManagementDAOException( + "Error occurred while performing the transaction on the database " + "for adding the platform - " + + platform.getIdentifier() + " , tenant domain - " + tenantId); } finally { ConnectionManagerUtil.closeConnection(); } } - private int getPlatformId(String tenantDomain, String platformIdentifier) throws PlatformManagementDAOException { - String query = "SELECT ID FROM APPM_PLATFORM WHERE (TENANT_DOMAIN=? AND IDENTIFIER=?) OR (IS_SHARED = TRUE AND IDENTIFIER=?)"; + private int getPlatformId(int tenantId, String platformIdentifier) throws PlatformManagementDAOException { + String query = "SELECT ID FROM APPM_PLATFORM WHERE (TENANT_ID=? AND IDENTIFIER=?) OR (IS_SHARED = TRUE AND " + + "IDENTIFIER=?)"; try { Connection connection = ConnectionManagerUtil.getConnection(); PreparedStatement preparedStatement = connection.prepareStatement(query); - preparedStatement.setString(1, tenantDomain); + preparedStatement.setInt(1, tenantId); preparedStatement.setString(2, platformIdentifier); preparedStatement.setString(3, platformIdentifier); ResultSet resultSet = preparedStatement.executeQuery(); @@ -198,10 +208,10 @@ public class GenericPlatformDAOImpl extends AbstractDAOImpl implements PlatformD @Override - public void unregister(String tenantDomain, String platformIdenfier) throws PlatformManagementDAOException { + public void unregister(int tenantId, String platformIdenfier) throws PlatformManagementDAOException { try { ConnectionManagerUtil.beginTransaction(); - int platformId = getPlatformId(tenantDomain, platformIdenfier); + int platformId = getPlatformId(tenantId, platformIdenfier); if (platformId != -1) { Connection connection = ConnectionManagerUtil.getConnection(); String deletePlatform = "DELETE FROM APPM_PLATFORM WHERE ID = ?"; @@ -211,16 +221,18 @@ public class GenericPlatformDAOImpl extends AbstractDAOImpl implements PlatformD ConnectionManagerUtil.commitTransaction(); } else { throw new PlatformManagementDAOException("Platform identifier - " + platformIdenfier - + " is already unregistered registered for tenant - " + tenantDomain); + + " is already unregistered registered for tenant - " + tenantId); } } catch (TransactionManagementException e) { ConnectionManagerUtil.rollbackTransaction(); - throw new PlatformManagementDAOException("Unable to start the transaction while trying to register the platform - " - + platformIdenfier + " for tenant - " + tenantDomain, e); + throw new PlatformManagementDAOException( + "Unable to start the transaction while trying to register the platform - " + platformIdenfier + + " for tenant - " + tenantId, e); } catch (DBConnectionException e) { ConnectionManagerUtil.rollbackTransaction(); - throw new PlatformManagementDAOException("Unable to obtain the connection while trying to register the platform - " - + platformIdenfier + " for tenant - " + tenantDomain, e); + throw new PlatformManagementDAOException( + "Unable to obtain the connection while trying to register the platform - " + platformIdenfier + + " for tenant - " + tenantId, e); } catch (SQLException e) { ConnectionManagerUtil.rollbackTransaction(); throw new PlatformManagementDAOException("Error while executing the SQL query. ", e); @@ -232,33 +244,37 @@ public class GenericPlatformDAOImpl extends AbstractDAOImpl implements PlatformD } } - public void addMapping(String tenantDomain, List platformIdentifiers) throws PlatformManagementDAOException { - String insertMapping = "INSERT INTO APPM_PLATFORM_TENANT_MAPPING(TENANT_DOMAIN, PLATFORM_ID) VALUES (?, ?)"; + public void addMapping(int tenantId, List platformIdentifiers) throws PlatformManagementDAOException { + String insertMapping = "INSERT INTO APPM_PLATFORM_TENANT_MAPPING(TENANT_ID, PLATFORM_ID) VALUES (?, ?)"; try { ConnectionManagerUtil.beginTransaction(); for (String platformIdentifier : platformIdentifiers) { - if (getTenantPlatformMapping(tenantDomain, platformIdentifier) != -1) { - int platformId = getPlatformId(tenantDomain, platformIdentifier); + if (getTenantPlatformMapping(tenantId, platformIdentifier) != -1) { + int platformId = getPlatformId(tenantId, platformIdentifier); Connection connection = ConnectionManagerUtil.getConnection(); PreparedStatement preparedStatement = connection.prepareStatement(insertMapping); - preparedStatement.setString(1, tenantDomain); + preparedStatement.setInt(1, tenantId); preparedStatement.setInt(2, platformId); preparedStatement.execute(); } else { - throw new PlatformManagementDAOException("Platform identifier - " + platformIdentifier + " is already assigned to tenant domain - " + tenantDomain); + throw new PlatformManagementDAOException("Platform identifier - " + platformIdentifier + " is " + + "already assigned to tenant domain - " + tenantId); } } ConnectionManagerUtil.commitTransaction(); } catch (TransactionManagementException e) { ConnectionManagerUtil.rollbackTransaction(); - throw new PlatformManagementDAOException("Error occured while trying to add the mapping of platform - " - + platformIdentifiers.toString() + " for tenant - " + tenantDomain, e); + throw new PlatformManagementDAOException( + "Error occured while trying to add the mapping of platform - " + platformIdentifiers.toString() + + " for tenant - " + tenantId, e); } catch (DBConnectionException e) { ConnectionManagerUtil.rollbackTransaction(); - throw new PlatformManagementDAOException("Error occurred when getting the connection for the database. ", e); + throw new PlatformManagementDAOException("Error occurred when getting the connection for the database. ", + e); } catch (SQLException e) { ConnectionManagerUtil.rollbackTransaction(); - throw new PlatformManagementDAOException("Error occured while executing the SQL query - " + insertMapping, e); + throw new PlatformManagementDAOException("Error occured while executing the SQL query - " + insertMapping, + e); } catch (PlatformManagementDAOException ex) { ConnectionManagerUtil.rollbackTransaction(); throw ex; @@ -267,13 +283,15 @@ public class GenericPlatformDAOImpl extends AbstractDAOImpl implements PlatformD } } - private int getTenantPlatformMapping(String tenantDomain, String platformIdentifier) throws PlatformManagementDAOException { - String getMapping = "SELECT MAPPING.ID as ID FROM (SELECT ID FROM APPM_PLATFORM_TENANT_MAPPING WHERE TENANT_DOMAIN=?) MAPPING JOIN " + - "(SELECT ID FROM APPM_PLATFORM WHERE APPM_PLATFORM.IDENTIFIER=?) PLATFORM ON MAPPING.PLATFORM_ID=PLATFORM.ID"; + private int getTenantPlatformMapping(int tenantId, String platformIdentifier) throws + PlatformManagementDAOException { + String getMapping = "SELECT MAPPING.ID as ID FROM (SELECT ID FROM APPM_PLATFORM_TENANT_MAPPING WHERE " + + "TENANT_ID=?) MAPPING JOIN (SELECT ID FROM APPM_PLATFORM WHERE APPM_PLATFORM.IDENTIFIER=?) " + + "PLATFORM ON MAPPING.PLATFORM_ID=PLATFORM.ID"; try { Connection connection = ConnectionManagerUtil.getConnection(); PreparedStatement preparedStatement = connection.prepareStatement(getMapping); - preparedStatement.setString(1, tenantDomain); + preparedStatement.setInt(1, tenantId); preparedStatement.setString(2, platformIdentifier); ResultSet resultSet = preparedStatement.executeQuery(); if (resultSet.next()) { @@ -281,18 +299,20 @@ public class GenericPlatformDAOImpl extends AbstractDAOImpl implements PlatformD } return -1; } catch (DBConnectionException e) { - throw new PlatformManagementDAOException("Error occured while obtaining the connection to get the existing " + - "Tenant - Platform Mapping.", e); + throw new PlatformManagementDAOException( + "Error occured while obtaining the connection to get the existing " + "Tenant - Platform Mapping.", + e); } catch (SQLException e) { throw new PlatformManagementDAOException("Error occured while executing the SQL query - " + getMapping, e); } } - public void removeMapping(String tenantDomain, String platformIdentifier) throws PlatformManagementDAOException { + @Override + public void removeMapping(int tenantId, String platformIdentifier) throws PlatformManagementDAOException { String deleteMapping = "DELETE FROM APPM_PLATFORM_TENANT_MAPPING WHERE ID = ?"; try { ConnectionManagerUtil.beginTransaction(); - int mappingId = getTenantPlatformMapping(tenantDomain, platformIdentifier); + int mappingId = getTenantPlatformMapping(tenantId, platformIdentifier); if (mappingId != -1) { Connection connection = ConnectionManagerUtil.getConnection(); PreparedStatement preparedStatement = connection.prepareStatement(deleteMapping); @@ -300,13 +320,14 @@ public class GenericPlatformDAOImpl extends AbstractDAOImpl implements PlatformD preparedStatement.execute(); ConnectionManagerUtil.commitTransaction(); } else { - throw new PlatformManagementDAOException("Platform - " + platformIdentifier - + " is already unassigned for tenant - " + tenantDomain); + throw new PlatformManagementDAOException( + "Platform - " + platformIdentifier + " is already unassigned for tenant - " + tenantId); } } catch (TransactionManagementException | DBConnectionException e) { ConnectionManagerUtil.rollbackTransaction(); - throw new PlatformManagementDAOException("Error occurred while unassigning the platform - " + platformIdentifier - + " for tenant - " + tenantDomain); + throw new PlatformManagementDAOException( + "Error occurred while unassigning the platform - " + platformIdentifier + " for tenant - " + + tenantId); } catch (SQLException e) { ConnectionManagerUtil.rollbackTransaction(); throw new PlatformManagementDAOException("Error occurred while executing the query - " + deleteMapping); @@ -320,18 +341,19 @@ public class GenericPlatformDAOImpl extends AbstractDAOImpl implements PlatformD @Override public void removeMappingTenants(String platformIdentifier) throws PlatformManagementDAOException { - int platformId = getPlatformId(MultitenantConstants.SUPER_TENANT_DOMAIN_NAME, platformIdentifier); - String getMapping = "DELETE FROM APPM_PLATFORM_TENANT_MAPPING WHERE TENANT_DOMAIN != ? AND PLATFORM_ID=?"; + int platformId = getPlatformId(MultitenantConstants.SUPER_TENANT_ID, platformIdentifier); + String getMapping = "DELETE FROM APPM_PLATFORM_TENANT_MAPPING WHERE TENANT_ID != ? AND PLATFORM_ID=?"; try { ConnectionManagerUtil.openConnection(); Connection connection = ConnectionManagerUtil.getConnection(); PreparedStatement preparedStatement = connection.prepareStatement(getMapping); - preparedStatement.setString(1, MultitenantConstants.SUPER_TENANT_DOMAIN_NAME); + preparedStatement.setInt(1, MultitenantConstants.SUPER_TENANT_ID); preparedStatement.setInt(2, platformId); preparedStatement.execute(); } catch (DBConnectionException e) { - throw new PlatformManagementDAOException("Error occured while obtaining the connection to get the existing " + - "Tenant - Platform Mapping.", e); + throw new PlatformManagementDAOException( + "Error occured while obtaining the connection to get the existing " + "Tenant - Platform Mapping.", + e); } catch (SQLException e) { throw new PlatformManagementDAOException("Error occured while executing the SQL query - " + getMapping, e); } finally { @@ -341,27 +363,27 @@ public class GenericPlatformDAOImpl extends AbstractDAOImpl implements PlatformD @Override - public List getPlatforms(String tenantDomain) throws PlatformManagementDAOException { + public List getPlatforms(int tenantId) throws PlatformManagementDAOException { if (log.isDebugEnabled()) { - log.debug("GetPlaforms request received for the tenantDomain " + tenantDomain); + log.debug("GetPlaforms request received for the tenant ID " + tenantId); } String selectQuery = - "SELECT MAPPING.ID, PLATFORM.IDENTIFIER FROM (SELECT * FROM APPM_PLATFORM WHERE TENANT_DOMAIN=? OR " + "SELECT MAPPING.ID, PLATFORM.IDENTIFIER FROM (SELECT * FROM APPM_PLATFORM WHERE TENANT_ID=? OR " + "IS_SHARED = TRUE AND FILE_BASED = FALSE) PLATFORM LEFT JOIN APPM_PLATFORM_TENANT_MAPPING " + "MAPPING ON PLATFORM.ID = MAPPING.PLATFORM_ID"; try { Connection connection = ConnectionManagerUtil.openConnection(); PreparedStatement preparedStatement = connection.prepareStatement(selectQuery); - preparedStatement.setString(1, tenantDomain); + preparedStatement.setInt(1, tenantId); ResultSet resultSet = preparedStatement.executeQuery(); List platforms = new ArrayList<>(); if (log.isDebugEnabled()) { - log.debug("Platform retrieved for the tenant domain " + tenantDomain); + log.debug("Platform retrieved for the tenant Id " + tenantId); } while (resultSet.next()) { String identifier = resultSet.getString("PLATFORM.IDENTIFIER"); int mappingID = resultSet.getInt("MAPPING.ID"); - Platform platform = getPlatform(tenantDomain, identifier); + Platform platform = getPlatform(tenantId, identifier); if (mappingID != 0) { platform.setEnabled(true); } else { @@ -373,13 +395,13 @@ public class GenericPlatformDAOImpl extends AbstractDAOImpl implements PlatformD } } if (log.isDebugEnabled()) { - log.debug("Number of platforms available for the tenant domain - " + tenantDomain + " :" + platforms + log.debug("Number of platforms available for the tenant ID - " + tenantId + " :" + platforms .size()); } return platforms; } catch (DBConnectionException e) { throw new PlatformManagementDAOException( - "Error occured when loading the platforms for tenant - " + tenantDomain, e); + "Error occured when loading the platforms for tenant - " + tenantId, e); } catch (SQLException e) { throw new PlatformManagementDAOException("Error occurred when executing query - " + selectQuery, e); } finally { diff --git a/components/application-mgt/org.wso2.carbon.device.application.mgt.core/src/main/java/org/wso2/carbon/device/application/mgt/core/deployer/Platform.java b/components/application-mgt/org.wso2.carbon.device.application.mgt.core/src/main/java/org/wso2/carbon/device/application/mgt/core/deployer/Platform.java index a622cf0fe7..104a82ffd8 100644 --- a/components/application-mgt/org.wso2.carbon.device.application.mgt.core/src/main/java/org/wso2/carbon/device/application/mgt/core/deployer/Platform.java +++ b/components/application-mgt/org.wso2.carbon.device.application.mgt.core/src/main/java/org/wso2/carbon/device/application/mgt/core/deployer/Platform.java @@ -17,11 +17,14 @@ */ package org.wso2.carbon.device.application.mgt.core.deployer; +import java.util.List; import javax.xml.bind.annotation.XmlAttribute; import javax.xml.bind.annotation.XmlElement; import javax.xml.bind.annotation.XmlRootElement; -import java.util.List; +/** + * Platform represents an Application Platform such as Android, IOS, etc. + */ @XmlRootElement(name = "Platform") public class Platform { 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 07659b5dd4..abc043918a 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 @@ -29,21 +29,23 @@ 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; -import javax.xml.bind.JAXBContext; -import javax.xml.bind.JAXBException; -import javax.xml.bind.Unmarshaller; import java.io.File; import java.util.ArrayList; import java.util.List; +import javax.xml.bind.JAXBContext; +import javax.xml.bind.JAXBException; +import javax.xml.bind.Unmarshaller; + public class PlatformDeployer extends AbstractDeployer { private static final Log log = LogFactory.getLog(PlatformDeployer.class); @Override public void init(ConfigurationContext configurationContext) { - File deployementDir = new File(MultitenantUtils.getAxis2RepositoryPath(CarbonContext.getThreadLocalCarbonContext(). - getTenantId()) + Constants.PLATFORMS_DEPLOYMENT_DIR_NAME); + File deployementDir = new File( + MultitenantUtils.getAxis2RepositoryPath(CarbonContext.getThreadLocalCarbonContext(). + getTenantId()) + Constants.PLATFORMS_DEPLOYMENT_DIR_NAME); if (!deployementDir.exists()) { if (!deployementDir.mkdir()) { log.warn("Unable to create the deployment dir at: " + deployementDir.getPath()); @@ -59,10 +61,12 @@ public class PlatformDeployer extends AbstractDeployer { Platform platformConf = (Platform) unmarshaller.unmarshal(deploymentFile); if (platformConf.getName().contentEquals(getPlatformID(deploymentFile.getName()))) { org.wso2.carbon.device.application.mgt.common.Platform platform = convert(platformConf); - DataHolder.getInstance().getPlatformManager().register(CarbonContext.getThreadLocalCarbonContext().getTenantDomain(), platform); + DataHolder.getInstance().getPlatformManager() + .register(CarbonContext.getThreadLocalCarbonContext().getTenantId(), platform); } else { - log.error("Unable to deploy the platform - " + deploymentFile.getAbsolutePath() + "!. Platform config file name - " - + deploymentFile.getName() + " should match with the 'id' provided within the platform configuration!"); + log.error("Unable to deploy the platform - " + deploymentFile.getAbsolutePath() + + "!. Platform config file name - " + deploymentFile.getName() + + " should match with the 'id' provided within the platform configuration!"); } } catch (JAXBException e) { log.error("Platform configuration file - " + deploymentFile.getAbsolutePath() + " is invalid!", e); @@ -74,7 +78,8 @@ public class PlatformDeployer extends AbstractDeployer { public void undeploy(String fileName) throws DeploymentException { String platformId = getPlatformID(fileName); try { - DataHolder.getInstance().getPlatformManager().unregister(CarbonContext.getThreadLocalCarbonContext().getTenantDomain(), platformId, true); + DataHolder.getInstance().getPlatformManager() + .unregister(CarbonContext.getThreadLocalCarbonContext().getTenantId(), platformId, true); } catch (PlatformManagementException e) { log.error("Error occurred while undeploying the platform - " + fileName); } @@ -89,7 +94,8 @@ public class PlatformDeployer extends AbstractDeployer { } private org.wso2.carbon.device.application.mgt.common.Platform convert(Platform platformConfig) { - org.wso2.carbon.device.application.mgt.common.Platform platform = new org.wso2.carbon.device.application.mgt.common.Platform(); + org.wso2.carbon.device.application.mgt.common.Platform platform = + new org.wso2.carbon.device.application.mgt.common.Platform(); platform.setIdentifier(platformConfig.getId()); platform.setName(platformConfig.getName()); platform.setDescription(platformConfig.getDescription()); @@ -100,7 +106,8 @@ public class PlatformDeployer extends AbstractDeployer { platform.setEnabled(false); List properties = new ArrayList<>(); for (Property propertyConfig : platformConfig.getProperties()) { - org.wso2.carbon.device.application.mgt.common.Platform.Property property = new org.wso2.carbon.device.application.mgt.common.Platform.Property(); + org.wso2.carbon.device.application.mgt.common.Platform.Property property = + new org.wso2.carbon.device.application.mgt.common.Platform.Property(); property.setName(propertyConfig.getName()); property.setDefaultValue(propertyConfig.getDefaultValue()); property.setOptional(propertyConfig.isOptional()); diff --git a/components/application-mgt/org.wso2.carbon.device.application.mgt.core/src/main/java/org/wso2/carbon/device/application/mgt/core/exception/PlatformManagementDAOException.java b/components/application-mgt/org.wso2.carbon.device.application.mgt.core/src/main/java/org/wso2/carbon/device/application/mgt/core/exception/PlatformManagementDAOException.java index 413efd6626..0cd87944de 100644 --- a/components/application-mgt/org.wso2.carbon.device.application.mgt.core/src/main/java/org/wso2/carbon/device/application/mgt/core/exception/PlatformManagementDAOException.java +++ b/components/application-mgt/org.wso2.carbon.device.application.mgt.core/src/main/java/org/wso2/carbon/device/application/mgt/core/exception/PlatformManagementDAOException.java @@ -19,6 +19,9 @@ package org.wso2.carbon.device.application.mgt.core.exception; import org.wso2.carbon.device.application.mgt.common.exception.PlatformManagementException; +/** + * Exception that will be thrown when there is a issue during Platform level DAO operations. + */ public class PlatformManagementDAOException extends PlatformManagementException { public PlatformManagementDAOException(String message, Throwable ex) { 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 4d9730c8bf..58cb92355d 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 @@ -34,8 +34,11 @@ import java.util.HashMap; import java.util.List; import java.util.Map; +/** + * Implementation of {@link PlatformManager}, which manages the CRUD operations on Application platforms. + */ public class PlatformManagerImpl implements PlatformManager { - private Map> inMemoryStore; + private Map> inMemoryStore; private static Log log = LogFactory.getLog(PlatformManagerImpl.class); public PlatformManagerImpl() { @@ -43,34 +46,34 @@ public class PlatformManagerImpl implements PlatformManager { } @Override - public void initialize(String tenantDomain) throws PlatformManagementException { - List platforms = DAOFactory.getPlatformDAO().getPlatforms(tenantDomain); + public void initialize(int tenantId) throws PlatformManagementException { + List platforms = DAOFactory.getPlatformDAO().getPlatforms(tenantId); List platformIdentifiers = new ArrayList<>(); for (Platform platform : platforms) { if (!platform.isEnabled() & platform.isDefaultTenantMapping()) { platformIdentifiers.add(platform.getIdentifier()); } } - addMapping(tenantDomain, platformIdentifiers); + addMapping(tenantId, platformIdentifiers); } @Override - public List getPlatforms(String tenantDomain) throws PlatformManagementException { + public List getPlatforms(int tenantId) throws PlatformManagementException { if (log.isDebugEnabled()) { - log.debug("Request for getting platforms received for the tenant domain " + tenantDomain + " at " + log.debug("Request for getting platforms received for the tenant ID " + tenantId + " at " + "PlatformManager level"); } - List platforms = DAOFactory.getPlatformDAO().getPlatforms(tenantDomain); + List platforms = DAOFactory.getPlatformDAO().getPlatforms(tenantId); int platformIndex = 0; if (log.isDebugEnabled()) { log.debug("Number of platforms received from DAO layer is " + platforms.size() + " for the tenant " - + tenantDomain); + + tenantId); } for (Platform platform : platforms) { if (platform.isFileBased()) { Map superTenantPlatforms = this.inMemoryStore - .get(MultitenantConstants.SUPER_TENANT_DOMAIN_NAME); + .get(MultitenantConstants.SUPER_TENANT_ID); Platform registeredPlatform = superTenantPlatforms.get(platform.getIdentifier()); if (registeredPlatform != null) { platforms.set(platformIndex, new Platform(registeredPlatform)); @@ -87,36 +90,37 @@ public class PlatformManagerImpl implements PlatformManager { platformIndex++; } if (log.isDebugEnabled()) { - log.debug("Number of effective platforms for the tenant " + tenantDomain + " : " + platforms.size()); + log.debug("Number of effective platforms for the tenant " + tenantId + + " : " + platforms.size()); } return platforms; } @Override - public Platform getPlatform(String tenantDomain, String identifier) throws PlatformManagementException { - Platform platform = getPlatformFromInMemory(tenantDomain, identifier); + public Platform getPlatform(int tenantId, String identifier) throws PlatformManagementException { + Platform platform = getPlatformFromInMemory(tenantId, identifier); if (platform == null) { - platform = DAOFactory.getPlatformDAO().getPlatform(tenantDomain, identifier); + platform = DAOFactory.getPlatformDAO().getPlatform(tenantId, identifier); if (platform != null) { return platform; } } else { return new Platform(platform); } - throw new PlatformManagementException("No platform was found for tenant - " + tenantDomain + + throw new PlatformManagementException("No platform was found for tenant - " + tenantId + " with Platform identifier - " + identifier); } - private Platform getPlatformFromInMemory(String tenantDomain, String identifier) { - Map platformMap = this.inMemoryStore.get(tenantDomain); + private Platform getPlatformFromInMemory(int tenantId, String identifier) { + Map platformMap = this.inMemoryStore.get(tenantId); if (platformMap != null) { Platform platform = platformMap.get(identifier); if (platform != null) { return platform; } } - if (!tenantDomain.equalsIgnoreCase(MultitenantConstants.SUPER_TENANT_DOMAIN_NAME)) { - platformMap = this.inMemoryStore.get(MultitenantConstants.SUPER_TENANT_DOMAIN_NAME); + if (tenantId != MultitenantConstants.SUPER_TENANT_ID) { + platformMap = this.inMemoryStore.get(MultitenantConstants.SUPER_TENANT_ID); if (platformMap != null) { Platform platform = platformMap.get(identifier); if (platform != null && platform.isShared()) { @@ -128,23 +132,25 @@ public class PlatformManagerImpl implements PlatformManager { } @Override - public synchronized void register(String tenantDomain, Platform platform) throws PlatformManagementException { - if (platform.isShared() && !tenantDomain.equals(MultitenantConstants.SUPER_TENANT_DOMAIN_NAME)) { - throw new PlatformManagementException("Platform sharing is a restricted operation, therefore Platform - " - + platform.getIdentifier() + " cannot be shared by the tenant domain - " + tenantDomain); + public synchronized void register(int tenantId, Platform platform) throws PlatformManagementException { + if (platform.isShared() && tenantId != MultitenantConstants.SUPER_TENANT_ID) { + throw new PlatformManagementException( + "Platform sharing is a restricted operation, therefore Platform - " + platform.getIdentifier() + + " cannot be shared by the tenant domain - " + tenantId); } - int platformId = DAOFactory.getPlatformDAO().register(tenantDomain, platform); + int platformId = DAOFactory.getPlatformDAO().register(tenantId, platform); if (platform.isFileBased()) { platform.setId(platformId); - Map tenantPlatforms = this.inMemoryStore.get(tenantDomain); + Map tenantPlatforms = this.inMemoryStore.get(tenantId); if (tenantPlatforms == null) { tenantPlatforms = new HashMap<>(); - this.inMemoryStore.put(tenantDomain, tenantPlatforms); + this.inMemoryStore.put(tenantId, tenantPlatforms); } if (tenantPlatforms.get(platform.getIdentifier()) == null) { tenantPlatforms.put(platform.getIdentifier(), platform); } else { - throw new PlatformManagementException("Platform - " + platform.getIdentifier() + " is already registered!"); + throw new PlatformManagementException( + "Platform - " + platform.getIdentifier() + " is already registered!"); } } if (platform.isDefaultTenantMapping()) { @@ -153,10 +159,10 @@ public class PlatformManagerImpl implements PlatformManager { TenantManager tenantManager = DataHolder.getInstance().getRealmService().getTenantManager(); Tenant[] tenants = tenantManager.getAllTenants(); for (Tenant tenant : tenants) { - addMapping(tenant.getDomain(), platform.getIdentifier()); + addMapping(tenant.getId(), platform.getIdentifier()); } } - addMapping(tenantDomain, platform.getIdentifier()); + addMapping(tenantId, platform.getIdentifier()); } catch (UserStoreException e) { throw new PlatformManagementException("Error occured while assigning the platforms for tenants!", e); } @@ -164,31 +170,34 @@ public class PlatformManagerImpl implements PlatformManager { } @Override - public void update(String tenantDomain, String oldPlatformIdentifier, Platform platform) + public void update(int tenantId, String oldPlatformIdentifier, Platform platform) throws PlatformManagementException { - if (platform.isShared() && !tenantDomain.equals(MultitenantConstants.SUPER_TENANT_DOMAIN_NAME)) { - throw new PlatformManagementException("Platform sharing is a restricted operation, therefore Platform - " - + platform.getIdentifier() + " cannot be shared by the tenant domain - " + tenantDomain); + if (platform.isShared() && tenantId != MultitenantConstants.SUPER_TENANT_ID) { + throw new PlatformManagementException( + "Platform sharing is a restricted operation, therefore Platform - " + platform.getIdentifier() + + " cannot be shared by the tenant domain - " + tenantId); } Platform oldPlatform; if (platform.isFileBased()) { - Map tenantPlatforms = this.inMemoryStore.get(tenantDomain); + Map tenantPlatforms = this.inMemoryStore.get(tenantId); if (tenantPlatforms == null) { - throw new PlatformManagementException("No platforms registered for the tenant - " + tenantDomain + - " with platform identifier - " + platform.getIdentifier()); + throw new PlatformManagementException( + "No platforms registered for the tenant - " + tenantId + " with platform identifier - " + + platform.getIdentifier()); } oldPlatform = tenantPlatforms.get(oldPlatformIdentifier); if (oldPlatform == null) { - throw new PlatformManagementException("No platforms registered for the tenant - " + tenantDomain + - " with platform identifier - " + platform.getIdentifier()); + throw new PlatformManagementException( + "No platforms registered for the tenant - " + tenantId + " with platform identifier - " + + platform.getIdentifier()); } else { - DAOFactory.getPlatformDAO().update(tenantDomain, oldPlatformIdentifier, platform); + DAOFactory.getPlatformDAO().update(tenantId, oldPlatformIdentifier, platform); platform.setId(oldPlatform.getId()); tenantPlatforms.put(platform.getIdentifier(), platform); } } else { - oldPlatform = DAOFactory.getPlatformDAO().getPlatform(tenantDomain, oldPlatformIdentifier); - DAOFactory.getPlatformDAO().update(tenantDomain, oldPlatformIdentifier, platform); + oldPlatform = DAOFactory.getPlatformDAO().getPlatform(tenantId, oldPlatformIdentifier); + DAOFactory.getPlatformDAO().update(tenantId, oldPlatformIdentifier, platform); } if (platform.isDefaultTenantMapping() && !oldPlatform.isDefaultTenantMapping()) { try { @@ -196,12 +205,12 @@ public class PlatformManagerImpl implements PlatformManager { TenantManager tenantManager = DataHolder.getInstance().getRealmService().getTenantManager(); Tenant[] tenants = tenantManager.getAllTenants(); for (Tenant tenant : tenants) { - addMapping(tenant.getDomain(), platform.getIdentifier()); + addMapping(tenant.getId(), platform.getIdentifier()); } } - addMapping(tenantDomain, platform.getIdentifier()); + addMapping(tenantId, platform.getIdentifier()); } catch (UserStoreException e) { - throw new PlatformManagementException("Error occured while assigning the platforms for tenants!", e); + throw new PlatformManagementException("Error occurred while assigning the platforms for tenants!", e); } } if (!platform.isShared() && oldPlatform.isShared()) { @@ -210,31 +219,32 @@ public class PlatformManagerImpl implements PlatformManager { } @Override - public void unregister(String tenantDomain, String identifier, boolean isFileBased) throws PlatformManagementException { + public void unregister(int tenantId, String identifier, boolean isFileBased) throws + PlatformManagementException { if (isFileBased) { - Map tenantPlatforms = this.inMemoryStore.get(tenantDomain); + Map tenantPlatforms = this.inMemoryStore.get(tenantId); if (tenantPlatforms != null) { this.inMemoryStore.remove(identifier); } } else { - DAOFactory.getPlatformDAO().unregister(tenantDomain, identifier); + DAOFactory.getPlatformDAO().unregister(tenantId, identifier); } } @Override - public void addMapping(String tenantDomain, List platformIdentifiers) throws PlatformManagementException { - DAOFactory.getPlatformDAO().addMapping(tenantDomain, platformIdentifiers); + public void addMapping(int tenantId, List platformIdentifiers) throws PlatformManagementException { + DAOFactory.getPlatformDAO().addMapping(tenantId, platformIdentifiers); } @Override - public void addMapping(String tenantDomain, String platformIdentifier) throws PlatformManagementException { + public void addMapping(int tenantId, String platformIdentifier) throws PlatformManagementException { List identifiers = new ArrayList<>(); identifiers.add(platformIdentifier); - DAOFactory.getPlatformDAO().addMapping(tenantDomain, identifiers); + DAOFactory.getPlatformDAO().addMapping(tenantId, identifiers); } @Override - public void removeMapping(String tenantDomain, String platformIdentifier) throws PlatformManagementException { - DAOFactory.getPlatformDAO().removeMapping(tenantDomain, platformIdentifier); + public void removeMapping(int tenantId, String platformIdentifier) throws PlatformManagementException { + DAOFactory.getPlatformDAO().removeMapping(tenantId, platformIdentifier); } } diff --git a/components/application-mgt/org.wso2.carbon.device.application.mgt.core/src/main/java/org/wso2/carbon/device/application/mgt/core/internal/DataHolder.java b/components/application-mgt/org.wso2.carbon.device.application.mgt.core/src/main/java/org/wso2/carbon/device/application/mgt/core/internal/DataHolder.java index d2719a9d0e..5c4bac0495 100644 --- a/components/application-mgt/org.wso2.carbon.device.application.mgt.core/src/main/java/org/wso2/carbon/device/application/mgt/core/internal/DataHolder.java +++ b/components/application-mgt/org.wso2.carbon.device.application.mgt.core/src/main/java/org/wso2/carbon/device/application/mgt/core/internal/DataHolder.java @@ -29,7 +29,6 @@ import org.wso2.carbon.device.application.mgt.common.services.SubscriptionManage import org.wso2.carbon.device.application.mgt.common.services.VisibilityManager; import org.wso2.carbon.device.application.mgt.common.services.VisibilityTypeManager; import org.wso2.carbon.device.mgt.core.service.DeviceManagementProviderService; -import org.wso2.carbon.ndatasource.core.DataSourceService; import org.wso2.carbon.user.core.service.RealmService; /** @@ -63,8 +62,6 @@ public class DataHolder { private static final DataHolder applicationMgtDataHolder = new DataHolder(); - private DataSourceService dataSourceService; - private DataHolder() { } @@ -169,11 +166,4 @@ public class DataHolder { this.realmService = realmService; } - public void setDataSourceService(DataSourceService dataSourceService) { - this.dataSourceService = dataSourceService; - } - - public DataSourceService getDataSourceService() { - return dataSourceService; - } } 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 c1b536a254..e729af9a6a 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 @@ -70,8 +70,7 @@ public class ServiceComponent { private static Log log = LogFactory.getLog(ServiceComponent.class); - protected void activate(ComponentContext componentContext) throws NamingException, - ApplicationManagementDAOException { + protected void activate(ComponentContext componentContext) throws NamingException { BundleContext bundleContext = componentContext.getBundleContext(); try { String datasourceName = ConfigurationManager.getInstance().getConfiguration().getDatasourceName(); @@ -119,9 +118,12 @@ public class ServiceComponent { bundleContext.registerService(ApplicationUploadManager.class.getName(), uploadManager, null); DAOFactory.init(datasourceName); + DAOFactory.initDatabases(); log.info("ApplicationManagement core bundle has been successfully initialized"); } catch (InvalidConfigurationException e) { log.error("Error while activating Application Management core component. ", e); + } catch (ApplicationManagementDAOException e) { + log.error("Error while activating Application Management core component.Failed to create the database ", e); } } @@ -152,10 +154,12 @@ public class ServiceComponent { } protected void setDataSourceService(DataSourceService dataSourceService) { - DataHolder.getInstance().setDataSourceService(dataSourceService); + //Not implemented. Not needed but to make sure the datasource service are registered, as it is needed create + // databases. } protected void unsetDataSourceService(DataSourceService dataSourceService) { - DataHolder.getInstance().setDataSourceService(null); + //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 ccdcf5e083..197b82921f 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 @@ -35,8 +35,8 @@ public class ApplicationMgtDatabaseCreator extends DatabaseCreator { CarbonUtils.getCarbonHome() + File.separator + "dbscripts" + File.separator + "cdm" + File.separator + "application-mgt" + File.separator; - public ApplicationMgtDatabaseCreator(DataSource dataSource) { - super(dataSource); + public ApplicationMgtDatabaseCreator(String dataSourceName) { + super(ConnectionManagerUtil.resolveDataSource(dataSourceName)); } protected String getDbScriptLocation(String databaseType) { 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 0a99a14e47..664e637901 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 @@ -160,18 +160,19 @@ public class ConnectionManagerUtil { } } - /** - * Resolve data source from the data source definition. + * Resolve the datasource from the datasource definition. * - * @param dataSourceName data source name + * @param dataSourceName Name of the datasource + * @return DataSource resolved by the datasource name */ - public static void resolveDataSource(String dataSourceName) { + public static DataSource resolveDataSource(String dataSourceName) { try { dataSource = InitialContext.doLookup(dataSourceName); } catch (Exception e) { throw new RuntimeException("Error in looking up data source: " + e.getMessage(), e); } + return dataSource; } @@ -183,4 +184,5 @@ public class ConnectionManagerUtil { } return null; } + } diff --git a/components/application-mgt/pom.xml b/components/application-mgt/pom.xml index 41ed7a816a..9770fd0643 100644 --- a/components/application-mgt/pom.xml +++ b/components/application-mgt/pom.xml @@ -41,6 +41,12 @@ + + + org.apache.maven.plugins + maven-checkstyle-plugin + + 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 32e670c95b..6f19489dec 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 @@ -13,6 +13,9 @@ CREATE TABLE IF NOT EXISTS APPM_PLATFORM ( ICON_NAME VARCHAR(100) NULL, DESCRIPTOR LONGTEXT NOT NULL, PUBLISHED TINYINT NULL, + IS_SHARED BOOLEAN DEFAULT FALSE, + FILE_BASED BOOLEAN DEFAULT FALSE, + TENANT_ID INT, PRIMARY KEY (ID), UNIQUE INDEX IDENTIFIER_UNIQUE (IDENTIFIER ASC)); @@ -27,9 +30,9 @@ CREATE TABLE IF NOT EXISTS APPM_APPLICATION_CATEGORY ( PRIMARY KEY (ID)); -- ----------------------------------------------------- --- Table APPM_PLATFORM_APPLICATION_MAPPING +-- Table APPM_PLATFORM_TENANT_MAPPING -- ----------------------------------------------------- -CREATE TABLE IF NOT EXISTS APPM_PLATFORM_APPLICATION_MAPPING ( +CREATE TABLE IF NOT EXISTS APPM_PLATFORM_TENANT_MAPPING ( ID INT NOT NULL AUTO_INCREMENT, PLATFORM_ID INT NOT NULL, TENANT_ID INT NOT NULL, @@ -40,7 +43,7 @@ CREATE TABLE IF NOT EXISTS APPM_PLATFORM_APPLICATION_MAPPING ( ON DELETE NO ACTION ON UPDATE NO ACTION); -CREATE INDEX FK_PLATFROM_APPLICATION_MAPPING_PLATFORM ON APPM_PLATFORM_APPLICATION_MAPPING(PLATFORM_ID ASC); +CREATE INDEX FK_PLATFROM_TENANT_MAPPING_PLATFORM ON APPM_PLATFORM_TENANT_MAPPING(PLATFORM_ID ASC); -- ----------------------------------------------------- @@ -83,9 +86,9 @@ CREATE TABLE IF NOT EXISTS APPM_APPLICATION ( REFERENCES APPM_APPLICATION_CATEGORY (ID) ON DELETE NO ACTION ON UPDATE NO ACTION, - CONSTRAINT fk_APPM_APPLICATION_APPM_PLATFORM_APPLICATION_MAPPING1 + CONSTRAINT fk_APPM_APPLICATION_APPM_PLATFORM_TENANT_MAPPING1 FOREIGN KEY (PLATFORM_APPLICATION_MAPPING_ID) - REFERENCES APPM_PLATFORM_APPLICATION_MAPPING (ID) + REFERENCES APPM_PLATFORM_TENANT_MAPPING (ID) ON DELETE NO ACTION ON UPDATE NO ACTION, CONSTRAINT fk_APPM_APPLICATION_APPM_LIFECYCLE_STATE1 @@ -278,8 +281,8 @@ CREATE INDEX FK_VISIBILITY_APPLICATION ON APPM_VISIBILITY(APPLICATION_ID ASC); -- Table APPM_SUBSCRIPTION_PROPERTIES -- ----------------------------------------------------- CREATE TABLE IF NOT EXISTS APPM_SUBSCRIPTION_PROPERTIES ( - PROP_KEY TEXT NOT NULL, - PROP_VALUE TEXT NULL, + PROP_KEY VARCHAR(500) NOT NULL, + PROP_VALUE VARCHAR(500) NULL, APPM_SUBSCRIPTION_ID INT NOT NULL, PRIMARY KEY (PROP_KEY, APPM_SUBSCRIPTION_ID), CONSTRAINT fk_APPM_SUBSCRIPTION_PROPERTIES_APPM_SUBSCRIPTION1 diff --git a/features/application-mgt/org.wso2.carbon.device.application.mgt.server.feature/src/main/resources/dbscripts/cdm/application-mgt/mysql.sql b/features/application-mgt/org.wso2.carbon.device.application.mgt.server.feature/src/main/resources/dbscripts/cdm/application-mgt/mysql.sql index 485fb3b2bf..a316f290e6 100644 --- a/features/application-mgt/org.wso2.carbon.device.application.mgt.server.feature/src/main/resources/dbscripts/cdm/application-mgt/mysql.sql +++ b/features/application-mgt/org.wso2.carbon.device.application.mgt.server.feature/src/main/resources/dbscripts/cdm/application-mgt/mysql.sql @@ -20,6 +20,8 @@ CREATE TABLE IF NOT EXISTS `APPM_PLATFORM` ( `DESCRIPTION` TEXT NULL, `IDENTIFIER` VARCHAR(100) NOT NULL, `DESCRIPTOR` LONGTEXT NOT NULL, + IS_SHARED BOOLEAN DEFAULT FALSE, + FILE_BASED BOOLEAN DEFAULT FALSE, PRIMARY KEY (`ID`), UNIQUE INDEX `IDENTIFIER_UNIQUE` (`IDENTIFIER` ASC)) ENGINE = InnoDB @@ -235,7 +237,7 @@ CREATE TABLE IF NOT EXISTS `APPM_COMMENT` ( -- ----------------------------------------------------- -- Table `APPM_PLATFORM_TENENT_MAPPING` -- ----------------------------------------------------- -CREATE TABLE IF NOT EXISTS `APPM_PLATFORM_TENENT_MAPPING` ( +CREATE TABLE IF NOT EXISTS `APPM_PLATFORM_TENANT_MAPPING` ( `ID` INT NOT NULL AUTO_INCREMENT, `PLATFORM_ID` INT NOT NULL, `TENANT_ID` INT NOT NULL, diff --git a/pom.xml b/pom.xml index 5164788cb9..753b1a5c5d 100644 --- a/pom.xml +++ b/pom.xml @@ -953,11 +953,6 @@ org.wso2.carbon.identity.application.common ${carbon.identity.framework.version} - - org.wso2.carbon.identity - org.wso2.carbon.identity.oauth2.grant.jwt - ${identity.jwt.extension.version} - org.wso2.carbon.identity.framework org.wso2.carbon.user.mgt @@ -1190,16 +1185,6 @@ org.wso2.carbon.application.mgt.stub ${carbon.commons.version} - - org.wso2.carbon.analytics - org.wso2.carbon.analytics.api - ${carbon.analytics.version} - - - org.wso2.carbon.analytics - org.wso2.carbon.analytics.datasource.commons - ${carbon.analytics.version} - org.wso2.carbon.event-processing org.wso2.carbon.event.processor.stub @@ -1430,11 +1415,6 @@ org.wso2.carbon.event.output.adapter.core ${carbon.analytics.common.version} - - org.wso2.carbon.analytics-common - org.wso2.carbon.event.receiver.stub - ${carbon.analytics.common.version} - org.wso2.carbon.analytics org.wso2.carbon.analytics.datasource.commons @@ -1445,11 +1425,6 @@ org.wso2.carbon.analytics.dataservice.commons ${carbon.analytics.version} - - org.wso2.carbon.analytics-common - org.wso2.carbon.event.stream.stub - ${carbon.analytics.common.version} - org.wso2.carbon.analytics-common org.wso2.carbon.event.publisher.stub @@ -1666,10 +1641,6 @@ org.apache.maven.plugins maven-deploy-plugin - - org.apache.maven.plugins - maven-checkstyle-plugin - @@ -2022,7 +1993,6 @@ 2.5 - 1.0.2 2.6.1.wso2v1 2.6.1.wso2v3 2.8.2.wso2v1 From e79c9b4919c16f045084b7073bd67f9636047a48 Mon Sep 17 00:00:00 2001 From: megala21 Date: Mon, 31 Jul 2017 15:34:02 +0530 Subject: [PATCH 02/10] Fixing minor issues with database queries --- .../impl/platform/GenericPlatformDAOImpl.java | 24 +++++--- .../mgt/core/deployer/PlatformDeployer.java | 19 +++--- .../dbscripts/cdm/application-mgt/h2.sql | 58 ++++++++++--------- .../dbscripts/cdm/application-mgt/mysql.sql | 34 +++++++---- 4 files changed, 79 insertions(+), 56 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 ae51b716d2..b7cee5755b 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 @@ -249,7 +249,7 @@ public class GenericPlatformDAOImpl extends AbstractDAOImpl implements PlatformD try { ConnectionManagerUtil.beginTransaction(); for (String platformIdentifier : platformIdentifiers) { - if (getTenantPlatformMapping(tenantId, platformIdentifier) != -1) { + if (getTenantPlatformMapping(tenantId, platformIdentifier) == -1) { int platformId = getPlatformId(tenantId, platformIdentifier); Connection connection = ConnectionManagerUtil.getConnection(); PreparedStatement preparedStatement = connection.prepareStatement(insertMapping); @@ -285,8 +285,8 @@ public class GenericPlatformDAOImpl extends AbstractDAOImpl implements PlatformD private int getTenantPlatformMapping(int tenantId, String platformIdentifier) throws PlatformManagementDAOException { - String getMapping = "SELECT MAPPING.ID as ID FROM (SELECT ID FROM APPM_PLATFORM_TENANT_MAPPING WHERE " - + "TENANT_ID=?) MAPPING JOIN (SELECT ID FROM APPM_PLATFORM WHERE APPM_PLATFORM.IDENTIFIER=?) " + String getMapping = "SELECT MAPPING.ID as ID FROM (SELECT ID, PLATFORM_ID FROM APPM_PLATFORM_TENANT_MAPPING " + + "WHERE TENANT_ID=?) MAPPING JOIN (SELECT ID FROM APPM_PLATFORM WHERE APPM_PLATFORM.IDENTIFIER=?) " + "PLATFORM ON MAPPING.PLATFORM_ID=PLATFORM.ID"; try { Connection connection = ConnectionManagerUtil.getConnection(); @@ -300,7 +300,7 @@ public class GenericPlatformDAOImpl extends AbstractDAOImpl implements PlatformD return -1; } catch (DBConnectionException e) { throw new PlatformManagementDAOException( - "Error occured while obtaining the connection to get the existing " + "Tenant - Platform Mapping.", + "Error occurred while obtaining the connection to get the existing " + "Tenant - Platform Mapping.", e); } catch (SQLException e) { throw new PlatformManagementDAOException("Error occured while executing the SQL query - " + getMapping, e); @@ -462,21 +462,27 @@ public class GenericPlatformDAOImpl extends AbstractDAOImpl implements PlatformD try { conn = this.getConnection(); - sql += "SELECT * "; - sql += "FROM APPM_PLATFORM "; - sql += "WHERE IDENTIFIER = ? "; + sql += "SELECT * FROM APPM_PLATFORM WHERE IDENTIFIER = ? AND TENANT_ID = ?"; stmt = conn.prepareStatement(sql); stmt.setString(1, identifier); + stmt.setInt(2, tenantId); rs = stmt.executeQuery(); Platform platform = null; if (rs.next()) { platform = new Platform(); - platform.setId(rs.getInt("ID")); - platform.setName(rs.getString("NAME")); + platform.setFileBased(rs.getBoolean("FILE_BASED")); + platform.setIdentifier(rs.getString("IDENTIFIER")); + 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")); + } } return platform; 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 abc043918a..51fb6bef4d 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 @@ -59,7 +59,7 @@ public class PlatformDeployer extends AbstractDeployer { JAXBContext jaxbContext = JAXBContext.newInstance(Platform.class); Unmarshaller unmarshaller = jaxbContext.createUnmarshaller(); Platform platformConf = (Platform) unmarshaller.unmarshal(deploymentFile); - if (platformConf.getName().contentEquals(getPlatformID(deploymentFile.getName()))) { + 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); @@ -105,13 +105,16 @@ public class PlatformDeployer extends AbstractDeployer { platform.setDefaultTenantMapping(platformConfig.isTenantMapping()); platform.setEnabled(false); List properties = new ArrayList<>(); - for (Property propertyConfig : platformConfig.getProperties()) { - org.wso2.carbon.device.application.mgt.common.Platform.Property property = - new org.wso2.carbon.device.application.mgt.common.Platform.Property(); - property.setName(propertyConfig.getName()); - property.setDefaultValue(propertyConfig.getDefaultValue()); - property.setOptional(propertyConfig.isOptional()); - properties.add(property); + + if (platformConfig.getProperties() != null) { + for (Property propertyConfig : platformConfig.getProperties()) { + org.wso2.carbon.device.application.mgt.common.Platform.Property property = + new org.wso2.carbon.device.application.mgt.common.Platform.Property(); + property.setName(propertyConfig.getName()); + property.setDefaultValue(propertyConfig.getDefaultValue()); + property.setOptional(propertyConfig.isOptional()); + properties.add(property); + } } platform.setProperties(properties); return platform; 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 6f19489dec..06c22b4750 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 @@ -6,18 +6,37 @@ -- Table APPM_PLATFORM -- ----------------------------------------------------- CREATE TABLE IF NOT EXISTS APPM_PLATFORM ( - ID INT NOT NULL, - NAME VARCHAR(100) NOT NULL, - DESCRIPTION TEXT NULL, - IDENTIFIER VARCHAR(100) NOT NULL, - ICON_NAME VARCHAR(100) NULL, - DESCRIPTOR LONGTEXT NOT NULL, - PUBLISHED TINYINT NULL, - IS_SHARED BOOLEAN DEFAULT FALSE, - FILE_BASED BOOLEAN DEFAULT FALSE, - TENANT_ID INT, - PRIMARY KEY (ID), - UNIQUE INDEX IDENTIFIER_UNIQUE (IDENTIFIER ASC)); +ID INT NOT NULL AUTO_INCREMENT UNIQUE, +IDENTIFIER VARCHAR (100) NOT NULL, +TENANT_ID INT NOT NULL , +NAME VARCHAR (255), +FILE_BASED BOOLEAN, +DESCRIPTION LONGVARCHAR, +IS_SHARED BOOLEAN, +ICON_NAME VARCHAR (100), +PRIMARY KEY (IDENTIFIER, TENANT_ID) +); + +CREATE TABLE IF NOT EXISTS APPM_PLATFORM_PROPERTIES ( +ID INT NOT NULL AUTO_INCREMENT, +PLATFORM_ID VARCHAR (100) NOT NULL, +PROP_NAME VARCHAR (100) NOT NULL, +OPTIONAL BOOLEAN, +DEFAUL_VALUE VARCHAR (255), +TENANT_ID INT NOT NULL , +FOREIGN KEY(PLATFORM_ID, TENANT_ID) REFERENCES APPM_PLATFORM(IDENTIFIER, TENANT_ID) ON DELETE CASCADE, +PRIMARY KEY (ID, PLATFORM_ID, PROP_NAME, TENANT_ID) +); + +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, +PRIMARY KEY (ID, TENANT_ID, PLATFORM_ID) +); + +CREATE INDEX FK_PLATFROM_TENANT_MAPPING_PLATFORM ON APPM_PLATFORM_TENANT_MAPPING(PLATFORM_ID ASC); -- ----------------------------------------------------- -- Table APPM_APPLICATION_CATEGORY @@ -29,21 +48,6 @@ CREATE TABLE IF NOT EXISTS APPM_APPLICATION_CATEGORY ( PUBLISHED TINYINT NULL, PRIMARY KEY (ID)); --- ----------------------------------------------------- --- Table APPM_PLATFORM_TENANT_MAPPING --- ----------------------------------------------------- -CREATE TABLE IF NOT EXISTS APPM_PLATFORM_TENANT_MAPPING ( - ID INT NOT NULL AUTO_INCREMENT, - PLATFORM_ID INT NOT NULL, - TENANT_ID INT NOT NULL, - PRIMARY KEY (ID, PLATFORM_ID), - CONSTRAINT fk_APPM_PLATFORM_TENANT_MAPPING_APPM_SUPPORTED_PLATFORM1 - FOREIGN KEY (PLATFORM_ID) - REFERENCES APPM_PLATFORM (ID) - ON DELETE NO ACTION - ON UPDATE NO ACTION); - -CREATE INDEX FK_PLATFROM_TENANT_MAPPING_PLATFORM ON APPM_PLATFORM_TENANT_MAPPING(PLATFORM_ID ASC); -- ----------------------------------------------------- diff --git a/features/application-mgt/org.wso2.carbon.device.application.mgt.server.feature/src/main/resources/dbscripts/cdm/application-mgt/mysql.sql b/features/application-mgt/org.wso2.carbon.device.application.mgt.server.feature/src/main/resources/dbscripts/cdm/application-mgt/mysql.sql index a316f290e6..3512f095c0 100644 --- a/features/application-mgt/org.wso2.carbon.device.application.mgt.server.feature/src/main/resources/dbscripts/cdm/application-mgt/mysql.sql +++ b/features/application-mgt/org.wso2.carbon.device.application.mgt.server.feature/src/main/resources/dbscripts/cdm/application-mgt/mysql.sql @@ -14,18 +14,28 @@ SET @OLD_SQL_MODE=@@SQL_MODE, SQL_MODE='TRADITIONAL,ALLOW_INVALID_DATES'; -- ----------------------------------------------------- -- Table `APPM_PLATFORM` -- ----------------------------------------------------- -CREATE TABLE IF NOT EXISTS `APPM_PLATFORM` ( - `ID` INT NOT NULL, - `NAME` VARCHAR(100) NOT NULL, - `DESCRIPTION` TEXT NULL, - `IDENTIFIER` VARCHAR(100) NOT NULL, - `DESCRIPTOR` LONGTEXT NOT NULL, - IS_SHARED BOOLEAN DEFAULT FALSE, - FILE_BASED BOOLEAN DEFAULT FALSE, - PRIMARY KEY (`ID`), - UNIQUE INDEX `IDENTIFIER_UNIQUE` (`IDENTIFIER` ASC)) - ENGINE = InnoDB - COMMENT = 'This table contains the data related to the application platform'; +CREATE TABLE IF NOT EXISTS APPM_PLATFORM ( +ID INT NOT NULL AUTO_INCREMENT UNIQUE, +IDENTIFIER VARCHAR (100) NOT NULL, +TENANT_ID INT NOT NULL , +NAME VARCHAR (255), +FILE_BASED BOOLEAN, +DESCRIPTION VARCHAR(2048), +IS_SHARED BOOLEAN, +ICON_NAME VARCHAR (100), +PRIMARY KEY (IDENTIFIER, TENANT_ID) +); + +CREATE TABLE IF NOT EXISTS APPM_PLATFORM_PROPERTIES ( +ID INT NOT NULL AUTO_INCREMENT UNIQUE, +PLATFORM_ID VARCHAR (100) NOT NULL, +TENANT_ID INT NOT NULL , +PROP_NAME VARCHAR (100) NOT NULL, +OPTIONAL BOOLEAN, +DEFAUL_VALUE VARCHAR (255), +PRIMARY KEY (ID, PLATFORM_ID, PROP_NAME, TENANT_ID), +FOREIGN KEY(PLATFORM_ID, TENANT_ID) REFERENCES APPM_PLATFORM(IDENTIFIER, TENANT_ID) ON DELETE CASCADE +); -- ----------------------------------------------------- From f16b03ffee88179115cfece72a4b48de912c2750 Mon Sep 17 00:00:00 2001 From: megala21 Date: Mon, 31 Jul 2017 17:37:35 +0530 Subject: [PATCH 03/10] Fixing minor issues in closing the database statements and resultsets --- .../mgt/api/beans/ErrorResponse.java | 2 +- .../impl/platform/GenericPlatformDAOImpl.java | 109 +++++++++++------- .../mgt/core/deployer/PlatformDeployer.java | 10 +- .../mgt/core/impl/PlatformManagerImpl.java | 3 +- .../util/ApplicationMgtDatabaseCreator.java | 9 +- .../mgt/core/util/ConnectionManagerUtil.java | 25 ++++ 6 files changed, 108 insertions(+), 50 deletions(-) diff --git a/components/application-mgt/org.wso2.carbon.device.application.mgt.api/src/main/java/org/wso2/carbon/device/application/mgt/api/beans/ErrorResponse.java b/components/application-mgt/org.wso2.carbon.device.application.mgt.api/src/main/java/org/wso2/carbon/device/application/mgt/api/beans/ErrorResponse.java index cfd1408c91..a755b82a4e 100644 --- a/components/application-mgt/org.wso2.carbon.device.application.mgt.api/src/main/java/org/wso2/carbon/device/application/mgt/api/beans/ErrorResponse.java +++ b/components/application-mgt/org.wso2.carbon.device.application.mgt.api/src/main/java/org/wso2/carbon/device/application/mgt/api/beans/ErrorResponse.java @@ -91,4 +91,4 @@ public class ErrorResponse { public void setErrorItems(List error) { this.errorItems = error; } -} \ No newline at end of file +} 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 b7cee5755b..588b29f920 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 @@ -66,13 +66,16 @@ public class GenericPlatformDAOImpl extends AbstractDAOImpl implements PlatformD String insertPlatformProps = "INSERT INTO APPM_PLATFORM_PROPERTIES (PLATFORM_ID, PROP_NAME, OPTIONAL, " + "DEFAULT_VALUE) VALUES ( ? , ?, ? , ?)"; - for (Platform.Property property : platform.getProperties()) { - preparedStatement = connection.prepareStatement(insertPlatformProps); - preparedStatement.setInt(1, platformId); - preparedStatement.setString(2, property.getName()); - preparedStatement.setBoolean(3, property.isOptional()); - preparedStatement.setString(4, property.getDefaultValue()); - preparedStatement.execute(); + + if (platform.getProperties() != null) { + for (Platform.Property property : platform.getProperties()) { + preparedStatement = connection.prepareStatement(insertPlatformProps); + preparedStatement.setInt(1, platformId); + preparedStatement.setString(2, property.getName()); + preparedStatement.setBoolean(3, property.isOptional()); + preparedStatement.setString(4, property.getDefaultValue()); + preparedStatement.execute(); + } } } else { String insertToPlatform = @@ -105,12 +108,14 @@ public class GenericPlatformDAOImpl extends AbstractDAOImpl implements PlatformD throw ex; } catch (DBConnectionException e) { ConnectionManagerUtil.rollbackTransaction(); - throw new PlatformManagementDAOException("Unable to obtain the connection while trying to register the platform - " - + platform.getIdentifier() + " for tenant - " + tenantId, e); + throw new PlatformManagementDAOException( + "Unable to obtain the connection while trying to register the platform - " + platform + .getIdentifier() + " for tenant - " + tenantId, e); } catch (TransactionManagementException e) { ConnectionManagerUtil.rollbackTransaction(); - throw new PlatformManagementDAOException("Error occurred while performing the transaction on the database " + - "for adding the platform - " + platform.getIdentifier() + " , tenant domain - " + tenantId); + throw new PlatformManagementDAOException( + "Error occurred while performing the transaction on the database " + "for adding the platform - " + + platform.getIdentifier() + " , tenant domain - " + tenantId); } finally { ConnectionManagerUtil.closeConnection(); } @@ -119,6 +124,7 @@ public class GenericPlatformDAOImpl extends AbstractDAOImpl implements PlatformD @Override public void update(int tenantId, String oldPlatformIdentifier, Platform platform) throws PlatformManagementDAOException { + PreparedStatement preparedStatement = null; try { ConnectionManagerUtil.beginTransaction(); int platformId = getPlatformId(tenantId, oldPlatformIdentifier); @@ -127,7 +133,7 @@ public class GenericPlatformDAOImpl extends AbstractDAOImpl implements PlatformD if (!platform.isFileBased()) { String insertToPlatform = "UPDATE APPM_PLATFORM SET IDENTIFIER = ?, NAME =?, DESCRIPTION=?, " + "IS_SHARED=?, ICON_NAME=? WHERE ID = ?"; - PreparedStatement preparedStatement = connection.prepareStatement(insertToPlatform); + preparedStatement = connection.prepareStatement(insertToPlatform); preparedStatement.setString(1, platform.getIdentifier()); preparedStatement.setString(2, platform.getName()); preparedStatement.setString(3, platform.getDescription()); @@ -154,7 +160,7 @@ public class GenericPlatformDAOImpl extends AbstractDAOImpl implements PlatformD } } else { String insertToPlatform = "UPDATE APPM_PLATFORM SET IDENTIFIER = ? WHERE ID = ?"; - PreparedStatement preparedStatement = connection.prepareStatement(insertToPlatform); + preparedStatement = connection.prepareStatement(insertToPlatform); preparedStatement.setInt(1, platformId); preparedStatement.execute(); } @@ -181,20 +187,23 @@ public class GenericPlatformDAOImpl extends AbstractDAOImpl implements PlatformD "Error occurred while performing the transaction on the database " + "for adding the platform - " + platform.getIdentifier() + " , tenant domain - " + tenantId); } finally { + ConnectionManagerUtil.cleanupResources(preparedStatement, null); ConnectionManagerUtil.closeConnection(); } } private int getPlatformId(int tenantId, String platformIdentifier) throws PlatformManagementDAOException { + PreparedStatement preparedStatement = null; + ResultSet resultSet = null; String query = "SELECT ID FROM APPM_PLATFORM WHERE (TENANT_ID=? AND IDENTIFIER=?) OR (IS_SHARED = TRUE AND " + "IDENTIFIER=?)"; try { Connection connection = ConnectionManagerUtil.getConnection(); - PreparedStatement preparedStatement = connection.prepareStatement(query); + preparedStatement = connection.prepareStatement(query); preparedStatement.setInt(1, tenantId); preparedStatement.setString(2, platformIdentifier); preparedStatement.setString(3, platformIdentifier); - ResultSet resultSet = preparedStatement.executeQuery(); + resultSet = preparedStatement.executeQuery(); if (resultSet.next()) { return resultSet.getInt("ID"); } @@ -203,19 +212,22 @@ public class GenericPlatformDAOImpl extends AbstractDAOImpl implements PlatformD throw new PlatformManagementDAOException("Error when trying to obtaining the database connection.", e); } catch (SQLException e) { throw new PlatformManagementDAOException("Error in executing the query - " + query, e); + } finally { + ConnectionManagerUtil.cleanupResources(preparedStatement, resultSet); } } @Override public void unregister(int tenantId, String platformIdenfier) throws PlatformManagementDAOException { + PreparedStatement preparedStatement = null; try { ConnectionManagerUtil.beginTransaction(); int platformId = getPlatformId(tenantId, platformIdenfier); if (platformId != -1) { Connection connection = ConnectionManagerUtil.getConnection(); String deletePlatform = "DELETE FROM APPM_PLATFORM WHERE ID = ?"; - PreparedStatement preparedStatement = connection.prepareStatement(deletePlatform); + preparedStatement = connection.prepareStatement(deletePlatform); preparedStatement.setInt(1, platformId); preparedStatement.execute(); ConnectionManagerUtil.commitTransaction(); @@ -240,25 +252,27 @@ public class GenericPlatformDAOImpl extends AbstractDAOImpl implements PlatformD ConnectionManagerUtil.rollbackTransaction(); throw ex; } finally { + ConnectionManagerUtil.cleanupResources(preparedStatement, null); ConnectionManagerUtil.closeConnection(); } } public void addMapping(int tenantId, List platformIdentifiers) throws PlatformManagementDAOException { String insertMapping = "INSERT INTO APPM_PLATFORM_TENANT_MAPPING(TENANT_ID, PLATFORM_ID) VALUES (?, ?)"; + PreparedStatement preparedStatement = null; try { ConnectionManagerUtil.beginTransaction(); for (String platformIdentifier : platformIdentifiers) { if (getTenantPlatformMapping(tenantId, platformIdentifier) == -1) { int platformId = getPlatformId(tenantId, platformIdentifier); Connection connection = ConnectionManagerUtil.getConnection(); - PreparedStatement preparedStatement = connection.prepareStatement(insertMapping); + preparedStatement = connection.prepareStatement(insertMapping); preparedStatement.setInt(1, tenantId); preparedStatement.setInt(2, platformId); preparedStatement.execute(); } else { - throw new PlatformManagementDAOException("Platform identifier - " + platformIdentifier + " is " - + "already assigned to tenant domain - " + tenantId); + log.error("Platform identifier - " + platformIdentifier + " is already assigned to tenant domain" + + " - " + tenantId); } } ConnectionManagerUtil.commitTransaction(); @@ -279,6 +293,7 @@ public class GenericPlatformDAOImpl extends AbstractDAOImpl implements PlatformD ConnectionManagerUtil.rollbackTransaction(); throw ex; } finally { + ConnectionManagerUtil.cleanupResources(preparedStatement, null); ConnectionManagerUtil.closeConnection(); } } @@ -310,12 +325,13 @@ public class GenericPlatformDAOImpl extends AbstractDAOImpl implements PlatformD @Override public void removeMapping(int tenantId, String platformIdentifier) throws PlatformManagementDAOException { String deleteMapping = "DELETE FROM APPM_PLATFORM_TENANT_MAPPING WHERE ID = ?"; + PreparedStatement preparedStatement = null; try { ConnectionManagerUtil.beginTransaction(); int mappingId = getTenantPlatformMapping(tenantId, platformIdentifier); if (mappingId != -1) { Connection connection = ConnectionManagerUtil.getConnection(); - PreparedStatement preparedStatement = connection.prepareStatement(deleteMapping); + preparedStatement = connection.prepareStatement(deleteMapping); preparedStatement.setInt(1, mappingId); preparedStatement.execute(); ConnectionManagerUtil.commitTransaction(); @@ -335,28 +351,31 @@ public class GenericPlatformDAOImpl extends AbstractDAOImpl implements PlatformD ConnectionManagerUtil.rollbackTransaction(); throw ex; } finally { + ConnectionManagerUtil.cleanupResources(preparedStatement, null); ConnectionManagerUtil.closeConnection(); } } @Override public void removeMappingTenants(String platformIdentifier) throws PlatformManagementDAOException { + PreparedStatement preparedStatement = null; int platformId = getPlatformId(MultitenantConstants.SUPER_TENANT_ID, platformIdentifier); String getMapping = "DELETE FROM APPM_PLATFORM_TENANT_MAPPING WHERE TENANT_ID != ? AND PLATFORM_ID=?"; try { ConnectionManagerUtil.openConnection(); Connection connection = ConnectionManagerUtil.getConnection(); - PreparedStatement preparedStatement = connection.prepareStatement(getMapping); + preparedStatement = connection.prepareStatement(getMapping); preparedStatement.setInt(1, MultitenantConstants.SUPER_TENANT_ID); preparedStatement.setInt(2, platformId); preparedStatement.execute(); } catch (DBConnectionException e) { throw new PlatformManagementDAOException( - "Error occured while obtaining the connection to get the existing " + "Tenant - Platform Mapping.", + "Error occurred while obtaining the connection to get the existing " + "Tenant - Platform Mapping.", e); } catch (SQLException e) { - throw new PlatformManagementDAOException("Error occured while executing the SQL query - " + getMapping, e); + throw new PlatformManagementDAOException("Error occurred while executing the SQL query - " + getMapping, e); } finally { + ConnectionManagerUtil.cleanupResources(preparedStatement, null); ConnectionManagerUtil.closeConnection(); } } @@ -364,6 +383,9 @@ public class GenericPlatformDAOImpl extends AbstractDAOImpl implements PlatformD @Override public List getPlatforms(int tenantId) throws PlatformManagementDAOException { + PreparedStatement preparedStatement = null; + ResultSet resultSet = null; + if (log.isDebugEnabled()) { log.debug("GetPlaforms request received for the tenant ID " + tenantId); } @@ -373,9 +395,9 @@ public class GenericPlatformDAOImpl extends AbstractDAOImpl implements PlatformD + "MAPPING ON PLATFORM.ID = MAPPING.PLATFORM_ID"; try { Connection connection = ConnectionManagerUtil.openConnection(); - PreparedStatement preparedStatement = connection.prepareStatement(selectQuery); + preparedStatement = connection.prepareStatement(selectQuery); preparedStatement.setInt(1, tenantId); - ResultSet resultSet = preparedStatement.executeQuery(); + resultSet = preparedStatement.executeQuery(); List platforms = new ArrayList<>(); if (log.isDebugEnabled()) { log.debug("Platform retrieved for the tenant Id " + tenantId); @@ -401,30 +423,33 @@ public class GenericPlatformDAOImpl extends AbstractDAOImpl implements PlatformD return platforms; } catch (DBConnectionException e) { throw new PlatformManagementDAOException( - "Error occured when loading the platforms for tenant - " + tenantId, e); + "Error occurred when loading the platforms for tenant - " + tenantId, e); } catch (SQLException e) { throw new PlatformManagementDAOException("Error occurred when executing query - " + selectQuery, e); } finally { + ConnectionManagerUtil.cleanupResources(preparedStatement,resultSet); ConnectionManagerUtil.closeConnection(); } } - public Platform getPlatform(String tenantDomain, String platformIdenfier) throws PlatformManagementDAOException { + public Platform getPlatform(String tenantDomain, String platformIdentifier) throws PlatformManagementDAOException { + PreparedStatement preparedStatement = null; + ResultSet resultSet = null; String platformQuery = "SELECT * FROM (SELECT * FROM APPM_PLATFORM WHERE (TENANT_DOMAIN=? AND IDENTIFIER=?) " + "OR (IS_SHARED = TRUE AND IDENTIFIER=?) AND FILE_BASED = FALSE ) PLATFORM " + "LEFT JOIN APPM_PLATFORM_PROPERTIES PROPS ON PLATFORM.ID = PROPS.PLATFORM_ID"; try { ConnectionManagerUtil.openConnection(); Connection connection = ConnectionManagerUtil.getConnection(); - PreparedStatement preparedStatement = connection.prepareStatement(platformQuery); + preparedStatement = connection.prepareStatement(platformQuery); preparedStatement.setString(1, tenantDomain); - preparedStatement.setString(2, platformIdenfier); - preparedStatement.setString(3, platformIdenfier); - ResultSet resultSet = preparedStatement.executeQuery(); + preparedStatement.setString(2, platformIdentifier); + preparedStatement.setString(3, platformIdentifier); + resultSet = preparedStatement.executeQuery(); Platform platform = new Platform(); if (resultSet.next()) { platform.setId(resultSet.getInt("PLATFORM.ID")); - platform.setIdentifier(platformIdenfier); + platform.setIdentifier(platformIdentifier); platform.setName(resultSet.getString("PLATFORM.NAME")); platform.setIconName(resultSet.getString("PLATFORM.DESCRIPTION")); platform.setIconName(resultSet.getString("PLATFORM.ICON_NAME")); @@ -442,27 +467,29 @@ public class GenericPlatformDAOImpl extends AbstractDAOImpl implements PlatformD } while (resultSet.next()); platform.setProperties(properties); } else { - platform.setIdentifier(platformIdenfier); + platform.setIdentifier(platformIdentifier); platform.setFileBased(true); } return platform; } catch (DBConnectionException e) { - throw new PlatformManagementDAOException("Error when loading the platform - " + platformIdenfier, e); + throw new PlatformManagementDAOException("Error when loading the platform - " + platformIdentifier, e); } catch (SQLException e) { throw new PlatformManagementDAOException("Error in executing the query - " + platformQuery, e); + } finally { + ConnectionManagerUtil.cleanupResources(preparedStatement, resultSet); + ConnectionManagerUtil.closeConnection(); } } public Platform getPlatform(int tenantId, String identifier) throws PlatformManagementDAOException { - - Connection conn = null; + Connection conn; PreparedStatement stmt = null; ResultSet rs = null; String sql = ""; try { conn = this.getConnection(); - sql += "SELECT * FROM APPM_PLATFORM WHERE IDENTIFIER = ? AND TENANT_ID = ?"; + sql = "SELECT * FROM APPM_PLATFORM WHERE IDENTIFIER = ? AND TENANT_ID = ?"; stmt = conn.prepareStatement(sql); stmt.setString(1, identifier); @@ -484,14 +511,14 @@ public class GenericPlatformDAOImpl extends AbstractDAOImpl implements PlatformD platform.setShared(rs.getBoolean("IS_SHARED")); } } - return platform; - } catch (SQLException e) { - throw new PlatformManagementDAOException("Error occurred while getting application List", e); + throw new PlatformManagementDAOException("Error occurred while getting platform with the identifier " + + identifier + ", for the tenant : " + tenantId, e); } catch (DBConnectionException e) { throw new PlatformManagementDAOException("Error occurred while obtaining the DB connection.", e); + } finally { + ConnectionManagerUtil.cleanupResources(stmt, rs); } } - } 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 51fb6bef4d..27e34a693d 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 @@ -37,6 +37,10 @@ import javax.xml.bind.JAXBContext; import javax.xml.bind.JAXBException; import javax.xml.bind.Unmarshaller; +/** + * PlatformDeployer is responsible for deploying platforms that are added in the filesystem. + * This will deploy the platforms that are added in /repository/deployment/server/platforms directory. + */ public class PlatformDeployer extends AbstractDeployer { private static final Log log = LogFactory.getLog(PlatformDeployer.class); @@ -53,6 +57,7 @@ public class PlatformDeployer extends AbstractDeployer { } } + @Override public void deploy(DeploymentFileData deploymentFileData) throws DeploymentException { File deploymentFile = new File(deploymentFileData.getAbsolutePath()); try { @@ -63,6 +68,7 @@ public class PlatformDeployer extends AbstractDeployer { 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"); } else { log.error("Unable to deploy the platform - " + deploymentFile.getAbsolutePath() + "!. Platform config file name - " + deploymentFile.getName() @@ -75,13 +81,15 @@ public class PlatformDeployer extends AbstractDeployer { } } + @Override public void undeploy(String fileName) throws DeploymentException { String platformId = getPlatformID(fileName); try { DataHolder.getInstance().getPlatformManager() .unregister(CarbonContext.getThreadLocalCarbonContext().getTenantId(), platformId, true); + log.info("Platform configuration : " + fileName + " un-deployed successfully"); } catch (PlatformManagementException e) { - log.error("Error occurred while undeploying the platform - " + fileName); + log.error("Error occurred while un-deploying the platform - " + fileName); } } 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 58cb92355d..4b1bdb273c 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 @@ -226,9 +226,8 @@ public class PlatformManagerImpl implements PlatformManager { if (tenantPlatforms != null) { this.inMemoryStore.remove(identifier); } - } else { - DAOFactory.getPlatformDAO().unregister(tenantId, identifier); } + DAOFactory.getPlatformDAO().unregister(tenantId, identifier); } @Override 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 197b82921f..7c40bfe00c 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 @@ -1,5 +1,5 @@ /* - * Copyright (c) 2016, WSO2 Inc. (http://www.wso2.org) All Rights Reserved. + * 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 @@ -18,14 +18,13 @@ package org.wso2.carbon.device.application.mgt.core.util; -import org.apache.commons.logging.Log; +import java.io.File; + import org.apache.commons.logging.LogFactory; +import org.apache.commons.logging.Log; import org.wso2.carbon.utils.CarbonUtils; import org.wso2.carbon.utils.dbcreator.DatabaseCreator; -import javax.sql.DataSource; -import java.io.File; - /** * 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 664e637901..ae857bbb64 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 @@ -27,6 +27,8 @@ import org.wso2.carbon.device.application.mgt.common.exception.TransactionManage import javax.naming.InitialContext; import javax.sql.DataSource; import java.sql.Connection; +import java.sql.PreparedStatement; +import java.sql.ResultSet; import java.sql.SQLException; public class ConnectionManagerUtil { @@ -185,4 +187,27 @@ public class ConnectionManagerUtil { return null; } + /** + * Cleanup resources used to transaction + * + * @param stmt Prepared statement used + * @param rs Obtained results set + */ + public static void cleanupResources(PreparedStatement stmt, ResultSet rs) { + if (rs != null) { + try { + rs.close(); + } catch (SQLException e) { + log.warn("Error occurred while closing result set", e); + } + } + if (stmt != null) { + try { + stmt.close(); + } catch (SQLException e) { + log.warn("Error occurred while closing prepared statement", e); + } + } + } + } From eda5b5909cf0cdbcd683e3220a2a44677fb7f51f Mon Sep 17 00:00:00 2001 From: megala21 Date: Tue, 1 Aug 2017 14:11:37 +0530 Subject: [PATCH 04/10] Adding platform removal --- .../api/services/PlatformManagementAPI.java | 46 +++ .../impl/PlatformManagementAPIImpl.java | 27 +- .../src/main/webapp/META-INF/permissions.xml | 8 +- .../application/mgt/core/dao/PlatformDAO.java | 2 +- .../application/mgt/core/dao/common/Util.java | 12 +- .../impl/platform/GenericPlatformDAOImpl.java | 212 ++++++------- .../mgt/core/impl/PlatformManagerImpl.java | 298 +++++++++++++----- .../util/ApplicationMgtDatabaseCreator.java | 5 +- .../mgt/core/util/ConnectionManagerUtil.java | 25 -- .../dbscripts/cdm/application-mgt/h2.sql | 7 +- .../dbscripts/cdm/application-mgt/mysql.sql | 11 +- 11 files changed, 407 insertions(+), 246 deletions(-) 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 30679ebeb3..3721326a8d 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 @@ -34,6 +34,7 @@ import org.wso2.carbon.device.application.mgt.common.Platform; import javax.validation.constraints.Size; import javax.ws.rs.Consumes; +import javax.ws.rs.DELETE; import javax.ws.rs.GET; import javax.ws.rs.POST; import javax.ws.rs.PUT; @@ -82,6 +83,12 @@ import javax.ws.rs.core.Response; description = "Update a platform", key = "perm:platform:update", permissions = {"/device-mgt/platform/update"} + ), + @org.wso2.carbon.apimgt.annotations.api.Scope( + name = "Remove a platform", + description = "Remove a platform", + key = "perm:platform:remove", + permissions = {"/device-mgt/platform/remove"} ) } ) @@ -251,4 +258,43 @@ public interface PlatformManagementAPI { String identifier ); + @DELETE + @Path("/{identifier}") + @Produces(MediaType.APPLICATION_JSON) + @Consumes(MediaType.APPLICATION_JSON) + @ApiOperation( + consumes = MediaType.APPLICATION_JSON, + produces = MediaType.APPLICATION_JSON, + httpMethod = "DELETE", + value = "Remove Platform", + notes = "This will remove the relevant platform.", + tags = "Platform Management", + extensions = { + @Extension(properties = { + @ExtensionProperty(name = SCOPE, value = "perm:platform:remove") + }) + } + ) + @ApiResponses( + value = { + @ApiResponse( + code = 200, + message = "OK. \n Successfully updated the platform"), + @ApiResponse( + code = 400, + message = "Bad Request. \n Invalid request parameters passed."), + @ApiResponse( + code = 500, + message = "Internal Server Error. \n Error occurred while getting the platform list.", + response = ErrorResponse.class) + }) + Response removePlatform( + @ApiParam( + name = "identifier", + required = true) + @PathParam("identifier") + @Size(max = 45) + String identifier + ); + } 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 b708b388e2..9db2273ce4 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 @@ -29,6 +29,7 @@ import org.wso2.carbon.device.application.mgt.core.exception.PlatformManagementD import java.util.ArrayList; import java.util.List; import javax.validation.constraints.Size; +import javax.ws.rs.DELETE; import javax.ws.rs.GET; import javax.ws.rs.POST; import javax.ws.rs.PUT; @@ -102,8 +103,12 @@ public class PlatformManagementAPIImpl implements PlatformManagementAPI { Platform platform = APIUtil.getPlatformManager().getPlatform(tenantId, id); return Response.status(Response.Status.OK).entity(platform).build(); } catch (PlatformManagementDAOException e) { + log.error("Error while trying the get 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("Error while trying the get the platform with the identifier : " + id + " for the tenant :" + + tenantId, e); return APIUtil.getResponse(e, Response.Status.NOT_FOUND); } } @@ -118,14 +123,17 @@ public class PlatformManagementAPIImpl implements PlatformManagementAPI { APIUtil.getPlatformManager().register(tenantId, platform); return Response.status(Response.Status.CREATED).build(); } else { - return APIUtil.getResponse("Invxalid payload! Platform ID and names are mandatory fields!", - Response.Status.BAD_REQUEST); + return APIUtil + .getResponse("Invalid payload! Platform 'identifier' and 'name' are mandatory fields!", + Response.Status.BAD_REQUEST); } } else { return APIUtil.getResponse("Invalid payload! Platform needs to be passed as payload!", Response.Status.BAD_REQUEST); } } catch (PlatformManagementException e) { + log.error("Platform Management Exception while trying to add the platform with identifier : " + platform + .getIdentifier() + " for the tenant : " + tenantId, e); return APIUtil.getResponse(e, Response.Status.INTERNAL_SERVER_ERROR); } } @@ -143,4 +151,19 @@ public class PlatformManagementAPIImpl implements PlatformManagementAPI { return APIUtil.getResponse(e, Response.Status.INTERNAL_SERVER_ERROR); } } + + @DELETE + @Path("/{identifier}") + @Override + public Response removePlatform(@PathParam("identifier") @Size(max = 45) String id) { + int tenantId = PrivilegedCarbonContext.getThreadLocalCarbonContext().getTenantId(true); + try { + APIUtil.getPlatformManager().unregister(tenantId, id, false); + return Response.status(Response.Status.OK).build(); + } catch (PlatformManagementException e) { + log.error("Platform Management Exception while trying to un-register the platform with the identifier : " + + id + " for the tenant : " + tenantId, e); + return APIUtil.getResponse(e, Response.Status.INTERNAL_SERVER_ERROR); + } + } } diff --git a/components/application-mgt/org.wso2.carbon.device.application.mgt.api/src/main/webapp/META-INF/permissions.xml b/components/application-mgt/org.wso2.carbon.device.application.mgt.api/src/main/webapp/META-INF/permissions.xml index 4b171e385e..0a4e39c82b 100644 --- a/components/application-mgt/org.wso2.carbon.device.application.mgt.api/src/main/webapp/META-INF/permissions.xml +++ b/components/application-mgt/org.wso2.carbon.device.application.mgt.api/src/main/webapp/META-INF/permissions.xml @@ -63,6 +63,10 @@ /application-mgt/platforms/* PUT - - + + Remove Platform + /device-mgt/platform/remove + /application-mgt/platforms/* + DELETE + 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 55e95ed17e..7311d74025 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 @@ -32,7 +32,7 @@ public interface PlatformDAO { void update(int tenantId, String oldPlatformIdentifier, Platform platform) throws PlatformManagementDAOException; - void unregister(int tenantId, String platformIdentifier) throws PlatformManagementDAOException; + void unregister(int tenantId, String platformIdentifier, boolean isFileBased) throws PlatformManagementDAOException; void addMapping(int tenantId, List platformIdentifiers) 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/common/Util.java b/components/application-mgt/org.wso2.carbon.device.application.mgt.core/src/main/java/org/wso2/carbon/device/application/mgt/core/dao/common/Util.java index f9ce7d288d..aebcd3e22c 100644 --- a/components/application-mgt/org.wso2.carbon.device.application.mgt.core/src/main/java/org/wso2/carbon/device/application/mgt/core/dao/common/Util.java +++ b/components/application-mgt/org.wso2.carbon.device.application.mgt.core/src/main/java/org/wso2/carbon/device/application/mgt/core/dao/common/Util.java @@ -20,17 +20,19 @@ package org.wso2.carbon.device.application.mgt.core.dao.common; import org.apache.commons.logging.Log; import org.apache.commons.logging.LogFactory; -import org.json.JSONArray; import org.json.JSONException; import org.wso2.carbon.device.application.mgt.common.Application; -import org.wso2.carbon.device.application.mgt.common.Platform; import org.wso2.carbon.device.application.mgt.common.Category; +import org.wso2.carbon.device.application.mgt.common.Platform; import org.wso2.carbon.device.application.mgt.core.util.JSONUtil; import java.sql.PreparedStatement; import java.sql.ResultSet; import java.sql.SQLException; -import java.util.*; +import java.util.ArrayList; +import java.util.HashMap; +import java.util.List; +import java.util.Map; public class Util { @@ -93,6 +95,4 @@ public class Util { } } } - - -} \ No newline at end of file +} 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 588b29f920..7fad43a452 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 @@ -24,6 +24,7 @@ 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; @@ -44,15 +45,15 @@ public class GenericPlatformDAOImpl extends AbstractDAOImpl implements PlatformD @Override public int register(int tenantId, Platform platform) throws PlatformManagementDAOException { + PreparedStatement preparedStatement = null; try { - ConnectionManagerUtil.beginTransaction(); int platformId = getPlatformId(tenantId, platform.getIdentifier()); if (platformId == -1) { - Connection connection = ConnectionManagerUtil.getConnection(); + Connection connection = this.getConnection(); if (!platform.isFileBased()) { String insertToPlatform = "INSERT INTO APPM_PLATFORM (IDENTIFIER, TENANT_ID, NAME, FILE_BASED, " + "DESCRIPTION, IS_SHARED, ICON_NAME)" + " VALUES (?, ?, ?, ?, ?, ?, ?)"; - PreparedStatement preparedStatement = connection.prepareStatement(insertToPlatform); + preparedStatement = connection.prepareStatement(insertToPlatform); preparedStatement.setString(1, platform.getIdentifier()); preparedStatement.setInt(2, tenantId); preparedStatement.setString(3, platform.getName()); @@ -80,7 +81,7 @@ public class GenericPlatformDAOImpl extends AbstractDAOImpl implements PlatformD } else { String insertToPlatform = "INSERT INTO APPM_PLATFORM (IDENTIFIER, TENANT_ID, FILE_BASED)" + " VALUES (?, ?, ?)"; - PreparedStatement preparedStatement = connection.prepareStatement(insertToPlatform); + preparedStatement = connection.prepareStatement(insertToPlatform); preparedStatement.setString(1, platform.getIdentifier()); preparedStatement.setInt(2, tenantId); preparedStatement.setBoolean(3, true); @@ -89,35 +90,24 @@ public class GenericPlatformDAOImpl extends AbstractDAOImpl implements PlatformD if (platformId == -1) { platformId = getPlatformId(tenantId, platform.getIdentifier()); } - ConnectionManagerUtil.commitTransaction(); return platformId; } else { if (!platform.isFileBased()) { - ConnectionManagerUtil.rollbackTransaction(); - throw new PlatformManagementDAOException("Platform - " + platform.getIdentifier() - + " is already registered for tenant - " + tenantId); + throw new PlatformManagementDAOException( + "Platform - " + platform.getIdentifier() + " is already registered for tenant - " + + tenantId); } else { return platformId; } } } catch (SQLException e) { - ConnectionManagerUtil.rollbackTransaction(); throw new PlatformManagementDAOException("Error while executing the SQL query. ", e); - } catch (PlatformManagementDAOException ex) { - ConnectionManagerUtil.rollbackTransaction(); - throw ex; } catch (DBConnectionException e) { - ConnectionManagerUtil.rollbackTransaction(); throw new PlatformManagementDAOException( "Unable to obtain the connection while trying to register the platform - " + platform .getIdentifier() + " for tenant - " + tenantId, e); - } catch (TransactionManagementException e) { - ConnectionManagerUtil.rollbackTransaction(); - throw new PlatformManagementDAOException( - "Error occurred while performing the transaction on the database " + "for adding the platform - " - + platform.getIdentifier() + " , tenant domain - " + tenantId); } finally { - ConnectionManagerUtil.closeConnection(); + Util.cleanupResources(preparedStatement, null); } } @@ -126,19 +116,39 @@ public class GenericPlatformDAOImpl extends AbstractDAOImpl implements PlatformD PlatformManagementDAOException { PreparedStatement preparedStatement = null; try { - ConnectionManagerUtil.beginTransaction(); int platformId = getPlatformId(tenantId, oldPlatformIdentifier); + boolean isIdentifierNull = platform.getIdentifier() == null; + boolean isNameNull = platform.getName() == null; + if (platformId != -1) { - Connection connection = ConnectionManagerUtil.getConnection(); + Connection connection = this.getConnection(); if (!platform.isFileBased()) { - String insertToPlatform = "UPDATE APPM_PLATFORM SET IDENTIFIER = ?, NAME =?, DESCRIPTION=?, " - + "IS_SHARED=?, ICON_NAME=? WHERE ID = ?"; + String insertToPlatform = "UPDATE APPM_PLATFORM SET DESCRIPTION=?, IS_SHARED=?, ICON_NAME=?"; + if (!isIdentifierNull) { + insertToPlatform += ",IDENTIFIER = ? "; + } + if (!isNameNull) { + insertToPlatform += ", NAME =?"; + } + insertToPlatform += " WHERE ID = ?"; preparedStatement = connection.prepareStatement(insertToPlatform); - preparedStatement.setString(1, platform.getIdentifier()); - preparedStatement.setString(2, platform.getName()); - preparedStatement.setString(3, platform.getDescription()); - preparedStatement.setBoolean(4, platform.isShared()); - preparedStatement.setString(5, platform.getIconName()); + preparedStatement.setString(1, platform.getDescription()); + preparedStatement.setBoolean(2, platform.isShared()); + preparedStatement.setString(3, platform.getIconName()); + + if (!isIdentifierNull && !isNameNull) { + preparedStatement.setString(4, platform.getIdentifier()); + preparedStatement.setString(5, platform.getName()); + preparedStatement.setInt(6, platformId); + } else if (isIdentifierNull && !isNameNull) { + preparedStatement.setString(4, platform.getName()); + preparedStatement.setInt(5, platformId); + } else if (!isIdentifierNull) { + preparedStatement.setString(4, platform.getIdentifier()); + preparedStatement.setInt(5, platformId); + } else { + preparedStatement.setInt(4, platformId); + } preparedStatement.execute(); platformId = getPlatformId(tenantId, platform.getIdentifier()); @@ -150,13 +160,16 @@ public class GenericPlatformDAOImpl extends AbstractDAOImpl implements PlatformD String insertPlatformProps = "INSERT INTO APPM_PLATFORM_PROPERTIES (PLATFORM_ID, PROP_NAME, OPTIONAL," + " DEFAULT_VALUE) VALUES ( ? , ?, ? , ?)"; - for (Platform.Property property : platform.getProperties()) { - preparedStatement = connection.prepareStatement(insertPlatformProps); - preparedStatement.setInt(1, platformId); - preparedStatement.setString(2, property.getName()); - preparedStatement.setBoolean(3, property.isOptional()); - preparedStatement.setString(4, property.getDefaultValue()); - preparedStatement.execute(); + + if (platform.getProperties() != null) { + for (Platform.Property property : platform.getProperties()) { + preparedStatement = connection.prepareStatement(insertPlatformProps); + preparedStatement.setInt(1, platformId); + preparedStatement.setString(2, property.getName()); + preparedStatement.setBoolean(3, property.isOptional()); + preparedStatement.setString(4, property.getDefaultValue()); + preparedStatement.execute(); + } } } else { String insertToPlatform = "UPDATE APPM_PLATFORM SET IDENTIFIER = ? WHERE ID = ?"; @@ -164,31 +177,19 @@ public class GenericPlatformDAOImpl extends AbstractDAOImpl implements PlatformD preparedStatement.setInt(1, platformId); preparedStatement.execute(); } - ConnectionManagerUtil.commitTransaction(); } else { throw new PlatformManagementDAOException( "Cannot find any platform that was registered with identifier - " + platform.getIdentifier() + " for tenant - " + tenantId); } } catch (SQLException e) { - ConnectionManagerUtil.rollbackTransaction(); throw new PlatformManagementDAOException("Error while executing the SQL query. ", e); - } catch (PlatformManagementDAOException ex) { - ConnectionManagerUtil.rollbackTransaction(); - throw ex; } catch (DBConnectionException e) { - ConnectionManagerUtil.rollbackTransaction(); throw new PlatformManagementDAOException( "Unable to obtain the connection while trying to register the platform - " + platform .getIdentifier() + " for tenant - " + tenantId, e); - } catch (TransactionManagementException e) { - ConnectionManagerUtil.rollbackTransaction(); - throw new PlatformManagementDAOException( - "Error occurred while performing the transaction on the database " + "for adding the platform - " - + platform.getIdentifier() + " , tenant domain - " + tenantId); } finally { - ConnectionManagerUtil.cleanupResources(preparedStatement, null); - ConnectionManagerUtil.closeConnection(); + Util.cleanupResources(preparedStatement, null); } } @@ -198,7 +199,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 = ConnectionManagerUtil.getConnection(); + Connection connection = this.getConnection(); preparedStatement = connection.prepareStatement(query); preparedStatement.setInt(1, tenantId); preparedStatement.setString(2, platformIdentifier); @@ -213,47 +214,46 @@ public class GenericPlatformDAOImpl extends AbstractDAOImpl implements PlatformD } catch (SQLException e) { throw new PlatformManagementDAOException("Error in executing the query - " + query, e); } finally { - ConnectionManagerUtil.cleanupResources(preparedStatement, resultSet); + Util.cleanupResources(preparedStatement, resultSet); } } @Override - public void unregister(int tenantId, String platformIdenfier) throws PlatformManagementDAOException { + public void unregister(int tenantId, String platformIdenfier, boolean isFileBased) throws + PlatformManagementDAOException { PreparedStatement preparedStatement = null; try { - ConnectionManagerUtil.beginTransaction(); - int platformId = getPlatformId(tenantId, platformIdenfier); - if (platformId != -1) { - Connection connection = ConnectionManagerUtil.getConnection(); - String deletePlatform = "DELETE FROM APPM_PLATFORM WHERE ID = ?"; - preparedStatement = connection.prepareStatement(deletePlatform); - preparedStatement.setInt(1, platformId); - preparedStatement.execute(); - ConnectionManagerUtil.commitTransaction(); + Platform platform = getPlatform(tenantId, platformIdenfier); + + if (platform != null) { + if (isFileBased == platform.isFileBased()) { + Connection connection = this.getConnection(); + String deletePlatform = "DELETE FROM APPM_PLATFORM WHERE ID = ?"; + preparedStatement = connection.prepareStatement(deletePlatform); + preparedStatement.setInt(1, platform.getId()); + preparedStatement.execute(); + } else { + if (isFileBased) { + throw new PlatformManagementDAOException("Platform with identifier - " + platformIdenfier + + " is not a file based platform. Try to remove that using PlatformManagement APIs"); + } else { + throw new PlatformManagementDAOException("Platform with identifier - " + platformIdenfier + + " is a file based platform. Try to remove that by un-deploying the relevant file."); + } + } } else { - throw new PlatformManagementDAOException("Platform identifier - " + platformIdenfier - + " is already unregistered registered for tenant - " + tenantId); + throw new PlatformManagementDAOException( + "Platform identifier - " + platformIdenfier + " is not registered for tenant - " + tenantId); } - } catch (TransactionManagementException e) { - ConnectionManagerUtil.rollbackTransaction(); - throw new PlatformManagementDAOException( - "Unable to start the transaction while trying to register the platform - " + platformIdenfier - + " for tenant - " + tenantId, e); } catch (DBConnectionException e) { - ConnectionManagerUtil.rollbackTransaction(); throw new PlatformManagementDAOException( "Unable to obtain the connection while trying to register the platform - " + platformIdenfier + " for tenant - " + tenantId, e); } catch (SQLException e) { - ConnectionManagerUtil.rollbackTransaction(); throw new PlatformManagementDAOException("Error while executing the SQL query. ", e); - } catch (PlatformManagementDAOException ex) { - ConnectionManagerUtil.rollbackTransaction(); - throw ex; } finally { - ConnectionManagerUtil.cleanupResources(preparedStatement, null); - ConnectionManagerUtil.closeConnection(); + Util.cleanupResources(preparedStatement, null); } } @@ -261,11 +261,10 @@ public class GenericPlatformDAOImpl extends AbstractDAOImpl implements PlatformD String insertMapping = "INSERT INTO APPM_PLATFORM_TENANT_MAPPING(TENANT_ID, PLATFORM_ID) VALUES (?, ?)"; PreparedStatement preparedStatement = null; try { - ConnectionManagerUtil.beginTransaction(); for (String platformIdentifier : platformIdentifiers) { if (getTenantPlatformMapping(tenantId, platformIdentifier) == -1) { int platformId = getPlatformId(tenantId, platformIdentifier); - Connection connection = ConnectionManagerUtil.getConnection(); + Connection connection = this.getConnection(); preparedStatement = connection.prepareStatement(insertMapping); preparedStatement.setInt(1, tenantId); preparedStatement.setInt(2, platformId); @@ -275,40 +274,30 @@ public class GenericPlatformDAOImpl extends AbstractDAOImpl implements PlatformD + " - " + tenantId); } } - ConnectionManagerUtil.commitTransaction(); - } catch (TransactionManagementException e) { - ConnectionManagerUtil.rollbackTransaction(); - throw new PlatformManagementDAOException( - "Error occured while trying to add the mapping of platform - " + platformIdentifiers.toString() - + " for tenant - " + tenantId, e); } catch (DBConnectionException e) { - ConnectionManagerUtil.rollbackTransaction(); throw new PlatformManagementDAOException("Error occurred when getting the connection for the database. ", e); } catch (SQLException e) { - ConnectionManagerUtil.rollbackTransaction(); throw new PlatformManagementDAOException("Error occured while executing the SQL query - " + insertMapping, e); - } catch (PlatformManagementDAOException ex) { - ConnectionManagerUtil.rollbackTransaction(); - throw ex; } finally { - ConnectionManagerUtil.cleanupResources(preparedStatement, null); - ConnectionManagerUtil.closeConnection(); + Util.cleanupResources(preparedStatement, null); } } private int getTenantPlatformMapping(int tenantId, String platformIdentifier) throws PlatformManagementDAOException { + PreparedStatement preparedStatement = null; + ResultSet resultSet = null; String getMapping = "SELECT MAPPING.ID as ID FROM (SELECT ID, PLATFORM_ID FROM APPM_PLATFORM_TENANT_MAPPING " + "WHERE TENANT_ID=?) MAPPING JOIN (SELECT ID FROM APPM_PLATFORM WHERE APPM_PLATFORM.IDENTIFIER=?) " + "PLATFORM ON MAPPING.PLATFORM_ID=PLATFORM.ID"; try { - Connection connection = ConnectionManagerUtil.getConnection(); - PreparedStatement preparedStatement = connection.prepareStatement(getMapping); + Connection connection = this.getConnection(); + preparedStatement = connection.prepareStatement(getMapping); preparedStatement.setInt(1, tenantId); preparedStatement.setString(2, platformIdentifier); - ResultSet resultSet = preparedStatement.executeQuery(); + resultSet = preparedStatement.executeQuery(); if (resultSet.next()) { return resultSet.getInt("ID"); } @@ -319,6 +308,8 @@ public class GenericPlatformDAOImpl extends AbstractDAOImpl implements PlatformD e); } catch (SQLException e) { throw new PlatformManagementDAOException("Error occured while executing the SQL query - " + getMapping, e); + } finally { + Util.cleanupResources(preparedStatement, resultSet); } } @@ -327,32 +318,24 @@ public class GenericPlatformDAOImpl extends AbstractDAOImpl implements PlatformD String deleteMapping = "DELETE FROM APPM_PLATFORM_TENANT_MAPPING WHERE ID = ?"; PreparedStatement preparedStatement = null; try { - ConnectionManagerUtil.beginTransaction(); int mappingId = getTenantPlatformMapping(tenantId, platformIdentifier); if (mappingId != -1) { - Connection connection = ConnectionManagerUtil.getConnection(); + Connection connection = this.getConnection(); preparedStatement = connection.prepareStatement(deleteMapping); preparedStatement.setInt(1, mappingId); preparedStatement.execute(); - ConnectionManagerUtil.commitTransaction(); } else { throw new PlatformManagementDAOException( "Platform - " + platformIdentifier + " is already unassigned for tenant - " + tenantId); } - } catch (TransactionManagementException | DBConnectionException e) { - ConnectionManagerUtil.rollbackTransaction(); + } catch (DBConnectionException e) { throw new PlatformManagementDAOException( "Error occurred while unassigning the platform - " + platformIdentifier + " for tenant - " + tenantId); } catch (SQLException e) { - ConnectionManagerUtil.rollbackTransaction(); throw new PlatformManagementDAOException("Error occurred while executing the query - " + deleteMapping); - } catch (PlatformManagementDAOException ex) { - ConnectionManagerUtil.rollbackTransaction(); - throw ex; } finally { - ConnectionManagerUtil.cleanupResources(preparedStatement, null); - ConnectionManagerUtil.closeConnection(); + Util.cleanupResources(preparedStatement, null); } } @@ -362,21 +345,19 @@ 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 { - ConnectionManagerUtil.openConnection(); - Connection connection = ConnectionManagerUtil.getConnection(); + Connection connection = this.getConnection(); preparedStatement = connection.prepareStatement(getMapping); preparedStatement.setInt(1, MultitenantConstants.SUPER_TENANT_ID); preparedStatement.setInt(2, platformId); preparedStatement.execute(); } catch (DBConnectionException e) { throw new PlatformManagementDAOException( - "Error occurred while obtaining the connection to get the existing " + "Tenant - Platform Mapping.", - e); + "Error occurred while obtaining the connection to remove existing " + "Tenant - Platform Mapping" + + " for the platform : " + platformIdentifier, e); } catch (SQLException e) { throw new PlatformManagementDAOException("Error occurred while executing the SQL query - " + getMapping, e); } finally { - ConnectionManagerUtil.cleanupResources(preparedStatement, null); - ConnectionManagerUtil.closeConnection(); + Util.cleanupResources(preparedStatement, null); } } @@ -394,7 +375,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 = ConnectionManagerUtil.openConnection(); + Connection connection = this.getConnection(); preparedStatement = connection.prepareStatement(selectQuery); preparedStatement.setInt(1, tenantId); resultSet = preparedStatement.executeQuery(); @@ -427,8 +408,7 @@ public class GenericPlatformDAOImpl extends AbstractDAOImpl implements PlatformD } catch (SQLException e) { throw new PlatformManagementDAOException("Error occurred when executing query - " + selectQuery, e); } finally { - ConnectionManagerUtil.cleanupResources(preparedStatement,resultSet); - ConnectionManagerUtil.closeConnection(); + Util.cleanupResources(preparedStatement,resultSet); } } @@ -439,8 +419,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 { - ConnectionManagerUtil.openConnection(); - Connection connection = ConnectionManagerUtil.getConnection(); + Connection connection = this.getConnection(); preparedStatement = connection.prepareStatement(platformQuery); preparedStatement.setString(1, tenantDomain); preparedStatement.setString(2, platformIdentifier); @@ -476,8 +455,7 @@ public class GenericPlatformDAOImpl extends AbstractDAOImpl implements PlatformD } catch (SQLException e) { throw new PlatformManagementDAOException("Error in executing the query - " + platformQuery, e); } finally { - ConnectionManagerUtil.cleanupResources(preparedStatement, resultSet); - ConnectionManagerUtil.closeConnection(); + Util.cleanupResources(preparedStatement, resultSet); } } @@ -518,7 +496,7 @@ public class GenericPlatformDAOImpl extends AbstractDAOImpl implements PlatformD } catch (DBConnectionException e) { throw new PlatformManagementDAOException("Error occurred while obtaining the DB connection.", e); } finally { - ConnectionManagerUtil.cleanupResources(stmt, rs); + Util.cleanupResources(stmt, rs); } } } 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 4b1bdb273c..2e4551eb86 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 @@ -20,10 +20,14 @@ package org.wso2.carbon.device.application.mgt.core.impl; 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.PlatformManagementException; +import org.wso2.carbon.device.application.mgt.common.exception.TransactionManagementException; import org.wso2.carbon.device.application.mgt.common.services.PlatformManager; import org.wso2.carbon.device.application.mgt.core.dao.common.DAOFactory; +import org.wso2.carbon.device.application.mgt.core.exception.PlatformManagementDAOException; import org.wso2.carbon.device.application.mgt.core.internal.DataHolder; +import org.wso2.carbon.device.application.mgt.core.util.ConnectionManagerUtil; import org.wso2.carbon.user.api.Tenant; import org.wso2.carbon.user.api.TenantManager; import org.wso2.carbon.user.api.UserStoreException; @@ -47,25 +51,49 @@ public class PlatformManagerImpl implements PlatformManager { @Override public void initialize(int tenantId) throws PlatformManagementException { - List platforms = DAOFactory.getPlatformDAO().getPlatforms(tenantId); - List platformIdentifiers = new ArrayList<>(); - for (Platform platform : platforms) { - if (!platform.isEnabled() & platform.isDefaultTenantMapping()) { - platformIdentifiers.add(platform.getIdentifier()); + try { + ConnectionManagerUtil.beginTransaction(); + List platforms = DAOFactory.getPlatformDAO().getPlatforms(tenantId); + List platformIdentifiers = new ArrayList<>(); + for (Platform platform : platforms) { + if (!platform.isEnabled() & platform.isDefaultTenantMapping()) { + platformIdentifiers.add(platform.getIdentifier()); + } } + DAOFactory.getPlatformDAO().addMapping(tenantId, platformIdentifiers); + ConnectionManagerUtil.commitTransaction(); + } catch (TransactionManagementException e) { + ConnectionManagerUtil.rollbackTransaction(); + throw new PlatformManagementDAOException( + "Transaction Management Exception while initializing the " + "platforms for the tenant : " + + tenantId, e); + } catch (DBConnectionException e) { + ConnectionManagerUtil.rollbackTransaction(); + throw new PlatformManagementDAOException( + "Database Connection Exception while initializing the " + "platforms for the tenant : " + tenantId, + e); + } finally { + ConnectionManagerUtil.closeConnection(); } - addMapping(tenantId, platformIdentifiers); } @Override public List getPlatforms(int tenantId) throws PlatformManagementException { + int platformIndex = 0; + List platforms; if (log.isDebugEnabled()) { log.debug("Request for getting platforms received for the tenant ID " + tenantId + " at " + "PlatformManager level"); } - List platforms = DAOFactory.getPlatformDAO().getPlatforms(tenantId); - int platformIndex = 0; - + try { + ConnectionManagerUtil.openConnection(); + platforms = DAOFactory.getPlatformDAO().getPlatforms(tenantId); + } catch (DBConnectionException e) { + throw new PlatformManagementDAOException( + "Database Connection Exception while getting the platforms for the tenant : " + tenantId, e); + } finally { + ConnectionManagerUtil.closeConnection(); + } if (log.isDebugEnabled()) { log.debug("Number of platforms received from DAO layer is " + platforms.size() + " for the tenant " + tenantId); @@ -90,8 +118,7 @@ public class PlatformManagerImpl implements PlatformManager { platformIndex++; } if (log.isDebugEnabled()) { - log.debug("Number of effective platforms for the tenant " + tenantId - + " : " + platforms.size()); + log.debug("Number of effective platforms for the tenant " + tenantId + " : " + platforms.size()); } return platforms; } @@ -100,15 +127,24 @@ public class PlatformManagerImpl implements PlatformManager { public Platform getPlatform(int tenantId, String identifier) throws PlatformManagementException { Platform platform = getPlatformFromInMemory(tenantId, identifier); if (platform == null) { - platform = DAOFactory.getPlatformDAO().getPlatform(tenantId, identifier); - if (platform != null) { - return platform; + try { + ConnectionManagerUtil.openConnection(); + platform = DAOFactory.getPlatformDAO().getPlatform(tenantId, identifier); + if (platform != null) { + return platform; + } + } catch (DBConnectionException e) { + throw new PlatformManagementDAOException( + "Database Connection Exception while trying to get the " + "platform with the id :" + identifier + + " for the tenant : " + tenantId, e); + } finally { + ConnectionManagerUtil.closeConnection(); } } else { return new Platform(platform); } - throw new PlatformManagementException("No platform was found for tenant - " + tenantId + - " with Platform identifier - " + identifier); + throw new PlatformManagementException( + "No platform was found for tenant - " + tenantId + " with platform identifier - " + identifier); } private Platform getPlatformFromInMemory(int tenantId, String identifier) { @@ -138,112 +174,212 @@ public class PlatformManagerImpl implements PlatformManager { "Platform sharing is a restricted operation, therefore Platform - " + platform.getIdentifier() + " cannot be shared by the tenant domain - " + tenantId); } - int platformId = DAOFactory.getPlatformDAO().register(tenantId, platform); - if (platform.isFileBased()) { - platform.setId(platformId); - Map tenantPlatforms = this.inMemoryStore.get(tenantId); - if (tenantPlatforms == null) { - tenantPlatforms = new HashMap<>(); - this.inMemoryStore.put(tenantId, tenantPlatforms); - } - if (tenantPlatforms.get(platform.getIdentifier()) == null) { - tenantPlatforms.put(platform.getIdentifier(), platform); - } else { - throw new PlatformManagementException( - "Platform - " + platform.getIdentifier() + " is already registered!"); + try { + ConnectionManagerUtil.beginTransaction(); + int platformId = DAOFactory.getPlatformDAO().register(tenantId, platform); + if (platform.isFileBased()) { + platform.setId(platformId); + Map tenantPlatforms = this.inMemoryStore.get(tenantId); + if (tenantPlatforms == null) { + tenantPlatforms = new HashMap<>(); + this.inMemoryStore.put(tenantId, tenantPlatforms); + } + if (tenantPlatforms.get(platform.getIdentifier()) == null) { + tenantPlatforms.put(platform.getIdentifier(), platform); + } else { + ConnectionManagerUtil.rollbackTransaction(); + throw new PlatformManagementException( + "Platform - " + platform.getIdentifier() + " is already registered!"); + } } - } - if (platform.isDefaultTenantMapping()) { - try { - if (platform.isShared()) { - TenantManager tenantManager = DataHolder.getInstance().getRealmService().getTenantManager(); - Tenant[] tenants = tenantManager.getAllTenants(); - for (Tenant tenant : tenants) { - addMapping(tenant.getId(), platform.getIdentifier()); + if (platform.isDefaultTenantMapping()) { + try { + if (platform.isShared()) { + TenantManager tenantManager = DataHolder.getInstance().getRealmService().getTenantManager(); + Tenant[] tenants = tenantManager.getAllTenants(); + for (Tenant tenant : tenants) { + DAOFactory.getPlatformDAO() + .addMapping(tenant.getId(), getListOfString(platform.getIdentifier())); + } } + DAOFactory.getPlatformDAO().addMapping(tenantId, getListOfString(platform.getIdentifier())); + } catch (UserStoreException e) { + ConnectionManagerUtil.rollbackTransaction(); + throw new PlatformManagementException("Error occurred while assigning the platforms for tenants!", + e); } - addMapping(tenantId, platform.getIdentifier()); - } catch (UserStoreException e) { - throw new PlatformManagementException("Error occured while assigning the platforms for tenants!", e); } + ConnectionManagerUtil.commitTransaction(); + } catch (TransactionManagementException e) { + ConnectionManagerUtil.rollbackTransaction(); + throw new PlatformManagementDAOException( + "Transaction Management Exception while trying to register a " + "platform with id " + platform + .getIdentifier() + " for tenant " + tenantId); + } catch (DBConnectionException e) { + ConnectionManagerUtil.rollbackTransaction(); + throw new PlatformManagementDAOException( + "Database Connection Exception while trying to register a " + "platform with id " + platform + .getIdentifier() + " for tenant " + tenantId); + } finally { + ConnectionManagerUtil.closeConnection(); } } @Override - public void update(int tenantId, String oldPlatformIdentifier, Platform platform) - throws PlatformManagementException { + public void update(int tenantId, String oldPlatformIdentifier, Platform platform) throws + PlatformManagementException { if (platform.isShared() && tenantId != MultitenantConstants.SUPER_TENANT_ID) { throw new PlatformManagementException( "Platform sharing is a restricted operation, therefore Platform - " + platform.getIdentifier() + " cannot be shared by the tenant domain - " + tenantId); } Platform oldPlatform; - if (platform.isFileBased()) { - Map tenantPlatforms = this.inMemoryStore.get(tenantId); - if (tenantPlatforms == null) { + + if (platform.getIdentifier() != null && !platform.getIdentifier().equals(oldPlatformIdentifier)) { + try { + ConnectionManagerUtil.openConnection(); + Platform existingPlatform = DAOFactory.getPlatformDAO().getPlatform(tenantId, platform.getIdentifier()); + + if (existingPlatform != null) { + 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( - "No platforms registered for the tenant - " + tenantId + " with platform identifier - " - + platform.getIdentifier()); + "Database Connection Exception while trying to update the " + "platform for the tenant : " + + tenantId, e); + } finally { + ConnectionManagerUtil.closeConnection(); } - oldPlatform = tenantPlatforms.get(oldPlatformIdentifier); - if (oldPlatform == null) { - throw new PlatformManagementException( - "No platforms registered for the tenant - " + tenantId + " with platform identifier - " - + platform.getIdentifier()); + } + try { + if (platform.isFileBased()) { + Map tenantPlatforms = this.inMemoryStore.get(tenantId); + if (tenantPlatforms == null) { + throw new PlatformManagementException( + "No platforms registered for the tenant - " + tenantId + " with platform identifier - " + + platform.getIdentifier()); + } + 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()); + tenantPlatforms.put(platform.getIdentifier(), platform); + } } else { + ConnectionManagerUtil.beginTransaction(); + oldPlatform = DAOFactory.getPlatformDAO().getPlatform(tenantId, oldPlatformIdentifier); DAOFactory.getPlatformDAO().update(tenantId, oldPlatformIdentifier, platform); - platform.setId(oldPlatform.getId()); - tenantPlatforms.put(platform.getIdentifier(), platform); } - } else { - oldPlatform = DAOFactory.getPlatformDAO().getPlatform(tenantId, oldPlatformIdentifier); - DAOFactory.getPlatformDAO().update(tenantId, oldPlatformIdentifier, platform); - } - if (platform.isDefaultTenantMapping() && !oldPlatform.isDefaultTenantMapping()) { - try { - if (platform.isShared() && !oldPlatform.isShared()) { - TenantManager tenantManager = DataHolder.getInstance().getRealmService().getTenantManager(); - Tenant[] tenants = tenantManager.getAllTenants(); - for (Tenant tenant : tenants) { - addMapping(tenant.getId(), platform.getIdentifier()); + if (platform.isDefaultTenantMapping() && !oldPlatform.isDefaultTenantMapping()) { + try { + if (platform.isShared() && !oldPlatform.isShared()) { + TenantManager tenantManager = DataHolder.getInstance().getRealmService().getTenantManager(); + Tenant[] tenants = tenantManager.getAllTenants(); + for (Tenant tenant : tenants) { + DAOFactory.getPlatformDAO() + .addMapping(tenant.getId(), getListOfString(platform.getIdentifier())); + + } } + DAOFactory.getPlatformDAO().addMapping(tenantId, getListOfString(platform.getIdentifier())); + } catch (UserStoreException e) { + throw new PlatformManagementException("Error occurred while assigning the platforms for tenants!", + e); } - addMapping(tenantId, platform.getIdentifier()); - } catch (UserStoreException e) { - throw new PlatformManagementException("Error occurred while assigning the platforms for tenants!", e); } + if (!platform.isShared() && oldPlatform.isShared()) { + DAOFactory.getPlatformDAO().removeMappingTenants(platform.getIdentifier()); + } + ConnectionManagerUtil.commitTransaction(); + } catch (TransactionManagementException e) { + ConnectionManagerUtil.rollbackTransaction(); + throw new PlatformManagementDAOException( + "Transaction Management Exception while trying to update " + "platform : " + oldPlatformIdentifier + + " of tenant :" + tenantId); + } catch (DBConnectionException e) { + ConnectionManagerUtil.rollbackTransaction(); + throw new PlatformManagementDAOException( + "Database Connection Exception while trying to update " + "platform : " + oldPlatformIdentifier + + " of tenant :" + tenantId); + } finally { + ConnectionManagerUtil.closeConnection(); } - if (!platform.isShared() && oldPlatform.isShared()) { - DAOFactory.getPlatformDAO().removeMappingTenants(platform.getIdentifier()); - } + } + + private List getListOfString(String platformIdentifier) { + List identifiers = new ArrayList<>(); + identifiers.add(platformIdentifier); + return identifiers; } @Override - public void unregister(int tenantId, String identifier, boolean isFileBased) throws - PlatformManagementException { - if (isFileBased) { - Map tenantPlatforms = this.inMemoryStore.get(tenantId); - if (tenantPlatforms != null) { - this.inMemoryStore.remove(identifier); + public void unregister(int tenantId, String identifier, boolean isFileBased) throws PlatformManagementException { + try { + ConnectionManagerUtil.beginTransaction(); + DAOFactory.getPlatformDAO().unregister(tenantId, identifier, isFileBased); + + if (isFileBased) { + Map tenantPlatforms = this.inMemoryStore.get(tenantId); + if (tenantPlatforms != null) { + tenantPlatforms.remove(identifier); + } } + ConnectionManagerUtil.commitTransaction(); + } catch (TransactionManagementException e) { + ConnectionManagerUtil.rollbackTransaction(); + throw new PlatformManagementDAOException( + "Transaction Management Exception while trying to un-register " + "the platform with identifier : " + + identifier + " tenant :" + tenantId, e); + } catch (DBConnectionException e) { + ConnectionManagerUtil.rollbackTransaction(); + throw new PlatformManagementDAOException( + "Database Connection Exception while trying to un-register " + "the platform with identifier : " + + identifier + " tenant :" + tenantId, e); + } finally { + ConnectionManagerUtil.closeConnection(); } - DAOFactory.getPlatformDAO().unregister(tenantId, identifier); } @Override public void addMapping(int tenantId, List platformIdentifiers) throws PlatformManagementException { - DAOFactory.getPlatformDAO().addMapping(tenantId, platformIdentifiers); + try { + ConnectionManagerUtil.openConnection(); + DAOFactory.getPlatformDAO().addMapping(tenantId, platformIdentifiers); + } catch (DBConnectionException e) { + throw new PlatformManagementDAOException( + "Database Connection Exception while trying to add tenant " + "mapping for tenant ID : " + + tenantId); + } finally { + ConnectionManagerUtil.closeConnection(); + } } @Override public void addMapping(int tenantId, String platformIdentifier) throws PlatformManagementException { List identifiers = new ArrayList<>(); identifiers.add(platformIdentifier); - DAOFactory.getPlatformDAO().addMapping(tenantId, identifiers); + addMapping(tenantId, identifiers); } @Override public void removeMapping(int tenantId, String platformIdentifier) throws PlatformManagementException { - DAOFactory.getPlatformDAO().removeMapping(tenantId, platformIdentifier); + try { + ConnectionManagerUtil.openConnection(); + DAOFactory.getPlatformDAO().removeMapping(tenantId, platformIdentifier); + } catch (DBConnectionException e) { + throw new PlatformManagementDAOException( + "Database Connection Exception while trying to remove tenant mapping for tenant ID : " + tenantId); + } finally { + ConnectionManagerUtil.closeConnection(); + } } } 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 7c40bfe00c..1fe8961a5b 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 @@ -20,11 +20,12 @@ package org.wso2.carbon.device.application.mgt.core.util; import java.io.File; -import org.apache.commons.logging.LogFactory; -import org.apache.commons.logging.Log; 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 ae857bbb64..664e637901 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 @@ -27,8 +27,6 @@ import org.wso2.carbon.device.application.mgt.common.exception.TransactionManage import javax.naming.InitialContext; import javax.sql.DataSource; import java.sql.Connection; -import java.sql.PreparedStatement; -import java.sql.ResultSet; import java.sql.SQLException; public class ConnectionManagerUtil { @@ -187,27 +185,4 @@ public class ConnectionManagerUtil { return null; } - /** - * Cleanup resources used to transaction - * - * @param stmt Prepared statement used - * @param rs Obtained results set - */ - public static void cleanupResources(PreparedStatement stmt, ResultSet rs) { - if (rs != null) { - try { - rs.close(); - } catch (SQLException e) { - log.warn("Error occurred while closing result set", e); - } - } - if (stmt != null) { - try { - stmt.close(); - } catch (SQLException e) { - log.warn("Error occurred while closing prepared statement", e); - } - } - } - } 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 06c22b4750..6ff557d534 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 @@ -19,13 +19,12 @@ PRIMARY KEY (IDENTIFIER, TENANT_ID) CREATE TABLE IF NOT EXISTS APPM_PLATFORM_PROPERTIES ( ID INT NOT NULL AUTO_INCREMENT, -PLATFORM_ID VARCHAR (100) NOT NULL, +PLATFORM_ID INT NOT NULL, PROP_NAME VARCHAR (100) NOT NULL, OPTIONAL BOOLEAN, DEFAUL_VALUE VARCHAR (255), -TENANT_ID INT NOT NULL , -FOREIGN KEY(PLATFORM_ID, TENANT_ID) REFERENCES APPM_PLATFORM(IDENTIFIER, TENANT_ID) ON DELETE CASCADE, -PRIMARY KEY (ID, PLATFORM_ID, PROP_NAME, TENANT_ID) +FOREIGN KEY(PLATFORM_ID) REFERENCES APPM_PLATFORM(ID) ON DELETE CASCADE, +PRIMARY KEY (ID, PLATFORM_ID, PROP_NAME) ); CREATE TABLE IF NOT EXISTS APPM_PLATFORM_TENANT_MAPPING ( diff --git a/features/application-mgt/org.wso2.carbon.device.application.mgt.server.feature/src/main/resources/dbscripts/cdm/application-mgt/mysql.sql b/features/application-mgt/org.wso2.carbon.device.application.mgt.server.feature/src/main/resources/dbscripts/cdm/application-mgt/mysql.sql index 3512f095c0..6809473a8a 100644 --- a/features/application-mgt/org.wso2.carbon.device.application.mgt.server.feature/src/main/resources/dbscripts/cdm/application-mgt/mysql.sql +++ b/features/application-mgt/org.wso2.carbon.device.application.mgt.server.feature/src/main/resources/dbscripts/cdm/application-mgt/mysql.sql @@ -20,21 +20,20 @@ IDENTIFIER VARCHAR (100) NOT NULL, TENANT_ID INT NOT NULL , NAME VARCHAR (255), FILE_BASED BOOLEAN, -DESCRIPTION VARCHAR(2048), +DESCRIPTION VARCHAR (2048), IS_SHARED BOOLEAN, ICON_NAME VARCHAR (100), PRIMARY KEY (IDENTIFIER, TENANT_ID) ); CREATE TABLE IF NOT EXISTS APPM_PLATFORM_PROPERTIES ( -ID INT NOT NULL AUTO_INCREMENT UNIQUE, -PLATFORM_ID VARCHAR (100) NOT NULL, -TENANT_ID INT NOT NULL , +ID INT NOT NULL AUTO_INCREMENT, +PLATFORM_ID INT NOT NULL, PROP_NAME VARCHAR (100) NOT NULL, OPTIONAL BOOLEAN, DEFAUL_VALUE VARCHAR (255), -PRIMARY KEY (ID, PLATFORM_ID, PROP_NAME, TENANT_ID), -FOREIGN KEY(PLATFORM_ID, TENANT_ID) REFERENCES APPM_PLATFORM(IDENTIFIER, TENANT_ID) ON DELETE CASCADE +FOREIGN KEY(PLATFORM_ID) REFERENCES APPM_PLATFORM(ID) ON DELETE CASCADE, +PRIMARY KEY (ID, PLATFORM_ID, PROP_NAME) ); From 7d191d63c5bd5d5956a66a8a42ce030c4bd53aac Mon Sep 17 00:00:00 2001 From: megala21 Date: Tue, 1 Aug 2017 15:55:46 +0530 Subject: [PATCH 05/10] 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 7fad43a452..dc8d866b15 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 27e34a693d..67ec78f448 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 2e4551eb86..83c85cb006 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); } From 9bc069a95794cdff3a5d63ee28101e199f04ff60 Mon Sep 17 00:00:00 2001 From: megala21 Date: Wed, 2 Aug 2017 17:12:07 +0530 Subject: [PATCH 06/10] 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) ); From ec0be0183e2d07fccf316d62f21640bd54c76857 Mon Sep 17 00:00:00 2001 From: megala21 Date: Thu, 3 Aug 2017 22:15:02 +0530 Subject: [PATCH 07/10] Re-structuring and adding changes after testing --- .../impl/PlatformManagementAPIImpl.java | 4 + .../application/mgt/common/Platform.java | 2 + .../application/mgt/core/dao/PlatformDAO.java | 6 + .../impl/platform/GenericPlatformDAOImpl.java | 209 ++++++++++++----- .../mgt/core/impl/PlatformManagerImpl.java | 219 ++++++++++++------ .../dbscripts/cdm/application-mgt/h2.sql | 1 + .../dbscripts/cdm/application-mgt/mysql.sql | 1 + 7 files changed, 322 insertions(+), 120 deletions(-) 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 7d342b55a2..de6d0829dd 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 @@ -101,6 +101,10 @@ public class PlatformManagementAPIImpl implements PlatformManagementAPI { int tenantId = PrivilegedCarbonContext.getThreadLocalCarbonContext().getTenantId(true); try { Platform platform = APIUtil.getPlatformManager().getPlatform(tenantId, id); + + if (platform == null) { + return Response.status(Response.Status.NOT_FOUND).entity("Platform not found").build(); + } return Response.status(Response.Status.OK).entity(platform).build(); } catch (PlatformManagementDAOException e) { log.error("Error while trying the get the platform with the identifier : " + id + " for the tenant :" diff --git a/components/application-mgt/org.wso2.carbon.device.application.mgt.common/src/main/java/org/wso2/carbon/device/application/mgt/common/Platform.java b/components/application-mgt/org.wso2.carbon.device.application.mgt.common/src/main/java/org/wso2/carbon/device/application/mgt/common/Platform.java index af7dc7133e..3ac3ef3ead 100644 --- a/components/application-mgt/org.wso2.carbon.device.application.mgt.common/src/main/java/org/wso2/carbon/device/application/mgt/common/Platform.java +++ b/components/application-mgt/org.wso2.carbon.device.application.mgt.common/src/main/java/org/wso2/carbon/device/application/mgt/common/Platform.java @@ -68,6 +68,8 @@ public class Platform { this.iconName = platform.getIconName(); this.fileBased = platform.isFileBased(); this.shared = platform.isShared(); + this.enabled = platform.isEnabled(); + this.defaultTenantMapping = platform.isDefaultTenantMapping(); if (platform.getProperties() != null) { this.properties = new ArrayList<>(); for (Property property : platform.getProperties()) { 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 770f4cd7fe..c67971a7f1 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 @@ -48,4 +48,10 @@ public interface PlatformDAO { void removePlatforms(int tenantId) throws PlatformManagementDAOException; + int getSuperTenantAndOwnPlatforms(String platformIdentifier, int tenantId) throws PlatformManagementDAOException; + + Platform getTenantOwnedPlatform(int tenantId, String platformIdentifier) throws PlatformManagementDAOException; + + int getMultiTenantPlatforms(String identifier) 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 954d203294..cb98c1fb29 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 @@ -41,8 +41,7 @@ import java.util.List; public class GenericPlatformDAOImpl extends AbstractDAOImpl implements PlatformDAO { private static Log log = LogFactory.getLog(GenericPlatformDAOImpl.class); - @Override - public int register(int tenantId, Platform platform) throws PlatformManagementDAOException { + @Override public int register(int tenantId, Platform platform) throws PlatformManagementDAOException { PreparedStatement preparedStatement = null; try { int platformId = getPlatformId(tenantId, platform.getIdentifier()); @@ -50,7 +49,8 @@ public class GenericPlatformDAOImpl extends AbstractDAOImpl implements PlatformD 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 (?, ?, ?, ?, ?, ?, ?)"; + + "DESCRIPTION, IS_SHARED, ICON_NAME, IS_DEFAULT_TENANT_MAPPING)" + " VALUES (?, ?, ?, ?, " + + "?, ?, ?, ?)"; preparedStatement = connection.prepareStatement(insertToPlatform); preparedStatement.setString(1, platform.getIdentifier()); preparedStatement.setInt(2, tenantId); @@ -59,6 +59,7 @@ public class GenericPlatformDAOImpl extends AbstractDAOImpl implements PlatformD preparedStatement.setString(5, platform.getDescription()); preparedStatement.setBoolean(6, platform.isShared()); preparedStatement.setString(7, platform.getIconName()); + preparedStatement.setBoolean(8, platform.isDefaultTenantMapping()); preparedStatement.execute(); platformId = getPlatformId(tenantId, platform.getIdentifier()); @@ -78,11 +79,14 @@ public class GenericPlatformDAOImpl extends AbstractDAOImpl implements PlatformD } } else { String insertToPlatform = - "INSERT INTO APPM_PLATFORM (IDENTIFIER, TENANT_ID, FILE_BASED)" + " VALUES (?, ?, ?)"; + "INSERT INTO APPM_PLATFORM (IDENTIFIER, TENANT_ID, FILE_BASED, IS_SHARED, " + + "IS_DEFAULT_TENANT_MAPPING) VALUES (?, ?, ?, ?, ?)"; preparedStatement = connection.prepareStatement(insertToPlatform); preparedStatement.setString(1, platform.getIdentifier()); preparedStatement.setInt(2, tenantId); preparedStatement.setBoolean(3, true); + preparedStatement.setBoolean(4, platform.isShared()); + preparedStatement.setBoolean(5, platform.isDefaultTenantMapping()); preparedStatement.execute(); } if (platformId == -1) { @@ -109,9 +113,8 @@ public class GenericPlatformDAOImpl extends AbstractDAOImpl implements PlatformD } } - @Override - public void update(int tenantId, String oldPlatformIdentifier, Platform platform) throws - PlatformManagementDAOException { + @Override public void update(int tenantId, String oldPlatformIdentifier, Platform platform) + throws PlatformManagementDAOException { PreparedStatement preparedStatement = null; try { int platformId = getPlatformId(tenantId, oldPlatformIdentifier); @@ -121,7 +124,8 @@ public class GenericPlatformDAOImpl extends AbstractDAOImpl implements PlatformD if (platformId != -1) { Connection connection = this.getDBConnection(); 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=?, " + + "IS_DEFAULT_TENANT_MAPPING=?"; if (!isIdentifierNull) { insertToPlatform += ",IDENTIFIER = ? "; } @@ -133,19 +137,20 @@ public class GenericPlatformDAOImpl extends AbstractDAOImpl implements PlatformD preparedStatement.setString(1, platform.getDescription()); preparedStatement.setBoolean(2, platform.isShared()); preparedStatement.setString(3, platform.getIconName()); + preparedStatement.setBoolean(4, platform.isDefaultTenantMapping()); if (!isIdentifierNull && !isNameNull) { - preparedStatement.setString(4, platform.getIdentifier()); + preparedStatement.setString(5, platform.getIdentifier()); + preparedStatement.setString(6, platform.getName()); + preparedStatement.setInt(7, platformId); + } else if (isIdentifierNull && !isNameNull) { preparedStatement.setString(5, platform.getName()); preparedStatement.setInt(6, platformId); - } else if (isIdentifierNull && !isNameNull) { - preparedStatement.setString(4, platform.getName()); - preparedStatement.setInt(5, platformId); } else if (!isIdentifierNull) { - preparedStatement.setString(4, platform.getIdentifier()); - preparedStatement.setInt(5, platformId); + preparedStatement.setString(5, platform.getIdentifier()); + preparedStatement.setInt(6, platformId); } else { - preparedStatement.setInt(4, platformId); + preparedStatement.setInt(5, platformId); } preparedStatement.execute(); @@ -217,10 +222,8 @@ public class GenericPlatformDAOImpl extends AbstractDAOImpl implements PlatformD } } - - @Override - public void unregister(int tenantId, String platformIdenfier, boolean isFileBased) throws - PlatformManagementDAOException { + @Override public void unregister(int tenantId, String platformIdenfier, boolean isFileBased) + throws PlatformManagementDAOException { PreparedStatement preparedStatement = null; try { Platform platform = getPlatform(tenantId, platformIdenfier); @@ -284,8 +287,8 @@ public class GenericPlatformDAOImpl extends AbstractDAOImpl implements PlatformD } } - private int getTenantPlatformMapping(int tenantId, String platformIdentifier) throws - PlatformManagementDAOException { + private int getTenantPlatformMapping(int tenantId, String platformIdentifier) + throws PlatformManagementDAOException { PreparedStatement preparedStatement = null; ResultSet resultSet = null; String getMapping = "SELECT MAPPING.ID as ID FROM (SELECT ID, PLATFORM_ID FROM APPM_PLATFORM_TENANT_MAPPING " @@ -312,8 +315,7 @@ public class GenericPlatformDAOImpl extends AbstractDAOImpl implements PlatformD } } - @Override - public void removeMapping(int tenantId, String platformIdentifier) throws PlatformManagementDAOException { + @Override public void removeMapping(int tenantId, String platformIdentifier) throws PlatformManagementDAOException { String deleteMapping = "DELETE FROM APPM_PLATFORM_TENANT_MAPPING WHERE ID = ?"; PreparedStatement preparedStatement = null; try { @@ -338,8 +340,7 @@ public class GenericPlatformDAOImpl extends AbstractDAOImpl implements PlatformD } } - @Override - public void removeMappingTenants(String platformIdentifier) throws PlatformManagementDAOException { + @Override public void removeMappingTenants(String platformIdentifier) throws PlatformManagementDAOException { PreparedStatement preparedStatement = null; int platformId = getPlatformId(MultitenantConstants.SUPER_TENANT_ID, platformIdentifier); String getMapping = "DELETE FROM APPM_PLATFORM_TENANT_MAPPING WHERE TENANT_ID != ? AND PLATFORM_ID=?"; @@ -360,23 +361,21 @@ public class GenericPlatformDAOImpl extends AbstractDAOImpl implements PlatformD } } - - @Override - public List getPlatforms(int tenantId) throws PlatformManagementDAOException { + @Override public List getPlatforms(int tenantId) throws PlatformManagementDAOException { PreparedStatement preparedStatement = null; ResultSet resultSet = null; if (log.isDebugEnabled()) { log.debug("GetPlaforms request received for the tenant ID " + tenantId); } - String selectQuery = - "SELECT MAPPING.ID, PLATFORM.IDENTIFIER FROM (SELECT * FROM APPM_PLATFORM WHERE TENANT_ID=? OR " - + "IS_SHARED = TRUE AND FILE_BASED = FALSE) PLATFORM LEFT JOIN APPM_PLATFORM_TENANT_MAPPING " - + "MAPPING ON PLATFORM.ID = MAPPING.PLATFORM_ID"; + String selectQuery = "SELECT MAPPING.ID, PLATFORM.IDENTIFIER FROM (SELECT * FROM APPM_PLATFORM " + + "WHERE TENANT_ID=? OR IS_SHARED = TRUE ) PLATFORM LEFT JOIN APPM_PLATFORM_TENANT_MAPPING " + + "MAPPING ON PLATFORM.ID = MAPPING.PLATFORM_ID AND MAPPING.TENANT_ID = ?"; try { Connection connection = this.getDBConnection(); preparedStatement = connection.prepareStatement(selectQuery); preparedStatement.setInt(1, tenantId); + preparedStatement.setInt(2, tenantId); resultSet = preparedStatement.executeQuery(); List platforms = new ArrayList<>(); if (log.isDebugEnabled()) { @@ -413,9 +412,9 @@ public class GenericPlatformDAOImpl extends AbstractDAOImpl implements PlatformD public Platform getPlatform(String tenantDomain, String platformIdentifier) throws PlatformManagementDAOException { PreparedStatement preparedStatement = null; ResultSet resultSet = null; - String platformQuery = "SELECT * FROM (SELECT * FROM APPM_PLATFORM WHERE (TENANT_DOMAIN=? AND IDENTIFIER=?) " + - "OR (IS_SHARED = TRUE AND IDENTIFIER=?) AND FILE_BASED = FALSE ) PLATFORM " + - "LEFT JOIN APPM_PLATFORM_PROPERTIES PROPS ON PLATFORM.ID = PROPS.PLATFORM_ID"; + String platformQuery = "SELECT * FROM (SELECT * FROM APPM_PLATFORM WHERE (TENANT_DOMAIN=? AND IDENTIFIER=?) " + + "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.getDBConnection(); preparedStatement = connection.prepareStatement(platformQuery); @@ -457,8 +456,7 @@ public class GenericPlatformDAOImpl extends AbstractDAOImpl implements PlatformD } } - @Override - public Platform getPlatform(int tenantId, String identifier) throws PlatformManagementDAOException { + @Override public Platform getPlatform(int tenantId, String identifier) throws PlatformManagementDAOException { Connection conn; PreparedStatement stmt = null; ResultSet rs = null; @@ -467,13 +465,15 @@ public class GenericPlatformDAOImpl extends AbstractDAOImpl implements PlatformD try { 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"; + + ".DESCRIPTION, PLATFORM.ICON_NAME, PLATFORM.IS_SHARED, PLATFORM.IS_DEFAULT_TENANT_MAPPING FROM " + + "(SELECT * FROM APPM_PLATFORM WHERE IDENTIFIER= ? AND (TENANT_ID=? OR IS_SHARED = TRUE)) " + + "PLATFORM LEFT JOIN APPM_PLATFORM_TENANT_MAPPING MAPPING ON PLATFORM.ID = MAPPING.PLATFORM_ID " + + "AND MAPPING.TENANT_ID = ?"; stmt = conn.prepareStatement(sql); stmt.setString(1, identifier); stmt.setInt(2, tenantId); + stmt.setInt(3, tenantId); rs = stmt.executeQuery(); Platform platform = null; @@ -481,15 +481,15 @@ public class GenericPlatformDAOImpl extends AbstractDAOImpl implements PlatformD if (rs.next()) { platform = new Platform(); platform.setFileBased(rs.getBoolean(3)); - platform.setIdentifier(rs.getString(2)); + platform.setShared(rs.getBoolean(8)); + platform.setDefaultTenantMapping(rs.getBoolean(9)); + if (!platform.isFileBased()) { 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 { @@ -499,17 +499,17 @@ public class GenericPlatformDAOImpl extends AbstractDAOImpl implements PlatformD } return platform; } catch (SQLException e) { - throw new PlatformManagementDAOException("Error occurred while getting platform with the identifier " + - identifier + ", for the tenant : " + tenantId, e); - } catch (DBConnectionException e) { + throw new PlatformManagementDAOException( + "Error occurred while getting platform with the identifier " + identifier + ", for the tenant : " + + tenantId, e); + } catch (DBConnectionException e) { throw new PlatformManagementDAOException("Error occurred while obtaining the DB connection.", e); } finally { Util.cleanupResources(stmt, rs); } } - @Override - public void removePlatforms(int tenantId) throws PlatformManagementDAOException { + @Override public void removePlatforms(int tenantId) throws PlatformManagementDAOException { PreparedStatement preparedStatement = null; String sql = "DELETE FROM APPM_PLATFORM WHERE TENANT_ID = ?"; @@ -519,13 +519,118 @@ public class GenericPlatformDAOImpl extends AbstractDAOImpl implements PlatformD preparedStatement.setInt(1, tenantId); preparedStatement.executeUpdate(); } catch (DBConnectionException e) { - throw new PlatformManagementDAOException("Database connection error while removing the platforms for the " - + "tenant - " + tenantId); + 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); + throw new PlatformManagementDAOException( + "SQL exception while executing the query " + sql + " for " + "the tenant : " + tenantId); } finally { Util.cleanupResources(preparedStatement, null); } } + + @Override public int getSuperTenantAndOwnPlatforms(String platformIdentifier, int tenantId) + throws PlatformManagementDAOException { + PreparedStatement preparedStatement = null; + ResultSet resultSet = null; + String sql = "SELECT ID from APPM_PLATFORM where IDENTIFIER = ? AND (TENANT_ID = ? OR (TENANT_ID = ? AND " + + "IS_SHARED = true)"; + + try { + Connection connection = this.getDBConnection(); + preparedStatement = connection.prepareStatement(sql); + preparedStatement.setString(1, platformIdentifier); + preparedStatement.setInt(2, tenantId); + preparedStatement.setInt(3, MultitenantConstants.SUPER_TENANT_ID); + resultSet = preparedStatement.executeQuery(); + + if (resultSet.next()) { + return resultSet.getInt(1); + } else { + return -1; + } + } catch (DBConnectionException e) { + throw new PlatformManagementDAOException( + "Database connection error while removing the platfor 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, resultSet); + } + + } + + @Override public Platform getTenantOwnedPlatform(int tenantId, String platformIdentifier) + throws PlatformManagementDAOException { + Connection conn; + PreparedStatement stmt = null; + ResultSet rs = null; + String sql = ""; + + try { + conn = this.getDBConnection(); + sql = "SELECT * from APPM_PLATFORM WHERE TENANT_ID = ? AND IDENTIFIER = ?"; + stmt = conn.prepareStatement(sql); + stmt.setInt(1, tenantId); + stmt.setString(2, platformIdentifier); + rs = stmt.executeQuery(); + + Platform platform = null; + + if (rs.next()) { + platform = new Platform(); + platform.setFileBased(rs.getBoolean("FILE_BASED")); + platform.setIdentifier(rs.getString("IDENTIFIER")); + platform.setShared(rs.getBoolean("IS_SHARED")); + platform.setDefaultTenantMapping(rs.getBoolean("IS_DEFAULT_TENANT_MAPPING")); + if (!platform.isFileBased()) { + platform.setId(rs.getInt("ID")); + platform.setName(rs.getString("NAME")); + platform.setDescription(rs.getString("DESCRIPTION")); + platform.setIconName(rs.getString("ICON_NAME")); + } + } + return platform; + } catch (SQLException e) { + throw new PlatformManagementDAOException("Error occurred while executing the query : " + sql + " for " + + "getting platforms owned by tenant : " + tenantId, e); + } catch (DBConnectionException e) { + throw new PlatformManagementDAOException( + "Error occurred while obtaining the DB connection for getting " + "platforms owned by tenant : " + + tenantId, e); + } finally { + Util.cleanupResources(stmt, rs); + } + } + + @Override + public int getMultiTenantPlatforms(String identifier) throws PlatformManagementDAOException { + Connection conn; + PreparedStatement stmt = null; + ResultSet rs = null; + String sql = ""; + + try { + conn = this.getDBConnection(); + sql = "SELECT ID from APPM_PLATFORM WHERE TENANT_ID != ? AND IDENTIFIER=?"; + + stmt = conn.prepareStatement(sql); + stmt.setInt(1, MultitenantConstants.SUPER_TENANT_ID); + stmt.setString(2, identifier); + + rs = stmt.executeQuery(); + + if (rs.next()) { + return rs.getInt(1); + } + return -1; + } catch (DBConnectionException e) { + throw new PlatformManagementDAOException("Database Connection exception while trying to get the tenants " + + "which has the platforms with the platform identifier : " + identifier, e); + } catch (SQLException e) { + throw new PlatformManagementDAOException("SQL exception while executing the query " + sql + " to get the" + + " tenants which has the platform with the platform identifier : " + identifier, 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 31ab5552b3..c9d7546a93 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 @@ -156,8 +156,7 @@ public class PlatformManagerImpl implements PlatformManager { } else { return new Platform(platform); } - throw new PlatformManagementException( - "No platform was found for tenant - " + tenantId + " with platform identifier - " + identifier); + return null; } private Platform getPlatformFromInMemory(int tenantId, String identifier) { @@ -182,11 +181,7 @@ public class PlatformManagerImpl implements PlatformManager { @Override public synchronized void register(int tenantId, Platform platform) throws PlatformManagementException { - if (platform.isShared() && tenantId != MultitenantConstants.SUPER_TENANT_ID) { - throw new PlatformManagementException( - "Platform sharing is a restricted operation, therefore Platform - " + platform.getIdentifier() - + " cannot be shared by the tenant domain - " + tenantId); - } + validateBeforeRegister(tenantId, platform); try { ConnectionManagerUtil.beginDBTransaction(); int platformId = DAOFactory.getPlatformDAO().register(tenantId, platform); @@ -208,12 +203,7 @@ public class PlatformManagerImpl implements PlatformManager { if (platform.isDefaultTenantMapping()) { try { if (platform.isShared()) { - TenantManager tenantManager = DataHolder.getInstance().getRealmService().getTenantManager(); - Tenant[] tenants = tenantManager.getAllTenants(); - for (Tenant tenant : tenants) { - DAOFactory.getPlatformDAO() - .addMapping(tenant.getId(), getListOfString(platform.getIdentifier())); - } + sharePlatformWithOtherTenants(platform.getIdentifier()); } DAOFactory.getPlatformDAO().addMapping(tenantId, getListOfString(platform.getIdentifier())); } catch (UserStoreException e) { @@ -244,70 +234,38 @@ public class PlatformManagerImpl implements PlatformManager { @Override public void update(int tenantId, String oldPlatformIdentifier, Platform platform) throws PlatformManagementException { - if (platform.isShared() && tenantId != MultitenantConstants.SUPER_TENANT_ID) { - throw new PlatformManagementException( - "Platform sharing is a restricted operation, therefore Platform - " + platform.getIdentifier() - + " cannot be shared by the tenant domain - " + tenantId); - } + Platform oldPlatform = validateBeforeUpdate(tenantId, oldPlatformIdentifier, platform); try { ConnectionManagerUtil.beginDBTransaction(); - Platform oldPlatform = DAOFactory.getPlatformDAO().getPlatform(tenantId, oldPlatformIdentifier); - - if (oldPlatform == null) { - ConnectionManagerUtil.commitDBTransaction(); - 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.commitDBTransaction(); - 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); - } - } - 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) { + if (tenantPlatforms != null) { + if (tenantPlatforms.get(oldPlatformIdentifier) == null) { + throw new PlatformManagementException( + "Cannot update platform with identifier " + oldPlatformIdentifier + " as it is not " + + " existing already for the tenant " + tenantId); + } + } else { tenantPlatforms = new HashMap<>(); this.inMemoryStore.put(tenantId, tenantPlatforms); } - 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 { DAOFactory.getPlatformDAO().update(tenantId, oldPlatformIdentifier, platform); } - if (platform.isDefaultTenantMapping() && !oldPlatform.isDefaultTenantMapping()) { - try { - if (platform.isShared() && !oldPlatform.isShared()) { - TenantManager tenantManager = DataHolder.getInstance().getRealmService().getTenantManager(); - Tenant[] tenants = tenantManager.getAllTenants(); - for (Tenant tenant : tenants) { - DAOFactory.getPlatformDAO() - .addMapping(tenant.getId(), getListOfString(platform.getIdentifier())); - } - } - DAOFactory.getPlatformDAO().addMapping(tenantId, getListOfString(platform.getIdentifier())); - } catch (UserStoreException e) { - ConnectionManagerUtil.rollbackDBTransaction(); - throw new PlatformManagementException("Error occurred while assigning the platforms for tenants!", - e); + try { + if (platform.isShared() && !oldPlatform.isShared()) { + sharePlatformWithOtherTenants(platform.getIdentifier()); } + } catch (UserStoreException e) { + ConnectionManagerUtil.rollbackDBTransaction(); + throw new PlatformManagementException("Error occurred while assigning the platforms for tenants!", + e); } if (!platform.isShared() && oldPlatform.isShared()) { DAOFactory.getPlatformDAO().removeMappingTenants(platform.getIdentifier()); @@ -331,12 +289,6 @@ public class PlatformManagerImpl implements PlatformManager { } } - private List getListOfString(String platformIdentifier) { - List identifiers = new ArrayList<>(); - identifiers.add(platformIdentifier); - return identifiers; - } - @Override public void unregister(int tenantId, String identifier, boolean isFileBased) throws PlatformManagementException { try { @@ -440,7 +392,6 @@ public class PlatformManagerImpl implements PlatformManager { } else { DAOFactory.getPlatformDAO().removeMapping(tenantId, platform.getIdentifier()); } - if (log.isDebugEnabled()) { log.debug("Platform with identifier : " + platformIdentifier + " successfully " + (isEnabledNewStatus ? "Enabled" : "Disabled")); @@ -458,7 +409,6 @@ public class PlatformManagerImpl implements PlatformManager { } finally { ConnectionManagerUtil.closeDBConnection(); } - } @Override @@ -479,4 +429,137 @@ public class PlatformManagerImpl implements PlatformManager { } } + /** + * To share the super-tenant platform with other tenants + * @param platformIdentifier Identifier of the platform + * @throws UserStoreException User Store Exception + * @throws PlatformManagementDAOException Platform Management DAO Exception + */ + private void sharePlatformWithOtherTenants(String platformIdentifier) + throws UserStoreException, PlatformManagementDAOException { + TenantManager tenantManager = DataHolder.getInstance().getRealmService().getTenantManager(); + Tenant[] tenants = tenantManager.getAllTenants(); + for (Tenant tenant : tenants) { + DAOFactory.getPlatformDAO() + .addMapping(tenant.getId(), getListOfString(platformIdentifier)); + } + } + + /** + * Validation need to be done before registering the platform + * + * @param tenantId ID of the tenant which the platform need to registered to + * @param platform Platform that need to be registered + * @throws PlatformManagementException Platform Management Exception + */ + private void validateBeforeRegister(int tenantId, Platform platform) throws PlatformManagementException { + validatePlatformSharing(tenantId, platform); + try { + ConnectionManagerUtil.beginDBTransaction(); + int existingPlatformId = DAOFactory.getPlatformDAO() + .getSuperTenantAndOwnPlatforms(platform.getIdentifier(), tenantId); + ConnectionManagerUtil.commitDBTransaction(); + if (existingPlatformId != -1) { + throw new PlatformManagementException( + "Another platform exists with the identifier " + platform.getIdentifier() + " in the tenant " + + tenantId + " or super-tenant. Please choose a " + + "different identifier for your platform"); + } + } catch (TransactionManagementException | DBConnectionException e) { + ConnectionManagerUtil.rollbackDBTransaction(); + throw new PlatformManagementException( + "Error while checking pre-conditions before registering" + " platform identifier '" + platform + .getIdentifier() + "' for the tenant :" + tenantId); + } finally { + ConnectionManagerUtil.closeDBConnection(); + } + } + + /** + * Validations that need to be done before updating the platform + * + * @param tenantId ID of the tenant + * @param oldPlatformIdentifier Identifier of the old platform + * @param platform Updated platform + * @return Old platform if all the validation succeeds + * @throws PlatformManagementException Platform ManagementException + */ + private Platform validateBeforeUpdate(int tenantId, String oldPlatformIdentifier, Platform platform) throws + PlatformManagementException { + validatePlatformSharing(tenantId, platform); + try { + ConnectionManagerUtil.beginDBTransaction(); + Platform oldPlatform = DAOFactory.getPlatformDAO().getTenantOwnedPlatform(tenantId, oldPlatformIdentifier); + if (oldPlatform == null) { + ConnectionManagerUtil.commitDBTransaction(); + throw new PlatformManagementException( + "Cannot update platform. Platform with identifier : " + oldPlatformIdentifier + + " does not exist for the tenant : " + tenantId); + } + if (platform.getIdentifier() != null && !platform.getIdentifier().equals(oldPlatformIdentifier)) { + int existingPlatformID = DAOFactory.getPlatformDAO() + .getSuperTenantAndOwnPlatforms(platform.getIdentifier(), tenantId); + ConnectionManagerUtil.commitDBTransaction(); + if (existingPlatformID == -1) { + 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 + " or in super-tenant"); + } + } + return oldPlatform; + } catch (TransactionManagementException | DBConnectionException e) { + ConnectionManagerUtil.rollbackDBTransaction(); + throw new PlatformManagementException( + "Database error while validating the platform update with the " + "platform identifier: " + + oldPlatformIdentifier + " for the tenant :" + tenantId); + } finally { + ConnectionManagerUtil.closeDBConnection(); + } + } + + /** + * To validate whether this platform can be shared or not before registering and updating the platform + * + * @param tenantId ID of the tenant + * @param platform Platform to be validated for sharing + */ + private void validatePlatformSharing(int tenantId, Platform platform) throws PlatformManagementException { + if (platform.isShared() && tenantId != MultitenantConstants.SUPER_TENANT_ID) { + throw new PlatformManagementException( + "Platform sharing is a restricted operation, therefore Platform - " + platform.getIdentifier() + + " cannot be shared by the tenant domain - " + tenantId); + } + try { + ConnectionManagerUtil.beginDBTransaction(); + if (platform.isShared()) { + int sharedPlatform = DAOFactory.getPlatformDAO().getMultiTenantPlatforms(platform.getIdentifier()); + ConnectionManagerUtil.commitDBTransaction(); + if (sharedPlatform != -1) { + throw new PlatformManagementException( + "Platform '" + platform.getIdentifier() + "' cannot be shared as some other tenants have " + + "platforms with the same identifier."); + } + } + } catch (TransactionManagementException | DBConnectionException e) { + ConnectionManagerUtil.rollbackDBTransaction(); + throw new PlatformManagementException( + "Error while checking platform sharing conditions for " + " platform identifier '" + platform + .getIdentifier() + "' for the tenant :" + tenantId); + } finally { + ConnectionManagerUtil.rollbackDBTransaction(); + } + } + + /** + * To get the list of the given platform Identifier + * @param platformIdentifier Identifier of the Platform + * @return Platform Identifier as a list + */ + private List getListOfString(String platformIdentifier) { + List identifiers = new ArrayList<>(); + identifiers.add(platformIdentifier); + return identifiers; + } } 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 c882ce90b3..ce2e3df6fa 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 @@ -13,6 +13,7 @@ NAME VARCHAR (255), FILE_BASED BOOLEAN, DESCRIPTION LONGVARCHAR, IS_SHARED BOOLEAN, +IS_DEFAULT_TENANT_MAPPING BOOLEAN, ICON_NAME VARCHAR (100), PRIMARY KEY (IDENTIFIER, TENANT_ID) ); diff --git a/features/application-mgt/org.wso2.carbon.device.application.mgt.server.feature/src/main/resources/dbscripts/cdm/application-mgt/mysql.sql b/features/application-mgt/org.wso2.carbon.device.application.mgt.server.feature/src/main/resources/dbscripts/cdm/application-mgt/mysql.sql index 6809473a8a..b52ddee657 100644 --- a/features/application-mgt/org.wso2.carbon.device.application.mgt.server.feature/src/main/resources/dbscripts/cdm/application-mgt/mysql.sql +++ b/features/application-mgt/org.wso2.carbon.device.application.mgt.server.feature/src/main/resources/dbscripts/cdm/application-mgt/mysql.sql @@ -22,6 +22,7 @@ NAME VARCHAR (255), FILE_BASED BOOLEAN, DESCRIPTION VARCHAR (2048), IS_SHARED BOOLEAN, +IS_DEFAULT_TENANT_MAPPING BOOLEAN, ICON_NAME VARCHAR (100), PRIMARY KEY (IDENTIFIER, TENANT_ID) ); From 612905bc5f2cdacfd9dd98576bed82e3fb2737d5 Mon Sep 17 00:00:00 2001 From: megala21 Date: Sun, 6 Aug 2017 00:26:41 +0530 Subject: [PATCH 08/10] Adding oracle scripts and impl for PlatformManagement --- .../mgt/core/dao/common/DAOFactory.java | 9 +- .../impl/platform/GenericPlatformDAOImpl.java | 48 +++---- .../impl/platform/OraclePlatformDAOImpl.java | 87 ++++++++++++ .../core/dao/impl/platform/SQLQueries.java | 40 ++++++ .../mgt/core/impl/PlatformManagerImpl.java | 2 +- .../dbscripts/cdm/application-mgt/oracle.sql | 124 +++++++++++------- 6 files changed, 238 insertions(+), 72 deletions(-) create mode 100644 components/application-mgt/org.wso2.carbon.device.application.mgt.core/src/main/java/org/wso2/carbon/device/application/mgt/core/dao/impl/platform/OraclePlatformDAOImpl.java create mode 100644 components/application-mgt/org.wso2.carbon.device.application.mgt.core/src/main/java/org/wso2/carbon/device/application/mgt/core/dao/impl/platform/SQLQueries.java diff --git a/components/application-mgt/org.wso2.carbon.device.application.mgt.core/src/main/java/org/wso2/carbon/device/application/mgt/core/dao/common/DAOFactory.java b/components/application-mgt/org.wso2.carbon.device.application.mgt.core/src/main/java/org/wso2/carbon/device/application/mgt/core/dao/common/DAOFactory.java index 027067a186..d1f675c838 100644 --- a/components/application-mgt/org.wso2.carbon.device.application.mgt.core/src/main/java/org/wso2/carbon/device/application/mgt/core/dao/common/DAOFactory.java +++ b/components/application-mgt/org.wso2.carbon.device.application.mgt.core/src/main/java/org/wso2/carbon/device/application/mgt/core/dao/common/DAOFactory.java @@ -29,6 +29,7 @@ import org.wso2.carbon.device.application.mgt.core.dao.impl.application.H2Applic import org.wso2.carbon.device.application.mgt.core.dao.impl.application.MySQLApplicationDAOImpl; import org.wso2.carbon.device.application.mgt.core.dao.impl.lifecyclestate.GenericLifecycleStateImpl; import org.wso2.carbon.device.application.mgt.core.dao.impl.platform.GenericPlatformDAOImpl; +import org.wso2.carbon.device.application.mgt.core.dao.impl.platform.OraclePlatformDAOImpl; import org.wso2.carbon.device.application.mgt.core.exception.ApplicationManagementDAOException; import org.wso2.carbon.device.application.mgt.core.util.ApplicationMgtDatabaseCreator; import org.wso2.carbon.device.application.mgt.core.util.ConnectionManagerUtil; @@ -73,6 +74,8 @@ public class DAOFactory { case Constants.DataBaseTypes.DB_TYPE_H2: case Constants.DataBaseTypes.DB_TYPE_MYSQL: return new GenericPlatformDAOImpl(); + case Constants.DataBaseTypes.DB_TYPE_ORACLE: + return new OraclePlatformDAOImpl(); default: throw new UnsupportedDatabaseEngineException("Unsupported database engine : " + databaseEngine); } @@ -111,9 +114,9 @@ public class DAOFactory { DatabaseCreator databaseCreator = new ApplicationMgtDatabaseCreator(dataSourceName); if (!databaseCreator.isDatabaseStructureCreated(validationQuery)) { databaseCreator.createRegistryDatabase(); - if (log.isDebugEnabled()) { - log.debug("Application Management tables are created in the database"); - } + log.info("Application Management tables are created in the database"); + } else { + log.info("Application Management Database structure already exists. Not creating the database."); } } } catch (SQLException e) { 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 cb98c1fb29..4cc2a8f67a 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 @@ -41,7 +41,8 @@ import java.util.List; public class GenericPlatformDAOImpl extends AbstractDAOImpl implements PlatformDAO { private static Log log = LogFactory.getLog(GenericPlatformDAOImpl.class); - @Override public int register(int tenantId, Platform platform) throws PlatformManagementDAOException { + @Override + public int register(int tenantId, Platform platform) throws PlatformManagementDAOException { PreparedStatement preparedStatement = null; try { int platformId = getPlatformId(tenantId, platform.getIdentifier()); @@ -113,7 +114,8 @@ public class GenericPlatformDAOImpl extends AbstractDAOImpl implements PlatformD } } - @Override public void update(int tenantId, String oldPlatformIdentifier, Platform platform) + @Override + public void update(int tenantId, String oldPlatformIdentifier, Platform platform) throws PlatformManagementDAOException { PreparedStatement preparedStatement = null; try { @@ -200,8 +202,7 @@ public class GenericPlatformDAOImpl extends AbstractDAOImpl implements PlatformD private int getPlatformId(int tenantId, String platformIdentifier) throws PlatformManagementDAOException { PreparedStatement preparedStatement = null; ResultSet resultSet = null; - String query = "SELECT ID FROM APPM_PLATFORM WHERE (TENANT_ID=? AND IDENTIFIER=?) OR (IS_SHARED = TRUE AND " - + "IDENTIFIER=?)"; + String query = SQLQueries.queryToGetPlatformId; try { Connection connection = this.getDBConnection(); preparedStatement = connection.prepareStatement(query); @@ -222,7 +223,8 @@ public class GenericPlatformDAOImpl extends AbstractDAOImpl implements PlatformD } } - @Override public void unregister(int tenantId, String platformIdenfier, boolean isFileBased) + @Override + public void unregister(int tenantId, String platformIdenfier, boolean isFileBased) throws PlatformManagementDAOException { PreparedStatement preparedStatement = null; try { @@ -315,7 +317,8 @@ public class GenericPlatformDAOImpl extends AbstractDAOImpl implements PlatformD } } - @Override public void removeMapping(int tenantId, String platformIdentifier) throws PlatformManagementDAOException { + @Override + public void removeMapping(int tenantId, String platformIdentifier) throws PlatformManagementDAOException { String deleteMapping = "DELETE FROM APPM_PLATFORM_TENANT_MAPPING WHERE ID = ?"; PreparedStatement preparedStatement = null; try { @@ -340,7 +343,8 @@ public class GenericPlatformDAOImpl extends AbstractDAOImpl implements PlatformD } } - @Override public void removeMappingTenants(String platformIdentifier) throws PlatformManagementDAOException { + @Override + public void removeMappingTenants(String platformIdentifier) throws PlatformManagementDAOException { PreparedStatement preparedStatement = null; int platformId = getPlatformId(MultitenantConstants.SUPER_TENANT_ID, platformIdentifier); String getMapping = "DELETE FROM APPM_PLATFORM_TENANT_MAPPING WHERE TENANT_ID != ? AND PLATFORM_ID=?"; @@ -361,16 +365,15 @@ public class GenericPlatformDAOImpl extends AbstractDAOImpl implements PlatformD } } - @Override public List getPlatforms(int tenantId) throws PlatformManagementDAOException { + @Override + public List getPlatforms(int tenantId) throws PlatformManagementDAOException { PreparedStatement preparedStatement = null; ResultSet resultSet = null; if (log.isDebugEnabled()) { log.debug("GetPlaforms request received for the tenant ID " + tenantId); } - String selectQuery = "SELECT MAPPING.ID, PLATFORM.IDENTIFIER FROM (SELECT * FROM APPM_PLATFORM " - + "WHERE TENANT_ID=? OR IS_SHARED = TRUE ) PLATFORM LEFT JOIN APPM_PLATFORM_TENANT_MAPPING " - + "MAPPING ON PLATFORM.ID = MAPPING.PLATFORM_ID AND MAPPING.TENANT_ID = ?"; + String selectQuery = SQLQueries.queryToGetPlatforms; try { Connection connection = this.getDBConnection(); preparedStatement = connection.prepareStatement(selectQuery); @@ -456,7 +459,8 @@ public class GenericPlatformDAOImpl extends AbstractDAOImpl implements PlatformD } } - @Override public Platform getPlatform(int tenantId, String identifier) throws PlatformManagementDAOException { + @Override + public Platform getPlatform(int tenantId, String identifier) throws PlatformManagementDAOException { Connection conn; PreparedStatement stmt = null; ResultSet rs = null; @@ -464,12 +468,7 @@ public class GenericPlatformDAOImpl extends AbstractDAOImpl implements PlatformD try { conn = this.getDBConnection(); - sql = "SELECT MAPPING.ID, PLATFORM.IDENTIFIER, PLATFORM.FILE_BASED, PLATFORM.ID, PLATFORM.NAME, PLATFORM" - + ".DESCRIPTION, PLATFORM.ICON_NAME, PLATFORM.IS_SHARED, PLATFORM.IS_DEFAULT_TENANT_MAPPING FROM " - + "(SELECT * FROM APPM_PLATFORM WHERE IDENTIFIER= ? AND (TENANT_ID=? OR IS_SHARED = TRUE)) " - + "PLATFORM LEFT JOIN APPM_PLATFORM_TENANT_MAPPING MAPPING ON PLATFORM.ID = MAPPING.PLATFORM_ID " - + "AND MAPPING.TENANT_ID = ?"; - + sql = SQLQueries.queryToGetPlatform; stmt = conn.prepareStatement(sql); stmt.setString(1, identifier); stmt.setInt(2, tenantId); @@ -509,7 +508,8 @@ public class GenericPlatformDAOImpl extends AbstractDAOImpl implements PlatformD } } - @Override public void removePlatforms(int tenantId) throws PlatformManagementDAOException { + @Override + public void removePlatforms(int tenantId) throws PlatformManagementDAOException { PreparedStatement preparedStatement = null; String sql = "DELETE FROM APPM_PLATFORM WHERE TENANT_ID = ?"; @@ -529,12 +529,12 @@ public class GenericPlatformDAOImpl extends AbstractDAOImpl implements PlatformD } } - @Override public int getSuperTenantAndOwnPlatforms(String platformIdentifier, int tenantId) + @Override + public int getSuperTenantAndOwnPlatforms(String platformIdentifier, int tenantId) throws PlatformManagementDAOException { PreparedStatement preparedStatement = null; ResultSet resultSet = null; - String sql = "SELECT ID from APPM_PLATFORM where IDENTIFIER = ? AND (TENANT_ID = ? OR (TENANT_ID = ? AND " - + "IS_SHARED = true)"; + String sql = SQLQueries.queryToGetSupertenantAndOwnPlatforms; try { Connection connection = this.getDBConnection(); @@ -558,10 +558,10 @@ public class GenericPlatformDAOImpl extends AbstractDAOImpl implements PlatformD } finally { Util.cleanupResources(preparedStatement, resultSet); } - } - @Override public Platform getTenantOwnedPlatform(int tenantId, String platformIdentifier) + @Override + public Platform getTenantOwnedPlatform(int tenantId, String platformIdentifier) throws PlatformManagementDAOException { Connection conn; PreparedStatement stmt = null; 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/OraclePlatformDAOImpl.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/OraclePlatformDAOImpl.java new file mode 100644 index 0000000000..1d6cf0d858 --- /dev/null +++ b/components/application-mgt/org.wso2.carbon.device.application.mgt.core/src/main/java/org/wso2/carbon/device/application/mgt/core/dao/impl/platform/OraclePlatformDAOImpl.java @@ -0,0 +1,87 @@ +/* + * 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.dao.impl.platform; + +import org.wso2.carbon.device.application.mgt.common.Platform; +import org.wso2.carbon.device.application.mgt.core.exception.PlatformManagementDAOException; + +import java.util.List; + +/** + * Oracle specific implementation for Platform DAO. + */ +public class OraclePlatformDAOImpl extends GenericPlatformDAOImpl { + + @Override + public int getSuperTenantAndOwnPlatforms(String platformIdentifier, int tenantId) + throws PlatformManagementDAOException { + SQLQueries.queryToGetSupertenantAndOwnPlatforms = "SELECT ID from APPM_PLATFORM where IDENTIFIER " + + "= ? AND (TENANT_ID = ? OR (TENANT_ID = ? AND IS_SHARED = 1))"; + return super.getSuperTenantAndOwnPlatforms(platformIdentifier, tenantId); + } + + @Override + public int register(int tenantId, Platform platform) throws PlatformManagementDAOException { + SQLQueries.queryToGetPlatformId = + "SELECT ID FROM APPM_PLATFORM WHERE (TENANT_ID=? AND IDENTIFIER=?) OR (IS_SHARED = 1 AND " + + "IDENTIFIER=?)"; + return super.register(tenantId, platform); + } + + @Override + public Platform getPlatform(int tenantId, String identifier) throws PlatformManagementDAOException { + SQLQueries.queryToGetPlatform = + "SELECT MAPPING.ID, PLATFORM.IDENTIFIER, PLATFORM.FILE_BASED, PLATFORM.ID, PLATFORM.NAME, " + + "PLATFORM.DESCRIPTION, PLATFORM.ICON_NAME, PLATFORM.IS_SHARED, " + + "PLATFORM.IS_DEFAULT_TENANT_MAPPING FROM (SELECT * FROM APPM_PLATFORM WHERE IDENTIFIER= ? " + + "AND (TENANT_ID=? OR IS_SHARED = 1)) PLATFORM LEFT JOIN APPM_PLATFORM_TENANT_MAPPING " + + "MAPPING ON PLATFORM.ID = MAPPING.PLATFORM_ID AND MAPPING.TENANT_ID = ?"; + return super.getPlatform(tenantId, identifier); + } + + public void removeMappingTenants(String platformIdentifier) throws PlatformManagementDAOException { + SQLQueries.queryToGetPlatformId = + "SELECT ID FROM APPM_PLATFORM WHERE (TENANT_ID=? AND IDENTIFIER=?) OR (IS_SHARED = 1 AND " + + "IDENTIFIER=?)"; + super.removeMappingTenants(platformIdentifier); + } + + public void update(int tenantId, String oldPlatformIdentifier, Platform platform) + throws PlatformManagementDAOException { + SQLQueries.queryToGetPlatformId = + "SELECT ID FROM APPM_PLATFORM WHERE (TENANT_ID=? AND IDENTIFIER=?) OR (IS_SHARED = 1 AND " + + "IDENTIFIER=?)"; + super.update(tenantId, oldPlatformIdentifier, platform); + } + + public void addMapping(int tenantId, List platformIdentifiers) throws PlatformManagementDAOException { + SQLQueries.queryToGetPlatformId = + "SELECT ID FROM APPM_PLATFORM WHERE (TENANT_ID=? AND IDENTIFIER=?) OR (IS_SHARED = 1 AND " + + "IDENTIFIER=?)"; + super.addMapping(tenantId, platformIdentifiers); + } + + public List getPlatforms(int tenantId) throws PlatformManagementDAOException { + SQLQueries.queryToGetPlatforms = "SELECT MAPPING.ID, PLATFORM.IDENTIFIER FROM (SELECT * FROM APPM_PLATFORM " + + "WHERE TENANT_ID=? OR IS_SHARED = 1) PLATFORM LEFT JOIN APPM_PLATFORM_TENANT_MAPPING " + + "MAPPING ON PLATFORM.ID = MAPPING.PLATFORM_ID AND MAPPING.TENANT_ID = ?"; + return super.getPlatforms(tenantId); + } +} 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/SQLQueries.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/SQLQueries.java new file mode 100644 index 0000000000..73eab98ac4 --- /dev/null +++ b/components/application-mgt/org.wso2.carbon.device.application.mgt.core/src/main/java/org/wso2/carbon/device/application/mgt/core/dao/impl/platform/SQLQueries.java @@ -0,0 +1,40 @@ +/* + * 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.dao.impl.platform; + +/** + * SQL Queries specific to Platform. + */ +public class SQLQueries { + static String queryToGetSupertenantAndOwnPlatforms = "SELECT ID from APPM_PLATFORM where IDENTIFIER " + + "= ? AND (TENANT_ID = ? OR (TENANT_ID = ? AND IS_SHARED = true))"; + static String queryToGetPlatformId = + "SELECT ID FROM APPM_PLATFORM WHERE (TENANT_ID=? AND IDENTIFIER=?) OR (IS_SHARED = TRUE AND " + + "IDENTIFIER=?)"; + static String queryToGetPlatform = + "SELECT MAPPING.ID, PLATFORM.IDENTIFIER, PLATFORM.FILE_BASED, PLATFORM.ID, PLATFORM.NAME, PLATFORM" + + ".DESCRIPTION, PLATFORM.ICON_NAME, PLATFORM.IS_SHARED, PLATFORM.IS_DEFAULT_TENANT_MAPPING FROM " + + "(SELECT * FROM APPM_PLATFORM WHERE IDENTIFIER= ? AND (TENANT_ID=? OR IS_SHARED = TRUE)) " + + "PLATFORM LEFT JOIN APPM_PLATFORM_TENANT_MAPPING MAPPING ON PLATFORM.ID = MAPPING.PLATFORM_ID " + + "AND MAPPING.TENANT_ID = ?"; + static String queryToGetPlatforms = "SELECT MAPPING.ID, PLATFORM.IDENTIFIER FROM (SELECT * FROM APPM_PLATFORM " + + "WHERE TENANT_ID=? OR IS_SHARED = TRUE ) PLATFORM LEFT JOIN APPM_PLATFORM_TENANT_MAPPING " + + "MAPPING ON PLATFORM.ID = MAPPING.PLATFORM_ID AND MAPPING.TENANT_ID = ?"; +} 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 c9d7546a93..44e516217c 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 @@ -548,7 +548,7 @@ public class PlatformManagerImpl implements PlatformManager { "Error while checking platform sharing conditions for " + " platform identifier '" + platform .getIdentifier() + "' for the tenant :" + tenantId); } finally { - ConnectionManagerUtil.rollbackDBTransaction(); + ConnectionManagerUtil.closeDBConnection(); } } diff --git a/features/application-mgt/org.wso2.carbon.device.application.mgt.server.feature/src/main/resources/dbscripts/cdm/application-mgt/oracle.sql b/features/application-mgt/org.wso2.carbon.device.application.mgt.server.feature/src/main/resources/dbscripts/cdm/application-mgt/oracle.sql index d512eafa36..b4b845e77c 100644 --- a/features/application-mgt/org.wso2.carbon.device.application.mgt.server.feature/src/main/resources/dbscripts/cdm/application-mgt/oracle.sql +++ b/features/application-mgt/org.wso2.carbon.device.application.mgt.server.feature/src/main/resources/dbscripts/cdm/application-mgt/oracle.sql @@ -1,44 +1,80 @@ -CREATE TABLE IF NOT EXISTS APPM_APPLICATION_TYPE ( - ID INTEGER AUTO_INCREMENT, - NAME VARCHAR (255), - DESCRIPTION TEXT, - CODE VARCHAR (255), - PARAMTERS LONGTEXT, - PRIMARY KEY (ID) -)ENGINE INNODB; - -CREATE TABLE IF NOT EXISTS APPM_APPLICATION_CATEGORY ( - ID INTEGER AUTO_INCREMENT, - NAME VARCHAR (255), - DESCRIPTION TEXT, - PRIMARY KEY (ID) -)ENGINE INNODB; - - -CREATE TABLE IF NOT EXISTS APPM_APPLICATION ( - ID INTEGER AUTO_INCREMENT, - NAME VARCHAR (255), - UUID VARCHAR (255), - DESCRIPTION MEDIUMTEXT, - ICON_NAME VARCHAR (255), - BANNER_NAME VARCHAR (255), - VIDEO_NAME VARCHAR (255), - SCREENSHOTS TEXT, - TAGS TEXT, - APPLICATION_TYPE_ID INTEGER, - CATEGORY_ID INTEGER, - CREATED_AT DATETIME, - MODIFIED_AT DATETIME, - PRIMARY KEY (ID), - FOREIGN KEY (CATEGORY_ID) REFERENCES APPM_APPLICATION_CATEGORY(ID), - FOREIGN KEY (APPLICATION_TYPE_ID) REFERENCES APPM_APPLICATION_TYPE(ID) -)ENGINE INNODB; - - -CREATE TABLE IF NOT EXISTS APPM_APPLICATION_PROPERTIES ( - PROP_KEY VARCHAR (255), - PROP_VAL MEDIUMTEXT, - APPLICATION_ID INTEGER, - PRIMARY KEY (APPLICATION_ID, PROP_KEY), - FOREIGN KEY (APPLICATION_ID) REFERENCES APPM_APPLICATION(ID) -)ENGINE INNODB; \ No newline at end of file +-- ----------------------------------------------------- +-- Schema WSO2DM_APPM_DB +-- ----------------------------------------------------- + +-- ----------------------------------------------------- +-- Table APPM_PLATFORM +-- ----------------------------------------------------- +CREATE TABLE APPM_PLATFORM ( +ID INT UNIQUE, +IDENTIFIER VARCHAR (100) NOT NULL, +TENANT_ID INT NOT NULL , +NAME VARCHAR (255), +FILE_BASED NUMBER (1), +DESCRIPTION VARCHAR (2048), +IS_SHARED NUMBER (1), +IS_DEFAULT_TENANT_MAPPING NUMBER (1), +ICON_NAME VARCHAR (100), +PRIMARY KEY (IDENTIFIER, TENANT_ID) +) +/ + +CREATE SEQUENCE APPM_PLATFORM_SEQ START WITH 1 INCREMENT BY 1 NOCACHE +/ +CREATE OR REPLACE TRIGGER APPM_PLATFORM_TRIG + BEFORE INSERT + ON APPM_PLATFORM + REFERENCING NEW AS NEW + FOR EACH ROW + BEGIN + SELECT APPM_PLATFORM_SEQ.nextval INTO :NEW.ID FROM dual; + END; +/ + + +CREATE TABLE APPM_PLATFORM_PROPERTIES ( +ID INT, +PLATFORM_ID INT NOT NULL, +PROP_NAME VARCHAR (100) NOT NULL, +OPTIONAL NUMBER (1), +DEFAUL_VALUE VARCHAR (255), +FOREIGN KEY(PLATFORM_ID) REFERENCES APPM_PLATFORM(ID) ON DELETE CASCADE, +PRIMARY KEY (ID, PLATFORM_ID, PROP_NAME) +) +/ + +CREATE SEQUENCE APPM_PLATFORM_PROPERTIES_SEQ START WITH 1 INCREMENT BY 1 NOCACHE +/ +CREATE OR REPLACE TRIGGER APPM_PLATFORM_PROPERTIES_TRIG + BEFORE INSERT + ON APPM_PLATFORM_PROPERTIES + REFERENCING NEW AS NEW + FOR EACH ROW + BEGIN + SELECT APPM_PLATFORM_PROPERTIES_SEQ.nextval INTO :NEW.ID FROM dual; + END; +/ + +CREATE TABLE APPM_PLATFORM_TENANT_MAPPING ( +ID INT, +TENANT_ID INT NOT NULL , +PLATFORM_ID INT NOT NULL, +FOREIGN KEY(PLATFORM_ID) REFERENCES APPM_PLATFORM(ID) ON DELETE CASCADE, +PRIMARY KEY (ID, TENANT_ID, PLATFORM_ID) +) +/ + +CREATE SEQUENCE APPM_TENANT_MAPPING_SEQ START WITH 1 INCREMENT BY 1 NOCACHE +/ +CREATE OR REPLACE TRIGGER APPM_TENANT_MAPPING_TRIG + BEFORE INSERT + ON APPM_PLATFORM_TENANT_MAPPING + REFERENCING NEW AS NEW + FOR EACH ROW + BEGIN + SELECT APPM_TENANT_MAPPING_SEQ.nextval INTO :NEW.ID FROM dual; + END; +/ + +CREATE INDEX FK_PLATFROM_TENANT_MAPPING ON APPM_PLATFORM_TENANT_MAPPING(PLATFORM_ID ASC) +/ From 1ab9807dbdbbadb4c118ace123e6c7e1a0c61fcb Mon Sep 17 00:00:00 2001 From: megala21 Date: Sun, 6 Aug 2017 22:19:43 +0530 Subject: [PATCH 09/10] Adding postgre mssql database supports for PlatformManagement --- .../mgt/core/dao/common/DAOFactory.java | 6 +- ...l.java => OracleMsSQLPlatformDAOImpl.java} | 4 +- .../dbscripts/cdm/application-mgt/mssql.sql | 73 ++++++++--------- .../cdm/application-mgt/postgresql.sql | 78 +++++++++---------- 4 files changed, 77 insertions(+), 84 deletions(-) rename components/application-mgt/org.wso2.carbon.device.application.mgt.core/src/main/java/org/wso2/carbon/device/application/mgt/core/dao/impl/platform/{OraclePlatformDAOImpl.java => OracleMsSQLPlatformDAOImpl.java} (96%) diff --git a/components/application-mgt/org.wso2.carbon.device.application.mgt.core/src/main/java/org/wso2/carbon/device/application/mgt/core/dao/common/DAOFactory.java b/components/application-mgt/org.wso2.carbon.device.application.mgt.core/src/main/java/org/wso2/carbon/device/application/mgt/core/dao/common/DAOFactory.java index d1f675c838..c70e9c8c00 100644 --- a/components/application-mgt/org.wso2.carbon.device.application.mgt.core/src/main/java/org/wso2/carbon/device/application/mgt/core/dao/common/DAOFactory.java +++ b/components/application-mgt/org.wso2.carbon.device.application.mgt.core/src/main/java/org/wso2/carbon/device/application/mgt/core/dao/common/DAOFactory.java @@ -29,7 +29,7 @@ import org.wso2.carbon.device.application.mgt.core.dao.impl.application.H2Applic import org.wso2.carbon.device.application.mgt.core.dao.impl.application.MySQLApplicationDAOImpl; import org.wso2.carbon.device.application.mgt.core.dao.impl.lifecyclestate.GenericLifecycleStateImpl; import org.wso2.carbon.device.application.mgt.core.dao.impl.platform.GenericPlatformDAOImpl; -import org.wso2.carbon.device.application.mgt.core.dao.impl.platform.OraclePlatformDAOImpl; +import org.wso2.carbon.device.application.mgt.core.dao.impl.platform.OracleMsSQLPlatformDAOImpl; import org.wso2.carbon.device.application.mgt.core.exception.ApplicationManagementDAOException; import org.wso2.carbon.device.application.mgt.core.util.ApplicationMgtDatabaseCreator; import org.wso2.carbon.device.application.mgt.core.util.ConnectionManagerUtil; @@ -73,9 +73,11 @@ public class DAOFactory { switch (databaseEngine) { case Constants.DataBaseTypes.DB_TYPE_H2: case Constants.DataBaseTypes.DB_TYPE_MYSQL: + case Constants.DataBaseTypes.DB_TYPE_POSTGRESQL: return new GenericPlatformDAOImpl(); + case Constants.DataBaseTypes.DB_TYPE_MSSQL: case Constants.DataBaseTypes.DB_TYPE_ORACLE: - return new OraclePlatformDAOImpl(); + return new OracleMsSQLPlatformDAOImpl(); default: throw new UnsupportedDatabaseEngineException("Unsupported database engine : " + databaseEngine); } 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/OraclePlatformDAOImpl.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/OracleMsSQLPlatformDAOImpl.java similarity index 96% rename from components/application-mgt/org.wso2.carbon.device.application.mgt.core/src/main/java/org/wso2/carbon/device/application/mgt/core/dao/impl/platform/OraclePlatformDAOImpl.java rename to components/application-mgt/org.wso2.carbon.device.application.mgt.core/src/main/java/org/wso2/carbon/device/application/mgt/core/dao/impl/platform/OracleMsSQLPlatformDAOImpl.java index 1d6cf0d858..2d9648dab4 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/OraclePlatformDAOImpl.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/OracleMsSQLPlatformDAOImpl.java @@ -25,9 +25,9 @@ import org.wso2.carbon.device.application.mgt.core.exception.PlatformManagementD import java.util.List; /** - * Oracle specific implementation for Platform DAO. + * Oracle and MsSQL specific implementation for Platform DAO. */ -public class OraclePlatformDAOImpl extends GenericPlatformDAOImpl { +public class OracleMsSQLPlatformDAOImpl extends GenericPlatformDAOImpl { @Override public int getSuperTenantAndOwnPlatforms(String platformIdentifier, int tenantId) diff --git a/features/application-mgt/org.wso2.carbon.device.application.mgt.server.feature/src/main/resources/dbscripts/cdm/application-mgt/mssql.sql b/features/application-mgt/org.wso2.carbon.device.application.mgt.server.feature/src/main/resources/dbscripts/cdm/application-mgt/mssql.sql index d512eafa36..d8c578a585 100644 --- a/features/application-mgt/org.wso2.carbon.device.application.mgt.server.feature/src/main/resources/dbscripts/cdm/application-mgt/mssql.sql +++ b/features/application-mgt/org.wso2.carbon.device.application.mgt.server.feature/src/main/resources/dbscripts/cdm/application-mgt/mssql.sql @@ -1,44 +1,35 @@ -CREATE TABLE IF NOT EXISTS APPM_APPLICATION_TYPE ( - ID INTEGER AUTO_INCREMENT, - NAME VARCHAR (255), - DESCRIPTION TEXT, - CODE VARCHAR (255), - PARAMTERS LONGTEXT, - PRIMARY KEY (ID) -)ENGINE INNODB; +IF NOT EXISTS (SELECT * FROM SYS.OBJECTS WHERE OBJECT_ID = OBJECT_ID(N'[DBO].[APPM_PLATFORM]') AND TYPE IN (N'U')) +CREATE TABLE APPM_PLATFORM ( + ID INT IDENTITY(1,1) NOT NULL UNIQUE, + IDENTIFIER VARCHAR (100) NOT NULL, + TENANT_ID INT NOT NULL , + NAME VARCHAR (255), + FILE_BASED BIT, + DESCRIPTION VARCHAR(2048), + IS_SHARED BIT, + IS_DEFAULT_TENANT_MAPPING BIT, + ICON_NAME VARCHAR (100), + PRIMARY KEY (IDENTIFIER, TENANT_ID) +); -CREATE TABLE IF NOT EXISTS APPM_APPLICATION_CATEGORY ( - ID INTEGER AUTO_INCREMENT, - NAME VARCHAR (255), - DESCRIPTION TEXT, - PRIMARY KEY (ID) -)ENGINE INNODB; +IF NOT EXISTS (SELECT * FROM SYS.OBJECTS WHERE OBJECT_ID = OBJECT_ID(N'[DBO].[APPM_PLATFORM_PROPERTIES]') AND TYPE IN (N'U')) +CREATE TABLE APPM_PLATFORM_PROPERTIES ( +ID INT IDENTITY(1,1) NOT NULL, +PLATFORM_ID INT NOT NULL, +PROP_NAME VARCHAR (100) NOT NULL, +OPTIONAL BIT, +DEFAUL_VALUE VARCHAR (255), +FOREIGN KEY(PLATFORM_ID) REFERENCES APPM_PLATFORM(ID) ON DELETE CASCADE, +PRIMARY KEY (ID, PLATFORM_ID, PROP_NAME) +); +IF NOT EXISTS (SELECT * FROM SYS.OBJECTS WHERE OBJECT_ID = OBJECT_ID(N'[DBO].[APPM_PLATFORM_TENANT_MAPPING]') AND TYPE IN (N'U')) +CREATE TABLE APPM_PLATFORM_TENANT_MAPPING ( +ID INT IDENTITY(1,1) NOT NULL, +TENANT_ID INT NOT NULL , +PLATFORM_ID INT NOT NULL, +FOREIGN KEY(PLATFORM_ID) REFERENCES APPM_PLATFORM(ID) ON DELETE CASCADE, +PRIMARY KEY (ID, TENANT_ID, PLATFORM_ID) +); -CREATE TABLE IF NOT EXISTS APPM_APPLICATION ( - ID INTEGER AUTO_INCREMENT, - NAME VARCHAR (255), - UUID VARCHAR (255), - DESCRIPTION MEDIUMTEXT, - ICON_NAME VARCHAR (255), - BANNER_NAME VARCHAR (255), - VIDEO_NAME VARCHAR (255), - SCREENSHOTS TEXT, - TAGS TEXT, - APPLICATION_TYPE_ID INTEGER, - CATEGORY_ID INTEGER, - CREATED_AT DATETIME, - MODIFIED_AT DATETIME, - PRIMARY KEY (ID), - FOREIGN KEY (CATEGORY_ID) REFERENCES APPM_APPLICATION_CATEGORY(ID), - FOREIGN KEY (APPLICATION_TYPE_ID) REFERENCES APPM_APPLICATION_TYPE(ID) -)ENGINE INNODB; - - -CREATE TABLE IF NOT EXISTS APPM_APPLICATION_PROPERTIES ( - PROP_KEY VARCHAR (255), - PROP_VAL MEDIUMTEXT, - APPLICATION_ID INTEGER, - PRIMARY KEY (APPLICATION_ID, PROP_KEY), - FOREIGN KEY (APPLICATION_ID) REFERENCES APPM_APPLICATION(ID) -)ENGINE INNODB; \ No newline at end of file +CREATE INDEX FK_PLATFROM_TENANT_MAPPING_PLATFORM ON APPM_PLATFORM_TENANT_MAPPING(PLATFORM_ID ASC); \ No newline at end of file diff --git a/features/application-mgt/org.wso2.carbon.device.application.mgt.server.feature/src/main/resources/dbscripts/cdm/application-mgt/postgresql.sql b/features/application-mgt/org.wso2.carbon.device.application.mgt.server.feature/src/main/resources/dbscripts/cdm/application-mgt/postgresql.sql index d512eafa36..96e806a012 100644 --- a/features/application-mgt/org.wso2.carbon.device.application.mgt.server.feature/src/main/resources/dbscripts/cdm/application-mgt/postgresql.sql +++ b/features/application-mgt/org.wso2.carbon.device.application.mgt.server.feature/src/main/resources/dbscripts/cdm/application-mgt/postgresql.sql @@ -1,44 +1,44 @@ -CREATE TABLE IF NOT EXISTS APPM_APPLICATION_TYPE ( - ID INTEGER AUTO_INCREMENT, - NAME VARCHAR (255), - DESCRIPTION TEXT, - CODE VARCHAR (255), - PARAMTERS LONGTEXT, - PRIMARY KEY (ID) -)ENGINE INNODB; +DROP TABLE IF EXISTS APPM_PLATFORM; +DROP SEQUENCE IF EXISTS APPM_PLATFORM_PK_SEQ; +CREATE SEQUENCE APPM_PLATFORM_PK_SEQ; -CREATE TABLE IF NOT EXISTS APPM_APPLICATION_CATEGORY ( - ID INTEGER AUTO_INCREMENT, - NAME VARCHAR (255), - DESCRIPTION TEXT, - PRIMARY KEY (ID) -)ENGINE INNODB; +CREATE TABLE APPM_PLATFORM ( +ID INT DEFAULT NEXTVAL('APPM_PLATFORM_PK_SEQ') UNIQUE, +IDENTIFIER VARCHAR (100) NOT NULL, +TENANT_ID INT NOT NULL , +NAME VARCHAR (255), +FILE_BASED BOOLEAN, +DESCRIPTION VARCHAR(2048), +IS_SHARED BOOLEAN, +IS_DEFAULT_TENANT_MAPPING BOOLEAN, +ICON_NAME VARCHAR (100), +PRIMARY KEY (IDENTIFIER, TENANT_ID) +); +DROP TABLE IF EXISTS APPM_PLATFORM_PROPERTIES; +DROP SEQUENCE IF EXISTS APPM_PLATFORM_PROPERTIES_PK_SEQ; +CREATE SEQUENCE APPM_PLATFORM_PROPERTIES_PK_SEQ; -CREATE TABLE IF NOT EXISTS APPM_APPLICATION ( - ID INTEGER AUTO_INCREMENT, - NAME VARCHAR (255), - UUID VARCHAR (255), - DESCRIPTION MEDIUMTEXT, - ICON_NAME VARCHAR (255), - BANNER_NAME VARCHAR (255), - VIDEO_NAME VARCHAR (255), - SCREENSHOTS TEXT, - TAGS TEXT, - APPLICATION_TYPE_ID INTEGER, - CATEGORY_ID INTEGER, - CREATED_AT DATETIME, - MODIFIED_AT DATETIME, - PRIMARY KEY (ID), - FOREIGN KEY (CATEGORY_ID) REFERENCES APPM_APPLICATION_CATEGORY(ID), - FOREIGN KEY (APPLICATION_TYPE_ID) REFERENCES APPM_APPLICATION_TYPE(ID) -)ENGINE INNODB; +CREATE TABLE APPM_PLATFORM_PROPERTIES ( +ID INT DEFAULT NEXTVAL('APPM_PLATFORM_PROPERTIES_PK_SEQ'), +PLATFORM_ID INT NOT NULL, +PROP_NAME VARCHAR (100) NOT NULL, +OPTIONAL BOOLEAN, +DEFAUL_VALUE VARCHAR (255), +FOREIGN KEY(PLATFORM_ID) REFERENCES APPM_PLATFORM(ID) ON DELETE CASCADE, +PRIMARY KEY (ID, PLATFORM_ID, PROP_NAME) +); +DROP TABLE IF EXISTS APPM_PLATFORM_TENANT_MAPPING; +DROP SEQUENCE IF EXISTS APPM_PLATFORM_TENANT_MAPPING_PK_SEQ; +CREATE SEQUENCE APPM_PLATFORM_TENANT_MAPPING_PK_SEQ; -CREATE TABLE IF NOT EXISTS APPM_APPLICATION_PROPERTIES ( - PROP_KEY VARCHAR (255), - PROP_VAL MEDIUMTEXT, - APPLICATION_ID INTEGER, - PRIMARY KEY (APPLICATION_ID, PROP_KEY), - FOREIGN KEY (APPLICATION_ID) REFERENCES APPM_APPLICATION(ID) -)ENGINE INNODB; \ No newline at end of file +CREATE TABLE APPM_PLATFORM_TENANT_MAPPING ( +ID INT DEFAULT NEXTVAL('APPM_PLATFORM_TENANT_MAPPING_PK_SEQ'), +TENANT_ID INT NOT NULL , +PLATFORM_ID INT NOT NULL, +FOREIGN KEY(PLATFORM_ID) REFERENCES APPM_PLATFORM(ID) ON DELETE CASCADE, +PRIMARY KEY (ID, TENANT_ID, PLATFORM_ID) +); + +CREATE INDEX FK_PLATFROM_TENANT_MAPPING_PLATFORM ON APPM_PLATFORM_TENANT_MAPPING(PLATFORM_ID ASC); \ No newline at end of file From 8dc7e2786d65ffe00a62181cd1e0ff94e1bf9422 Mon Sep 17 00:00:00 2001 From: megala21 Date: Tue, 8 Aug 2017 09:45:50 +0530 Subject: [PATCH 10/10] Adding javadoc comments --- .../mgt/common/services/PlatformManager.java | 101 ++++++++++++++++-- 1 file changed, 90 insertions(+), 11 deletions(-) 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 58c12e558b..b9c07f9130 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 @@ -29,29 +29,108 @@ import java.util.List; */ public interface PlatformManager { - void initialize(int tenantId) throws PlatformManagementException; + /** + * To initialize the shared platforms for the tenant during the tenant initialization time. + * + * @param tenantId ID of the tenant + * @throws PlatformManagementException Platform Management Exception + */ + public void initialize(int tenantId) throws PlatformManagementException; - List getPlatforms(int tenantId) throws PlatformManagementException; + /** + * To get platforms of the specific tenant. + * + * @param tenantId ID of the tenant + * @return List of platforms + * @throws PlatformManagementException Platform Management Exception + */ + public List getPlatforms(int tenantId) throws PlatformManagementException; - Platform getPlatform(int tenantId, String platformIdentifier) throws PlatformManagementException; + /** + * To get platform with the given platform identifier and tenant ID. + * + * @param tenantId ID of the tenant + * @param platformIdentifier Unique identifier of the platform. + * @return the Specific platform with the platform identifier and tenant + * @throws PlatformManagementException Platform Management Exception + */ + public Platform getPlatform(int tenantId, String platformIdentifier) throws PlatformManagementException; - void register(int tenantId, Platform platform) throws PlatformManagementException; + /** + * To register a platform under particular tenant. + * + * @param tenantId ID of the tenant. + * @param platform Platform to be registered + * @throws PlatformManagementException Platform Management Exception + */ + public void register(int tenantId, Platform platform) throws PlatformManagementException; - void update(int tenantId, String oldPlatformIdentifier, Platform platform) + /** + * To update a platform. + * + * @param tenantId ID of the tenant + * @param oldPlatformIdentifier Old platform Identifier + * @param platform Platform to be updated + * @throws PlatformManagementException Platform Management Exception + */ + public void update(int tenantId, String oldPlatformIdentifier, Platform platform) throws PlatformManagementException; - void unregister(int tenantId, String platformIdentifier, boolean isFileBased) + /** + * To un-register the platform. + * + * @param tenantId ID of the tenant. + * @param platformIdentifier ID of the platform + * @param isFileBased To indicate whether a file based or not. + * @throws PlatformManagementException Platform Management Exception. + */ + public void unregister(int tenantId, String platformIdentifier, boolean isFileBased) throws PlatformManagementException; - void addMapping(int tenantId, List platformIdentifiers) throws PlatformManagementException; + /** + * To add mapping to platform identifiers with the tenant ID. + * + * @param tenantId ID of the tenant + * @param platformIdentifiers Platform Identifiers + * @throws PlatformManagementException Platform Management Exception + */ + public void addMapping(int tenantId, List platformIdentifiers) throws PlatformManagementException; - void addMapping(int tenantId, String platformIdentifier) throws PlatformManagementException; + /** + * To add mapping to a platform for a tenant. + * + * @param tenantId ID of the tenant. + * @param platformIdentifier ID of the platform, the mapping should be added. + * @throws PlatformManagementException Platform Management Exception. + */ + public void addMapping(int tenantId, String platformIdentifier) throws PlatformManagementException; - void removeMapping(int tenantId, String platformIdentifier) throws PlatformManagementException; + /** + * To remove a mapping of a platform to a tenant. + * + * @param tenantId ID of the tenant. + * @param platformIdentifier ID of the platform. + * @throws PlatformManagementException Platform Management Exception. + */ + public void removeMapping(int tenantId, String platformIdentifier) throws PlatformManagementException; - void updatePlatformStatus(int tenantId, String platformIdentifier, String status) + /** + * To update the platform status(ENABLED / DISABLED). + * + * @param tenantId Id of the tenant + * @param platformIdentifier ID of the platform + * @param status Status to be updated. + * @throws PlatformManagementException Platform Management Exception. + */ + public void updatePlatformStatus(int tenantId, String platformIdentifier, String status) throws PlatformManagementException; - void removePlatforms(int tenantId) throws PlatformManagementException; + /** + * To remove platforms that belongs to particular tenant. + * + * @param tenantId ID of the tenant. + * @throws PlatformManagementException Platform Management Exception. + */ + public void removePlatforms(int tenantId) throws PlatformManagementException; }