From 9adfb05b37ed705fe5316d49d369bc79196c55d1 Mon Sep 17 00:00:00 2001 From: hasuniea Date: Thu, 29 Oct 2015 14:12:52 +0530 Subject: [PATCH] refactored WindowsFeatureManager --- .../impl/windows/WindowsDeviceManager.java | 5 + .../impl/windows/WindowsFeatureManager.java | 97 ++++++++++++++++--- .../impl/windows/dao/WindowsDAOFactory.java | 3 +- .../dao/impl/WindowsFeatureDAOImpl.java | 46 +++++++-- .../dbscripts/plugins/windows/mssql.sql | 4 +- .../dbscripts/plugins/windows/mysql.sql | 13 --- .../dbscripts/plugins/windows/oracle.sql | 14 --- .../dbscripts/plugins/windows/postgresql.sql | 13 --- 8 files changed, 126 insertions(+), 69 deletions(-) diff --git a/components/device-mgt/org.wso2.carbon.device.mgt.mobile.impl/src/main/java/org/wso2/carbon/device/mgt/mobile/impl/windows/WindowsDeviceManager.java b/components/device-mgt/org.wso2.carbon.device.mgt.mobile.impl/src/main/java/org/wso2/carbon/device/mgt/mobile/impl/windows/WindowsDeviceManager.java index 18e9d6360..106b1def7 100644 --- a/components/device-mgt/org.wso2.carbon.device.mgt.mobile.impl/src/main/java/org/wso2/carbon/device/mgt/mobile/impl/windows/WindowsDeviceManager.java +++ b/components/device-mgt/org.wso2.carbon.device.mgt.mobile.impl/src/main/java/org/wso2/carbon/device/mgt/mobile/impl/windows/WindowsDeviceManager.java @@ -57,11 +57,16 @@ public class WindowsDeviceManager implements DeviceManager { public WindowsDeviceManager() { this.daoFactory = new WindowsDAOFactory(); this.licenseManager = new RegistryBasedLicenseManager(); + License defaultLicense = WindowsPluginUtils.getDefaultLicense(); + try { licenseManager.addLicense(WindowsDeviceManagementService.DEVICE_TYPE_WINDOWS, defaultLicense); + featureManager.addSupportedFeaturesToDB(); } catch (LicenseManagementException e) { log.error("Error occurred while adding default license for Windows devices", e); + } catch (DeviceManagementException e) { + log.error("Error occurred while adding supported device features for Windows platform", e); } } diff --git a/components/device-mgt/org.wso2.carbon.device.mgt.mobile.impl/src/main/java/org/wso2/carbon/device/mgt/mobile/impl/windows/WindowsFeatureManager.java b/components/device-mgt/org.wso2.carbon.device.mgt.mobile.impl/src/main/java/org/wso2/carbon/device/mgt/mobile/impl/windows/WindowsFeatureManager.java index 889d40f26..21396b766 100644 --- a/components/device-mgt/org.wso2.carbon.device.mgt.mobile.impl/src/main/java/org/wso2/carbon/device/mgt/mobile/impl/windows/WindowsFeatureManager.java +++ b/components/device-mgt/org.wso2.carbon.device.mgt.mobile.impl/src/main/java/org/wso2/carbon/device/mgt/mobile/impl/windows/WindowsFeatureManager.java @@ -18,12 +18,9 @@ */ package org.wso2.carbon.device.mgt.mobile.impl.windows; -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.Feature; import org.wso2.carbon.device.mgt.common.FeatureManager; -import org.wso2.carbon.device.mgt.mobile.dao.AbstractMobileDeviceManagementDAOFactory; import org.wso2.carbon.device.mgt.mobile.dao.MobileDeviceManagementDAOException; import org.wso2.carbon.device.mgt.mobile.dao.MobileDeviceManagementDAOFactory; import org.wso2.carbon.device.mgt.mobile.dao.MobileFeatureDAO; @@ -37,7 +34,6 @@ import java.util.List; public class WindowsFeatureManager implements FeatureManager { private MobileFeatureDAO featureDAO; - private static final Log log = LogFactory.getLog(WindowsFeatureManager.class); public WindowsFeatureManager() { MobileDeviceManagementDAOFactory daoFactory = new WindowsDAOFactory(); @@ -47,27 +43,39 @@ public class WindowsFeatureManager implements FeatureManager { @Override public boolean addFeature(Feature feature) throws DeviceManagementException { try { - WindowsDAOFactory.beginTransaction(); + WindowsDAOFactory.beginTransaction(); MobileFeature mobileFeature = MobileDeviceManagementUtil.convertToMobileFeature(feature); featureDAO.addFeature(mobileFeature); - WindowsDAOFactory.commitTransaction(); + WindowsDAOFactory.commitTransaction(); return true; } catch (MobileDeviceManagementDAOException e) { - WindowsDAOFactory.rollbackTransaction(); + WindowsDAOFactory.rollbackTransaction(); throw new DeviceManagementException("Error occurred while adding the feature", e); } } @Override - public boolean addFeatures(List list) throws DeviceManagementException { - return false; + public boolean addFeatures(List features) throws DeviceManagementException { + List mobileFeatures = new ArrayList(); + for (Feature feature : features) { + mobileFeatures.add(MobileDeviceManagementUtil.convertToMobileFeature(feature)); + } + try { + WindowsDAOFactory.beginTransaction(); + featureDAO.addFeatures(mobileFeatures); + WindowsDAOFactory.commitTransaction(); + return true; + } catch (MobileDeviceManagementDAOException e) { + WindowsDAOFactory.rollbackTransaction(); + throw new DeviceManagementException("Error occurred while adding the features", e); + } } @Override public Feature getFeature(String name) throws DeviceManagementException { try { MobileFeature mobileFeature = featureDAO.getFeatureByCode(name); - Feature feature = MobileDeviceManagementUtil.convertToFeature(mobileFeature); + Feature feature = MobileDeviceManagementUtil.convertToFeature(mobileFeature); return feature; } catch (MobileDeviceManagementDAOException e) { throw new DeviceManagementException("Error occurred while retrieving the feature", e); @@ -86,7 +94,7 @@ public class WindowsFeatureManager implements FeatureManager { return featureList; } catch (MobileDeviceManagementDAOException e) { throw new DeviceManagementException("Error occurred while retrieving the list of features registered for " + - "Android platform", e); + "Windows platform", e); } } @@ -94,12 +102,12 @@ public class WindowsFeatureManager implements FeatureManager { public boolean removeFeature(String code) throws DeviceManagementException { boolean status; try { - WindowsDAOFactory.beginTransaction(); + WindowsDAOFactory.beginTransaction(); featureDAO.deleteFeatureByCode(code); - WindowsDAOFactory.commitTransaction(); + WindowsDAOFactory.commitTransaction(); status = true; } catch (MobileDeviceManagementDAOException e) { - WindowsDAOFactory.rollbackTransaction(); + WindowsDAOFactory.rollbackTransaction(); throw new DeviceManagementException("Error occurred while removing the feature", e); } return status; @@ -107,7 +115,64 @@ public class WindowsFeatureManager implements FeatureManager { @Override public boolean addSupportedFeaturesToDB() throws DeviceManagementException { - return false; + synchronized (this) { + List supportedFeatures = getSupportedFeatures(); + List existingFeatures = this.getFeatures(); + List missingFeatures = MobileDeviceManagementUtil. + getMissingFeatures(supportedFeatures, existingFeatures); + if (missingFeatures.size() > 0) { + return this.addFeatures(missingFeatures); + } + return true; + } } -} \ No newline at end of file + public static List getSupportedFeatures() { + List supportedFeatures = new ArrayList(); + Feature feature = new Feature(); + feature.setCode("DEVICE_LOCK"); + feature.setName("Device Lock"); + feature.setDescription("Lock the device"); + supportedFeatures.add(feature); + feature = new Feature(); + feature.setCode("CAMERA"); + feature.setName("camera"); + feature.setDescription("Enable or disable camera"); + supportedFeatures.add(feature); + feature = new Feature(); + feature.setCode("DEVICE_INFO"); + feature.setName("Device info"); + feature.setDescription("Request device information"); + supportedFeatures.add(feature); + feature = new Feature(); + feature.setCode("WIPE_DATA"); + feature.setName("Wipe Data"); + feature.setDescription("Factory reset the device"); + supportedFeatures.add(feature); + feature = new Feature(); + feature.setCode("ENCRYPT_STORAGE"); + feature.setName("Encrypt storage"); + feature.setDescription("Encrypt storage"); + supportedFeatures.add(feature); + feature = new Feature(); + feature.setCode("DEVICE_RING"); + feature.setName("Ring"); + feature.setDescription("Ring the device"); + supportedFeatures.add(feature); + feature = new Feature(); + feature.setCode("PASSCODE_POLICY"); + feature.setName("Password Policy"); + feature.setDescription("Set passcode policy"); + supportedFeatures.add(feature); + feature = new Feature(); + feature.setCode("DISENROLL"); + feature.setName("DisEnroll"); + feature.setDescription("DisEnroll the device"); + supportedFeatures.add(feature); + feature = new Feature(); + feature.setCode("LOCK_RESET"); + feature.setName("LockReset"); + feature.setDescription("Lock Reset device"); + return supportedFeatures; + } +} diff --git a/components/device-mgt/org.wso2.carbon.device.mgt.mobile.impl/src/main/java/org/wso2/carbon/device/mgt/mobile/impl/windows/dao/WindowsDAOFactory.java b/components/device-mgt/org.wso2.carbon.device.mgt.mobile.impl/src/main/java/org/wso2/carbon/device/mgt/mobile/impl/windows/dao/WindowsDAOFactory.java index f9e930d8b..0cf128d4c 100644 --- a/components/device-mgt/org.wso2.carbon.device.mgt.mobile.impl/src/main/java/org/wso2/carbon/device/mgt/mobile/impl/windows/dao/WindowsDAOFactory.java +++ b/components/device-mgt/org.wso2.carbon.device.mgt.mobile.impl/src/main/java/org/wso2/carbon/device/mgt/mobile/impl/windows/dao/WindowsDAOFactory.java @@ -26,6 +26,7 @@ import org.wso2.carbon.device.mgt.mobile.dao.MobileDeviceDAO; import org.wso2.carbon.device.mgt.mobile.dao.MobileDeviceManagementDAOException; import org.wso2.carbon.device.mgt.mobile.dao.MobileFeatureDAO; import org.wso2.carbon.device.mgt.mobile.impl.windows.dao.impl.WindowsDeviceDAOImpl; +import org.wso2.carbon.device.mgt.mobile.impl.windows.dao.impl.WindowsFeatureDAOImpl; import javax.sql.DataSource; import java.sql.Connection; @@ -48,7 +49,7 @@ public class WindowsDAOFactory extends AbstractMobileDeviceManagementDAOFactory @Override public MobileFeatureDAO getMobileFeatureDAO() { - return null; + return new WindowsFeatureDAOImpl(); } public static void beginTransaction() throws MobileDeviceManagementDAOException { diff --git a/components/device-mgt/org.wso2.carbon.device.mgt.mobile.impl/src/main/java/org/wso2/carbon/device/mgt/mobile/impl/windows/dao/impl/WindowsFeatureDAOImpl.java b/components/device-mgt/org.wso2.carbon.device.mgt.mobile.impl/src/main/java/org/wso2/carbon/device/mgt/mobile/impl/windows/dao/impl/WindowsFeatureDAOImpl.java index a8c110942..682767071 100644 --- a/components/device-mgt/org.wso2.carbon.device.mgt.mobile.impl/src/main/java/org/wso2/carbon/device/mgt/mobile/impl/windows/dao/impl/WindowsFeatureDAOImpl.java +++ b/components/device-mgt/org.wso2.carbon.device.mgt.mobile.impl/src/main/java/org/wso2/carbon/device/mgt/mobile/impl/windows/dao/impl/WindowsFeatureDAOImpl.java @@ -46,6 +46,7 @@ public class WindowsFeatureDAOImpl implements MobileFeatureDAO { public WindowsFeatureDAOImpl() { } + @Override public boolean addFeature(MobileFeature mobileFeature) throws MobileDeviceManagementDAOException { PreparedStatement stmt = null; @@ -53,7 +54,7 @@ public class WindowsFeatureDAOImpl implements MobileFeatureDAO { Connection conn; try { conn = WindowsDAOFactory.getConnection(); - String sql = "INSERT INTO WINDOWS_FEATURE(CODE, NAME, DESCRIPTION) VALUES (?, ?, ?)"; + String sql = "INSERT INTO WIN_FEATURE(CODE, NAME, DESCRIPTION) VALUES (?, ?, ?)"; stmt = conn.prepareStatement(sql); stmt.setString(1, mobileFeature.getCode()); stmt.setString(2, mobileFeature.getName()); @@ -71,6 +72,31 @@ public class WindowsFeatureDAOImpl implements MobileFeatureDAO { return status; } + @Override + public boolean addFeatures(List mobileFeatures) throws MobileDeviceManagementDAOException { + PreparedStatement stmt = null; + boolean status = false; + Connection conn; + try { + conn = WindowsDAOFactory.getConnection(); + stmt = conn.prepareStatement("INSERT INTO WIN_FEATURE(CODE, NAME, DESCRIPTION) VALUES (?, ?, ?)"); + for (MobileFeature mobileFeature : mobileFeatures) { + stmt.setString(1, mobileFeature.getCode()); + stmt.setString(2, mobileFeature.getName()); + stmt.setString(3, mobileFeature.getDescription()); + stmt.addBatch(); + } + stmt.executeBatch(); + status = true; + } catch (SQLException e) { + throw new WindowsFeatureManagementDAOException( + "Error occurred while adding windows features into the metadata repository", e); + } finally { + MobileDeviceManagementDAOUtil.cleanupResources(stmt, null); + } + return status; + } + @Override public boolean updateFeature(MobileFeature mobileFeature) throws MobileDeviceManagementDAOException { boolean status = false; @@ -79,7 +105,7 @@ public class WindowsFeatureDAOImpl implements MobileFeatureDAO { try { conn = WindowsDAOFactory.getConnection(); String updateDBQuery = - "UPDATE WINDOWS_FEATURE SET NAME = ?, DESCRIPTION = ?" + + "UPDATE WIN_FEATURE SET NAME = ?, DESCRIPTION = ?" + "WHERE CODE = ?"; stmt = conn.prepareStatement(updateDBQuery); @@ -110,10 +136,10 @@ public class WindowsFeatureDAOImpl implements MobileFeatureDAO { public boolean deleteFeatureById(int mblFeatureId) throws MobileDeviceManagementDAOException { PreparedStatement stmt = null; boolean status = false; - Connection conn = null; + Connection conn; try { conn = WindowsDAOFactory.getConnection(); - String sql = "DELETE FROM WINDOWS_FEATURE WHERE FEATURE_ID = ?"; + String sql = "DELETE FROM WIN_FEATURE WHERE FEATURE_ID = ?"; stmt = conn.prepareStatement(sql); stmt.setInt(1, mblFeatureId); stmt.execute(); @@ -132,10 +158,10 @@ public class WindowsFeatureDAOImpl implements MobileFeatureDAO { public boolean deleteFeatureByCode(String mblFeatureCode) throws MobileDeviceManagementDAOException { PreparedStatement stmt = null; boolean status = false; - Connection conn = null; + Connection conn; try { conn = WindowsDAOFactory.getConnection(); - String sql = "DELETE FROM WINDOWS_FEATURE WHERE CODE = ?"; + String sql = "DELETE FROM WIN_FEATURE WHERE CODE = ?"; stmt = conn.prepareStatement(sql); stmt.setString(1, mblFeatureCode); stmt.execute(); @@ -154,10 +180,10 @@ public class WindowsFeatureDAOImpl implements MobileFeatureDAO { public MobileFeature getFeatureById(int mblFeatureId) throws MobileDeviceManagementDAOException { PreparedStatement stmt = null; ResultSet rs = null; - Connection conn = null; + Connection conn; try { conn = WindowsDAOFactory.getConnection(); - String sql = "SELECT FEATURE_ID, CODE, NAME, DESCRIPTION FROM WINDOWS_FEATURE WHERE ID = ?"; + String sql = "SELECT FEATURE_ID, CODE, NAME, DESCRIPTION FROM WIN_FEATURE WHERE ID = ?"; stmt = conn.prepareStatement(sql); stmt.setInt(1, mblFeatureId); rs = stmt.executeQuery(); @@ -191,7 +217,7 @@ public class WindowsFeatureDAOImpl implements MobileFeatureDAO { try { conn = WindowsDAOFactory.getConnection(); - String sql = "SELECT FEATURE_ID, CODE, NAME, DESCRIPTION FROM WINDOWS_FEATURE WHERE CODE = ?"; + String sql = "SELECT FEATURE_ID, CODE, NAME, DESCRIPTION FROM WIN_FEATURE WHERE CODE = ?"; stmt = conn.prepareStatement(sql); stmt.setString(1, mblFeatureCode); rs = stmt.executeQuery(); @@ -231,7 +257,7 @@ public class WindowsFeatureDAOImpl implements MobileFeatureDAO { try { conn = WindowsDAOFactory.getConnection(); - String sql = "SELECT FEATURE_ID, CODE, NAME, DESCRIPTION FROM WINDOWS_FEATURE"; + String sql = "SELECT FEATURE_ID, CODE, NAME, DESCRIPTION FROM WIN_FEATURE"; stmt = conn.prepareStatement(sql); rs = stmt.executeQuery(); MobileFeature mobileFeature; diff --git a/features/device-mgt/org.wso2.carbon.device.mgt.mobile.feature/src/main/resources/dbscripts/plugins/windows/mssql.sql b/features/device-mgt/org.wso2.carbon.device.mgt.mobile.feature/src/main/resources/dbscripts/plugins/windows/mssql.sql index c905c89a4..4bcd1e3db 100644 --- a/features/device-mgt/org.wso2.carbon.device.mgt.mobile.feature/src/main/resources/dbscripts/plugins/windows/mssql.sql +++ b/features/device-mgt/org.wso2.carbon.device.mgt.mobile.feature/src/main/resources/dbscripts/plugins/windows/mssql.sql @@ -1,7 +1,7 @@ -- ----------------------------------------------------- -- Table `WINDOWS_FEATURE` -- ----------------------------------------------------- -CREATE TABLE WINDOWS_FEATURE ( +CREATE TABLE WIN_FEATURE ( ID INT NOT NULL IDENTITY, CODE VARCHAR(45) NOT NULL, NAME VARCHAR(100) NULL, @@ -12,7 +12,7 @@ CREATE TABLE WINDOWS_FEATURE ( -- ----------------------------------------------------- -- Table `WINDOWS_DEVICE` -- ----------------------------------------------------- - CREATE TABLE WINDOWS_DEVICE ( + CREATE TABLE WIN_DEVICE ( DEVICE_ID VARCHAR(45) NOT NULL, CHANNEL_URI VARCHAR(100) NULL DEFAULT NULL, DEVICE_INFO VARCHAR(8000) NULL DEFAULT NULL, diff --git a/features/device-mgt/org.wso2.carbon.device.mgt.mobile.feature/src/main/resources/dbscripts/plugins/windows/mysql.sql b/features/device-mgt/org.wso2.carbon.device.mgt.mobile.feature/src/main/resources/dbscripts/plugins/windows/mysql.sql index ecdacf5b9..347f1ad2e 100644 --- a/features/device-mgt/org.wso2.carbon.device.mgt.mobile.feature/src/main/resources/dbscripts/plugins/windows/mysql.sql +++ b/features/device-mgt/org.wso2.carbon.device.mgt.mobile.feature/src/main/resources/dbscripts/plugins/windows/mysql.sql @@ -31,18 +31,5 @@ CREATE TABLE IF NOT EXISTS `WIN_FEATURE` ( PRIMARY KEY (`FEATURE_ID`)) ENGINE = InnoDB; --- ----------------------------------------------------- --- Table `WIN_FEATURE_PROPERTY` --- ----------------------------------------------------- -CREATE TABLE IF NOT EXISTS `WIN_FEATURE_PROPERTY` ( - `PROPERTY` VARCHAR(45) NOT NULL , - `FEATURE_ID` INT NOT NULL , - PRIMARY KEY (`PROPERTY`), - CONSTRAINT `fk_WIN_FEATURE_PROPERTY_WIN_FEATURE1` - FOREIGN KEY (`FEATURE_ID`) - REFERENCES `WIN_FEATURE` (`FEATURE_ID`) - ON DELETE NO ACTION - ON UPDATE NO ACTION) -ENGINE = InnoDB; diff --git a/features/device-mgt/org.wso2.carbon.device.mgt.mobile.feature/src/main/resources/dbscripts/plugins/windows/oracle.sql b/features/device-mgt/org.wso2.carbon.device.mgt.mobile.feature/src/main/resources/dbscripts/plugins/windows/oracle.sql index 952b4d817..8a96b7947 100644 --- a/features/device-mgt/org.wso2.carbon.device.mgt.mobile.feature/src/main/resources/dbscripts/plugins/windows/oracle.sql +++ b/features/device-mgt/org.wso2.carbon.device.mgt.mobile.feature/src/main/resources/dbscripts/plugins/windows/oracle.sql @@ -32,20 +32,6 @@ CREATE TABLE WIN_FEATURE ( ); / --- ----------------------------------------------------- --- Table `WIN_FEATURE_PROPERTY` --- ----------------------------------------------------- -CREATE TABLE WIN_FEATURE_PROPERTY ( - PROPERTY VARCHAR(45) NOT NULL, - FEATURE_ID INT NOT NULL, - PRIMARY KEY (PROPERTY), - CONSTRAINT fk_WIN_FEATURE_PROPERTY_WIN_FEATURE1 - FOREIGN KEY (FEATURE_ID) - REFERENCES WIN_FEATURE (ID) - ON DELETE NO ACTION - ON UPDATE NO ACTION); -/ - -- ----------------------------------------------------- -- Sequence `WIN_FEATURE_ID_INC_SEQ` -- ----------------------------------------------------- diff --git a/features/device-mgt/org.wso2.carbon.device.mgt.mobile.feature/src/main/resources/dbscripts/plugins/windows/postgresql.sql b/features/device-mgt/org.wso2.carbon.device.mgt.mobile.feature/src/main/resources/dbscripts/plugins/windows/postgresql.sql index 45754d06f..865f30682 100644 --- a/features/device-mgt/org.wso2.carbon.device.mgt.mobile.feature/src/main/resources/dbscripts/plugins/windows/postgresql.sql +++ b/features/device-mgt/org.wso2.carbon.device.mgt.mobile.feature/src/main/resources/dbscripts/plugins/windows/postgresql.sql @@ -30,16 +30,3 @@ CREATE TABLE IF NOT EXISTS WIN_FEATURE ( PRIMARY KEY (ID) ); --- ----------------------------------------------------- --- Table `WIN_FEATURE_PROPERTY` --- ----------------------------------------------------- -CREATE TABLE IF NOT EXISTS WIN_FEATURE_PROPERTY ( - PROPERTY VARCHAR(45) NOT NULL , - FEATURE_ID INT NOT NULL , - PRIMARY KEY (PROPERTY), - CONSTRAINT fk_WIN_FEATURE_PROPERTY_WIN_FEATURE1 - FOREIGN KEY (FEATURE_ID) - REFERENCES WIN_FEATURE (ID) - ON DELETE NO ACTION - ON UPDATE NO ACTION -);