From fe26352ec9a8796b3e1e14c5d0e7e325fc84ee44 Mon Sep 17 00:00:00 2001 From: sinthuja Date: Tue, 13 Jun 2017 13:55:04 +0530 Subject: [PATCH] Adding platform manager first API. --- .../device/application/mgt/api/APIUtil.java | 24 +++++- .../services/ApplicationManagementAPI.java | 4 +- .../api/services/PlatformManagementAPI.java | 82 +++++++++++++++++++ .../impl/ApplicationManagementAPIImpl.java | 1 - .../impl/PlatformManagementAPIImpl.java | 76 +++++++++++++++++ .../device/application/mgt/common/Filter.java | 1 - .../application/mgt/common/Platform.java | 19 +++++ .../mgt/common/services/PlatformManager.java | 4 + .../pom.xml | 7 -- .../application/mgt/core/dao/PlatformDAO.java | 2 +- .../mgt/core/dao/impl/PlatformDAOImpl.java | 42 ++++++---- .../mgt/core/deployer/Platform.java | 10 +++ .../mgt/core/deployer/PlatformDeployer.java | 2 + .../mgt/core/impl/PlatformManagerImpl.java | 39 ++++++++- .../src/test/resources/Platform.xml | 2 +- .../src/test/resources/h2.sql | 2 +- 16 files changed, 284 insertions(+), 33 deletions(-) create mode 100644 components/application-mgt/org.wso2.carbon.device.application.mgt.api/src/main/java/org/wso2/carbon/device/application/mgt/api/services/PlatformManagementAPI.java create mode 100644 components/application-mgt/org.wso2.carbon.device.application.mgt.api/src/main/java/org/wso2/carbon/device/application/mgt/api/services/impl/PlatformManagementAPIImpl.java diff --git a/components/application-mgt/org.wso2.carbon.device.application.mgt.api/src/main/java/org/wso2/carbon/device/application/mgt/api/APIUtil.java b/components/application-mgt/org.wso2.carbon.device.application.mgt.api/src/main/java/org/wso2/carbon/device/application/mgt/api/APIUtil.java index 917c35bbf5..03e3666178 100644 --- a/components/application-mgt/org.wso2.carbon.device.application.mgt.api/src/main/java/org/wso2/carbon/device/application/mgt/api/APIUtil.java +++ b/components/application-mgt/org.wso2.carbon.device.application.mgt.api/src/main/java/org/wso2/carbon/device/application/mgt/api/APIUtil.java @@ -24,6 +24,7 @@ import org.wso2.carbon.context.PrivilegedCarbonContext; import org.wso2.carbon.device.application.mgt.api.beans.ErrorResponse; import org.wso2.carbon.device.application.mgt.common.exception.ApplicationManagementException; import org.wso2.carbon.device.application.mgt.common.services.ApplicationManager; +import org.wso2.carbon.device.application.mgt.common.services.PlatformManager; import javax.ws.rs.core.Response; @@ -36,9 +37,9 @@ public class APIUtil { private static Log log = LogFactory.getLog(APIUtil.class); private static ApplicationManager applicationManager; + private static PlatformManager platformManager; public static ApplicationManager getApplicationManager() { - if (applicationManager == null) { synchronized (APIUtil.class) { if (applicationManager == null) { @@ -57,9 +58,25 @@ public class APIUtil { return applicationManager; } - public static Response getResponse(ApplicationManagementException ex, Response.Status status) { + public static PlatformManager getPlatformManager() { + if (platformManager == null) { + synchronized (APIUtil.class) { + if (platformManager == null) { + PrivilegedCarbonContext ctx = PrivilegedCarbonContext.getThreadLocalCarbonContext(); + platformManager = + (PlatformManager) ctx.getOSGiService(PlatformManager.class, null); + if (platformManager == null) { + String msg = "Platform Manager service has not initialized."; + log.error(msg); + throw new IllegalStateException(msg); + } + } + } + } + return platformManager; + } - //TODO: check for exception type and set the response code. + public static Response getResponse(ApplicationManagementException ex, Response.Status status) { ErrorResponse errorMessage = new ErrorResponse(); errorMessage.setMessage(ex.getMessage()); if (status == null) { @@ -67,6 +84,5 @@ public class APIUtil { } errorMessage.setCode(status.getStatusCode()); return Response.status(status).entity(errorMessage).build(); - } } diff --git a/components/application-mgt/org.wso2.carbon.device.application.mgt.api/src/main/java/org/wso2/carbon/device/application/mgt/api/services/ApplicationManagementAPI.java b/components/application-mgt/org.wso2.carbon.device.application.mgt.api/src/main/java/org/wso2/carbon/device/application/mgt/api/services/ApplicationManagementAPI.java index 5ac7909929..d62b722d5e 100644 --- a/components/application-mgt/org.wso2.carbon.device.application.mgt.api/src/main/java/org/wso2/carbon/device/application/mgt/api/services/ApplicationManagementAPI.java +++ b/components/application-mgt/org.wso2.carbon.device.application.mgt.api/src/main/java/org/wso2/carbon/device/application/mgt/api/services/ApplicationManagementAPI.java @@ -26,8 +26,8 @@ import javax.ws.rs.*; import javax.ws.rs.core.MediaType; import javax.ws.rs.core.Response; -@Api(value = "Application Management", description = "This API carries all device management related operations " + - "such as get all the available devices, etc.") +@Api(value = "Application Management", description = "This API carries all application management related operations " + + "such as get all the applications, add application, etc.") @Produces(MediaType.APPLICATION_JSON) @Consumes(MediaType.APPLICATION_JSON) public interface ApplicationManagementAPI { 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 new file mode 100644 index 0000000000..3cc5761f6e --- /dev/null +++ b/components/application-mgt/org.wso2.carbon.device.application.mgt.api/src/main/java/org/wso2/carbon/device/application/mgt/api/services/PlatformManagementAPI.java @@ -0,0 +1,82 @@ +package org.wso2.carbon.device.application.mgt.api.services;/* +* 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. +* +*/ + +import io.swagger.annotations.*; +import org.wso2.carbon.device.application.mgt.api.beans.ErrorResponse; +import org.wso2.carbon.device.application.mgt.common.ApplicationList; +import org.wso2.carbon.device.application.mgt.common.Platform; + +import javax.validation.constraints.Size; +import javax.ws.rs.*; +import javax.ws.rs.core.MediaType; +import javax.ws.rs.core.Response; + +@Api(value = "Platform Management", description = "This API carries all platform management related operations " + + "such as get all the available platform for a tenant, etc.") +@Produces(MediaType.APPLICATION_JSON) +@Consumes(MediaType.APPLICATION_JSON) +public interface PlatformManagementAPI { + public final static String SCOPE = "scope"; + + @GET + @Path("platforms") + @Produces(MediaType.APPLICATION_JSON) + @Consumes(MediaType.APPLICATION_JSON) + @ApiOperation( + consumes = MediaType.APPLICATION_JSON, + produces = MediaType.APPLICATION_JSON, + httpMethod = "GET", + value = "get all platforms", + notes = "This will get all applications", + tags = "Application Management", + extensions = { + @Extension(properties = { + @ExtensionProperty(name = SCOPE, value = "perm:get-platforms") + }) + } + ) + @ApiResponses( + value = { + @ApiResponse( + code = 200, + message = "OK. \n Successfully got platforms list.", + response = Platform.class, + responseContainer = "List"), + @ApiResponse( + code = 304, + message = "Not Modified. \n " + + "Empty body because the client already has the latest version of the requested resource."), + @ApiResponse( + code = 500, + message = "Internal Server Error. \n Error occurred while getting the platform list.", + response = ErrorResponse.class) + }) + Response getPlatforms( + @ApiParam( + name = "status", + allowableValues = "ENABLED, DISABLED, ALL", + 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 can be used by the tenant but disabled to be used for tenant\n" + + "- ALL: All the list of platforms that can be used by the tenant", + required = false) + @QueryParam("status") + @Size(max = 45) + 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/ApplicationManagementAPIImpl.java b/components/application-mgt/org.wso2.carbon.device.application.mgt.api/src/main/java/org/wso2/carbon/device/application/mgt/api/services/impl/ApplicationManagementAPIImpl.java index f45883c46e..b3b1660490 100644 --- a/components/application-mgt/org.wso2.carbon.device.application.mgt.api/src/main/java/org/wso2/carbon/device/application/mgt/api/services/impl/ApplicationManagementAPIImpl.java +++ b/components/application-mgt/org.wso2.carbon.device.application.mgt.api/src/main/java/org/wso2/carbon/device/application/mgt/api/services/impl/ApplicationManagementAPIImpl.java @@ -38,7 +38,6 @@ public class ApplicationManagementAPIImpl { private static Log log = LogFactory.getLog(ApplicationManagementAPIImpl.class); - @GET @Consumes("application/json") @Path("applications") 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 new file mode 100644 index 0000000000..5448b2f878 --- /dev/null +++ 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 @@ -0,0 +1,76 @@ +/* +* 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.api.services.impl; + +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.api.APIUtil; +import org.wso2.carbon.device.application.mgt.api.services.PlatformManagementAPI; +import org.wso2.carbon.device.application.mgt.common.Platform; +import org.wso2.carbon.device.application.mgt.common.exception.PlatformManagementException; + +import javax.ws.rs.QueryParam; +import javax.ws.rs.core.Response; +import java.util.ArrayList; +import java.util.List; + +public class PlatformManagementAPIImpl implements PlatformManagementAPI { + + private static final String ALL_STATUS = "ALL"; + private static final String ENABLED_STATUS = "ENABLED"; + private static final String DISABLED_STATUS = "DISABLED"; + + private static Log log = LogFactory.getLog(PlatformManagementAPIImpl.class); + + @Override + public Response getPlatforms(@QueryParam("status") String status) { + String tenantDomain = PrivilegedCarbonContext.getThreadLocalCarbonContext().getTenantDomain(true); + try { + List platforms = APIUtil.getPlatformManager().getPlatforms(tenantDomain); + List results; + if (status != null) { + if (status.contentEquals(ALL_STATUS)) { + results = platforms; + } else if (status.contentEquals(ENABLED_STATUS)) { + results = new ArrayList<>(); + for (Platform platform : platforms) { + if (platform.isEnabled()) { + results.add(platform); + } + } + } else if (status.contentEquals(DISABLED_STATUS)) { + results = new ArrayList<>(); + for (Platform platform : platforms) { + if (!platform.isEnabled()) { + results.add(platform); + } + } + } else { + results = platforms; + } + } else { + results = platforms; + } + return Response.status(Response.Status.OK).entity(results).build(); + } catch (PlatformManagementException e) { + log.error("Error while getting the platforms for tenant - " + tenantDomain, e); + return APIUtil.getResponse(e, Response.Status.INTERNAL_SERVER_ERROR); + } + } +} diff --git a/components/application-mgt/org.wso2.carbon.device.application.mgt.common/src/main/java/org/wso2/carbon/device/application/mgt/common/Filter.java b/components/application-mgt/org.wso2.carbon.device.application.mgt.common/src/main/java/org/wso2/carbon/device/application/mgt/common/Filter.java index bfdb5d3ab0..5a83306ea0 100644 --- a/components/application-mgt/org.wso2.carbon.device.application.mgt.common/src/main/java/org/wso2/carbon/device/application/mgt/common/Filter.java +++ b/components/application-mgt/org.wso2.carbon.device.application.mgt.common/src/main/java/org/wso2/carbon/device/application/mgt/common/Filter.java @@ -31,7 +31,6 @@ public class Filter { private int offset; - //TODO: private String filter; private List filterProperties; 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 a03540ea8f..b58410138d 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 @@ -44,6 +44,10 @@ public class Platform implements Cloneable { private List properties; + private boolean enabled; + + private boolean defaultTenantMapping; + public Platform(Platform platform) { this.id = platform.getId(); this.name = platform.getName(); @@ -145,6 +149,21 @@ public class Platform implements Cloneable { this.shared = shared; } + public boolean isEnabled() { + return enabled; + } + + public void setEnabled(boolean enabled) { + this.enabled = enabled; + } + + public boolean isDefaultTenantMapping() { + return defaultTenantMapping; + } + + public void setDefaultTenantMapping(boolean defaultTenantMapping) { + this.defaultTenantMapping = defaultTenantMapping; + } public static class Property implements Cloneable { 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 d21b4b533a..05f538586d 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,12 +29,16 @@ import java.util.List; */ public interface PlatformManager { + void initialize(String tenantDomain) throws PlatformManagementException; + List getPlatforms(String tenantDomain) throws PlatformManagementException; void register(String tenantDomain, Platform platform) throws PlatformManagementException; void unregister(String tenantDomain, String platformCode, boolean isFileBased) throws PlatformManagementException; + void addMapping(String tenantDomain, List platformCode) throws PlatformManagementException; + void addMapping(String tenantDomain, String platformCode) throws PlatformManagementException; void removeMapping(String tenantDomain, String platformCode) 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 a624f13cd2..cb64c11f24 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 @@ -82,13 +82,6 @@ - - - - - - - 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 5540bfe0a0..65a1c4ca91 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 @@ -29,7 +29,7 @@ public interface PlatformDAO { void unregister(String tenantDomain, String platformCode) throws PlatformManagementDAOException; - void addMapping(String tenantDomain, String platformCode) throws PlatformManagementDAOException; + void addMapping(String tenantDomain, List platformCode) throws PlatformManagementDAOException; void removeMapping(String tenantDomain, String platformCode) 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/PlatformDAOImpl.java b/components/application-mgt/org.wso2.carbon.device.application.mgt.core/src/main/java/org/wso2/carbon/device/application/mgt/core/dao/impl/PlatformDAOImpl.java index 209788094c..baef15102e 100644 --- a/components/application-mgt/org.wso2.carbon.device.application.mgt.core/src/main/java/org/wso2/carbon/device/application/mgt/core/dao/impl/PlatformDAOImpl.java +++ b/components/application-mgt/org.wso2.carbon.device.application.mgt.core/src/main/java/org/wso2/carbon/device/application/mgt/core/dao/impl/PlatformDAOImpl.java @@ -148,22 +148,26 @@ public class PlatformDAOImpl implements PlatformDAO { } } - public void addMapping(String tenantDomain, String platformCode) throws PlatformManagementDAOException { + public void addMapping(String tenantDomain, List platformCodes) throws PlatformManagementDAOException { String insertMapping = "INSERT INTO APPM_PLATFORM_TENANT_MAPPING(TENANT_DOMAIN, PLATFORM_CODE) VALUES (?, ?)"; try { ConnectionManagerUtil.beginTransaction(); - if (getTenantPlatformMapping(tenantDomain, platformCode) != -1) { - Connection connection = ConnectionManagerUtil.getConnection(); - PreparedStatement preparedStatement = connection.prepareStatement(insertMapping); - preparedStatement.execute(); - ConnectionManagerUtil.commitTransaction(); - } else { - throw new PlatformManagementDAOException("Platform - " + platformCode + " is already assigned to tenant domain - " + tenantDomain); + for (String platformCode : platformCodes) { + if (getTenantPlatformMapping(tenantDomain, platformCode) != -1) { + Connection connection = ConnectionManagerUtil.getConnection(); + PreparedStatement preparedStatement = connection.prepareStatement(insertMapping); + preparedStatement.setString(1, tenantDomain); + preparedStatement.setString(2, platformCode); + preparedStatement.execute(); + } else { + throw new PlatformManagementDAOException("Platform - " + platformCode + " is already assigned to tenant domain - " + tenantDomain); + } } + ConnectionManagerUtil.commitTransaction(); } catch (TransactionManagementException e) { ConnectionManagerUtil.rollbackTransaction(); throw new PlatformManagementDAOException("Error occured while trying to add the mapping of platform - " - + platformCode + " for tenant - " + tenantDomain, e); + + platformCodes.toString() + " for tenant - " + tenantDomain, e); } catch (DBConnectionException e) { ConnectionManagerUtil.rollbackTransaction(); throw new PlatformManagementDAOException("Error occurred when getting the connection for the database. ", e); @@ -232,15 +236,23 @@ public class PlatformDAOImpl implements PlatformDAO { @Override public List getPlatforms(String tenantDomain) throws PlatformManagementDAOException { - String selectQuery = "SELECT PLATFORM_CODE FROM APPM_PLATFORM_TENANT_MAPPING WHERE TENANT_DOMAIN=?"; + String selectQuery = "SELECT * FROM (SELECT * FROM APPM_PLATFORM WHERE TENANT_DOMAIN=? OR IS_SHARED = TRUE) PLATFORM " + + "LEFT JOIN APPM_PLATFORM_TENANT_MAPPING MAPPING ON PLATFORM.CODE = MAPPING.PLATFORM_CODE"; try { Connection connection = ConnectionManagerUtil.openConnection(); PreparedStatement preparedStatement = connection.prepareStatement(selectQuery); ResultSet resultSet = preparedStatement.executeQuery(); List platforms = new ArrayList<>(); while (resultSet.next()) { - String platformCode = resultSet.getString(1); - platforms.add(getPlatform(tenantDomain, platformCode)); + String platformCode = resultSet.getString("PLATFORM.CODE"); + int mappingID = resultSet.getInt("MAPPING.ID"); + Platform platform = getPlatform(tenantDomain, platformCode); + if (mappingID != 0) { + platform.setEnabled(true); + } else { + platform.setEnabled(false); + } + platforms.add(platform); } return platforms; } catch (DBConnectionException e) { @@ -253,13 +265,14 @@ public class PlatformDAOImpl implements PlatformDAO { } private Platform getPlatform(String tenantDomain, String platformCode) throws PlatformManagementDAOException { - String platformQuery = "SELECT * FROM (SELECT * FROM APPM_PLATFORM WHERE (TENANT_DOMAIN=? AND CODE=?) PLATFORM " + - "LEFT JOIN APPM_PLATFORM_PROPERTIES PROPS ON PLATFORM.ID = PROPS.PLATFORM_ID ORDER BY PLATFORM.CODE DESC"; + String platformQuery = "SELECT * FROM (SELECT * FROM APPM_PLATFORM WHERE (TENANT_DOMAIN=? AND CODE=?) OR (IS_SHARED = TRUE AND CODE=?)) PLATFORM " + + "LEFT JOIN APPM_PLATFORM_PROPERTIES PROPS ON PLATFORM.ID = PROPS.PLATFORM_ID"; try { Connection connection = ConnectionManagerUtil.getConnection(); PreparedStatement preparedStatement = connection.prepareStatement(platformQuery); preparedStatement.setString(1, tenantDomain); preparedStatement.setString(2, platformCode); + preparedStatement.setString(3, platformCode); ResultSet resultSet = preparedStatement.executeQuery(); Platform platform = new Platform(); if (resultSet.next()) { @@ -292,4 +305,5 @@ public class PlatformDAOImpl implements PlatformDAO { throw new PlatformManagementDAOException("Error in executing the query - " + platformQuery, e); } } + } 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 0fa38308c2..a622cf0fe7 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 @@ -30,6 +30,7 @@ public class Platform { private String description; private String icon; private boolean shared; + private boolean tenantMapping; private List properties; @XmlAttribute(name = "id") @@ -85,4 +86,13 @@ public class Platform { public void setShared(boolean shared) { this.shared = shared; } + + @XmlAttribute(name = "tenantMapping") + public boolean isTenantMapping() { + return tenantMapping; + } + + public void setTenantMapping(boolean tenantMapping) { + this.tenantMapping = tenantMapping; + } } 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 42c5297e2d..e477c9354f 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 @@ -96,6 +96,8 @@ public class PlatformDeployer extends AbstractDeployer { platform.setIconName(platformConfig.getIcon()); platform.setFileBased(true); platform.setShared(platformConfig.isShared()); + 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(); 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 98e31bca1a..4bec94ad5c 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 @@ -21,8 +21,13 @@ import org.wso2.carbon.device.application.mgt.common.Platform; import org.wso2.carbon.device.application.mgt.common.services.PlatformManager; import org.wso2.carbon.device.application.mgt.common.exception.PlatformManagementException; import org.wso2.carbon.device.application.mgt.core.dao.common.DAOFactory; +import org.wso2.carbon.device.application.mgt.core.internal.DataHolder; +import org.wso2.carbon.user.api.Tenant; +import org.wso2.carbon.user.api.TenantManager; +import org.wso2.carbon.user.api.UserStoreException; import org.wso2.carbon.utils.multitenancy.MultitenantConstants; +import java.util.ArrayList; import java.util.HashMap; import java.util.List; import java.util.Map; @@ -34,6 +39,17 @@ public class PlatformManagerImpl implements PlatformManager { this.inMemoryStore = new HashMap<>(); } + @Override + public void initialize(String tenantDomain) throws PlatformManagementException { + List platforms = DAOFactory.getPlatformDAO().getPlatforms(tenantDomain); + List platformCodes = new ArrayList<>(); + for (Platform platform : platforms) { + if (!platform.isEnabled() & platform.isDefaultTenantMapping()) { + platformCodes.add(platform.getCode()); + } + } + addMapping(tenantDomain, platformCodes); + } @Override public List getPlatforms(String tenantDomain) throws PlatformManagementException { @@ -74,6 +90,20 @@ public class PlatformManagerImpl implements PlatformManager { } else { DAOFactory.getPlatformDAO().register(tenantDomain, platform); } + if (platform.isDefaultTenantMapping()) { + try { + if (platform.isShared()) { + TenantManager tenantManager = DataHolder.getInstance().getRealmService().getTenantManager(); + Tenant[] tenants = tenantManager.getAllTenants(); + for (Tenant tenant : tenants) { + addMapping(tenant.getDomain(), platform.getCode()); + } + } + addMapping(tenantDomain, platform.getCode()); + } catch (UserStoreException e) { + throw new PlatformManagementException("Error occured while assigning the platforms for tenants!", e); + } + } } @Override @@ -89,10 +119,17 @@ public class PlatformManagerImpl implements PlatformManager { } @Override - public void addMapping(String tenantDomain, String platformCode) throws PlatformManagementException { + public void addMapping(String tenantDomain, List platformCode) throws PlatformManagementException { DAOFactory.getPlatformDAO().addMapping(tenantDomain, platformCode); } + @Override + public void addMapping(String tenantDomain, String platformCode) throws PlatformManagementException { + List codes = new ArrayList<>(); + codes.add(platformCode); + DAOFactory.getPlatformDAO().addMapping(tenantDomain, codes); + } + @Override public void removeMapping(String tenantDomain, String platformCode) throws PlatformManagementException { DAOFactory.getPlatformDAO().removeMapping(tenantDomain, platformCode); diff --git a/components/application-mgt/org.wso2.carbon.device.application.mgt.core/src/test/resources/Platform.xml b/components/application-mgt/org.wso2.carbon.device.application.mgt.core/src/test/resources/Platform.xml index e3a1db0b92..8c06d41c88 100644 --- a/components/application-mgt/org.wso2.carbon.device.application.mgt.core/src/test/resources/Platform.xml +++ b/components/application-mgt/org.wso2.carbon.device.application.mgt.core/src/test/resources/Platform.xml @@ -1,5 +1,5 @@ - + test number diff --git a/components/application-mgt/org.wso2.carbon.device.application.mgt.core/src/test/resources/h2.sql b/components/application-mgt/org.wso2.carbon.device.application.mgt.core/src/test/resources/h2.sql index 5733fcd55b..4c6f2ad842 100644 --- a/components/application-mgt/org.wso2.carbon.device.application.mgt.core/src/test/resources/h2.sql +++ b/components/application-mgt/org.wso2.carbon.device.application.mgt.core/src/test/resources/h2.sql @@ -22,7 +22,7 @@ PRIMARY KEY (ID, PLATFORM_ID, PROP_NAME) CREATE TABLE APPM_PLATFORM_TENANT_MAPPING ( ID INT NOT NULL AUTO_INCREMENT, TENANT_DOMAIN VARCHAR (100) NOT NULL , -PLATFORM_CODE VARCHAR (100) NOT NULL +PLATFORM_CODE VARCHAR (100) NOT NULL, FOREIGN KEY(PLATFORM_CODE) REFERENCES APPM_PLATFORM(CODE) ON DELETE CASCADE, PRIMARY KEY (ID, TENANT_DOMAIN, PLATFORM_CODE) )