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

@ -44,4 +44,6 @@ public class AndroidPluginConstants {
public static final String ANDROID_FEATURE_NAME = "NAME"; public static final String ANDROID_FEATURE_NAME = "NAME";
public static final String ANDROID_FEATURE_DESCRIPTION = "DESCRIPTION"; 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.common.MobilePluginConstants;
import org.wso2.carbon.device.mgt.mobile.dto.*; 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.device.mgt.mobile.internal.MobileDeviceManagementDataHolder;
import org.wso2.carbon.registry.api.RegistryException; import org.wso2.carbon.registry.api.RegistryException;
import org.wso2.carbon.registry.api.Resource; import org.wso2.carbon.registry.api.Resource;
import org.wso2.carbon.registry.core.Registry; import org.wso2.carbon.registry.core.Registry;
@ -260,12 +259,10 @@ public class MobileDeviceManagementUtil {
String regPath = ""; String regPath = "";
switch (platform) { switch (platform) {
case DeviceManagementConstants.MobileDeviceTypes.MOBILE_DEVICE_TYPE_ANDROID: case DeviceManagementConstants.MobileDeviceTypes.MOBILE_DEVICE_TYPE_ANDROID:
regPath = MobilePluginConstants.MOBILE_CONFIG_REGISTRY_ROOT + "/" + regPath = DeviceManagementConstants.MobileDeviceTypes.MOBILE_DEVICE_TYPE_ANDROID;
DeviceManagementConstants.MobileDeviceTypes.MOBILE_DEVICE_TYPE_ANDROID;
break; break;
case DeviceManagementConstants.MobileDeviceTypes.MOBILE_DEVICE_TYPE_WINDOWS: case DeviceManagementConstants.MobileDeviceTypes.MOBILE_DEVICE_TYPE_WINDOWS:
regPath = MobilePluginConstants.MOBILE_CONFIG_REGISTRY_ROOT + "/" + regPath = DeviceManagementConstants.MobileDeviceTypes.MOBILE_DEVICE_TYPE_WINDOWS;
DeviceManagementConstants.MobileDeviceTypes.MOBILE_DEVICE_TYPE_WINDOWS;
break; break;
} }
return regPath; return regPath;

Loading…
Cancel
Save