Merge pull request #740 from milanperera/iot-master

Added a feature to pick default platform configurations
revert-70aa11f8
Geeth 8 years ago committed by GitHub
commit 4c3b342789

@ -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 {

@ -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);
}
}
}

@ -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;
}

@ -87,7 +87,6 @@
<li><a href="{{@app.context}}/policies"><i class="fw fw-policy"></i>Policy Management</a></li>
{{/if}}
{{#unless isCloud}}
{{#if permissions.TENANT_CONFIGURATION}}
<li><a><i class="fw fw-settings"></i>Configuration Management</a>
<ul>
@ -99,7 +98,6 @@
</ul>
</li>
{{/if}}
{{/unless}}

Loading…
Cancel
Save