From 31b931537f44963ce596d9b04141313696b6d384 Mon Sep 17 00:00:00 2001 From: prabathabey Date: Tue, 17 Mar 2015 17:30:30 +0530 Subject: [PATCH] Adding operation and license management implementations --- .../device/mgt/common/DeviceIdentifier.java | 3 +- .../org.wso2.carbon.device.mgt.core/pom.xml | 9 +- .../mgt/core/DeviceManagementRepository.java | 22 +- .../mgt/core/DeviceManagementService.java | 27 +- .../DeviceManagementServiceProviderImpl.java | 368 +++++++++--------- .../carbon/device/mgt/core/dao/DeviceDAO.java | 20 +- .../core/dao/DeviceManagementDAOFactory.java | 1 + .../device/mgt/core/dao/DeviceTypeDAO.java | 6 +- .../mgt/core/dao/impl/DeviceDAOImpl.java | 341 ++++++++-------- .../mgt/core/dao/impl/DeviceTypeDAOImpl.java | 95 ++--- .../carbon/device/mgt/core/dto/Device.java | 8 +- .../device/mgt/core/dto/DeviceType.java | 6 +- .../internal/DeviceManagementDataHolder.java | 12 +- .../DeviceManagementServiceComponent.java | 98 ++--- .../mgt/GenericArtifactManagerFactory.java | 74 +++- .../license/mgt/LicenseManagementService.java | 4 +- .../mgt/core/license/mgt/LicenseManager.java | 4 +- .../core/license/mgt/LicenseManagerImpl.java | 128 ++++-- .../core/operation/mgt/CommandOperation.java | 10 +- .../core/operation/mgt/ConfigOperation.java | 56 ++- .../core/operation/mgt/OperationManager.java | 5 +- .../operation/mgt/OperationManagerImpl.java | 41 +- .../core/operation/mgt/SimpleOperation.java | 4 +- .../core/operation/mgt/dao/OperationDAO.java | 17 +- .../dao/OperationManagementDAOException.java | 36 ++ .../dao/OperationManagementDAOFactory.java | 125 ++++++ .../mgt/dao/OperationManagementDAOUtil.java | 27 ++ .../mgt/dao/OperationMappingDAO.java | 11 +- .../mgt/dao/impl/AbstractOperationDAO.java | 50 ++- .../mgt/dao/impl/CommandOperationDAOImpl.java | 38 +- .../mgt/dao/impl/ConfigOperationDAOImpl.java | 39 +- .../mgt/dao/impl/OperationMappingDAOImpl.java | 48 ++- .../mgt/dao/impl/SimpleOperationDAOImpl.java | 33 +- .../service/DeviceManagementServiceImpl.java | 84 ++-- .../mgt/core/util/DeviceManagerUtil.java | 46 +-- .../mgt/core/DeviceManagementBaseTest.java | 48 +++ .../core/DeviceManagementRepositoryTests.java | 31 +- .../core/DeviceOperationManagementTests.java | 56 ++- .../core/dao/DeviceManagementDAOTests.java | 10 +- .../src/test/resources/sql/h2.sql | 58 +++ .../src/test/resources/testng.xml | 1 + pom.xml | 4 +- 42 files changed, 1460 insertions(+), 644 deletions(-) diff --git a/components/device-mgt/org.wso2.carbon.device.mgt.common/src/main/java/org/wso2/carbon/device/mgt/common/DeviceIdentifier.java b/components/device-mgt/org.wso2.carbon.device.mgt.common/src/main/java/org/wso2/carbon/device/mgt/common/DeviceIdentifier.java index 8ac709519a..351fd1656b 100644 --- a/components/device-mgt/org.wso2.carbon.device.mgt.common/src/main/java/org/wso2/carbon/device/mgt/common/DeviceIdentifier.java +++ b/components/device-mgt/org.wso2.carbon.device.mgt.common/src/main/java/org/wso2/carbon/device/mgt/common/DeviceIdentifier.java @@ -19,9 +19,8 @@ package org.wso2.carbon.device.mgt.common; public class DeviceIdentifier { - private String type; - private String id; + private String type; public String getType() { return type; diff --git a/components/device-mgt/org.wso2.carbon.device.mgt.core/pom.xml b/components/device-mgt/org.wso2.carbon.device.mgt.core/pom.xml index 8babf3187b..3d7c7ed651 100644 --- a/components/device-mgt/org.wso2.carbon.device.mgt.core/pom.xml +++ b/components/device-mgt/org.wso2.carbon.device.mgt.core/pom.xml @@ -71,7 +71,10 @@ org.wso2.carbon.registry.core.exceptions, org.wso2.carbon.registry.core.service, 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 !org.wso2.carbon.device.mgt.core.internal, @@ -152,6 +155,10 @@ org.wso2.carbon org.wso2.carbon.base + + org.wso2.carbon.governance + org.wso2.carbon.governance.api + diff --git a/components/device-mgt/org.wso2.carbon.device.mgt.core/src/main/java/org/wso2/carbon/device/mgt/core/DeviceManagementRepository.java b/components/device-mgt/org.wso2.carbon.device.mgt.core/src/main/java/org/wso2/carbon/device/mgt/core/DeviceManagementRepository.java index db8aaaeacb..048317a1e7 100644 --- a/components/device-mgt/org.wso2.carbon.device.mgt.core/src/main/java/org/wso2/carbon/device/mgt/core/DeviceManagementRepository.java +++ b/components/device-mgt/org.wso2.carbon.device.mgt.core/src/main/java/org/wso2/carbon/device/mgt/core/DeviceManagementRepository.java @@ -20,7 +20,8 @@ package org.wso2.carbon.device.mgt.core; import org.apache.commons.logging.Log; import org.apache.commons.logging.LogFactory; 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 java.util.HashMap; @@ -28,35 +29,36 @@ import java.util.Map; public class DeviceManagementRepository { - private static final Log log = LogFactory.getLog(DeviceManagerUtil.class); - private Map providers; + private Map providers; public DeviceManagementRepository() { - providers = new HashMap(); + providers = new HashMap(); } - public void addDeviceManagementProvider(DeviceManagerService provider) { + public void addDeviceManagementProvider(DeviceManager provider) throws DeviceManagementException { String deviceType = provider.getProviderType(); try { DeviceManagerUtil.registerDeviceType(deviceType); } 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); } - public void removeDeviceManagementProvider(DeviceManagerService provider) { + public void removeDeviceManagementProvider(DeviceManager provider) throws DeviceManagementException { String deviceType = provider.getProviderType(); - try { DeviceManagerUtil.unregisterDeviceType(deviceType); } 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); } - public DeviceManagerService getDeviceManagementProvider(String type) { + public DeviceManager getDeviceManagementProvider(String type) { return providers.get(type); } + } diff --git a/components/device-mgt/org.wso2.carbon.device.mgt.core/src/main/java/org/wso2/carbon/device/mgt/core/DeviceManagementService.java b/components/device-mgt/org.wso2.carbon.device.mgt.core/src/main/java/org/wso2/carbon/device/mgt/core/DeviceManagementService.java index bef3adf5d5..9521daf7e6 100644 --- a/components/device-mgt/org.wso2.carbon.device.mgt.core/src/main/java/org/wso2/carbon/device/mgt/core/DeviceManagementService.java +++ b/components/device-mgt/org.wso2.carbon.device.mgt.core/src/main/java/org/wso2/carbon/device/mgt/core/DeviceManagementService.java @@ -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.DeviceIdentifier; 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.license.mgt.LicenseManager; 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 * and resolve the appropriate plugin implementation */ -public interface DeviceManagementService { - - 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; +public interface DeviceManagementService extends DeviceManager, LicenseManager, OperationManager { List 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; - } diff --git a/components/device-mgt/org.wso2.carbon.device.mgt.core/src/main/java/org/wso2/carbon/device/mgt/core/DeviceManagementServiceProviderImpl.java b/components/device-mgt/org.wso2.carbon.device.mgt.core/src/main/java/org/wso2/carbon/device/mgt/core/DeviceManagementServiceProviderImpl.java index 0c21ccd7e4..c5421a6b6c 100644 --- a/components/device-mgt/org.wso2.carbon.device.mgt.core/src/main/java/org/wso2/carbon/device/mgt/core/DeviceManagementServiceProviderImpl.java +++ b/components/device-mgt/org.wso2.carbon.device.mgt.core/src/main/java/org/wso2/carbon/device/mgt/core/DeviceManagementServiceProviderImpl.java @@ -17,18 +17,9 @@ */ package org.wso2.carbon.device.mgt.core; -import org.apache.commons.logging.Log; -import org.apache.commons.logging.LogFactory; -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.common.*; +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.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.DeviceManagementDAOException; import org.wso2.carbon.device.mgt.core.dao.DeviceManagementDAOFactory; @@ -36,195 +27,218 @@ 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.dto.DeviceType; 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 java.util.ArrayList; import java.util.List; -import java.util.Locale; -public class DeviceManagementServiceProviderImpl implements DeviceManager { +public class DeviceManagementServiceProviderImpl implements DeviceManagementService { - private DeviceDAO deviceDAO; - private DeviceTypeDAO deviceTypeDAO; - private DeviceManagementRepository pluginRepository; + private DeviceDAO deviceDAO; + private DeviceTypeDAO deviceTypeDAO; + private DeviceManagementRepository pluginRepository; private OperationManager operationManager; private LicenseManager licenseManager; - public DeviceManagementServiceProviderImpl(DeviceManagementRepository pluginRepository) { - this.pluginRepository = pluginRepository; - this.deviceDAO = DeviceManagementDAOFactory.getDeviceDAO(); - this.deviceTypeDAO = DeviceManagementDAOFactory.getDeviceTypeDAO(); + public DeviceManagementServiceProviderImpl(DeviceManagementRepository pluginRepository) { + this.pluginRepository = pluginRepository; + this.deviceDAO = DeviceManagementDAOFactory.getDeviceDAO(); + this.deviceTypeDAO = DeviceManagementDAOFactory.getDeviceTypeDAO(); this.operationManager = new OperationManagerImpl(); this.licenseManager = new LicenseManagerImpl(); - } - - @Override - public boolean enrollDevice(Device device) throws DeviceManagementException { - DeviceManagerService dms = - this.getPluginRepository().getDeviceManagementProvider(device.getType()); - boolean status = dms.enrollDevice(device); - try { - org.wso2.carbon.device.mgt.core.dto.Device deviceDto = - DeviceManagementDAOUtil.convertDevice(device); - Integer deviceTypeId = - this.getDeviceTypeDAO().getDeviceTypeIdByDeviceTypeName(device.getType()); - deviceDto.setStatus(Status.ACTIVE); - deviceDto.setDeviceTypeId(deviceTypeId); - this.getDeviceDAO().addDevice(deviceDto); - } catch (DeviceManagementDAOException e) { - throw new DeviceManagementException("Error occurred while enrolling the device '" + device.getId() + - "'", e); - } - return status; - } - - @Override - public boolean modifyEnrollment(Device device) throws DeviceManagementException { - DeviceManagerService dms = - this.getPluginRepository().getDeviceManagementProvider(device.getType()); - boolean status = dms.modifyEnrollment(device); - try { - this.getDeviceDAO().updateDevice(DeviceManagementDAOUtil.convertDevice(device)); - } catch (DeviceManagementDAOException e) { - throw new DeviceManagementException("Error occurred while modifying the device '" + device.getId() + - "'", e); - } - return status; - } - - @Override - public boolean disenrollDevice(DeviceIdentifier deviceId) throws DeviceManagementException { - DeviceManagerService dms = - this.getPluginRepository().getDeviceManagementProvider(deviceId.getType()); - return dms.disenrollDevice(deviceId); - } - - @Override - public boolean isEnrolled(DeviceIdentifier deviceId) throws DeviceManagementException { - DeviceManagerService dms = - this.getPluginRepository().getDeviceManagementProvider(deviceId.getType()); - return dms.isEnrolled(deviceId); - } - - @Override - public boolean isActive(DeviceIdentifier deviceId) throws DeviceManagementException { - DeviceManagerService dms = - this.getPluginRepository().getDeviceManagementProvider(deviceId.getType()); - return dms.isActive(deviceId); - } - - @Override - public boolean setActive(DeviceIdentifier deviceId, boolean status) - throws DeviceManagementException { - DeviceManagerService dms = - this.getPluginRepository().getDeviceManagementProvider(deviceId.getType()); - return dms.setActive(deviceId, status); - } - - @Override - public List getAllDevices(String type) throws DeviceManagementException { - DeviceManagerService dms = this.getPluginRepository().getDeviceManagementProvider(type); - List devicesList = new ArrayList(); - try { - Integer deviceTypeId = this.getDeviceTypeDAO().getDeviceTypeIdByDeviceTypeName(type); - List devices = - this.getDeviceDAO().getDevices(deviceTypeId); - - for (org.wso2.carbon.device.mgt.core.dto.Device device : devices) { - DeviceType deviceType = this.deviceTypeDAO.getDeviceType(device.getDeviceTypeId()); - Device convertedDevice = DeviceManagementDAOUtil.convertDevice(device, deviceType); - DeviceIdentifier deviceIdentifier = - DeviceManagementDAOUtil.createDeviceIdentifier(device, deviceType); - Device dmsDevice = dms.getDevice(deviceIdentifier); - if (dmsDevice != null) { - convertedDevice.setProperties(dmsDevice.getProperties()); - convertedDevice.setFeatures(dmsDevice.getFeatures()); - } - devicesList.add(convertedDevice); - } - } catch (DeviceManagementDAOException e) { - throw new DeviceManagementException("Error occurred while obtaining the device for type '" + type + - "'", e); - } - return devicesList; - } - - @Override - public Device getDevice(DeviceIdentifier deviceId) throws DeviceManagementException { - DeviceManagerService dms = - this.getPluginRepository().getDeviceManagementProvider(deviceId.getType()); - Device convertedDevice = null; - try { - Integer deviceTypeId = - this.getDeviceTypeDAO().getDeviceTypeIdByDeviceTypeName(deviceId.getType()); - org.wso2.carbon.device.mgt.core.dto.Device device = - this.getDeviceDAO().getDeviceByDeviceIdentifier(deviceTypeId, deviceId.getId()); - if (device != null) { - convertedDevice = DeviceManagementDAOUtil - .convertDevice(device, this.getDeviceTypeDAO().getDeviceType(deviceTypeId)); - Device dmsDevice = dms.getDevice(deviceId); - if (dmsDevice != null) { - convertedDevice.setProperties(dmsDevice.getProperties()); - convertedDevice.setFeatures(dmsDevice.getFeatures()); - } - } - } catch (DeviceManagementDAOException e) { - throw new DeviceManagementException("Error occurred while obtaining the device for id '" + - deviceId.getId() + "'", e); - } - return convertedDevice; - } - - @Override - public boolean updateDeviceInfo(Device device) throws DeviceManagementException { - DeviceManagerService dms = - this.getPluginRepository().getDeviceManagementProvider(device.getType()); - return dms.updateDeviceInfo(device); - } - - @Override - public boolean setOwnership(DeviceIdentifier deviceId, String ownershipType) - throws DeviceManagementException { - DeviceManagerService dms = - this.getPluginRepository().getDeviceManagementProvider(deviceId.getType()); - return dms.setOwnership(deviceId, ownershipType); - } - - public OperationManager getOperationManager(String type) throws DeviceManagementException { - return operationManager; - } - - @Override - public License getLicense(String type) throws DeviceManagementException { + } + + @Override + public String getProviderType() { + return null; + } + + @Override + public boolean enrollDevice(Device device) throws DeviceManagementException { + DeviceManager dms = + this.getPluginRepository().getDeviceManagementProvider(device.getType()); + boolean status = dms.enrollDevice(device); try { - return licenseManager.getLicense(type, Locale.ENGLISH.getLanguage()); - } catch (LicenseManagementException e) { - throw new DeviceManagementException("Error occurred while retrieving license configured for " + - "device type '" + type + "'", e); + org.wso2.carbon.device.mgt.core.dto.Device deviceDto = DeviceManagementDAOUtil.convertDevice(device); + DeviceType deviceType = this.getDeviceTypeDAO().getDeviceType(device.getType()); + deviceDto.setStatus(Status.ACTIVE); + deviceDto.setDeviceTypeId(deviceType.getId()); + this.getDeviceDAO().addDevice(deviceDto); + } catch (DeviceManagementDAOException e) { + throw new DeviceManagementException("Error occurred while enrolling the device " + + "'" + device.getId() + "'", e); } + return status; } @Override - public boolean addLicense(String type, License license) throws DeviceManagementException { + public boolean modifyEnrollment(Device device) throws DeviceManagementException { + DeviceManager dms = + this.getPluginRepository().getDeviceManagementProvider(device.getType()); + boolean status = dms.modifyEnrollment(device); try { - return licenseManager.addLicense(type, license); - } catch (LicenseManagementException e) { - throw new DeviceManagementException("Error occurred while adding license for device type '" + - type + "'", e); + this.getDeviceDAO().updateDevice(DeviceManagementDAOUtil.convertDevice(device)); + } catch (DeviceManagementDAOException e) { + throw new DeviceManagementException("Error occurred while modifying the device " + + "'" + device.getId() + "'", e); + } + return status; + } + + @Override + public boolean disenrollDevice(DeviceIdentifier deviceId) throws DeviceManagementException { + DeviceManager dms = + this.getPluginRepository().getDeviceManagementProvider(deviceId.getType()); + return dms.disenrollDevice(deviceId); + } + + @Override + public boolean isEnrolled(DeviceIdentifier deviceId) throws DeviceManagementException { + DeviceManager dms = + this.getPluginRepository().getDeviceManagementProvider(deviceId.getType()); + return dms.isEnrolled(deviceId); + } + @Override + public boolean isActive(DeviceIdentifier deviceId) throws DeviceManagementException { + DeviceManager dms = + this.getPluginRepository().getDeviceManagementProvider(deviceId.getType()); + return dms.isActive(deviceId); + } + + @Override + public boolean setActive(DeviceIdentifier deviceId, boolean status) + throws DeviceManagementException { + DeviceManager dms = + this.getPluginRepository().getDeviceManagementProvider(deviceId.getType()); + return dms.setActive(deviceId, status); + } + + @Override + public List getAllDevices() throws DeviceManagementException { + return null; + } + + @Override + public List getAllDevices(String type) throws DeviceManagementException { + DeviceManager dms = this.getPluginRepository().getDeviceManagementProvider(type); + List devicesList = new ArrayList(); + try { + DeviceType dt = this.getDeviceTypeDAO().getDeviceType(type); + List devices = + this.getDeviceDAO().getDevices(dt.getId()); + + for (org.wso2.carbon.device.mgt.core.dto.Device device : devices) { + DeviceType deviceType = this.deviceTypeDAO.getDeviceType(device.getDeviceTypeId()); + Device convertedDevice = DeviceManagementDAOUtil.convertDevice(device, deviceType); + DeviceIdentifier deviceIdentifier = + DeviceManagementDAOUtil.createDeviceIdentifier(device, deviceType); + Device dmsDevice = dms.getDevice(deviceIdentifier); + if (dmsDevice != null) { + convertedDevice.setProperties(dmsDevice.getProperties()); + convertedDevice.setFeatures(dmsDevice.getFeatures()); + } + devicesList.add(convertedDevice); + } + } catch (DeviceManagementDAOException e) { + throw new DeviceManagementException("Error occurred while obtaining the device for type " + + "'" + type + "'", e); } + return devicesList; + } + + @Override + public Device getDevice(DeviceIdentifier deviceId) throws DeviceManagementException { + DeviceManager dms = + this.getPluginRepository().getDeviceManagementProvider(deviceId.getType()); + Device convertedDevice = null; + try { + DeviceType deviceType = + this.getDeviceTypeDAO().getDeviceType(deviceId.getType()); + org.wso2.carbon.device.mgt.core.dto.Device device = + this.getDeviceDAO().getDevice(deviceId); + if (device != null) { + convertedDevice = DeviceManagementDAOUtil + .convertDevice(device, this.getDeviceTypeDAO().getDeviceType(deviceType.getId())); + Device dmsDevice = dms.getDevice(deviceId); + if (dmsDevice != null) { + convertedDevice.setProperties(dmsDevice.getProperties()); + convertedDevice.setFeatures(dmsDevice.getFeatures()); + } + } + } catch (DeviceManagementDAOException e) { + throw new DeviceManagementException("Error occurred while obtaining the device for id " + + "'" + deviceId.getId() + "'", e); + } + return convertedDevice; + } + + @Override + public boolean updateDeviceInfo(Device device) throws DeviceManagementException { + DeviceManager dms = + this.getPluginRepository().getDeviceManagementProvider(device.getType()); + return dms.updateDeviceInfo(device); + } + + @Override + public boolean setOwnership(DeviceIdentifier deviceId, String ownershipType) + throws DeviceManagementException { + DeviceManager dms = + this.getPluginRepository().getDeviceManagementProvider(deviceId.getType()); + return dms.setOwnership(deviceId, ownershipType); + } + + public OperationManager getOperationManager(String type) throws DeviceManagementException { + return operationManager; + } + + @Override + public License getLicense(String deviceType, String languageCode) throws LicenseManagementException { + return licenseManager.getLicense(deviceType, languageCode); + } + + @Override + public boolean addLicense(String type, License license) throws LicenseManagementException { + return licenseManager.addLicense(type, license); } public DeviceDAO getDeviceDAO() { - return deviceDAO; - } + return deviceDAO; + } + + public DeviceTypeDAO getDeviceTypeDAO() { + return deviceTypeDAO; + } + + public DeviceManagementRepository getPluginRepository() { + return pluginRepository; + } + + @Override + public boolean addOperation(Operation operation, + List devices) throws OperationManagementException { + return operationManager.addOperation(operation, devices); + } + + @Override + public List getOperations(DeviceIdentifier deviceId) throws OperationManagementException { + return operationManager.getOperations(deviceId); + } - public DeviceTypeDAO getDeviceTypeDAO() { - return deviceTypeDAO; - } + @Override + public List getPendingOperations(DeviceIdentifier deviceId) throws OperationManagementException { + return operationManager.getPendingOperations(deviceId); + } - public DeviceManagementRepository getPluginRepository() { - return pluginRepository; - } + @Override + public List getFeatures(String deviceType) throws FeatureManagementException { + return operationManager.getFeatures(deviceType); + } } diff --git a/components/device-mgt/org.wso2.carbon.device.mgt.core/src/main/java/org/wso2/carbon/device/mgt/core/dao/DeviceDAO.java b/components/device-mgt/org.wso2.carbon.device.mgt.core/src/main/java/org/wso2/carbon/device/mgt/core/dao/DeviceDAO.java index 4e3a7f86ca..d4f07cf5d2 100644 --- a/components/device-mgt/org.wso2.carbon.device.mgt.core/src/main/java/org/wso2/carbon/device/mgt/core/dao/DeviceDAO.java +++ b/components/device-mgt/org.wso2.carbon.device.mgt.core/src/main/java/org/wso2/carbon/device/mgt/core/dao/DeviceDAO.java @@ -18,6 +18,7 @@ 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.Status; @@ -32,27 +33,22 @@ public interface DeviceDAO { 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; - /** - * @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; + Device getDevice(DeviceIdentifier deviceIdentifier) throws DeviceManagementDAOException; List getDevices() throws DeviceManagementDAOException; + List getDeviceIds(List devices) throws DeviceManagementDAOException; + /** * @param type - The device type id. * @return a list of devices based on the type id. * @throws DeviceManagementDAOException */ - List getDevices(Integer type) throws DeviceManagementDAOException; + List getDevices(int type) throws DeviceManagementDAOException; } diff --git a/components/device-mgt/org.wso2.carbon.device.mgt.core/src/main/java/org/wso2/carbon/device/mgt/core/dao/DeviceManagementDAOFactory.java b/components/device-mgt/org.wso2.carbon.device.mgt.core/src/main/java/org/wso2/carbon/device/mgt/core/dao/DeviceManagementDAOFactory.java index df6ee073a7..10df0539a5 100644 --- a/components/device-mgt/org.wso2.carbon.device.mgt.core/src/main/java/org/wso2/carbon/device/mgt/core/dao/DeviceManagementDAOFactory.java +++ b/components/device-mgt/org.wso2.carbon.device.mgt.core/src/main/java/org/wso2/carbon/device/mgt/core/dao/DeviceManagementDAOFactory.java @@ -90,4 +90,5 @@ public class DeviceManagementDAOFactory { public static DataSource getDataSource() { return dataSource; } + } diff --git a/components/device-mgt/org.wso2.carbon.device.mgt.core/src/main/java/org/wso2/carbon/device/mgt/core/dao/DeviceTypeDAO.java b/components/device-mgt/org.wso2.carbon.device.mgt.core/src/main/java/org/wso2/carbon/device/mgt/core/dao/DeviceTypeDAO.java index bed0d07ddd..b59781b876 100644 --- a/components/device-mgt/org.wso2.carbon.device.mgt.core/src/main/java/org/wso2/carbon/device/mgt/core/dao/DeviceTypeDAO.java +++ b/components/device-mgt/org.wso2.carbon.device.mgt.core/src/main/java/org/wso2/carbon/device/mgt/core/dao/DeviceTypeDAO.java @@ -33,10 +33,10 @@ public interface DeviceTypeDAO { List 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; } diff --git a/components/device-mgt/org.wso2.carbon.device.mgt.core/src/main/java/org/wso2/carbon/device/mgt/core/dao/impl/DeviceDAOImpl.java b/components/device-mgt/org.wso2.carbon.device.mgt.core/src/main/java/org/wso2/carbon/device/mgt/core/dao/impl/DeviceDAOImpl.java index 54010a8554..df80629a0f 100644 --- a/components/device-mgt/org.wso2.carbon.device.mgt.core/src/main/java/org/wso2/carbon/device/mgt/core/dao/impl/DeviceDAOImpl.java +++ b/components/device-mgt/org.wso2.carbon.device.mgt.core/src/main/java/org/wso2/carbon/device/mgt/core/dao/impl/DeviceDAOImpl.java @@ -20,6 +20,7 @@ package org.wso2.carbon.device.mgt.core.dao.impl; import org.apache.commons.logging.Log; 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.DeviceManagementDAOException; import org.wso2.carbon.device.mgt.core.dao.util.DeviceManagementDAOUtil; @@ -37,164 +38,186 @@ import java.util.List; public class DeviceDAOImpl implements DeviceDAO { - private DataSource dataSource; - private static final Log log = LogFactory.getLog(DeviceDAOImpl.class); - - public DeviceDAOImpl(DataSource dataSource) { - this.dataSource = dataSource; - } - - @Override - public void addDevice(Device device) throws DeviceManagementDAOException { - Connection conn = null; - PreparedStatement stmt = null; - try { - conn = this.getConnection(); - String createDBQuery = - "INSERT INTO DM_DEVICE(DESCRIPTION, NAME, DATE_OF_ENROLLMENT, DATE_OF_LAST_UPDATE, " + - "OWNERSHIP, STATUS, DEVICE_TYPE_ID, DEVICE_IDENTIFICATION, OWNER, TENANT_ID) " + - "VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?, ?)"; - - stmt = conn.prepareStatement(createDBQuery); - stmt.setString(1, device.getDescription()); - stmt.setString(2, device.getName()); - stmt.setLong(3, new Date().getTime()); - stmt.setLong(4, new Date().getTime()); - stmt.setString(5, device.getOwnerShip()); - stmt.setString(6, device.getStatus().toString()); - stmt.setInt(7, device.getDeviceTypeId()); - stmt.setString(8, device.getDeviceIdentificationId()); - stmt.setString(9, device.getOwnerId()); - stmt.setInt(10, device.getTenantId()); - stmt.executeUpdate(); - } catch (SQLException e) { - String msg = "Error occurred while enrolling device '" + device.getName() + "'"; - log.error(msg, e); - throw new DeviceManagementDAOException(msg, e); - } finally { - DeviceManagementDAOUtil.cleanupResources(conn, stmt, null); - } - } - - @Override - public void updateDevice(Device device) throws DeviceManagementDAOException { - - } - - @Override - public void updateDeviceStatus(Long deviceId, Status status) - throws DeviceManagementDAOException { - - } - - @Override - public void deleteDevice(Long deviceId) throws DeviceManagementDAOException { - - } - - @Override - public Device getDeviceByDeviceId(Long deviceId) throws DeviceManagementDAOException { - return null; - } - - @Override - public Device getDeviceByDeviceIdentifier(Integer type, String identifier) - throws DeviceManagementDAOException { - Connection conn = null; - PreparedStatement stmt = null; - ResultSet resultSet = null; - Device device = null; - try { - conn = this.getConnection(); - String selectDBQueryForType = "SELECT ID, DESCRIPTION, NAME, DATE_OF_ENROLLMENT, " + - "DATE_OF_LAST_UPDATE, OWNERSHIP, STATUS, DEVICE_TYPE_ID, " + - "DEVICE_IDENTIFICATION, OWNER, TENANT_ID FROM DM_DEVICE " + - "WHERE DM_DEVICE.DEVICE_TYPE_ID=? AND " + - "DM_DEVICE.DEVICE_IDENTIFICATION=?"; - stmt = conn.prepareStatement(selectDBQueryForType); - stmt.setInt(1, type); - stmt.setString(2, identifier); - resultSet = stmt.executeQuery(); - while (resultSet.next()) { - device = new Device(); - device.setId(resultSet.getInt(1)); - device.setDescription(resultSet.getString(2)); - device.setName(resultSet.getString(3)); - device.setDateOfEnrollment(resultSet.getLong(4)); - device.setDateOfLastUpdate(resultSet.getLong(5)); - //TODO:- Ownership is not a enum in DeviceDAO - device.setOwnerShip(resultSet.getString(6)); - device.setStatus(Status.valueOf(resultSet.getString(7))); - device.setDeviceTypeId(resultSet.getInt(8)); - device.setDeviceIdentificationId(resultSet.getString(9)); - device.setOwnerId(resultSet.getString(10)); - device.setTenantId(resultSet.getInt(11)); - } - } catch (SQLException e) { - String msg = "Error occurred while listing devices for type '" + type + "'"; - log.error(msg, e); - throw new DeviceManagementDAOException(msg, e); - } finally { - DeviceManagementDAOUtil.cleanupResources(conn, stmt, resultSet); - } - return device; - } - - @Override - public List getDevices() throws DeviceManagementDAOException { - return null; - } - - @Override - public List getDevices(Integer type) throws DeviceManagementDAOException { - Connection conn = null; - PreparedStatement stmt = null; - ResultSet resultSet = null; - List devicesList = null; - try { - conn = this.getConnection(); - String selectDBQueryForType = "SELECT ID, DESCRIPTION, NAME, DATE_OF_ENROLLMENT, " + - "DATE_OF_LAST_UPDATE, OWNERSHIP, STATUS, DEVICE_TYPE_ID, " + - "DEVICE_IDENTIFICATION, OWNER, TENANT_ID FROM DM_DEVICE " + - "WHERE DM_DEVICE.DEVICE_TYPE_ID=?"; - stmt = conn.prepareStatement(selectDBQueryForType); - stmt.setInt(1, type); - resultSet = stmt.executeQuery(); - devicesList = new ArrayList(); - while (resultSet.next()) { - Device device = new Device(); - device.setId(resultSet.getInt(1)); - device.setDescription(resultSet.getString(2)); - device.setName(resultSet.getString(3)); - device.setDateOfEnrollment(resultSet.getLong(4)); - device.setDateOfLastUpdate(resultSet.getLong(5)); - //TODO:- Ownership is not a enum in DeviceDAO - device.setOwnerShip(resultSet.getString(6)); - device.setStatus(Status.valueOf(resultSet.getString(7))); - device.setDeviceTypeId(resultSet.getInt(8)); - device.setDeviceIdentificationId(resultSet.getString(9)); - device.setOwnerId(resultSet.getString(10)); - device.setTenantId(resultSet.getInt(11)); - devicesList.add(device); - } - } catch (SQLException e) { - String msg = "Error occurred while listing devices for type '" + type + "'"; - log.error(msg, e); - throw new DeviceManagementDAOException(msg, e); - } finally { - DeviceManagementDAOUtil.cleanupResources(conn, stmt, resultSet); - } - return devicesList; - } - - private Connection getConnection() throws DeviceManagementDAOException { - try { - return dataSource.getConnection(); - } catch (SQLException e) { - throw new DeviceManagementDAOException( - "Error occurred while obtaining a connection from the device " + - "management metadata repository datasource", e); - } - } + private DataSource dataSource; + private static final Log log = LogFactory.getLog(DeviceDAOImpl.class); + + public DeviceDAOImpl(DataSource dataSource) { + this.dataSource = dataSource; + } + + @Override + public void addDevice(Device device) throws DeviceManagementDAOException { + Connection conn = null; + PreparedStatement stmt = null; + try { + conn = this.getConnection(); + String sql = + "INSERT INTO DM_DEVICE(DESCRIPTION, NAME, DATE_OF_ENROLLMENT, DATE_OF_LAST_UPDATE, " + + "OWNERSHIP, STATUS, DEVICE_TYPE_ID, DEVICE_IDENTIFICATION, OWNER, TENANT_ID) " + + "VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?, ?)"; + stmt = conn.prepareStatement(sql); + stmt.setString(1, device.getDescription()); + stmt.setString(2, device.getName()); + stmt.setLong(3, new Date().getTime()); + stmt.setLong(4, new Date().getTime()); + stmt.setString(5, device.getOwnerShip()); + stmt.setString(6, device.getStatus().toString()); + stmt.setInt(7, device.getDeviceTypeId()); + stmt.setString(8, device.getDeviceIdentificationId()); + stmt.setString(9, device.getOwnerId()); + stmt.setInt(10, device.getTenantId()); + stmt.executeUpdate(); + } catch (SQLException e) { + throw new DeviceManagementDAOException("Error occurred while enrolling device " + + "'" + device.getName() + "'", e); + } finally { + DeviceManagementDAOUtil.cleanupResources(conn, stmt, null); + } + } + + @Override + public void updateDevice(Device device) throws DeviceManagementDAOException { + + } + + @Override + public void updateDeviceStatus(int deviceId, Status status) throws DeviceManagementDAOException { + + } + + @Override + public void deleteDevice(int deviceId) throws DeviceManagementDAOException { + + } + + @Override + public Device getDevice(int deviceId) throws DeviceManagementDAOException { + return null; + } + + @Override + public Device getDevice(DeviceIdentifier deviceIdentifier) throws DeviceManagementDAOException { + Connection conn = null; + PreparedStatement stmt = null; + ResultSet rs = null; + Device device = null; + try { + conn = this.getConnection(); + String sql = + "SELECT d.ID, d.DESCRIPTION, d.NAME, d.DATE_OF_ENROLLMENT, d.DATE_OF_LAST_UPDATE, d.OWNERSHIP, d.STATUS, " + + "d.DEVICE_TYPE_ID, d.DEVICE_IDENTIFICATION, d.OWNER, d.TENANT_ID FROM DM_DEVICE d, DEVICE_TYPE dt WHERE " + + "dt.NAME = ? AND d.DEVICE_IDENTIFICATION = ?"; + stmt = conn.prepareStatement(sql); + stmt.setString(1, deviceIdentifier.getType()); + stmt.setString(2, deviceIdentifier.getId()); + + rs = stmt.executeQuery(); + if (rs.next()) { + device = new Device(); + device.setId(rs.getInt("ID")); + device.setDescription(rs.getString("DESCRIPTION")); + device.setName(rs.getString("NAME")); + device.setDateOfEnrollment(rs.getLong("DATE_OF_ENROLLMENT")); + device.setDateOfLastUpdate(rs.getLong("DATE_OF_LAST_UPDATE")); + //TODO:- Ownership is not a enum in DeviceDAO + device.setOwnerShip(rs.getString("OWNERSHIP")); + device.setStatus(Status.valueOf(rs.getString("STATUS"))); + device.setDeviceTypeId(rs.getInt("DEVICE_TYPE_ID")); + device.setDeviceIdentificationId(rs.getString("DEVICE_IDENTIFICATION")); + device.setOwnerId(rs.getString("OWNER")); + device.setTenantId(rs.getInt("TENANT_ID")); + } + } catch (SQLException e) { + throw new DeviceManagementDAOException("Error occurred while listing devices for type " + + "'" + deviceIdentifier.getType() + "'", e); + } finally { + DeviceManagementDAOUtil.cleanupResources(conn, stmt, rs); + } + return device; + } + + @Override + public List getDevices() throws DeviceManagementDAOException { + return null; + } + + @Override + public List getDeviceIds(List devices) throws DeviceManagementDAOException { + List deviceIds = new ArrayList(); + 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 devices) { + StringBuilder sb = new StringBuilder(); + for (DeviceIdentifier device : devices) { + sb.append(device.getId()); + } + return sb.toString(); + } + + @Override + public List getDevices(int type) throws DeviceManagementDAOException { + Connection conn = null; + PreparedStatement stmt = null; + ResultSet resultSet = null; + List devicesList = null; + try { + conn = this.getConnection(); + String selectDBQueryForType = "SELECT ID, DESCRIPTION, NAME, DATE_OF_ENROLLMENT, " + + "DATE_OF_LAST_UPDATE, OWNERSHIP, STATUS, DEVICE_TYPE_ID, " + + "DEVICE_IDENTIFICATION, OWNER, TENANT_ID FROM DM_DEVICE " + + "WHERE DM_DEVICE.DEVICE_TYPE_ID = ?"; + stmt = conn.prepareStatement(selectDBQueryForType); + stmt.setInt(1, type); + resultSet = stmt.executeQuery(); + devicesList = new ArrayList(); + while (resultSet.next()) { + Device device = new Device(); + device.setId(resultSet.getInt(1)); + device.setDescription(resultSet.getString(2)); + device.setName(resultSet.getString(3)); + device.setDateOfEnrollment(resultSet.getLong(4)); + device.setDateOfLastUpdate(resultSet.getLong(5)); + //TODO:- Ownership is not a enum in DeviceDAO + device.setOwnerShip(resultSet.getString(6)); + device.setStatus(Status.valueOf(resultSet.getString(7))); + device.setDeviceTypeId(resultSet.getInt(8)); + device.setDeviceIdentificationId(resultSet.getString(9)); + device.setOwnerId(resultSet.getString(10)); + device.setTenantId(resultSet.getInt(11)); + devicesList.add(device); + } + } catch (SQLException e) { + String msg = "Error occurred while listing devices for type '" + type + "'"; + log.error(msg, e); + throw new DeviceManagementDAOException(msg, e); + } finally { + DeviceManagementDAOUtil.cleanupResources(conn, stmt, resultSet); + } + return devicesList; + } + + private Connection getConnection() throws DeviceManagementDAOException { + try { + return dataSource.getConnection(); + } catch (SQLException e) { + throw new DeviceManagementDAOException( + "Error occurred while obtaining a connection from the device " + + "management metadata repository datasource", e); + } + } } diff --git a/components/device-mgt/org.wso2.carbon.device.mgt.core/src/main/java/org/wso2/carbon/device/mgt/core/dao/impl/DeviceTypeDAOImpl.java b/components/device-mgt/org.wso2.carbon.device.mgt.core/src/main/java/org/wso2/carbon/device/mgt/core/dao/impl/DeviceTypeDAOImpl.java index a7bf9d3802..caac62456f 100644 --- a/components/device-mgt/org.wso2.carbon.device.mgt.core/src/main/java/org/wso2/carbon/device/mgt/core/dao/impl/DeviceTypeDAOImpl.java +++ b/components/device-mgt/org.wso2.carbon.device.mgt.core/src/main/java/org/wso2/carbon/device/mgt/core/dao/impl/DeviceTypeDAOImpl.java @@ -50,9 +50,8 @@ public class DeviceTypeDAOImpl implements DeviceTypeDAO { stmt.setString(1, deviceType.getName()); stmt.execute(); } catch (SQLException e) { - String msg = "Error occurred while registering the device type '" + deviceType.getName() + "'"; - log.error(msg, e); - throw new DeviceManagementDAOException(msg, e); + throw new DeviceManagementDAOException("Error occurred while registering the device type " + + "'" + deviceType.getName() + "'", e); } finally { DeviceManagementDAOUtil.cleanupResources(conn, stmt, null); } @@ -65,85 +64,88 @@ public class DeviceTypeDAOImpl implements DeviceTypeDAO { @Override public List getDeviceTypes() throws DeviceManagementDAOException { - Connection conn = this.getConnection(); + Connection conn = null; PreparedStatement stmt = null; - List deviceTypes = new ArrayList(); + ResultSet rs = null; + List deviceTypes = null; try { - stmt = conn.prepareStatement("SELECT ID AS DEVICE_TYPE_ID, NAME AS DEVICE_TYPE FROM DM_DEVICE_TYPE"); - ResultSet results = stmt.executeQuery(); + conn = this.getConnection(); + 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 = new DeviceType(); - deviceType.setId(results.getLong("DEVICE_TYPE_ID")); - deviceType.setName(results.getString("DEVICE_TYPE")); + deviceType.setId(rs.getInt("DEVICE_TYPE_ID")); + deviceType.setName(rs.getString("DEVICE_TYPE")); deviceTypes.add(deviceType); } + return deviceTypes; } catch (SQLException e) { - String msg = "Error occurred while fetching the registered device types"; - log.error(msg, e); - throw new DeviceManagementDAOException(msg, e); + throw new DeviceManagementDAOException("Error occurred while fetching the registered device types", e); } finally { - DeviceManagementDAOUtil.cleanupResources(conn, stmt, null); + DeviceManagementDAOUtil.cleanupResources(conn, stmt, rs); } - return deviceTypes; } @Override - public DeviceType getDeviceType(Integer id) throws DeviceManagementDAOException { - Connection conn = this.getConnection(); + public DeviceType getDeviceType(int id) throws DeviceManagementDAOException { + Connection conn = null; PreparedStatement stmt = null; - DeviceType deviceType = null; + ResultSet rs = null; 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); - ResultSet results = stmt.executeQuery(); - while (results.next()) { + rs = stmt.executeQuery(); + DeviceType deviceType = null; + while (rs.next()) { deviceType = new DeviceType(); - deviceType.setId(results.getLong("DEVICE_TYPE_ID")); - deviceType.setName(results.getString("DEVICE_TYPE")); + deviceType.setId(rs.getInt("DEVICE_TYPE_ID")); + deviceType.setName(rs.getString("DEVICE_TYPE")); } + return deviceType; } catch (SQLException e) { - String msg = "Error occurred while fetching the registered device type"; - log.error(msg, e); - throw new DeviceManagementDAOException(msg, e); + throw new DeviceManagementDAOException("Error occurred while fetching the registered device type", e); } finally { - DeviceManagementDAOUtil.cleanupResources(conn, stmt, null); + DeviceManagementDAOUtil.cleanupResources(conn, stmt, rs); } - return deviceType; } @Override - public Integer getDeviceTypeIdByDeviceTypeName(String type) throws DeviceManagementDAOException { - - Connection conn = this.getConnection(); + public DeviceType getDeviceType(String type) throws DeviceManagementDAOException { + Connection conn = null; PreparedStatement stmt = null; - ResultSet resultSet = null; - Integer deviceTypeId = null; - + ResultSet rs = null; try { - String createDBQuery = "SELECT * From DM_DEVICE_TYPE DT WHERE DT.NAME=?"; - stmt = conn.prepareStatement(createDBQuery); + conn = this.getConnection(); + String sql = "SELECT ID From DM_DEVICE_TYPE WHERE NAME = ?"; + stmt = conn.prepareStatement(sql); stmt.setString(1, type); - resultSet = stmt.executeQuery(); + rs = stmt.executeQuery(); - while (resultSet.next()) { - deviceTypeId = resultSet.getInt(1); + int id = -1; + if (rs.next()) { + id = rs.getInt("ID"); } + DeviceType deviceType = new DeviceType(); + deviceType.setId(id); + deviceType.setName(type); + return deviceType; } catch (SQLException e) { - String msg = "Error occurred while fetch device type id for device type '" + type + "'"; - log.error(msg, e); - throw new DeviceManagementDAOException(msg, e); + throw new DeviceManagementDAOException("Error occurred while fetch device type id for device type " + + "'" + type + "'", e); } finally { - DeviceManagementDAOUtil.cleanupResources(conn, stmt, null); + DeviceManagementDAOUtil.cleanupResources(conn, stmt, rs); } - - return deviceTypeId; } @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); } } + } diff --git a/components/device-mgt/org.wso2.carbon.device.mgt.core/src/main/java/org/wso2/carbon/device/mgt/core/dto/Device.java b/components/device-mgt/org.wso2.carbon.device.mgt.core/src/main/java/org/wso2/carbon/device/mgt/core/dto/Device.java index fae390ac6b..87e4e477e6 100644 --- a/components/device-mgt/org.wso2.carbon.device.mgt.core/src/main/java/org/wso2/carbon/device/mgt/core/dto/Device.java +++ b/components/device-mgt/org.wso2.carbon.device.mgt.core/src/main/java/org/wso2/carbon/device/mgt/core/dto/Device.java @@ -23,7 +23,7 @@ import java.io.Serializable; public class Device implements Serializable { private static final long serialVersionUID = -8101106997837486245L; - private Integer id; + private int id; private String description; private String name; private Long dateOfEnrollment; @@ -33,13 +33,13 @@ public class Device implements Serializable { private String ownerId; private String ownerShip; private int tenantId; - private Integer deviceTypeId; + private int deviceTypeId; - public Integer getDeviceTypeId() { + public int getDeviceTypeId() { return deviceTypeId; } - public void setDeviceTypeId(Integer deviceTypeId) { + public void setDeviceTypeId(int deviceTypeId) { this.deviceTypeId = deviceTypeId; } diff --git a/components/device-mgt/org.wso2.carbon.device.mgt.core/src/main/java/org/wso2/carbon/device/mgt/core/dto/DeviceType.java b/components/device-mgt/org.wso2.carbon.device.mgt.core/src/main/java/org/wso2/carbon/device/mgt/core/dto/DeviceType.java index cd23498202..85f1f948f9 100644 --- a/components/device-mgt/org.wso2.carbon.device.mgt.core/src/main/java/org/wso2/carbon/device/mgt/core/dto/DeviceType.java +++ b/components/device-mgt/org.wso2.carbon.device.mgt.core/src/main/java/org/wso2/carbon/device/mgt/core/dto/DeviceType.java @@ -23,14 +23,14 @@ import java.io.Serializable; public class DeviceType implements Serializable { private static final long serialVersionUID = 7927802716452548282L; - private Long id; + private int id; private String name; - public Long getId() { + public int getId() { return id; } - public void setId(Long id) { + public void setId(int id) { this.id = id; } diff --git a/components/device-mgt/org.wso2.carbon.device.mgt.core/src/main/java/org/wso2/carbon/device/mgt/core/internal/DeviceManagementDataHolder.java b/components/device-mgt/org.wso2.carbon.device.mgt.core/src/main/java/org/wso2/carbon/device/mgt/core/internal/DeviceManagementDataHolder.java index 21c9379bae..7406993c3b 100644 --- a/components/device-mgt/org.wso2.carbon.device.mgt.core/src/main/java/org/wso2/carbon/device/mgt/core/internal/DeviceManagementDataHolder.java +++ b/components/device-mgt/org.wso2.carbon.device.mgt.core/src/main/java/org/wso2/carbon/device/mgt/core/internal/DeviceManagementDataHolder.java @@ -19,7 +19,7 @@ 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.license.mgt.LicenseManager; import org.wso2.carbon.registry.core.service.RegistryService; @@ -30,7 +30,7 @@ public class DeviceManagementDataHolder { private RealmService realmService; private TenantManager tenantManager; - private DeviceManager deviceManager; + private DeviceManagementService deviceManagerProvider; private LicenseManager licenseManager; private RegistryService registryService; private LicenseConfig licenseConfig; @@ -64,12 +64,12 @@ public class DeviceManagementDataHolder { return tenantManager; } - public DeviceManager getDeviceManager() { - return deviceManager; + public DeviceManagementService getDeviceManagementProvider() { + return deviceManagerProvider; } - public void setDeviceManager(DeviceManager deviceManager) { - this.deviceManager = deviceManager; + public void setDeviceManagementProvider(DeviceManagementService deviceManagerProvider) { + this.deviceManagerProvider = deviceManagerProvider; } public RegistryService getRegistryService() { diff --git a/components/device-mgt/org.wso2.carbon.device.mgt.core/src/main/java/org/wso2/carbon/device/mgt/core/internal/DeviceManagementServiceComponent.java b/components/device-mgt/org.wso2.carbon.device.mgt.core/src/main/java/org/wso2/carbon/device/mgt/core/internal/DeviceManagementServiceComponent.java index 979323200e..ac3a7b832e 100644 --- a/components/device-mgt/org.wso2.carbon.device.mgt.core/src/main/java/org/wso2/carbon/device/mgt/core/internal/DeviceManagementServiceComponent.java +++ b/components/device-mgt/org.wso2.carbon.device.mgt.core/src/main/java/org/wso2/carbon/device/mgt/core/internal/DeviceManagementServiceComponent.java @@ -22,11 +22,11 @@ import org.apache.commons.logging.LogFactory; import org.osgi.framework.BundleContext; import org.osgi.service.component.ComponentContext; 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.DeviceManagementRepository; -import org.wso2.carbon.device.mgt.core.DeviceManager; -import org.wso2.carbon.device.mgt.core.DeviceManagerImpl; +import org.wso2.carbon.device.mgt.core.DeviceManagementServiceProviderImpl; +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.DeviceManagementConfig; 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.LicenseManager; 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.registry.core.service.RegistryService; import org.wso2.carbon.user.core.service.RealmService; @@ -52,11 +52,11 @@ import org.wso2.carbon.user.core.service.RealmService; * bind="setRealmService" * unbind="unsetRealmService" * @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" * policy="dynamic" - * bind="setDeviceManagerService" - * unbind="unsetDeviceManagerService" + * bind="setDeviceManager" + * unbind="unsetDeviceManager" * @scr.reference name="registry.service" * interface="org.wso2.carbon.registry.core.service.RegistryService" * cardinality="1..1" @@ -74,6 +74,7 @@ public class DeviceManagementServiceComponent { if (log.isDebugEnabled()) { log.debug("Initializing device management core bundle"); } + /* Initializing Device Management Configuration */ DeviceConfigurationManager.getInstance().initConfig(); DeviceManagementConfig config = @@ -82,8 +83,9 @@ public class DeviceManagementServiceComponent { DataSourceConfig dsConfig = config.getDeviceMgtRepository().getDataSourceConfig(); DeviceManagementDAOFactory.init(dsConfig); - DeviceManager deviceManager = new DeviceManagerImpl(config, this.getPluginRepository()); - DeviceManagementDataHolder.getInstance().setDeviceManager(deviceManager); + DeviceManagementService deviceManagementProvider = + new DeviceManagementServiceProviderImpl(this.getPluginRepository()); + DeviceManagementDataHolder.getInstance().setDeviceManagementProvider(deviceManagementProvider); LicenseConfigurationManager.getInstance().initConfig(); LicenseConfig licenseConfig = @@ -98,25 +100,17 @@ public class DeviceManagementServiceComponent { System.getProperty(DeviceManagementConstants.Common.PROPERTY_SETUP); if (setupOption != null) { if (log.isDebugEnabled()) { - log.debug( - "-Dsetup is enabled. Device management repository schema initialization " + - "is about to begin"); + log.debug("-Dsetup is enabled. Device management repository schema initialization is about to " + + "begin"); } this.setupDeviceManagementSchema(dsConfig); - // this.setupDefaultLicenses(licenseConfig); + this.setupDefaultLicenses(licenseConfig); } - if (log.isDebugEnabled()) { - log.debug("Registering OSGi service DeviceManagementService"); - } - /* 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()) { + /* Registering declarative service instances exposed by DeviceManagementServiceComponent */ + this.registerServices(componentContext); + + if (log.isDebugEnabled()) { log.debug("Device management core bundle has been successfully initialized"); } } catch (Throwable e) { @@ -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) throws DeviceManagementException { DeviceManagementSchemaInitializer initializer = @@ -139,50 +143,54 @@ public class DeviceManagementServiceComponent { "database schema", e); } if (log.isDebugEnabled()) { - log.debug( - "Device management metadata repository schema has been successfully initialized"); + log.debug("Device management metadata repository schema has been successfully initialized"); } } private void setupDefaultLicenses(LicenseConfig licenseConfig) throws LicenseManagementException { - LicenseManager licenseManager = - DeviceManagementDataHolder.getInstance().getLicenseManager(); + LicenseManager licenseManager = DeviceManagementDataHolder.getInstance().getLicenseManager(); for (License license : licenseConfig.getLicenses()) { - License extLicense = - licenseManager.getLicense(license.getName(), license.getLanguage()); - if (extLicense != null) { - continue; + License extLicense = licenseManager.getLicense(license.getName(), license.getLanguage()); + if (extLicense == null) { + licenseManager.addLicense(license.getName(), license);; } - licenseManager.addLicense(license); } } /** * 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()) { - log.debug("Setting Device Management Service Provider : '" + - deviceManagerService.getProviderType() + "'"); + log.debug("Setting Device Management Service Provider: '" + deviceManager.getProviderType() + "'"); } - this.getPluginRepository().addDeviceManagementProvider(deviceManagerService); - } + try { + this.getPluginRepository().addDeviceManagementProvider(deviceManager); + } catch (DeviceManagementException e) { + log.error("Error occurred while adding device management provider '" + + deviceManager.getProviderType() + "'"); + } + } /** * 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()) { - log.debug("Unsetting Device Management Service Provider : '" + - deviceManagerService.getProviderType() + "'"); + log.debug("Unsetting Device Management Service Provider : '" + deviceManager.getProviderType() + "'"); } - this.getPluginRepository().removeDeviceManagementProvider(deviceManagerService); - } + try { + this.getPluginRepository().removeDeviceManagementProvider(deviceManager); + } catch (DeviceManagementException e) { + log.error("Error occurred while removing device management provider '" + + deviceManager.getProviderType() + "'"); + } + } /** * Sets Realm Service. diff --git a/components/device-mgt/org.wso2.carbon.device.mgt.core/src/main/java/org/wso2/carbon/device/mgt/core/license/mgt/GenericArtifactManagerFactory.java b/components/device-mgt/org.wso2.carbon.device.mgt.core/src/main/java/org/wso2/carbon/device/mgt/core/license/mgt/GenericArtifactManagerFactory.java index 8d07b6343c..f1fbbb617f 100644 --- a/components/device-mgt/org.wso2.carbon.device.mgt.core/src/main/java/org/wso2/carbon/device/mgt/core/license/mgt/GenericArtifactManagerFactory.java +++ b/components/device-mgt/org.wso2.carbon.device.mgt.core/src/main/java/org/wso2/carbon/device/mgt/core/license/mgt/GenericArtifactManagerFactory.java @@ -1,22 +1,72 @@ /* - * Copyright (c) 2015, WSO2 Inc. (http://www.wso2.org) All Rights Reserved. + * Copyright (c) 2015, WSO2 Inc. (http://www.wso2.org) All Rights Reserved. * - * WSO2 Inc. licenses this file to you under the Apache License, - * Version 2.0 (the "License"); you may not use this file except - * in compliance with the License. - * You may obtain a copy of the License at + * WSO2 Inc. licenses this file to you under the Apache License, + * Version 2.0 (the "License"); you may not use this file except + * in compliance with the License. + * You may obtain a copy of the License at * - * http://www.apache.org/licenses/LICENSE-2.0 + * http://www.apache.org/licenses/LICENSE-2.0 * - * Unless required by applicable law or agreed to in writing, - * software distributed under the License is distributed on an - * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY - * KIND, either express or implied. See the License for the - * specific language governing permissions and limitations - * under the License. + * Unless required by applicable law or agreed to in writing, + * software distributed under the License is distributed on an + * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY + * KIND, either express or implied. See the License for the + * specific language governing permissions and limitations + * under the License. * */ 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 { + + private static Map tenantArtifactManagers = + new HashMap(); + 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() + "'"); + } + } + } diff --git a/components/device-mgt/org.wso2.carbon.device.mgt.core/src/main/java/org/wso2/carbon/device/mgt/core/license/mgt/LicenseManagementService.java b/components/device-mgt/org.wso2.carbon.device.mgt.core/src/main/java/org/wso2/carbon/device/mgt/core/license/mgt/LicenseManagementService.java index d6fe3df5da..92615e66d6 100644 --- a/components/device-mgt/org.wso2.carbon.device.mgt.core/src/main/java/org/wso2/carbon/device/mgt/core/license/mgt/LicenseManagementService.java +++ b/components/device-mgt/org.wso2.carbon.device.mgt.core/src/main/java/org/wso2/carbon/device/mgt/core/license/mgt/LicenseManagementService.java @@ -29,8 +29,8 @@ public class LicenseManagementService implements LicenseManager { } @Override - public void addLicense(License license) throws LicenseManagementException { - DeviceManagementDataHolder.getInstance().getLicenseManager().addLicense(license); + public boolean addLicense(String deviceType, License license) throws LicenseManagementException { + return DeviceManagementDataHolder.getInstance().getLicenseManager().addLicense(deviceType, license); } } diff --git a/components/device-mgt/org.wso2.carbon.device.mgt.core/src/main/java/org/wso2/carbon/device/mgt/core/license/mgt/LicenseManager.java b/components/device-mgt/org.wso2.carbon.device.mgt.core/src/main/java/org/wso2/carbon/device/mgt/core/license/mgt/LicenseManager.java index 18dfa06d01..df614f5fbb 100644 --- a/components/device-mgt/org.wso2.carbon.device.mgt.core/src/main/java/org/wso2/carbon/device/mgt/core/license/mgt/LicenseManager.java +++ b/components/device-mgt/org.wso2.carbon.device.mgt.core/src/main/java/org/wso2/carbon/device/mgt/core/license/mgt/LicenseManager.java @@ -22,8 +22,8 @@ import org.wso2.carbon.device.mgt.core.config.license.License; 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; } diff --git a/components/device-mgt/org.wso2.carbon.device.mgt.core/src/main/java/org/wso2/carbon/device/mgt/core/license/mgt/LicenseManagerImpl.java b/components/device-mgt/org.wso2.carbon.device.mgt.core/src/main/java/org/wso2/carbon/device/mgt/core/license/mgt/LicenseManagerImpl.java index dca15462bc..42af35fcc9 100644 --- a/components/device-mgt/org.wso2.carbon.device.mgt.core/src/main/java/org/wso2/carbon/device/mgt/core/license/mgt/LicenseManagerImpl.java +++ b/components/device-mgt/org.wso2.carbon.device.mgt.core/src/main/java/org/wso2/carbon/device/mgt/core/license/mgt/LicenseManagerImpl.java @@ -1,57 +1,111 @@ /* - * Copyright (c) 2015, WSO2 Inc. (http://www.wso2.org) All Rights Reserved. + * Copyright (c) 2015, WSO2 Inc. (http://www.wso2.org) All Rights Reserved. * - * WSO2 Inc. licenses this file to you under the Apache License, - * Version 2.0 (the "License"); you may not use this file except - * in compliance with the License. - * You may obtain a copy of the License at + * WSO2 Inc. licenses this file to you under the Apache License, + * Version 2.0 (the "License"); you may not use this file except + * in compliance with the License. + * You may obtain a copy of the License at * - * http://www.apache.org/licenses/LICENSE-2.0 + * http://www.apache.org/licenses/LICENSE-2.0 * - * Unless required by applicable law or agreed to in writing, - * software distributed under the License is distributed on an - * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY - * KIND, either express or implied. See the License for the - * specific language governing permissions and limitations - * under the License. + * Unless required by applicable law or agreed to in writing, + * software distributed under the License is distributed on an + * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY + * KIND, either express or implied. See the License for the + * specific language governing permissions and limitations + * under the License. * */ + package org.wso2.carbon.device.mgt.core.license.mgt; import org.apache.commons.logging.Log; 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.LicenseConfig; -import org.wso2.carbon.device.mgt.core.internal.DeviceManagementDataHolder; +import org.wso2.carbon.governance.api.exception.GovernanceException; +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.ParseException; import java.text.SimpleDateFormat; import java.util.Locale; public class LicenseManagerImpl implements LicenseManager { - private static Log log = LogFactory.getLog(DeviceManagerImpl.class); - private static final DateFormat format = new SimpleDateFormat("dd-mm-yyyy", Locale.ENGLISH); - - @Override - public License getLicense(final String deviceType, final String languageCode) - throws LicenseManagementException { - License deviceLicense = new License(); - LicenseConfig licenseConfig = DeviceManagementDataHolder.getInstance().getLicenseConfig(); - for (License license : licenseConfig.getLicenses()) { - if ((deviceType.equals(license.getName())) && - (languageCode.equals(license.getLanguage()))) { - deviceLicense = license; - break; - } - } - return deviceLicense; - } - - @Override - public void addLicense(License license) throws LicenseManagementException { - - } + private static Log log = LogFactory.getLog(DeviceManagementServiceProviderImpl.class); + private static final DateFormat format = new SimpleDateFormat("dd-mm-yyyy", Locale.ENGLISH); + + @Override + public License getLicense(final String deviceType, final String languageCode) throws LicenseManagementException { + GenericArtifactManager artifactManager = + GenericArtifactManagerFactory.getTenantAwareGovernanceArtifactManager(); + try { + GenericArtifact[] artifacts = artifactManager.findGenericArtifacts(new GenericArtifactFilter() { + @Override + public boolean matches(GenericArtifact artifact) throws GovernanceException { + String attributeNameVal = artifact.getAttribute( + 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 + "'"); + } + } + + private License populateLicense(GenericArtifact artifact) throws GovernanceException, ParseException { + 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); + } + } } diff --git a/components/device-mgt/org.wso2.carbon.device.mgt.core/src/main/java/org/wso2/carbon/device/mgt/core/operation/mgt/CommandOperation.java b/components/device-mgt/org.wso2.carbon.device.mgt.core/src/main/java/org/wso2/carbon/device/mgt/core/operation/mgt/CommandOperation.java index 37e65a8352..c135c41188 100644 --- a/components/device-mgt/org.wso2.carbon.device.mgt.core/src/main/java/org/wso2/carbon/device/mgt/core/operation/mgt/CommandOperation.java +++ b/components/device-mgt/org.wso2.carbon.device.mgt.core/src/main/java/org/wso2/carbon/device/mgt/core/operation/mgt/CommandOperation.java @@ -18,10 +18,18 @@ */ 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 { + private boolean enabled; + public boolean isEnabled() { + return enabled; + } + + public void setEnabled(boolean enabled) { + this.enabled = enabled; + } } diff --git a/components/device-mgt/org.wso2.carbon.device.mgt.core/src/main/java/org/wso2/carbon/device/mgt/core/operation/mgt/ConfigOperation.java b/components/device-mgt/org.wso2.carbon.device.mgt.core/src/main/java/org/wso2/carbon/device/mgt/core/operation/mgt/ConfigOperation.java index ec94611279..2d25687779 100644 --- a/components/device-mgt/org.wso2.carbon.device.mgt.core/src/main/java/org/wso2/carbon/device/mgt/core/operation/mgt/ConfigOperation.java +++ b/components/device-mgt/org.wso2.carbon.device.mgt.core/src/main/java/org/wso2/carbon/device/mgt/core/operation/mgt/ConfigOperation.java @@ -18,5 +18,59 @@ */ 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 properties; + + public ConfigOperation() { + properties = new ArrayList(); + } + + public List 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; + } + } + } diff --git a/components/device-mgt/org.wso2.carbon.device.mgt.core/src/main/java/org/wso2/carbon/device/mgt/core/operation/mgt/OperationManager.java b/components/device-mgt/org.wso2.carbon.device.mgt.core/src/main/java/org/wso2/carbon/device/mgt/core/operation/mgt/OperationManager.java index 98b521c988..f7d9048de3 100644 --- a/components/device-mgt/org.wso2.carbon.device.mgt.core/src/main/java/org/wso2/carbon/device/mgt/core/operation/mgt/OperationManager.java +++ b/components/device-mgt/org.wso2.carbon.device.mgt.core/src/main/java/org/wso2/carbon/device/mgt/core/operation/mgt/OperationManager.java @@ -18,6 +18,7 @@ package org.wso2.carbon.device.mgt.core.operation.mgt; import org.wso2.carbon.device.mgt.common.*; +import org.wso2.carbon.device.mgt.core.dto.DeviceType; import java.util.List; @@ -32,7 +33,7 @@ public interface OperationManager { * * @param operation Operation to be added * @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 */ public boolean addOperation(Operation operation, List devices) @@ -64,6 +65,6 @@ public interface OperationManager { * @return a list of Feature objects. * @throws org.wso2.carbon.device.mgt.common.FeatureManagementException */ - public List getFeaturesForDeviceType(String deviceType) throws FeatureManagementException; + public List getFeatures(String deviceType) throws FeatureManagementException; } \ No newline at end of file diff --git a/components/device-mgt/org.wso2.carbon.device.mgt.core/src/main/java/org/wso2/carbon/device/mgt/core/operation/mgt/OperationManagerImpl.java b/components/device-mgt/org.wso2.carbon.device.mgt.core/src/main/java/org/wso2/carbon/device/mgt/core/operation/mgt/OperationManagerImpl.java index 9d9923e0eb..13946eb07e 100644 --- a/components/device-mgt/org.wso2.carbon.device.mgt.core/src/main/java/org/wso2/carbon/device/mgt/core/operation/mgt/OperationManagerImpl.java +++ b/components/device-mgt/org.wso2.carbon.device.mgt.core/src/main/java/org/wso2/carbon/device/mgt/core/operation/mgt/OperationManagerImpl.java @@ -18,25 +18,56 @@ */ 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.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.OperationManagementDAOException; 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; +/** + * 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 { - private OperationDAO commandOperationDAO = OperationManagementDAOFactory.getCommandOperationDAO(); - private OperationDAO configOperationDAO = OperationManagementDAOFactory.getConfigOperationDAO(); - private OperationDAO simpleOperationDAO = OperationManagementDAOFactory.getSimpleOperationDAO(); + private static final Log log = LogFactory.getLog(OperationManagerImpl.class); + + 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 public boolean addOperation(Operation operation, List devices) throws OperationManagementException { 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) { + 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); } } @@ -52,7 +83,7 @@ public class OperationManagerImpl implements OperationManager { } @Override - public List getFeaturesForDeviceType(String deviceType) throws FeatureManagementException { + public List getFeatures(String deviceType) throws FeatureManagementException { return null; } diff --git a/components/device-mgt/org.wso2.carbon.device.mgt.core/src/main/java/org/wso2/carbon/device/mgt/core/operation/mgt/SimpleOperation.java b/components/device-mgt/org.wso2.carbon.device.mgt.core/src/main/java/org/wso2/carbon/device/mgt/core/operation/mgt/SimpleOperation.java index 2b967cd2a1..b2a57eccab 100644 --- a/components/device-mgt/org.wso2.carbon.device.mgt.core/src/main/java/org/wso2/carbon/device/mgt/core/operation/mgt/SimpleOperation.java +++ b/components/device-mgt/org.wso2.carbon.device.mgt.core/src/main/java/org/wso2/carbon/device/mgt/core/operation/mgt/SimpleOperation.java @@ -18,5 +18,7 @@ */ 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 { } diff --git a/components/device-mgt/org.wso2.carbon.device.mgt.core/src/main/java/org/wso2/carbon/device/mgt/core/operation/mgt/dao/OperationDAO.java b/components/device-mgt/org.wso2.carbon.device.mgt.core/src/main/java/org/wso2/carbon/device/mgt/core/operation/mgt/dao/OperationDAO.java index 0833f05981..00479d4908 100644 --- a/components/device-mgt/org.wso2.carbon.device.mgt.core/src/main/java/org/wso2/carbon/device/mgt/core/operation/mgt/dao/OperationDAO.java +++ b/components/device-mgt/org.wso2.carbon.device.mgt.core/src/main/java/org/wso2/carbon/device/mgt/core/operation/mgt/dao/OperationDAO.java @@ -18,5 +18,20 @@ */ 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 getOperations() throws OperationManagementDAOException; + } diff --git a/components/device-mgt/org.wso2.carbon.device.mgt.core/src/main/java/org/wso2/carbon/device/mgt/core/operation/mgt/dao/OperationManagementDAOException.java b/components/device-mgt/org.wso2.carbon.device.mgt.core/src/main/java/org/wso2/carbon/device/mgt/core/operation/mgt/dao/OperationManagementDAOException.java index 918259ebb1..e3e75f2afd 100644 --- a/components/device-mgt/org.wso2.carbon.device.mgt.core/src/main/java/org/wso2/carbon/device/mgt/core/operation/mgt/dao/OperationManagementDAOException.java +++ b/components/device-mgt/org.wso2.carbon.device.mgt.core/src/main/java/org/wso2/carbon/device/mgt/core/operation/mgt/dao/OperationManagementDAOException.java @@ -20,4 +20,40 @@ package org.wso2.carbon.device.mgt.core.operation.mgt.dao; 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); + } + + } diff --git a/components/device-mgt/org.wso2.carbon.device.mgt.core/src/main/java/org/wso2/carbon/device/mgt/core/operation/mgt/dao/OperationManagementDAOFactory.java b/components/device-mgt/org.wso2.carbon.device.mgt.core/src/main/java/org/wso2/carbon/device/mgt/core/operation/mgt/dao/OperationManagementDAOFactory.java index b3d52caf3d..20c1aa2d80 100644 --- a/components/device-mgt/org.wso2.carbon.device.mgt.core/src/main/java/org/wso2/carbon/device/mgt/core/operation/mgt/dao/OperationManagementDAOFactory.java +++ b/components/device-mgt/org.wso2.carbon.device.mgt.core/src/main/java/org/wso2/carbon/device/mgt/core/operation/mgt/dao/OperationManagementDAOFactory.java @@ -18,5 +18,130 @@ */ 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 { + + private static DataSource dataSource; + private static final Log log = LogFactory.getLog(OperationManagementDAOFactory.class); + private static ThreadLocal currentConnection = new ThreadLocal(); + + 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 jndiPropertyList = + jndiConfig.getJndiProperties(); + if (jndiPropertyList != null) { + Hashtable jndiProperties = new Hashtable(); + 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; + } + } diff --git a/components/device-mgt/org.wso2.carbon.device.mgt.core/src/main/java/org/wso2/carbon/device/mgt/core/operation/mgt/dao/OperationManagementDAOUtil.java b/components/device-mgt/org.wso2.carbon.device.mgt.core/src/main/java/org/wso2/carbon/device/mgt/core/operation/mgt/dao/OperationManagementDAOUtil.java index 266c664d18..bd0b5588c5 100644 --- a/components/device-mgt/org.wso2.carbon.device.mgt.core/src/main/java/org/wso2/carbon/device/mgt/core/operation/mgt/dao/OperationManagementDAOUtil.java +++ b/components/device-mgt/org.wso2.carbon.device.mgt.core/src/main/java/org/wso2/carbon/device/mgt/core/operation/mgt/dao/OperationManagementDAOUtil.java @@ -18,5 +18,32 @@ */ 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 { + + 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); + } + } + } + } diff --git a/components/device-mgt/org.wso2.carbon.device.mgt.core/src/main/java/org/wso2/carbon/device/mgt/core/operation/mgt/dao/OperationMappingDAO.java b/components/device-mgt/org.wso2.carbon.device.mgt.core/src/main/java/org/wso2/carbon/device/mgt/core/operation/mgt/dao/OperationMappingDAO.java index f4405a5813..39873c306a 100644 --- a/components/device-mgt/org.wso2.carbon.device.mgt.core/src/main/java/org/wso2/carbon/device/mgt/core/operation/mgt/dao/OperationMappingDAO.java +++ b/components/device-mgt/org.wso2.carbon.device.mgt.core/src/main/java/org/wso2/carbon/device/mgt/core/operation/mgt/dao/OperationMappingDAO.java @@ -18,5 +18,14 @@ */ 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 deviceIds) throws OperationManagementDAOException; + + void removeOperationMapping(int operationId, List deviceIds) throws OperationManagementDAOException; + } diff --git a/components/device-mgt/org.wso2.carbon.device.mgt.core/src/main/java/org/wso2/carbon/device/mgt/core/operation/mgt/dao/impl/AbstractOperationDAO.java b/components/device-mgt/org.wso2.carbon.device.mgt.core/src/main/java/org/wso2/carbon/device/mgt/core/operation/mgt/dao/impl/AbstractOperationDAO.java index c89087fbef..fa7e907f26 100644 --- a/components/device-mgt/org.wso2.carbon.device.mgt.core/src/main/java/org/wso2/carbon/device/mgt/core/operation/mgt/dao/impl/AbstractOperationDAO.java +++ b/components/device-mgt/org.wso2.carbon.device.mgt.core/src/main/java/org/wso2/carbon/device/mgt/core/operation/mgt/dao/impl/AbstractOperationDAO.java @@ -18,5 +18,53 @@ */ 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); + } + } + } diff --git a/components/device-mgt/org.wso2.carbon.device.mgt.core/src/main/java/org/wso2/carbon/device/mgt/core/operation/mgt/dao/impl/CommandOperationDAOImpl.java b/components/device-mgt/org.wso2.carbon.device.mgt.core/src/main/java/org/wso2/carbon/device/mgt/core/operation/mgt/dao/impl/CommandOperationDAOImpl.java index bfb95d7034..ba3880daee 100644 --- a/components/device-mgt/org.wso2.carbon.device.mgt.core/src/main/java/org/wso2/carbon/device/mgt/core/operation/mgt/dao/impl/CommandOperationDAOImpl.java +++ b/components/device-mgt/org.wso2.carbon.device.mgt.core/src/main/java/org/wso2/carbon/device/mgt/core/operation/mgt/dao/impl/CommandOperationDAOImpl.java @@ -18,11 +18,17 @@ */ 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.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 java.sql.Connection; +import java.sql.PreparedStatement; +import java.sql.ResultSet; +import java.sql.SQLException; import java.util.List; public class CommandOperationDAOImpl extends AbstractOperationDAO { @@ -32,20 +38,34 @@ public class CommandOperationDAOImpl extends AbstractOperationDAO { } @Override - public boolean addOperation(Operation operation) throws OperationManagementDAOException { - CommandOperation booleanOp = (CommandOperation) operation; - addOperationMetadata(); - return false; + public int addOperation(Operation operation) throws OperationManagementDAOException { + int operationId = super.addOperation(operation); + CommandOperation commandOp = (CommandOperation) operation; + 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 - public boolean updateOperation(Operation operation) throws OperationManagementDAOException { - return false; + public int updateOperation(Operation operation) throws OperationManagementDAOException { + return 0; } @Override - public boolean deleteOperation(int id) throws OperationManagementDAOException { - return false; + public int deleteOperation(int id) throws OperationManagementDAOException { + return 0; } @Override diff --git a/components/device-mgt/org.wso2.carbon.device.mgt.core/src/main/java/org/wso2/carbon/device/mgt/core/operation/mgt/dao/impl/ConfigOperationDAOImpl.java b/components/device-mgt/org.wso2.carbon.device.mgt.core/src/main/java/org/wso2/carbon/device/mgt/core/operation/mgt/dao/impl/ConfigOperationDAOImpl.java index 9a3a351138..1cc87e76a9 100644 --- a/components/device-mgt/org.wso2.carbon.device.mgt.core/src/main/java/org/wso2/carbon/device/mgt/core/operation/mgt/dao/impl/ConfigOperationDAOImpl.java +++ b/components/device-mgt/org.wso2.carbon.device.mgt.core/src/main/java/org/wso2/carbon/device/mgt/core/operation/mgt/dao/impl/ConfigOperationDAOImpl.java @@ -18,5 +18,42 @@ */ 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 getOperations() throws OperationManagementDAOException { + return null; + } + } diff --git a/components/device-mgt/org.wso2.carbon.device.mgt.core/src/main/java/org/wso2/carbon/device/mgt/core/operation/mgt/dao/impl/OperationMappingDAOImpl.java b/components/device-mgt/org.wso2.carbon.device.mgt.core/src/main/java/org/wso2/carbon/device/mgt/core/operation/mgt/dao/impl/OperationMappingDAOImpl.java index c98367ca00..ec05cc2129 100644 --- a/components/device-mgt/org.wso2.carbon.device.mgt.core/src/main/java/org/wso2/carbon/device/mgt/core/operation/mgt/dao/impl/OperationMappingDAOImpl.java +++ b/components/device-mgt/org.wso2.carbon.device.mgt.core/src/main/java/org/wso2/carbon/device/mgt/core/operation/mgt/dao/impl/OperationMappingDAOImpl.java @@ -18,5 +18,51 @@ */ 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 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 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); + } + } + } diff --git a/components/device-mgt/org.wso2.carbon.device.mgt.core/src/main/java/org/wso2/carbon/device/mgt/core/operation/mgt/dao/impl/SimpleOperationDAOImpl.java b/components/device-mgt/org.wso2.carbon.device.mgt.core/src/main/java/org/wso2/carbon/device/mgt/core/operation/mgt/dao/impl/SimpleOperationDAOImpl.java index 107d08953d..43ba587889 100644 --- a/components/device-mgt/org.wso2.carbon.device.mgt.core/src/main/java/org/wso2/carbon/device/mgt/core/operation/mgt/dao/impl/SimpleOperationDAOImpl.java +++ b/components/device-mgt/org.wso2.carbon.device.mgt.core/src/main/java/org/wso2/carbon/device/mgt/core/operation/mgt/dao/impl/SimpleOperationDAOImpl.java @@ -18,5 +18,36 @@ */ 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 getOperations() throws OperationManagementDAOException { + return null; + } + } diff --git a/components/device-mgt/org.wso2.carbon.device.mgt.core/src/main/java/org/wso2/carbon/device/mgt/core/service/DeviceManagementServiceImpl.java b/components/device-mgt/org.wso2.carbon.device.mgt.core/src/main/java/org/wso2/carbon/device/mgt/core/service/DeviceManagementServiceImpl.java index 9b742f7054..ba6b9df2bd 100644 --- a/components/device-mgt/org.wso2.carbon.device.mgt.core/src/main/java/org/wso2/carbon/device/mgt/core/service/DeviceManagementServiceImpl.java +++ b/components/device-mgt/org.wso2.carbon.device.mgt.core/src/main/java/org/wso2/carbon/device/mgt/core/service/DeviceManagementServiceImpl.java @@ -17,71 +17,111 @@ */ package org.wso2.carbon.device.mgt.core.service; -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.operation.mgt.OperationManager; -import org.wso2.carbon.device.mgt.core.DeviceManager; +import org.wso2.carbon.device.mgt.common.*; +import org.wso2.carbon.device.mgt.core.DeviceManagementService; +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.operation.mgt.Operation; +import org.wso2.carbon.device.mgt.core.operation.mgt.OperationManagementException; import org.wso2.carbon.device.mgt.core.internal.DeviceManagementDataHolder; import java.util.List; -public class DeviceManagementServiceImpl implements DeviceManager { +public class DeviceManagementServiceImpl implements DeviceManagementService { - @Override + @Override + public String getProviderType() { + return null; + } + + @Override public boolean enrollDevice(Device device) throws DeviceManagementException { - return DeviceManagementDataHolder.getInstance().getDeviceManager().enrollDevice(device); + return DeviceManagementDataHolder.getInstance().getDeviceManagementProvider().enrollDevice(device); } @Override public boolean modifyEnrollment(Device device) throws DeviceManagementException { - return DeviceManagementDataHolder.getInstance().getDeviceManager().modifyEnrollment(device); + return DeviceManagementDataHolder.getInstance().getDeviceManagementProvider().modifyEnrollment(device); } @Override public boolean disenrollDevice(DeviceIdentifier deviceId) throws DeviceManagementException { - return DeviceManagementDataHolder.getInstance().getDeviceManager().disenrollDevice(deviceId); + return DeviceManagementDataHolder.getInstance().getDeviceManagementProvider().disenrollDevice(deviceId); } @Override public boolean isEnrolled(DeviceIdentifier deviceId) throws DeviceManagementException { - return DeviceManagementDataHolder.getInstance().getDeviceManager().isEnrolled(deviceId); + return DeviceManagementDataHolder.getInstance().getDeviceManagementProvider().isEnrolled(deviceId); } @Override public boolean isActive(DeviceIdentifier deviceId) throws DeviceManagementException { - return DeviceManagementDataHolder.getInstance().getDeviceManager().isActive(deviceId); + return DeviceManagementDataHolder.getInstance().getDeviceManagementProvider().isActive(deviceId); } @Override public boolean setActive(DeviceIdentifier deviceId, boolean status) throws DeviceManagementException { - return DeviceManagementDataHolder.getInstance().getDeviceManager().setActive(deviceId, status); + return DeviceManagementDataHolder.getInstance().getDeviceManagementProvider().setActive(deviceId, status); } - @Override + @Override + public List getAllDevices() throws DeviceManagementException { + return null; //To change body of implemented methods use File | Settings | File Templates. + } + + @Override public List getAllDevices(String type) throws DeviceManagementException { - return DeviceManagementDataHolder.getInstance().getDeviceManager().getAllDevices(type); + return DeviceManagementDataHolder.getInstance().getDeviceManagementProvider().getAllDevices(type); } @Override public org.wso2.carbon.device.mgt.common.Device getDevice(DeviceIdentifier deviceId) throws DeviceManagementException { - return DeviceManagementDataHolder.getInstance().getDeviceManager().getDevice(deviceId); + return DeviceManagementDataHolder.getInstance().getDeviceManagementProvider().getDevice(deviceId); } @Override public boolean updateDeviceInfo(Device device) throws DeviceManagementException { - return DeviceManagementDataHolder.getInstance().getDeviceManager().updateDeviceInfo(device); + return DeviceManagementDataHolder.getInstance().getDeviceManagementProvider().updateDeviceInfo(device); } @Override 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 OperationManager getOperationManager(String type) throws DeviceManagementException { - return DeviceManagementDataHolder.getInstance().getDeviceManager().getOperationManager(type); - } + @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 devices) throws OperationManagementException { + return DeviceManagementDataHolder.getInstance().getDeviceManagementProvider().addOperation(operation, devices); + } + + @Override + public List getOperations(DeviceIdentifier deviceId) throws OperationManagementException { + return DeviceManagementDataHolder.getInstance().getDeviceManagementProvider().getOperations(deviceId); + } + + @Override + public List getPendingOperations(DeviceIdentifier deviceId) throws OperationManagementException { + return DeviceManagementDataHolder.getInstance().getDeviceManagementProvider().getPendingOperations(deviceId); + } + + @Override + public List getFeaturesForDeviceType(String deviceType) throws FeatureManagementException { + return DeviceManagementDataHolder.getInstance().getDeviceManagementProvider().getFeaturesForDeviceType( + deviceType); + } } diff --git a/components/device-mgt/org.wso2.carbon.device.mgt.core/src/main/java/org/wso2/carbon/device/mgt/core/util/DeviceManagerUtil.java b/components/device-mgt/org.wso2.carbon.device.mgt.core/src/main/java/org/wso2/carbon/device/mgt/core/util/DeviceManagerUtil.java index fd4a442164..110e46dacf 100644 --- a/components/device-mgt/org.wso2.carbon.device.mgt.core/src/main/java/org/wso2/carbon/device/mgt/core/util/DeviceManagerUtil.java +++ b/components/device-mgt/org.wso2.carbon.device.mgt.core/src/main/java/org/wso2/carbon/device/mgt/core/util/DeviceManagerUtil.java @@ -50,11 +50,10 @@ public final class DeviceManagerUtil { DocumentBuilder docBuilder = factory.newDocumentBuilder(); return docBuilder.parse(file); } catch (Exception e) { - throw new DeviceManagementException( - "Error occurred while parsing file, while converting " + - "to a org.w3c.dom.Document : " + e.getMessage(), e); + throw new DeviceManagementException("Error occurred while parsing file, while converting " + + "to a org.w3c.dom.Document", e); } - } + } /** * Resolve data source from the data source definition. @@ -65,16 +64,13 @@ public final class DeviceManagerUtil { public static DataSource resolveDataSource(DataSourceConfig config) { DataSource dataSource = null; if (config == null) { - throw new RuntimeException( - "Device Management Repository data source configuration " + - "is null and thus, is not initialized"); + 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"); + log.debug("Initializing Device Management Repository data source using the JNDI Lookup Definition"); } List jndiPropertyList = jndiConfig.getJndiProperties(); @@ -83,11 +79,9 @@ public final class DeviceManagerUtil { for (JNDILookupDefinition.JNDIProperty prop : jndiPropertyList) { jndiProperties.put(prop.getName(), prop.getValue()); } - dataSource = DeviceManagementDAOUtil - .lookupDataSource(jndiConfig.getJndiName(), jndiProperties); + dataSource = DeviceManagementDAOUtil.lookupDataSource(jndiConfig.getJndiName(), jndiProperties); } else { - dataSource = - DeviceManagementDAOUtil.lookupDataSource(jndiConfig.getJndiName(), null); + dataSource = DeviceManagementDAOUtil.lookupDataSource(jndiConfig.getJndiName(), null); } } return dataSource; @@ -103,7 +97,7 @@ public final class DeviceManagerUtil { boolean status; try { DeviceTypeDAO deviceTypeDAO = DeviceManagementDAOFactory.getDeviceTypeDAO(); - Integer deviceTypeId = deviceTypeDAO.getDeviceTypeIdByDeviceTypeName(deviceType); + DeviceType deviceTypeId = deviceTypeDAO.getDeviceType(deviceType); if (deviceTypeId == null) { DeviceType dt = new DeviceType(); dt.setName(deviceType); @@ -111,14 +105,14 @@ public final class DeviceManagerUtil { } status = true; } catch (DeviceManagementDAOException e) { - String msg = "Error occurred while registering the device type " + deviceType; - throw new DeviceManagementException(msg, e); + throw new DeviceManagementException("Error occurred while registering the device type '" + + deviceType + "'", e); } 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 * @return status of the operation @@ -126,25 +120,17 @@ public final class DeviceManagerUtil { public static boolean unregisterDeviceType(String deviceType) throws DeviceManagementException { try { DeviceTypeDAO deviceTypeDAO = DeviceManagementDAOFactory.getDeviceTypeDAO(); - Integer deviceTypeId = deviceTypeDAO.getDeviceTypeIdByDeviceTypeName(deviceType); + DeviceType deviceTypeId = deviceTypeDAO.getDeviceType(deviceType); if (deviceTypeId == null) { DeviceType dt = new DeviceType(); dt.setName(deviceType); - deviceTypeDAO.removeDeviceType(dt); + deviceTypeDAO.removeDeviceType(deviceType); } return true; } catch (DeviceManagementDAOException e) { - String msg = "Error occurred while registering the device type " + deviceType; - throw new DeviceManagementException(msg, e); - } - } - - public static Map convertPropertiesToMap(List properties) { - Map propertiesMap = new HashMap(); - for (Device.Property prop : properties) { - propertiesMap.put(prop.getName(), prop.getValue()); + throw new DeviceManagementException("Error occurred while registering the device type '" + + deviceType + "'", e); } - return propertiesMap; } } diff --git a/components/device-mgt/org.wso2.carbon.device.mgt.core/src/test/java/org/wso2/carbon/device/mgt/core/DeviceManagementBaseTest.java b/components/device-mgt/org.wso2.carbon.device.mgt.core/src/test/java/org/wso2/carbon/device/mgt/core/DeviceManagementBaseTest.java index d2a329f503..e0e32265ec 100644 --- a/components/device-mgt/org.wso2.carbon.device.mgt.core/src/test/java/org/wso2/carbon/device/mgt/core/DeviceManagementBaseTest.java +++ b/components/device-mgt/org.wso2.carbon.device.mgt.core/src/test/java/org/wso2/carbon/device/mgt/core/DeviceManagementBaseTest.java @@ -18,5 +18,53 @@ */ 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 { + + 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; + } + } diff --git a/components/device-mgt/org.wso2.carbon.device.mgt.core/src/test/java/org/wso2/carbon/device/mgt/core/DeviceManagementRepositoryTests.java b/components/device-mgt/org.wso2.carbon.device.mgt.core/src/test/java/org/wso2/carbon/device/mgt/core/DeviceManagementRepositoryTests.java index 79c19a0e0e..2b85365f2d 100644 --- a/components/device-mgt/org.wso2.carbon.device.mgt.core/src/test/java/org/wso2/carbon/device/mgt/core/DeviceManagementRepositoryTests.java +++ b/components/device-mgt/org.wso2.carbon.device.mgt.core/src/test/java/org/wso2/carbon/device/mgt/core/DeviceManagementRepositoryTests.java @@ -20,7 +20,8 @@ package org.wso2.carbon.device.mgt.core; import org.testng.Assert; import org.testng.annotations.BeforeClass; 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 { @@ -33,23 +34,27 @@ public class DeviceManagementRepositoryTests { @Test public void testAddDeviceManagementService() { - DeviceManagerService sourceProvider = new TestDeviceManagerService(); - this.getRepository().addDeviceManagementProvider(sourceProvider); - - DeviceManagerService targetProvider = - this.getRepository().getDeviceManagementProvider(TestDeviceManagerService.DEVICE_TYPE_TEST); - + DeviceManager sourceProvider = new TestDeviceManager(); + try { + this.getRepository().addDeviceManagementProvider(sourceProvider); + } catch (DeviceManagementException e) { + Assert.fail("Unexpected error occurred while invoking addDeviceManagementProvider functionality", e); + } + DeviceManager targetProvider = + this.getRepository().getDeviceManagementProvider(TestDeviceManager.DEVICE_TYPE_TEST); Assert.assertEquals(targetProvider.getProviderType(), sourceProvider.getProviderType()); } @Test(dependsOnMethods = "testAddDeviceManagementService") public void testRemoveDeviceManagementService() { - DeviceManagerService sourceProvider = new TestDeviceManagerService(); - this.getRepository().removeDeviceManagementProvider(sourceProvider); - - DeviceManagerService targetProvider = - this.getRepository().getDeviceManagementProvider(TestDeviceManagerService.DEVICE_TYPE_TEST); - + DeviceManager sourceProvider = new TestDeviceManager(); + try { + this.getRepository().removeDeviceManagementProvider(sourceProvider); + } catch (DeviceManagementException e) { + Assert.fail("Unexpected error occurred while invoking removeDeviceManagementProvider functionality", e); + } + DeviceManager targetProvider = + this.getRepository().getDeviceManagementProvider(TestDeviceManager.DEVICE_TYPE_TEST); Assert.assertNull(targetProvider); } diff --git a/components/device-mgt/org.wso2.carbon.device.mgt.core/src/test/java/org/wso2/carbon/device/mgt/core/DeviceOperationManagementTests.java b/components/device-mgt/org.wso2.carbon.device.mgt.core/src/test/java/org/wso2/carbon/device/mgt/core/DeviceOperationManagementTests.java index 8fa8d1dca9..5038dc8acf 100644 --- a/components/device-mgt/org.wso2.carbon.device.mgt.core/src/test/java/org/wso2/carbon/device/mgt/core/DeviceOperationManagementTests.java +++ b/components/device-mgt/org.wso2.carbon.device.mgt.core/src/test/java/org/wso2/carbon/device/mgt/core/DeviceOperationManagementTests.java @@ -18,5 +18,59 @@ */ 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 deviceIds = new ArrayList(); + 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(); + } + } + + } diff --git a/components/device-mgt/org.wso2.carbon.device.mgt.core/src/test/java/org/wso2/carbon/device/mgt/core/dao/DeviceManagementDAOTests.java b/components/device-mgt/org.wso2.carbon.device.mgt.core/src/test/java/org/wso2/carbon/device/mgt/core/dao/DeviceManagementDAOTests.java index 7ccc88d343..486859d61f 100644 --- a/components/device-mgt/org.wso2.carbon.device.mgt.core/src/test/java/org/wso2/carbon/device/mgt/core/dao/DeviceManagementDAOTests.java +++ b/components/device-mgt/org.wso2.carbon.device.mgt.core/src/test/java/org/wso2/carbon/device/mgt/core/dao/DeviceManagementDAOTests.java @@ -114,17 +114,17 @@ public class DeviceManagementDAOTests { deviceType.setName("IOS"); deviceTypeMgtDAO.addDeviceType(deviceType); - Long id = null; + int id = -1; Connection conn = 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 { conn = this.getDataSource().getConnection(); stmt = conn.prepareStatement(sql); ResultSet rs = stmt.executeQuery(); if (rs.next()) { - id = rs.getLong("ID"); + id = rs.getInt("ID"); } } catch (SQLException e) { throw new DeviceManagementDAOException("error in fetch device type by name IOS", e); @@ -147,9 +147,9 @@ public class DeviceManagementDAOTests { device.setDeviceIdentificationId("111"); 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.setOwnerId("111"); device.setTenantId(-1234); diff --git a/components/device-mgt/org.wso2.carbon.device.mgt.core/src/test/resources/sql/h2.sql b/components/device-mgt/org.wso2.carbon.device.mgt.core/src/test/resources/sql/h2.sql index e69de29bb2..c87c9fcf4e 100644 --- a/components/device-mgt/org.wso2.carbon.device.mgt.core/src/test/resources/sql/h2.sql +++ b/components/device-mgt/org.wso2.carbon.device.mgt.core/src/test/resources/sql/h2.sql @@ -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 +) + diff --git a/components/device-mgt/org.wso2.carbon.device.mgt.core/src/test/resources/testng.xml b/components/device-mgt/org.wso2.carbon.device.mgt.core/src/test/resources/testng.xml index dbce1ebf52..998633e06b 100644 --- a/components/device-mgt/org.wso2.carbon.device.mgt.core/src/test/resources/testng.xml +++ b/components/device-mgt/org.wso2.carbon.device.mgt.core/src/test/resources/testng.xml @@ -28,6 +28,7 @@ + \ No newline at end of file diff --git a/pom.xml b/pom.xml index 5191ff4a60..98ea945241 100644 --- a/pom.xml +++ b/pom.xml @@ -93,7 +93,7 @@ org.wso2.carbon.base ${carbon.kernel.version} - +