Merge pull request #7 from milanperera/master

Refactored device enrolment in plugin
revert-dabc3590
kasundananjaya 9 years ago
commit d0d98ceff8

@ -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());
}
boolean isEnrolled = this.isEnrolled(new DeviceIdentifier(device.getDeviceIdentifier(), device.getType()));
if (isEnrolled) {
this.modifyEnrollment(device);
} else {
AndroidDAOFactory.beginTransaction();
status = daoFactory.getMobileDeviceDAO().addMobileDevice(
mobileDevice);
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;
}
@ -188,19 +195,19 @@ public class AndroidDeviceManager implements DeviceManager {
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 {

@ -19,17 +19,24 @@
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 {
private static Log log = LogFactory.getLog(AndroidPolicyMonitoringService.class);
@Override
public void notifyDevices(List<Device> list) throws PolicyComplianceException {
@ -37,8 +44,24 @@ public class AndroidPolicyMonitoringService implements PolicyMonitoringService {
@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<ComplianceFeature> complianceFeatures = (List<ComplianceFeature>) o;
complianceData.setComplianceFeatures(complianceFeatures);
for (ComplianceFeature cf : complianceFeatures) {
if(!cf.isCompliance()){
complianceData.setStatus(false);
break;
}
}
return complianceData;
}
@Override
public String getType() {

@ -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<String, String> deviceProperties, String property) {
String deviceProperty = deviceProperties.get(property);
return deviceProperty;
}
}
Loading…
Cancel
Save