|
|
@ -18,6 +18,7 @@ package org.wso2.carbon.device.mgt.core.util;
|
|
|
|
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.w3c.dom.Document;
|
|
|
|
import org.w3c.dom.Document;
|
|
|
|
|
|
|
|
import org.wso2.carbon.device.mgt.common.Device;
|
|
|
|
import org.wso2.carbon.device.mgt.common.DeviceManagementException;
|
|
|
|
import org.wso2.carbon.device.mgt.common.DeviceManagementException;
|
|
|
|
import org.wso2.carbon.device.mgt.core.config.datasource.DataSourceConfig;
|
|
|
|
import org.wso2.carbon.device.mgt.core.config.datasource.DataSourceConfig;
|
|
|
|
import org.wso2.carbon.device.mgt.core.config.datasource.JNDILookupDefinition;
|
|
|
|
import org.wso2.carbon.device.mgt.core.config.datasource.JNDILookupDefinition;
|
|
|
@ -31,103 +32,111 @@ import javax.sql.DataSource;
|
|
|
|
import javax.xml.parsers.DocumentBuilder;
|
|
|
|
import javax.xml.parsers.DocumentBuilder;
|
|
|
|
import javax.xml.parsers.DocumentBuilderFactory;
|
|
|
|
import javax.xml.parsers.DocumentBuilderFactory;
|
|
|
|
import java.io.File;
|
|
|
|
import java.io.File;
|
|
|
|
|
|
|
|
import java.util.HashMap;
|
|
|
|
import java.util.Hashtable;
|
|
|
|
import java.util.Hashtable;
|
|
|
|
import java.util.List;
|
|
|
|
import java.util.List;
|
|
|
|
|
|
|
|
import java.util.Map;
|
|
|
|
|
|
|
|
|
|
|
|
public final class DeviceManagerUtil {
|
|
|
|
public final class DeviceManagerUtil {
|
|
|
|
|
|
|
|
|
|
|
|
private static final Log log = LogFactory.getLog(DeviceManagerUtil.class);
|
|
|
|
private static final Log log = LogFactory.getLog(DeviceManagerUtil.class);
|
|
|
|
|
|
|
|
|
|
|
|
public static Document convertToDocument(File file) throws DeviceManagementException {
|
|
|
|
public static Document convertToDocument(File file) throws DeviceManagementException {
|
|
|
|
DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();
|
|
|
|
DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();
|
|
|
|
factory.setNamespaceAware(true);
|
|
|
|
factory.setNamespaceAware(true);
|
|
|
|
try {
|
|
|
|
try {
|
|
|
|
DocumentBuilder docBuilder = factory.newDocumentBuilder();
|
|
|
|
DocumentBuilder docBuilder = factory.newDocumentBuilder();
|
|
|
|
return docBuilder.parse(file);
|
|
|
|
return docBuilder.parse(file);
|
|
|
|
} catch (Exception e) {
|
|
|
|
} catch (Exception e) {
|
|
|
|
throw new DeviceManagementException("Error occurred while parsing file, while converting " +
|
|
|
|
throw new DeviceManagementException("Error occurred while parsing file, while converting " +
|
|
|
|
"to a org.w3c.dom.Document : " + e.getMessage(), e);
|
|
|
|
"to a org.w3c.dom.Document : " + e.getMessage(), e);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
/**
|
|
|
|
* Resolve data source from the data source definition.
|
|
|
|
* Resolve data source from the data source definition.
|
|
|
|
*
|
|
|
|
*
|
|
|
|
* @param config data source configuration
|
|
|
|
* @param config data source configuration
|
|
|
|
* @return data source resolved from the data source definition
|
|
|
|
* @return data source resolved from the data source definition
|
|
|
|
*/
|
|
|
|
*/
|
|
|
|
public static DataSource resolveDataSource(DataSourceConfig config) {
|
|
|
|
public static DataSource resolveDataSource(DataSourceConfig config) {
|
|
|
|
DataSource dataSource = null;
|
|
|
|
DataSource dataSource = null;
|
|
|
|
if (config == null) {
|
|
|
|
if (config == null) {
|
|
|
|
throw new RuntimeException("Device Management Repository data source configuration " +
|
|
|
|
throw new RuntimeException(
|
|
|
|
"is null and thus, is not initialized");
|
|
|
|
"Device Management Repository data source configuration " + "is null and thus, is not initialized");
|
|
|
|
}
|
|
|
|
}
|
|
|
|
JNDILookupDefinition jndiConfig = config.getJndiLookupDefintion();
|
|
|
|
JNDILookupDefinition jndiConfig = config.getJndiLookupDefintion();
|
|
|
|
if (jndiConfig != null) {
|
|
|
|
if (jndiConfig != null) {
|
|
|
|
if (log.isDebugEnabled()) {
|
|
|
|
if (log.isDebugEnabled()) {
|
|
|
|
log.debug("Initializing Device Management Repository data source using the JNDI " +
|
|
|
|
log.debug(
|
|
|
|
"Lookup Definition");
|
|
|
|
"Initializing Device Management Repository data source using the JNDI " + "Lookup Definition");
|
|
|
|
}
|
|
|
|
}
|
|
|
|
List<JNDILookupDefinition.JNDIProperty> jndiPropertyList =
|
|
|
|
List<JNDILookupDefinition.JNDIProperty> jndiPropertyList = jndiConfig.getJndiProperties();
|
|
|
|
jndiConfig.getJndiProperties();
|
|
|
|
if (jndiPropertyList != null) {
|
|
|
|
if (jndiPropertyList != null) {
|
|
|
|
Hashtable<Object, Object> jndiProperties = new Hashtable<Object, Object>();
|
|
|
|
Hashtable<Object, Object> jndiProperties = new Hashtable<Object, Object>();
|
|
|
|
for (JNDILookupDefinition.JNDIProperty prop : jndiPropertyList) {
|
|
|
|
for (JNDILookupDefinition.JNDIProperty prop : jndiPropertyList) {
|
|
|
|
jndiProperties.put(prop.getName(), prop.getValue());
|
|
|
|
jndiProperties.put(prop.getName(), prop.getValue());
|
|
|
|
}
|
|
|
|
}
|
|
|
|
dataSource = DeviceManagementDAOUtil.lookupDataSource(jndiConfig.getJndiName(), jndiProperties);
|
|
|
|
dataSource =
|
|
|
|
} else {
|
|
|
|
DeviceManagementDAOUtil.lookupDataSource(jndiConfig.getJndiName(), jndiProperties);
|
|
|
|
dataSource = DeviceManagementDAOUtil.lookupDataSource(jndiConfig.getJndiName(), null);
|
|
|
|
} else {
|
|
|
|
}
|
|
|
|
dataSource = DeviceManagementDAOUtil.lookupDataSource(jndiConfig.getJndiName(), null);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return dataSource;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return dataSource;
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
/**
|
|
|
|
* Adds a new device type to the database if it does not exists.
|
|
|
|
* Adds a new device type to the database if it does not exists.
|
|
|
|
*
|
|
|
|
*
|
|
|
|
* @param deviceType device type
|
|
|
|
* @param deviceType device type
|
|
|
|
* @return status of the operation
|
|
|
|
* @return status of the operation
|
|
|
|
*/
|
|
|
|
*/
|
|
|
|
public static boolean registerDeviceType(String deviceType) throws DeviceManagementException {
|
|
|
|
public static boolean registerDeviceType(String deviceType) throws DeviceManagementException {
|
|
|
|
boolean status;
|
|
|
|
boolean status;
|
|
|
|
try {
|
|
|
|
try {
|
|
|
|
DeviceTypeDAO deviceTypeDAO = DeviceManagementDAOFactory.getDeviceTypeDAO();
|
|
|
|
DeviceTypeDAO deviceTypeDAO = DeviceManagementDAOFactory.getDeviceTypeDAO();
|
|
|
|
Integer deviceTypeId = deviceTypeDAO.getDeviceTypeIdByDeviceTypeName(deviceType);
|
|
|
|
Integer deviceTypeId = deviceTypeDAO.getDeviceTypeIdByDeviceTypeName(deviceType);
|
|
|
|
if (deviceTypeId == null) {
|
|
|
|
if (deviceTypeId == null) {
|
|
|
|
DeviceType dt = new DeviceType();
|
|
|
|
DeviceType dt = new DeviceType();
|
|
|
|
dt.setName(deviceType);
|
|
|
|
dt.setName(deviceType);
|
|
|
|
deviceTypeDAO.addDeviceType(dt);
|
|
|
|
deviceTypeDAO.addDeviceType(dt);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
status = true;
|
|
|
|
status = true;
|
|
|
|
} catch (DeviceManagementDAOException e) {
|
|
|
|
} catch (DeviceManagementDAOException e) {
|
|
|
|
String msg = "Error occurred while registering the device type " + deviceType;
|
|
|
|
String msg = "Error occurred while registering the device type " + deviceType;
|
|
|
|
throw new DeviceManagementException(msg, e);
|
|
|
|
throw new DeviceManagementException(msg, e);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return status;
|
|
|
|
return status;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
/**
|
|
|
|
* Unregisters an existing device type from the device management metadata repository.
|
|
|
|
* Unregisters an existing device type from the device management metadata repository.
|
|
|
|
*
|
|
|
|
*
|
|
|
|
* @param deviceType device type
|
|
|
|
* @param deviceType device type
|
|
|
|
* @return status of the operation
|
|
|
|
* @return status of the operation
|
|
|
|
*/
|
|
|
|
*/
|
|
|
|
public static boolean unregisterDeviceType(String deviceType) throws DeviceManagementException {
|
|
|
|
public static boolean unregisterDeviceType(String deviceType) throws DeviceManagementException {
|
|
|
|
try {
|
|
|
|
try {
|
|
|
|
DeviceTypeDAO deviceTypeDAO = DeviceManagementDAOFactory.getDeviceTypeDAO();
|
|
|
|
DeviceTypeDAO deviceTypeDAO = DeviceManagementDAOFactory.getDeviceTypeDAO();
|
|
|
|
Integer deviceTypeId = deviceTypeDAO.getDeviceTypeIdByDeviceTypeName(deviceType);
|
|
|
|
Integer deviceTypeId = deviceTypeDAO.getDeviceTypeIdByDeviceTypeName(deviceType);
|
|
|
|
if (deviceTypeId == null) {
|
|
|
|
if (deviceTypeId == null) {
|
|
|
|
DeviceType dt = new DeviceType();
|
|
|
|
DeviceType dt = new DeviceType();
|
|
|
|
dt.setName(deviceType);
|
|
|
|
dt.setName(deviceType);
|
|
|
|
deviceTypeDAO.removeDeviceType(dt);
|
|
|
|
deviceTypeDAO.removeDeviceType(dt);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return true;
|
|
|
|
return true;
|
|
|
|
} catch (DeviceManagementDAOException e) {
|
|
|
|
} catch (DeviceManagementDAOException e) {
|
|
|
|
String msg = "Error occurred while registering the device type " + deviceType;
|
|
|
|
String msg = "Error occurred while registering the device type " + deviceType;
|
|
|
|
throw new DeviceManagementException(msg, e);
|
|
|
|
throw new DeviceManagementException(msg, e);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
public static Map<String, String> convertPropertiesToMap(List<Device.Property> properties) {
|
|
|
|
|
|
|
|
Map<String, String> propertiesMap = new HashMap<String, String>();
|
|
|
|
|
|
|
|
for (Device.Property prop : properties) {
|
|
|
|
|
|
|
|
propertiesMap.put(prop.getName(), prop.getValue());
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
return propertiesMap;
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
}
|
|
|
|
}
|
|
|
|