From 4c921d54b10f1ce54f45bf534075e91b71094454 Mon Sep 17 00:00:00 2001 From: lasantha Date: Mon, 25 Oct 2021 14:55:33 +0530 Subject: [PATCH] Update APPM subscriptions when operation log updates --- .../impl/DeviceManagementServiceImpl.java | 15 +++++++++++++-- .../mgt/jaxrs/util/DeviceMgtAPIUtils.java | 19 +++++++++++++++++++ 2 files changed, 32 insertions(+), 2 deletions(-) diff --git a/components/device-mgt/org.wso2.carbon.device.mgt.api/src/main/java/org/wso2/carbon/device/mgt/jaxrs/service/impl/DeviceManagementServiceImpl.java b/components/device-mgt/org.wso2.carbon.device.mgt.api/src/main/java/org/wso2/carbon/device/mgt/jaxrs/service/impl/DeviceManagementServiceImpl.java index 5d039f27a6..df8a4b2be2 100644 --- a/components/device-mgt/org.wso2.carbon.device.mgt.api/src/main/java/org/wso2/carbon/device/mgt/jaxrs/service/impl/DeviceManagementServiceImpl.java +++ b/components/device-mgt/org.wso2.carbon.device.mgt.api/src/main/java/org/wso2/carbon/device/mgt/jaxrs/service/impl/DeviceManagementServiceImpl.java @@ -37,6 +37,7 @@ package org.wso2.carbon.device.mgt.jaxrs.service.impl; import com.google.gson.Gson; +import io.entgra.application.mgt.common.services.ApplicationManager; import org.apache.commons.httpclient.HttpStatus; import org.apache.commons.lang.StringUtils; import org.apache.commons.logging.Log; @@ -1417,9 +1418,15 @@ public class DeviceManagementServiceImpl implements DeviceManagementService { Operation operation = DeviceMgtAPIUtils.validateOperationStatusBean(operationStatusBean); operation.setId(operationStatusBean.getOperationId()); DeviceMgtAPIUtils.getDeviceManagementService().updateOperation(device, operation); + + if (MDMAppConstants.AndroidConstants.OPCODE_INSTALL_APPLICATION.equals(operation.getCode()) || + MDMAppConstants.AndroidConstants.OPCODE_UNINSTALL_APPLICATION.equals(operation.getCode())) { + ApplicationManager applicationManager = DeviceMgtAPIUtils.getApplicationManager(); + applicationManager.updateSubsStatus(device.getId(), operation.getId(),operation.getStatus().toString()); + } return Response.status(Response.Status.OK).entity("OperationStatus updated successfully.").build(); } catch (BadRequestException e) { - String msg = "Error occured due to invalid request"; + String msg = "Error occurred due to invalid request"; log.error(msg, e); return Response.status(Response.Status.BAD_REQUEST).entity(msg).build(); } catch (DeviceManagementException e) { @@ -1430,7 +1437,11 @@ public class DeviceManagementServiceImpl implements DeviceManagementService { String msg = "Error occurred when updating operation of device " + deviceIdentifier; log.error(msg, e); return Response.status(Response.Status.INTERNAL_SERVER_ERROR).entity(msg).build(); - } + } catch (io.entgra.application.mgt.common.exception.ApplicationManagementException e) { + String msg = "Error occurred when updating the application subscription status of the operation. " + + "The device identifier is: " + deviceIdentifier; + log.error(msg, e); + return Response.status(Response.Status.INTERNAL_SERVER_ERROR).entity(msg).build(); } } @GET diff --git a/components/device-mgt/org.wso2.carbon.device.mgt.api/src/main/java/org/wso2/carbon/device/mgt/jaxrs/util/DeviceMgtAPIUtils.java b/components/device-mgt/org.wso2.carbon.device.mgt.api/src/main/java/org/wso2/carbon/device/mgt/jaxrs/util/DeviceMgtAPIUtils.java index 8275ea0163..0a8d6c565c 100644 --- a/components/device-mgt/org.wso2.carbon.device.mgt.api/src/main/java/org/wso2/carbon/device/mgt/jaxrs/util/DeviceMgtAPIUtils.java +++ b/components/device-mgt/org.wso2.carbon.device.mgt.api/src/main/java/org/wso2/carbon/device/mgt/jaxrs/util/DeviceMgtAPIUtils.java @@ -35,6 +35,7 @@ package org.wso2.carbon.device.mgt.jaxrs.util; +import io.entgra.application.mgt.common.services.ApplicationManager; import org.apache.axis2.AxisFault; import org.apache.axis2.client.Options; import org.apache.axis2.java.security.SSLProtocolSocketFactory; @@ -172,6 +173,7 @@ public class DeviceMgtAPIUtils { private static OTPManagementService otpManagementService; private static volatile SubscriptionManager subscriptionManager; + private static volatile ApplicationManager applicationManager; static { String keyStorePassword = ServerConfiguration.getInstance().getFirstProperty("Security.KeyStore.Password"); @@ -233,6 +235,23 @@ public class DeviceMgtAPIUtils { return subscriptionManager; } + public static ApplicationManager getApplicationManager() { + if (applicationManager == null) { + synchronized (DeviceMgtAPIUtils.class) { + if (applicationManager == null) { + PrivilegedCarbonContext ctx = PrivilegedCarbonContext.getThreadLocalCarbonContext(); + applicationManager = + (ApplicationManager) ctx.getOSGiService(ApplicationManager.class, null); + if (applicationManager == null) { + String msg = "Application Manager service has not initialized."; + log.error(msg); + throw new IllegalStateException(msg); + } + } + } + } + return applicationManager; + } public static void scheduleTaskService(int notifierFrequency) { TaskScheduleService taskScheduleService;