From d07c226dd52d29fbc7ee2993ecf0f9700aa43ece Mon Sep 17 00:00:00 2001 From: harshanl Date: Wed, 22 Jul 2015 15:39:02 +0530 Subject: [PATCH] Added tenant configuration related changes --- .../pom.xml | 16 +- .../mobile/common/MobilePluginConstants.java | 1 + .../impl/android/AndroidDeviceManager.java | 484 ++++++++++-------- .../impl/windows/WindowsDeviceManager.java | 14 + ...obileDeviceManagementServiceComponent.java | 15 + ...bileDeviceManagementServiceDataHolder.java | 47 ++ .../util/MobileDeviceManagementUtil.java | 175 +++++-- pom.xml | 70 ++- 8 files changed, 579 insertions(+), 243 deletions(-) create mode 100644 components/device-mgt/org.wso2.carbon.device.mgt.mobile.impl/src/main/java/org/wso2/carbon/device/mgt/mobile/internal/MobileDeviceManagementServiceDataHolder.java diff --git a/components/device-mgt/org.wso2.carbon.device.mgt.mobile.impl/pom.xml b/components/device-mgt/org.wso2.carbon.device.mgt.mobile.impl/pom.xml index 00af11e11d..c0b4489aa9 100644 --- a/components/device-mgt/org.wso2.carbon.device.mgt.mobile.impl/pom.xml +++ b/components/device-mgt/org.wso2.carbon.device.mgt.mobile.impl/pom.xml @@ -65,11 +65,17 @@ javax.xml.parsers, org.w3c.dom, org.wso2.carbon.core, + org.wso2.carbon.context, org.wso2.carbon.utils.*, org.wso2.carbon.device.mgt.common.*, org.wso2.carbon.ndatasource.core, org.wso2.carbon.policy.mgt.common.*, - org.wso2.carbon.policy.mgt.core.* + org.wso2.carbon.policy.mgt.core.*, + org.wso2.carbon.registry.core, + org.wso2.carbon.registry.core.exceptions, + org.wso2.carbon.registry.core.service, + org.wso2.carbon.registry.core.session, + org.wso2.carbon.registry.api, !org.wso2.carbon.device.mgt.mobile.internal, @@ -133,6 +139,14 @@ org.wso2.carbon.devicemgt org.wso2.carbon.policy.mgt.core + + org.wso2.carbon + org.wso2.carbon.registry.api + + + org.wso2.carbon + org.wso2.carbon.registry.core + org.testng testng diff --git a/components/device-mgt/org.wso2.carbon.device.mgt.mobile.impl/src/main/java/org/wso2/carbon/device/mgt/mobile/common/MobilePluginConstants.java b/components/device-mgt/org.wso2.carbon.device.mgt.mobile.impl/src/main/java/org/wso2/carbon/device/mgt/mobile/common/MobilePluginConstants.java index 8287e60c84..b5f19808b1 100644 --- a/components/device-mgt/org.wso2.carbon.device.mgt.mobile.impl/src/main/java/org/wso2/carbon/device/mgt/mobile/common/MobilePluginConstants.java +++ b/components/device-mgt/org.wso2.carbon.device.mgt.mobile.impl/src/main/java/org/wso2/carbon/device/mgt/mobile/common/MobilePluginConstants.java @@ -19,5 +19,6 @@ package org.wso2.carbon.device.mgt.mobile.common; public class MobilePluginConstants { public static final String MOBILE_DB_SCRIPTS_FOLDER = "cdm"; + public static final String MOBILE_CONFIG_REGISTRY_ROOT = "/_system/config"; } 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 d6c1e7c98b..20692e5e66 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,239 +21,313 @@ 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.mobile.common.MobileDeviceMgtPluginException; 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.util.MobileDeviceManagementUtil; +import org.wso2.carbon.registry.api.Collection; +import org.wso2.carbon.registry.api.Resource; +import org.wso2.carbon.registry.api.RegistryException; import java.util.ArrayList; import java.util.List; public class AndroidDeviceManager implements DeviceManager { - private MobileDeviceManagementDAOFactory mobileDeviceManagementDAOFactory; - private static final Log log = LogFactory.getLog(AndroidDeviceManagementService.class); - private FeatureManager featureManager = new AndroidFeatureManager(); - private License license; + private MobileDeviceManagementDAOFactory mobileDeviceManagementDAOFactory; + private static final Log log = LogFactory.getLog(AndroidDeviceManagementService.class); + private FeatureManager featureManager = new AndroidFeatureManager(); + private License license; - public AndroidDeviceManager() { - mobileDeviceManagementDAOFactory = new AndroidDAOFactory(); - } + public AndroidDeviceManager() { + mobileDeviceManagementDAOFactory = new AndroidDAOFactory(); + } - @Override - public FeatureManager getFeatureManager() { - return featureManager; - } + @Override + public FeatureManager getFeatureManager() { + return featureManager; + } - @Override - public boolean enrollDevice(Device device) throws DeviceManagementException { - boolean status; - MobileDevice mobileDevice = MobileDeviceManagementUtil.convertToMobileDevice(device); - try { - if (log.isDebugEnabled()) { - log.debug("Enrolling a new Android device : " + device.getDeviceIdentifier()); - } - AndroidDAOFactory.beginTransaction(); - status = mobileDeviceManagementDAOFactory.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(); - log.warn(msg, mobileDAOEx); - } - String msg = "Error while enrolling the Android device : " + device.getDeviceIdentifier(); - log.error(msg, e); - throw new DeviceManagementException(msg, e); - } - return status; - } + @Override + public boolean saveConfiguration(TenantConfiguration tenantConfiguration) + throws DeviceManagementException { + boolean status = false; + Resource resource; + try { + if (log.isDebugEnabled()) { + log.debug("Persisting android configurations in Registry"); + } + String resourcePath = MobileDeviceManagementUtil.getPlatformConfigPath( + DeviceManagementConstants. + MobileDeviceTypes.MOBILE_DEVICE_TYPE_ANDROID); + MobileDeviceManagementUtil.createRegistryCollection(resourcePath); + for (ConfigurationEntry configEntry : tenantConfiguration.getConfiguration()) { + resource = MobileDeviceManagementUtil.getRegistry().newResource(); + resource.setContent(configEntry.getValue()); + MobileDeviceManagementUtil.putRegistryResource(resourcePath + "/" + configEntry.getName(), resource); + } + status = true; + } catch (MobileDeviceMgtPluginException e) { + throw new DeviceManagementException( + "Error occurred while retrieving the Registry instance : " + e.getMessage(), e); + } catch (RegistryException e) { + throw new DeviceManagementException( + "Error occurred while persisting the Registry resource : " + e.getMessage(), e); + } + return status; + } - @Override - public boolean modifyEnrollment(Device device) throws DeviceManagementException { - boolean status; - MobileDevice mobileDevice = MobileDeviceManagementUtil.convertToMobileDevice(device); - try { - if (log.isDebugEnabled()) { - log.debug("Modifying the Android device enrollment data"); - } - AndroidDAOFactory.beginTransaction(); - status = mobileDeviceManagementDAOFactory.getMobileDeviceDAO() - .updateMobileDevice(mobileDevice); - AndroidDAOFactory.commitTransaction(); - } catch (MobileDeviceManagementDAOException e) { - try { - AndroidDAOFactory.rollbackTransaction(); - } catch (MobileDeviceManagementDAOException mobileDAOEx) { - String msg = "Error occurred while roll back the update device transaction :" + device.toString(); - log.warn(msg, mobileDAOEx); - } - String msg = "Error while updating the enrollment of the Android device : " + - device.getDeviceIdentifier(); - log.error(msg, e); - throw new DeviceManagementException(msg, e); - } - return status; - } + @Override + public TenantConfiguration getConfiguration() throws DeviceManagementException { + Collection dsCollection = null; + TenantConfiguration tenantConfiguration; + List 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); + } catch (MobileDeviceMgtPluginException e) { + throw new DeviceManagementException( + "Error occurred while retrieving the Registry instance : " + e.getMessage(), e); + } catch (RegistryException e) { + throw new DeviceManagementException( + "Error occurred while retrieving the Registry data : " + e.getMessage(), e); + } + return tenantConfiguration; + } - @Override - public boolean disenrollDevice(DeviceIdentifier deviceId) throws DeviceManagementException { - boolean status; - try { - if (log.isDebugEnabled()) { - log.debug("Dis-enrolling Android device : " + deviceId); - } - AndroidDAOFactory.beginTransaction(); - status = mobileDeviceManagementDAOFactory.getMobileDeviceDAO() - .deleteMobileDevice(deviceId.getId()); - AndroidDAOFactory.commitTransaction(); - } catch (MobileDeviceManagementDAOException e) { - try { - AndroidDAOFactory.rollbackTransaction(); - } catch (MobileDeviceManagementDAOException mobileDAOEx) { - String msg = "Error occurred while roll back the device dis enrol transaction :" + deviceId.toString(); - log.warn(msg, mobileDAOEx); - } - String msg = "Error while removing the Android device : " + deviceId.getId(); - log.error(msg, e); - throw new DeviceManagementException(msg, e); - } - return status; - } + @Override + public boolean enrollDevice(Device device) throws DeviceManagementException { + boolean status; + MobileDevice mobileDevice = MobileDeviceManagementUtil.convertToMobileDevice(device); + try { + if (log.isDebugEnabled()) { + log.debug("Enrolling a new Android device : " + device.getDeviceIdentifier()); + } + AndroidDAOFactory.beginTransaction(); + status = mobileDeviceManagementDAOFactory.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(); + log.warn(msg, mobileDAOEx); + } + String msg = + "Error while enrolling the Android device : " + device.getDeviceIdentifier(); + log.error(msg, e); + throw new DeviceManagementException(msg, e); + } + return status; + } - @Override - public boolean isEnrolled(DeviceIdentifier deviceId) throws DeviceManagementException { - boolean isEnrolled = false; - try { - if (log.isDebugEnabled()) { - log.debug("Checking the enrollment of Android device : " + deviceId.getId()); - } - MobileDevice mobileDevice = - mobileDeviceManagementDAOFactory.getMobileDeviceDAO().getMobileDevice( - deviceId.getId()); - if (mobileDevice != null) { - isEnrolled = true; - } - } catch (MobileDeviceManagementDAOException 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; - } + @Override + public boolean modifyEnrollment(Device device) throws DeviceManagementException { + boolean status; + MobileDevice mobileDevice = MobileDeviceManagementUtil.convertToMobileDevice(device); + try { + if (log.isDebugEnabled()) { + log.debug("Modifying the Android device enrollment data"); + } + AndroidDAOFactory.beginTransaction(); + status = mobileDeviceManagementDAOFactory.getMobileDeviceDAO() + .updateMobileDevice(mobileDevice); + AndroidDAOFactory.commitTransaction(); + } catch (MobileDeviceManagementDAOException e) { + try { + AndroidDAOFactory.rollbackTransaction(); + } catch (MobileDeviceManagementDAOException mobileDAOEx) { + String msg = "Error occurred while roll back the update device transaction :" + + device.toString(); + log.warn(msg, mobileDAOEx); + } + String msg = "Error while updating the enrollment of the Android device : " + + device.getDeviceIdentifier(); + log.error(msg, e); + throw new DeviceManagementException(msg, e); + } + return status; + } - @Override - public boolean isActive(DeviceIdentifier deviceId) throws DeviceManagementException { - return true; - } + @Override + public boolean disenrollDevice(DeviceIdentifier deviceId) throws DeviceManagementException { + boolean status; + try { + if (log.isDebugEnabled()) { + log.debug("Dis-enrolling Android device : " + deviceId); + } + AndroidDAOFactory.beginTransaction(); + status = mobileDeviceManagementDAOFactory.getMobileDeviceDAO() + .deleteMobileDevice(deviceId.getId()); + AndroidDAOFactory.commitTransaction(); + } catch (MobileDeviceManagementDAOException e) { + try { + AndroidDAOFactory.rollbackTransaction(); + } catch (MobileDeviceManagementDAOException mobileDAOEx) { + String msg = "Error occurred while roll back the device dis enrol transaction :" + + deviceId.toString(); + log.warn(msg, mobileDAOEx); + } + String msg = "Error while removing the Android device : " + deviceId.getId(); + log.error(msg, e); + throw new DeviceManagementException(msg, e); + } + return status; + } - @Override - public boolean setActive(DeviceIdentifier deviceId, boolean status) - throws DeviceManagementException { - return true; - } + @Override + public boolean isEnrolled(DeviceIdentifier deviceId) throws DeviceManagementException { + boolean isEnrolled = false; + try { + if (log.isDebugEnabled()) { + log.debug("Checking the enrollment of Android device : " + deviceId.getId()); + } + MobileDevice mobileDevice = + mobileDeviceManagementDAOFactory.getMobileDeviceDAO().getMobileDevice( + deviceId.getId()); + if (mobileDevice != null) { + isEnrolled = true; + } + } catch (MobileDeviceManagementDAOException 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; + } - @Override - public Device getDevice(DeviceIdentifier deviceId) throws DeviceManagementException { - Device device; - try { - if (log.isDebugEnabled()) { - log.debug("Getting the details of Android device : " + deviceId.getId()); - } - MobileDevice mobileDevice = mobileDeviceManagementDAOFactory.getMobileDeviceDAO(). - getMobileDevice(deviceId.getId()); - device = MobileDeviceManagementUtil.convertToDevice(mobileDevice); - } catch (MobileDeviceManagementDAOException e) { - String msg = "Error while fetching the Android device : " + deviceId.getId(); - log.error(msg, e); - throw new DeviceManagementException(msg, e); - } - return device; - } + @Override + public boolean isActive(DeviceIdentifier deviceId) throws DeviceManagementException { + return true; + } - @Override - public boolean setOwnership(DeviceIdentifier deviceId, String ownershipType) - throws DeviceManagementException { - return true; - } + @Override + public boolean setActive(DeviceIdentifier deviceId, boolean status) + throws DeviceManagementException { + return true; + } - @Override - public boolean isClaimable(DeviceIdentifier deviceIdentifier) throws DeviceManagementException { - return false; - } + @Override + public Device getDevice(DeviceIdentifier deviceId) throws DeviceManagementException { + Device device; + try { + if (log.isDebugEnabled()) { + log.debug("Getting the details of Android device : " + deviceId.getId()); + } + MobileDevice mobileDevice = mobileDeviceManagementDAOFactory.getMobileDeviceDAO(). + getMobileDevice(deviceId.getId()); + device = MobileDeviceManagementUtil.convertToDevice(mobileDevice); + } catch (MobileDeviceManagementDAOException e) { + String msg = "Error while fetching the Android device : " + deviceId.getId(); + log.error(msg, e); + throw new DeviceManagementException(msg, e); + } + return device; + } - @Override - public boolean setStatus(DeviceIdentifier deviceIdentifier, String currentUser, - EnrolmentInfo.Status status) throws DeviceManagementException { - return false; - } + @Override + public boolean setOwnership(DeviceIdentifier deviceId, String ownershipType) + throws DeviceManagementException { + return true; + } - @Override - public boolean updateDeviceInfo(DeviceIdentifier deviceIdentifier, Device device) throws DeviceManagementException { - boolean status; - Device deviceDB = this.getDevice(deviceIdentifier); - // This object holds the current persisted device object - MobileDevice mobileDeviceDB = MobileDeviceManagementUtil.convertToMobileDevice(deviceDB); + @Override + public boolean isClaimable(DeviceIdentifier deviceIdentifier) throws DeviceManagementException { + return false; + } - // This object holds the newly received device object from response - MobileDevice mobileDevice = MobileDeviceManagementUtil.convertToMobileDevice(device); + @Override + public boolean setStatus(DeviceIdentifier deviceIdentifier, String currentUser, + EnrolmentInfo.Status status) throws DeviceManagementException { + return false; + } - // Updating current object features using newer ones - mobileDeviceDB.setLatitude(mobileDevice.getLatitude()); - mobileDeviceDB.setLongitude(mobileDevice.getLongitude()); - mobileDeviceDB.setDeviceProperties(mobileDevice.getDeviceProperties()); + @Override + public boolean updateDeviceInfo(DeviceIdentifier deviceIdentifier, Device device) + throws DeviceManagementException { + boolean status; + Device deviceDB = this.getDevice(deviceIdentifier); + // This object holds the current persisted device object + MobileDevice mobileDeviceDB = MobileDeviceManagementUtil.convertToMobileDevice(deviceDB); - try { - if (log.isDebugEnabled()) { - log.debug( - "updating the details of Android device : " + device.getDeviceIdentifier()); - } - AndroidDAOFactory.beginTransaction(); - status = mobileDeviceManagementDAOFactory.getMobileDeviceDAO() - .updateMobileDevice(mobileDeviceDB); - AndroidDAOFactory.commitTransaction(); - } catch (MobileDeviceManagementDAOException e) { - try { - AndroidDAOFactory.rollbackTransaction(); - } catch (MobileDeviceManagementDAOException mobileDAOEx) { - String msg = "Error occurred while roll back the update device info transaction :" + device.toString(); - log.warn(msg, mobileDAOEx); - } - String msg = - "Error while updating the Android device : " + device.getDeviceIdentifier(); - log.error(msg, e); - throw new DeviceManagementException(msg, e); - } - return status; - } + // This object holds the newly received device object from response + MobileDevice mobileDevice = MobileDeviceManagementUtil.convertToMobileDevice(device); - @Override - public List getAllDevices() throws DeviceManagementException { - List devices = null; - try { - if (log.isDebugEnabled()) { - log.debug("Fetching the details of all Android devices"); - } - List mobileDevices = - mobileDeviceManagementDAOFactory.getMobileDeviceDAO(). - getAllMobileDevices(); - if (mobileDevices != null) { - devices = new ArrayList(); - for (MobileDevice mobileDevice : mobileDevices) { - devices.add(MobileDeviceManagementUtil.convertToDevice(mobileDevice)); - } - } - } catch (MobileDeviceManagementDAOException e) { - String msg = "Error while fetching all Android devices."; - log.error(msg, e); - throw new DeviceManagementException(msg, e); - } - return devices; - } + // Updating current object features using newer ones + mobileDeviceDB.setLatitude(mobileDevice.getLatitude()); + mobileDeviceDB.setLongitude(mobileDevice.getLongitude()); + mobileDeviceDB.setDeviceProperties(mobileDevice.getDeviceProperties()); + + try { + if (log.isDebugEnabled()) { + log.debug( + "updating the details of Android device : " + device.getDeviceIdentifier()); + } + AndroidDAOFactory.beginTransaction(); + status = mobileDeviceManagementDAOFactory.getMobileDeviceDAO() + .updateMobileDevice(mobileDeviceDB); + AndroidDAOFactory.commitTransaction(); + } catch (MobileDeviceManagementDAOException e) { + try { + AndroidDAOFactory.rollbackTransaction(); + } catch (MobileDeviceManagementDAOException mobileDAOEx) { + String msg = "Error occurred while roll back the update device info transaction :" + + device.toString(); + log.warn(msg, mobileDAOEx); + } + String msg = + "Error while updating the Android device : " + device.getDeviceIdentifier(); + log.error(msg, e); + throw new DeviceManagementException(msg, e); + } + return status; + } + + @Override + public List getAllDevices() throws DeviceManagementException { + List devices = null; + try { + if (log.isDebugEnabled()) { + log.debug("Fetching the details of all Android devices"); + } + List mobileDevices = + mobileDeviceManagementDAOFactory.getMobileDeviceDAO(). + getAllMobileDevices(); + if (mobileDevices != null) { + devices = new ArrayList(); + for (MobileDevice mobileDevice : mobileDevices) { + devices.add(MobileDeviceManagementUtil.convertToDevice(mobileDevice)); + } + } + } catch (MobileDeviceManagementDAOException e) { + String msg = "Error while fetching all Android devices."; + log.error(msg, e); + throw new DeviceManagementException(msg, e); + } + return devices; + } } 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 b8f07199ed..467c5fa5ba 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 @@ -21,6 +21,8 @@ package org.wso2.carbon.device.mgt.mobile.impl.windows; 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.mobile.dao.MobileDeviceManagementDAOException; import org.wso2.carbon.device.mgt.mobile.dao.MobileDeviceManagementDAOFactory; import org.wso2.carbon.device.mgt.mobile.dto.MobileDevice; @@ -28,6 +30,7 @@ import org.wso2.carbon.device.mgt.mobile.impl.windows.dao.WindowsDAOFactory; import org.wso2.carbon.device.mgt.mobile.util.MobileDeviceManagementUtil; import java.util.List; +import java.util.Map; public class WindowsDeviceManager implements DeviceManager { @@ -43,6 +46,17 @@ public class WindowsDeviceManager implements DeviceManager { return null; } + @Override + public boolean saveConfiguration(TenantConfiguration tenantConfiguration) + throws DeviceManagementException { + return false; + } + + @Override + public TenantConfiguration getConfiguration() throws DeviceManagementException { + return null; + } + @Override public boolean modifyEnrollment(Device device) throws DeviceManagementException { return true; diff --git a/components/device-mgt/org.wso2.carbon.device.mgt.mobile.impl/src/main/java/org/wso2/carbon/device/mgt/mobile/internal/MobileDeviceManagementServiceComponent.java b/components/device-mgt/org.wso2.carbon.device.mgt.mobile.impl/src/main/java/org/wso2/carbon/device/mgt/mobile/internal/MobileDeviceManagementServiceComponent.java index 0be1c46624..1951d1c0e1 100644 --- a/components/device-mgt/org.wso2.carbon.device.mgt.mobile.impl/src/main/java/org/wso2/carbon/device/mgt/mobile/internal/MobileDeviceManagementServiceComponent.java +++ b/components/device-mgt/org.wso2.carbon.device.mgt.mobile.impl/src/main/java/org/wso2/carbon/device/mgt/mobile/internal/MobileDeviceManagementServiceComponent.java @@ -36,6 +36,7 @@ import org.wso2.carbon.device.mgt.mobile.impl.windows.WindowsDeviceManagementSer import org.wso2.carbon.device.mgt.mobile.impl.windows.WindowsPolicyMonitoringService; import org.wso2.carbon.ndatasource.core.DataSourceService; import org.wso2.carbon.policy.mgt.common.spi.PolicyMonitoringService; +import org.wso2.carbon.registry.core.service.RegistryService; import java.util.Map; @@ -48,6 +49,9 @@ import java.util.Map; * policy="dynamic" * bind="setDataSourceService" * unbind="unsetDataSourceService" + * @scr.reference name="registry.service" + * interface="org.wso2.carbon.registry.core.service.RegistryService" cardinality="0..1" + * policy="dynamic" bind="setRegistryService" unbind="unsetRegistryService" *

* Adding reference to API Manager Configuration service is an unavoidable hack to get rid of NPEs thrown while * initializing APIMgtDAOs attempting to register APIs programmatically. APIMgtDAO needs to be proper cleaned up @@ -149,4 +153,15 @@ public class MobileDeviceManagementServiceComponent { //do nothing } + protected void setRegistryService(RegistryService registryService) { + if (log.isDebugEnabled()) { + log.debug("RegistryService acquired"); + } + MobileDeviceManagementServiceDataHolder.getInstance().setRegistryService(registryService); + } + + protected void unsetRegistryService(RegistryService registryService) { + MobileDeviceManagementServiceDataHolder.getInstance().setRegistryService(null); + } + } diff --git a/components/device-mgt/org.wso2.carbon.device.mgt.mobile.impl/src/main/java/org/wso2/carbon/device/mgt/mobile/internal/MobileDeviceManagementServiceDataHolder.java b/components/device-mgt/org.wso2.carbon.device.mgt.mobile.impl/src/main/java/org/wso2/carbon/device/mgt/mobile/internal/MobileDeviceManagementServiceDataHolder.java new file mode 100644 index 0000000000..4ad231ebf3 --- /dev/null +++ b/components/device-mgt/org.wso2.carbon.device.mgt.mobile.impl/src/main/java/org/wso2/carbon/device/mgt/mobile/internal/MobileDeviceManagementServiceDataHolder.java @@ -0,0 +1,47 @@ +/* + * 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.internal; + +import org.wso2.carbon.registry.core.service.RegistryService; + +/** + * DataHolder class of Mobile plugins component. + */ +public class MobileDeviceManagementServiceDataHolder { + + private RegistryService registryService; + + private static MobileDeviceManagementServiceDataHolder thisInstance = new MobileDeviceManagementServiceDataHolder(); + + private MobileDeviceManagementServiceDataHolder() { + } + + public static MobileDeviceManagementServiceDataHolder getInstance() { + return thisInstance; + } + + public RegistryService getRegistryService() { + return registryService; + } + + public void setRegistryService(RegistryService registryService) { + this.registryService = registryService; + } + +} 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 5d7f8b35f3..82d8b68058 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 @@ -21,11 +21,19 @@ package org.wso2.carbon.device.mgt.mobile.util; import org.apache.commons.logging.Log; import org.apache.commons.logging.LogFactory; import org.w3c.dom.Document; +import org.wso2.carbon.context.PrivilegedCarbonContext; import org.wso2.carbon.device.mgt.common.Device; +import org.wso2.carbon.device.mgt.common.DeviceManagementConstants; import org.wso2.carbon.device.mgt.common.DeviceManagementException; import org.wso2.carbon.device.mgt.common.Feature; import org.wso2.carbon.device.mgt.common.operation.mgt.Operation; +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.MobileDeviceManagementServiceDataHolder; +import org.wso2.carbon.registry.api.RegistryException; +import org.wso2.carbon.registry.api.Resource; +import org.wso2.carbon.registry.core.Registry; import javax.xml.parsers.DocumentBuilder; import javax.xml.parsers.DocumentBuilderFactory; @@ -54,8 +62,9 @@ public class MobileDeviceManagementUtil { DocumentBuilder docBuilder = factory.newDocumentBuilder(); return docBuilder.parse(file); } catch (Exception e) { - throw new DeviceManagementException("Error occurred while parsing file, while converting " + - "to a org.w3c.dom.Document : " + e.getMessage(), e); + throw new DeviceManagementException( + "Error occurred while parsing file, while converting " + + "to a org.w3c.dom.Document : " + e.getMessage(), e); } } @@ -91,16 +100,16 @@ public class MobileDeviceManagementUtil { mobileDevice.setLatitude(getPropertyValue(device, MOBILE_DEVICE_LATITUDE)); mobileDevice.setLongitude(getPropertyValue(device, MOBILE_DEVICE_LONGITUDE)); - if (device.getProperties() != null) { - Map deviceProperties = new HashMap(); - for (Device.Property deviceProperty : device.getProperties()) { - deviceProperties.put(deviceProperty.getName(), deviceProperty.getValue()); - } + if (device.getProperties() != null) { + Map deviceProperties = new HashMap(); + for (Device.Property deviceProperty : device.getProperties()) { + deviceProperties.put(deviceProperty.getName(), deviceProperty.getValue()); + } - mobileDevice.setDeviceProperties(deviceProperties); - } else { - mobileDevice.setDeviceProperties(new HashMap()); - } + mobileDevice.setDeviceProperties(deviceProperties); + } else { + mobileDevice.setDeviceProperties(new HashMap()); + } } return mobileDevice; } @@ -119,13 +128,15 @@ public class MobileDeviceManagementUtil { propertyList.add(getProperty(MOBILE_DEVICE_LONGITUDE, mobileDevice.getLongitude())); propertyList.add(getProperty(MOBILE_DEVICE_SERIAL, mobileDevice.getSerial())); - if (mobileDevice.getDeviceProperties() != null) { - for (Map.Entry deviceProperty : mobileDevice.getDeviceProperties().entrySet()) { - propertyList.add(getProperty(deviceProperty.getKey(), deviceProperty.getValue())); - } - } + if (mobileDevice.getDeviceProperties() != null) { + for (Map.Entry deviceProperty : mobileDevice.getDeviceProperties() + .entrySet()) { + propertyList + .add(getProperty(deviceProperty.getKey(), deviceProperty.getValue())); + } + } - device.setProperties(propertyList); + device.setProperties(propertyList); device.setDeviceIdentifier(mobileDevice.getMobileDeviceId()); } return device; @@ -162,27 +173,121 @@ public class MobileDeviceManagementUtil { Properties properties = new Properties(); operation.setCode(mobileOperation.getFeatureCode()); for (MobileOperationProperty mobileOperationProperty : mobileOperation.getProperties()) { - properties.put(mobileOperationProperty.getProperty(), mobileOperationProperty.getValue()); + properties + .put(mobileOperationProperty.getProperty(), mobileOperationProperty.getValue()); } operation.setProperties(properties); return operation; } - public static MobileFeature convertToMobileFeature(Feature feature) { - MobileFeature mobileFeature = new MobileFeature(); - mobileFeature.setName(feature.getName()); - mobileFeature.setCode(feature.getCode()); - mobileFeature.setDescription(feature.getDescription()); - mobileFeature.setDeviceType(feature.getDeviceType()); - return mobileFeature; - } - - public static Feature convertToFeature(MobileFeature mobileFeature) { - Feature feature = new Feature(); - feature.setDescription(mobileFeature.getDescription()); - feature.setDeviceType(mobileFeature.getDeviceType()); - feature.setCode(mobileFeature.getCode()); - feature.setName(mobileFeature.getName()); - return feature; - } + public static MobileFeature convertToMobileFeature(Feature feature) { + MobileFeature mobileFeature = new MobileFeature(); + mobileFeature.setName(feature.getName()); + mobileFeature.setCode(feature.getCode()); + mobileFeature.setDescription(feature.getDescription()); + mobileFeature.setDeviceType(feature.getDeviceType()); + return mobileFeature; + } + + public static Feature convertToFeature(MobileFeature mobileFeature) { + Feature feature = new Feature(); + feature.setDescription(mobileFeature.getDescription()); + feature.setDeviceType(mobileFeature.getDeviceType()); + feature.setCode(mobileFeature.getCode()); + feature.setName(mobileFeature.getName()); + return feature; + } + + public static Registry getRegistry() throws MobileDeviceMgtPluginException { + try { + int tenantId = PrivilegedCarbonContext.getThreadLocalCarbonContext().getTenantId(); + return MobileDeviceManagementServiceDataHolder.getInstance().getRegistryService() + .getConfigSystemRegistry( + tenantId); + } catch (RegistryException e) { + throw new MobileDeviceMgtPluginException( + "Error in retrieving conf registry instance: " + + e.getMessage(), e); + } + } + + public static Resource getRegistryResource(String path) throws MobileDeviceMgtPluginException { + try { + return MobileDeviceManagementUtil.getRegistry().get(path); + + } catch (RegistryException e) { + throw new MobileDeviceMgtPluginException("Error in retrieving registry resource : " + + e.getMessage(), e); + } + } + + public static boolean putRegistryResource(String path, + Resource resource) + throws MobileDeviceMgtPluginException { + boolean status = false; + try { + MobileDeviceManagementUtil.getRegistry().beginTransaction(); + MobileDeviceManagementUtil.getRegistry().put(path, resource); + MobileDeviceManagementUtil.getRegistry().commitTransaction(); + status = true; + } catch (RegistryException e) { + throw new MobileDeviceMgtPluginException( + "Error occurred while persisting registry resource : " + + e.getMessage(), e); + } + return status; + } + + public static String getResourcePath(String resourceName, String platform) { + String regPath = ""; + switch (platform) { + case DeviceManagementConstants.MobileDeviceTypes.MOBILE_DEVICE_TYPE_ANDROID: + regPath = MobilePluginConstants.MOBILE_CONFIG_REGISTRY_ROOT + "/" + + DeviceManagementConstants.MobileDeviceTypes.MOBILE_DEVICE_TYPE_ANDROID + + "/" + resourceName; + break; + case DeviceManagementConstants.MobileDeviceTypes.MOBILE_DEVICE_TYPE_WINDOWS: + regPath = MobilePluginConstants.MOBILE_CONFIG_REGISTRY_ROOT + "/" + + DeviceManagementConstants.MobileDeviceTypes.MOBILE_DEVICE_TYPE_WINDOWS + + "/" + resourceName; + break; + } + return regPath; + } + + public static String getPlatformConfigPath(String platform) { + String regPath = ""; + switch (platform) { + case DeviceManagementConstants.MobileDeviceTypes.MOBILE_DEVICE_TYPE_ANDROID: + regPath = MobilePluginConstants.MOBILE_CONFIG_REGISTRY_ROOT + "/" + + 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; + break; + } + return regPath; + } + + 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(); + } + return true; + } catch (MobileDeviceMgtPluginException e) { + throw new MobileDeviceMgtPluginException( + "Error occurred while creating a registry collection : " + + e.getMessage(), e); + } catch (RegistryException e) { + throw new MobileDeviceMgtPluginException( + "Error occurred while creating a registry collection : " + + e.getMessage(), e); + } + } } diff --git a/pom.xml b/pom.xml index 0e07aa8d30..1bb9c0edb1 100644 --- a/pom.xml +++ b/pom.xml @@ -419,6 +419,72 @@ + + + org.wso2.carbon + org.wso2.carbon.registry.api + ${carbon.kernel.version} + + + org.wso2.carbon + org.wso2.carbon.registry.core + ${carbon.kernel.version} + + + commons-io.wso2 + commons-io + + + net.sourceforge.findbugs + annotations + + + org.eclipse.osgi + org.eclipse.osgi.services + + + org.apache.ws.commons.schema.wso2 + XmlSchema + + + org.wso2.carbon + javax.cache.wso2 + + + org.wso2.carbon + org.wso2.carbon.registry.xboot + + + org.wso2.securevault + org.wso2.securevault + + + org.compass-project.wso2 + compass + + + org.apache.abdera.wso2 + abdera + + + org.wso2.carbon + org.wso2.carbon.ndatasource.rdbms + + + org.apache.poi.wso2 + poi-scratchpad + + + commons-httpclient.wso2 + commons-httpclient + + + commons-pool.wso2 + commons-pool + + + + @@ -521,8 +587,8 @@ 2.3.1 UTF-8 - 1.6 - 1.6 + 1.7 + 1.7