Adding operation and license management implementations

revert-70aa11f8
prabathabey 10 years ago
parent 8662eb36e4
commit 31b931537f

@ -19,9 +19,8 @@ package org.wso2.carbon.device.mgt.common;
public class DeviceIdentifier { public class DeviceIdentifier {
private String type;
private String id; private String id;
private String type;
public String getType() { public String getType() {
return type; return type;

@ -71,7 +71,10 @@
org.wso2.carbon.registry.core.exceptions, org.wso2.carbon.registry.core.exceptions,
org.wso2.carbon.registry.core.service, org.wso2.carbon.registry.core.service,
org.wso2.carbon.registry.core.session, org.wso2.carbon.registry.core.session,
org.w3c.dom org.w3c.dom,
org.wso2.carbon.governance.api.exception,
org.wso2.carbon.governance.api.generic,
org.wso2.carbon.governance.api.generic.dataobjects
</Import-Package> </Import-Package>
<Export-Package> <Export-Package>
!org.wso2.carbon.device.mgt.core.internal, !org.wso2.carbon.device.mgt.core.internal,
@ -152,6 +155,10 @@
<groupId>org.wso2.carbon</groupId> <groupId>org.wso2.carbon</groupId>
<artifactId>org.wso2.carbon.base</artifactId> <artifactId>org.wso2.carbon.base</artifactId>
</dependency> </dependency>
<dependency>
<groupId>org.wso2.carbon.governance</groupId>
<artifactId>org.wso2.carbon.governance.api</artifactId>
</dependency>
</dependencies> </dependencies>
</project> </project>

@ -20,7 +20,8 @@ package org.wso2.carbon.device.mgt.core;
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.wso2.carbon.device.mgt.common.DeviceManagementException; import org.wso2.carbon.device.mgt.common.DeviceManagementException;
import org.wso2.carbon.device.mgt.common.spi.DeviceManagerService; import org.wso2.carbon.device.mgt.common.spi.DeviceManager;
import org.wso2.carbon.device.mgt.core.dao.DeviceManagementDAOException;
import org.wso2.carbon.device.mgt.core.util.DeviceManagerUtil; import org.wso2.carbon.device.mgt.core.util.DeviceManagerUtil;
import java.util.HashMap; import java.util.HashMap;
@ -28,35 +29,36 @@ import java.util.Map;
public class DeviceManagementRepository { public class DeviceManagementRepository {
private static final Log log = LogFactory.getLog(DeviceManagerUtil.class); private Map<String, DeviceManager> providers;
private Map<String, DeviceManagerService> providers;
public DeviceManagementRepository() { public DeviceManagementRepository() {
providers = new HashMap<String, DeviceManagerService>(); providers = new HashMap<String, DeviceManager>();
} }
public void addDeviceManagementProvider(DeviceManagerService provider) { public void addDeviceManagementProvider(DeviceManager provider) throws DeviceManagementException {
String deviceType = provider.getProviderType(); String deviceType = provider.getProviderType();
try { try {
DeviceManagerUtil.registerDeviceType(deviceType); DeviceManagerUtil.registerDeviceType(deviceType);
} catch (DeviceManagementException e) { } catch (DeviceManagementException e) {
log.error("Exception occurred while registering the device type.", e); throw new DeviceManagementException("Error occurred while adding device management provider '" +
deviceType + "'");
} }
providers.put(deviceType, provider); providers.put(deviceType, provider);
} }
public void removeDeviceManagementProvider(DeviceManagerService provider) { public void removeDeviceManagementProvider(DeviceManager provider) throws DeviceManagementException {
String deviceType = provider.getProviderType(); String deviceType = provider.getProviderType();
try { try {
DeviceManagerUtil.unregisterDeviceType(deviceType); DeviceManagerUtil.unregisterDeviceType(deviceType);
} catch (DeviceManagementException e) { } catch (DeviceManagementException e) {
log.error("Exception occurred while registering the device type.", e); throw new DeviceManagementException("Error occurred while removing device management provider '" +
deviceType + "'", e);
} }
providers.remove(deviceType); providers.remove(deviceType);
} }
public DeviceManagerService getDeviceManagementProvider(String type) { public DeviceManager getDeviceManagementProvider(String type) {
return providers.get(type); return providers.get(type);
} }
} }

@ -20,6 +20,7 @@ package org.wso2.carbon.device.mgt.core;
import org.wso2.carbon.device.mgt.common.Device; import org.wso2.carbon.device.mgt.common.Device;
import org.wso2.carbon.device.mgt.common.DeviceIdentifier; import org.wso2.carbon.device.mgt.common.DeviceIdentifier;
import org.wso2.carbon.device.mgt.common.DeviceManagementException; import org.wso2.carbon.device.mgt.common.DeviceManagementException;
import org.wso2.carbon.device.mgt.common.spi.DeviceManager;
import org.wso2.carbon.device.mgt.core.config.license.License; import org.wso2.carbon.device.mgt.core.config.license.License;
import org.wso2.carbon.device.mgt.core.license.mgt.LicenseManager; import org.wso2.carbon.device.mgt.core.license.mgt.LicenseManager;
import org.wso2.carbon.device.mgt.core.operation.mgt.OperationManager; import org.wso2.carbon.device.mgt.core.operation.mgt.OperationManager;
@ -31,32 +32,8 @@ import java.util.List;
* Proxy class for all Device Management related operations that take the corresponding plugin type in * Proxy class for all Device Management related operations that take the corresponding plugin type in
* and resolve the appropriate plugin implementation * and resolve the appropriate plugin implementation
*/ */
public interface DeviceManagementService { public interface DeviceManagementService extends DeviceManager, LicenseManager, OperationManager {
boolean enrollDevice(Device device) throws DeviceManagementException;
boolean modifyEnrollment(Device device) throws DeviceManagementException;
boolean disenrollDevice(DeviceIdentifier deviceId) throws DeviceManagementException;
boolean isEnrolled(DeviceIdentifier deviceId) throws DeviceManagementException;
boolean isActive(DeviceIdentifier deviceId) throws DeviceManagementException;
boolean setActive(DeviceIdentifier deviceId, boolean status) throws DeviceManagementException;
List<Device> getAllDevices(String type) throws DeviceManagementException; List<Device> getAllDevices(String type) throws DeviceManagementException;
Device getDevice(DeviceIdentifier deviceId) throws DeviceManagementException;
boolean updateDeviceInfo(Device device) throws DeviceManagementException;
boolean setOwnership(DeviceIdentifier deviceId, String ownershipType) throws DeviceManagementException;
OperationManager getOperationManager(String type) throws DeviceManagementException;
License getLicense(String type) throws DeviceManagementException;
boolean addLicense(String type, License license) throws DeviceManagementException;
} }

@ -17,18 +17,9 @@
*/ */
package org.wso2.carbon.device.mgt.core; package org.wso2.carbon.device.mgt.core;
import org.apache.commons.logging.Log; import org.wso2.carbon.device.mgt.common.*;
import org.apache.commons.logging.LogFactory; import org.wso2.carbon.device.mgt.common.spi.DeviceManager;
import org.wso2.carbon.device.mgt.common.Device;
import org.wso2.carbon.device.mgt.common.DeviceIdentifier;
import org.wso2.carbon.device.mgt.common.DeviceManagementException;
import org.wso2.carbon.device.mgt.core.config.license.License; import org.wso2.carbon.device.mgt.core.config.license.License;
import org.wso2.carbon.device.mgt.core.license.mgt.LicenseManagementException;
import org.wso2.carbon.device.mgt.core.license.mgt.LicenseManager;
import org.wso2.carbon.device.mgt.core.license.mgt.LicenseManagerImpl;
import org.wso2.carbon.device.mgt.core.operation.mgt.OperationManager;
import org.wso2.carbon.device.mgt.common.spi.DeviceManagerService;
import org.wso2.carbon.device.mgt.core.config.DeviceManagementConfig;
import org.wso2.carbon.device.mgt.core.dao.DeviceDAO; import org.wso2.carbon.device.mgt.core.dao.DeviceDAO;
import org.wso2.carbon.device.mgt.core.dao.DeviceManagementDAOException; import org.wso2.carbon.device.mgt.core.dao.DeviceManagementDAOException;
import org.wso2.carbon.device.mgt.core.dao.DeviceManagementDAOFactory; import org.wso2.carbon.device.mgt.core.dao.DeviceManagementDAOFactory;
@ -36,13 +27,18 @@ import org.wso2.carbon.device.mgt.core.dao.DeviceTypeDAO;
import org.wso2.carbon.device.mgt.core.dao.util.DeviceManagementDAOUtil; import org.wso2.carbon.device.mgt.core.dao.util.DeviceManagementDAOUtil;
import org.wso2.carbon.device.mgt.core.dto.DeviceType; import org.wso2.carbon.device.mgt.core.dto.DeviceType;
import org.wso2.carbon.device.mgt.core.dto.Status; import org.wso2.carbon.device.mgt.core.dto.Status;
import org.wso2.carbon.device.mgt.core.license.mgt.LicenseManagementException;
import org.wso2.carbon.device.mgt.core.license.mgt.LicenseManager;
import org.wso2.carbon.device.mgt.core.license.mgt.LicenseManagerImpl;
import org.wso2.carbon.device.mgt.core.operation.mgt.Operation;
import org.wso2.carbon.device.mgt.core.operation.mgt.OperationManagementException;
import org.wso2.carbon.device.mgt.core.operation.mgt.OperationManager;
import org.wso2.carbon.device.mgt.core.operation.mgt.OperationManagerImpl; import org.wso2.carbon.device.mgt.core.operation.mgt.OperationManagerImpl;
import java.util.ArrayList; import java.util.ArrayList;
import java.util.List; import java.util.List;
import java.util.Locale;
public class DeviceManagementServiceProviderImpl implements DeviceManager { public class DeviceManagementServiceProviderImpl implements DeviceManagementService {
private DeviceDAO deviceDAO; private DeviceDAO deviceDAO;
private DeviceTypeDAO deviceTypeDAO; private DeviceTypeDAO deviceTypeDAO;
@ -58,57 +54,60 @@ public class DeviceManagementServiceProviderImpl implements DeviceManager {
this.licenseManager = new LicenseManagerImpl(); this.licenseManager = new LicenseManagerImpl();
} }
@Override
public String getProviderType() {
return null;
}
@Override @Override
public boolean enrollDevice(Device device) throws DeviceManagementException { public boolean enrollDevice(Device device) throws DeviceManagementException {
DeviceManagerService dms = DeviceManager dms =
this.getPluginRepository().getDeviceManagementProvider(device.getType()); this.getPluginRepository().getDeviceManagementProvider(device.getType());
boolean status = dms.enrollDevice(device); boolean status = dms.enrollDevice(device);
try { try {
org.wso2.carbon.device.mgt.core.dto.Device deviceDto = org.wso2.carbon.device.mgt.core.dto.Device deviceDto = DeviceManagementDAOUtil.convertDevice(device);
DeviceManagementDAOUtil.convertDevice(device); DeviceType deviceType = this.getDeviceTypeDAO().getDeviceType(device.getType());
Integer deviceTypeId =
this.getDeviceTypeDAO().getDeviceTypeIdByDeviceTypeName(device.getType());
deviceDto.setStatus(Status.ACTIVE); deviceDto.setStatus(Status.ACTIVE);
deviceDto.setDeviceTypeId(deviceTypeId); deviceDto.setDeviceTypeId(deviceType.getId());
this.getDeviceDAO().addDevice(deviceDto); this.getDeviceDAO().addDevice(deviceDto);
} catch (DeviceManagementDAOException e) { } catch (DeviceManagementDAOException e) {
throw new DeviceManagementException("Error occurred while enrolling the device '" + device.getId() + throw new DeviceManagementException("Error occurred while enrolling the device " +
"'", e); "'" + device.getId() + "'", e);
} }
return status; return status;
} }
@Override @Override
public boolean modifyEnrollment(Device device) throws DeviceManagementException { public boolean modifyEnrollment(Device device) throws DeviceManagementException {
DeviceManagerService dms = DeviceManager dms =
this.getPluginRepository().getDeviceManagementProvider(device.getType()); this.getPluginRepository().getDeviceManagementProvider(device.getType());
boolean status = dms.modifyEnrollment(device); boolean status = dms.modifyEnrollment(device);
try { try {
this.getDeviceDAO().updateDevice(DeviceManagementDAOUtil.convertDevice(device)); this.getDeviceDAO().updateDevice(DeviceManagementDAOUtil.convertDevice(device));
} catch (DeviceManagementDAOException e) { } catch (DeviceManagementDAOException e) {
throw new DeviceManagementException("Error occurred while modifying the device '" + device.getId() + throw new DeviceManagementException("Error occurred while modifying the device " +
"'", e); "'" + device.getId() + "'", e);
} }
return status; return status;
} }
@Override @Override
public boolean disenrollDevice(DeviceIdentifier deviceId) throws DeviceManagementException { public boolean disenrollDevice(DeviceIdentifier deviceId) throws DeviceManagementException {
DeviceManagerService dms = DeviceManager dms =
this.getPluginRepository().getDeviceManagementProvider(deviceId.getType()); this.getPluginRepository().getDeviceManagementProvider(deviceId.getType());
return dms.disenrollDevice(deviceId); return dms.disenrollDevice(deviceId);
} }
@Override @Override
public boolean isEnrolled(DeviceIdentifier deviceId) throws DeviceManagementException { public boolean isEnrolled(DeviceIdentifier deviceId) throws DeviceManagementException {
DeviceManagerService dms = DeviceManager dms =
this.getPluginRepository().getDeviceManagementProvider(deviceId.getType()); this.getPluginRepository().getDeviceManagementProvider(deviceId.getType());
return dms.isEnrolled(deviceId); return dms.isEnrolled(deviceId);
} }
@Override @Override
public boolean isActive(DeviceIdentifier deviceId) throws DeviceManagementException { public boolean isActive(DeviceIdentifier deviceId) throws DeviceManagementException {
DeviceManagerService dms = DeviceManager dms =
this.getPluginRepository().getDeviceManagementProvider(deviceId.getType()); this.getPluginRepository().getDeviceManagementProvider(deviceId.getType());
return dms.isActive(deviceId); return dms.isActive(deviceId);
} }
@ -116,19 +115,24 @@ public class DeviceManagementServiceProviderImpl implements DeviceManager {
@Override @Override
public boolean setActive(DeviceIdentifier deviceId, boolean status) public boolean setActive(DeviceIdentifier deviceId, boolean status)
throws DeviceManagementException { throws DeviceManagementException {
DeviceManagerService dms = DeviceManager dms =
this.getPluginRepository().getDeviceManagementProvider(deviceId.getType()); this.getPluginRepository().getDeviceManagementProvider(deviceId.getType());
return dms.setActive(deviceId, status); return dms.setActive(deviceId, status);
} }
@Override
public List<Device> getAllDevices() throws DeviceManagementException {
return null;
}
@Override @Override
public List<Device> getAllDevices(String type) throws DeviceManagementException { public List<Device> getAllDevices(String type) throws DeviceManagementException {
DeviceManagerService dms = this.getPluginRepository().getDeviceManagementProvider(type); DeviceManager dms = this.getPluginRepository().getDeviceManagementProvider(type);
List<Device> devicesList = new ArrayList<Device>(); List<Device> devicesList = new ArrayList<Device>();
try { try {
Integer deviceTypeId = this.getDeviceTypeDAO().getDeviceTypeIdByDeviceTypeName(type); DeviceType dt = this.getDeviceTypeDAO().getDeviceType(type);
List<org.wso2.carbon.device.mgt.core.dto.Device> devices = List<org.wso2.carbon.device.mgt.core.dto.Device> devices =
this.getDeviceDAO().getDevices(deviceTypeId); this.getDeviceDAO().getDevices(dt.getId());
for (org.wso2.carbon.device.mgt.core.dto.Device device : devices) { for (org.wso2.carbon.device.mgt.core.dto.Device device : devices) {
DeviceType deviceType = this.deviceTypeDAO.getDeviceType(device.getDeviceTypeId()); DeviceType deviceType = this.deviceTypeDAO.getDeviceType(device.getDeviceTypeId());
@ -143,25 +147,25 @@ public class DeviceManagementServiceProviderImpl implements DeviceManager {
devicesList.add(convertedDevice); devicesList.add(convertedDevice);
} }
} catch (DeviceManagementDAOException e) { } catch (DeviceManagementDAOException e) {
throw new DeviceManagementException("Error occurred while obtaining the device for type '" + type + throw new DeviceManagementException("Error occurred while obtaining the device for type " +
"'", e); "'" + type + "'", e);
} }
return devicesList; return devicesList;
} }
@Override @Override
public Device getDevice(DeviceIdentifier deviceId) throws DeviceManagementException { public Device getDevice(DeviceIdentifier deviceId) throws DeviceManagementException {
DeviceManagerService dms = DeviceManager dms =
this.getPluginRepository().getDeviceManagementProvider(deviceId.getType()); this.getPluginRepository().getDeviceManagementProvider(deviceId.getType());
Device convertedDevice = null; Device convertedDevice = null;
try { try {
Integer deviceTypeId = DeviceType deviceType =
this.getDeviceTypeDAO().getDeviceTypeIdByDeviceTypeName(deviceId.getType()); this.getDeviceTypeDAO().getDeviceType(deviceId.getType());
org.wso2.carbon.device.mgt.core.dto.Device device = org.wso2.carbon.device.mgt.core.dto.Device device =
this.getDeviceDAO().getDeviceByDeviceIdentifier(deviceTypeId, deviceId.getId()); this.getDeviceDAO().getDevice(deviceId);
if (device != null) { if (device != null) {
convertedDevice = DeviceManagementDAOUtil convertedDevice = DeviceManagementDAOUtil
.convertDevice(device, this.getDeviceTypeDAO().getDeviceType(deviceTypeId)); .convertDevice(device, this.getDeviceTypeDAO().getDeviceType(deviceType.getId()));
Device dmsDevice = dms.getDevice(deviceId); Device dmsDevice = dms.getDevice(deviceId);
if (dmsDevice != null) { if (dmsDevice != null) {
convertedDevice.setProperties(dmsDevice.getProperties()); convertedDevice.setProperties(dmsDevice.getProperties());
@ -169,15 +173,15 @@ public class DeviceManagementServiceProviderImpl implements DeviceManager {
} }
} }
} catch (DeviceManagementDAOException e) { } catch (DeviceManagementDAOException e) {
throw new DeviceManagementException("Error occurred while obtaining the device for id '" + throw new DeviceManagementException("Error occurred while obtaining the device for id " +
deviceId.getId() + "'", e); "'" + deviceId.getId() + "'", e);
} }
return convertedDevice; return convertedDevice;
} }
@Override @Override
public boolean updateDeviceInfo(Device device) throws DeviceManagementException { public boolean updateDeviceInfo(Device device) throws DeviceManagementException {
DeviceManagerService dms = DeviceManager dms =
this.getPluginRepository().getDeviceManagementProvider(device.getType()); this.getPluginRepository().getDeviceManagementProvider(device.getType());
return dms.updateDeviceInfo(device); return dms.updateDeviceInfo(device);
} }
@ -185,7 +189,7 @@ public class DeviceManagementServiceProviderImpl implements DeviceManager {
@Override @Override
public boolean setOwnership(DeviceIdentifier deviceId, String ownershipType) public boolean setOwnership(DeviceIdentifier deviceId, String ownershipType)
throws DeviceManagementException { throws DeviceManagementException {
DeviceManagerService dms = DeviceManager dms =
this.getPluginRepository().getDeviceManagementProvider(deviceId.getType()); this.getPluginRepository().getDeviceManagementProvider(deviceId.getType());
return dms.setOwnership(deviceId, ownershipType); return dms.setOwnership(deviceId, ownershipType);
} }
@ -195,24 +199,13 @@ public class DeviceManagementServiceProviderImpl implements DeviceManager {
} }
@Override @Override
public License getLicense(String type) throws DeviceManagementException { public License getLicense(String deviceType, String languageCode) throws LicenseManagementException {
try { return licenseManager.getLicense(deviceType, languageCode);
return licenseManager.getLicense(type, Locale.ENGLISH.getLanguage());
} catch (LicenseManagementException e) {
throw new DeviceManagementException("Error occurred while retrieving license configured for " +
"device type '" + type + "'", e);
}
} }
@Override @Override
public boolean addLicense(String type, License license) throws DeviceManagementException { public boolean addLicense(String type, License license) throws LicenseManagementException {
try {
return licenseManager.addLicense(type, license); return licenseManager.addLicense(type, license);
} catch (LicenseManagementException e) {
throw new DeviceManagementException("Error occurred while adding license for device type '" +
type + "'", e);
}
} }
public DeviceDAO getDeviceDAO() { public DeviceDAO getDeviceDAO() {
@ -227,4 +220,25 @@ public class DeviceManagementServiceProviderImpl implements DeviceManager {
return pluginRepository; return pluginRepository;
} }
@Override
public boolean addOperation(Operation operation,
List<DeviceIdentifier> devices) throws OperationManagementException {
return operationManager.addOperation(operation, devices);
}
@Override
public List<Operation> getOperations(DeviceIdentifier deviceId) throws OperationManagementException {
return operationManager.getOperations(deviceId);
}
@Override
public List<Operation> getPendingOperations(DeviceIdentifier deviceId) throws OperationManagementException {
return operationManager.getPendingOperations(deviceId);
}
@Override
public List<Feature> getFeatures(String deviceType) throws FeatureManagementException {
return operationManager.getFeatures(deviceType);
}
} }

@ -18,6 +18,7 @@
package org.wso2.carbon.device.mgt.core.dao; package org.wso2.carbon.device.mgt.core.dao;
import org.wso2.carbon.device.mgt.common.DeviceIdentifier;
import org.wso2.carbon.device.mgt.core.dto.Device; import org.wso2.carbon.device.mgt.core.dto.Device;
import org.wso2.carbon.device.mgt.core.dto.Status; import org.wso2.carbon.device.mgt.core.dto.Status;
@ -32,27 +33,22 @@ public interface DeviceDAO {
void updateDevice(Device device) throws DeviceManagementDAOException; void updateDevice(Device device) throws DeviceManagementDAOException;
void updateDeviceStatus(Long deviceId, Status status) throws DeviceManagementDAOException; void updateDeviceStatus(int deviceId, Status status) throws DeviceManagementDAOException;
void deleteDevice(Long deviceId) throws DeviceManagementDAOException; void deleteDevice(int deviceId) throws DeviceManagementDAOException;
Device getDeviceByDeviceId(Long deviceId) throws DeviceManagementDAOException; Device getDevice(int deviceId) throws DeviceManagementDAOException;
/** Device getDevice(DeviceIdentifier deviceIdentifier) throws DeviceManagementDAOException;
* @param type - Device type.
* @param identifier - Device identifier.
* @return the Device object which matches given data
* @throws DeviceManagementDAOException
*/
Device getDeviceByDeviceIdentifier(Integer type, String identifier)
throws DeviceManagementDAOException;
List<Device> getDevices() throws DeviceManagementDAOException; List<Device> getDevices() throws DeviceManagementDAOException;
List<Integer> getDeviceIds(List<DeviceIdentifier> devices) throws DeviceManagementDAOException;
/** /**
* @param type - The device type id. * @param type - The device type id.
* @return a list of devices based on the type id. * @return a list of devices based on the type id.
* @throws DeviceManagementDAOException * @throws DeviceManagementDAOException
*/ */
List<Device> getDevices(Integer type) throws DeviceManagementDAOException; List<Device> getDevices(int type) throws DeviceManagementDAOException;
} }

@ -90,4 +90,5 @@ public class DeviceManagementDAOFactory {
public static DataSource getDataSource() { public static DataSource getDataSource() {
return dataSource; return dataSource;
} }
} }

@ -33,10 +33,10 @@ public interface DeviceTypeDAO {
List<DeviceType> getDeviceTypes() throws DeviceManagementDAOException; List<DeviceType> getDeviceTypes() throws DeviceManagementDAOException;
DeviceType getDeviceType(Integer id) throws DeviceManagementDAOException; DeviceType getDeviceType(int id) throws DeviceManagementDAOException;
Integer getDeviceTypeIdByDeviceTypeName(String type) throws DeviceManagementDAOException; DeviceType getDeviceType(String name) throws DeviceManagementDAOException;
void removeDeviceType(DeviceType deviceType) throws DeviceManagementDAOException; void removeDeviceType(String type) throws DeviceManagementDAOException;
} }

@ -20,6 +20,7 @@ package org.wso2.carbon.device.mgt.core.dao.impl;
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.wso2.carbon.device.mgt.common.DeviceIdentifier;
import org.wso2.carbon.device.mgt.core.dao.DeviceDAO; import org.wso2.carbon.device.mgt.core.dao.DeviceDAO;
import org.wso2.carbon.device.mgt.core.dao.DeviceManagementDAOException; import org.wso2.carbon.device.mgt.core.dao.DeviceManagementDAOException;
import org.wso2.carbon.device.mgt.core.dao.util.DeviceManagementDAOUtil; import org.wso2.carbon.device.mgt.core.dao.util.DeviceManagementDAOUtil;
@ -50,12 +51,11 @@ public class DeviceDAOImpl implements DeviceDAO {
PreparedStatement stmt = null; PreparedStatement stmt = null;
try { try {
conn = this.getConnection(); conn = this.getConnection();
String createDBQuery = String sql =
"INSERT INTO DM_DEVICE(DESCRIPTION, NAME, DATE_OF_ENROLLMENT, DATE_OF_LAST_UPDATE, " + "INSERT INTO DM_DEVICE(DESCRIPTION, NAME, DATE_OF_ENROLLMENT, DATE_OF_LAST_UPDATE, " +
"OWNERSHIP, STATUS, DEVICE_TYPE_ID, DEVICE_IDENTIFICATION, OWNER, TENANT_ID) " + "OWNERSHIP, STATUS, DEVICE_TYPE_ID, DEVICE_IDENTIFICATION, OWNER, TENANT_ID) " +
"VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?, ?)"; "VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?, ?)";
stmt = conn.prepareStatement(sql);
stmt = conn.prepareStatement(createDBQuery);
stmt.setString(1, device.getDescription()); stmt.setString(1, device.getDescription());
stmt.setString(2, device.getName()); stmt.setString(2, device.getName());
stmt.setLong(3, new Date().getTime()); stmt.setLong(3, new Date().getTime());
@ -68,9 +68,8 @@ public class DeviceDAOImpl implements DeviceDAO {
stmt.setInt(10, device.getTenantId()); stmt.setInt(10, device.getTenantId());
stmt.executeUpdate(); stmt.executeUpdate();
} catch (SQLException e) { } catch (SQLException e) {
String msg = "Error occurred while enrolling device '" + device.getName() + "'"; throw new DeviceManagementDAOException("Error occurred while enrolling device " +
log.error(msg, e); "'" + device.getName() + "'", e);
throw new DeviceManagementDAOException(msg, e);
} finally { } finally {
DeviceManagementDAOUtil.cleanupResources(conn, stmt, null); DeviceManagementDAOUtil.cleanupResources(conn, stmt, null);
} }
@ -82,60 +81,57 @@ public class DeviceDAOImpl implements DeviceDAO {
} }
@Override @Override
public void updateDeviceStatus(Long deviceId, Status status) public void updateDeviceStatus(int deviceId, Status status) throws DeviceManagementDAOException {
throws DeviceManagementDAOException {
} }
@Override @Override
public void deleteDevice(Long deviceId) throws DeviceManagementDAOException { public void deleteDevice(int deviceId) throws DeviceManagementDAOException {
} }
@Override @Override
public Device getDeviceByDeviceId(Long deviceId) throws DeviceManagementDAOException { public Device getDevice(int deviceId) throws DeviceManagementDAOException {
return null; return null;
} }
@Override @Override
public Device getDeviceByDeviceIdentifier(Integer type, String identifier) public Device getDevice(DeviceIdentifier deviceIdentifier) throws DeviceManagementDAOException {
throws DeviceManagementDAOException {
Connection conn = null; Connection conn = null;
PreparedStatement stmt = null; PreparedStatement stmt = null;
ResultSet resultSet = null; ResultSet rs = null;
Device device = null; Device device = null;
try { try {
conn = this.getConnection(); conn = this.getConnection();
String selectDBQueryForType = "SELECT ID, DESCRIPTION, NAME, DATE_OF_ENROLLMENT, " + String sql =
"DATE_OF_LAST_UPDATE, OWNERSHIP, STATUS, DEVICE_TYPE_ID, " + "SELECT d.ID, d.DESCRIPTION, d.NAME, d.DATE_OF_ENROLLMENT, d.DATE_OF_LAST_UPDATE, d.OWNERSHIP, d.STATUS, " +
"DEVICE_IDENTIFICATION, OWNER, TENANT_ID FROM DM_DEVICE " + "d.DEVICE_TYPE_ID, d.DEVICE_IDENTIFICATION, d.OWNER, d.TENANT_ID FROM DM_DEVICE d, DEVICE_TYPE dt WHERE " +
"WHERE DM_DEVICE.DEVICE_TYPE_ID=? AND " + "dt.NAME = ? AND d.DEVICE_IDENTIFICATION = ?";
"DM_DEVICE.DEVICE_IDENTIFICATION=?"; stmt = conn.prepareStatement(sql);
stmt = conn.prepareStatement(selectDBQueryForType); stmt.setString(1, deviceIdentifier.getType());
stmt.setInt(1, type); stmt.setString(2, deviceIdentifier.getId());
stmt.setString(2, identifier);
resultSet = stmt.executeQuery(); rs = stmt.executeQuery();
while (resultSet.next()) { if (rs.next()) {
device = new Device(); device = new Device();
device.setId(resultSet.getInt(1)); device.setId(rs.getInt("ID"));
device.setDescription(resultSet.getString(2)); device.setDescription(rs.getString("DESCRIPTION"));
device.setName(resultSet.getString(3)); device.setName(rs.getString("NAME"));
device.setDateOfEnrollment(resultSet.getLong(4)); device.setDateOfEnrollment(rs.getLong("DATE_OF_ENROLLMENT"));
device.setDateOfLastUpdate(resultSet.getLong(5)); device.setDateOfLastUpdate(rs.getLong("DATE_OF_LAST_UPDATE"));
//TODO:- Ownership is not a enum in DeviceDAO //TODO:- Ownership is not a enum in DeviceDAO
device.setOwnerShip(resultSet.getString(6)); device.setOwnerShip(rs.getString("OWNERSHIP"));
device.setStatus(Status.valueOf(resultSet.getString(7))); device.setStatus(Status.valueOf(rs.getString("STATUS")));
device.setDeviceTypeId(resultSet.getInt(8)); device.setDeviceTypeId(rs.getInt("DEVICE_TYPE_ID"));
device.setDeviceIdentificationId(resultSet.getString(9)); device.setDeviceIdentificationId(rs.getString("DEVICE_IDENTIFICATION"));
device.setOwnerId(resultSet.getString(10)); device.setOwnerId(rs.getString("OWNER"));
device.setTenantId(resultSet.getInt(11)); device.setTenantId(rs.getInt("TENANT_ID"));
} }
} catch (SQLException e) { } catch (SQLException e) {
String msg = "Error occurred while listing devices for type '" + type + "'"; throw new DeviceManagementDAOException("Error occurred while listing devices for type " +
log.error(msg, e); "'" + deviceIdentifier.getType() + "'", e);
throw new DeviceManagementDAOException(msg, e);
} finally { } finally {
DeviceManagementDAOUtil.cleanupResources(conn, stmt, resultSet); DeviceManagementDAOUtil.cleanupResources(conn, stmt, rs);
} }
return device; return device;
} }
@ -146,7 +142,34 @@ public class DeviceDAOImpl implements DeviceDAO {
} }
@Override @Override
public List<Device> getDevices(Integer type) throws DeviceManagementDAOException { public List<Integer> getDeviceIds(List<DeviceIdentifier> devices) throws DeviceManagementDAOException {
List<Integer> deviceIds = new ArrayList<Integer>();
try {
Connection conn = this.getConnection();
String sql = "SELECT DISTINCT ID FROM DEVICE WHERE NAME IN (?) AND ID IN (?)";
PreparedStatement stmt = conn.prepareStatement(sql);
//stmt.setArray(1, new java.sql.Date[0]);
stmt.setString(2, "");
ResultSet rs = stmt.executeQuery();
while (rs.next()) {
deviceIds.add(rs.getInt("ID"));
}
return deviceIds;
} catch (SQLException e) {
throw new DeviceManagementDAOException("Error occurred while retrieving device ids", e);
}
}
private String getDeviceNameString(List<DeviceIdentifier> devices) {
StringBuilder sb = new StringBuilder();
for (DeviceIdentifier device : devices) {
sb.append(device.getId());
}
return sb.toString();
}
@Override
public List<Device> getDevices(int type) throws DeviceManagementDAOException {
Connection conn = null; Connection conn = null;
PreparedStatement stmt = null; PreparedStatement stmt = null;
ResultSet resultSet = null; ResultSet resultSet = null;
@ -156,7 +179,7 @@ public class DeviceDAOImpl implements DeviceDAO {
String selectDBQueryForType = "SELECT ID, DESCRIPTION, NAME, DATE_OF_ENROLLMENT, " + String selectDBQueryForType = "SELECT ID, DESCRIPTION, NAME, DATE_OF_ENROLLMENT, " +
"DATE_OF_LAST_UPDATE, OWNERSHIP, STATUS, DEVICE_TYPE_ID, " + "DATE_OF_LAST_UPDATE, OWNERSHIP, STATUS, DEVICE_TYPE_ID, " +
"DEVICE_IDENTIFICATION, OWNER, TENANT_ID FROM DM_DEVICE " + "DEVICE_IDENTIFICATION, OWNER, TENANT_ID FROM DM_DEVICE " +
"WHERE DM_DEVICE.DEVICE_TYPE_ID=?"; "WHERE DM_DEVICE.DEVICE_TYPE_ID = ?";
stmt = conn.prepareStatement(selectDBQueryForType); stmt = conn.prepareStatement(selectDBQueryForType);
stmt.setInt(1, type); stmt.setInt(1, type);
resultSet = stmt.executeQuery(); resultSet = stmt.executeQuery();

@ -50,9 +50,8 @@ public class DeviceTypeDAOImpl implements DeviceTypeDAO {
stmt.setString(1, deviceType.getName()); stmt.setString(1, deviceType.getName());
stmt.execute(); stmt.execute();
} catch (SQLException e) { } catch (SQLException e) {
String msg = "Error occurred while registering the device type '" + deviceType.getName() + "'"; throw new DeviceManagementDAOException("Error occurred while registering the device type " +
log.error(msg, e); "'" + deviceType.getName() + "'", e);
throw new DeviceManagementDAOException(msg, e);
} finally { } finally {
DeviceManagementDAOUtil.cleanupResources(conn, stmt, null); DeviceManagementDAOUtil.cleanupResources(conn, stmt, null);
} }
@ -65,85 +64,88 @@ public class DeviceTypeDAOImpl implements DeviceTypeDAO {
@Override @Override
public List<DeviceType> getDeviceTypes() throws DeviceManagementDAOException { public List<DeviceType> getDeviceTypes() throws DeviceManagementDAOException {
Connection conn = this.getConnection(); Connection conn = null;
PreparedStatement stmt = null; PreparedStatement stmt = null;
List<DeviceType> deviceTypes = new ArrayList<DeviceType>(); ResultSet rs = null;
List<DeviceType> deviceTypes = null;
try { try {
stmt = conn.prepareStatement("SELECT ID AS DEVICE_TYPE_ID, NAME AS DEVICE_TYPE FROM DM_DEVICE_TYPE"); conn = this.getConnection();
ResultSet results = stmt.executeQuery(); String sql = "SELECT ID AS DEVICE_TYPE_ID, NAME AS DEVICE_TYPE FROM DM_DEVICE_TYPE";
stmt = conn.prepareStatement(sql);
rs = stmt.executeQuery();
while (results.next()) { while (rs.next()) {
deviceTypes = new ArrayList<DeviceType>();
DeviceType deviceType = new DeviceType(); DeviceType deviceType = new DeviceType();
deviceType.setId(results.getLong("DEVICE_TYPE_ID")); deviceType.setId(rs.getInt("DEVICE_TYPE_ID"));
deviceType.setName(results.getString("DEVICE_TYPE")); deviceType.setName(rs.getString("DEVICE_TYPE"));
deviceTypes.add(deviceType); deviceTypes.add(deviceType);
} }
return deviceTypes;
} catch (SQLException e) { } catch (SQLException e) {
String msg = "Error occurred while fetching the registered device types"; throw new DeviceManagementDAOException("Error occurred while fetching the registered device types", e);
log.error(msg, e);
throw new DeviceManagementDAOException(msg, e);
} finally { } finally {
DeviceManagementDAOUtil.cleanupResources(conn, stmt, null); DeviceManagementDAOUtil.cleanupResources(conn, stmt, rs);
} }
return deviceTypes;
} }
@Override @Override
public DeviceType getDeviceType(Integer id) throws DeviceManagementDAOException { public DeviceType getDeviceType(int id) throws DeviceManagementDAOException {
Connection conn = this.getConnection(); Connection conn = null;
PreparedStatement stmt = null; PreparedStatement stmt = null;
DeviceType deviceType = null; ResultSet rs = null;
try { try {
stmt = conn.prepareStatement("SELECT ID AS DEVICE_TYPE_ID, NAME AS DEVICE_TYPE FROM DM_DEVICE_TYPE WHERE ID=?"); conn = this.getConnection();
String sql = "SELECT ID AS DEVICE_TYPE_ID, NAME AS DEVICE_TYPE FROM DM_DEVICE_TYPE WHERE ID = ?";
stmt = conn.prepareStatement(sql);
stmt.setInt(1, id); stmt.setInt(1, id);
ResultSet results = stmt.executeQuery();
while (results.next()) { rs = stmt.executeQuery();
DeviceType deviceType = null;
while (rs.next()) {
deviceType = new DeviceType(); deviceType = new DeviceType();
deviceType.setId(results.getLong("DEVICE_TYPE_ID")); deviceType.setId(rs.getInt("DEVICE_TYPE_ID"));
deviceType.setName(results.getString("DEVICE_TYPE")); deviceType.setName(rs.getString("DEVICE_TYPE"));
} }
return deviceType;
} catch (SQLException e) { } catch (SQLException e) {
String msg = "Error occurred while fetching the registered device type"; throw new DeviceManagementDAOException("Error occurred while fetching the registered device type", e);
log.error(msg, e);
throw new DeviceManagementDAOException(msg, e);
} finally { } finally {
DeviceManagementDAOUtil.cleanupResources(conn, stmt, null); DeviceManagementDAOUtil.cleanupResources(conn, stmt, rs);
} }
return deviceType;
} }
@Override @Override
public Integer getDeviceTypeIdByDeviceTypeName(String type) throws DeviceManagementDAOException { public DeviceType getDeviceType(String type) throws DeviceManagementDAOException {
Connection conn = null;
Connection conn = this.getConnection();
PreparedStatement stmt = null; PreparedStatement stmt = null;
ResultSet resultSet = null; ResultSet rs = null;
Integer deviceTypeId = null;
try { try {
String createDBQuery = "SELECT * From DM_DEVICE_TYPE DT WHERE DT.NAME=?"; conn = this.getConnection();
stmt = conn.prepareStatement(createDBQuery); String sql = "SELECT ID From DM_DEVICE_TYPE WHERE NAME = ?";
stmt = conn.prepareStatement(sql);
stmt.setString(1, type); stmt.setString(1, type);
resultSet = stmt.executeQuery(); rs = stmt.executeQuery();
while (resultSet.next()) { int id = -1;
deviceTypeId = resultSet.getInt(1); if (rs.next()) {
id = rs.getInt("ID");
} }
DeviceType deviceType = new DeviceType();
deviceType.setId(id);
deviceType.setName(type);
return deviceType;
} catch (SQLException e) { } catch (SQLException e) {
String msg = "Error occurred while fetch device type id for device type '" + type + "'"; throw new DeviceManagementDAOException("Error occurred while fetch device type id for device type " +
log.error(msg, e); "'" + type + "'", e);
throw new DeviceManagementDAOException(msg, e);
} finally { } finally {
DeviceManagementDAOUtil.cleanupResources(conn, stmt, null); DeviceManagementDAOUtil.cleanupResources(conn, stmt, rs);
} }
return deviceTypeId;
} }
@Override @Override
public void removeDeviceType(DeviceType deviceType) throws DeviceManagementDAOException { public void removeDeviceType(String type) throws DeviceManagementDAOException {
} }
@ -157,4 +159,5 @@ public class DeviceTypeDAOImpl implements DeviceTypeDAO {
throw new DeviceManagementDAOException(msg, e); throw new DeviceManagementDAOException(msg, e);
} }
} }
} }

@ -23,7 +23,7 @@ import java.io.Serializable;
public class Device implements Serializable { public class Device implements Serializable {
private static final long serialVersionUID = -8101106997837486245L; private static final long serialVersionUID = -8101106997837486245L;
private Integer id; private int id;
private String description; private String description;
private String name; private String name;
private Long dateOfEnrollment; private Long dateOfEnrollment;
@ -33,13 +33,13 @@ public class Device implements Serializable {
private String ownerId; private String ownerId;
private String ownerShip; private String ownerShip;
private int tenantId; private int tenantId;
private Integer deviceTypeId; private int deviceTypeId;
public Integer getDeviceTypeId() { public int getDeviceTypeId() {
return deviceTypeId; return deviceTypeId;
} }
public void setDeviceTypeId(Integer deviceTypeId) { public void setDeviceTypeId(int deviceTypeId) {
this.deviceTypeId = deviceTypeId; this.deviceTypeId = deviceTypeId;
} }

@ -23,14 +23,14 @@ import java.io.Serializable;
public class DeviceType implements Serializable { public class DeviceType implements Serializable {
private static final long serialVersionUID = 7927802716452548282L; private static final long serialVersionUID = 7927802716452548282L;
private Long id; private int id;
private String name; private String name;
public Long getId() { public int getId() {
return id; return id;
} }
public void setId(Long id) { public void setId(int id) {
this.id = id; this.id = id;
} }

@ -19,7 +19,7 @@
package org.wso2.carbon.device.mgt.core.internal; package org.wso2.carbon.device.mgt.core.internal;
import org.wso2.carbon.device.mgt.core.DeviceManager; import org.wso2.carbon.device.mgt.core.DeviceManagementService;
import org.wso2.carbon.device.mgt.core.config.license.LicenseConfig; import org.wso2.carbon.device.mgt.core.config.license.LicenseConfig;
import org.wso2.carbon.device.mgt.core.license.mgt.LicenseManager; import org.wso2.carbon.device.mgt.core.license.mgt.LicenseManager;
import org.wso2.carbon.registry.core.service.RegistryService; import org.wso2.carbon.registry.core.service.RegistryService;
@ -30,7 +30,7 @@ public class DeviceManagementDataHolder {
private RealmService realmService; private RealmService realmService;
private TenantManager tenantManager; private TenantManager tenantManager;
private DeviceManager deviceManager; private DeviceManagementService deviceManagerProvider;
private LicenseManager licenseManager; private LicenseManager licenseManager;
private RegistryService registryService; private RegistryService registryService;
private LicenseConfig licenseConfig; private LicenseConfig licenseConfig;
@ -64,12 +64,12 @@ public class DeviceManagementDataHolder {
return tenantManager; return tenantManager;
} }
public DeviceManager getDeviceManager() { public DeviceManagementService getDeviceManagementProvider() {
return deviceManager; return deviceManagerProvider;
} }
public void setDeviceManager(DeviceManager deviceManager) { public void setDeviceManagementProvider(DeviceManagementService deviceManagerProvider) {
this.deviceManager = deviceManager; this.deviceManagerProvider = deviceManagerProvider;
} }
public RegistryService getRegistryService() { public RegistryService getRegistryService() {

@ -22,11 +22,11 @@ import org.apache.commons.logging.LogFactory;
import org.osgi.framework.BundleContext; import org.osgi.framework.BundleContext;
import org.osgi.service.component.ComponentContext; import org.osgi.service.component.ComponentContext;
import org.wso2.carbon.device.mgt.common.DeviceManagementException; import org.wso2.carbon.device.mgt.common.DeviceManagementException;
import org.wso2.carbon.device.mgt.common.spi.DeviceManagerService; import org.wso2.carbon.device.mgt.common.spi.DeviceManager;
import org.wso2.carbon.device.mgt.core.DeviceManagementConstants; import org.wso2.carbon.device.mgt.core.DeviceManagementConstants;
import org.wso2.carbon.device.mgt.core.DeviceManagementRepository; import org.wso2.carbon.device.mgt.core.DeviceManagementRepository;
import org.wso2.carbon.device.mgt.core.DeviceManager; import org.wso2.carbon.device.mgt.core.DeviceManagementServiceProviderImpl;
import org.wso2.carbon.device.mgt.core.DeviceManagerImpl; import org.wso2.carbon.device.mgt.core.DeviceManagementService;
import org.wso2.carbon.device.mgt.core.config.DeviceConfigurationManager; import org.wso2.carbon.device.mgt.core.config.DeviceConfigurationManager;
import org.wso2.carbon.device.mgt.core.config.DeviceManagementConfig; import org.wso2.carbon.device.mgt.core.config.DeviceManagementConfig;
import org.wso2.carbon.device.mgt.core.config.datasource.DataSourceConfig; import org.wso2.carbon.device.mgt.core.config.datasource.DataSourceConfig;
@ -38,7 +38,7 @@ import org.wso2.carbon.device.mgt.core.license.mgt.LicenseManagementException;
import org.wso2.carbon.device.mgt.core.license.mgt.LicenseManagementService; import org.wso2.carbon.device.mgt.core.license.mgt.LicenseManagementService;
import org.wso2.carbon.device.mgt.core.license.mgt.LicenseManager; import org.wso2.carbon.device.mgt.core.license.mgt.LicenseManager;
import org.wso2.carbon.device.mgt.core.license.mgt.LicenseManagerImpl; import org.wso2.carbon.device.mgt.core.license.mgt.LicenseManagerImpl;
import org.wso2.carbon.device.mgt.core.service.DeviceManagementService; import org.wso2.carbon.device.mgt.core.service.DeviceManagementServiceImpl;
import org.wso2.carbon.device.mgt.core.util.DeviceManagementSchemaInitializer; import org.wso2.carbon.device.mgt.core.util.DeviceManagementSchemaInitializer;
import org.wso2.carbon.registry.core.service.RegistryService; import org.wso2.carbon.registry.core.service.RegistryService;
import org.wso2.carbon.user.core.service.RealmService; import org.wso2.carbon.user.core.service.RealmService;
@ -52,11 +52,11 @@ import org.wso2.carbon.user.core.service.RealmService;
* bind="setRealmService" * bind="setRealmService"
* unbind="unsetRealmService" * unbind="unsetRealmService"
* @scr.reference name="device.manager.service" * @scr.reference name="device.manager.service"
* interface="org.wso2.carbon.device.mgt.common.spi.DeviceManagerService" * interface="org.wso2.carbon.device.mgt.common.spi.DeviceManager"
* cardinality="0..n" * cardinality="0..n"
* policy="dynamic" * policy="dynamic"
* bind="setDeviceManagerService" * bind="setDeviceManager"
* unbind="unsetDeviceManagerService" * unbind="unsetDeviceManager"
* @scr.reference name="registry.service" * @scr.reference name="registry.service"
* interface="org.wso2.carbon.registry.core.service.RegistryService" * interface="org.wso2.carbon.registry.core.service.RegistryService"
* cardinality="1..1" * cardinality="1..1"
@ -74,6 +74,7 @@ public class DeviceManagementServiceComponent {
if (log.isDebugEnabled()) { if (log.isDebugEnabled()) {
log.debug("Initializing device management core bundle"); log.debug("Initializing device management core bundle");
} }
/* Initializing Device Management Configuration */ /* Initializing Device Management Configuration */
DeviceConfigurationManager.getInstance().initConfig(); DeviceConfigurationManager.getInstance().initConfig();
DeviceManagementConfig config = DeviceManagementConfig config =
@ -82,8 +83,9 @@ public class DeviceManagementServiceComponent {
DataSourceConfig dsConfig = config.getDeviceMgtRepository().getDataSourceConfig(); DataSourceConfig dsConfig = config.getDeviceMgtRepository().getDataSourceConfig();
DeviceManagementDAOFactory.init(dsConfig); DeviceManagementDAOFactory.init(dsConfig);
DeviceManager deviceManager = new DeviceManagerImpl(config, this.getPluginRepository()); DeviceManagementService deviceManagementProvider =
DeviceManagementDataHolder.getInstance().setDeviceManager(deviceManager); new DeviceManagementServiceProviderImpl(this.getPluginRepository());
DeviceManagementDataHolder.getInstance().setDeviceManagementProvider(deviceManagementProvider);
LicenseConfigurationManager.getInstance().initConfig(); LicenseConfigurationManager.getInstance().initConfig();
LicenseConfig licenseConfig = LicenseConfig licenseConfig =
@ -98,24 +100,16 @@ public class DeviceManagementServiceComponent {
System.getProperty(DeviceManagementConstants.Common.PROPERTY_SETUP); System.getProperty(DeviceManagementConstants.Common.PROPERTY_SETUP);
if (setupOption != null) { if (setupOption != null) {
if (log.isDebugEnabled()) { if (log.isDebugEnabled()) {
log.debug( log.debug("-Dsetup is enabled. Device management repository schema initialization is about to " +
"-Dsetup is enabled. Device management repository schema initialization " + "begin");
"is about to begin");
} }
this.setupDeviceManagementSchema(dsConfig); this.setupDeviceManagementSchema(dsConfig);
// this.setupDefaultLicenses(licenseConfig); this.setupDefaultLicenses(licenseConfig);
} }
if (log.isDebugEnabled()) { /* Registering declarative service instances exposed by DeviceManagementServiceComponent */
log.debug("Registering OSGi service DeviceManagementService"); this.registerServices(componentContext);
}
/* Registering Device Management service */
BundleContext bundleContext = componentContext.getBundleContext();
bundleContext.registerService(DeviceManagementService.class.getName(),
new DeviceManagementService(), null);
/* Registering License Management service */
bundleContext.registerService(LicenseManagementService.class.getName(),
new LicenseManagementService(), null);
if (log.isDebugEnabled()) { if (log.isDebugEnabled()) {
log.debug("Device management core bundle has been successfully initialized"); log.debug("Device management core bundle has been successfully initialized");
} }
@ -125,6 +119,16 @@ public class DeviceManagementServiceComponent {
} }
} }
private void registerServices(ComponentContext componentContext) {
if (log.isDebugEnabled()) {
log.debug("Registering OSGi service DeviceManagementServiceImpl");
}
/* Registering Device Management Service */
BundleContext bundleContext = componentContext.getBundleContext();
bundleContext.registerService(DeviceManagementServiceImpl.class.getName(),
new DeviceManagementServiceImpl(), null);
}
private void setupDeviceManagementSchema(DataSourceConfig config) private void setupDeviceManagementSchema(DataSourceConfig config)
throws DeviceManagementException { throws DeviceManagementException {
DeviceManagementSchemaInitializer initializer = DeviceManagementSchemaInitializer initializer =
@ -139,49 +143,53 @@ public class DeviceManagementServiceComponent {
"database schema", e); "database schema", e);
} }
if (log.isDebugEnabled()) { if (log.isDebugEnabled()) {
log.debug( log.debug("Device management metadata repository schema has been successfully initialized");
"Device management metadata repository schema has been successfully initialized");
} }
} }
private void setupDefaultLicenses(LicenseConfig licenseConfig) private void setupDefaultLicenses(LicenseConfig licenseConfig)
throws LicenseManagementException { throws LicenseManagementException {
LicenseManager licenseManager = LicenseManager licenseManager = DeviceManagementDataHolder.getInstance().getLicenseManager();
DeviceManagementDataHolder.getInstance().getLicenseManager();
for (License license : licenseConfig.getLicenses()) { for (License license : licenseConfig.getLicenses()) {
License extLicense = License extLicense = licenseManager.getLicense(license.getName(), license.getLanguage());
licenseManager.getLicense(license.getName(), license.getLanguage()); if (extLicense == null) {
if (extLicense != null) { licenseManager.addLicense(license.getName(), license);;
continue;
} }
licenseManager.addLicense(license);
} }
} }
/** /**
* Sets Device Manager service. * Sets Device Manager service.
* *
* @param deviceManagerService An instance of DeviceManagerService * @param deviceManager An instance of DeviceManager
*/ */
protected void setDeviceManagerService(DeviceManagerService deviceManagerService) { protected void setDeviceManager(DeviceManager deviceManager) {
if (log.isDebugEnabled()) { if (log.isDebugEnabled()) {
log.debug("Setting Device Management Service Provider : '" + log.debug("Setting Device Management Service Provider: '" + deviceManager.getProviderType() + "'");
deviceManagerService.getProviderType() + "'"); }
try {
this.getPluginRepository().addDeviceManagementProvider(deviceManager);
} catch (DeviceManagementException e) {
log.error("Error occurred while adding device management provider '" +
deviceManager.getProviderType() + "'");
} }
this.getPluginRepository().addDeviceManagementProvider(deviceManagerService);
} }
/** /**
* Unsets Device Management service. * Unsets Device Management service.
* *
* @param deviceManagerService An Instance of DeviceManagerService * @param deviceManager An Instance of DeviceManager
*/ */
protected void unsetDeviceManagerService(DeviceManagerService deviceManagerService) { protected void unsetDeviceManager(DeviceManager deviceManager) {
if (log.isDebugEnabled()) { if (log.isDebugEnabled()) {
log.debug("Unsetting Device Management Service Provider : '" + log.debug("Unsetting Device Management Service Provider : '" + deviceManager.getProviderType() + "'");
deviceManagerService.getProviderType() + "'"); }
try {
this.getPluginRepository().removeDeviceManagementProvider(deviceManager);
} catch (DeviceManagementException e) {
log.error("Error occurred while removing device management provider '" +
deviceManager.getProviderType() + "'");
} }
this.getPluginRepository().removeDeviceManagementProvider(deviceManagerService);
} }
/** /**

@ -18,5 +18,55 @@
*/ */
package org.wso2.carbon.device.mgt.core.license.mgt; package org.wso2.carbon.device.mgt.core.license.mgt;
import org.wso2.carbon.context.CarbonContext;
import org.wso2.carbon.device.mgt.common.DeviceManagementConstants;
import org.wso2.carbon.device.mgt.core.internal.DeviceManagementDataHolder;
import org.wso2.carbon.governance.api.generic.GenericArtifactManager;
import org.wso2.carbon.registry.api.Registry;
import org.wso2.carbon.registry.core.exceptions.RegistryException;
import java.util.HashMap;
import java.util.Map;
public class GenericArtifactManagerFactory { public class GenericArtifactManagerFactory {
private static Map<Integer, GenericArtifactManager> tenantArtifactManagers =
new HashMap<Integer, GenericArtifactManager>();
private static final Object lock = new Object();
public static GenericArtifactManager getTenantAwareGovernanceArtifactManager() throws
LicenseManagementException {
Registry registry;
int tenantId;
try {
tenantId = CarbonContext.getThreadLocalCarbonContext().getTenantId();
registry =
DeviceManagementDataHolder.getInstance().getRegistryService().getGovernanceSystemRegistry(
tenantId);
} catch (RegistryException e) {
throw new LicenseManagementException("Error occurred while initializing tenant system registry " +
"to be used to manipulate License artifacts", e);
}
try {
GenericArtifactManager artifactManager;
synchronized (lock) {
artifactManager =
tenantArtifactManagers.get(tenantId);
if (artifactManager == null) {
/* Hack, to fix https://wso2.org/jira/browse/REGISTRY-2427 */
//GovernanceUtils.loadGovernanceArtifacts((UserRegistry) registry);
artifactManager =
new GenericArtifactManager((org.wso2.carbon.registry.core.Registry) registry,
DeviceManagementConstants.LicenseProperties.LICENSE_REGISTRY_KEY);
tenantArtifactManagers.put(tenantId, artifactManager);
}
}
return artifactManager;
} catch (RegistryException e) {
throw new LicenseManagementException("Error occurred while initializing GovernanceArtifactManager " +
"associated with tenant '" + CarbonContext.getThreadLocalCarbonContext().getTenantDomain() + "'");
}
}
} }

@ -29,8 +29,8 @@ public class LicenseManagementService implements LicenseManager {
} }
@Override @Override
public void addLicense(License license) throws LicenseManagementException { public boolean addLicense(String deviceType, License license) throws LicenseManagementException {
DeviceManagementDataHolder.getInstance().getLicenseManager().addLicense(license); return DeviceManagementDataHolder.getInstance().getLicenseManager().addLicense(deviceType, license);
} }
} }

@ -22,8 +22,8 @@ import org.wso2.carbon.device.mgt.core.config.license.License;
public interface LicenseManager { public interface LicenseManager {
License getLicense(final String deviceType, final String languageCode) throws LicenseManagementException; License getLicense(String deviceType, String languageCode) throws LicenseManagementException;
void addLicense(License license) throws LicenseManagementException; boolean addLicense(String deviceType, License license) throws LicenseManagementException;
} }

@ -16,42 +16,96 @@
* under the License. * under the License.
* *
*/ */
package org.wso2.carbon.device.mgt.core.license.mgt; package org.wso2.carbon.device.mgt.core.license.mgt;
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.wso2.carbon.device.mgt.core.DeviceManagerImpl; import org.wso2.carbon.device.mgt.common.DeviceManagementConstants;
import org.wso2.carbon.device.mgt.core.DeviceManagementServiceProviderImpl;
import org.wso2.carbon.device.mgt.core.config.license.License; import org.wso2.carbon.device.mgt.core.config.license.License;
import org.wso2.carbon.device.mgt.core.config.license.LicenseConfig; import org.wso2.carbon.governance.api.exception.GovernanceException;
import org.wso2.carbon.device.mgt.core.internal.DeviceManagementDataHolder; import org.wso2.carbon.governance.api.generic.GenericArtifactFilter;
import org.wso2.carbon.governance.api.generic.GenericArtifactManager;
import org.wso2.carbon.governance.api.generic.dataobjects.GenericArtifact;
import javax.xml.namespace.QName;
import java.text.DateFormat; import java.text.DateFormat;
import java.text.ParseException;
import java.text.SimpleDateFormat; import java.text.SimpleDateFormat;
import java.util.Locale; import java.util.Locale;
public class LicenseManagerImpl implements LicenseManager { public class LicenseManagerImpl implements LicenseManager {
private static Log log = LogFactory.getLog(DeviceManagerImpl.class); private static Log log = LogFactory.getLog(DeviceManagementServiceProviderImpl.class);
private static final DateFormat format = new SimpleDateFormat("dd-mm-yyyy", Locale.ENGLISH); private static final DateFormat format = new SimpleDateFormat("dd-mm-yyyy", Locale.ENGLISH);
@Override @Override
public License getLicense(final String deviceType, final String languageCode) public License getLicense(final String deviceType, final String languageCode) throws LicenseManagementException {
throws LicenseManagementException { GenericArtifactManager artifactManager =
License deviceLicense = new License(); GenericArtifactManagerFactory.getTenantAwareGovernanceArtifactManager();
LicenseConfig licenseConfig = DeviceManagementDataHolder.getInstance().getLicenseConfig(); try {
for (License license : licenseConfig.getLicenses()) { GenericArtifact[] artifacts = artifactManager.findGenericArtifacts(new GenericArtifactFilter() {
if ((deviceType.equals(license.getName())) && @Override
(languageCode.equals(license.getLanguage()))) { public boolean matches(GenericArtifact artifact) throws GovernanceException {
deviceLicense = license; String attributeNameVal = artifact.getAttribute(
break; DeviceManagementConstants.LicenseProperties.NAME);
String attributeLangVal = artifact.getAttribute(
DeviceManagementConstants.LicenseProperties.LANGUAGE);
return (attributeNameVal != null && attributeLangVal != null && attributeNameVal.equals
(deviceType) && attributeLangVal.equals(languageCode));
} }
});
if (artifacts == null || artifacts.length <= 0) {
return null;
}
return this.populateLicense(artifacts[0]);
} catch (GovernanceException e) {
throw new LicenseManagementException("Error occurred while retrieving license corresponding to " +
"device type '" + deviceType + "'");
} catch (ParseException e) {
throw new LicenseManagementException("Error occurred while parsing the ToDate/FromDate date string " +
"of the license configured upon the device type '" + deviceType + "'");
} }
return deviceLicense;
} }
@Override private License populateLicense(GenericArtifact artifact) throws GovernanceException, ParseException {
public void addLicense(License license) throws LicenseManagementException { License license = new License();
license.setName(artifact.getAttribute(DeviceManagementConstants.LicenseProperties.NAME));
license.setProvider(artifact.getAttribute(DeviceManagementConstants.LicenseProperties.PROVIDER));
license.setVersion(artifact.getAttribute(DeviceManagementConstants.LicenseProperties.VERSION));
license.setLanguage(artifact.getAttribute(DeviceManagementConstants.LicenseProperties.LANGUAGE));
license.setText(artifact.getAttribute(DeviceManagementConstants.LicenseProperties.TEXT));
license.setValidFrom(format.parse(artifact.getAttribute(
DeviceManagementConstants.LicenseProperties.VALID_FROM)));
license.setValidTo(format.parse(artifact.getAttribute(
DeviceManagementConstants.LicenseProperties.VALID_TO)));
return license;
}
@Override
public boolean addLicense(String deviceType, License license) throws LicenseManagementException {
GenericArtifactManager artifactManager =
GenericArtifactManagerFactory.getTenantAwareGovernanceArtifactManager();
try {
GenericArtifact artifact =
artifactManager.newGovernanceArtifact(new QName("http://www.wso2.com",
DeviceManagementConstants.LicenseProperties.LICENSE_REGISTRY_KEY));
artifact.setAttribute(DeviceManagementConstants.LicenseProperties.NAME, license.getName());
artifact.setAttribute(DeviceManagementConstants.LicenseProperties.VERSION, license.getVersion());
artifact.setAttribute(DeviceManagementConstants.LicenseProperties.PROVIDER, license.getProvider());
artifact.setAttribute(DeviceManagementConstants.LicenseProperties.LANGUAGE, license.getLanguage());
artifact.setAttribute(DeviceManagementConstants.LicenseProperties.TEXT, license.getText());
artifact.setAttribute(DeviceManagementConstants.LicenseProperties.VALID_TO,
license.getValidTo().toString());
artifact.setAttribute(DeviceManagementConstants.LicenseProperties.VALID_FROM,
license.getValidFrom().toString());
artifactManager.addGenericArtifact(artifact);
return true;
} catch (GovernanceException e) {
throw new LicenseManagementException("Error occurred while adding license for device type " +
deviceType + "'", e);
}
} }
} }

@ -18,10 +18,18 @@
*/ */
package org.wso2.carbon.device.mgt.core.operation.mgt; package org.wso2.carbon.device.mgt.core.operation.mgt;
import org.wso2.carbon.device.mgt.common.Operation; import org.wso2.carbon.device.mgt.core.operation.mgt.Operation;
public class CommandOperation extends Operation { public class CommandOperation extends Operation {
private boolean enabled;
public boolean isEnabled() {
return enabled;
}
public void setEnabled(boolean enabled) {
this.enabled = enabled;
}
} }

@ -18,5 +18,59 @@
*/ */
package org.wso2.carbon.device.mgt.core.operation.mgt; package org.wso2.carbon.device.mgt.core.operation.mgt;
public class ConfigOperation { import java.util.ArrayList;
import java.util.List;
public class ConfigOperation extends Operation {
private List<Property> properties;
public ConfigOperation() {
properties = new ArrayList<Property>();
}
public List<Property> getConfigProperties() {
return properties;
}
public void addConfigProperty(String name, Object value, Class<?> type) {
properties.add(new Property(name, value, type));
}
public class Property {
private String name;
private Object value;
private Class<?> type;
public Property(String name, Object value, Class<?> type) {
this.name = name;
this.value = value;
this.type = type;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public Object getValue() {
return value;
}
public void setValue(Object value) {
this.value = value;
}
public Class<?> getType() {
return type;
}
public void setType(Class<?> type) {
this.type = type;
}
}
} }

@ -18,6 +18,7 @@
package org.wso2.carbon.device.mgt.core.operation.mgt; package org.wso2.carbon.device.mgt.core.operation.mgt;
import org.wso2.carbon.device.mgt.common.*; import org.wso2.carbon.device.mgt.common.*;
import org.wso2.carbon.device.mgt.core.dto.DeviceType;
import java.util.List; import java.util.List;
@ -32,7 +33,7 @@ public interface OperationManager {
* *
* @param operation Operation to be added * @param operation Operation to be added
* @param devices List of DeviceIdentifiers to execute the operation * @param devices List of DeviceIdentifiers to execute the operation
* @throws org.wso2.carbon.device.mgt.common.OperationManagementException If some unusual behaviour is observed while adding the * @throws OperationManagementException If some unusual behaviour is observed while adding the
* operation * operation
*/ */
public boolean addOperation(Operation operation, List<DeviceIdentifier> devices) public boolean addOperation(Operation operation, List<DeviceIdentifier> devices)
@ -64,6 +65,6 @@ public interface OperationManager {
* @return a list of Feature objects. * @return a list of Feature objects.
* @throws org.wso2.carbon.device.mgt.common.FeatureManagementException * @throws org.wso2.carbon.device.mgt.common.FeatureManagementException
*/ */
public List<Feature> getFeaturesForDeviceType(String deviceType) throws FeatureManagementException; public List<Feature> getFeatures(String deviceType) throws FeatureManagementException;
} }

@ -18,25 +18,56 @@
*/ */
package org.wso2.carbon.device.mgt.core.operation.mgt; package org.wso2.carbon.device.mgt.core.operation.mgt;
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.*;
import org.wso2.carbon.device.mgt.core.dao.DeviceDAO;
import org.wso2.carbon.device.mgt.core.dao.DeviceManagementDAOFactory;
import org.wso2.carbon.device.mgt.core.operation.mgt.dao.OperationDAO; import org.wso2.carbon.device.mgt.core.operation.mgt.dao.OperationDAO;
import org.wso2.carbon.device.mgt.core.operation.mgt.dao.OperationManagementDAOException; import org.wso2.carbon.device.mgt.core.operation.mgt.dao.OperationManagementDAOException;
import org.wso2.carbon.device.mgt.core.operation.mgt.dao.OperationManagementDAOFactory; import org.wso2.carbon.device.mgt.core.operation.mgt.dao.OperationManagementDAOFactory;
import org.wso2.carbon.device.mgt.core.operation.mgt.dao.OperationMappingDAO;
import java.util.List; import java.util.List;
/**
* This class implements all the functionalities exposed as part of the OperationManager. Any transaction initiated
* upon persisting information related to operation state, etc has to be managed, demarcated and terminated via the
* methods available in OperationManagementDAOFactory.
*/
public class OperationManagerImpl implements OperationManager { public class OperationManagerImpl implements OperationManager {
private OperationDAO commandOperationDAO = OperationManagementDAOFactory.getCommandOperationDAO(); private static final Log log = LogFactory.getLog(OperationManagerImpl.class);
private OperationDAO configOperationDAO = OperationManagementDAOFactory.getConfigOperationDAO();
private OperationDAO simpleOperationDAO = OperationManagementDAOFactory.getSimpleOperationDAO(); private OperationDAO commandOperationDAO;
private OperationDAO configOperationDAO;
private OperationDAO simpleOperationDAO;
private OperationMappingDAO operationMappingDAO;
private DeviceDAO deviceDAO;
public OperationManagerImpl() {
commandOperationDAO = OperationManagementDAOFactory.getCommandOperationDAO();
configOperationDAO = OperationManagementDAOFactory.getConfigOperationDAO();
simpleOperationDAO = OperationManagementDAOFactory.getSimpleOperationDAO();
operationMappingDAO = OperationManagementDAOFactory.getOperationMappingDAO();
deviceDAO = DeviceManagementDAOFactory.getDeviceDAO();
}
@Override @Override
public boolean addOperation(Operation operation, public boolean addOperation(Operation operation,
List<DeviceIdentifier> devices) throws OperationManagementException { List<DeviceIdentifier> devices) throws OperationManagementException {
try { try {
return this.lookupOperationDAO(operation).addOperation(operation); OperationManagementDAOFactory.beginTransaction();
int operationId = this.lookupOperationDAO(operation).addOperation(operation);
operationMappingDAO.addOperationMapping(operationId, null);
OperationManagementDAOFactory.commitTransaction();
return true;
} catch (OperationManagementDAOException e) { } catch (OperationManagementDAOException e) {
try {
OperationManagementDAOFactory.rollbackTransaction();
} catch (OperationManagementDAOException e1) {
log.warn("Error occurred while roll-backing the transaction", e);
}
throw new OperationManagementException("Error occurred while adding operation", e); throw new OperationManagementException("Error occurred while adding operation", e);
} }
} }
@ -52,7 +83,7 @@ public class OperationManagerImpl implements OperationManager {
} }
@Override @Override
public List<Feature> getFeaturesForDeviceType(String deviceType) throws FeatureManagementException { public List<Feature> getFeatures(String deviceType) throws FeatureManagementException {
return null; return null;
} }

@ -18,5 +18,7 @@
*/ */
package org.wso2.carbon.device.mgt.core.operation.mgt; package org.wso2.carbon.device.mgt.core.operation.mgt;
public class SimpleOperation { import org.wso2.carbon.device.mgt.core.operation.mgt.Operation;
public class SimpleOperation extends Operation {
} }

@ -18,5 +18,20 @@
*/ */
package org.wso2.carbon.device.mgt.core.operation.mgt.dao; package org.wso2.carbon.device.mgt.core.operation.mgt.dao;
public class OperationDAO { import org.wso2.carbon.device.mgt.core.operation.mgt.Operation;
import java.util.List;
public interface OperationDAO {
int addOperation(Operation operation) throws OperationManagementDAOException;
int updateOperation(Operation operation) throws OperationManagementDAOException;
int deleteOperation(int id) throws OperationManagementDAOException;
Operation getOperation(int id) throws OperationManagementDAOException;
List<Operation> getOperations() throws OperationManagementDAOException;
} }

@ -20,4 +20,40 @@ package org.wso2.carbon.device.mgt.core.operation.mgt.dao;
public class OperationManagementDAOException extends Exception { public class OperationManagementDAOException extends Exception {
private static final long serialVersionUID = -3151279311929070299L;
private String errorMessage;
public String getErrorMessage() {
return errorMessage;
}
public void setErrorMessage(String errorMessage) {
this.errorMessage = errorMessage;
}
public OperationManagementDAOException(String msg, Exception nestedEx) {
super(msg, nestedEx);
setErrorMessage(msg);
}
public OperationManagementDAOException(String message, Throwable cause) {
super(message, cause);
setErrorMessage(message);
}
public OperationManagementDAOException(String msg) {
super(msg);
setErrorMessage(msg);
}
public OperationManagementDAOException() {
super();
}
public OperationManagementDAOException(Throwable cause) {
super(cause);
}
} }

@ -18,5 +18,130 @@
*/ */
package org.wso2.carbon.device.mgt.core.operation.mgt.dao; package org.wso2.carbon.device.mgt.core.operation.mgt.dao;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
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.dao.util.DeviceManagementDAOUtil;
import org.wso2.carbon.device.mgt.core.operation.mgt.dao.impl.CommandOperationDAOImpl;
import org.wso2.carbon.device.mgt.core.operation.mgt.dao.impl.ConfigOperationDAOImpl;
import org.wso2.carbon.device.mgt.core.operation.mgt.dao.impl.OperationMappingDAOImpl;
import org.wso2.carbon.device.mgt.core.operation.mgt.dao.impl.SimpleOperationDAOImpl;
import javax.sql.DataSource;
import java.sql.Connection;
import java.sql.SQLException;
import java.util.Hashtable;
import java.util.List;
public class OperationManagementDAOFactory { public class OperationManagementDAOFactory {
private static DataSource dataSource;
private static final Log log = LogFactory.getLog(OperationManagementDAOFactory.class);
private static ThreadLocal<Connection> currentConnection = new ThreadLocal<Connection>();
public static OperationDAO getCommandOperationDAO() {
return new CommandOperationDAOImpl(dataSource);
}
public static OperationDAO getConfigOperationDAO() {
return new ConfigOperationDAOImpl(dataSource);
}
public static OperationDAO getSimpleOperationDAO() {
return new SimpleOperationDAOImpl(dataSource);
}
public static OperationMappingDAO getOperationMappingDAO() {
return new OperationMappingDAOImpl();
}
public static void init(DataSource dtSource) {
dataSource = dtSource;
}
public static void beginTransaction() throws OperationManagementDAOException {
try {
currentConnection.set(dataSource.getConnection());
} catch (SQLException e) {
throw new OperationManagementDAOException("Error occurred while retrieving datasource connection", e);
}
}
public static Connection getConnection() {
return currentConnection.get();
}
public static void commitTransaction() throws OperationManagementDAOException {
try {
Connection conn = currentConnection.get();
if (conn != null) {
conn.commit();
} else {
if (log.isDebugEnabled()) {
log.debug("Datasource connection associated with the current thread is null, hence commit " +
"has not been attempted");
}
}
} catch (SQLException e) {
throw new OperationManagementDAOException("Error occurred while committing the transaction", e);
}
}
public static void rollbackTransaction() throws OperationManagementDAOException {
try {
Connection conn = currentConnection.get();
if (conn != null) {
conn.rollback();
} else {
if (log.isDebugEnabled()) {
log.debug("Datasource connection associated with the current thread is null, hence rollback " +
"has not been attempted");
}
}
} catch (SQLException e) {
throw new OperationManagementDAOException("Error occurred while rollbacking the transaction", e);
}
}
/**
* Resolve data source from the data source definition
*
* @param config data source configuration
* @return data source resolved from the data source definition
*/
private 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;
}
public static DataSource getDataSource() {
return dataSource;
}
} }

@ -18,5 +18,32 @@
*/ */
package org.wso2.carbon.device.mgt.core.operation.mgt.dao; package org.wso2.carbon.device.mgt.core.operation.mgt.dao;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
public class OperationManagementDAOUtil { public class OperationManagementDAOUtil {
private static final Log log = LogFactory.getLog(OperationManagementDAOUtil.class);
public static void cleanupResources(Statement stmt, ResultSet rs) {
if (rs != null) {
try {
rs.close();
} catch (SQLException e) {
log.warn("Error occurred while closing the result set", e);
}
}
if (stmt != null) {
try {
stmt.close();
} catch (SQLException e) {
log.warn("Error occurred while closing the statement", e);
}
}
}
} }

@ -18,5 +18,14 @@
*/ */
package org.wso2.carbon.device.mgt.core.operation.mgt.dao; package org.wso2.carbon.device.mgt.core.operation.mgt.dao;
public class OperationMappingDAO { import org.wso2.carbon.device.mgt.common.DeviceIdentifier;
import java.util.List;
public interface OperationMappingDAO {
void addOperationMapping(int operationId, List<Integer> deviceIds) throws OperationManagementDAOException;
void removeOperationMapping(int operationId, List<Integer> deviceIds) throws OperationManagementDAOException;
} }

@ -18,5 +18,53 @@
*/ */
package org.wso2.carbon.device.mgt.core.operation.mgt.dao.impl; package org.wso2.carbon.device.mgt.core.operation.mgt.dao.impl;
public class AbstractOperationDAO { import org.wso2.carbon.device.mgt.core.operation.mgt.Operation;
import org.wso2.carbon.device.mgt.core.operation.mgt.dao.*;
import javax.sql.DataSource;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.Timestamp;
import java.util.Date;
import java.sql.SQLException;
public abstract class AbstractOperationDAO implements OperationDAO {
private DataSource dataSource;
public AbstractOperationDAO(DataSource dataSource) {
this.dataSource = dataSource;
}
public DataSource getDataSource() {
return dataSource;
}
public int addOperation(Operation operation) throws OperationManagementDAOException {
PreparedStatement stmt = null;
ResultSet rs = null;
try {
Connection connection = OperationManagementDAOFactory.getConnection();
stmt = connection.prepareStatement(
"INSERT INTO DM_OPERATION(TYPE, CREATED_TIMESTAMP, RECEIVED_TIMESTAMP, STATUS) VALUES (?, ?, ?, ?)");
stmt.setString(1, operation.getType().toString());
stmt.setTimestamp(2, new Timestamp(new Date().getTime()));
stmt.setTimestamp(3, null);
stmt.setBoolean(4, false);
stmt.executeUpdate();
rs = stmt.getGeneratedKeys();
int id = -1;
if (rs.next()) {
id = rs.getInt(1);
}
return id;
} catch (SQLException e) {
throw new OperationManagementDAOException("Error occurred while adding operation metadata", e);
} finally {
OperationManagementDAOUtil.cleanupResources(stmt, rs);
}
}
} }

@ -18,11 +18,17 @@
*/ */
package org.wso2.carbon.device.mgt.core.operation.mgt.dao.impl; package org.wso2.carbon.device.mgt.core.operation.mgt.dao.impl;
import org.wso2.carbon.device.mgt.common.Operation; import org.wso2.carbon.device.mgt.core.operation.mgt.Operation;
import org.wso2.carbon.device.mgt.core.operation.mgt.CommandOperation; import org.wso2.carbon.device.mgt.core.operation.mgt.CommandOperation;
import org.wso2.carbon.device.mgt.core.operation.mgt.dao.OperationManagementDAOException; import org.wso2.carbon.device.mgt.core.operation.mgt.dao.OperationManagementDAOException;
import org.wso2.carbon.device.mgt.core.operation.mgt.dao.OperationManagementDAOFactory;
import org.wso2.carbon.device.mgt.core.operation.mgt.dao.OperationManagementDAOUtil;
import javax.sql.DataSource; import javax.sql.DataSource;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.List; import java.util.List;
public class CommandOperationDAOImpl extends AbstractOperationDAO { public class CommandOperationDAOImpl extends AbstractOperationDAO {
@ -32,20 +38,34 @@ public class CommandOperationDAOImpl extends AbstractOperationDAO {
} }
@Override @Override
public boolean addOperation(Operation operation) throws OperationManagementDAOException { public int addOperation(Operation operation) throws OperationManagementDAOException {
CommandOperation booleanOp = (CommandOperation) operation; int operationId = super.addOperation(operation);
addOperationMetadata(); CommandOperation commandOp = (CommandOperation) operation;
return false; Connection conn = OperationManagementDAOFactory.getConnection();
PreparedStatement stmt = null;
ResultSet rs = null;
try {
stmt = conn.prepareStatement("INSERT INTO DM_COMMAND_OPERATION(OPERATION_ID, ENABLED) VALUES(?, ?)");
stmt.setInt(1, operationId);
stmt.setBoolean(2, commandOp.isEnabled());
stmt.executeUpdate();
} catch (SQLException e) {
throw new OperationManagementDAOException("Error occurred while adding command operation", e);
} finally {
OperationManagementDAOUtil.cleanupResources(stmt, rs);
}
return operationId;
} }
@Override @Override
public boolean updateOperation(Operation operation) throws OperationManagementDAOException { public int updateOperation(Operation operation) throws OperationManagementDAOException {
return false; return 0;
} }
@Override @Override
public boolean deleteOperation(int id) throws OperationManagementDAOException { public int deleteOperation(int id) throws OperationManagementDAOException {
return false; return 0;
} }
@Override @Override

@ -18,5 +18,42 @@
*/ */
package org.wso2.carbon.device.mgt.core.operation.mgt.dao.impl; package org.wso2.carbon.device.mgt.core.operation.mgt.dao.impl;
public class ConfigOperationDAOImpl { import org.wso2.carbon.device.mgt.core.operation.mgt.Operation;
import org.wso2.carbon.device.mgt.core.operation.mgt.dao.OperationDAO;
import org.wso2.carbon.device.mgt.core.operation.mgt.dao.OperationManagementDAOException;
import javax.sql.DataSource;
import java.util.List;
public class ConfigOperationDAOImpl extends AbstractOperationDAO {
public ConfigOperationDAOImpl(DataSource dataSource) {
super(dataSource);
}
@Override
public int addOperation(Operation operation) throws OperationManagementDAOException {
return 0;
}
@Override
public int updateOperation(Operation operation) throws OperationManagementDAOException {
return 0;
}
@Override
public int deleteOperation(int id) throws OperationManagementDAOException {
return 0;
}
@Override
public Operation getOperation(int id) throws OperationManagementDAOException {
return null;
}
@Override
public List<Operation> getOperations() throws OperationManagementDAOException {
return null;
}
} }

@ -18,5 +18,51 @@
*/ */
package org.wso2.carbon.device.mgt.core.operation.mgt.dao.impl; package org.wso2.carbon.device.mgt.core.operation.mgt.dao.impl;
public class OperationMappingDAOImpl { import org.wso2.carbon.device.mgt.core.operation.mgt.dao.OperationManagementDAOException;
import org.wso2.carbon.device.mgt.core.operation.mgt.dao.OperationManagementDAOFactory;
import org.wso2.carbon.device.mgt.core.operation.mgt.dao.OperationManagementDAOUtil;
import org.wso2.carbon.device.mgt.core.operation.mgt.dao.OperationMappingDAO;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.SQLException;
import java.util.List;
public class OperationMappingDAOImpl implements OperationMappingDAO {
@Override
public void addOperationMapping(int operationId, List<Integer> deviceIds) throws OperationManagementDAOException {
PreparedStatement stmt = null;
try {
Connection conn = OperationManagementDAOFactory.getConnection();
String sql = "INSERT INTO DEVICE_OPERATION_MAPPING(DEVICE_ID, OPERATION_ID) VALUES(?, ?)";
stmt = conn.prepareStatement(sql);
stmt.setInt(1, 0);
stmt.setInt(2, operationId);
stmt.executeUpdate();
} catch (SQLException e) {
throw new OperationManagementDAOException("Error occurred while persisting device operation mappings", e);
} finally {
OperationManagementDAOUtil.cleanupResources(stmt, null);
}
}
@Override
public void removeOperationMapping(int operationId,
List<Integer> deviceIds) throws OperationManagementDAOException {
PreparedStatement stmt = null;
try {
Connection conn = OperationManagementDAOFactory.getConnection();
String sql = "DELETE FROM DEVICE_OPERATION_MAPPING WHERE DEVICE_ID = ? AND OPERATION_ID = ?";
stmt = conn.prepareStatement(sql);
stmt.setInt(1, 0);
stmt.setInt(2, operationId);
stmt.executeUpdate();
} catch (SQLException e) {
throw new OperationManagementDAOException("Error occurred while persisting device operation mappings", e);
} finally {
OperationManagementDAOUtil.cleanupResources(stmt, null);
}
}
} }

@ -18,5 +18,36 @@
*/ */
package org.wso2.carbon.device.mgt.core.operation.mgt.dao.impl; package org.wso2.carbon.device.mgt.core.operation.mgt.dao.impl;
public class SimpleOperationDAOImpl { import org.wso2.carbon.device.mgt.core.operation.mgt.Operation;
import org.wso2.carbon.device.mgt.core.operation.mgt.dao.OperationManagementDAOException;
import javax.sql.DataSource;
import java.util.List;
public class SimpleOperationDAOImpl extends AbstractOperationDAO {
public SimpleOperationDAOImpl(DataSource dataSource) {
super(dataSource);
}
@Override
public int updateOperation(Operation operation) throws OperationManagementDAOException {
return 0;
}
@Override
public int deleteOperation(int id) throws OperationManagementDAOException {
return 0;
}
@Override
public Operation getOperation(int id) throws OperationManagementDAOException {
return null;
}
@Override
public List<Operation> getOperations() throws OperationManagementDAOException {
return null;
}
} }

@ -17,71 +17,111 @@
*/ */
package org.wso2.carbon.device.mgt.core.service; package org.wso2.carbon.device.mgt.core.service;
import org.wso2.carbon.device.mgt.common.Device; import org.wso2.carbon.device.mgt.common.*;
import org.wso2.carbon.device.mgt.common.DeviceIdentifier; import org.wso2.carbon.device.mgt.core.DeviceManagementService;
import org.wso2.carbon.device.mgt.common.DeviceManagementException; import org.wso2.carbon.device.mgt.core.config.license.License;
import org.wso2.carbon.device.mgt.core.operation.mgt.OperationManager; import org.wso2.carbon.device.mgt.core.license.mgt.LicenseManagementException;
import org.wso2.carbon.device.mgt.core.DeviceManager; import org.wso2.carbon.device.mgt.core.operation.mgt.Operation;
import org.wso2.carbon.device.mgt.core.operation.mgt.OperationManagementException;
import org.wso2.carbon.device.mgt.core.internal.DeviceManagementDataHolder; import org.wso2.carbon.device.mgt.core.internal.DeviceManagementDataHolder;
import java.util.List; import java.util.List;
public class DeviceManagementServiceImpl implements DeviceManager { public class DeviceManagementServiceImpl implements DeviceManagementService {
@Override
public String getProviderType() {
return null;
}
@Override @Override
public boolean enrollDevice(Device device) throws DeviceManagementException { public boolean enrollDevice(Device device) throws DeviceManagementException {
return DeviceManagementDataHolder.getInstance().getDeviceManager().enrollDevice(device); return DeviceManagementDataHolder.getInstance().getDeviceManagementProvider().enrollDevice(device);
} }
@Override @Override
public boolean modifyEnrollment(Device device) throws DeviceManagementException { public boolean modifyEnrollment(Device device) throws DeviceManagementException {
return DeviceManagementDataHolder.getInstance().getDeviceManager().modifyEnrollment(device); return DeviceManagementDataHolder.getInstance().getDeviceManagementProvider().modifyEnrollment(device);
} }
@Override @Override
public boolean disenrollDevice(DeviceIdentifier deviceId) throws DeviceManagementException { public boolean disenrollDevice(DeviceIdentifier deviceId) throws DeviceManagementException {
return DeviceManagementDataHolder.getInstance().getDeviceManager().disenrollDevice(deviceId); return DeviceManagementDataHolder.getInstance().getDeviceManagementProvider().disenrollDevice(deviceId);
} }
@Override @Override
public boolean isEnrolled(DeviceIdentifier deviceId) throws DeviceManagementException { public boolean isEnrolled(DeviceIdentifier deviceId) throws DeviceManagementException {
return DeviceManagementDataHolder.getInstance().getDeviceManager().isEnrolled(deviceId); return DeviceManagementDataHolder.getInstance().getDeviceManagementProvider().isEnrolled(deviceId);
} }
@Override @Override
public boolean isActive(DeviceIdentifier deviceId) throws DeviceManagementException { public boolean isActive(DeviceIdentifier deviceId) throws DeviceManagementException {
return DeviceManagementDataHolder.getInstance().getDeviceManager().isActive(deviceId); return DeviceManagementDataHolder.getInstance().getDeviceManagementProvider().isActive(deviceId);
} }
@Override @Override
public boolean setActive(DeviceIdentifier deviceId, boolean status) throws DeviceManagementException { public boolean setActive(DeviceIdentifier deviceId, boolean status) throws DeviceManagementException {
return DeviceManagementDataHolder.getInstance().getDeviceManager().setActive(deviceId, status); return DeviceManagementDataHolder.getInstance().getDeviceManagementProvider().setActive(deviceId, status);
}
@Override
public List<Device> getAllDevices() throws DeviceManagementException {
return null; //To change body of implemented methods use File | Settings | File Templates.
} }
@Override @Override
public List<Device> getAllDevices(String type) throws DeviceManagementException { public List<Device> getAllDevices(String type) throws DeviceManagementException {
return DeviceManagementDataHolder.getInstance().getDeviceManager().getAllDevices(type); return DeviceManagementDataHolder.getInstance().getDeviceManagementProvider().getAllDevices(type);
} }
@Override @Override
public org.wso2.carbon.device.mgt.common.Device getDevice(DeviceIdentifier deviceId) public org.wso2.carbon.device.mgt.common.Device getDevice(DeviceIdentifier deviceId)
throws DeviceManagementException { throws DeviceManagementException {
return DeviceManagementDataHolder.getInstance().getDeviceManager().getDevice(deviceId); return DeviceManagementDataHolder.getInstance().getDeviceManagementProvider().getDevice(deviceId);
} }
@Override @Override
public boolean updateDeviceInfo(Device device) throws DeviceManagementException { public boolean updateDeviceInfo(Device device) throws DeviceManagementException {
return DeviceManagementDataHolder.getInstance().getDeviceManager().updateDeviceInfo(device); return DeviceManagementDataHolder.getInstance().getDeviceManagementProvider().updateDeviceInfo(device);
} }
@Override @Override
public boolean setOwnership(DeviceIdentifier deviceId, String ownershipType) throws DeviceManagementException { public boolean setOwnership(DeviceIdentifier deviceId, String ownershipType) throws DeviceManagementException {
return DeviceManagementDataHolder.getInstance().getDeviceManager().setOwnership(deviceId, ownershipType); return DeviceManagementDataHolder.getInstance().getDeviceManagementProvider().setOwnership(deviceId,
ownershipType);
}
@Override
public License getLicense(String deviceType, String languageCode) throws LicenseManagementException {
return DeviceManagementDataHolder.getInstance().getDeviceManagementProvider().getLicense(deviceType,
languageCode);
}
@Override
public boolean addLicense(String type, License license) throws LicenseManagementException {
return DeviceManagementDataHolder.getInstance().getDeviceManagementProvider().addLicense(type, license);
}
@Override
public boolean addOperation(Operation operation,
List<DeviceIdentifier> devices) throws OperationManagementException {
return DeviceManagementDataHolder.getInstance().getDeviceManagementProvider().addOperation(operation, devices);
}
@Override
public List<Operation> getOperations(DeviceIdentifier deviceId) throws OperationManagementException {
return DeviceManagementDataHolder.getInstance().getDeviceManagementProvider().getOperations(deviceId);
}
@Override
public List<Operation> getPendingOperations(DeviceIdentifier deviceId) throws OperationManagementException {
return DeviceManagementDataHolder.getInstance().getDeviceManagementProvider().getPendingOperations(deviceId);
} }
@Override @Override
public OperationManager getOperationManager(String type) throws DeviceManagementException { public List<Feature> getFeaturesForDeviceType(String deviceType) throws FeatureManagementException {
return DeviceManagementDataHolder.getInstance().getDeviceManager().getOperationManager(type); return DeviceManagementDataHolder.getInstance().getDeviceManagementProvider().getFeaturesForDeviceType(
deviceType);
} }
} }

@ -50,9 +50,8 @@ public final class DeviceManagerUtil {
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( throw new DeviceManagementException("Error occurred while parsing file, while converting " +
"Error occurred while parsing file, while converting " + "to a org.w3c.dom.Document", e);
"to a org.w3c.dom.Document : " + e.getMessage(), e);
} }
} }
@ -65,16 +64,13 @@ public final class DeviceManagerUtil {
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( throw new RuntimeException("Device Management Repository data source configuration is null and thus, " +
"Device Management Repository data source configuration " + "is not initialized");
"is null and thus, is not initialized");
} }
JNDILookupDefinition jndiConfig = config.getJndiLookupDefinition(); JNDILookupDefinition jndiConfig = config.getJndiLookupDefinition();
if (jndiConfig != null) { if (jndiConfig != null) {
if (log.isDebugEnabled()) { if (log.isDebugEnabled()) {
log.debug( log.debug("Initializing Device Management Repository data source using the JNDI 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();
@ -83,11 +79,9 @@ public final class DeviceManagerUtil {
for (JNDILookupDefinition.JNDIProperty prop : jndiPropertyList) { for (JNDILookupDefinition.JNDIProperty prop : jndiPropertyList) {
jndiProperties.put(prop.getName(), prop.getValue()); jndiProperties.put(prop.getName(), prop.getValue());
} }
dataSource = DeviceManagementDAOUtil dataSource = DeviceManagementDAOUtil.lookupDataSource(jndiConfig.getJndiName(), jndiProperties);
.lookupDataSource(jndiConfig.getJndiName(), jndiProperties);
} else { } else {
dataSource = dataSource = DeviceManagementDAOUtil.lookupDataSource(jndiConfig.getJndiName(), null);
DeviceManagementDAOUtil.lookupDataSource(jndiConfig.getJndiName(), null);
} }
} }
return dataSource; return dataSource;
@ -103,7 +97,7 @@ public final class DeviceManagerUtil {
boolean status; boolean status;
try { try {
DeviceTypeDAO deviceTypeDAO = DeviceManagementDAOFactory.getDeviceTypeDAO(); DeviceTypeDAO deviceTypeDAO = DeviceManagementDAOFactory.getDeviceTypeDAO();
Integer deviceTypeId = deviceTypeDAO.getDeviceTypeIdByDeviceTypeName(deviceType); DeviceType deviceTypeId = deviceTypeDAO.getDeviceType(deviceType);
if (deviceTypeId == null) { if (deviceTypeId == null) {
DeviceType dt = new DeviceType(); DeviceType dt = new DeviceType();
dt.setName(deviceType); dt.setName(deviceType);
@ -111,14 +105,14 @@ public final class DeviceManagerUtil {
} }
status = true; status = true;
} catch (DeviceManagementDAOException e) { } catch (DeviceManagementDAOException e) {
String msg = "Error occurred while registering the device type " + deviceType; throw new DeviceManagementException("Error occurred while registering the device type '" +
throw new DeviceManagementException(msg, e); deviceType + "'", e);
} }
return status; return status;
} }
/** /**
* Unregisters an existing device type from the device management metadata repository. * Un-registers 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
@ -126,25 +120,17 @@ public final class DeviceManagerUtil {
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); DeviceType deviceTypeId = deviceTypeDAO.getDeviceType(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(deviceType);
} }
return true; return true;
} catch (DeviceManagementDAOException e) { } catch (DeviceManagementDAOException e) {
String msg = "Error occurred while registering the device type " + deviceType; throw new DeviceManagementException("Error occurred while registering the device type '" +
throw new DeviceManagementException(msg, e); deviceType + "'", 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;
}
} }

@ -18,5 +18,53 @@
*/ */
package org.wso2.carbon.device.mgt.core; package org.wso2.carbon.device.mgt.core;
import org.apache.tomcat.jdbc.pool.PoolProperties;
import org.testng.Assert;
import javax.sql.DataSource;
import java.sql.Connection;
import java.sql.SQLException;
import java.sql.Statement;
public class DeviceManagementBaseTest { public class DeviceManagementBaseTest {
private DataSource dataSource;
public void init() {
this.initDataSource();
try {
this.initDeviceManagementDatabaseSchema();
} catch (SQLException e) {
Assert.fail("Error occurred while initializing database schema", e);
}
}
private void initDeviceManagementDatabaseSchema() throws SQLException {
Connection conn = null;
Statement stmt = null;
try {
if (dataSource == null) {
Assert.fail("Device management datasource is not initialized peroperly");
}
conn = dataSource.getConnection();
stmt = conn.createStatement();
stmt.executeUpdate("RUNSCRIPT FROM './src/test/resources/sql/h2.sql'");
} finally {
TestUtils.cleanupResources(conn, stmt, null);
}
}
private void initDataSource() {
PoolProperties properties = new PoolProperties();
properties.setUrl("jdbc:h2:mem:MDM_DB;DB_CLOSE_DELAY=-1");
properties.setDriverClassName("org.h2.Driver");
properties.setUsername("wso2carbon");
properties.setPassword("wso2carbon");
this.dataSource = new org.apache.tomcat.jdbc.pool.DataSource(properties);
}
protected DataSource getDataSource() {
return dataSource;
}
} }

@ -20,7 +20,8 @@ package org.wso2.carbon.device.mgt.core;
import org.testng.Assert; import org.testng.Assert;
import org.testng.annotations.BeforeClass; import org.testng.annotations.BeforeClass;
import org.testng.annotations.Test; import org.testng.annotations.Test;
import org.wso2.carbon.device.mgt.common.spi.DeviceManagerService; import org.wso2.carbon.device.mgt.common.DeviceManagementException;
import org.wso2.carbon.device.mgt.common.spi.DeviceManager;
public class DeviceManagementRepositoryTests { public class DeviceManagementRepositoryTests {
@ -33,23 +34,27 @@ public class DeviceManagementRepositoryTests {
@Test @Test
public void testAddDeviceManagementService() { public void testAddDeviceManagementService() {
DeviceManagerService sourceProvider = new TestDeviceManagerService(); DeviceManager sourceProvider = new TestDeviceManager();
try {
this.getRepository().addDeviceManagementProvider(sourceProvider); this.getRepository().addDeviceManagementProvider(sourceProvider);
} catch (DeviceManagementException e) {
DeviceManagerService targetProvider = Assert.fail("Unexpected error occurred while invoking addDeviceManagementProvider functionality", e);
this.getRepository().getDeviceManagementProvider(TestDeviceManagerService.DEVICE_TYPE_TEST); }
DeviceManager targetProvider =
this.getRepository().getDeviceManagementProvider(TestDeviceManager.DEVICE_TYPE_TEST);
Assert.assertEquals(targetProvider.getProviderType(), sourceProvider.getProviderType()); Assert.assertEquals(targetProvider.getProviderType(), sourceProvider.getProviderType());
} }
@Test(dependsOnMethods = "testAddDeviceManagementService") @Test(dependsOnMethods = "testAddDeviceManagementService")
public void testRemoveDeviceManagementService() { public void testRemoveDeviceManagementService() {
DeviceManagerService sourceProvider = new TestDeviceManagerService(); DeviceManager sourceProvider = new TestDeviceManager();
try {
this.getRepository().removeDeviceManagementProvider(sourceProvider); this.getRepository().removeDeviceManagementProvider(sourceProvider);
} catch (DeviceManagementException e) {
DeviceManagerService targetProvider = Assert.fail("Unexpected error occurred while invoking removeDeviceManagementProvider functionality", e);
this.getRepository().getDeviceManagementProvider(TestDeviceManagerService.DEVICE_TYPE_TEST); }
DeviceManager targetProvider =
this.getRepository().getDeviceManagementProvider(TestDeviceManager.DEVICE_TYPE_TEST);
Assert.assertNull(targetProvider); Assert.assertNull(targetProvider);
} }

@ -18,5 +18,59 @@
*/ */
package org.wso2.carbon.device.mgt.core; package org.wso2.carbon.device.mgt.core;
public class DeviceOperationManagementTests { import org.testng.annotations.BeforeClass;
import org.testng.annotations.Test;
import org.wso2.carbon.device.mgt.common.DeviceIdentifier;
import org.wso2.carbon.device.mgt.core.dto.Device;
import org.wso2.carbon.device.mgt.core.operation.mgt.*;
import org.wso2.carbon.device.mgt.core.operation.mgt.dao.OperationManagementDAOFactory;
import java.util.ArrayList;
import java.util.List;
public class DeviceOperationManagementTests extends DeviceManagementBaseTest {
private OperationManager operationManager;
@BeforeClass(alwaysRun = true)
public void init() {
super.init();
this.initOperationManager();
OperationManagementDAOFactory.init(this.getDataSource());
}
public void initOperationManager() {
this.operationManager = new OperationManagerImpl();
}
@Test
public void testAddOperation() throws Exception {
CommandOperation op = new CommandOperation();
op.setEnabled(true);
op.setType(Operation.Type.COMMAND);
List<DeviceIdentifier> deviceIds = new ArrayList<DeviceIdentifier>();
DeviceIdentifier deviceId = new DeviceIdentifier();
deviceId.setId("Test");
deviceId.setType("Android");
deviceIds.add(deviceId);
try {
operationManager.addOperation(op, deviceIds);
} catch (OperationManagementException e) {
e.printStackTrace();
throw new Exception(e);
}
}
public void testGetOperations() {
try {
operationManager.getOperations(null);
} catch (OperationManagementException e) {
e.printStackTrace();
}
}
} }

@ -114,17 +114,17 @@ public class DeviceManagementDAOTests {
deviceType.setName("IOS"); deviceType.setName("IOS");
deviceTypeMgtDAO.addDeviceType(deviceType); deviceTypeMgtDAO.addDeviceType(deviceType);
Long id = null; int id = -1;
Connection conn = null; Connection conn = null;
PreparedStatement stmt = null; PreparedStatement stmt = null;
String sql = "SELECT ID, NAME from DM_DEVICE_TYPE DType where DType.NAME='IOS'"; String sql = "SELECT dt.ID, dt.NAME FROM DM_DEVICE_TYPE dt where dt.NAME = 'IOS'";
try { try {
conn = this.getDataSource().getConnection(); conn = this.getDataSource().getConnection();
stmt = conn.prepareStatement(sql); stmt = conn.prepareStatement(sql);
ResultSet rs = stmt.executeQuery(); ResultSet rs = stmt.executeQuery();
if (rs.next()) { if (rs.next()) {
id = rs.getLong("ID"); id = rs.getInt("ID");
} }
} catch (SQLException e) { } catch (SQLException e) {
throw new DeviceManagementDAOException("error in fetch device type by name IOS", e); throw new DeviceManagementDAOException("error in fetch device type by name IOS", e);
@ -147,9 +147,9 @@ public class DeviceManagementDAOTests {
device.setDeviceIdentificationId("111"); device.setDeviceIdentificationId("111");
DeviceType deviceType = new DeviceType(); DeviceType deviceType = new DeviceType();
deviceType.setId(Long.parseLong("1")); deviceType.setId(Integer.parseInt("1"));
device.setDeviceTypeId(deviceType.getId().intValue()); device.setDeviceTypeId(deviceType.getId());
device.setOwnerShip(OwnerShip.BYOD.toString()); device.setOwnerShip(OwnerShip.BYOD.toString());
device.setOwnerId("111"); device.setOwnerId("111");
device.setTenantId(-1234); device.setTenantId(-1234);

@ -0,0 +1,58 @@
CREATE TABLE IF NOT EXISTS DM_DEVICE_TYPE (
ID INT auto_increment NOT NULL,
NAME VARCHAR(300) NULL DEFAULT NULL,
PRIMARY KEY (ID)
);
CREATE TABLE IF NOT EXISTS DM_DEVICE (
ID INTEGER auto_increment NOT NULL,
DESCRIPTION TEXT NULL DEFAULT NULL,
NAME VARCHAR(100) NULL DEFAULT NULL,
DATE_OF_ENROLLMENT BIGINT NULL DEFAULT NULL,
DATE_OF_LAST_UPDATE BIGINT NULL DEFAULT NULL,
OWNERSHIP VARCHAR(45) NULL DEFAULT NULL,
STATUS VARCHAR(15) NULL DEFAULT NULL,
DEVICE_TYPE_ID INT(11) NULL DEFAULT NULL,
DEVICE_IDENTIFICATION VARCHAR(300) NULL DEFAULT NULL,
OWNER VARCHAR(45) NULL DEFAULT NULL,
TENANT_ID INTEGER DEFAULT 0,
PRIMARY KEY (ID),
CONSTRAINT fk_DM_DEVICE_DM_DEVICE_TYPE2 FOREIGN KEY (DEVICE_TYPE_ID )
REFERENCES DM_DEVICE_TYPE (ID) ON DELETE NO ACTION ON UPDATE NO ACTION
);
CREATE TABLE IF NOT EXISTS DM_OPERATION (
ID INTEGER AUTO_INCREMENT NOT NULL,
TYPE VARCHAR(50) NOT NULL,
CREATED_TIMESTAMP TIMESTAMP NOT NULL,
RECEIVED_TIMESTAMP TIMESTAMP NULL,
STATUS VARCHAR(50) NULL,
PRIMARY KEY (ID)
)
CREATE TABLE IF NOT EXISTS DM_CONFIG_OPERATION (
OPERATION_ID INTEGER NOT NULL,
PRIMARY KEY (OPERATION_ID),
CONSTRAINT fk_dm_operation_config FOREIGN KEY (OPERATION_ID) REFERENCES
DM_OPERATION (ID) ON DELETE NO ACTION ON UPDATE NO ACTION
)
CREATE TABLE IF NOT EXISTS DM_COMMAND_OPERATION (
OPERATION_ID INTEGER NOT NULL,
ENABLED INTEGER NOT NULL DEFAULT 0,
PRIMARY KEY (OPERATION_ID),
CONSTRAINT fk_dm_operation_command FOREIGN KEY (OPERATION_ID) REFERENCES
DM_OPERATION (ID) ON DELETE NO ACTION ON UPDATE NO ACTION
)
CREATE TABLE IF NOT EXISTS DM_DEVICE_OPERATION_MAPPING (
ID INTEGER AUTO_INCREMENT NOT NULL,
DEVICE_ID INTEGER NOT NULL,
OPERATION_ID INTEGER NOT NULL,
PRIMARY KEY (ID),
CONSTRAINT fk_dm_device_operation_mapping_device FOREIGN KEY (DEVICE_ID) REFERENCES
DM_DEVICE (ID) ON DELETE NO ACTION ON UPDATE NO ACTION
CONSTRAINT fk_dm_device_operation_mapping_operation FOREIGN KEY (OPERATION_ID) REFERENCES
DM_OPERATION (ID) ON DELETE NO ACTION ON UPDATE NO ACTION
)

@ -28,6 +28,7 @@
<class name="org.wso2.carbon.device.mgt.core.dao.DeviceManagementDAOTests"/> <class name="org.wso2.carbon.device.mgt.core.dao.DeviceManagementDAOTests"/>
<class name="org.wso2.carbon.device.mgt.core.DeviceManagementRepositoryTests"/> <class name="org.wso2.carbon.device.mgt.core.DeviceManagementRepositoryTests"/>
<class name="org.wso2.carbon.device.mgt.core.DeviceManagementConfigTests"/> <class name="org.wso2.carbon.device.mgt.core.DeviceManagementConfigTests"/>
<class name="org.wso2.carbon.device.mgt.core.DeviceOperationManagementTests"/>
</classes> </classes>
</test> </test>
</suite> </suite>

@ -93,7 +93,7 @@
<artifactId>org.wso2.carbon.base</artifactId> <artifactId>org.wso2.carbon.base</artifactId>
<version>${carbon.kernel.version}</version> <version>${carbon.kernel.version}</version>
</dependency> </dependency>
<!--dependency> <dependency>
<groupId>org.wso2.carbon.governance</groupId> <groupId>org.wso2.carbon.governance</groupId>
<artifactId>org.wso2.carbon.governance.api</artifactId> <artifactId>org.wso2.carbon.governance.api</artifactId>
<version>${carbon.governance.version}</version> <version>${carbon.governance.version}</version>
@ -143,7 +143,7 @@
<artifactId>org.wso2.carbon.registry.extensions</artifactId> <artifactId>org.wso2.carbon.registry.extensions</artifactId>
</exclusion> </exclusion>
</exclusions> </exclusions>
</dependency--> </dependency>
<!-- End of Governance dependencies --> <!-- End of Governance dependencies -->
<!-- OSGi dependencies--> <!-- OSGi dependencies-->

Loading…
Cancel
Save