diff --git a/components/device-mgt/org.wso2.carbon.device.mgt.mobile.impl/src/main/java/org/wso2/carbon/device/mgt/mobile/impl/android/AndroidDeviceManager.java b/components/device-mgt/org.wso2.carbon.device.mgt.mobile.impl/src/main/java/org/wso2/carbon/device/mgt/mobile/impl/android/AndroidDeviceManager.java index 2a2bfd181a..c9a5017147 100644 --- a/components/device-mgt/org.wso2.carbon.device.mgt.mobile.impl/src/main/java/org/wso2/carbon/device/mgt/mobile/impl/android/AndroidDeviceManager.java +++ b/components/device-mgt/org.wso2.carbon.device.mgt.mobile.impl/src/main/java/org/wso2/carbon/device/mgt/mobile/impl/android/AndroidDeviceManager.java @@ -21,6 +21,7 @@ package org.wso2.carbon.device.mgt.mobile.impl.android; import org.apache.commons.logging.Log; import org.apache.commons.logging.LogFactory; import org.wso2.carbon.device.mgt.common.*; + import org.wso2.carbon.device.mgt.common.configuration.mgt.ConfigurationEntry; import org.wso2.carbon.device.mgt.common.configuration.mgt.TenantConfiguration; import org.wso2.carbon.device.mgt.common.license.mgt.License; @@ -131,26 +132,30 @@ public class AndroidDeviceManager implements DeviceManager { @Override public boolean enrollDevice(Device device) throws DeviceManagementException { - boolean status; + boolean status = false; MobileDevice mobileDevice = MobileDeviceManagementUtil.convertToMobileDevice(device); try { if (log.isDebugEnabled()) { log.debug("Enrolling a new Android device : " + device.getDeviceIdentifier()); } - AndroidDAOFactory.beginTransaction(); - status = daoFactory.getMobileDeviceDAO().addMobileDevice( - mobileDevice); - AndroidDAOFactory.commitTransaction(); + boolean isEnrolled = this.isEnrolled(new DeviceIdentifier(device.getDeviceIdentifier(), device.getType())); + if (isEnrolled) { + this.modifyEnrollment(device); + } else { + AndroidDAOFactory.beginTransaction(); + status = daoFactory.getMobileDeviceDAO().addMobileDevice(mobileDevice); + AndroidDAOFactory.commitTransaction(); + } } catch (MobileDeviceManagementDAOException e) { try { AndroidDAOFactory.rollbackTransaction(); } catch (MobileDeviceManagementDAOException mobileDAOEx) { - String msg = "Error occurred while roll back the device enrol transaction :" + - device.toString(); + String msg = "Error occurred while roll back the device enrol transaction :" + device.toString(); log.warn(msg, mobileDAOEx); } - throw new DeviceManagementException("Error while enrolling the Android device: '" + - device.getDeviceIdentifier() + "'", e); + String msg = "Error while enrolling the Android device : " + device.getDeviceIdentifier(); + log.error(msg, e); + throw new DeviceManagementException(msg, e); } return status; } @@ -174,8 +179,10 @@ public class AndroidDeviceManager implements DeviceManager { device.toString(); log.warn(msg, mobileDAOEx); } - throw new DeviceManagementException("Error while updating the enrollment of the Android device: '" + - device.getDeviceIdentifier() + "'", e); + String msg = "Error while updating the enrollment of the Android device : " + + device.getDeviceIdentifier(); + log.error(msg, e); + throw new DeviceManagementException(msg, e); } return status; } @@ -185,22 +192,22 @@ public class AndroidDeviceManager implements DeviceManager { boolean status; try { if (log.isDebugEnabled()) { - log.debug("Dis-enrolling Android device: " + deviceId); + log.debug("Dis-enrolling Android device : " + deviceId); } AndroidDAOFactory.beginTransaction(); - status = daoFactory.getMobileDeviceDAO() - .deleteMobileDevice(deviceId.getId()); + status = daoFactory.getMobileDeviceDAO().deleteMobileDevice(deviceId.getId()); AndroidDAOFactory.commitTransaction(); } catch (MobileDeviceManagementDAOException e) { try { AndroidDAOFactory.rollbackTransaction(); - } catch (MobileDeviceManagementDAOException e1) { + } catch (MobileDeviceManagementDAOException mobileDAOEx) { String msg = "Error occurred while roll back the device dis enrol transaction :" + deviceId.toString(); - log.warn(msg, e1); + log.warn(msg, mobileDAOEx); } - throw new DeviceManagementException("Error occurred while removing the Android device: '" + - deviceId.getId() + "'", e); + String msg = "Error while removing the Android device : " + deviceId.getId(); + log.error(msg, e); + throw new DeviceManagementException(msg, e); } return status; } @@ -213,14 +220,15 @@ public class AndroidDeviceManager implements DeviceManager { log.debug("Checking the enrollment of Android device : " + deviceId.getId()); } MobileDevice mobileDevice = - daoFactory.getMobileDeviceDAO().getMobileDevice( - deviceId.getId()); + daoFactory.getMobileDeviceDAO().getMobileDevice(deviceId.getId()); if (mobileDevice != null) { isEnrolled = true; } } catch (MobileDeviceManagementDAOException e) { - throw new DeviceManagementException("Error occurred while checking the enrollment status of Android " + - "device: '" + deviceId.getId() + "'", e); + String msg = "Error while checking the enrollment status of Android device : " + + deviceId.getId(); + log.error(msg, e); + throw new DeviceManagementException(msg, e); } return isEnrolled; } @@ -270,6 +278,7 @@ public class AndroidDeviceManager implements DeviceManager { return false; } + @Override public License getLicense(String languageCode) throws LicenseManagementException { return licenseManager.getLicense(AndroidDeviceManagementService.DEVICE_TYPE_ANDROID, languageCode); @@ -284,17 +293,17 @@ public class AndroidDeviceManager implements DeviceManager { public boolean updateDeviceInfo(DeviceIdentifier deviceIdentifier, Device device) throws DeviceManagementException { boolean status; - Device deviceDB = this.getDevice(deviceIdentifier); + Device existingDevice = this.getDevice(deviceIdentifier); // This object holds the current persisted device object - MobileDevice mobileDeviceDB = MobileDeviceManagementUtil.convertToMobileDevice(deviceDB); + MobileDevice existingMobileDevice = MobileDeviceManagementUtil.convertToMobileDevice(existingDevice); // This object holds the newly received device object from response MobileDevice mobileDevice = MobileDeviceManagementUtil.convertToMobileDevice(device); // Updating current object features using newer ones - mobileDeviceDB.setLatitude(mobileDevice.getLatitude()); - mobileDeviceDB.setLongitude(mobileDevice.getLongitude()); - mobileDeviceDB.setDeviceProperties(mobileDevice.getDeviceProperties()); + existingMobileDevice.setLatitude(mobileDevice.getLatitude()); + existingMobileDevice.setLongitude(mobileDevice.getLongitude()); + existingMobileDevice.setDeviceProperties(mobileDevice.getDeviceProperties()); try { if (log.isDebugEnabled()) { @@ -302,7 +311,7 @@ public class AndroidDeviceManager implements DeviceManager { } AndroidDAOFactory.beginTransaction(); status = daoFactory.getMobileDeviceDAO() - .updateMobileDevice(mobileDeviceDB); + .updateMobileDevice(existingMobileDevice); AndroidDAOFactory.commitTransaction(); } catch (MobileDeviceManagementDAOException e) { try { diff --git a/components/device-mgt/org.wso2.carbon.device.mgt.mobile.impl/src/main/java/org/wso2/carbon/device/mgt/mobile/impl/android/AndroidPolicyMonitoringService.java b/components/device-mgt/org.wso2.carbon.device.mgt.mobile.impl/src/main/java/org/wso2/carbon/device/mgt/mobile/impl/android/AndroidPolicyMonitoringService.java index 7b3f7e85a3..de37861c83 100644 --- a/components/device-mgt/org.wso2.carbon.device.mgt.mobile.impl/src/main/java/org/wso2/carbon/device/mgt/mobile/impl/android/AndroidPolicyMonitoringService.java +++ b/components/device-mgt/org.wso2.carbon.device.mgt.mobile.impl/src/main/java/org/wso2/carbon/device/mgt/mobile/impl/android/AndroidPolicyMonitoringService.java @@ -19,29 +19,52 @@ package org.wso2.carbon.device.mgt.mobile.impl.android; +import org.apache.commons.logging.Log; +import org.apache.commons.logging.LogFactory; import org.wso2.carbon.device.mgt.common.Device; import org.wso2.carbon.device.mgt.common.DeviceIdentifier; import org.wso2.carbon.device.mgt.common.DeviceManagementConstants; import org.wso2.carbon.policy.mgt.common.Policy; import org.wso2.carbon.policy.mgt.common.monitor.ComplianceData; +import org.wso2.carbon.policy.mgt.common.monitor.ComplianceFeature; import org.wso2.carbon.policy.mgt.common.monitor.PolicyComplianceException; import org.wso2.carbon.policy.mgt.common.spi.PolicyMonitoringService; +import java.util.ArrayList; import java.util.List; public class AndroidPolicyMonitoringService implements PolicyMonitoringService { - @Override - public void notifyDevices(List list) throws PolicyComplianceException { - } + private static Log log = LogFactory.getLog(AndroidPolicyMonitoringService.class); - @Override - public ComplianceData checkPolicyCompliance(DeviceIdentifier deviceIdentifier, Policy policy, Object o) throws PolicyComplianceException { - return null; - } + @Override + public void notifyDevices(List list) throws PolicyComplianceException { - @Override - public String getType() { - return DeviceManagementConstants.MobileDeviceTypes.MOBILE_DEVICE_TYPE_ANDROID; - } -} + } + + @Override + public ComplianceData checkPolicyCompliance(DeviceIdentifier deviceIdentifier, Policy policy, Object o) throws PolicyComplianceException { + ComplianceData complianceData = new ComplianceData(); + if (log.isDebugEnabled()) { + log.debug("Checking policy compliance status of device '" + deviceIdentifier.getId() + "'"); + } + if (o == null || policy == null) { + return null; + } + List complianceFeatures = (List) o; + complianceData.setComplianceFeatures(complianceFeatures); + + for (ComplianceFeature cf : complianceFeatures) { + if(!cf.isCompliance()){ + complianceData.setStatus(false); + break; + } + } + return complianceData; + } + + @Override + public String getType() { + return DeviceManagementConstants.MobileDeviceTypes.MOBILE_DEVICE_TYPE_ANDROID; + } +} \ No newline at end of file diff --git a/components/device-mgt/org.wso2.carbon.device.mgt.mobile.impl/src/main/java/org/wso2/carbon/device/mgt/mobile/impl/android/util/AndroidUtils.java b/components/device-mgt/org.wso2.carbon.device.mgt.mobile.impl/src/main/java/org/wso2/carbon/device/mgt/mobile/impl/android/util/AndroidUtils.java new file mode 100644 index 0000000000..d177680993 --- /dev/null +++ b/components/device-mgt/org.wso2.carbon.device.mgt.mobile.impl/src/main/java/org/wso2/carbon/device/mgt/mobile/impl/android/util/AndroidUtils.java @@ -0,0 +1,33 @@ +/* + * Copyright (c) 2015, 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.mgt.mobile.impl.android.util; + +import java.util.Map; + +/** + * Contains utility methods used by Android plugin. + */ +public class AndroidUtils { + + public static String getDeviceProperty(Map deviceProperties, String property) { + + String deviceProperty = deviceProperties.get(property); + return deviceProperty; + } +}