From c21ddceae1748f00d0ec85aa772b16d8b083269f Mon Sep 17 00:00:00 2001 From: lasantha Date: Wed, 6 Dec 2017 15:15:07 +0530 Subject: [PATCH] adding application and release management implementation --- .../device/application/mgt/api/APIUtil.java | 44 --- .../services/ApplicationManagementAPI.java | 180 ++++++------ .../impl/ApplicationManagementAPIImpl.java | 273 +++++++++--------- .../application/mgt/common/Application.java | 20 ++ .../common/services/ApplicationManager.java | 8 +- .../mgt/core/dao/ApplicationDAO.java | 8 + .../GenericApplicationDAOImpl.java | 38 ++- .../mgt/core/impl/ApplicationManagerImpl.java | 121 +++++--- .../mgt/core/internal/ServiceComponent.java | 16 - .../core/util/ApplicationManagementUtil.java | 15 - 10 files changed, 383 insertions(+), 340 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 04c162165b..f129c0f202 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 @@ -40,12 +40,10 @@ public class APIUtil { private static Log log = LogFactory.getLog(APIUtil.class); private static ApplicationManager applicationManager; - private static PlatformManager platformManager; private static LifecycleStateManager lifecycleStateManager; private static ApplicationReleaseManager applicationReleaseManager; private static ApplicationStorageManager applicationStorageManager; private static SubscriptionManager subscriptionManager; - private static PlatformStorageManager platformStorageManager; private static CategoryManager categoryManager; public static ApplicationManager getApplicationManager() { @@ -67,24 +65,6 @@ public class APIUtil { return applicationManager; } - 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; - } - public static LifecycleStateManager getLifecycleStateManager() { if (lifecycleStateManager == null) { synchronized (APIUtil.class) { @@ -148,30 +128,6 @@ public class APIUtil { return applicationStorageManager; } - /** - * To get the Platform Storage Manager from the osgi context. - * - * @return PlatformStoreManager instance in the current osgi context. - */ - public static PlatformStorageManager getPlatformStorageManager() { - if (platformStorageManager == null) { - synchronized (APIUtil.class) { - if (platformStorageManager == null) { - PrivilegedCarbonContext ctx = PrivilegedCarbonContext.getThreadLocalCarbonContext(); - platformStorageManager = (PlatformStorageManager) ctx - .getOSGiService(PlatformStorageManager.class, null); - if (platformStorageManager == null) { - String msg = "Platform Storage Manager service has not initialized."; - log.error(msg); - throw new IllegalStateException(msg); - } - } - } - } - return platformStorageManager; - } - - /** * To get the Category Manager from the osgi context. * 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 8bb9f107ac..8024b10fc9 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 @@ -126,95 +126,95 @@ public interface ApplicationManagementAPI { String SCOPE = "scope"; -// @GET -// @Produces(MediaType.APPLICATION_JSON) -// @Consumes(MediaType.APPLICATION_JSON) -// @ApiOperation( -// consumes = MediaType.APPLICATION_JSON, -// produces = MediaType.APPLICATION_JSON, -// httpMethod = "GET", -// value = "get all applications", -// notes = "This will get all applications", -// tags = "Application Management", -// extensions = { -// @Extension(properties = { -// @ExtensionProperty(name = SCOPE, value = "perm:application:get") -// }) -// } -// ) -// @ApiResponses( -// value = { -// @ApiResponse( -// code = 200, -// message = "OK. \n Successfully got application list.", -// response = ApplicationList.class), -// @ApiResponse( -// code = 304, -// message = "Not Modified. 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 application list.", -// response = ErrorResponse.class) -// }) -// Response getApplications( -// @ApiParam( -// name = "offset", -// value = "Provide from which position apps should return", defaultValue = "20") -// @QueryParam("offset") int offset, -// @ApiParam( -// name = "limit", -// value = "Provide how many apps it should return", defaultValue = "0") -// @QueryParam("limit") int limit, -// @ApiParam( -// name = "searchQuery", -// value = "Relevant search query to search on", defaultValue = "*") -// @QueryParam("searchQuery") String searchQuery -// ); -// -// @GET -// @Path("/{uuid}") -// @Produces(MediaType.APPLICATION_JSON) -// @Consumes(MediaType.APPLICATION_JSON) -// @ApiOperation( -// consumes = MediaType.APPLICATION_JSON, -// produces = MediaType.APPLICATION_JSON, -// httpMethod = "GET", -// value = "get the application specified by the UUID", -// notes = "This will get the application identified by the UUID, if exists", -// tags = "Application Management", -// extensions = { -// @Extension(properties = { -// @ExtensionProperty(name = SCOPE, value = "perm:application:get") -// }) -// } -// ) -// @ApiResponses( -// value = { -// @ApiResponse( -// code = 200, -// message = "OK. \n Successfully retrieved relevant application.", -// response = Application.class), -// @ApiResponse( -// code = 404, -// message = "Application not found"), -// @ApiResponse( -// code = 500, -// message = "Internal Server Error. \n Error occurred while getting relevant application.", -// response = ErrorResponse.class) -// }) -// Response getApplication( -// @ApiParam( -// name = "uuid", -// value = "UUID of the application", -// required = true) -// @PathParam("uuid") String uuid, -// @ApiParam( -// name = "isWithImages", -// value = "Whether to return application with images", -// required = false) -// @QueryParam("isWithImages") Boolean isWithImages -// ); + @GET + @Produces(MediaType.APPLICATION_JSON) + @Consumes(MediaType.APPLICATION_JSON) + @ApiOperation( + consumes = MediaType.APPLICATION_JSON, + produces = MediaType.APPLICATION_JSON, + httpMethod = "GET", + value = "get all applications", + notes = "This will get all applications", + tags = "Application Management", + extensions = { + @Extension(properties = { + @ExtensionProperty(name = SCOPE, value = "perm:application:get") + }) + } + ) + @ApiResponses( + value = { + @ApiResponse( + code = 200, + message = "OK. \n Successfully got application list.", + response = ApplicationList.class), + @ApiResponse( + code = 304, + message = "Not Modified. 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 application list.", + response = ErrorResponse.class) + }) + Response getApplications( + @ApiParam( + name = "offset", + value = "Provide from which position apps should return", defaultValue = "20") + @QueryParam("offset") int offset, + @ApiParam( + name = "limit", + value = "Provide how many apps it should return", defaultValue = "0") + @QueryParam("limit") int limit, + @ApiParam( + name = "searchQuery", + value = "Relevant search query to search on", defaultValue = "*") + @QueryParam("searchQuery") String searchQuery + ); + + @GET + @Path("/{appType}") + @Produces(MediaType.APPLICATION_JSON) + @Consumes(MediaType.APPLICATION_JSON) + @ApiOperation( + consumes = MediaType.APPLICATION_JSON, + produces = MediaType.APPLICATION_JSON, + httpMethod = "GET", + value = "get the application of requesting application type", + notes = "This will get the application identified by the application type and name, if exists", + tags = "Application Management", + extensions = { + @Extension(properties = { + @ExtensionProperty(name = SCOPE, value = "perm:application:get") + }) + } + ) + @ApiResponses( + value = { + @ApiResponse( + code = 200, + message = "OK. \n Successfully retrieved relevant application.", + response = Application.class), + @ApiResponse( + code = 404, + message = "Application not found"), + @ApiResponse( + code = 500, + message = "Internal Server Error. \n Error occurred while getting relevant application.", + response = ErrorResponse.class) + }) + Response getApplication( + @ApiParam( + name = "appType", + value = "Type of the application", + required = true) + @PathParam("appType") String appType, + @ApiParam( + name = "appName", + value = "Application name", + required = true) + @QueryParam("isWithImages") String appName + ); @PUT @Produces(MediaType.APPLICATION_JSON) @@ -321,7 +321,7 @@ public interface ApplicationManagementAPI { Response createApplicationRelease( @Multipart(value = "applicationRelease", type = "application/json") ApplicationRelease applicationRelease, @Multipart(value = "binaryFile") Attachment binaryFile, - @PathParam("appId") int applicationId)); + @PathParam("appId") int applicationId); @POST 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 dd3515faff..9e046c27c1 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 @@ -69,85 +69,114 @@ public class ApplicationManagementAPIImpl implements ApplicationManagementAPI { private static final int DEFAULT_LIMIT = 20; private static Log log = LogFactory.getLog(ApplicationManagementAPIImpl.class); -// @GET -// @Consumes("application/json") -// public Response getApplications(@QueryParam("offset") int offset, @QueryParam("limit") int limit, -// @QueryParam("query") String searchQuery) { -// ApplicationManager applicationManager = APIUtil.getApplicationManager(); -// ApplicationStorageManager applicationStorageManager = APIUtil.getApplicationStorageManager(); -// -// try { -// if (limit == 0) { -// limit = DEFAULT_LIMIT; -// } -// Filter filter = new Filter(); -// filter.setOffset(offset); -// filter.setLimit(limit); -// filter.setSearchQuery(searchQuery); -// -// ApplicationList applications = applicationManager.getApplications(filter); -// -// for (Application application : applications.getApplications()) { -// ImageArtifact imageArtifact = applicationStorageManager.getImageArtifact(application.getUuid(), -// Constants.IMAGE_ARTIFACTS[0], 0); -// application.setIcon(imageArtifact); -// } -// return Response.status(Response.Status.OK).entity(applications).build(); -// } catch (NotFoundException e) { -// return Response.status(Response.Status.NOT_FOUND).build(); -// } catch (ApplicationManagementException e) { -// String msg = "Error occurred while getting the application list"; -// log.error(msg, e); -// return Response.status(Response.Status.BAD_REQUEST).build(); -// } catch (ApplicationStorageManagementException e) { -// log.error("Error occurred while getting the image artifacts of the application", e); -// return APIUtil.getResponse(e, Response.Status.INTERNAL_SERVER_ERROR); -// } -// } -// -// @GET -// @Consumes("application/json") -// @Path("/{uuid}") -// public Response getApplication(@PathParam("uuid") String uuid, @QueryParam("isWithImages") Boolean isWithImages) { -// ApplicationManager applicationManager = APIUtil.getApplicationManager(); -// ApplicationStorageManager applicationStorageManager = APIUtil.getApplicationStorageManager(); -// try { -// Application application = applicationManager.getApplication(uuid); -// if (application == null) { -// return Response.status(Response.Status.NOT_FOUND) -// .entity("Application with UUID " + uuid + " not found").build(); -// } -// -// if (isWithImages != null && isWithImages) { -// ImageArtifact icon = applicationStorageManager.getImageArtifact(uuid, Constants.IMAGE_ARTIFACTS[0], 0); -// ImageArtifact banner = applicationStorageManager.getImageArtifact(uuid, Constants.IMAGE_ARTIFACTS[1], -// 0); -// int screenShotCount = application.getScreenShotCount(); -// for (int count = 1; count < screenShotCount; count++) { -// ImageArtifact screenShot = applicationStorageManager.getImageArtifact(uuid, Constants -// .IMAGE_ARTIFACTS[2], count); -// application.addScreenShot(screenShot); -// } -// application.setIcon(icon); -// application.setBanner(banner); -// } -// return Response.status(Response.Status.OK).entity(application).build(); -// } catch (NotFoundException e) { -// return Response.status(Response.Status.NOT_FOUND).build(); -// } catch (ApplicationManagementException e) { -// log.error("Error occurred while getting application with the uuid " + uuid, e); -// return APIUtil.getResponse(e, Response.Status.INTERNAL_SERVER_ERROR); -// } catch (ApplicationStorageManagementException e) { -// log.error("Error occurred while getting the image artifacts of the application with the uuid " + uuid, e); -// return APIUtil.getResponse(e, Response.Status.INTERNAL_SERVER_ERROR); -// } -// } + @GET + @Consumes("application/json") + public Response getApplications(@QueryParam("offset") int offset, @QueryParam("limit") int limit, + @QueryParam("query") String searchQuery) { + ApplicationManager applicationManager = APIUtil.getApplicationManager(); + ApplicationStorageManager applicationStorageManager = APIUtil.getApplicationStorageManager(); + + try { + if (limit == 0) { + limit = DEFAULT_LIMIT; + } + Filter filter = new Filter(); + filter.setOffset(offset); + filter.setLimit(limit); + filter.setSearchQuery(searchQuery); + + ApplicationList applications = applicationManager.getApplications(filter); + + for (Application application : applications.getApplications()) { +// ToDo : use better approach to solve this + String uuId = applicationManager.getUuidOfLatestRelease(application.getId()); + if (uuId != null){ + application.setUuidOfLatestRelease(uuId); + ImageArtifact imageArtifact = applicationStorageManager.getImageArtifact(uuId, Constants.IMAGE_ARTIFACTS[0], 0); + application.setIconOfLatestRelease(imageArtifact); + }else{ +// ToDo set default icon + } + } + return Response.status(Response.Status.OK).entity(applications).build(); + } catch (NotFoundException e) { + return Response.status(Response.Status.NOT_FOUND).build(); + } catch (ApplicationManagementException e) { + String msg = "Error occurred while getting the application list"; + log.error(msg, e); + return Response.status(Response.Status.BAD_REQUEST).build(); + } catch (ApplicationStorageManagementException e) { + log.error("Error occurred while getting the image artifacts of the application", e); + return APIUtil.getResponse(e, Response.Status.INTERNAL_SERVER_ERROR); + } + } + + @GET + @Consumes("application/json") + @Path("/{appType}") + public Response getApplication(@PathParam("appType") String appType, @QueryParam("appName") String appName) { + ApplicationManager applicationManager = APIUtil.getApplicationManager(); + ApplicationStorageManager applicationStorageManager = APIUtil.getApplicationStorageManager(); + try { + Application application = applicationManager.getApplication(appType, appName); + if (application == null) { + return Response.status(Response.Status.NOT_FOUND) + .entity("Application with UUID " + appType + " not found").build(); + } + + // ToDo : use better approach to solve this + String uuId = applicationManager.getUuidOfLatestRelease(application.getId()); + if (uuId != null){ + application.setUuidOfLatestRelease(uuId); + ImageArtifact imageArtifact = applicationStorageManager.getImageArtifact(uuId, Constants.IMAGE_ARTIFACTS[0], 0); + application.setIconOfLatestRelease(imageArtifact); + }else{ + // ToDo set default icon + } + + return Response.status(Response.Status.OK).entity(application).build(); + } catch (NotFoundException e) { + return Response.status(Response.Status.NOT_FOUND).build(); + } catch (ApplicationManagementException e) { + log.error("Error occurred while getting application with the uuid " + appType, e); + return APIUtil.getResponse(e, Response.Status.INTERNAL_SERVER_ERROR); + } catch (ApplicationStorageManagementException e) { + log.error("Error occurred while getting the image artifacts of the application with the uuid " + appType, e); + return APIUtil.getResponse(e, Response.Status.INTERNAL_SERVER_ERROR); + } + } + + @POST + @Consumes("application/json") + public Response createApplication(@Valid Application application) { + ApplicationManager applicationManager = APIUtil.getApplicationManager(); + try { + Application createdApplication = applicationManager.createApplication(application); + + if (application != null){ + return Response.status(Response.Status.CREATED).entity(createdApplication).build(); + }else{ + String msg = "Given device type is not matched with existing device types"; + log.error(msg); + return Response.status(Response.Status.BAD_REQUEST).build(); + } + }catch (DeviceManagementDAOException e) { + String msg = "Error occurred while getting the device type"; + log.error(msg, e); + return Response.status(Response.Status.BAD_REQUEST).build(); + }catch (ApplicationManagementException e) { + String msg = "Error occurred while creating the application"; + log.error(msg, e); + return Response.status(Response.Status.BAD_REQUEST).build(); + } + } + +// ToDo @PUT @Consumes("application/json") @Path("/{uuid}/lifecycle") - public Response changeLifecycleState(@PathParam("uuid") String applicationUUID, - @QueryParam("state") String state) { + public Response changeLifecycleState(@PathParam("uuid") String applicationUUID, @QueryParam("state") String state) { ApplicationManager applicationManager = APIUtil.getApplicationManager(); if (!Arrays.asList(Constants.LIFE_CYCLES).contains(state)) { @@ -199,33 +228,6 @@ public class ApplicationManagementAPIImpl implements ApplicationManagementAPI { } } - @POST - @Consumes("application/json") - public Response createApplication(@Valid Application application) { - ApplicationManager applicationManager = APIUtil.getApplicationManager(); - try { - Application createdApplication = applicationManager.createApplication(application); - - if (application != null){ - return Response.status(Response.Status.CREATED).entity(createdApplication).build(); - }else{ - String msg = "Given device type is not matched with existing device types"; - log.error(msg); - return Response.status(Response.Status.BAD_REQUEST).build(); - } - }catch (DeviceManagementDAOException e) { - String msg = "Error occurred while getting the device type"; - log.error(msg, e); - return Response.status(Response.Status.BAD_REQUEST).build(); - }catch (ApplicationManagementException e) { - String msg = "Error occurred while creating the application"; - log.error(msg, e); - return Response.status(Response.Status.BAD_REQUEST).build(); - } - } - - //working on this// - @Override @POST @Path("{appId}/release") @@ -258,7 +260,6 @@ public class ApplicationManagementAPIImpl implements ApplicationManagementAPI { return APIUtil.getResponse(e, Response.Status.INTERNAL_SERVER_ERROR); } } - //working on this// @Override @POST @@ -393,39 +394,39 @@ public class ApplicationManagementAPIImpl implements ApplicationManagementAPI { } } -// @Override -// @POST -// @Path("/release/{uuid}") -// public Response createApplicationRelease(@PathParam("uuid") String applicationUUID, -// @Multipart("applicationRelease") ApplicationRelease applicationRelease, -// @Multipart("binaryFile") Attachment binaryFile) { -// ApplicationReleaseManager applicationReleaseManager = APIUtil.getApplicationReleaseManager(); -// ApplicationStorageManager applicationStorageManager = APIUtil.getApplicationStorageManager(); -// try { -// applicationRelease = applicationReleaseManager.createRelease(applicationUUID, applicationRelease); -// -// if (binaryFile != null) { -// applicationStorageManager.uploadReleaseArtifacts(applicationUUID, applicationRelease.getVersion(), -// binaryFile.getDataHandler().getInputStream()); -// } -// return Response.status(Response.Status.CREATED).entity(applicationRelease).build(); -// } catch (ApplicationManagementException e) { -// log.error("Error while creating an application release for the application with UUID " + applicationUUID, -// e); -// return APIUtil.getResponse(e, Response.Status.INTERNAL_SERVER_ERROR); -// } catch (IOException e) { -// String errorMessage = -// "Error while uploading binary file for the application release of the application with UUID " -// + applicationUUID; -// log.error(errorMessage, e); -// return APIUtil.getResponse(new ApplicationManagementException(errorMessage, e), -// Response.Status.INTERNAL_SERVER_ERROR); -// } catch (ResourceManagementException e) { -// log.error("Error occurred while uploading the releases artifacts of the application with the uuid " -// + applicationUUID + " for the release " + applicationRelease.getVersion(), e); -// return APIUtil.getResponse(e, Response.Status.INTERNAL_SERVER_ERROR); -// } -// } + @Override + @POST + @Path("/release/{uuid}") + public Response createApplicationRelease(@PathParam("uuid") String applicationUUID, + @Multipart("applicationRelease") ApplicationRelease applicationRelease, + @Multipart("binaryFile") Attachment binaryFile) { + ApplicationReleaseManager applicationReleaseManager = APIUtil.getApplicationReleaseManager(); + ApplicationStorageManager applicationStorageManager = APIUtil.getApplicationStorageManager(); + try { + applicationRelease = applicationReleaseManager.createRelease(applicationUUID, applicationRelease); + + if (binaryFile != null) { + applicationStorageManager.uploadReleaseArtifacts(applicationUUID, applicationRelease.getVersion(), + binaryFile.getDataHandler().getInputStream()); + } + return Response.status(Response.Status.CREATED).entity(applicationRelease).build(); + } catch (ApplicationManagementException e) { + log.error("Error while creating an application release for the application with UUID " + applicationUUID, + e); + return APIUtil.getResponse(e, Response.Status.INTERNAL_SERVER_ERROR); + } catch (IOException e) { + String errorMessage = + "Error while uploading binary file for the application release of the application with UUID " + + applicationUUID; + log.error(errorMessage, e); + return APIUtil.getResponse(new ApplicationManagementException(errorMessage, e), + Response.Status.INTERNAL_SERVER_ERROR); + } catch (ResourceManagementException e) { + log.error("Error occurred while uploading the releases artifacts of the application with the uuid " + + applicationUUID + " for the release " + applicationRelease.getVersion(), e); + return APIUtil.getResponse(e, Response.Status.INTERNAL_SERVER_ERROR); + } + } @Override @PUT diff --git a/components/application-mgt/org.wso2.carbon.device.application.mgt.common/src/main/java/org/wso2/carbon/device/application/mgt/common/Application.java b/components/application-mgt/org.wso2.carbon.device.application.mgt.common/src/main/java/org/wso2/carbon/device/application/mgt/common/Application.java index ce8444674b..1184091e18 100644 --- a/components/application-mgt/org.wso2.carbon.device.application.mgt.common/src/main/java/org/wso2/carbon/device/application/mgt/common/Application.java +++ b/components/application-mgt/org.wso2.carbon.device.application.mgt.common/src/main/java/org/wso2/carbon/device/application/mgt/common/Application.java @@ -91,6 +91,10 @@ public class Application { this.user = user; } + public String uuidOfLatestRelease; + + public ImageArtifact iconOfLatestRelease; + public List getUnrestrictedRoles() { return unrestrictedRoles; } @@ -146,4 +150,20 @@ public class Application { public void setDevicetype(DeviceType devicetype) { this.devicetype = devicetype; } + + public String getUuidOfLatestRelease() { + return uuidOfLatestRelease; + } + + public void setUuidOfLatestRelease(String uuidOfLatestRelease) { + this.uuidOfLatestRelease = uuidOfLatestRelease; + } + + public ImageArtifact getIconOfLatestRelease() { + return iconOfLatestRelease; + } + + public void setIconOfLatestRelease(ImageArtifact iconOfLatestRelease) { + this.iconOfLatestRelease = iconOfLatestRelease; + } } diff --git a/components/application-mgt/org.wso2.carbon.device.application.mgt.common/src/main/java/org/wso2/carbon/device/application/mgt/common/services/ApplicationManager.java b/components/application-mgt/org.wso2.carbon.device.application.mgt.common/src/main/java/org/wso2/carbon/device/application/mgt/common/services/ApplicationManager.java index 21841f0159..573593c0af 100644 --- a/components/application-mgt/org.wso2.carbon.device.application.mgt.common/src/main/java/org/wso2/carbon/device/application/mgt/common/services/ApplicationManager.java +++ b/components/application-mgt/org.wso2.carbon.device.application.mgt.common/src/main/java/org/wso2/carbon/device/application/mgt/common/services/ApplicationManager.java @@ -64,6 +64,9 @@ public interface ApplicationManager { */ ApplicationList getApplications(Filter filter) throws ApplicationManagementException; + String getUuidOfLatestRelease(int appId) throws ApplicationManagementException; + + /** * To change the lifecycle of the Application. * @@ -87,9 +90,10 @@ public interface ApplicationManager { /** * To get Application with the given UUID. * - * @param uuid UUID of the Application + * @param appType type of the Application + * @param appName name of the Application * @return the Application identified by the UUID * @throws ApplicationManagementException Application Management Exception. */ - Application getApplication(String uuid) throws ApplicationManagementException; + Application getApplication(String appType, String appName) throws ApplicationManagementException; } diff --git a/components/application-mgt/org.wso2.carbon.device.application.mgt.core/src/main/java/org/wso2/carbon/device/application/mgt/core/dao/ApplicationDAO.java b/components/application-mgt/org.wso2.carbon.device.application.mgt.core/src/main/java/org/wso2/carbon/device/application/mgt/core/dao/ApplicationDAO.java index 207f92199f..530a48cc05 100644 --- a/components/application-mgt/org.wso2.carbon.device.application.mgt.core/src/main/java/org/wso2/carbon/device/application/mgt/core/dao/ApplicationDAO.java +++ b/components/application-mgt/org.wso2.carbon.device.application.mgt.core/src/main/java/org/wso2/carbon/device/application/mgt/core/dao/ApplicationDAO.java @@ -65,6 +65,14 @@ public interface ApplicationDAO { */ ApplicationList getApplications(Filter filter, int tenantId) throws ApplicationManagementDAOException; + /** + * To get the UUID of latest app release that satisfy the given criteria. + * + * @param appId application id + * @throws ApplicationManagementDAOException Application Management DAO Exception. + */ + String getUuidOfLatestRelease(int appId) throws ApplicationManagementDAOException; + /** * To get the application with the given uuid * 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/application/GenericApplicationDAOImpl.java b/components/application-mgt/org.wso2.carbon.device.application.mgt.core/src/main/java/org/wso2/carbon/device/application/mgt/core/dao/impl/application/GenericApplicationDAOImpl.java index 38b74cc41a..ed11a1feef 100644 --- a/components/application-mgt/org.wso2.carbon.device.application.mgt.core/src/main/java/org/wso2/carbon/device/application/mgt/core/dao/impl/application/GenericApplicationDAOImpl.java +++ b/components/application-mgt/org.wso2.carbon.device.application.mgt.core/src/main/java/org/wso2/carbon/device/application/mgt/core/dao/impl/application/GenericApplicationDAOImpl.java @@ -179,7 +179,7 @@ public class GenericApplicationDAOImpl extends AbstractDAOImpl implements Applic } } - sql += " LIMIT ? OFFSET ?"; + sql += " LIMIT ? OFFSET ? ORDER BY DESC APP_ID"; pagination.setLimit(filter.getLimit()); pagination.setOffset(filter.getOffset()); @@ -220,6 +220,42 @@ public class GenericApplicationDAOImpl extends AbstractDAOImpl implements Applic return applicationList; } + @Override + public String getUuidOfLatestRelease(int appId) throws ApplicationManagementDAOException { + if (log.isDebugEnabled()) { + log.debug("Getting UUID from the latest app release"); + } + + Connection conn; + PreparedStatement stmt = null; + ResultSet rs = null; + String sql = ""; + int index = 0; + String uuId = null; + try { + conn = this.getDBConnection(); + sql += "SELECT APP_RELEASE.UUID AS UUID FROM AP_APP_RELEASE AS APP_RELEASE, AP_APP_LIFECYCLE_STATE " + + "AS LIFECYCLE WHERE APP_RELEASE.AP_APP_ID=? AND APP_RELEASE.ID = LIFECYCLE.AP_APP_RELEASE_ID " + + "AND LIFECYCLE.CURRENT_STATE = ? order by APP_RELEASE.ID DESC;"; + + stmt = conn.prepareStatement(sql); + stmt.setInt(++index, appId); + stmt.setString(++index, "PUBLISHED"); + rs = stmt.executeQuery(); + if (rs.next()) { + uuId = rs.getString("UUID"); + } + return uuId; + } catch (SQLException e) { + throw new ApplicationManagementDAOException("Error occurred while getting uuid of latest app release", e); + } catch (DBConnectionException e) { + throw new ApplicationManagementDAOException("Error occurred while obtaining the DB connection for " + + "getting app release id", e); + } finally { + Util.cleanupResources(stmt, rs); + } + } + @Override public int getApplicationCount(Filter filter) throws ApplicationManagementDAOException { if (log.isDebugEnabled()) { 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 e81f0a1b7d..b712954cea 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 @@ -21,6 +21,7 @@ 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.CarbonConstants; +import org.wso2.carbon.context.CarbonContext; import org.wso2.carbon.context.PrivilegedCarbonContext; import org.wso2.carbon.device.application.mgt.common.*; import org.wso2.carbon.device.application.mgt.common.exception.ApplicationManagementException; @@ -216,30 +217,72 @@ public class ApplicationManagerImpl implements ApplicationManager { public ApplicationList getApplications(Filter filter) throws ApplicationManagementException { int tenantId = PrivilegedCarbonContext.getThreadLocalCarbonContext().getTenantId(true); String userName = PrivilegedCarbonContext.getThreadLocalCarbonContext().getUsername(); + ApplicationList roleRestrictedApplicationList = new ApplicationList(); + ApplicationList applicationList ; try { - if (isAuthorized(userName, tenantId, CarbonConstants.UI_ADMIN_PERMISSION_COLLECTION)) { - userName = "ALL"; + filter.setUserName(userName); + ConnectionManagerUtil.openDBConnection(); + applicationList = applicationDAO.getApplications(filter, tenantId); + if (isAdminUser(userName, tenantId, CarbonConstants.UI_ADMIN_PERMISSION_COLLECTION)) { + return applicationList; } - } catch (UserStoreException e) { + for (Application application : applicationList.getApplications()) { + if (application.getUnrestrictedRoles().isEmpty()){ + roleRestrictedApplicationList.getApplications().add(application); + }else{ + if (isRoleExists(application.getUnrestrictedRoles(), userName){ + roleRestrictedApplicationList.getApplications().add(application); + } + } + } + return roleRestrictedApplicationList; + }catch (UserStoreException e) { throw new ApplicationManagementException("User-store exception while checking whether the user " + userName + " of tenant " + tenantId + " has the publisher permission"); } - filter.setUserName(userName); + finally { + ConnectionManagerUtil.closeDBConnection(); + } + } + @Override + public String getUuidOfLatestRelease(int appId) throws ApplicationManagementException { try { ConnectionManagerUtil.openDBConnection(); - ApplicationDAO applicationDAO = ApplicationManagementDAOFactory.getApplicationDAO(); - ApplicationList applicationList = applicationDAO.getApplications(filter, tenantId); - for (Application application : applicationList.getApplications()) { - application.setVisibility(DataHolder.getInstance().getVisibilityManager().get(application.getId())); - } - return applicationList; - } finally { + return applicationDAO.getUuidOfLatestRelease(appId); + }finally { ConnectionManagerUtil.closeDBConnection(); } + } + private boolean isRoleExists(List unrestrictedRoleList, String userName) + throws UserStoreException { + String[] roleList; + roleList = getRoleOfUser(userName); + for (UnrestrictedRole unrestrictedRole : unrestrictedRoleList) { + for (String role : roleList) { + if (unrestrictedRole.getRole().equals(role)) { + return true; + } + } + } + return false; + } + + private String[] getRoleOfUser(String userName) throws UserStoreException { + UserRealm userRealm = CarbonContext.getThreadLocalCarbonContext().getUserRealm(); + String[] roleList = {}; + if (userRealm != null) { + roleList = userRealm.getUserStoreManager().getRoleListOfUser(userName); + } else { + log.error("role list is empty of user :"+ userName); + } + return roleList; + } + + @Override public void changeLifecycle(String applicationUuid, String lifecycleIdentifier) throws ApplicationManagementException { @@ -305,7 +348,7 @@ public class ApplicationManagerImpl implements ApplicationManager { // log.debug("In order to make the state change to " + transition.getNextState() + " permission " // + permission + " is required"); // } -// if (isAuthorized(userName, tenantId, permission)) { +// if (isAdminUser(userName, tenantId, permission)) { // filteredTransitions.add(transition); // } else { // if (log.isDebugEnabled()) { @@ -333,28 +376,34 @@ public class ApplicationManagerImpl implements ApplicationManager { } @Override - public Application getApplication(String uuid) throws ApplicationManagementException { -// int tenantId = PrivilegedCarbonContext.getThreadLocalCarbonContext().getTenantId(true); -// String userName = PrivilegedCarbonContext.getThreadLocalCarbonContext().getUsername(); -// -// try { -// if (isAuthorized(userName, tenantId, CarbonConstants.UI_ADMIN_PERMISSION_COLLECTION)) { -// userName = "ALL"; -// } -// } catch (UserStoreException e) { -// throw new ApplicationManagementException( -// "User-store exception while getting application with the UUID " + uuid); -// } -// try { -// ConnectionManagerUtil.openDBConnection(); -// Application application = ApplicationManagementDAOFactory.getApplicationDAO().getApplication(uuid, tenantId, userName); -// if (application != null) { -// application.setVisibility(DataHolder.getInstance().getVisibilityManager().get(application.getId())); -// } -// return application; -// } finally { -// ConnectionManagerUtil.closeDBConnection(); -// } + public Application getApplication(String appType, String appName) throws ApplicationManagementException { + int tenantId = PrivilegedCarbonContext.getThreadLocalCarbonContext().getTenantId(true); + String userName = PrivilegedCarbonContext.getThreadLocalCarbonContext().getUsername(); + Application application; + boolean isAppAllowed = false; + try { + ConnectionManagerUtil.openDBConnection(); + application = ApplicationManagementDAOFactory.getApplicationDAO().getApplication(appType, appName, tenantId); + if (isAdminUser(userName, tenantId, CarbonConstants.UI_ADMIN_PERMISSION_COLLECTION)) { + return application; + } + + if (application.getUnrestrictedRoles().isEmpty()){ + isAppAllowed = true; + }else if(isRoleExists(application.getUnrestrictedRoles(), userName)){ + isAppAllowed = true ; + } + + if (!isAppAllowed){ + return null; + } + return application; + } catch (UserStoreException e) { + throw new ApplicationManagementException("User-store exception while getting application with the " + + "application name " + appName); + } finally { + ConnectionManagerUtil.closeDBConnection(); + } } /** @@ -367,7 +416,7 @@ public class ApplicationManagerImpl implements ApplicationManager { private boolean isApplicationOwnerOrAdmin(String applicationUUID, String userName, int tenantId) throws ApplicationManagementException { // try { -// if (isAuthorized(userName, tenantId, CarbonConstants.UI_ADMIN_PERMISSION_COLLECTION)) { +// if (isAdminUser(userName, tenantId, CarbonConstants.UI_ADMIN_PERMISSION_COLLECTION)) { // return true; // } // } catch (UserStoreException e) { @@ -393,7 +442,7 @@ public class ApplicationManagerImpl implements ApplicationManager { * @return true if the current user has the permission, otherwise false. * @throws UserStoreException UserStoreException */ - private boolean isAuthorized(String username, int tenantId, String permission) throws UserStoreException { + private boolean isAdminUser(String username, int tenantId, String permission) throws UserStoreException { UserRealm userRealm = DataHolder.getInstance().getRealmService().getTenantUserRealm(tenantId); return userRealm != null && userRealm.getAuthorizationManager() != null && userRealm.getAuthorizationManager() .isUserAuthorized(MultitenantUtils.getTenantAwareUsername(username), 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 81681d2625..2468175ea9 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 @@ -73,7 +73,6 @@ public class ServiceComponent { BundleContext bundleContext = componentContext.getBundleContext(); try { String datasourceName = ConfigurationManager.getInstance().getConfiguration().getDatasourceName(); -// ApplicationManagementDAOFactory.init(datasourceName); ApplicationManager applicationManager = ApplicationManagementUtil.getApplicationManagerInstance(); DataHolder.getInstance().setApplicationManager(applicationManager); @@ -96,11 +95,6 @@ public class ServiceComponent { DataHolder.getInstance().setLifecycleStateManager(lifecycleStateManager); bundleContext.registerService(LifecycleStateManager.class.getName(), lifecycleStateManager, null); - //remove this - PlatformManager platformManager = ApplicationManagementUtil.getPlatformManagerInstance(); - DataHolder.getInstance().setPlatformManager(platformManager); - bundleContext.registerService(PlatformManager.class.getName(), platformManager, null); - SubscriptionManager subscriptionManager = ApplicationManagementUtil.getSubscriptionManagerInstance(); DataHolder.getInstance().setSubscriptionManager(subscriptionManager); bundleContext.registerService(SubscriptionManager.class.getName(), subscriptionManager, null); @@ -114,16 +108,6 @@ public class ServiceComponent { DataHolder.getInstance().setApplicationStorageManager(applicationStorageManager); bundleContext.registerService(ApplicationStorageManager.class.getName(), applicationStorageManager, null); - //can remove - PlatformStorageManager platformStorageManager = ApplicationManagementUtil - .getPlatformStorageManagerInstance(); - DataHolder.getInstance().setPlatformStorageManager(platformStorageManager); - bundleContext.registerService(PlatformStorageManager.class.getName(), platformStorageManager, null); - - //can remove - bundleContext.registerService(Axis2ConfigurationContextObserver.class.getName(), - new PlatformManagementAxis2ConfigurationObserverImpl(), null); - ApplicationManagementDAOFactory.init(datasourceName); ApplicationManagementDAOFactory.initDatabases(); log.info("ApplicationManagement core bundle has been successfully initialized"); diff --git a/components/application-mgt/org.wso2.carbon.device.application.mgt.core/src/main/java/org/wso2/carbon/device/application/mgt/core/util/ApplicationManagementUtil.java b/components/application-mgt/org.wso2.carbon.device.application.mgt.core/src/main/java/org/wso2/carbon/device/application/mgt/core/util/ApplicationManagementUtil.java index bdd44d9477..51d9f29356 100644 --- a/components/application-mgt/org.wso2.carbon.device.application.mgt.core/src/main/java/org/wso2/carbon/device/application/mgt/core/util/ApplicationManagementUtil.java +++ b/components/application-mgt/org.wso2.carbon.device.application.mgt.core/src/main/java/org/wso2/carbon/device/application/mgt/core/util/ApplicationManagementUtil.java @@ -73,12 +73,6 @@ public class ApplicationManagementUtil { return getInstance(extension, LifecycleStateManager.class); } - public static PlatformManager getPlatformManagerInstance() throws InvalidConfigurationException { - ConfigurationManager configurationManager = ConfigurationManager.getInstance(); - Extension extension = configurationManager.getExtension(Extension.Name.PlatformManager); - return getInstance(extension, PlatformManager.class); - } - public static VisibilityManager getVisibilityManagerInstance() throws InvalidConfigurationException { ConfigurationManager configurationManager = ConfigurationManager.getInstance(); Extension extension = configurationManager.getExtension(Extension.Name.VisibilityManager); @@ -98,15 +92,6 @@ public class ApplicationManagementUtil { return getInstance(extension, ApplicationStorageManager.class); } - //can remove - public static PlatformStorageManager getPlatformStorageManagerInstance() throws - InvalidConfigurationException { - ConfigurationManager configurationManager = ConfigurationManager.getInstance(); - Extension extension = configurationManager.getExtension(Extension.Name.PlatformStorageManager); - return getInstance(extension, PlatformStorageManager.class); - } - - private static T getInstance(Extension extension, Class cls) throws InvalidConfigurationException { try { Class theClass = Class.forName(extension.getClassName());