Merge remote-tracking branch 'upstream/application-mgt' into appm

feature/appm-store/pbac
Chathura Ekanayake 8 years ago
commit 73a0a119b1

@ -1,4 +1,4 @@
package org.wso2.carbon.device.application.mgt.api.services;/* /*
* Copyright (c) 2017, WSO2 Inc. (http://www.wso2.org) All Rights Reserved. * Copyright (c) 2017, WSO2 Inc. (http://www.wso2.org) All Rights Reserved.
* WSO2 Inc. licenses this file to you under the Apache License, * WSO2 Inc. licenses this file to you under the Apache License,
* Version 2.0 (the "License"); you may not use this file except * Version 2.0 (the "License"); you may not use this file except
@ -16,22 +16,82 @@ package org.wso2.carbon.device.application.mgt.api.services;/*
* *
*/ */
import io.swagger.annotations.*; package org.wso2.carbon.device.application.mgt.api.services;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import io.swagger.annotations.ApiResponse;
import io.swagger.annotations.ApiResponses;
import io.swagger.annotations.Extension;
import io.swagger.annotations.ExtensionProperty;
import io.swagger.annotations.Info;
import io.swagger.annotations.SwaggerDefinition;
import io.swagger.annotations.Tag;
import org.wso2.carbon.apimgt.annotations.api.Scopes;
import org.wso2.carbon.device.application.mgt.api.beans.ErrorResponse; import org.wso2.carbon.device.application.mgt.api.beans.ErrorResponse;
import org.wso2.carbon.device.application.mgt.common.Platform; import org.wso2.carbon.device.application.mgt.common.Platform;
import javax.validation.constraints.Size; import javax.validation.constraints.Size;
import javax.ws.rs.*; import javax.ws.rs.Consumes;
import javax.ws.rs.GET;
import javax.ws.rs.POST;
import javax.ws.rs.PUT;
import javax.ws.rs.Path;
import javax.ws.rs.PathParam;
import javax.ws.rs.Produces;
import javax.ws.rs.QueryParam;
import javax.ws.rs.core.MediaType; import javax.ws.rs.core.MediaType;
import javax.ws.rs.core.Response; import javax.ws.rs.core.Response;
/**
* API for handling platform related operations in application management.
*/
@SwaggerDefinition(
info = @Info(
version = "1.0.0",
title = "Platform Management Service",
extensions = {
@Extension(properties = {
@ExtensionProperty(name = "name", value = "PlatformManagementService"),
@ExtensionProperty(name = "context", value = "/api/application-mgt/v1.0/platforms"),
})
}
),
tags = {
@Tag(name = "application_management", description = "Platform Management APIS related with "
+ "Application Management")
}
)
@Scopes (
scopes = {
@org.wso2.carbon.apimgt.annotations.api.Scope(
name = "Get platform details",
description = "Get platform details",
key = "perm:platform:get",
permissions = {"/device-mgt/platform/get"}
),
@org.wso2.carbon.apimgt.annotations.api.Scope(
name = "Add a platform",
description = "Add a platform",
key = "perm:platform:add",
permissions = {"/device-mgt/platform/add"}
),
@org.wso2.carbon.apimgt.annotations.api.Scope(
name = "Update a platform",
description = "Update a platform",
key = "perm:platform:update",
permissions = {"/device-mgt/platform/update"}
)
}
)
@Api(value = "Platform Management", description = "This API carries all platform management related operations " + @Api(value = "Platform Management", description = "This API carries all platform management related operations " +
"such as get all the available platform for a tenant, etc.") "such as get all the available platform for a tenant, etc.")
@Path("/platforms")
@Produces(MediaType.APPLICATION_JSON) @Produces(MediaType.APPLICATION_JSON)
@Consumes(MediaType.APPLICATION_JSON) @Consumes(MediaType.APPLICATION_JSON)
@Path("/platforms")
public interface PlatformManagementAPI { public interface PlatformManagementAPI {
public final static String SCOPE = "scope"; String SCOPE = "scope";
@GET @GET
@Produces(MediaType.APPLICATION_JSON) @Produces(MediaType.APPLICATION_JSON)
@ -45,7 +105,7 @@ public interface PlatformManagementAPI {
tags = "Platform Management", tags = "Platform Management",
extensions = { extensions = {
@Extension(properties = { @Extension(properties = {
@ExtensionProperty(name = SCOPE, value = "perm:get-platform") @ExtensionProperty(name = SCOPE, value = "perm:platform:get")
}) })
} }
) )
@ -62,14 +122,12 @@ public interface PlatformManagementAPI {
response = ErrorResponse.class) response = ErrorResponse.class)
}) })
Response getPlatforms( Response getPlatforms(
@ApiParam( @ApiParam(name = "status", allowableValues = "ENABLED, DISABLED, ALL", value =
name = "status", "Provide the status of platform for that tenant:\n"
allowableValues = "ENABLED, DISABLED, ALL", + "- ENABLED: The platforms that are currently enabled for the tenant\n"
value = "Provide the status of platform for that tenant:\n" + + "- DISABLED: The platforms that can be used by the tenant but disabled "
"- ENABLED: The platforms that are currently enabled for the tenant\n" + + "to be used for 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)
"- ALL: All the list of platforms that can be used by the tenant",
required = false)
@QueryParam("status") @QueryParam("status")
@Size(max = 45) @Size(max = 45)
String status String status
@ -84,11 +142,11 @@ public interface PlatformManagementAPI {
produces = MediaType.APPLICATION_JSON, produces = MediaType.APPLICATION_JSON,
httpMethod = "GET", httpMethod = "GET",
value = "get platform", value = "get platform",
notes = "This will get application which was registered with {code}", notes = "This will return the platform which is registered with {identifier}",
tags = "Platform Management", tags = "Platform Management",
extensions = { extensions = {
@Extension(properties = { @Extension(properties = {
@ExtensionProperty(name = SCOPE, value = "perm:get-platform") @ExtensionProperty(name = SCOPE, value = "perm:platform:get")
}) })
} }
) )
@ -124,7 +182,7 @@ public interface PlatformManagementAPI {
tags = "Platform Management", tags = "Platform Management",
extensions = { extensions = {
@Extension(properties = { @Extension(properties = {
@ExtensionProperty(name = SCOPE, value = "perm:add-platform") @ExtensionProperty(name = SCOPE, value = "perm:platform:add")
}) })
} }
) )
@ -162,7 +220,7 @@ public interface PlatformManagementAPI {
tags = "Platform Management", tags = "Platform Management",
extensions = { extensions = {
@Extension(properties = { @Extension(properties = {
@ExtensionProperty(name = SCOPE, value = "perm:update-platform") @ExtensionProperty(name = SCOPE, value = "perm:platform:update")
}) })
} }
) )

