|
|
|
@ -20,6 +20,7 @@ package org.wso2.carbon.device.mgt.core.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.DeviceManagementException;
|
|
|
|
|
import org.wso2.carbon.device.mgt.core.config.datasource.DataSourceConfig;
|
|
|
|
@ -39,103 +40,111 @@ import java.util.Hashtable;
|
|
|
|
|
import java.util.List;
|
|
|
|
|
import java.util.Map;
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
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 {
|
|
|
|
|
DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();
|
|
|
|
|
factory.setNamespaceAware(true);
|
|
|
|
|
try {
|
|
|
|
|
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);
|
|
|
|
|
}
|
|
|
|
|
public static Document convertToDocument(File file) throws DeviceManagementException {
|
|
|
|
|
DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();
|
|
|
|
|
factory.setNamespaceAware(true);
|
|
|
|
|
try {
|
|
|
|
|
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);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* Resolve data source from the data source definition.
|
|
|
|
|
*
|
|
|
|
|
* @param config data source configuration
|
|
|
|
|
* @return data source resolved from the data source definition
|
|
|
|
|
*/
|
|
|
|
|
public static DataSource resolveDataSource(DataSourceConfig config) {
|
|
|
|
|
DataSource dataSource = null;
|
|
|
|
|
if (config == null) {
|
|
|
|
|
throw new RuntimeException("Device Management Repository data source configuration is null and thus, " +
|
|
|
|
|
/**
|
|
|
|
|
* Resolve data source from the data source definition.
|
|
|
|
|
*
|
|
|
|
|
* @param config data source configuration
|
|
|
|
|
* @return data source resolved from the data source definition
|
|
|
|
|
*/
|
|
|
|
|
public static DataSource resolveDataSource(DataSourceConfig config) {
|
|
|
|
|
DataSource dataSource = null;
|
|
|
|
|
if (config == null) {
|
|
|
|
|
throw new RuntimeException("Device Management Repository data source configuration is null and thus, " +
|
|
|
|
|
"is not initialized");
|
|
|
|
|
}
|
|
|
|
|
JNDILookupDefinition jndiConfig = config.getJndiLookupDefinition();
|
|
|
|
|
if (jndiConfig != null) {
|
|
|
|
|
if (log.isDebugEnabled()) {
|
|
|
|
|
log.debug("Initializing Device Management Repository data source using the JNDI Lookup Definition");
|
|
|
|
|
}
|
|
|
|
|
List<JNDILookupDefinition.JNDIProperty> jndiPropertyList =
|
|
|
|
|
jndiConfig.getJndiProperties();
|
|
|
|
|
if (jndiPropertyList != null) {
|
|
|
|
|
Hashtable<Object, Object> jndiProperties = new Hashtable<Object, Object>();
|
|
|
|
|
for (JNDILookupDefinition.JNDIProperty prop : jndiPropertyList) {
|
|
|
|
|
jndiProperties.put(prop.getName(), prop.getValue());
|
|
|
|
|
}
|
|
|
|
|
dataSource = DeviceManagementDAOUtil.lookupDataSource(jndiConfig.getJndiName(), jndiProperties);
|
|
|
|
|
} else {
|
|
|
|
|
dataSource = DeviceManagementDAOUtil.lookupDataSource(jndiConfig.getJndiName(), null);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
return dataSource;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
JNDILookupDefinition jndiConfig = config.getJndiLookupDefinition();
|
|
|
|
|
if (jndiConfig != null) {
|
|
|
|
|
if (log.isDebugEnabled()) {
|
|
|
|
|
log.debug("Initializing Device Management Repository data source using the JNDI Lookup Definition");
|
|
|
|
|
}
|
|
|
|
|
List<JNDILookupDefinition.JNDIProperty> jndiPropertyList =
|
|
|
|
|
jndiConfig.getJndiProperties();
|
|
|
|
|
if (jndiPropertyList != null) {
|
|
|
|
|
Hashtable<Object, Object> jndiProperties = new Hashtable<Object, Object>();
|
|
|
|
|
for (JNDILookupDefinition.JNDIProperty prop : jndiPropertyList) {
|
|
|
|
|
jndiProperties.put(prop.getName(), prop.getValue());
|
|
|
|
|
}
|
|
|
|
|
dataSource = DeviceManagementDAOUtil.lookupDataSource(jndiConfig.getJndiName(), jndiProperties);
|
|
|
|
|
} else {
|
|
|
|
|
dataSource = DeviceManagementDAOUtil.lookupDataSource(jndiConfig.getJndiName(), null);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
return dataSource;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* Adds a new device type to the database if it does not exists.
|
|
|
|
|
*
|
|
|
|
|
* @param deviceType device type
|
|
|
|
|
* @return status of the operation
|
|
|
|
|
*/
|
|
|
|
|
public static boolean registerDeviceType(String deviceType) throws DeviceManagementException {
|
|
|
|
|
boolean status;
|
|
|
|
|
try {
|
|
|
|
|
DeviceTypeDAO deviceTypeDAO = DeviceManagementDAOFactory.getDeviceTypeDAO();
|
|
|
|
|
DeviceType deviceTypeId = deviceTypeDAO.getDeviceType(deviceType);
|
|
|
|
|
if (deviceTypeId == null) {
|
|
|
|
|
DeviceType dt = new DeviceType();
|
|
|
|
|
dt.setName(deviceType);
|
|
|
|
|
deviceTypeDAO.addDeviceType(dt);
|
|
|
|
|
}
|
|
|
|
|
status = true;
|
|
|
|
|
} catch (DeviceManagementDAOException e) {
|
|
|
|
|
throw new DeviceManagementException("Error occurred while registering the device type '" +
|
|
|
|
|
/**
|
|
|
|
|
* Adds a new device type to the database if it does not exists.
|
|
|
|
|
*
|
|
|
|
|
* @param deviceType device type
|
|
|
|
|
* @return status of the operation
|
|
|
|
|
*/
|
|
|
|
|
public static boolean registerDeviceType(String deviceType) throws DeviceManagementException {
|
|
|
|
|
boolean status;
|
|
|
|
|
try {
|
|
|
|
|
DeviceTypeDAO deviceTypeDAO = DeviceManagementDAOFactory.getDeviceTypeDAO();
|
|
|
|
|
DeviceType deviceTypeId = deviceTypeDAO.getDeviceType(deviceType);
|
|
|
|
|
if (deviceTypeId == null) {
|
|
|
|
|
DeviceType dt = new DeviceType();
|
|
|
|
|
dt.setName(deviceType);
|
|
|
|
|
deviceTypeDAO.addDeviceType(dt);
|
|
|
|
|
}
|
|
|
|
|
status = true;
|
|
|
|
|
} catch (DeviceManagementDAOException e) {
|
|
|
|
|
throw new DeviceManagementException("Error occurred while registering the device type '" +
|
|
|
|
|
deviceType + "'", e);
|
|
|
|
|
}
|
|
|
|
|
return status;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
return status;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* Un-registers an existing device type from the device management metadata repository.
|
|
|
|
|
*
|
|
|
|
|
* @param deviceType device type
|
|
|
|
|
* @return status of the operation
|
|
|
|
|
*/
|
|
|
|
|
public static boolean unregisterDeviceType(String deviceType) throws DeviceManagementException {
|
|
|
|
|
try {
|
|
|
|
|
DeviceTypeDAO deviceTypeDAO = DeviceManagementDAOFactory.getDeviceTypeDAO();
|
|
|
|
|
DeviceType deviceTypeId = deviceTypeDAO.getDeviceType(deviceType);
|
|
|
|
|
if (deviceTypeId == null) {
|
|
|
|
|
DeviceType dt = new DeviceType();
|
|
|
|
|
dt.setName(deviceType);
|
|
|
|
|
deviceTypeDAO.removeDeviceType(deviceType);
|
|
|
|
|
}
|
|
|
|
|
return true;
|
|
|
|
|
} catch (DeviceManagementDAOException e) {
|
|
|
|
|
throw new DeviceManagementException("Error occurred while registering the device type '" +
|
|
|
|
|
/**
|
|
|
|
|
* Un-registers an existing device type from the device management metadata repository.
|
|
|
|
|
*
|
|
|
|
|
* @param deviceType device type
|
|
|
|
|
* @return status of the operation
|
|
|
|
|
*/
|
|
|
|
|
public static boolean unregisterDeviceType(String deviceType) throws DeviceManagementException {
|
|
|
|
|
try {
|
|
|
|
|
DeviceTypeDAO deviceTypeDAO = DeviceManagementDAOFactory.getDeviceTypeDAO();
|
|
|
|
|
DeviceType deviceTypeId = deviceTypeDAO.getDeviceType(deviceType);
|
|
|
|
|
if (deviceTypeId == null) {
|
|
|
|
|
DeviceType dt = new DeviceType();
|
|
|
|
|
dt.setName(deviceType);
|
|
|
|
|
deviceTypeDAO.removeDeviceType(deviceType);
|
|
|
|
|
}
|
|
|
|
|
return true;
|
|
|
|
|
} catch (DeviceManagementDAOException e) {
|
|
|
|
|
throw new DeviceManagementException("Error occurred while registering the device type '" +
|
|
|
|
|
deviceType + "'", e);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public static void sendEmail(){
|
|
|
|
|
public static Map<String, String> convertDevicePropertiesToMap(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;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public static int getTenantId() {
|
|
|
|
|
PrivilegedCarbonContext ctx = PrivilegedCarbonContext.getThreadLocalCarbonContext();
|
|
|
|
|
return ctx.getTenantId();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
}
|
|
|
|
|