From 41def8fe76512a397ea0d2a1698fef40e5d11938 Mon Sep 17 00:00:00 2001 From: sinthuja Date: Fri, 9 Jun 2017 19:04:02 +0530 Subject: [PATCH 1/3] Adding first cut of platform manager capabilities. --- .../application/mgt/common/Platform.java | 101 +++++- .../PlatformManagementException.java | 29 ++ .../mgt/common/services/PlatformManager.java | 20 ++ .../pom.xml | 7 + .../mgt/core/config/Configuration.java | 2 +- .../mgt/core/config/ConfigurationManager.java | 6 + .../application/mgt/core/dao/PlatformDAO.java | 15 + .../mgt/core/dao/common/DAOFactory.java | 11 +- .../mgt/core/dao/impl/PlatformDAOImpl.java | 295 ++++++++++++++++++ .../mgt/core/deployer/Platform.java | 88 ++++++ .../mgt/core/deployer/PlatformDeployer.java | 118 +++++++ .../mgt/core/deployer/Property.java | 57 ++++ .../PlatformManagementDAOException.java | 31 ++ .../mgt/core/impl/ApplicationManagerImpl.java | 3 +- .../mgt/core/impl/PlatformManagerImpl.java | 77 +++++ .../mgt/core/internal/DataHolder.java | 20 +- .../mgt/core/internal/ServiceComponent.java | 16 + .../mgt/core/util/ConnectionManagerUtil.java | 3 +- .../application/mgt/core/util/Constants.java | 2 + .../src/main/resources/META-INF/component.xml | 23 ++ .../src/test/resources/Platform.xml | 6 + .../src/test/resources/h2.sql | 29 ++ .../pom.xml | 141 --------- components/application-mgt/pom.xml | 2 +- 24 files changed, 945 insertions(+), 157 deletions(-) create mode 100644 components/application-mgt/org.wso2.carbon.device.application.mgt.common/src/main/java/org/wso2/carbon/device/application/mgt/common/exception/PlatformManagementException.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/PlatformDAOImpl.java create mode 100644 components/application-mgt/org.wso2.carbon.device.application.mgt.core/src/main/java/org/wso2/carbon/device/application/mgt/core/deployer/Platform.java create mode 100644 components/application-mgt/org.wso2.carbon.device.application.mgt.core/src/main/java/org/wso2/carbon/device/application/mgt/core/deployer/PlatformDeployer.java create mode 100644 components/application-mgt/org.wso2.carbon.device.application.mgt.core/src/main/java/org/wso2/carbon/device/application/mgt/core/deployer/Property.java create mode 100644 components/application-mgt/org.wso2.carbon.device.application.mgt.core/src/main/java/org/wso2/carbon/device/application/mgt/core/exception/PlatformManagementDAOException.java create mode 100644 components/application-mgt/org.wso2.carbon.device.application.mgt.core/src/main/resources/META-INF/component.xml create mode 100644 components/application-mgt/org.wso2.carbon.device.application.mgt.core/src/test/resources/Platform.xml create mode 100644 components/application-mgt/org.wso2.carbon.device.application.mgt.core/src/test/resources/h2.sql delete mode 100644 components/application-mgt/org.wso2.carbon.device.application.mgt.extensions/pom.xml 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 27550b9ecc..a03540ea8f 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 @@ -20,10 +20,10 @@ package org.wso2.carbon.device.application.mgt.common; import org.wso2.carbon.device.application.mgt.common.jaxrs.Exclude; +import java.util.ArrayList; import java.util.List; -import java.util.Map; -public class Platform { +public class Platform implements Cloneable { @Exclude private int id; @@ -36,9 +36,39 @@ public class Platform { private String iconName; + private boolean fileBased; + + private boolean shared; + private List tags; - private Map properties; + private List properties; + + public Platform(Platform platform) { + this.id = platform.getId(); + this.name = platform.getName(); + this.description = platform.getDescription(); + this.code = platform.getCode(); + this.iconName = platform.getIconName(); + this.fileBased = platform.isFileBased(); + this.shared = platform.isShared(); + if (platform.getProperties() != null) { + this.properties = new ArrayList<>(); + for (Property property : platform.getProperties()) { + this.properties.add(new Property(property)); + } + } + if (platform.getTags() != null) { + this.tags = new ArrayList<>(); + for (String tag : platform.getTags()) { + this.tags.add(tag); + } + } + } + + public Platform() { + + } public int getId() { return id; @@ -69,6 +99,9 @@ public class Platform { } public void setCode(String code) { + if (code != null) { + code = code.trim().toLowerCase(); + } this.code = code; } @@ -80,11 +113,11 @@ public class Platform { this.iconName = iconName; } - public Map getProperties() { + public List getProperties() { return properties; } - public void setProperties(Map properties) { + public void setProperties(List properties) { this.properties = properties; } @@ -95,4 +128,62 @@ public class Platform { public void setTags(List tags) { this.tags = tags; } + + public boolean isFileBased() { + return fileBased; + } + + public void setFileBased(boolean fileBased) { + this.fileBased = fileBased; + } + + public boolean isShared() { + return shared; + } + + public void setShared(boolean shared) { + this.shared = shared; + } + + + public static class Property implements Cloneable { + + private String name; + private boolean optional; + private String defaultValue; + + public Property(Property property) { + this.name = property.getName(); + this.optional = property.isOptional(); + this.defaultValue = property.getDefaultValue(); + } + + public Property() { + + } + + public String getName() { + return name; + } + + public void setName(String name) { + this.name = name; + } + + public boolean isOptional() { + return optional; + } + + public void setOptional(boolean optional) { + this.optional = optional; + } + + public String getDefaultValue() { + return defaultValue; + } + + public void setDefaultValue(String defaultValue) { + this.defaultValue = defaultValue; + } + } } diff --git a/components/application-mgt/org.wso2.carbon.device.application.mgt.common/src/main/java/org/wso2/carbon/device/application/mgt/common/exception/PlatformManagementException.java b/components/application-mgt/org.wso2.carbon.device.application.mgt.common/src/main/java/org/wso2/carbon/device/application/mgt/common/exception/PlatformManagementException.java new file mode 100644 index 0000000000..b7daea0c29 --- /dev/null +++ b/components/application-mgt/org.wso2.carbon.device.application.mgt.common/src/main/java/org/wso2/carbon/device/application/mgt/common/exception/PlatformManagementException.java @@ -0,0 +1,29 @@ +/* +* 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.common.exception; + +public class PlatformManagementException extends ApplicationManagementException { + + public PlatformManagementException(String message, Throwable ex) { + super(message, ex); + } + + public PlatformManagementException(String message) { + super(message); + } +} 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 fb57b924a3..d21b4b533a 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 @@ -18,5 +18,25 @@ */ package org.wso2.carbon.device.application.mgt.common.services; +import org.wso2.carbon.device.application.mgt.common.Platform; +import org.wso2.carbon.device.application.mgt.common.exception.PlatformManagementException; + +import java.util.List; + +/** + * Platform manager is responsible for handling platforms, which will be used to as a registry of platforms. + * And will be able to provide the platforms related informations to other classes which requires. + */ public interface PlatformManager { + + 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, 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 f40fb22813..a624f13cd2 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 @@ -71,11 +71,14 @@ org.wso2.carbon.device.application.mgt.common.*, org.wso2.carbon.device.mgt.core.*, org.wso2.carbon.device.mgt.common.*, + org.apache.axis2.*, + org.wso2.carbon.user.core.* !org.wso2.carbon.device.application.mgt.core.internal.*, org.wso2.carbon.device.application.mgt.core.* + PlatformDeployer @@ -152,6 +155,10 @@ org.wso2.carbon.devicemgt org.wso2.carbon.device.mgt.core + + org.wso2.carbon + org.wso2.carbon.user.core + diff --git a/components/application-mgt/org.wso2.carbon.device.application.mgt.core/src/main/java/org/wso2/carbon/device/application/mgt/core/config/Configuration.java b/components/application-mgt/org.wso2.carbon.device.application.mgt.core/src/main/java/org/wso2/carbon/device/application/mgt/core/config/Configuration.java index b41501c7a3..967a396bef 100644 --- a/components/application-mgt/org.wso2.carbon.device.application.mgt.core/src/main/java/org/wso2/carbon/device/application/mgt/core/config/Configuration.java +++ b/components/application-mgt/org.wso2.carbon.device.application.mgt.core/src/main/java/org/wso2/carbon/device/application/mgt/core/config/Configuration.java @@ -18,7 +18,7 @@ */ package org.wso2.carbon.device.application.mgt.core.config; -import org.wso2.carbon.device.application.mgt.common.exception.InvalidConfigurationException; +import org.wso2.carbon.device.application.mgt.core.deployer.Platform; import javax.xml.bind.annotation.XmlElement; import javax.xml.bind.annotation.XmlElementWrapper; 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 d81240e573..cf959cb91d 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,11 +22,16 @@ 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 { @@ -73,6 +78,7 @@ public class ConfigurationManager { if (configPath == null) { configPath = Constants.DEFAULT_CONFIG_FILE_LOCATION; } + //TODO: Add validation for the configurations this.configuration = (Configuration) unmarshaller.unmarshal(new File(configPath)); } catch (Exception e) { log.error(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/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 b1ff1bf97e..5540bfe0a0 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 @@ -18,6 +18,21 @@ */ 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.PlatformManagementDAOException; + +import java.util.List; + public interface PlatformDAO { + void register(String tenantDomain, Platform platform) throws PlatformManagementDAOException; + + void unregister(String tenantDomain, String platformCode) throws PlatformManagementDAOException; + + void addMapping(String tenantDomain, String platformCode) throws PlatformManagementDAOException; + + void removeMapping(String tenantDomain, String platformCode) throws PlatformManagementDAOException; + + List getPlatforms(String tenantDomain) 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 71c3e675fa..7f89c53575 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 @@ -22,13 +22,13 @@ import org.apache.commons.logging.Log; import org.apache.commons.logging.LogFactory; import org.wso2.carbon.device.application.mgt.common.exception.UnsupportedDatabaseEngineException; import org.wso2.carbon.device.application.mgt.core.dao.ApplicationDAO; +import org.wso2.carbon.device.application.mgt.core.dao.PlatformDAO; +import org.wso2.carbon.device.application.mgt.core.dao.impl.PlatformDAOImpl; import org.wso2.carbon.device.application.mgt.core.dao.impl.application.H2ApplicationDAOImpl; import org.wso2.carbon.device.application.mgt.core.dao.impl.application.MySQLApplicationDAOImpl; import org.wso2.carbon.device.application.mgt.core.util.Constants; import org.wso2.carbon.device.application.mgt.core.util.ConnectionManagerUtil; -import javax.sql.DataSource; - /** * This class intends to act as the primary entity that hides all DAO instantiation related complexities and logic so @@ -59,4 +59,11 @@ public class DAOFactory { } throw new IllegalStateException("Database engine has not initialized properly."); } + + public static PlatformDAO getPlatformDAO(){ + if (databaseEngine != null){ + return new PlatformDAOImpl(); + } + throw new IllegalStateException("Database engine has not initialized properly."); + } } \ 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/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 new file mode 100644 index 0000000000..209788094c --- /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/PlatformDAOImpl.java @@ -0,0 +1,295 @@ +/* +* 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; + + +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.exception.PlatformManagementDAOException; +import org.wso2.carbon.device.application.mgt.core.util.ConnectionManagerUtil; + +import java.sql.Connection; +import java.sql.PreparedStatement; +import java.sql.ResultSet; +import java.sql.SQLException; +import java.util.ArrayList; +import java.util.List; + +public class PlatformDAOImpl implements PlatformDAO { + + @Override + public void register(String tenantDomain, Platform platform) throws PlatformManagementDAOException { + try { + ConnectionManagerUtil.beginTransaction(); + if (getPlatformId(tenantDomain, platform.getCode()) == -1) { + Connection connection = ConnectionManagerUtil.getConnection(); + + String insertToPlatform = "INSERT INTO APPM_PLATFORM (CODE, TENANT_DOMAIN, NAME, DESCRIPTION, IS_SHARED, ICON_NAME)" + + " VALUES (?, ?, ?, ?, ?, ?)"; + PreparedStatement preparedStatement = connection.prepareStatement(insertToPlatform); + preparedStatement.setString(1, platform.getCode()); + preparedStatement.setString(2, tenantDomain); + preparedStatement.setString(3, platform.getName()); + preparedStatement.setString(4, platform.getDescription()); + preparedStatement.setBoolean(5, platform.isShared()); + preparedStatement.setString(6, platform.getIconName()); + preparedStatement.execute(); + + int platformID = getPlatformId(tenantDomain, platform.getCode()); + + 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(); + } + ConnectionManagerUtil.commitTransaction(); + } else { + ConnectionManagerUtil.rollbackTransaction(); + throw new PlatformManagementDAOException("Platform - " + platform.getCode() + + " is already registered for tenant - " + tenantDomain); + } + } catch (TransactionManagementException e) { + ConnectionManagerUtil.rollbackTransaction(); + throw new PlatformManagementDAOException("Unable to start the transaction while trying to register the platform - " + + platform.getCode() + " for tenant - " + tenantDomain, e); + } catch (DBConnectionException e) { + ConnectionManagerUtil.rollbackTransaction(); + throw new PlatformManagementDAOException("Unable to obtain the connection while trying to register the platform - " + + platform.getCode() + " for tenant - " + tenantDomain, 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.closeConnection(); + } + } + + private int getPlatformId(String tenantDomain, String platformCode) throws PlatformManagementDAOException { + String query = "SELECT ID FROM APPM_PLATFORM WHERE (TENANT_DOMAIN=? AND CODE=?) OR (IS_SHARED = TRUE AND CODE=?)"; + try { + Connection connection = ConnectionManagerUtil.getConnection(); + PreparedStatement preparedStatement = connection.prepareStatement(query); + preparedStatement.setString(1, tenantDomain); + preparedStatement.setString(2, platformCode); + preparedStatement.setString(3, platformCode); + ResultSet resultSet = preparedStatement.executeQuery(); + if (resultSet.next()) { + return resultSet.getInt("ID"); + } + return -1; + } catch (DBConnectionException e) { + 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); + } + } + + + @Override + public void unregister(String tenantDomain, String platformCode) throws PlatformManagementDAOException { + try { + ConnectionManagerUtil.beginTransaction(); + int platformId = getPlatformId(tenantDomain, platformCode); + if (platformId != -1) { + Connection connection = ConnectionManagerUtil.getConnection(); + + String deletePlatform = "DELETE FROM APPM_PLATFORM WHERE ID = ?"; + PreparedStatement preparedStatement = connection.prepareStatement(deletePlatform); + preparedStatement.setInt(1, platformId); + preparedStatement.execute(); + + ConnectionManagerUtil.commitTransaction(); + } else { + throw new PlatformManagementDAOException("Platform - " + platformCode + + " is already unregistered registered for tenant - " + tenantDomain); + } + } catch (TransactionManagementException e) { + ConnectionManagerUtil.rollbackTransaction(); + throw new PlatformManagementDAOException("Unable to start the transaction while trying to register the platform - " + + platformCode + " for tenant - " + tenantDomain, e); + } catch (DBConnectionException e) { + ConnectionManagerUtil.rollbackTransaction(); + throw new PlatformManagementDAOException("Unable to obtain the connection while trying to register the platform - " + + platformCode + " for tenant - " + tenantDomain, 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.closeConnection(); + } + } + + public void addMapping(String tenantDomain, String platformCode) 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); + } + } catch (TransactionManagementException e) { + ConnectionManagerUtil.rollbackTransaction(); + throw new PlatformManagementDAOException("Error occured while trying to add the mapping of platform - " + + platformCode + " for tenant - " + tenantDomain, 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.closeConnection(); + } + } + + private int getTenantPlatformMapping(String tenantDomain, String platformCode) throws PlatformManagementDAOException { + String getMapping = "SELECT ID FROM APPM_PLATFORM_TENANT_MAPPING WHERE TENANT_DOMAIN=? AND PLATFORM_CODE=?"; + try { + Connection connection = ConnectionManagerUtil.getConnection(); + PreparedStatement preparedStatement = connection.prepareStatement(getMapping); + preparedStatement.setString(1, tenantDomain); + preparedStatement.setString(2, platformCode); + ResultSet resultSet = preparedStatement.executeQuery(); + if (resultSet.next()) { + return resultSet.getInt("ID"); + } + return -1; + } catch (DBConnectionException 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 platformCode) throws PlatformManagementDAOException { + String deleteMapping = "DELETE FROM APPM_PLATFORM_TENANT_MAPPING WHERE ID = ?"; + try { + ConnectionManagerUtil.beginTransaction(); + int mappingId = getTenantPlatformMapping(tenantDomain, platformCode); + if (mappingId != -1) { + Connection connection = ConnectionManagerUtil.getConnection(); + + PreparedStatement preparedStatement = connection.prepareStatement(deleteMapping); + preparedStatement.setInt(1, mappingId); + preparedStatement.execute(); + + ConnectionManagerUtil.commitTransaction(); + } else { + throw new PlatformManagementDAOException("Platform - " + platformCode + + " is already unassigned for tenant - " + tenantDomain); + } + } catch (TransactionManagementException | DBConnectionException e) { + ConnectionManagerUtil.rollbackTransaction(); + throw new PlatformManagementDAOException("Error occurred while unassigning the platform - " + platformCode + + " for tenant - " + tenantDomain); + } catch (SQLException e) { + ConnectionManagerUtil.rollbackTransaction(); + throw new PlatformManagementDAOException("Error occured while executing the query - " + deleteMapping); + } catch (PlatformManagementDAOException ex) { + ConnectionManagerUtil.rollbackTransaction(); + throw ex; + } finally { + ConnectionManagerUtil.closeConnection(); + } + } + + @Override + public List getPlatforms(String tenantDomain) throws PlatformManagementDAOException { + String selectQuery = "SELECT PLATFORM_CODE FROM APPM_PLATFORM_TENANT_MAPPING WHERE TENANT_DOMAIN=?"; + 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)); + } + return platforms; + } catch (DBConnectionException e) { + throw new PlatformManagementDAOException("Error occured when loading the platforms for tenant - " + tenantDomain, e); + } catch (SQLException e) { + throw new PlatformManagementDAOException("Error occured when executing query - " + selectQuery, e); + } finally { + ConnectionManagerUtil.closeConnection(); + } + } + + 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"; + try { + Connection connection = ConnectionManagerUtil.getConnection(); + PreparedStatement preparedStatement = connection.prepareStatement(platformQuery); + preparedStatement.setString(1, tenantDomain); + preparedStatement.setString(2, platformCode); + ResultSet resultSet = preparedStatement.executeQuery(); + Platform platform = new Platform(); + if (resultSet.next()) { + platform.setId(resultSet.getInt("PLATFORM.ID")); + platform.setCode(platformCode); + platform.setName(resultSet.getString("PLATFORM.NAME")); + platform.setIconName(resultSet.getString("PLATFORM.DESCRIPTION")); + platform.setIconName(resultSet.getString("PLATFORM.ICON_NAME")); + platform.setShared(resultSet.getBoolean("PLATFORM.IS_SHARED")); + platform.setFileBased(false); + List properties = new ArrayList<>(); + do { + if (resultSet.getString("PROPS.PROP_NAME") != null) { + Platform.Property property = new Platform.Property(); + property.setName(resultSet.getString("PROPS.PROP_NAME")); + property.setOptional(resultSet.getBoolean("PROPS.OPTIONAL")); + property.setDefaultValue(resultSet.getString("PROPS.DEFAUL_VALUE")); + properties.add(property); + } + } while (resultSet.next()); + platform.setProperties(properties); + } else { + platform.setCode(platformCode); + platform.setFileBased(true); + } + return platform; + } catch (DBConnectionException e) { + throw new PlatformManagementDAOException("Error when loading the platform - " + platformCode, e); + } catch (SQLException e) { + 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 new file mode 100644 index 0000000000..0fa38308c2 --- /dev/null +++ b/components/application-mgt/org.wso2.carbon.device.application.mgt.core/src/main/java/org/wso2/carbon/device/application/mgt/core/deployer/Platform.java @@ -0,0 +1,88 @@ +/* +* 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.deployer; + +import javax.xml.bind.annotation.XmlAttribute; +import javax.xml.bind.annotation.XmlElement; +import javax.xml.bind.annotation.XmlRootElement; +import java.util.List; + +@XmlRootElement(name = "Platform") +public class Platform { + + private String id; + private String name; + private String description; + private String icon; + private boolean shared; + private List properties; + + @XmlAttribute(name = "id") + public String getId() { + return id; + } + + public void setId(String id) { + this.id = id; + } + + @XmlAttribute(name = "name") + public String getName() { + return name; + } + + public void setName(String name) { + this.name = name; + } + + @XmlElement(name = "Property") + public List getProperties() { + return properties; + } + + + public String getDescription() { + return description; + } + + public void setDescription(String description) { + this.description = description; + } + + public void setProperties(List properties) { + this.properties = properties; + } + + @XmlAttribute(name = "icon") + public String getIcon() { + return icon; + } + + public void setIcon(String icon) { + this.icon = icon; + } + + @XmlAttribute(name = "isShared") + public boolean isShared() { + return shared; + } + + public void setShared(boolean shared) { + this.shared = shared; + } +} 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 new file mode 100644 index 0000000000..42c5297e2d --- /dev/null +++ b/components/application-mgt/org.wso2.carbon.device.application.mgt.core/src/main/java/org/wso2/carbon/device/application/mgt/core/deployer/PlatformDeployer.java @@ -0,0 +1,118 @@ +/* +* 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.deployer; + +import org.apache.axis2.context.ConfigurationContext; +import org.apache.axis2.deployment.AbstractDeployer; +import org.apache.axis2.deployment.DeploymentException; +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.device.application.mgt.common.exception.PlatformManagementException; +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; + +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); + if (!deployementDir.exists()) { + if (!deployementDir.mkdir()) { + log.warn("Unable to create the deployment dir at: " + deployementDir.getPath()); + } + } + } + + public void deploy(DeploymentFileData deploymentFileData) throws DeploymentException { + File deploymentFile = new File(deploymentFileData.getAbsolutePath()); + try { + JAXBContext jaxbContext = JAXBContext.newInstance(Platform.class); + Unmarshaller unmarshaller = jaxbContext.createUnmarshaller(); + 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); + } 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!"); + } + } catch (JAXBException e) { + log.error("Platform configuration file - " + deploymentFile.getAbsolutePath() + " is invalid!", e); + } catch (PlatformManagementException e) { + log.error("Unable to deploy the platform - " + deploymentFile.getAbsolutePath(), e); + } + } + + public void undeploy(String fileName) throws DeploymentException { + String platformId = getPlatformID(fileName); + try { + DataHolder.getInstance().getPlatformManager().unregister(CarbonContext.getThreadLocalCarbonContext().getTenantDomain(), platformId, true); + } catch (PlatformManagementException e) { + log.error("Error occurred while undeploying the platform - " + fileName); + } + } + + private static String getPlatformID(String deploymentFileName) { + if (deploymentFileName.contains(Constants.PLATFORM_DEPLOYMENT_EXT)) { + return deploymentFileName.substring(0, deploymentFileName.length() - + Constants.PLATFORM_DEPLOYMENT_EXT.length()); + } + return deploymentFileName; + } + + 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(); + platform.setCode(platformConfig.getId()); + platform.setName(platformConfig.getName()); + platform.setDescription(platformConfig.getDescription()); + platform.setIconName(platformConfig.getIcon()); + platform.setFileBased(true); + platform.setShared(platformConfig.isShared()); + 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); + } + platform.setProperties(properties); + return platform; + } + + @Override + public void setDirectory(String s) { + } + + @Override + public void setExtension(String s) { + } +} diff --git a/components/application-mgt/org.wso2.carbon.device.application.mgt.core/src/main/java/org/wso2/carbon/device/application/mgt/core/deployer/Property.java b/components/application-mgt/org.wso2.carbon.device.application.mgt.core/src/main/java/org/wso2/carbon/device/application/mgt/core/deployer/Property.java new file mode 100644 index 0000000000..4290253fb7 --- /dev/null +++ b/components/application-mgt/org.wso2.carbon.device.application.mgt.core/src/main/java/org/wso2/carbon/device/application/mgt/core/deployer/Property.java @@ -0,0 +1,57 @@ +/* +* 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.deployer; + +import javax.xml.bind.annotation.XmlAttribute; +import javax.xml.bind.annotation.XmlRootElement; +import javax.xml.bind.annotation.XmlValue; + +@XmlRootElement(name = "Property") +public class Property { + + private String name; + private boolean optional; + private String defaultValue; + + @XmlValue + public String getName() { + return name; + } + + public void setName(String name) { + this.name = name; + } + + @XmlAttribute(name = "optional") + public boolean isOptional() { + return optional; + } + + public void setOptional(boolean optional) { + this.optional = optional; + } + + @XmlAttribute(name = "default") + public String getDefaultValue() { + return defaultValue; + } + + public void setDefaultValue(String defaultValue) { + this.defaultValue = defaultValue; + } +} 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 new file mode 100644 index 0000000000..413efd6626 --- /dev/null +++ b/components/application-mgt/org.wso2.carbon.device.application.mgt.core/src/main/java/org/wso2/carbon/device/application/mgt/core/exception/PlatformManagementDAOException.java @@ -0,0 +1,31 @@ +/* +* 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.exception; + +import org.wso2.carbon.device.application.mgt.common.exception.PlatformManagementException; + +public class PlatformManagementDAOException extends PlatformManagementException { + + public PlatformManagementDAOException(String message, Throwable ex) { + super(message, ex); + } + + public PlatformManagementDAOException(String message) { + super(message); + } +} diff --git a/components/application-mgt/org.wso2.carbon.device.application.mgt.core/src/main/java/org/wso2/carbon/device/application/mgt/core/impl/ApplicationManagerImpl.java b/components/application-mgt/org.wso2.carbon.device.application.mgt.core/src/main/java/org/wso2/carbon/device/application/mgt/core/impl/ApplicationManagerImpl.java index baa915ee9c..5fbb970ae9 100644 --- a/components/application-mgt/org.wso2.carbon.device.application.mgt.core/src/main/java/org/wso2/carbon/device/application/mgt/core/impl/ApplicationManagerImpl.java +++ b/components/application-mgt/org.wso2.carbon.device.application.mgt.core/src/main/java/org/wso2/carbon/device/application/mgt/core/impl/ApplicationManagerImpl.java @@ -22,6 +22,7 @@ import org.wso2.carbon.device.application.mgt.common.Application; import org.wso2.carbon.device.application.mgt.common.ApplicationList; import org.wso2.carbon.device.application.mgt.common.Filter; import org.wso2.carbon.device.application.mgt.common.exception.ApplicationManagementException; +import org.wso2.carbon.device.application.mgt.common.exception.DBConnectionException; import org.wso2.carbon.device.application.mgt.common.services.ApplicationManager; import org.wso2.carbon.device.application.mgt.core.dao.ApplicationDAO; import org.wso2.carbon.device.application.mgt.core.dao.common.DAOFactory; @@ -39,7 +40,7 @@ public class ApplicationManagerImpl implements ApplicationManager { ConnectionManagerUtil.openConnection(); ApplicationDAO applicationDAO = DAOFactory.getApplicationDAO(); return applicationDAO.getApplications(filter); - } finally { + } 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/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 52c302fde6..98e31bca1a 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 @@ -17,7 +17,84 @@ */ package org.wso2.carbon.device.application.mgt.core.impl; +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.utils.multitenancy.MultitenantConstants; + +import java.util.HashMap; +import java.util.List; +import java.util.Map; public class PlatformManagerImpl implements PlatformManager { + private Map> inMemoryStore; + + public PlatformManagerImpl() { + this.inMemoryStore = new HashMap<>(); + } + + + @Override + public List getPlatforms(String tenantDomain) throws PlatformManagementException { + List platforms = DAOFactory.getPlatformDAO().getPlatforms(tenantDomain); + int platformIndex = 0; + for (Platform platform : platforms) { + if (platform.isFileBased()) { + Map superTenantPlatforms = this.inMemoryStore.get(MultitenantConstants.SUPER_TENANT_DOMAIN_NAME); + Platform registeredPlatform = superTenantPlatforms.get(platform.getCode()); + if (registeredPlatform != null) { + platforms.set(platformIndex, new Platform(registeredPlatform)); + } else { + platforms.remove(platformIndex); + } + } + platformIndex++; + } + return platforms; + } + + @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.getCode() + " cannot be shared by the tenant domain - " + tenantDomain); + } + if (platform.isFileBased()) { + Map tenantPlatforms = this.inMemoryStore.get(tenantDomain); + if (tenantPlatforms == null) { + tenantPlatforms = new HashMap<>(); + this.inMemoryStore.put(tenantDomain, tenantPlatforms); + } + if (tenantPlatforms.get(platform.getCode()) == null) { + tenantPlatforms.put(platform.getCode(), platform); + } else { + throw new PlatformManagementException("Platform - " + platform.getCode() + " is already registered!"); + } + } else { + DAOFactory.getPlatformDAO().register(tenantDomain, platform); + } + } + + @Override + public void unregister(String tenantDomain, String platformCode, boolean isFileBased) throws PlatformManagementException { + if (isFileBased) { + Map tenantPlatforms = this.inMemoryStore.get(tenantDomain); + if (tenantPlatforms != null) { + this.inMemoryStore.remove(platformCode); + } + } else { + DAOFactory.getPlatformDAO().unregister(tenantDomain, platformCode); + } + } + + @Override + public void addMapping(String tenantDomain, String platformCode) throws PlatformManagementException { + DAOFactory.getPlatformDAO().addMapping(tenantDomain, platformCode); + } + + @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/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 948c143220..3b21fa0f0c 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 @@ -20,12 +20,14 @@ package org.wso2.carbon.device.application.mgt.core.internal; import org.wso2.carbon.device.application.mgt.common.services.*; import org.wso2.carbon.device.mgt.core.service.DeviceManagementProviderService; +import org.wso2.carbon.user.core.service.RealmService; public class DataHolder { - //TODO move the osgi classes here. private DeviceManagementProviderService deviceManagementService; + private RealmService realmService; + private ApplicationManager applicationManager; private ApplicationReleaseManager releaseManager; @@ -64,10 +66,6 @@ public class DataHolder { this.deviceManagementService = deviceManagementService; } - public static DataHolder getApplicationMgtDataHolder() { - return applicationMgtDataHolder; - } - public ApplicationManager getApplicationManager() { return applicationManager; } @@ -143,4 +141,16 @@ public class DataHolder { public void setApplicationUploadManager(ApplicationUploadManager applicationUploadManager) { this.applicationUploadManager = applicationUploadManager; } + + public ApplicationUploadManager getApplicationUploadManager() { + return applicationUploadManager; + } + + public RealmService getRealmService() { + return realmService; + } + + public void setRealmService(RealmService realmService) { + this.realmService = realmService; + } } 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 11a9957aa9..5e432f067d 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 @@ -28,6 +28,7 @@ import org.wso2.carbon.device.application.mgt.core.config.ConfigurationManager; import org.wso2.carbon.device.application.mgt.core.dao.common.DAOFactory; import org.wso2.carbon.device.application.mgt.core.util.ApplicationManagementUtil; import org.wso2.carbon.device.mgt.core.service.DeviceManagementProviderService; +import org.wso2.carbon.user.core.service.RealmService; import javax.naming.NamingException; @@ -39,6 +40,13 @@ import javax.naming.NamingException; * policy="dynamic" * bind="setDeviceManagementService" * unbind="unsetDeviceManagementService" + * @scr.reference name="realm.service" + * immediate="true" + * interface="org.wso2.carbon.user.core.service.RealmService" + * cardinality="1..1" + * policy="dynamic" + * bind="setRealmService" + * unbind="unsetRealmService" */ public class ServiceComponent { @@ -119,4 +127,12 @@ public class ServiceComponent { } DataHolder.getInstance().setDeviceManagementService(null); } + + protected void setRealmService(RealmService realmService) { + DataHolder.getInstance().setRealmService(realmService); + } + + protected void unsetRealmService(RealmService realmService) { + DataHolder.getInstance().setRealmService(null); + } } 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 b7c7d4830b..566e681f92 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 @@ -45,7 +45,7 @@ public class ConnectionManagerUtil { return currentConnection; } - public static void openConnection() throws DBConnectionException { + public static Connection openConnection() throws DBConnectionException { Connection conn = currentConnection.get(); if (conn != null) { throw new IllegalTransactionStateException("A transaction is already active within the context of " + @@ -60,6 +60,7 @@ public class ConnectionManagerUtil { } currentConnection.set(conn); currentTxState.set(TxState.CONNECTION_BORROWED); + return conn; } public static Connection getConnection() throws DBConnectionException { diff --git a/components/application-mgt/org.wso2.carbon.device.application.mgt.core/src/main/java/org/wso2/carbon/device/application/mgt/core/util/Constants.java b/components/application-mgt/org.wso2.carbon.device.application.mgt.core/src/main/java/org/wso2/carbon/device/application/mgt/core/util/Constants.java index 6ed1fe951a..ace8622010 100644 --- a/components/application-mgt/org.wso2.carbon.device.application.mgt.core/src/main/java/org/wso2/carbon/device/application/mgt/core/util/Constants.java +++ b/components/application-mgt/org.wso2.carbon.device.application.mgt.core/src/main/java/org/wso2/carbon/device/application/mgt/core/util/Constants.java @@ -28,6 +28,8 @@ public class Constants { public static final String DEFAULT_CONFIG_FILE_LOCATION = CarbonUtils.getCarbonConfigDirPath() + File.separator + Constants.APPLICATION_CONFIG_XML_FILE; + public static final String PLATFORMS_DEPLOYMENT_DIR_NAME = "platforms"; + public static final String PLATFORM_DEPLOYMENT_EXT = ".xml"; public static final class DataBaseTypes { diff --git a/components/application-mgt/org.wso2.carbon.device.application.mgt.core/src/main/resources/META-INF/component.xml b/components/application-mgt/org.wso2.carbon.device.application.mgt.core/src/main/resources/META-INF/component.xml new file mode 100644 index 0000000000..77d2788a7a --- /dev/null +++ b/components/application-mgt/org.wso2.carbon.device.application.mgt.core/src/main/resources/META-INF/component.xml @@ -0,0 +1,23 @@ + + + + + platforms + xml + org.wso2.carbon.device.application.mgt.core.deployer.PlatformDeployer + + + 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 new file mode 100644 index 0000000000..e3a1db0b92 --- /dev/null +++ b/components/application-mgt/org.wso2.carbon.device.application.mgt.core/src/test/resources/Platform.xml @@ -0,0 +1,6 @@ + + + test + number + + \ No newline at end of file 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 new file mode 100644 index 0000000000..5733fcd55b --- /dev/null +++ b/components/application-mgt/org.wso2.carbon.device.application.mgt.core/src/test/resources/h2.sql @@ -0,0 +1,29 @@ +CREATE TABLE APPM_PLATFORM ( +ID INT NOT NULL AUTO_INCREMENT, +CODE VARCHAR (100) NOT NULL, +TENANT_DOMAIN VARCHAR (100) NOT NULL , +NAME VARCHAR (255) NOT NULL, +DESCRIPTION LONGVARCHAR, +IS_SHARED BOOLEAN, +ICON_NAME VARCHAR (100), +PRIMARY KEY (ID, CODE, TENANT_DOMAIN) +); + +CREATE TABLE APPM_PLATFORM_PROPERTIES ( +ID INT NOT NULL AUTO_INCREMENT, +PLATFORM_ID INTEGER 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) +); + +CREATE TABLE APPM_PLATFORM_TENANT_MAPPING ( +ID INT NOT NULL AUTO_INCREMENT, +TENANT_DOMAIN 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) +) + diff --git a/components/application-mgt/org.wso2.carbon.device.application.mgt.extensions/pom.xml b/components/application-mgt/org.wso2.carbon.device.application.mgt.extensions/pom.xml deleted file mode 100644 index d7895183a4..0000000000 --- a/components/application-mgt/org.wso2.carbon.device.application.mgt.extensions/pom.xml +++ /dev/null @@ -1,141 +0,0 @@ - - - - - - org.wso2.carbon.devicemgt - application-mgt - 2.0.63-SNAPSHOT - ../pom.xml - - - 4.0.0 - org.wso2.carbon.device.application.mgt.extensions - 2.0.63-SNAPSHOT - bundle - WSO2 Carbon - Application Management Extensions - WSO2 Carbon - Application Management Extensions - http://wso2.org - - - - - org.apache.felix - maven-scr-plugin - - - org.apache.felix - maven-bundle-plugin - 1.4.0 - true - - - ${project.artifactId} - ${project.artifactId} - ${carbon.device.mgt.version} - Application Management Extensions Bundle - - org.osgi.framework, - org.osgi.service.component, - org.apache.commons.logging, - javax.xml.*, - javax.xml.parsers;version="${javax.xml.parsers.import.pkg.version}";resolution:=optional, - org.wso2.carbon.context.*, - org.wso2.carbon.utils.*, - org.w3c.dom, - org.json, - javax.sql, - com.google.gson, - javax.naming, - javax.xml.bind.annotation, - javax.xml.bind, - org.wso2.carbon.device.application.mgt.common.*, - org.wso2.carbon.device.mgt.core.*, - org.wso2.carbon.device.mgt.common.*, - - - org.wso2.carbon.device.application.mgt.extensions.* - - - - - - - - - - org.eclipse.osgi - org.eclipse.osgi - - - org.eclipse.osgi - org.eclipse.osgi.services - - - org.eclipse.equinox - org.eclipse.equinox.common - - - org.testng - testng - - - org.wso2.carbon - org.wso2.carbon.logging - - - org.wso2.carbon - org.wso2.carbon.utils - - - commons-codec.wso2 - commons-codec - - - commons-io.wso2 - commons-io - - - org.slf4j - slf4j-simple - test - - - org.json.wso2 - json - - - com.google.code.gson - gson - - - org.wso2.carbon.devicemgt - org.wso2.carbon.device.application.mgt.common - - - org.wso2.carbon.devicemgt - org.wso2.carbon.device.mgt.common - - - org.wso2.carbon.devicemgt - org.wso2.carbon.device.mgt.core - - - - diff --git a/components/application-mgt/pom.xml b/components/application-mgt/pom.xml index 043f32a694..0feac88010 100644 --- a/components/application-mgt/pom.xml +++ b/components/application-mgt/pom.xml @@ -38,7 +38,7 @@ org.wso2.carbon.device.application.mgt.common org.wso2.carbon.device.application.mgt.api org.wso2.carbon.device.application.mgt.ui - + From fe26352ec9a8796b3e1e14c5d0e7e325fc84ee44 Mon Sep 17 00:00:00 2001 From: sinthuja Date: Tue, 13 Jun 2017 13:55:04 +0530 Subject: [PATCH 2/3] 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) ) From 6020b3fb6530ce9064eb5f33f0c20d54dfe4f401 Mon Sep 17 00:00:00 2001 From: sinthuja Date: Tue, 13 Jun 2017 18:15:58 +0530 Subject: [PATCH 3/3] Adding platform manager first API. --- .../device/application/mgt/api/APIUtil.java | 6 +- .../api/services/PlatformManagementAPI.java | 88 +++++++++++++++++-- .../impl/PlatformManagementAPIImpl.java | 41 +++++++++ .../application/mgt/common/Platform.java | 4 + .../mgt/common/services/PlatformManager.java | 2 + .../pom.xml | 3 +- .../application/mgt/core/dao/PlatformDAO.java | 2 + .../mgt/core/dao/impl/PlatformDAOImpl.java | 2 +- .../mgt/core/impl/PlatformManagerImpl.java | 32 +++++++ 9 files changed, 168 insertions(+), 12 deletions(-) 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 03e3666178..5ffc2ccda5 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 @@ -77,8 +77,12 @@ public class APIUtil { } public static Response getResponse(ApplicationManagementException ex, Response.Status status) { + return getResponse(ex.getMessage(), status); + } + + public static Response getResponse(String message, Response.Status status) { ErrorResponse errorMessage = new ErrorResponse(); - errorMessage.setMessage(ex.getMessage()); + errorMessage.setMessage(message); if (status == null) { status = Response.Status.INTERNAL_SERVER_ERROR; } 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 3cc5761f6e..7741ccf657 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 @@ -18,7 +18,6 @@ package org.wso2.carbon.device.application.mgt.api.services;/* 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; @@ -30,11 +29,11 @@ import javax.ws.rs.core.Response; "such as get all the available platform for a tenant, etc.") @Produces(MediaType.APPLICATION_JSON) @Consumes(MediaType.APPLICATION_JSON) +@Path("/platforms") public interface PlatformManagementAPI { public final static String SCOPE = "scope"; @GET - @Path("platforms") @Produces(MediaType.APPLICATION_JSON) @Consumes(MediaType.APPLICATION_JSON) @ApiOperation( @@ -42,11 +41,11 @@ public interface PlatformManagementAPI { produces = MediaType.APPLICATION_JSON, httpMethod = "GET", value = "get all platforms", - notes = "This will get all applications", - tags = "Application Management", + notes = "This will get all platforms that is visible for tenants", + tags = "Platform Management", extensions = { @Extension(properties = { - @ExtensionProperty(name = SCOPE, value = "perm:get-platforms") + @ExtensionProperty(name = SCOPE, value = "perm:get-platform") }) } ) @@ -57,10 +56,6 @@ public interface PlatformManagementAPI { 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.", @@ -79,4 +74,79 @@ public interface PlatformManagementAPI { @Size(max = 45) String status ); + + @GET + @Path("/{code}") + @Produces(MediaType.APPLICATION_JSON) + @Consumes(MediaType.APPLICATION_JSON) + @ApiOperation( + consumes = MediaType.APPLICATION_JSON, + produces = MediaType.APPLICATION_JSON, + httpMethod = "GET", + value = "get platform", + notes = "This will get application which was registered with {code}", + tags = "Platform Management", + extensions = { + @Extension(properties = { + @ExtensionProperty(name = SCOPE, value = "perm:get-platform") + }) + } + ) + @ApiResponses( + value = { + @ApiResponse( + code = 200, + message = "OK. \n Successfully got requested platform.", + response = Platform.class), + @ApiResponse( + code = 500, + message = "Internal Server Error. \n Error occurred while getting the platform.", + response = ErrorResponse.class) + }) + Response getPlatform( + @ApiParam( + name = "code", + required = true) + @PathParam("code") + @Size(max = 45) + String code + ); + + @POST + @Produces(MediaType.APPLICATION_JSON) + @Consumes(MediaType.APPLICATION_JSON) + @ApiOperation( + consumes = MediaType.APPLICATION_JSON, + produces = MediaType.APPLICATION_JSON, + httpMethod = "POST", + value = "Add Platform", + notes = "This will a platform for the tenant space", + tags = "Platform Management", + extensions = { + @Extension(properties = { + @ExtensionProperty(name = SCOPE, value = "perm:add-platform") + }) + } + ) + @ApiResponses( + value = { + @ApiResponse( + code = 200, + message = "OK. \n Successfully added 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 addPlatform( + @ApiParam( + name = "platform", + value = "The payload of the platform", + required = true) + Platform platform + ); + } 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 5448b2f878..401a82d022 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 @@ -17,6 +17,7 @@ */ package org.wso2.carbon.device.application.mgt.api.services.impl; +import io.swagger.annotations.ApiParam; import org.apache.commons.logging.Log; import org.apache.commons.logging.LogFactory; import org.wso2.carbon.context.PrivilegedCarbonContext; @@ -24,7 +25,12 @@ 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 org.wso2.carbon.device.application.mgt.core.exception.PlatformManagementDAOException; +import javax.validation.constraints.Size; +import javax.ws.rs.GET; +import javax.ws.rs.Path; +import javax.ws.rs.PathParam; import javax.ws.rs.QueryParam; import javax.ws.rs.core.Response; import java.util.ArrayList; @@ -38,6 +44,7 @@ public class PlatformManagementAPIImpl implements PlatformManagementAPI { private static Log log = LogFactory.getLog(PlatformManagementAPIImpl.class); + @Override public Response getPlatforms(@QueryParam("status") String status) { String tenantDomain = PrivilegedCarbonContext.getThreadLocalCarbonContext().getTenantDomain(true); @@ -73,4 +80,38 @@ public class PlatformManagementAPIImpl implements PlatformManagementAPI { return APIUtil.getResponse(e, Response.Status.INTERNAL_SERVER_ERROR); } } + + @GET + @Override + @Path("/{code}") + public Response getPlatform(@PathParam("code") String code) { + String tenantDomain = PrivilegedCarbonContext.getThreadLocalCarbonContext().getTenantDomain(true); + try { + Platform platform = APIUtil.getPlatformManager().getPlatform(tenantDomain, code); + return Response.status(Response.Status.OK).entity(platform).build(); + } catch (PlatformManagementDAOException e) { + return APIUtil.getResponse(e, Response.Status.INTERNAL_SERVER_ERROR); + } catch (PlatformManagementException e) { + return APIUtil.getResponse(e, Response.Status.NOT_FOUND); + } + } + + @Override + public Response addPlatform(Platform platform) { + String tenantDomain = PrivilegedCarbonContext.getThreadLocalCarbonContext().getTenantDomain(true); + try { + if (platform != null) { + if (platform.validate()) { + APIUtil.getPlatformManager().register(tenantDomain, platform); + return Response.status(Response.Status.CREATED).build(); + } else { + return APIUtil.getResponse("Invalid payload! Platform code and names 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) { + 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/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 b58410138d..4f4d08b88c 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 @@ -165,6 +165,10 @@ public class Platform implements Cloneable { this.defaultTenantMapping = defaultTenantMapping; } + public boolean validate(){ + return !(name == null || code == null); + } + public static class Property implements Cloneable { private String name; 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 05f538586d..9685054840 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 @@ -33,6 +33,8 @@ public interface PlatformManager { List getPlatforms(String tenantDomain) throws PlatformManagementException; + Platform getPlatform(String tenantDomain, String code) throws PlatformManagementException; + void register(String tenantDomain, Platform platform) throws PlatformManagementException; void unregister(String tenantDomain, String platformCode, boolean isFileBased) 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 cb64c11f24..1d74027d97 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 @@ -72,7 +72,8 @@ org.wso2.carbon.device.mgt.core.*, org.wso2.carbon.device.mgt.common.*, org.apache.axis2.*, - org.wso2.carbon.user.core.* + org.wso2.carbon.user.core.*, + org.wso2.carbon.user.api.* !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/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 65a1c4ca91..a569d5061b 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 @@ -35,4 +35,6 @@ public interface PlatformDAO { List getPlatforms(String tenantDomain) throws PlatformManagementDAOException; + Platform getPlatform(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 baef15102e..db0b880362 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 @@ -264,7 +264,7 @@ public class PlatformDAOImpl implements PlatformDAO { } } - private Platform getPlatform(String tenantDomain, String platformCode) throws PlatformManagementDAOException { + public Platform getPlatform(String tenantDomain, String platformCode) throws PlatformManagementDAOException { 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 { 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 4bec94ad5c..cf4c468fef 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 @@ -70,6 +70,38 @@ public class PlatformManagerImpl implements PlatformManager { return platforms; } + @Override + public Platform getPlatform(String tenantDomain, String code) throws PlatformManagementException { + Platform platform = getPlatformFromInMemory(tenantDomain, code); + if (platform == null) { + platform = DAOFactory.getPlatformDAO().getPlatform(tenantDomain, code); + } + if (platform != null) { + return new Platform(platform); + } + throw new PlatformManagementException("No platform was found for tenant - "+ tenantDomain+" with Platform code - "+ code); + } + + private Platform getPlatformFromInMemory(String tenantDomain, String code) { + Map platformMap = this.inMemoryStore.get(tenantDomain); + if (platformMap != null) { + Platform platform = platformMap.get(code); + if (platform != null) { + return platform; + } + } + if (!tenantDomain.equalsIgnoreCase(MultitenantConstants.SUPER_TENANT_DOMAIN_NAME)) { + platformMap = this.inMemoryStore.get(MultitenantConstants.SUPER_TENANT_DOMAIN_NAME); + if (platformMap != null) { + Platform platform = platformMap.get(code); + if (platform != null && platform.isShared()) { + return platform; + } + } + } + return null; + } + @Override public synchronized void register(String tenantDomain, Platform platform) throws PlatformManagementException { if (platform.isShared() && !tenantDomain.equals(MultitenantConstants.SUPER_TENANT_DOMAIN_NAME)) {