@ -26,12 +26,18 @@ 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.common.exception.PlatformManagementException;
import org.wso2.carbon.device.application.mgt.core.exception.PlatformManagementDAOException; import org.wso2.carbon.device.application.mgt.core.exception.PlatformManagementDAOException;
import javax.validation.constraints.Size;
import javax.ws.rs.*;
import javax.ws.rs.core.Response;
import java.util.ArrayList; import java.util.ArrayList;
import java.util.List; import java.util.List;
import javax.validation.constraints.Size;
import javax.ws.rs.GET;
import javax.ws.rs.POST;
import javax.ws.rs.PUT;
import javax.ws.rs.Path;
import javax.ws.rs.PathParam;
import javax.ws.rs.QueryParam;
import javax.ws.rs.core.Response;
@Path("/platforms")
public class PlatformManagementAPIImpl implements PlatformManagementAPI { public class PlatformManagementAPIImpl implements PlatformManagementAPI {
private static final String ALL_STATUS = "ALL"; private static final String ALL_STATUS = "ALL";
@ -40,9 +46,14 @@ public class PlatformManagementAPIImpl implements PlatformManagementAPI {
private static Log log = LogFactory.getLog(PlatformManagementAPIImpl.class); private static Log log = LogFactory.getLog(PlatformManagementAPIImpl.class);
@GET
@Override @Override
public Response getPlatforms(@QueryParam("status") String status) { public Response getPlatforms(@QueryParam("status") String status) {
String tenantDomain = PrivilegedCarbonContext.getThreadLocalCarbonContext().getTenantDomain(true); String tenantDomain = PrivilegedCarbonContext.getThreadLocalCarbonContext().getTenantDomain(true);
if (log.isDebugEnabled()) {
log.debug("API request received for getting the platforms with the status " + status);
}
try { try {
List<Platform> platforms = APIUtil.getPlatformManager().getPlatforms(tenantDomain); List<Platform> platforms = APIUtil.getPlatformManager().getPlatforms(tenantDomain);
List<Platform> results; List<Platform> results;
@ -69,6 +80,9 @@ public class PlatformManagementAPIImpl implements PlatformManagementAPI {
} else { } else {
results = platforms; results = platforms;
} }
if (log.isDebugEnabled()) {
log.debug("Number of platforms with the status " + status + " : " + results.size());
}
return Response.status(Response.Status.OK).entity(results).build(); return Response.status(Response.Status.OK).entity(results).build();
} catch (PlatformManagementException e) { } catch (PlatformManagementException e) {
log.error("Error while getting the platforms for tenant - " + tenantDomain, e); log.error("Error while getting the platforms for tenant - " + tenantDomain, e);
@ -91,6 +105,7 @@ public class PlatformManagementAPIImpl implements PlatformManagementAPI {
} }
} }
@POST
@Override @Override
public Response addPlatform(Platform platform) { public Response addPlatform(Platform platform) {
String tenantDomain = PrivilegedCarbonContext.getThreadLocalCarbonContext().getTenantDomain(true); String tenantDomain = PrivilegedCarbonContext.getThreadLocalCarbonContext().getTenantDomain(true);
@ -100,10 +115,12 @@ public class PlatformManagementAPIImpl implements PlatformManagementAPI {
APIUtil.getPlatformManager().register(tenantDomain, platform); APIUtil.getPlatformManager().register(tenantDomain, platform);
return Response.status(Response.Status.CREATED).build(); return Response.status(Response.Status.CREATED).build();
} else { } else {
return APIUtil.getResponse("Invalid payload! Platform ID and names are mandatory fields!", Response.Status.BAD_REQUEST); return APIUtil.getResponse("Invxalid payload! Platform ID and names are mandatory fields!",
Response.Status.BAD_REQUEST);
} }
} else { } else {
return APIUtil.getResponse("Invalid payload! Platform needs to be passed as payload!", Response.Status.BAD_REQUEST); return APIUtil.getResponse("Invalid payload! Platform needs to be passed as payload!",
Response.Status.BAD_REQUEST);
} }
} catch (PlatformManagementException e) { } catch (PlatformManagementException e) {
return APIUtil.getResponse(e, Response.Status.INTERNAL_SERVER_ERROR); return APIUtil.getResponse(e, Response.Status.INTERNAL_SERVER_ERROR);

@ -29,8 +29,8 @@
--> -->
<PermissionConfiguration> <PermissionConfiguration>
<APIVersion></APIVersion> <APIVersion></APIVersion>
<!--Permission Tree Name-->
<!--End of Permission Tree--> <!-- Application related permissions -->
<Permission> <Permission>
<name>Get Application</name> <name>Get Application</name>
<path>/device-mgt/application/get</path> <path>/device-mgt/application/get</path>
@ -43,4 +43,26 @@
<url>/application-mgt/applications</url> <url>/application-mgt/applications</url>
<method>POST</method> <method>POST</method>
</Permission> </Permission>
<!-- Platform related permissions -->
<Permission>
<name>Get Platform</name>
<path>/device-mgt/platform/get</path>
<url>/application-mgt/platforms/*</url>
<method>GET</method>
</Permission>
<Permission>
<name>Add Platform</name>
<path>/device-mgt/platform/add</path>
<url>/application-mgt/platforms</url>
<method>POST</method>
</Permission>
<Permission>
<name>Update Platform</name>
<path>/device-mgt/platform/update</path>
<url>/application-mgt/platforms/*</url>
<method>PUT</method>
</Permission>
</PermissionConfiguration> </PermissionConfiguration>

@ -73,7 +73,9 @@
org.wso2.carbon.device.mgt.common.*, org.wso2.carbon.device.mgt.common.*,
org.apache.axis2.*, org.apache.axis2.*,
org.wso2.carbon.user.core.*, org.wso2.carbon.user.core.*,
org.wso2.carbon.user.api.* org.wso2.carbon.user.api.*,
org.wso2.carbon.ndatasource.core,
org.apache.axiom.om.*; version="${axiom.osgi.version.range}"
</Import-Package> </Import-Package>
<Export-Package> <Export-Package>
!org.wso2.carbon.device.application.mgt.core.internal.*, !org.wso2.carbon.device.application.mgt.core.internal.*,
@ -153,6 +155,10 @@
<groupId>org.wso2.carbon</groupId> <groupId>org.wso2.carbon</groupId>
<artifactId>org.wso2.carbon.user.core</artifactId> <artifactId>org.wso2.carbon.user.core</artifactId>
</dependency> </dependency>
<dependency>
<groupId>org.wso2.carbon</groupId>
<artifactId>org.wso2.carbon.ndatasource.core</artifactId>
</dependency>
</dependencies> </dependencies>
</project> </project>

@ -18,9 +18,13 @@
*/ */
package org.wso2.carbon.device.application.mgt.core.dao.common; package org.wso2.carbon.device.application.mgt.core.dao.common;
import org.apache.axiom.om.OMElement;
import org.apache.axiom.om.util.AXIOMUtil;
import org.apache.commons.logging.Log; import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory; import org.apache.commons.logging.LogFactory;
import org.wso2.carbon.context.PrivilegedCarbonContext;
import org.wso2.carbon.device.application.mgt.common.exception.UnsupportedDatabaseEngineException; import org.wso2.carbon.device.application.mgt.common.exception.UnsupportedDatabaseEngineException;
import org.wso2.carbon.device.application.mgt.core.config.ConfigurationManager;
import org.wso2.carbon.device.application.mgt.core.dao.ApplicationDAO; import org.wso2.carbon.device.application.mgt.core.dao.ApplicationDAO;
import org.wso2.carbon.device.application.mgt.core.dao.LifecycleStateDAO; import org.wso2.carbon.device.application.mgt.core.dao.LifecycleStateDAO;
import org.wso2.carbon.device.application.mgt.core.dao.PlatformDAO; import org.wso2.carbon.device.application.mgt.core.dao.PlatformDAO;
@ -28,9 +32,20 @@ import org.wso2.carbon.device.application.mgt.core.dao.impl.application.H2Applic
import org.wso2.carbon.device.application.mgt.core.dao.impl.application.MySQLApplicationDAOImpl; import org.wso2.carbon.device.application.mgt.core.dao.impl.application.MySQLApplicationDAOImpl;
import org.wso2.carbon.device.application.mgt.core.dao.impl.lifecyclestate.GenericLifecycleStateImpl; import org.wso2.carbon.device.application.mgt.core.dao.impl.lifecyclestate.GenericLifecycleStateImpl;
import org.wso2.carbon.device.application.mgt.core.dao.impl.platform.GenericPlatformDAOImpl; import org.wso2.carbon.device.application.mgt.core.dao.impl.platform.GenericPlatformDAOImpl;
import org.wso2.carbon.device.application.mgt.core.exception.ApplicationManagementDAOException;
import org.wso2.carbon.device.application.mgt.core.internal.DataHolder;
import org.wso2.carbon.device.application.mgt.core.util.ApplicationMgtDatabaseCreator;
import org.wso2.carbon.device.application.mgt.core.util.Constants; import org.wso2.carbon.device.application.mgt.core.util.Constants;
import org.wso2.carbon.device.application.mgt.core.util.ConnectionManagerUtil; import org.wso2.carbon.device.application.mgt.core.util.ConnectionManagerUtil;
import org.wso2.carbon.ndatasource.core.CarbonDataSource;
import org.wso2.carbon.ndatasource.core.DataSourceService;
import org.wso2.carbon.utils.dbcreator.DatabaseCreator;
import org.wso2.carbon.utils.multitenancy.MultitenantConstants;
import javax.sql.DataSource;
import javax.xml.namespace.QName;
import javax.xml.stream.XMLStreamException;
import java.sql.SQLException;
/** /**
* This class intends to act as the primary entity that hides all DAO instantiation related complexities and logic so * This class intends to act as the primary entity that hides all DAO instantiation related complexities and logic so
@ -87,5 +102,68 @@ public class DAOFactory {
} }
throw new IllegalStateException("Database engine has not initialized properly."); throw new IllegalStateException("Database engine has not initialized properly.");
} }
/**
* This method initializes the databases by creating the database.
* @throws ApplicationManagementDAOException Exceptions thrown during the creation of the tables
*/
public static void initDatabases() throws ApplicationManagementDAOException {
CarbonDataSource carbonDataSource = null;
DataSource dataSource = null;
String dataSourceName = ConfigurationManager.getInstance()
.getConfiguration().getDatasourceName();
DataSourceService service = DataHolder.getInstance().getDataSourceService();
PrivilegedCarbonContext.startTenantFlow();
PrivilegedCarbonContext.getThreadLocalCarbonContext()
.setTenantDomain(MultitenantConstants.SUPER_TENANT_DOMAIN_NAME, true);
try {
carbonDataSource = service.getDataSource(dataSourceName);
dataSource = (DataSource) carbonDataSource.getDSObject();
if (System.getProperty("setup") == null) {
if (log.isDebugEnabled()) {
log.debug("Application Management Database schema initialization check was skipped since "
+ "\'setup\' variable was not given during startup");
}
} else {
DatabaseCreator databaseCreator = new ApplicationMgtDatabaseCreator(dataSource);
String validationQuery = getValidationQuery(
(String) carbonDataSource.getDSMInfo().getDefinition().getDsXMLConfiguration());
if (!databaseCreator.isDatabaseStructureCreated(validationQuery)) {
databaseCreator.createRegistryDatabase();
if (log.isDebugEnabled()) {
log.debug("Application Management tables are created in the database");
}
}
}
} catch (SQLException e) {
throw new ApplicationManagementDAOException("Error while creating application-mgt database during the "
+ "startup ", e);
} catch (Exception e) {
throw new ApplicationManagementDAOException("Error while creating application-mgt database in the "
+ "startup ", e);
}
}
/**
* To get the the validation query to make sure whether the tables exist already in application management databse
* @param dsXMLConfiguration Datasource XML configurations
* @return Validation query
*/
private static String getValidationQuery(String dsXMLConfiguration) {
String DEFAULT_VALIDATION_QUERY = "SELECT 1";
try {
OMElement omElement = AXIOMUtil.stringToOM(dsXMLConfiguration);
return omElement.getFirstChildWithName(new QName("validationQuery")).getText();
} catch (XMLStreamException e) {
log.error("Error while reading the validation query from the data source configuration of "
+ "application-mgt (application-mgt-datasources.xml", e);
if (log.isDebugEnabled()) {
log.debug("Due to fail to read the validation query from application-mgt datasources, using the "
+ "default validation query : " + DEFAULT_VALIDATION_QUERY);
}
return DEFAULT_VALIDATION_QUERY;
}
}
} }

