Fixed tenant configuration persisting issue

revert-dabc3590
harshanl 10 years ago
parent d0d98ceff8
commit a89b67aa25

@ -21,8 +21,6 @@ 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;
import org.wso2.carbon.device.mgt.common.license.mgt.LicenseManagementException;
@ -33,12 +31,19 @@ import org.wso2.carbon.device.mgt.mobile.dao.MobileDeviceManagementDAOException;
import org.wso2.carbon.device.mgt.mobile.dao.MobileDeviceManagementDAOFactory;
import org.wso2.carbon.device.mgt.mobile.dto.MobileDevice;
import org.wso2.carbon.device.mgt.mobile.impl.android.dao.AndroidDAOFactory;
import org.wso2.carbon.device.mgt.mobile.impl.android.util.AndroidPluginConstants;
import org.wso2.carbon.device.mgt.mobile.impl.android.util.AndroidPluginUtils;
import org.wso2.carbon.device.mgt.mobile.util.MobileDeviceManagementUtil;
import org.wso2.carbon.registry.api.Collection;
import org.wso2.carbon.registry.api.RegistryException;
import org.wso2.carbon.registry.api.Resource;
import javax.xml.bind.JAXBContext;
import javax.xml.bind.JAXBException;
import javax.xml.bind.Marshaller;
import javax.xml.bind.Unmarshaller;
import java.io.StringReader;
import java.io.StringWriter;
import java.nio.charset.Charset;
import java.util.ArrayList;
import java.util.List;
@ -55,7 +60,8 @@ public class AndroidDeviceManager implements DeviceManager {
License defaultLicense = AndroidPluginUtils.getDefaultLicense();
try {
licenseManager.addLicense(AndroidDeviceManagementService.DEVICE_TYPE_ANDROID, defaultLicense);
licenseManager
.addLicense(AndroidDeviceManagementService.DEVICE_TYPE_ANDROID, defaultLicense);
} catch (LicenseManagementException e) {
log.error("Error occurred while adding default license for Android devices", e);
}
@ -69,7 +75,7 @@ public class AndroidDeviceManager implements DeviceManager {
@Override
public boolean saveConfiguration(TenantConfiguration tenantConfiguration)
throws DeviceManagementException {
boolean status;
boolean status = false;
Resource resource;
try {
if (log.isDebugEnabled()) {
@ -78,12 +84,15 @@ public class AndroidDeviceManager implements DeviceManager {
String resourcePath = MobileDeviceManagementUtil.getPlatformConfigPath(
DeviceManagementConstants.
MobileDeviceTypes.MOBILE_DEVICE_TYPE_ANDROID);
MobileDeviceManagementUtil.createRegistryCollection(resourcePath);
for (ConfigurationEntry configEntry : tenantConfiguration.getConfiguration()) {
StringWriter writer = new StringWriter();
JAXBContext context = JAXBContext.newInstance(TenantConfiguration.class);
Marshaller marshaller = context.createMarshaller();
marshaller.marshal(tenantConfiguration, writer);
resource = MobileDeviceManagementUtil.getRegistry().newResource();
resource.setContent(configEntry.getValue());
MobileDeviceManagementUtil.putRegistryResource(resourcePath + "/" + configEntry.getName(), resource);
}
resource.setContent(writer.toString());
resource.setMediaType(AndroidPluginConstants.MEDIA_TYPE_XML);
MobileDeviceManagementUtil.putRegistryResource(resourcePath, resource);
status = true;
} catch (MobileDeviceMgtPluginException e) {
throw new DeviceManagementException(
@ -91,43 +100,37 @@ public class AndroidDeviceManager implements DeviceManager {
} catch (RegistryException e) {
throw new DeviceManagementException(
"Error occurred while persisting the Registry resource : " + e.getMessage(), e);
} catch (JAXBException e) {
throw new DeviceManagementException(
"Error occurred while parsing the configuration : " + e.getMessage(), e);
}
return status;
}
@Override
public TenantConfiguration getConfiguration() throws DeviceManagementException {
Collection dsCollection;
TenantConfiguration tenantConfiguration;
List<ConfigurationEntry> configs = new ArrayList<>();
ConfigurationEntry entry;
Resource resource;
try {
String androidRegPath =
MobileDeviceManagementUtil.getPlatformConfigPath(DeviceManagementConstants.
MobileDeviceTypes.MOBILE_DEVICE_TYPE_ANDROID);
dsCollection =
(Collection) MobileDeviceManagementUtil.getRegistryResource(androidRegPath);
String[] dsmPaths = dsCollection.getChildren();
for (String dsmPath : dsmPaths) {
entry = new ConfigurationEntry();
resource = MobileDeviceManagementUtil.getRegistryResource(dsmPath);
entry.setValue(resource.getContent());
entry.setName(resource.getId());
configs.add(entry);
}
tenantConfiguration = new TenantConfiguration();
tenantConfiguration.setConfiguration(configs);
tenantConfiguration.setType(DeviceManagementConstants.
MobileDeviceTypes.MOBILE_DEVICE_TYPE_ANDROID);
resource = MobileDeviceManagementUtil.getRegistryResource(androidRegPath);
JAXBContext context = JAXBContext.newInstance(TenantConfiguration.class);
Unmarshaller unmarshaller = context.createUnmarshaller();
return (TenantConfiguration) unmarshaller.unmarshal(
new StringReader(new String((byte[]) resource.getContent(), Charset
.forName("UTF8"))));
} catch (MobileDeviceMgtPluginException e) {
throw new DeviceManagementException(
"Error occurred while retrieving the Registry instance : " + e.getMessage(), e);
} catch (JAXBException e) {
throw new DeviceManagementException(
"Error occurred while parsing the configuration : " + e.getMessage(), e);
} catch (RegistryException e) {
throw new DeviceManagementException(
"Error occurred while retrieving the Registry data : " + e.getMessage(), e);
"Error occurred while retrieving the Registry resource : " + e.getMessage(), e);
}
return tenantConfiguration;
}
@Override
@ -138,7 +141,8 @@ public class AndroidDeviceManager implements DeviceManager {
if (log.isDebugEnabled()) {
log.debug("Enrolling a new Android device : " + device.getDeviceIdentifier());
}
boolean isEnrolled = this.isEnrolled(new DeviceIdentifier(device.getDeviceIdentifier(), device.getType()));
boolean isEnrolled = this.isEnrolled(
new DeviceIdentifier(device.getDeviceIdentifier(), device.getType()));
if (isEnrolled) {
this.modifyEnrollment(device);
} else {
@ -150,10 +154,12 @@ public class AndroidDeviceManager implements DeviceManager {
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);
}
String msg = "Error while enrolling the Android device : " + device.getDeviceIdentifier();
String msg =
"Error while enrolling the Android device : " + device.getDeviceIdentifier();
log.error(msg, e);
throw new DeviceManagementException(msg, e);
}
@ -255,7 +261,8 @@ public class AndroidDeviceManager implements DeviceManager {
getMobileDevice(deviceId.getId());
device = MobileDeviceManagementUtil.convertToDevice(mobileDevice);
} catch (MobileDeviceManagementDAOException e) {
throw new DeviceManagementException("Error occurred while fetching the Android device: '" +
throw new DeviceManagementException(
"Error occurred while fetching the Android device: '" +
deviceId.getId() + "'", e);
}
return device;
@ -278,10 +285,10 @@ public class AndroidDeviceManager implements DeviceManager {
return false;
}
@Override
public License getLicense(String languageCode) throws LicenseManagementException {
return licenseManager.getLicense(AndroidDeviceManagementService.DEVICE_TYPE_ANDROID, languageCode);
return licenseManager
.getLicense(AndroidDeviceManagementService.DEVICE_TYPE_ANDROID, languageCode);
}
@Override
@ -295,7 +302,8 @@ public class AndroidDeviceManager implements DeviceManager {
boolean status;
Device existingDevice = this.getDevice(deviceIdentifier);
// This object holds the current persisted device object
MobileDevice existingMobileDevice = MobileDeviceManagementUtil.convertToMobileDevice(existingDevice);
MobileDevice existingMobileDevice =
MobileDeviceManagementUtil.convertToMobileDevice(existingDevice);
// This object holds the newly received device object from response
MobileDevice mobileDevice = MobileDeviceManagementUtil.convertToMobileDevice(device);
@ -307,7 +315,8 @@ public class AndroidDeviceManager implements DeviceManager {
try {
if (log.isDebugEnabled()) {
log.debug("updating the details of Android device : " + device.getDeviceIdentifier());
log.debug(
"updating the details of Android device : " + device.getDeviceIdentifier());
}
AndroidDAOFactory.beginTransaction();
status = daoFactory.getMobileDeviceDAO()
@ -320,7 +329,8 @@ public class AndroidDeviceManager implements DeviceManager {
log.warn("Error occurred while roll back the update device info transaction : '" +
device.toString() + "'", e1);
}
throw new DeviceManagementException("Error occurred while updating the Android device: '" +
throw new DeviceManagementException(
"Error occurred while updating the Android device: '" +
device.getDeviceIdentifier() + "'", e);
}
return status;
@ -342,7 +352,8 @@ public class AndroidDeviceManager implements DeviceManager {
}
}
} catch (MobileDeviceManagementDAOException e) {
throw new DeviceManagementException("Error occurred while fetching all Android devices", e);
throw new DeviceManagementException("Error occurred while fetching all Android devices",
e);
}
return devices;
}

@ -44,4 +44,6 @@ public class AndroidPluginConstants {
public static final String ANDROID_FEATURE_NAME = "NAME";
public static final String ANDROID_FEATURE_DESCRIPTION = "DESCRIPTION";
public static final String MEDIA_TYPE_XML = "application/xml";
}

@ -31,7 +31,6 @@ import org.wso2.carbon.device.mgt.mobile.common.MobileDeviceMgtPluginException;
import org.wso2.carbon.device.mgt.mobile.common.MobilePluginConstants;
import org.wso2.carbon.device.mgt.mobile.dto.*;
import org.wso2.carbon.device.mgt.mobile.internal.MobileDeviceManagementDataHolder;
import org.wso2.carbon.device.mgt.mobile.internal.MobileDeviceManagementDataHolder;
import org.wso2.carbon.registry.api.RegistryException;
import org.wso2.carbon.registry.api.Resource;
import org.wso2.carbon.registry.core.Registry;
@ -260,12 +259,10 @@ public class MobileDeviceManagementUtil {
String regPath = "";
switch (platform) {
case DeviceManagementConstants.MobileDeviceTypes.MOBILE_DEVICE_TYPE_ANDROID:
regPath = MobilePluginConstants.MOBILE_CONFIG_REGISTRY_ROOT + "/" +
DeviceManagementConstants.MobileDeviceTypes.MOBILE_DEVICE_TYPE_ANDROID;
regPath = DeviceManagementConstants.MobileDeviceTypes.MOBILE_DEVICE_TYPE_ANDROID;
break;
case DeviceManagementConstants.MobileDeviceTypes.MOBILE_DEVICE_TYPE_WINDOWS:
regPath = MobilePluginConstants.MOBILE_CONFIG_REGISTRY_ROOT + "/" +
DeviceManagementConstants.MobileDeviceTypes.MOBILE_DEVICE_TYPE_WINDOWS;
regPath = DeviceManagementConstants.MobileDeviceTypes.MOBILE_DEVICE_TYPE_WINDOWS;
break;
}
return regPath;

Loading…
Cancel
Save