From 17ecadb91346b0a839879a3e3ac44d1b5159d4f2 Mon Sep 17 00:00:00 2001 From: Milan Perera Date: Tue, 9 May 2017 06:28:03 +0530 Subject: [PATCH] Added a feature to pick default platform configurations --- .../deployer/template/DeviceTypeManager.java | 51 +++++++++++++++++++ .../template/util/DeviceTypeUtils.java | 19 +++++++ .../device/mgt/common/DeviceManager.java | 9 ++++ .../cdmf.unit.ui.navbar.nav-menu/nav-menu.hbs | 2 - 4 files changed, 79 insertions(+), 2 deletions(-) diff --git a/components/device-mgt-extensions/org.wso2.carbon.device.mgt.extensions.device.type.deployer/src/main/java/org/wso2/carbon/device/mgt/extensions/device/type/deployer/template/DeviceTypeManager.java b/components/device-mgt-extensions/org.wso2.carbon.device.mgt.extensions.device.type.deployer/src/main/java/org/wso2/carbon/device/mgt/extensions/device/type/deployer/template/DeviceTypeManager.java index 98c5ef2870..87795967f1 100644 --- a/components/device-mgt-extensions/org.wso2.carbon.device.mgt.extensions.device.type.deployer/src/main/java/org/wso2/carbon/device/mgt/extensions/device/type/deployer/template/DeviceTypeManager.java +++ b/components/device-mgt-extensions/org.wso2.carbon.device.mgt.extensions.device.type.deployer/src/main/java/org/wso2/carbon/device/mgt/extensions/device/type/deployer/template/DeviceTypeManager.java @@ -20,6 +20,7 @@ package org.wso2.carbon.device.mgt.extensions.device.type.deployer.template; 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.DeviceIdentifier; @@ -42,12 +43,14 @@ import org.wso2.carbon.device.mgt.extensions.device.type.deployer.template.util. import org.wso2.carbon.device.mgt.extensions.license.mgt.registry.RegistryBasedLicenseManager; import org.wso2.carbon.registry.api.RegistryException; import org.wso2.carbon.registry.api.Resource; +import org.wso2.carbon.utils.CarbonUtils; import org.wso2.carbon.utils.multitenancy.MultitenantConstants; import javax.xml.bind.JAXBContext; import javax.xml.bind.JAXBException; import javax.xml.bind.Marshaller; import javax.xml.bind.Unmarshaller; +import java.io.File; import java.io.StringReader; import java.io.StringWriter; import java.nio.charset.Charset; @@ -63,10 +66,15 @@ public class DeviceTypeManager implements DeviceManager { private String deviceType; private DeviceTypePluginDAOManager deviceTypePluginDAOManager; private LicenseManager licenseManager; + private PlatformConfiguration defaultPlatformConfiguration; private boolean propertiesExist; private boolean requiredDeviceTypeAuthorization; private boolean claimable; + private static final String PATH_MOBILE_PLUGIN_CONF_DIR = + CarbonUtils.getEtcCarbonConfigDirPath() + File.separator + "device-mgt-plugin-configs" + File.separator + + "mobile"; + private FeatureManager featureManager; public DeviceTypeManager(DeviceTypeConfigIdentifier deviceTypeConfigIdentifier, @@ -107,6 +115,14 @@ public class DeviceTypeManager implements DeviceManager { claimable = deviceTypeConfiguration.getClaimable().isEnabled(); } + // Loading default platform configuration + try { + defaultPlatformConfiguration = this.getDefaultConfiguration(); + } catch (DeviceManagementException e) { + String msg = "Error occurred while default platform configuration"; + throw new DeviceTypeDeployerFileException(msg, e); + } + DeviceDetails deviceDetails = deviceTypeConfiguration.getDeviceDetails(); if (deviceDetails != null) { @@ -205,6 +221,8 @@ public class DeviceTypeManager implements DeviceManager { return (PlatformConfiguration) unmarshaller.unmarshal( new StringReader(new String((byte[]) resource.getContent(), Charset. forName(DeviceTypePluginConstants.CHARSET_UTF8)))); + } else if (defaultPlatformConfiguration != null) { + return defaultPlatformConfiguration; } return null; } catch (DeviceTypeMgtPluginException e) { @@ -371,6 +389,39 @@ public class DeviceTypeManager implements DeviceManager { return requiredDeviceTypeAuthorization; } + @Override + public PlatformConfiguration getDefaultConfiguration() throws DeviceManagementException { + + if (log.isDebugEnabled()) { + log.debug("Loading default " + deviceType + " platform configuration from " + deviceType + + "-default-platform-configuration.xml"); + } + try { + String platformConfigurationPath = + PATH_MOBILE_PLUGIN_CONF_DIR + File.separator + deviceType + "-default-platform-configuration.xml"; + File platformConfig = new File(platformConfigurationPath); + + if (platformConfig.exists()) { + Document doc = DeviceTypeUtils.convertToDocument(platformConfig); + JAXBContext context = JAXBContext.newInstance(PlatformConfiguration.class); + Unmarshaller unmarshaller = context.createUnmarshaller(); + return (PlatformConfiguration) unmarshaller.unmarshal(doc); + } else { + log.warn(deviceType + "-default-platform-configuration.xml is not available, hence default " + + deviceType + "platform configuration cannot be loaded."); + } + return null; + } catch (JAXBException e) { + throw new DeviceManagementException( + "Error occurred while parsing the " + deviceType + " default platform configuration : " + e + .getMessage(), e); + } catch (DeviceTypeMgtPluginException e) { + throw new DeviceManagementException( + "Error occurred while parsing the " + deviceType + " default platform configuration : " + e + .getMessage(), e); + } + } + @Override public boolean updateDeviceInfo(DeviceIdentifier deviceIdentifier, Device device) throws DeviceManagementException { diff --git a/components/device-mgt-extensions/org.wso2.carbon.device.mgt.extensions.device.type.deployer/src/main/java/org/wso2/carbon/device/mgt/extensions/device/type/deployer/template/util/DeviceTypeUtils.java b/components/device-mgt-extensions/org.wso2.carbon.device.mgt.extensions.device.type.deployer/src/main/java/org/wso2/carbon/device/mgt/extensions/device/type/deployer/template/util/DeviceTypeUtils.java index 466b3edcfe..20417fa4be 100644 --- a/components/device-mgt-extensions/org.wso2.carbon.device.mgt.extensions.device.type.deployer/src/main/java/org/wso2/carbon/device/mgt/extensions/device/type/deployer/template/util/DeviceTypeUtils.java +++ b/components/device-mgt-extensions/org.wso2.carbon.device.mgt.extensions.device.type.deployer/src/main/java/org/wso2/carbon/device/mgt/extensions/device/type/deployer/template/util/DeviceTypeUtils.java @@ -20,6 +20,7 @@ package org.wso2.carbon.device.mgt.extensions.device.type.deployer.template.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.extensions.device.type.deployer.exception.DeviceTypeMgtPluginException; import org.wso2.carbon.device.mgt.extensions.device.type.deployer.internal.DeviceTypeManagementDataHolder; @@ -30,6 +31,10 @@ import org.wso2.carbon.registry.core.Registry; import javax.naming.Context; import javax.naming.InitialContext; import javax.sql.DataSource; +import javax.xml.XMLConstants; +import javax.xml.parsers.DocumentBuilder; +import javax.xml.parsers.DocumentBuilderFactory; +import java.io.File; import java.sql.Connection; import java.sql.PreparedStatement; import java.sql.ResultSet; @@ -128,4 +133,18 @@ public class DeviceTypeUtils { } } + public static Document convertToDocument(File file) throws DeviceTypeMgtPluginException { + DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance(); + factory.setNamespaceAware(true); + try { + factory.setFeature(XMLConstants.FEATURE_SECURE_PROCESSING, true); + DocumentBuilder docBuilder = factory.newDocumentBuilder(); + return docBuilder.parse(file); + } catch (Exception e) { + throw new DeviceTypeMgtPluginException("Error occurred while parsing file '" + file.getName() + "' to" + + " a org.w3c.dom.Document", e); + } + } + + } diff --git a/components/device-mgt/org.wso2.carbon.device.mgt.common/src/main/java/org/wso2/carbon/device/mgt/common/DeviceManager.java b/components/device-mgt/org.wso2.carbon.device.mgt.common/src/main/java/org/wso2/carbon/device/mgt/common/DeviceManager.java index 060865c726..3c97fa81ce 100644 --- a/components/device-mgt/org.wso2.carbon.device.mgt.common/src/main/java/org/wso2/carbon/device/mgt/common/DeviceManager.java +++ b/components/device-mgt/org.wso2.carbon.device.mgt.common/src/main/java/org/wso2/carbon/device/mgt/common/DeviceManager.java @@ -157,4 +157,13 @@ public interface DeviceManager { */ boolean requireDeviceAuthorization(); + /** + * This method returns the default configuration values which is stored in the configuration file + * rather than fetching from the registry. + * + * @return Returns Default PlatformConfiguration + * @throws DeviceManagementException + */ + PlatformConfiguration getDefaultConfiguration() throws DeviceManagementException; + } diff --git a/components/device-mgt/org.wso2.carbon.device.mgt.ui/src/main/resources/jaggeryapps/devicemgt/app/units/cdmf.unit.ui.navbar.nav-menu/nav-menu.hbs b/components/device-mgt/org.wso2.carbon.device.mgt.ui/src/main/resources/jaggeryapps/devicemgt/app/units/cdmf.unit.ui.navbar.nav-menu/nav-menu.hbs index 78aa84e001..f5416944dd 100644 --- a/components/device-mgt/org.wso2.carbon.device.mgt.ui/src/main/resources/jaggeryapps/devicemgt/app/units/cdmf.unit.ui.navbar.nav-menu/nav-menu.hbs +++ b/components/device-mgt/org.wso2.carbon.device.mgt.ui/src/main/resources/jaggeryapps/devicemgt/app/units/cdmf.unit.ui.navbar.nav-menu/nav-menu.hbs @@ -87,7 +87,6 @@
  • Policy Management
  • {{/if}} - {{#unless isCloud}} {{#if permissions.TENANT_CONFIGURATION}}
  • Configuration Management
  • {{/if}} - {{/unless}}