@ -18,6 +18,8 @@
*/ */
package org.wso2.carbon.device.application.mgt.core.dao.impl.platform; package org.wso2.carbon.device.application.mgt.core.dao.impl.platform;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.wso2.carbon.device.application.mgt.common.Platform; import org.wso2.carbon.device.application.mgt.common.Platform;
import org.wso2.carbon.device.application.mgt.common.exception.DBConnectionException; import org.wso2.carbon.device.application.mgt.common.exception.DBConnectionException;
import org.wso2.carbon.device.application.mgt.common.exception.TransactionManagementException; import org.wso2.carbon.device.application.mgt.common.exception.TransactionManagementException;
@ -35,6 +37,7 @@ import java.util.ArrayList;
import java.util.List; import java.util.List;
public class GenericPlatformDAOImpl extends AbstractDAOImpl implements PlatformDAO { public class GenericPlatformDAOImpl extends AbstractDAOImpl implements PlatformDAO {
private static Log log = LogFactory.getLog(GenericPlatformDAOImpl.class);
@Override @Override
public int register(String tenantDomain, Platform platform) throws PlatformManagementDAOException { public int register(String tenantDomain, Platform platform) throws PlatformManagementDAOException {
@ -339,14 +342,22 @@ public class GenericPlatformDAOImpl extends AbstractDAOImpl implements PlatformD
@Override @Override
public List<Platform> getPlatforms(String tenantDomain) throws PlatformManagementDAOException { public List<Platform> getPlatforms(String tenantDomain) throws PlatformManagementDAOException {
String selectQuery = "SELECT MAPPING.ID, PLATFORM.IDENTIFIER FROM (SELECT * FROM APPM_PLATFORM WHERE TENANT_DOMAIN=? OR IS_SHARED = TRUE AND FILE_BASED = FALSE) PLATFORM " + if (log.isDebugEnabled()) {
"LEFT JOIN APPM_PLATFORM_TENANT_MAPPING MAPPING ON PLATFORM.ID = MAPPING.PLATFORM_ID"; log.debug("GetPlaforms request received for the tenantDomain " + tenantDomain);
}
String selectQuery =
"SELECT MAPPING.ID, PLATFORM.IDENTIFIER FROM (SELECT * FROM APPM_PLATFORM WHERE TENANT_DOMAIN=? OR "
+ "IS_SHARED = TRUE AND FILE_BASED = FALSE) PLATFORM LEFT JOIN APPM_PLATFORM_TENANT_MAPPING "
+ "MAPPING ON PLATFORM.ID = MAPPING.PLATFORM_ID";
try { try {
Connection connection = ConnectionManagerUtil.openConnection(); Connection connection = ConnectionManagerUtil.openConnection();
PreparedStatement preparedStatement = connection.prepareStatement(selectQuery); PreparedStatement preparedStatement = connection.prepareStatement(selectQuery);
preparedStatement.setString(1, tenantDomain); preparedStatement.setString(1, tenantDomain);
ResultSet resultSet = preparedStatement.executeQuery(); ResultSet resultSet = preparedStatement.executeQuery();
List<Platform> platforms = new ArrayList<>(); List<Platform> platforms = new ArrayList<>();
if (log.isDebugEnabled()) {
log.debug("Platform retrieved for the tenant domain " + tenantDomain);
}
while (resultSet.next()) { while (resultSet.next()) {
String identifier = resultSet.getString("PLATFORM.IDENTIFIER"); String identifier = resultSet.getString("PLATFORM.IDENTIFIER");
int mappingID = resultSet.getInt("MAPPING.ID"); int mappingID = resultSet.getInt("MAPPING.ID");
@ -357,12 +368,20 @@ public class GenericPlatformDAOImpl extends AbstractDAOImpl implements PlatformD
platform.setEnabled(false); platform.setEnabled(false);
} }
platforms.add(platform); platforms.add(platform);
if (log.isDebugEnabled()) {
log.debug("Platform Identifier - " + identifier + " isEnabled - " + platform.isEnabled());
}
}
if (log.isDebugEnabled()) {
log.debug("Number of platforms available for the tenant domain - " + tenantDomain + " :" + platforms
.size());
} }
return platforms; return platforms;
} catch (DBConnectionException e) { } catch (DBConnectionException e) {
throw new PlatformManagementDAOException("Error occured when loading the platforms for tenant - " + tenantDomain, e); throw new PlatformManagementDAOException(
"Error occured when loading the platforms for tenant - " + tenantDomain, e);
} catch (SQLException e) { } catch (SQLException e) {
throw new PlatformManagementDAOException("Error occured when executing query - " + selectQuery, e); throw new PlatformManagementDAOException("Error occurred when executing query - " + selectQuery, e);
} finally { } finally {
ConnectionManagerUtil.closeConnection(); ConnectionManagerUtil.closeConnection();
} }

@ -17,9 +17,11 @@
*/ */
package org.wso2.carbon.device.application.mgt.core.impl; package org.wso2.carbon.device.application.mgt.core.impl;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.wso2.carbon.device.application.mgt.common.Platform; import org.wso2.carbon.device.application.mgt.common.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.common.exception.PlatformManagementException;
import org.wso2.carbon.device.application.mgt.common.services.PlatformManager;
import org.wso2.carbon.device.application.mgt.core.dao.common.DAOFactory; import org.wso2.carbon.device.application.mgt.core.dao.common.DAOFactory;
import org.wso2.carbon.device.application.mgt.core.internal.DataHolder; import org.wso2.carbon.device.application.mgt.core.internal.DataHolder;
import org.wso2.carbon.user.api.Tenant; import org.wso2.carbon.user.api.Tenant;
@ -34,6 +36,7 @@ import java.util.Map;
public class PlatformManagerImpl implements PlatformManager { public class PlatformManagerImpl implements PlatformManager {
private Map<String, Map<String, Platform>> inMemoryStore; private Map<String, Map<String, Platform>> inMemoryStore;
private static Log log = LogFactory.getLog(PlatformManagerImpl.class);
public PlatformManagerImpl() { public PlatformManagerImpl() {
this.inMemoryStore = new HashMap<>(); this.inMemoryStore = new HashMap<>();
@ -53,20 +56,39 @@ public class PlatformManagerImpl implements PlatformManager {
@Override @Override
public List<Platform> getPlatforms(String tenantDomain) throws PlatformManagementException { public List<Platform> getPlatforms(String tenantDomain) throws PlatformManagementException {
if (log.isDebugEnabled()) {
log.debug("Request for getting platforms received for the tenant domain " + tenantDomain + " at "
+ "PlatformManager level");
}
List<Platform> platforms = DAOFactory.getPlatformDAO().getPlatforms(tenantDomain); List<Platform> platforms = DAOFactory.getPlatformDAO().getPlatforms(tenantDomain);
int platformIndex = 0; int platformIndex = 0;
if (log.isDebugEnabled()) {
log.debug("Number of platforms received from DAO layer is " + platforms.size() + " for the tenant "
+ tenantDomain);
}
for (Platform platform : platforms) { for (Platform platform : platforms) {
if (platform.isFileBased()) { if (platform.isFileBased()) {
Map<String, Platform> superTenantPlatforms = this.inMemoryStore.get(MultitenantConstants.SUPER_TENANT_DOMAIN_NAME); Map<String, Platform> superTenantPlatforms = this.inMemoryStore
.get(MultitenantConstants.SUPER_TENANT_DOMAIN_NAME);
Platform registeredPlatform = superTenantPlatforms.get(platform.getIdentifier()); Platform registeredPlatform = superTenantPlatforms.get(platform.getIdentifier());
if (registeredPlatform != null) { if (registeredPlatform != null) {
platforms.set(platformIndex, new Platform(registeredPlatform)); platforms.set(platformIndex, new Platform(registeredPlatform));
if (log.isDebugEnabled()) {
log.debug("Platform Name - " + platform.getName() + ", IsRegistered - " + true);
}
} else { } else {
platforms.remove(platformIndex); platforms.remove(platformIndex);
if (log.isDebugEnabled()) {
log.debug("Platform Name - " + platform.getName() + ", IsRegistered - " + false);
}
} }
} }
platformIndex++; platformIndex++;
} }
if (log.isDebugEnabled()) {
log.debug("Number of effective platforms for the tenant " + tenantDomain + " : " + platforms.size());
}
return platforms; return platforms;
} }

@ -18,10 +18,23 @@
*/ */
package org.wso2.carbon.device.application.mgt.core.internal; package org.wso2.carbon.device.application.mgt.core.internal;
import org.wso2.carbon.device.application.mgt.common.services.*; import org.wso2.carbon.device.application.mgt.common.services.ApplicationManager;
import org.wso2.carbon.device.application.mgt.common.services.ApplicationReleaseManager;
import org.wso2.carbon.device.application.mgt.common.services.ApplicationUploadManager;
import org.wso2.carbon.device.application.mgt.common.services.CategoryManager;
import org.wso2.carbon.device.application.mgt.common.services.CommentsManager;
import org.wso2.carbon.device.application.mgt.common.services.LifecycleStateManager;
import org.wso2.carbon.device.application.mgt.common.services.PlatformManager;
import org.wso2.carbon.device.application.mgt.common.services.SubscriptionManager;
import org.wso2.carbon.device.application.mgt.common.services.VisibilityManager;
import org.wso2.carbon.device.application.mgt.common.services.VisibilityTypeManager;
import org.wso2.carbon.device.mgt.core.service.DeviceManagementProviderService; import org.wso2.carbon.device.mgt.core.service.DeviceManagementProviderService;
import org.wso2.carbon.ndatasource.core.DataSourceService;
import org.wso2.carbon.user.core.service.RealmService; import org.wso2.carbon.user.core.service.RealmService;
/**
* DataHolder is responsible for holding the references to OSGI Services.
*/
public class DataHolder { public class DataHolder {
private DeviceManagementProviderService deviceManagementService; private DeviceManagementProviderService deviceManagementService;
@ -50,6 +63,8 @@ public class DataHolder {
private static final DataHolder applicationMgtDataHolder = new DataHolder(); private static final DataHolder applicationMgtDataHolder = new DataHolder();
private DataSourceService dataSourceService;
private DataHolder() { private DataHolder() {
} }
@ -153,4 +168,12 @@ public class DataHolder {
public void setRealmService(RealmService realmService) { public void setRealmService(RealmService realmService) {
this.realmService = realmService; this.realmService = realmService;
} }
public void setDataSourceService(DataSourceService dataSourceService) {
this.dataSourceService = dataSourceService;
}
public DataSourceService getDataSourceService() {
return dataSourceService;
}
} }

@ -19,15 +19,26 @@
package org.wso2.carbon.device.application.mgt.core.internal; package org.wso2.carbon.device.application.mgt.core.internal;
import org.apache.commons.logging.Log; import org.apache.commons.logging.Log;
import org.osgi.service.component.ComponentContext;
import org.osgi.framework.BundleContext;
import org.apache.commons.logging.LogFactory; import org.apache.commons.logging.LogFactory;
import org.osgi.framework.BundleContext;
import org.osgi.service.component.ComponentContext;
import org.wso2.carbon.device.application.mgt.common.exception.InvalidConfigurationException; import org.wso2.carbon.device.application.mgt.common.exception.InvalidConfigurationException;
import org.wso2.carbon.device.application.mgt.common.services.*; import org.wso2.carbon.device.application.mgt.common.services.ApplicationManager;
import org.wso2.carbon.device.application.mgt.common.services.ApplicationReleaseManager;
import org.wso2.carbon.device.application.mgt.common.services.ApplicationUploadManager;
import org.wso2.carbon.device.application.mgt.common.services.CategoryManager;
import org.wso2.carbon.device.application.mgt.common.services.CommentsManager;
import org.wso2.carbon.device.application.mgt.common.services.LifecycleStateManager;
import org.wso2.carbon.device.application.mgt.common.services.PlatformManager;
import org.wso2.carbon.device.application.mgt.common.services.SubscriptionManager;
import org.wso2.carbon.device.application.mgt.common.services.VisibilityManager;
import org.wso2.carbon.device.application.mgt.common.services.VisibilityTypeManager;
import org.wso2.carbon.device.application.mgt.core.config.ConfigurationManager; 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.dao.common.DAOFactory;
import org.wso2.carbon.device.application.mgt.core.exception.ApplicationManagementDAOException;
import org.wso2.carbon.device.application.mgt.core.util.ApplicationManagementUtil; import org.wso2.carbon.device.application.mgt.core.util.ApplicationManagementUtil;
import org.wso2.carbon.device.mgt.core.service.DeviceManagementProviderService; import org.wso2.carbon.device.mgt.core.service.DeviceManagementProviderService;
import org.wso2.carbon.ndatasource.core.DataSourceService;
import org.wso2.carbon.user.core.service.RealmService; import org.wso2.carbon.user.core.service.RealmService;
import javax.naming.NamingException; import javax.naming.NamingException;
@ -47,24 +58,31 @@ import javax.naming.NamingException;
* policy="dynamic" * policy="dynamic"
* bind="setRealmService" * bind="setRealmService"
* unbind="unsetRealmService" * unbind="unsetRealmService"
* @scr.reference name="datasource.service"
* interface="org.wso2.carbon.ndatasource.core.DataSourceService"
* cardinality="1..1"
* policy="dynamic"
* bind="setDataSourceService"
* unbind="unsetDataSourceService"
*/ */
public class ServiceComponent { public class ServiceComponent {
private static Log log = LogFactory.getLog(ServiceComponent.class); private static Log log = LogFactory.getLog(ServiceComponent.class);
protected void activate(ComponentContext componentContext) throws NamingException { protected void activate(ComponentContext componentContext) throws NamingException,
ApplicationManagementDAOException {
BundleContext bundleContext = componentContext.getBundleContext(); BundleContext bundleContext = componentContext.getBundleContext();
try { try {
String datasourceName = ConfigurationManager.getInstance() String datasourceName = ConfigurationManager.getInstance().getConfiguration().getDatasourceName();
.getConfiguration().getDatasourceName();
DAOFactory.init(datasourceName); DAOFactory.init(datasourceName);
ApplicationManager applicationManager = ApplicationManagementUtil.getApplicationManagerInstance(); ApplicationManager applicationManager = ApplicationManagementUtil.getApplicationManagerInstance();
DataHolder.getInstance().setApplicationManager(applicationManager); DataHolder.getInstance().setApplicationManager(applicationManager);
bundleContext.registerService(ApplicationManager.class.getName(), applicationManager, null); bundleContext.registerService(ApplicationManager.class.getName(), applicationManager, null);
ApplicationReleaseManager applicationReleaseManager = ApplicationManagementUtil.getApplicationReleaseManagerInstance(); ApplicationReleaseManager applicationReleaseManager = ApplicationManagementUtil
.getApplicationReleaseManagerInstance();
DataHolder.getInstance().setReleaseManager(applicationReleaseManager); DataHolder.getInstance().setReleaseManager(applicationReleaseManager);
bundleContext.registerService(ApplicationReleaseManager.class.getName(), applicationReleaseManager, null); bundleContext.registerService(ApplicationReleaseManager.class.getName(), applicationReleaseManager, null);
@ -100,11 +118,8 @@ public class ServiceComponent {
DataHolder.getInstance().setApplicationUploadManager(uploadManager); DataHolder.getInstance().setApplicationUploadManager(uploadManager);
bundleContext.registerService(ApplicationUploadManager.class.getName(), uploadManager, null); bundleContext.registerService(ApplicationUploadManager.class.getName(), uploadManager, null);
DAOFactory.init(datasourceName);
log.info("ApplicationManagement core bundle has been successfully initialized"); log.info("ApplicationManagement core bundle has been successfully initialized");
if (log.isDebugEnabled()) {
log.debug("ApplicationManagement core bundle has been successfully initialized");
}
} catch (InvalidConfigurationException e) { } catch (InvalidConfigurationException e) {
log.error("Error while activating Application Management core component. ", e); log.error("Error while activating Application Management core component. ", e);
} }
@ -135,4 +150,12 @@ public class ServiceComponent {
protected void unsetRealmService(RealmService realmService) { protected void unsetRealmService(RealmService realmService) {
DataHolder.getInstance().setRealmService(null); DataHolder.getInstance().setRealmService(null);
} }
protected void setDataSourceService(DataSourceService dataSourceService) {
DataHolder.getInstance().setDataSourceService(dataSourceService);
}
protected void unsetDataSourceService(DataSourceService dataSourceService) {
DataHolder.getInstance().setDataSourceService(null);
}
} }

@ -0,0 +1,49 @@
/*
* Copyright (c) 2016, 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.util;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.wso2.carbon.utils.CarbonUtils;
import org.wso2.carbon.utils.dbcreator.DatabaseCreator;
import javax.sql.DataSource;
import java.io.File;
/**
* ApplicationMgtDatabaseCreator is responsible for creating the Application Management related tables.
*/
public class ApplicationMgtDatabaseCreator extends DatabaseCreator {
private static final Log log = LogFactory.getLog(ApplicationMgtDatabaseCreator.class);
private static final String setupSQLScriptBaseLocation =
CarbonUtils.getCarbonHome() + File.separator + "dbscripts" + File.separator + "cdm" + File.separator
+ "application-mgt" + File.separator;
public ApplicationMgtDatabaseCreator(DataSource dataSource) {
super(dataSource);
}
protected String getDbScriptLocation(String databaseType) {
String scriptName = databaseType + ".sql";
if (log.isDebugEnabled()) {
log.debug("Loading application management database script from :" + scriptName);
}
return setupSQLScriptBaseLocation + scriptName;
}
}

@ -1,7 +1,6 @@
{ {
"displayName": "Carbon Application Management App", "displayName": "Carbon Application Management App",
"logLevel": "info", "logLevel": "info",
"initScripts": ["/app/modules/init.js"],
"urlMappings": [ "urlMappings": [
{ {
"url": "/uuf/login", "url": "/uuf/login",

Loading…
Cancel
Save