From 6e469087b6d525a1b603443c5711be822ce520e2 Mon Sep 17 00:00:00 2001 From: harshanl Date: Fri, 21 Aug 2015 17:42:06 +0530 Subject: [PATCH] Tenant configuration changes --- .../impl/android/AndroidDeviceManager.java | 19 +++++++------- .../impl/windows/WindowsDeviceManager.java | 15 ++++++----- .../util/MobileDeviceManagementUtil.java | 26 ++++++++++--------- 3 files changed, 33 insertions(+), 27 deletions(-) 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 d4e064ef4..5db5baa8b 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 @@ -75,8 +75,7 @@ public class AndroidDeviceManager implements DeviceManager { @Override public boolean saveConfiguration(TenantConfiguration tenantConfiguration) throws DeviceManagementException { - boolean status = false; - Resource resource; + boolean status; try { if (log.isDebugEnabled()) { log.debug("Persisting android configurations in Registry"); @@ -89,7 +88,7 @@ public class AndroidDeviceManager implements DeviceManager { Marshaller marshaller = context.createMarshaller(); marshaller.marshal(tenantConfiguration, writer); - resource = MobileDeviceManagementUtil.getRegistry().newResource(); + Resource resource = MobileDeviceManagementUtil.getConfigurationRegistry().newResource(); resource.setContent(writer.toString()); resource.setMediaType(MobilePluginConstants.MEDIA_TYPE_XML); MobileDeviceManagementUtil.putRegistryResource(resourcePath, resource); @@ -115,12 +114,14 @@ public class AndroidDeviceManager implements DeviceManager { MobileDeviceManagementUtil.getPlatformConfigPath(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(MobilePluginConstants.CHARSET_UTF8)))); - + if(resource != null){ + JAXBContext context = JAXBContext.newInstance(TenantConfiguration.class); + Unmarshaller unmarshaller = context.createUnmarshaller(); + return (TenantConfiguration) unmarshaller.unmarshal( + new StringReader(new String((byte[]) resource.getContent(), Charset + .forName(MobilePluginConstants.CHARSET_UTF8)))); + } + return new TenantConfiguration(); } catch (MobileDeviceMgtPluginException e) { throw new DeviceManagementException( "Error occurred while retrieving the Registry instance : " + e.getMessage(), e); diff --git a/components/device-mgt/org.wso2.carbon.device.mgt.mobile.impl/src/main/java/org/wso2/carbon/device/mgt/mobile/impl/windows/WindowsDeviceManager.java b/components/device-mgt/org.wso2.carbon.device.mgt.mobile.impl/src/main/java/org/wso2/carbon/device/mgt/mobile/impl/windows/WindowsDeviceManager.java index d7dbe3e61..691fe454d 100644 --- a/components/device-mgt/org.wso2.carbon.device.mgt.mobile.impl/src/main/java/org/wso2/carbon/device/mgt/mobile/impl/windows/WindowsDeviceManager.java +++ b/components/device-mgt/org.wso2.carbon.device.mgt.mobile.impl/src/main/java/org/wso2/carbon/device/mgt/mobile/impl/windows/WindowsDeviceManager.java @@ -79,7 +79,7 @@ public class WindowsDeviceManager implements DeviceManager { Marshaller marshaller = context.createMarshaller(); marshaller.marshal(tenantConfiguration, writer); - resource = MobileDeviceManagementUtil.getRegistry().newResource(); + resource = MobileDeviceManagementUtil.getConfigurationRegistry().newResource(); resource.setContent(writer.toString()); resource.setMediaType(MobilePluginConstants.MEDIA_TYPE_XML); MobileDeviceManagementUtil.putRegistryResource(resourcePath, resource); @@ -105,11 +105,14 @@ public class WindowsDeviceManager implements DeviceManager { MobileDeviceManagementUtil.getPlatformConfigPath(DeviceManagementConstants. MobileDeviceTypes.MOBILE_DEVICE_TYPE_WINDOWS); 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(MobilePluginConstants.CHARSET_UTF8)))); + if(resource != null){ + JAXBContext context = JAXBContext.newInstance(TenantConfiguration.class); + Unmarshaller unmarshaller = context.createUnmarshaller(); + return (TenantConfiguration) unmarshaller.unmarshal( + new StringReader(new String((byte[]) resource.getContent(), Charset + .forName(MobilePluginConstants.CHARSET_UTF8)))); + } + return new TenantConfiguration(); } catch (MobileDeviceMgtPluginException e) { throw new DeviceManagementException( diff --git a/components/device-mgt/org.wso2.carbon.device.mgt.mobile.impl/src/main/java/org/wso2/carbon/device/mgt/mobile/util/MobileDeviceManagementUtil.java b/components/device-mgt/org.wso2.carbon.device.mgt.mobile.impl/src/main/java/org/wso2/carbon/device/mgt/mobile/util/MobileDeviceManagementUtil.java index 3975c08f6..768c55d4c 100644 --- a/components/device-mgt/org.wso2.carbon.device.mgt.mobile.impl/src/main/java/org/wso2/carbon/device/mgt/mobile/util/MobileDeviceManagementUtil.java +++ b/components/device-mgt/org.wso2.carbon.device.mgt.mobile.impl/src/main/java/org/wso2/carbon/device/mgt/mobile/util/MobileDeviceManagementUtil.java @@ -198,7 +198,7 @@ public class MobileDeviceManagementUtil { return feature; } - public static Registry getRegistry() throws MobileDeviceMgtPluginException { + public static Registry getConfigurationRegistry() throws MobileDeviceMgtPluginException { try { int tenantId = PrivilegedCarbonContext.getThreadLocalCarbonContext().getTenantId(); return MobileDeviceManagementDataHolder.getInstance().getRegistryService() @@ -213,8 +213,10 @@ public class MobileDeviceManagementUtil { public static Resource getRegistryResource(String path) throws MobileDeviceMgtPluginException { try { - return MobileDeviceManagementUtil.getRegistry().get(path); - + if(MobileDeviceManagementUtil.getConfigurationRegistry().resourceExists(path)){ + return MobileDeviceManagementUtil.getConfigurationRegistry().get(path); + } + return null; } catch (RegistryException e) { throw new MobileDeviceMgtPluginException("Error in retrieving registry resource : " + e.getMessage(), e); @@ -224,11 +226,11 @@ public class MobileDeviceManagementUtil { public static boolean putRegistryResource(String path, Resource resource) throws MobileDeviceMgtPluginException { - boolean status = false; + boolean status; try { - MobileDeviceManagementUtil.getRegistry().beginTransaction(); - MobileDeviceManagementUtil.getRegistry().put(path, resource); - MobileDeviceManagementUtil.getRegistry().commitTransaction(); + MobileDeviceManagementUtil.getConfigurationRegistry().beginTransaction(); + MobileDeviceManagementUtil.getConfigurationRegistry().put(path, resource); + MobileDeviceManagementUtil.getConfigurationRegistry().commitTransaction(); status = true; } catch (RegistryException e) { throw new MobileDeviceMgtPluginException( @@ -279,11 +281,11 @@ public class MobileDeviceManagementUtil { public static boolean createRegistryCollection(String path) throws MobileDeviceMgtPluginException { try { - if (! MobileDeviceManagementUtil.getRegistry().resourceExists(path)) { - Resource resource = MobileDeviceManagementUtil.getRegistry().newCollection(); - MobileDeviceManagementUtil.getRegistry().beginTransaction(); - MobileDeviceManagementUtil.getRegistry().put(path, resource); - MobileDeviceManagementUtil.getRegistry().commitTransaction(); + if (! MobileDeviceManagementUtil.getConfigurationRegistry().resourceExists(path)) { + Resource resource = MobileDeviceManagementUtil.getConfigurationRegistry().newCollection(); + MobileDeviceManagementUtil.getConfigurationRegistry().beginTransaction(); + MobileDeviceManagementUtil.getConfigurationRegistry().put(path, resource); + MobileDeviceManagementUtil.getConfigurationRegistry().commitTransaction(); } return true; } catch (MobileDeviceMgtPluginException e) {