From 2177d2b9870e7fc91e3fcc917196d9a18c776f9f Mon Sep 17 00:00:00 2001 From: hasuniea Date: Fri, 21 Aug 2015 06:27:37 +0530 Subject: [PATCH 1/8] refactored windows device plugin for enrollment --- .../impl/windows/WindowsDeviceManager.java | 24 ++- .../impl/windows/dao/WindowsDAOFactory.java | 10 +- .../dao/impl/WindowsDeviceDAOImpl.java | 166 ++++++++++++++++++ .../windows/util/WindowsPluginConstants.java | 38 ++++ .../impl/windows/util/WindowsUtils.java | 34 ++++ 5 files changed, 264 insertions(+), 8 deletions(-) create mode 100644 components/device-mgt/org.wso2.carbon.device.mgt.mobile.impl/src/main/java/org/wso2/carbon/device/mgt/mobile/impl/windows/dao/impl/WindowsDeviceDAOImpl.java create mode 100644 components/device-mgt/org.wso2.carbon.device.mgt.mobile.impl/src/main/java/org/wso2/carbon/device/mgt/mobile/impl/windows/util/WindowsPluginConstants.java create mode 100644 components/device-mgt/org.wso2.carbon.device.mgt.mobile.impl/src/main/java/org/wso2/carbon/device/mgt/mobile/impl/windows/util/WindowsUtils.java 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 d7dbe3e61..0116cb8dc 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 @@ -125,7 +125,29 @@ public class WindowsDeviceManager implements DeviceManager { @Override public boolean modifyEnrollment(Device device) throws DeviceManagementException { - return true; + boolean status; + MobileDevice mobileDevice = MobileDeviceManagementUtil.convertToMobileDevice(device); + try { + if (log.isDebugEnabled()) { + log.debug("Modifying the Windows device enrollment data"); + } + WindowsDAOFactory.beginTransaction(); + status = daoFactory.getMobileDeviceDAO() + .updateMobileDevice(mobileDevice); + WindowsDAOFactory.commitTransaction(); + } catch (MobileDeviceManagementDAOException e) { + try { + WindowsDAOFactory.rollbackTransaction(); + } catch (MobileDeviceManagementDAOException mobileDAOEx) { + String msg = "Error occurred while roll back the update device transaction :" + device.toString(); + log.warn(msg, mobileDAOEx); + } + String msg = "Error while updating the enrollment of the Windows device : " + + device.getDeviceIdentifier(); + log.error(msg, e); + throw new DeviceManagementException(msg, e); + } + return status; } @Override 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 d1e0882a0..f8f288991 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 @@ -21,8 +21,8 @@ package org.wso2.carbon.device.mgt.mobile.impl.windows.dao; import org.apache.commons.logging.Log; import org.apache.commons.logging.LogFactory; import org.wso2.carbon.device.mgt.common.DeviceManagementConstants; -import org.wso2.carbon.device.mgt.mobile.config.datasource.MobileDataSourceConfig; import org.wso2.carbon.device.mgt.mobile.dao.*; +import org.wso2.carbon.device.mgt.mobile.impl.windows.dao.impl.WindowsDeviceDAOImpl; import javax.sql.DataSource; import java.sql.Connection; @@ -41,14 +41,10 @@ public class WindowsDAOFactory extends MobileDeviceManagementDAOFactory } @Override - public MobileDeviceDAO getMobileDeviceDAO() { - return null; - } + public MobileDeviceDAO getMobileDeviceDAO() {return new WindowsDeviceDAOImpl();} @Override - public MobileOperationDAO getMobileOperationDAO() { - return null; - } + public MobileOperationDAO getMobileOperationDAO() {return null;} @Override public MobileOperationPropertyDAO getMobileOperationPropertyDAO() { 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/WindowsDeviceDAOImpl.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/WindowsDeviceDAOImpl.java new file mode 100644 index 000000000..d28b790ac --- /dev/null +++ b/components/device-mgt/org.wso2.carbon.device.mgt.mobile.impl/src/main/java/org/wso2/carbon/device/mgt/mobile/impl/windows/dao/impl/WindowsDeviceDAOImpl.java @@ -0,0 +1,166 @@ + +/* + * 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 + * 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. + */ + +package org.wso2.carbon.device.mgt.mobile.impl.windows.dao.impl; + +import org.apache.commons.logging.Log; +import org.apache.commons.logging.LogFactory; +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.util.MobileDeviceManagementDAOUtil; +import org.wso2.carbon.device.mgt.mobile.dto.MobileDevice; +import org.wso2.carbon.device.mgt.mobile.impl.windows.dao.WindowsDAOFactory; +import org.wso2.carbon.device.mgt.mobile.impl.windows.util.WindowsPluginConstants; +import org.wso2.carbon.device.mgt.mobile.impl.windows.util.WindowsUtils; + +import java.sql.Connection; +import java.sql.PreparedStatement; +import java.sql.SQLException; +import java.util.HashMap; +import java.util.List; + +/** + * Implements MobileDeviceDAO for Windows Devices. + */ +public class WindowsDeviceDAOImpl implements MobileDeviceDAO { + + private static final Log log = LogFactory.getLog(WindowsDeviceDAOImpl.class); + + @Override + public MobileDevice getMobileDevice(String mblDeviceId) throws MobileDeviceManagementDAOException { + return null; + } + + @Override + public boolean addMobileDevice(MobileDevice mobileDevice) throws MobileDeviceManagementDAOException { + boolean status = false; + Connection conn = null; + PreparedStatement stmt = null; + try { + conn = WindowsDAOFactory.getConnection(); + String createDBQuery = + "INSERT INTO WINDOWS_DEVICE(MOBILE_DEVICE_ID, CHANNEL_URI, DEVICE_INFO, IMEI, " + + "IMSI, OS_VERSION, DEVICE_MODEL, VENDOR, LATITUDE, LONGITUDE, SERIAL, " + + "MAC_ADDRESS, DEVICE_NAME) VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?)"; + + stmt = conn.prepareStatement(createDBQuery); + stmt.setString(1, mobileDevice.getMobileDeviceId()); + + if (mobileDevice.getDeviceProperties() == null) { + mobileDevice.setDeviceProperties(new HashMap()); + } + + stmt.setString(2, WindowsUtils.getDeviceProperty(mobileDevice.getDeviceProperties(), + WindowsPluginConstants.CHANNEL_URI)); + stmt.setString(3, WindowsUtils.getDeviceProperty(mobileDevice.getDeviceProperties(), + WindowsPluginConstants.DEVICE_INFO)); + stmt.setString(4, mobileDevice.getImei()); + stmt.setString(5, mobileDevice.getImsi()); + stmt.setString(6, mobileDevice.getOsVersion()); + stmt.setString(7, mobileDevice.getModel()); + stmt.setString(8, mobileDevice.getVendor()); + stmt.setString(9, mobileDevice.getLatitude()); + stmt.setString(10, mobileDevice.getLongitude()); + stmt.setString(11, mobileDevice.getSerial()); + stmt.setString(12, WindowsUtils.getDeviceProperty(mobileDevice.getDeviceProperties(), + WindowsPluginConstants.MAC_ADDRESS)); + stmt.setString(13, WindowsUtils.getDeviceProperty(mobileDevice.getDeviceProperties(), + WindowsPluginConstants.DEVICE_NAME)); + int rows = stmt.executeUpdate(); + if (rows > 0) { + status = true; + if (log.isDebugEnabled()) { + log.debug("Windows device " + mobileDevice.getMobileDeviceId() + " data has been" + + " added to the Windows database."); + } + } + } catch (SQLException e) { + String msg = "Error occurred while adding the Windows device '" + + mobileDevice.getMobileDeviceId() + "' to the Windows db."; + log.error(msg, e); + throw new MobileDeviceManagementDAOException(msg, e); + } finally { + MobileDeviceManagementDAOUtil.cleanupResources(stmt, null); + } + return status; + + } + + @Override + public boolean updateMobileDevice(MobileDevice mobileDevice) throws MobileDeviceManagementDAOException { + boolean status = false; + Connection conn = null; + PreparedStatement stmt = null; + try { + conn = WindowsDAOFactory.getConnection(); + String updateDBQuery = + "UPDATE WINDOWS_DEVICE SET CHANNEL_URI = ?, DEVICE_INFO = ?, IMEI = ?, IMSI = ?, " + + "OS_VERSION = ?, DEVICE_MODEL = ?, VENDOR = ?, LATITUDE = ?, LONGITUDE = ?, " + + "SERIAL = ?, MAC_ADDRESS = ?, DEVICE_NAME = ? WHERE MOBILE_DEVICE_ID = ?"; + + stmt = conn.prepareStatement(updateDBQuery); + + if (mobileDevice.getDeviceProperties() == null) { + mobileDevice.setDeviceProperties(new HashMap()); + } + + stmt.setString(1, WindowsUtils.getDeviceProperty( + mobileDevice.getDeviceProperties(), + WindowsPluginConstants.CHANNEL_URI)); + stmt.setString(2, WindowsUtils.getDeviceProperty(mobileDevice.getDeviceProperties(), + WindowsPluginConstants.DEVICE_INFO)); + stmt.setString(3, mobileDevice.getImei()); + stmt.setString(4, mobileDevice.getImsi()); + stmt.setString(5, mobileDevice.getOsVersion() ); + stmt.setString(6, mobileDevice.getModel()); + stmt.setString(7, mobileDevice.getVendor()); + stmt.setString(8, mobileDevice.getLatitude()); + stmt.setString(9, mobileDevice.getLongitude()); + stmt.setString(10, mobileDevice.getSerial()); + stmt.setString(11, WindowsUtils.getDeviceProperty(mobileDevice.getDeviceProperties(), + WindowsPluginConstants.MAC_ADDRESS)); + stmt.setString(12, WindowsUtils.getDeviceProperty(mobileDevice.getDeviceProperties(), + WindowsPluginConstants.DEVICE_NAME)); + stmt.setString(13, mobileDevice.getMobileDeviceId()); + int rows = stmt.executeUpdate(); + if (rows > 0) { + status = true; + if (log.isDebugEnabled()) { + log.debug("Windows device " + mobileDevice.getMobileDeviceId() + " data has been" + + " modified."); + } + } + } catch (SQLException e) { + String msg = "Error occurred while modifying the Windows device '" + + mobileDevice.getMobileDeviceId() + "' data."; + log.error(msg, e); + throw new MobileDeviceManagementDAOException(msg, e); + } finally { + MobileDeviceManagementDAOUtil.cleanupResources(stmt, null); + } + return status; + } + + @Override + public boolean deleteMobileDevice(String mblDeviceId) throws MobileDeviceManagementDAOException { + return false; + } + + @Override + public List getAllMobileDevices() throws MobileDeviceManagementDAOException { + return null; + } +} diff --git a/components/device-mgt/org.wso2.carbon.device.mgt.mobile.impl/src/main/java/org/wso2/carbon/device/mgt/mobile/impl/windows/util/WindowsPluginConstants.java b/components/device-mgt/org.wso2.carbon.device.mgt.mobile.impl/src/main/java/org/wso2/carbon/device/mgt/mobile/impl/windows/util/WindowsPluginConstants.java new file mode 100644 index 000000000..95201e366 --- /dev/null +++ b/components/device-mgt/org.wso2.carbon.device.mgt.mobile.impl/src/main/java/org/wso2/carbon/device/mgt/mobile/impl/windows/util/WindowsPluginConstants.java @@ -0,0 +1,38 @@ +/* + * 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 + * 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. + */ + +package org.wso2.carbon.device.mgt.mobile.impl.windows.util; + +/** + * Define constance used by Windows plugin. + */ +public class WindowsPluginConstants { + + //properties related to database table WINDOWS_DEVICE + public static final String MOBILE_DEVICE_ID = "MOBILE_DEVICE_ID"; + public static final String CHANNEL_URI = "CHANNEL_URI "; + public static final String DEVICE_INFO = "DEVICE_INFO"; + public static final String IMEI = "IMEI"; + public static final String IMSI = "IMSI"; + public static final String OS_VERSION = "OS_VERSION"; + public static final String DEVICE_MODEL = "DEVICE_MODEL"; + public static final String VENDOR = "VENDOR"; + public static final String LATITUDE = "LATITUDE"; + public static final String LONGITUDE = "LONGITUDE"; + public static final String SERIAL = "SERIAL"; + public static final String MAC_ADDRESS ="MAC_ADDRESS "; + public static final String DEVICE_NAME ="DEVICE_NAME"; + +} diff --git a/components/device-mgt/org.wso2.carbon.device.mgt.mobile.impl/src/main/java/org/wso2/carbon/device/mgt/mobile/impl/windows/util/WindowsUtils.java b/components/device-mgt/org.wso2.carbon.device.mgt.mobile.impl/src/main/java/org/wso2/carbon/device/mgt/mobile/impl/windows/util/WindowsUtils.java new file mode 100644 index 000000000..b782d7461 --- /dev/null +++ b/components/device-mgt/org.wso2.carbon.device.mgt.mobile.impl/src/main/java/org/wso2/carbon/device/mgt/mobile/impl/windows/util/WindowsUtils.java @@ -0,0 +1,34 @@ +/* + * 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 + * 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. + */ + +package org.wso2.carbon.device.mgt.mobile.impl.windows.util; + +import java.util.Map; + +/** + * + * Contains utility methods used by Android plugin. + */ +public class WindowsUtils { + public static String getDeviceProperty(Map deviceProperties, String property) { + + String deviceProperty = deviceProperties.get(property); + if (deviceProperty == null) { + return ""; + } + + return deviceProperty; + } +} From e39478366808e0834cf9ff33fd3e63564541d6d8 Mon Sep 17 00:00:00 2001 From: hasuniea Date: Fri, 21 Aug 2015 19:54:50 +0530 Subject: [PATCH 2/8] refactored windows device disenrollment --- .../WindowsDeviceManagementService.java | 11 +++----- .../impl/windows/WindowsDeviceManager.java | 26 ++++++++++++++++--- .../dao/impl/WindowsDeviceDAOImpl.java | 25 +++++++++++++++++- .../impl/windows/util/WindowsUtils.java | 2 +- 4 files changed, 51 insertions(+), 13 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/WindowsDeviceManagementService.java b/components/device-mgt/org.wso2.carbon.device.mgt.mobile.impl/src/main/java/org/wso2/carbon/device/mgt/mobile/impl/windows/WindowsDeviceManagementService.java index 08f686ad2..bdf06bbf0 100644 --- a/components/device-mgt/org.wso2.carbon.device.mgt.mobile.impl/src/main/java/org/wso2/carbon/device/mgt/mobile/impl/windows/WindowsDeviceManagementService.java +++ b/components/device-mgt/org.wso2.carbon.device.mgt.mobile.impl/src/main/java/org/wso2/carbon/device/mgt/mobile/impl/windows/WindowsDeviceManagementService.java @@ -18,19 +18,14 @@ 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.*; +import org.wso2.carbon.device.mgt.common.DeviceIdentifier; +import org.wso2.carbon.device.mgt.common.DeviceManagementException; +import org.wso2.carbon.device.mgt.common.DeviceManager; import org.wso2.carbon.device.mgt.common.app.mgt.Application; import org.wso2.carbon.device.mgt.common.app.mgt.ApplicationManagementException; import org.wso2.carbon.device.mgt.common.app.mgt.ApplicationManager; import org.wso2.carbon.device.mgt.common.operation.mgt.Operation; import org.wso2.carbon.device.mgt.common.spi.DeviceManagementService; -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.dto.MobileDevice; -import org.wso2.carbon.device.mgt.mobile.impl.windows.dao.WindowsDAOFactory; -import org.wso2.carbon.device.mgt.mobile.util.MobileDeviceManagementUtil; import java.util.List; 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 0116cb8dc..4564b891f 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 @@ -101,10 +101,10 @@ public class WindowsDeviceManager implements DeviceManager { public TenantConfiguration getConfiguration() throws DeviceManagementException { Resource resource; try { - String androidRegPath = + String windowsRegPath = MobileDeviceManagementUtil.getPlatformConfigPath(DeviceManagementConstants. MobileDeviceTypes.MOBILE_DEVICE_TYPE_WINDOWS); - resource = MobileDeviceManagementUtil.getRegistryResource(androidRegPath); + resource = MobileDeviceManagementUtil.getRegistryResource(windowsRegPath); JAXBContext context = JAXBContext.newInstance(TenantConfiguration.class); Unmarshaller unmarshaller = context.createUnmarshaller(); return (TenantConfiguration) unmarshaller.unmarshal( @@ -152,7 +152,27 @@ public class WindowsDeviceManager implements DeviceManager { @Override public boolean disenrollDevice(DeviceIdentifier deviceId) throws DeviceManagementException { - return true; + boolean status; + try { + if (log.isDebugEnabled()) { + log.debug("Dis-enrolling windows device : " + deviceId); + } + WindowsDAOFactory.beginTransaction(); + status = daoFactory.getMobileDeviceDAO().deleteMobileDevice(deviceId.getId()); + WindowsDAOFactory.commitTransaction(); + } catch (MobileDeviceManagementDAOException e) { + try { + WindowsDAOFactory.rollbackTransaction(); + } catch (MobileDeviceManagementDAOException mobileDAOEx) { + String msg = "Error occurred while roll back the device dis enrol transaction :" + + deviceId.toString(); + log.warn(msg, mobileDAOEx); + } + String msg = "Error while removing the Windows device : " + deviceId.getId(); + log.error(msg, e); + throw new DeviceManagementException(msg, e); + } + return status; } @Override 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/WindowsDeviceDAOImpl.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/WindowsDeviceDAOImpl.java index d28b790ac..1c0e8b301 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/WindowsDeviceDAOImpl.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/WindowsDeviceDAOImpl.java @@ -156,7 +156,30 @@ public class WindowsDeviceDAOImpl implements MobileDeviceDAO { @Override public boolean deleteMobileDevice(String mblDeviceId) throws MobileDeviceManagementDAOException { - return false; + boolean status = false; + Connection conn; + PreparedStatement stmt = null; + try { + conn = WindowsDAOFactory.getConnection(); + String deleteDBQuery = + "DELETE FROM WINDOWS_DEVICE WHERE MOBILE_DEVICE_ID = ?"; + stmt = conn.prepareStatement(deleteDBQuery); + stmt.setString(1, mblDeviceId); + int rows = stmt.executeUpdate(); + if (rows > 0) { + status = true; + if (log.isDebugEnabled()) { + log.debug("Windows device " + mblDeviceId + " data has deleted" + + " from the windows database."); + } + } + } catch (SQLException e) { + throw new MobileDeviceManagementDAOException("Error occurred while deleting windows device '" + + mblDeviceId + "'", e); + } finally { + MobileDeviceManagementDAOUtil.cleanupResources(stmt, null); + } + return status; } @Override diff --git a/components/device-mgt/org.wso2.carbon.device.mgt.mobile.impl/src/main/java/org/wso2/carbon/device/mgt/mobile/impl/windows/util/WindowsUtils.java b/components/device-mgt/org.wso2.carbon.device.mgt.mobile.impl/src/main/java/org/wso2/carbon/device/mgt/mobile/impl/windows/util/WindowsUtils.java index b782d7461..43e41c267 100644 --- a/components/device-mgt/org.wso2.carbon.device.mgt.mobile.impl/src/main/java/org/wso2/carbon/device/mgt/mobile/impl/windows/util/WindowsUtils.java +++ b/components/device-mgt/org.wso2.carbon.device.mgt.mobile.impl/src/main/java/org/wso2/carbon/device/mgt/mobile/impl/windows/util/WindowsUtils.java @@ -19,7 +19,7 @@ import java.util.Map; /** * - * Contains utility methods used by Android plugin. + * Contains utility methods used by Windows plugin. */ public class WindowsUtils { public static String getDeviceProperty(Map deviceProperties, String property) { From 7584e4d0c31baa82cdaabf63587482e7a3396399 Mon Sep 17 00:00:00 2001 From: hasuniea Date: Mon, 31 Aug 2015 18:37:09 +0530 Subject: [PATCH 3/8] refactored windows plugin properties --- .../mgt/mobile/impl/windows/util/WindowsPluginConstants.java | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/components/device-mgt/org.wso2.carbon.device.mgt.mobile.impl/src/main/java/org/wso2/carbon/device/mgt/mobile/impl/windows/util/WindowsPluginConstants.java b/components/device-mgt/org.wso2.carbon.device.mgt.mobile.impl/src/main/java/org/wso2/carbon/device/mgt/mobile/impl/windows/util/WindowsPluginConstants.java index 95201e366..0330936eb 100644 --- a/components/device-mgt/org.wso2.carbon.device.mgt.mobile.impl/src/main/java/org/wso2/carbon/device/mgt/mobile/impl/windows/util/WindowsPluginConstants.java +++ b/components/device-mgt/org.wso2.carbon.device.mgt.mobile.impl/src/main/java/org/wso2/carbon/device/mgt/mobile/impl/windows/util/WindowsPluginConstants.java @@ -32,7 +32,7 @@ public class WindowsPluginConstants { public static final String LATITUDE = "LATITUDE"; public static final String LONGITUDE = "LONGITUDE"; public static final String SERIAL = "SERIAL"; - public static final String MAC_ADDRESS ="MAC_ADDRESS "; + public static final String MAC_ADDRESS ="MAC_ADDRESS"; public static final String DEVICE_NAME ="DEVICE_NAME"; } From 716daee269730dec2c6be9f9741015f35c4537c4 Mon Sep 17 00:00:00 2001 From: hasuniea Date: Thu, 10 Sep 2015 17:24:06 +0530 Subject: [PATCH 4/8] refactored issues in codes --- .../WindowsDeviceManagementService.java | 5 +- .../impl/windows/WindowsDeviceManager.java | 64 ++++------- .../WindowsPolicyMonitoringService.java | 8 +- .../impl/windows/dao/WindowsDAOFactory.java | 48 ++++---- .../dao/impl/WindowsDeviceDAOImpl.java | 105 ++++++++++++++++-- .../windows/util/WindowsPluginConstants.java | 4 +- .../impl/windows/util/WindowsUtils.java | 4 +- 7 files changed, 146 insertions(+), 92 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/WindowsDeviceManagementService.java b/components/device-mgt/org.wso2.carbon.device.mgt.mobile.impl/src/main/java/org/wso2/carbon/device/mgt/mobile/impl/windows/WindowsDeviceManagementService.java index bdf06bbf0..ac9577bb8 100644 --- a/components/device-mgt/org.wso2.carbon.device.mgt.mobile.impl/src/main/java/org/wso2/carbon/device/mgt/mobile/impl/windows/WindowsDeviceManagementService.java +++ b/components/device-mgt/org.wso2.carbon.device.mgt.mobile.impl/src/main/java/org/wso2/carbon/device/mgt/mobile/impl/windows/WindowsDeviceManagementService.java @@ -1,13 +1,10 @@ /* * 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 - * - * 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 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 e51850f3a..16342a0f0 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 @@ -1,21 +1,18 @@ /* - * 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 - * - * 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. - * + * 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 + * 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. */ + package org.wso2.carbon.device.mgt.mobile.impl.windows; import org.apache.commons.logging.Log; @@ -101,16 +98,15 @@ public class WindowsDeviceManager implements DeviceManager { public TenantConfiguration getConfiguration() throws DeviceManagementException { Resource resource; try { - String windowsRegPath = + String windowsTenantRegistryPath = MobileDeviceManagementUtil.getPlatformConfigPath(DeviceManagementConstants. - MobileDeviceTypes.MOBILE_DEVICE_TYPE_WINDOWS); - resource = MobileDeviceManagementUtil.getRegistryResource(windowsRegPath); + MobileDeviceTypes.MOBILE_DEVICE_TYPE_WINDOWS); + resource = MobileDeviceManagementUtil.getRegistryResource(windowsTenantRegistryPath); JAXBContext context = JAXBContext.newInstance(TenantConfiguration.class); Unmarshaller unmarshaller = context.createUnmarshaller(); return (TenantConfiguration) unmarshaller.unmarshal( new StringReader(new String((byte[]) resource.getContent(), Charset .forName(MobilePluginConstants.CHARSET_UTF8)))); - } catch (MobileDeviceMgtPluginException e) { throw new DeviceManagementException( "Error occurred while retrieving the Registry instance : " + e.getMessage(), e); @@ -132,20 +128,12 @@ public class WindowsDeviceManager implements DeviceManager { log.debug("Modifying the Windows device enrollment data"); } WindowsDAOFactory.beginTransaction(); - status = daoFactory.getMobileDeviceDAO() - .updateMobileDevice(mobileDevice); + status = daoFactory.getMobileDeviceDAO().updateMobileDevice(mobileDevice); WindowsDAOFactory.commitTransaction(); } catch (MobileDeviceManagementDAOException e) { - try { - WindowsDAOFactory.rollbackTransaction(); - } catch (MobileDeviceManagementDAOException mobileDAOEx) { - String msg = "Error occurred while roll back the update device transaction :" + device.toString(); - log.warn(msg, mobileDAOEx); - } - String msg = "Error while updating the enrollment of the Windows device : " + - device.getDeviceIdentifier(); - log.error(msg, e); - throw new DeviceManagementException(msg, e); + WindowsDAOFactory.rollbackTransaction(); + throw new DeviceManagementException("Error while updating the enrollment of the Windows device : " + + device.getDeviceIdentifier(), e); } return status; } @@ -161,16 +149,8 @@ public class WindowsDeviceManager implements DeviceManager { status = daoFactory.getMobileDeviceDAO().deleteMobileDevice(deviceId.getId()); WindowsDAOFactory.commitTransaction(); } catch (MobileDeviceManagementDAOException e) { - try { - WindowsDAOFactory.rollbackTransaction(); - } catch (MobileDeviceManagementDAOException mobileDAOEx) { - String msg = "Error occurred while roll back the device dis enrol transaction :" + - deviceId.toString(); - log.warn(msg, mobileDAOEx); - } - String msg = "Error while removing the Windows device : " + deviceId.getId(); - log.error(msg, e); - throw new DeviceManagementException(msg, e); + WindowsDAOFactory.rollbackTransaction(); + throw new DeviceManagementException("Error while removing the Windows device : " + deviceId.getId(), e); } return status; } diff --git a/components/device-mgt/org.wso2.carbon.device.mgt.mobile.impl/src/main/java/org/wso2/carbon/device/mgt/mobile/impl/windows/WindowsPolicyMonitoringService.java b/components/device-mgt/org.wso2.carbon.device.mgt.mobile.impl/src/main/java/org/wso2/carbon/device/mgt/mobile/impl/windows/WindowsPolicyMonitoringService.java index b3f677d4a..600cf85ba 100644 --- a/components/device-mgt/org.wso2.carbon.device.mgt.mobile.impl/src/main/java/org/wso2/carbon/device/mgt/mobile/impl/windows/WindowsPolicyMonitoringService.java +++ b/components/device-mgt/org.wso2.carbon.device.mgt.mobile.impl/src/main/java/org/wso2/carbon/device/mgt/mobile/impl/windows/WindowsPolicyMonitoringService.java @@ -1,13 +1,10 @@ /* * 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 - * - * http://www.apache.org/licenses/LICENSE-2.0 - * + * you may obtain a copy of the License at + * 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 @@ -16,7 +13,6 @@ * under the License. */ - package org.wso2.carbon.device.mgt.mobile.impl.windows; import org.wso2.carbon.device.mgt.common.Device; 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 f8f288991..3553f99ac 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 @@ -1,21 +1,18 @@ /* - * 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 - * - * 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. - * + * 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 + * 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. */ + package org.wso2.carbon.device.mgt.mobile.impl.windows.dao; import org.apache.commons.logging.Log; @@ -41,10 +38,14 @@ public class WindowsDAOFactory extends MobileDeviceManagementDAOFactory } @Override - public MobileDeviceDAO getMobileDeviceDAO() {return new WindowsDeviceDAOImpl();} + public MobileDeviceDAO getMobileDeviceDAO() { + return new WindowsDeviceDAOImpl(); + } @Override - public MobileOperationDAO getMobileOperationDAO() {return null;} + public MobileOperationDAO getMobileOperationDAO() { + return null; + } @Override public MobileOperationPropertyDAO getMobileOperationPropertyDAO() { @@ -88,7 +89,7 @@ public class WindowsDAOFactory extends MobileDeviceManagementDAOFactory return currentConnection.get(); } - public static void commitTransaction() throws MobileDeviceManagementDAOException { + public static void commitTransaction() { try { Connection conn = currentConnection.get(); if (conn != null) { @@ -100,13 +101,13 @@ public class WindowsDAOFactory extends MobileDeviceManagementDAOFactory } } } catch (SQLException e) { - throw new MobileDeviceManagementDAOException("Error occurred while committing the transaction", e); + log.error("Error occurred while committing the transaction", e); } finally { closeConnection(); } } - public static void closeConnection() throws MobileDeviceManagementDAOException { + public static void closeConnection() { Connection con = currentConnection.get(); try { @@ -117,7 +118,7 @@ public class WindowsDAOFactory extends MobileDeviceManagementDAOFactory currentConnection.remove(); } - public static void rollbackTransaction() throws MobileDeviceManagementDAOException { + public static void rollbackTransaction() { try { Connection conn = currentConnection.get(); if (conn != null) { @@ -129,7 +130,8 @@ public class WindowsDAOFactory extends MobileDeviceManagementDAOFactory } } } catch (SQLException e) { - throw new MobileDeviceManagementDAOException("Error occurred while rollback the transaction", e); + log.warn("Error occurred while roll-backing the transaction", e); + } finally { closeConnection(); } 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/WindowsDeviceDAOImpl.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/WindowsDeviceDAOImpl.java index 1c0e8b301..665a66790 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/WindowsDeviceDAOImpl.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/WindowsDeviceDAOImpl.java @@ -1,4 +1,3 @@ - /* * Copyright (c) 2015, WSO2 Inc. (http://www.wso2.org) All Rights Reserved. * WSO2 Inc. licenses this file to you under the Apache License, @@ -28,9 +27,12 @@ import org.wso2.carbon.device.mgt.mobile.impl.windows.util.WindowsUtils; import java.sql.Connection; import java.sql.PreparedStatement; +import java.sql.ResultSet; import java.sql.SQLException; +import java.util.ArrayList; import java.util.HashMap; import java.util.List; +import java.util.Map; /** * Implements MobileDeviceDAO for Windows Devices. @@ -41,7 +43,49 @@ public class WindowsDeviceDAOImpl implements MobileDeviceDAO { @Override public MobileDevice getMobileDevice(String mblDeviceId) throws MobileDeviceManagementDAOException { - return null; + Connection conn; + PreparedStatement stmt = null; + ResultSet rs = null; + MobileDevice mobileDevice = null; + List mobileDevices = new ArrayList(); + try { + conn = WindowsDAOFactory.getConnection(); + String selectDBQuery = + "SELECT MOBILE_DEVICE_ID, CHANNEL_URI, DEVICE_INFO, IMEI, IMSI, " + + "OS_VERSION, DEVICE_MODEL, VENDOR, LATITUDE, LONGITUDE, SERIAL, MAC_ADDRESS, OS_VERSION, DEVICE_NAME " + + "FROM WINDOWS_DEVICE WHERE MOBILE_DEVICE_ID = ?"; + stmt = conn.prepareStatement(selectDBQuery); + stmt.setString(1, mblDeviceId); + rs = stmt.executeQuery(); + + while (rs.next()) { + mobileDevice = new MobileDevice(); + mobileDevice.setMobileDeviceId(rs.getString(WindowsPluginConstants.MOBILE_DEVICE_ID)); + mobileDevice.setVendor(rs.getString(WindowsPluginConstants.IMEI)); + mobileDevice.setLatitude(rs.getString(WindowsPluginConstants.IMSI)); + mobileDevice.setLongitude(rs.getString(WindowsPluginConstants.OS_VERSION)); + mobileDevice.setImei(rs.getString(WindowsPluginConstants.DEVICE_MODEL)); + mobileDevice.setImsi(rs.getString(WindowsPluginConstants.VENDOR)); + mobileDevice.setOsVersion(rs.getString(WindowsPluginConstants.LATITUDE)); + + Map propertyMap = new HashMap(); + propertyMap.put(WindowsPluginConstants.CHANNEL_URI, rs.getString(WindowsPluginConstants.CHANNEL_URI)); + propertyMap.put(WindowsPluginConstants.DEVICE_INFO, rs.getString(WindowsPluginConstants.DEVICE_INFO)); + propertyMap.put(WindowsPluginConstants.DEVICE_NAME, rs.getString(WindowsPluginConstants.DEVICE_NAME)); + mobileDevice.setDeviceProperties(propertyMap); + + mobileDevices.add(mobileDevice); + } + if (log.isDebugEnabled()) { + log.debug("All Windows device details have fetched from Windows database."); + } + return mobileDevice; + } catch (SQLException e) { + throw new MobileDeviceManagementDAOException("Error occurred while fetching all Windows device data", e); + } finally { + MobileDeviceManagementDAOUtil.cleanupResources(stmt, rs); + WindowsDAOFactory.closeConnection(); + } } @Override @@ -88,10 +132,8 @@ public class WindowsDeviceDAOImpl implements MobileDeviceDAO { } } } catch (SQLException e) { - String msg = "Error occurred while adding the Windows device '" + - mobileDevice.getMobileDeviceId() + "' to the Windows db."; - log.error(msg, e); - throw new MobileDeviceManagementDAOException(msg, e); + throw new MobileDeviceManagementDAOException("Error occurred while adding the Windows device '" + + mobileDevice.getMobileDeviceId() + "' to the Windows db.", e); } finally { MobileDeviceManagementDAOUtil.cleanupResources(stmt, null); } @@ -124,7 +166,7 @@ public class WindowsDeviceDAOImpl implements MobileDeviceDAO { WindowsPluginConstants.DEVICE_INFO)); stmt.setString(3, mobileDevice.getImei()); stmt.setString(4, mobileDevice.getImsi()); - stmt.setString(5, mobileDevice.getOsVersion() ); + stmt.setString(5, mobileDevice.getOsVersion()); stmt.setString(6, mobileDevice.getModel()); stmt.setString(7, mobileDevice.getVendor()); stmt.setString(8, mobileDevice.getLatitude()); @@ -144,10 +186,8 @@ public class WindowsDeviceDAOImpl implements MobileDeviceDAO { } } } catch (SQLException e) { - String msg = "Error occurred while modifying the Windows device '" + - mobileDevice.getMobileDeviceId() + "' data."; - log.error(msg, e); - throw new MobileDeviceManagementDAOException(msg, e); + throw new MobileDeviceManagementDAOException("Error occurred while modifying the Windows device '" + + mobileDevice.getMobileDeviceId() + "' data.", e); } finally { MobileDeviceManagementDAOUtil.cleanupResources(stmt, null); } @@ -184,6 +224,47 @@ public class WindowsDeviceDAOImpl implements MobileDeviceDAO { @Override public List getAllMobileDevices() throws MobileDeviceManagementDAOException { - return null; + Connection conn; + PreparedStatement stmt = null; + ResultSet rs = null; + MobileDevice mobileDevice; + List mobileDevices = new ArrayList(); + try { + conn = WindowsDAOFactory.getConnection(); + String selectDBQuery = + "SELECT MOBILE_DEVICE_ID, CHANNEL_URI, DEVICE_INFO, IMEI, IMSI, " + + "OS_VERSION, DEVICE_MODEL, VENDOR, LATITUDE, LONGITUDE, SERIAL, MAC_ADDRESS, OS_VERSION, DEVICE_NAME " + + "FROM WINDOWS_DEVICE"; + stmt = conn.prepareStatement(selectDBQuery); + rs = stmt.executeQuery(); + + while (rs.next()) { + mobileDevice = new MobileDevice(); + mobileDevice.setMobileDeviceId(rs.getString(WindowsPluginConstants.MOBILE_DEVICE_ID)); + mobileDevice.setVendor(rs.getString(WindowsPluginConstants.IMEI)); + mobileDevice.setLatitude(rs.getString(WindowsPluginConstants.IMSI)); + mobileDevice.setLongitude(rs.getString(WindowsPluginConstants.OS_VERSION)); + mobileDevice.setImei(rs.getString(WindowsPluginConstants.DEVICE_MODEL)); + mobileDevice.setImsi(rs.getString(WindowsPluginConstants.VENDOR)); + mobileDevice.setOsVersion(rs.getString(WindowsPluginConstants.LATITUDE)); + + Map propertyMap = new HashMap(); + propertyMap.put(WindowsPluginConstants.CHANNEL_URI, rs.getString(WindowsPluginConstants.CHANNEL_URI)); + propertyMap.put(WindowsPluginConstants.DEVICE_INFO, rs.getString(WindowsPluginConstants.DEVICE_INFO)); + propertyMap.put(WindowsPluginConstants.DEVICE_NAME, rs.getString(WindowsPluginConstants.DEVICE_NAME)); + mobileDevice.setDeviceProperties(propertyMap); + + mobileDevices.add(mobileDevice); + } + if (log.isDebugEnabled()) { + log.debug("All Windows device details have fetched from Windows database."); + } + return mobileDevices; + } catch (SQLException e) { + throw new MobileDeviceManagementDAOException("Error occurred while fetching all Windows device data", e); + } finally { + MobileDeviceManagementDAOUtil.cleanupResources(stmt, rs); + WindowsDAOFactory.closeConnection(); + } } } diff --git a/components/device-mgt/org.wso2.carbon.device.mgt.mobile.impl/src/main/java/org/wso2/carbon/device/mgt/mobile/impl/windows/util/WindowsPluginConstants.java b/components/device-mgt/org.wso2.carbon.device.mgt.mobile.impl/src/main/java/org/wso2/carbon/device/mgt/mobile/impl/windows/util/WindowsPluginConstants.java index 0330936eb..7624b5aa1 100644 --- a/components/device-mgt/org.wso2.carbon.device.mgt.mobile.impl/src/main/java/org/wso2/carbon/device/mgt/mobile/impl/windows/util/WindowsPluginConstants.java +++ b/components/device-mgt/org.wso2.carbon.device.mgt.mobile.impl/src/main/java/org/wso2/carbon/device/mgt/mobile/impl/windows/util/WindowsPluginConstants.java @@ -32,7 +32,7 @@ public class WindowsPluginConstants { public static final String LATITUDE = "LATITUDE"; public static final String LONGITUDE = "LONGITUDE"; public static final String SERIAL = "SERIAL"; - public static final String MAC_ADDRESS ="MAC_ADDRESS"; - public static final String DEVICE_NAME ="DEVICE_NAME"; + public static final String MAC_ADDRESS = "MAC_ADDRESS"; + public static final String DEVICE_NAME = "DEVICE_NAME"; } diff --git a/components/device-mgt/org.wso2.carbon.device.mgt.mobile.impl/src/main/java/org/wso2/carbon/device/mgt/mobile/impl/windows/util/WindowsUtils.java b/components/device-mgt/org.wso2.carbon.device.mgt.mobile.impl/src/main/java/org/wso2/carbon/device/mgt/mobile/impl/windows/util/WindowsUtils.java index 43e41c267..c5ee77590 100644 --- a/components/device-mgt/org.wso2.carbon.device.mgt.mobile.impl/src/main/java/org/wso2/carbon/device/mgt/mobile/impl/windows/util/WindowsUtils.java +++ b/components/device-mgt/org.wso2.carbon.device.mgt.mobile.impl/src/main/java/org/wso2/carbon/device/mgt/mobile/impl/windows/util/WindowsUtils.java @@ -18,7 +18,6 @@ package org.wso2.carbon.device.mgt.mobile.impl.windows.util; import java.util.Map; /** - * * Contains utility methods used by Windows plugin. */ public class WindowsUtils { @@ -26,9 +25,8 @@ public class WindowsUtils { String deviceProperty = deviceProperties.get(property); if (deviceProperty == null) { - return ""; + return null; } - return deviceProperty; } } From c06617d762b6a38ad4f846cd9228c4abb54e2e9c Mon Sep 17 00:00:00 2001 From: hasuniea Date: Mon, 14 Sep 2015 10:58:18 +0530 Subject: [PATCH 5/8] refactored formatting issues --- .../impl/windows/WindowsDeviceManagementService.java | 7 +++++-- .../mgt/mobile/impl/windows/WindowsDeviceManager.java | 7 +++++-- .../impl/windows/WindowsPolicyMonitoringService.java | 7 +++++-- .../mgt/mobile/impl/windows/dao/WindowsDAOFactory.java | 7 +++++-- .../mobile/impl/windows/dao/impl/WindowsDeviceDAOImpl.java | 7 +++++-- .../mobile/impl/windows/util/WindowsPluginConstants.java | 7 +++++-- .../device/mgt/mobile/impl/windows/util/WindowsUtils.java | 7 +++++-- 7 files changed, 35 insertions(+), 14 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/WindowsDeviceManagementService.java b/components/device-mgt/org.wso2.carbon.device.mgt.mobile.impl/src/main/java/org/wso2/carbon/device/mgt/mobile/impl/windows/WindowsDeviceManagementService.java index ac9577bb8..39743e642 100644 --- a/components/device-mgt/org.wso2.carbon.device.mgt.mobile.impl/src/main/java/org/wso2/carbon/device/mgt/mobile/impl/windows/WindowsDeviceManagementService.java +++ b/components/device-mgt/org.wso2.carbon.device.mgt.mobile.impl/src/main/java/org/wso2/carbon/device/mgt/mobile/impl/windows/WindowsDeviceManagementService.java @@ -1,10 +1,13 @@ /* * 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 - * http://www.apache.org/licenses/LICENSE-2.0 + * You may obtain a copy of the License at + * + * 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 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 16342a0f0..42c5c0544 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 @@ -1,10 +1,13 @@ /* * 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 - * http://www.apache.org/licenses/LICENSE-2.0 + * You may obtain a copy of the License at + * + * 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 diff --git a/components/device-mgt/org.wso2.carbon.device.mgt.mobile.impl/src/main/java/org/wso2/carbon/device/mgt/mobile/impl/windows/WindowsPolicyMonitoringService.java b/components/device-mgt/org.wso2.carbon.device.mgt.mobile.impl/src/main/java/org/wso2/carbon/device/mgt/mobile/impl/windows/WindowsPolicyMonitoringService.java index 600cf85ba..f1034d4ce 100644 --- a/components/device-mgt/org.wso2.carbon.device.mgt.mobile.impl/src/main/java/org/wso2/carbon/device/mgt/mobile/impl/windows/WindowsPolicyMonitoringService.java +++ b/components/device-mgt/org.wso2.carbon.device.mgt.mobile.impl/src/main/java/org/wso2/carbon/device/mgt/mobile/impl/windows/WindowsPolicyMonitoringService.java @@ -1,10 +1,13 @@ /* * 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 - * http://www.apache.org/licenses/LICENSE-2.0 + * You may obtain a copy of the License at + * + * 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 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 3553f99ac..138858b4e 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 @@ -1,10 +1,13 @@ /* * 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 - * http://www.apache.org/licenses/LICENSE-2.0 + * You may obtain a copy of the License at + * + * 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 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/WindowsDeviceDAOImpl.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/WindowsDeviceDAOImpl.java index 665a66790..2e4adf503 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/WindowsDeviceDAOImpl.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/WindowsDeviceDAOImpl.java @@ -1,10 +1,13 @@ /* * 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 - * http://www.apache.org/licenses/LICENSE-2.0 + * You may obtain a copy of the License at + * + * 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 diff --git a/components/device-mgt/org.wso2.carbon.device.mgt.mobile.impl/src/main/java/org/wso2/carbon/device/mgt/mobile/impl/windows/util/WindowsPluginConstants.java b/components/device-mgt/org.wso2.carbon.device.mgt.mobile.impl/src/main/java/org/wso2/carbon/device/mgt/mobile/impl/windows/util/WindowsPluginConstants.java index 7624b5aa1..05e4f6fe5 100644 --- a/components/device-mgt/org.wso2.carbon.device.mgt.mobile.impl/src/main/java/org/wso2/carbon/device/mgt/mobile/impl/windows/util/WindowsPluginConstants.java +++ b/components/device-mgt/org.wso2.carbon.device.mgt.mobile.impl/src/main/java/org/wso2/carbon/device/mgt/mobile/impl/windows/util/WindowsPluginConstants.java @@ -1,10 +1,13 @@ /* * 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 - * http://www.apache.org/licenses/LICENSE-2.0 + * You may obtain a copy of the License at + * + * 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 diff --git a/components/device-mgt/org.wso2.carbon.device.mgt.mobile.impl/src/main/java/org/wso2/carbon/device/mgt/mobile/impl/windows/util/WindowsUtils.java b/components/device-mgt/org.wso2.carbon.device.mgt.mobile.impl/src/main/java/org/wso2/carbon/device/mgt/mobile/impl/windows/util/WindowsUtils.java index c5ee77590..fabcbc70e 100644 --- a/components/device-mgt/org.wso2.carbon.device.mgt.mobile.impl/src/main/java/org/wso2/carbon/device/mgt/mobile/impl/windows/util/WindowsUtils.java +++ b/components/device-mgt/org.wso2.carbon.device.mgt.mobile.impl/src/main/java/org/wso2/carbon/device/mgt/mobile/impl/windows/util/WindowsUtils.java @@ -1,10 +1,13 @@ /* * 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 - * http://www.apache.org/licenses/LICENSE-2.0 + * You may obtain a copy of the License at + * + * 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 From f35c3d084a030efdc30c1ed939c21973207eb779 Mon Sep 17 00:00:00 2001 From: hasuniea Date: Fri, 18 Sep 2015 14:18:26 +0530 Subject: [PATCH 6/8] refactored code issues --- .../impl/windows/dao/WindowsDAOFactory.java | 2 - .../dao/impl/WindowsDeviceDAOImpl.java | 48 +++++-------------- 2 files changed, 12 insertions(+), 38 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/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 138858b4e..62e10f6e2 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 @@ -111,7 +111,6 @@ public class WindowsDAOFactory extends MobileDeviceManagementDAOFactory } public static void closeConnection() { - Connection con = currentConnection.get(); try { con.close(); @@ -134,7 +133,6 @@ public class WindowsDAOFactory extends MobileDeviceManagementDAOFactory } } catch (SQLException e) { log.warn("Error occurred while roll-backing the transaction", e); - } finally { closeConnection(); } 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/WindowsDeviceDAOImpl.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/WindowsDeviceDAOImpl.java index 2e4adf503..bb9910ac0 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/WindowsDeviceDAOImpl.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/WindowsDeviceDAOImpl.java @@ -26,7 +26,6 @@ import org.wso2.carbon.device.mgt.mobile.dao.util.MobileDeviceManagementDAOUtil; import org.wso2.carbon.device.mgt.mobile.dto.MobileDevice; import org.wso2.carbon.device.mgt.mobile.impl.windows.dao.WindowsDAOFactory; import org.wso2.carbon.device.mgt.mobile.impl.windows.util.WindowsPluginConstants; -import org.wso2.carbon.device.mgt.mobile.impl.windows.util.WindowsUtils; import java.sql.Connection; import java.sql.PreparedStatement; @@ -50,7 +49,6 @@ public class WindowsDeviceDAOImpl implements MobileDeviceDAO { PreparedStatement stmt = null; ResultSet rs = null; MobileDevice mobileDevice = null; - List mobileDevices = new ArrayList(); try { conn = WindowsDAOFactory.getConnection(); String selectDBQuery = @@ -76,8 +74,6 @@ public class WindowsDeviceDAOImpl implements MobileDeviceDAO { propertyMap.put(WindowsPluginConstants.DEVICE_INFO, rs.getString(WindowsPluginConstants.DEVICE_INFO)); propertyMap.put(WindowsPluginConstants.DEVICE_NAME, rs.getString(WindowsPluginConstants.DEVICE_NAME)); mobileDevice.setDeviceProperties(propertyMap); - - mobileDevices.add(mobileDevice); } if (log.isDebugEnabled()) { log.debug("All Windows device details have fetched from Windows database."); @@ -87,14 +83,13 @@ public class WindowsDeviceDAOImpl implements MobileDeviceDAO { throw new MobileDeviceManagementDAOException("Error occurred while fetching all Windows device data", e); } finally { MobileDeviceManagementDAOUtil.cleanupResources(stmt, rs); - WindowsDAOFactory.closeConnection(); } } @Override public boolean addMobileDevice(MobileDevice mobileDevice) throws MobileDeviceManagementDAOException { boolean status = false; - Connection conn = null; + Connection conn; PreparedStatement stmt = null; try { conn = WindowsDAOFactory.getConnection(); @@ -106,14 +101,9 @@ public class WindowsDeviceDAOImpl implements MobileDeviceDAO { stmt = conn.prepareStatement(createDBQuery); stmt.setString(1, mobileDevice.getMobileDeviceId()); - if (mobileDevice.getDeviceProperties() == null) { - mobileDevice.setDeviceProperties(new HashMap()); - } - - stmt.setString(2, WindowsUtils.getDeviceProperty(mobileDevice.getDeviceProperties(), - WindowsPluginConstants.CHANNEL_URI)); - stmt.setString(3, WindowsUtils.getDeviceProperty(mobileDevice.getDeviceProperties(), - WindowsPluginConstants.DEVICE_INFO)); + Map properties = mobileDevice.getDeviceProperties(); + stmt.setString(2, properties.get(WindowsPluginConstants.CHANNEL_URI)); + stmt.setString(3, properties.get(WindowsPluginConstants.DEVICE_INFO)); stmt.setString(4, mobileDevice.getImei()); stmt.setString(5, mobileDevice.getImsi()); stmt.setString(6, mobileDevice.getOsVersion()); @@ -122,10 +112,8 @@ public class WindowsDeviceDAOImpl implements MobileDeviceDAO { stmt.setString(9, mobileDevice.getLatitude()); stmt.setString(10, mobileDevice.getLongitude()); stmt.setString(11, mobileDevice.getSerial()); - stmt.setString(12, WindowsUtils.getDeviceProperty(mobileDevice.getDeviceProperties(), - WindowsPluginConstants.MAC_ADDRESS)); - stmt.setString(13, WindowsUtils.getDeviceProperty(mobileDevice.getDeviceProperties(), - WindowsPluginConstants.DEVICE_NAME)); + stmt.setString(12, properties.get(WindowsPluginConstants.MAC_ADDRESS)); + stmt.setString(13, properties.get(WindowsPluginConstants.DEVICE_NAME)); int rows = stmt.executeUpdate(); if (rows > 0) { status = true; @@ -141,7 +129,6 @@ public class WindowsDeviceDAOImpl implements MobileDeviceDAO { MobileDeviceManagementDAOUtil.cleanupResources(stmt, null); } return status; - } @Override @@ -158,15 +145,9 @@ public class WindowsDeviceDAOImpl implements MobileDeviceDAO { stmt = conn.prepareStatement(updateDBQuery); - if (mobileDevice.getDeviceProperties() == null) { - mobileDevice.setDeviceProperties(new HashMap()); - } - - stmt.setString(1, WindowsUtils.getDeviceProperty( - mobileDevice.getDeviceProperties(), - WindowsPluginConstants.CHANNEL_URI)); - stmt.setString(2, WindowsUtils.getDeviceProperty(mobileDevice.getDeviceProperties(), - WindowsPluginConstants.DEVICE_INFO)); + Map properties = mobileDevice.getDeviceProperties(); + stmt.setString(1, properties.get(WindowsPluginConstants.CHANNEL_URI)); + stmt.setString(2, properties.get(WindowsPluginConstants.DEVICE_INFO)); stmt.setString(3, mobileDevice.getImei()); stmt.setString(4, mobileDevice.getImsi()); stmt.setString(5, mobileDevice.getOsVersion()); @@ -175,10 +156,8 @@ public class WindowsDeviceDAOImpl implements MobileDeviceDAO { stmt.setString(8, mobileDevice.getLatitude()); stmt.setString(9, mobileDevice.getLongitude()); stmt.setString(10, mobileDevice.getSerial()); - stmt.setString(11, WindowsUtils.getDeviceProperty(mobileDevice.getDeviceProperties(), - WindowsPluginConstants.MAC_ADDRESS)); - stmt.setString(12, WindowsUtils.getDeviceProperty(mobileDevice.getDeviceProperties(), - WindowsPluginConstants.DEVICE_NAME)); + stmt.setString(11, properties.get(WindowsPluginConstants.MAC_ADDRESS)); + stmt.setString(12, properties.get(WindowsPluginConstants.DEVICE_NAME)); stmt.setString(13, mobileDevice.getMobileDeviceId()); int rows = stmt.executeUpdate(); if (rows > 0) { @@ -204,8 +183,7 @@ public class WindowsDeviceDAOImpl implements MobileDeviceDAO { PreparedStatement stmt = null; try { conn = WindowsDAOFactory.getConnection(); - String deleteDBQuery = - "DELETE FROM WINDOWS_DEVICE WHERE MOBILE_DEVICE_ID = ?"; + String deleteDBQuery = "DELETE FROM WINDOWS_DEVICE WHERE MOBILE_DEVICE_ID = ?"; stmt = conn.prepareStatement(deleteDBQuery); stmt.setString(1, mblDeviceId); int rows = stmt.executeUpdate(); @@ -256,7 +234,6 @@ public class WindowsDeviceDAOImpl implements MobileDeviceDAO { propertyMap.put(WindowsPluginConstants.DEVICE_INFO, rs.getString(WindowsPluginConstants.DEVICE_INFO)); propertyMap.put(WindowsPluginConstants.DEVICE_NAME, rs.getString(WindowsPluginConstants.DEVICE_NAME)); mobileDevice.setDeviceProperties(propertyMap); - mobileDevices.add(mobileDevice); } if (log.isDebugEnabled()) { @@ -267,7 +244,6 @@ public class WindowsDeviceDAOImpl implements MobileDeviceDAO { throw new MobileDeviceManagementDAOException("Error occurred while fetching all Windows device data", e); } finally { MobileDeviceManagementDAOUtil.cleanupResources(stmt, rs); - WindowsDAOFactory.closeConnection(); } } } From 7091a45590267ef450ace505617dead488b230ef Mon Sep 17 00:00:00 2001 From: hasuniea Date: Fri, 18 Sep 2015 15:34:58 +0530 Subject: [PATCH 7/8] refactored code issues --- .../impl/windows/WindowsDeviceManager.java | 46 ++++++++++++++++++- .../impl/windows/dao/WindowsDAOFactory.java | 20 ++++++-- 2 files changed, 60 insertions(+), 6 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 42c5c0544..51eabc60d 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 @@ -43,6 +43,7 @@ import javax.xml.bind.Unmarshaller; import java.io.StringReader; import java.io.StringWriter; import java.nio.charset.Charset; +import java.util.ArrayList; import java.util.List; public class WindowsDeviceManager implements DeviceManager { @@ -137,6 +138,8 @@ public class WindowsDeviceManager implements DeviceManager { WindowsDAOFactory.rollbackTransaction(); throw new DeviceManagementException("Error while updating the enrollment of the Windows device : " + device.getDeviceIdentifier(), e); + } finally { + WindowsDAOFactory.closeConnection(); } return status; } @@ -154,6 +157,8 @@ public class WindowsDeviceManager implements DeviceManager { } catch (MobileDeviceManagementDAOException e) { WindowsDAOFactory.rollbackTransaction(); throw new DeviceManagementException("Error while removing the Windows device : " + deviceId.getId(), e); + } finally { + WindowsDAOFactory.closeConnection(); } return status; } @@ -175,12 +180,45 @@ public class WindowsDeviceManager implements DeviceManager { } public List getAllDevices() throws DeviceManagementException { - return null; + List devices = null; + try { + if (log.isDebugEnabled()) { + log.debug("Fetching the details of all Windows devices"); + } + WindowsDAOFactory.openConnection(); + List mobileDevices = daoFactory.getMobileDeviceDAO().getAllMobileDevices(); + if (mobileDevices != null) { + devices = new ArrayList<>(); + for (MobileDevice mobileDevice : mobileDevices) { + devices.add(MobileDeviceManagementUtil.convertToDevice(mobileDevice)); + } + } + } catch (MobileDeviceManagementDAOException e) { + throw new DeviceManagementException("Error occurred while fetching all Windows devices", e); + } finally { + WindowsDAOFactory.closeConnection(); + } + return devices; } @Override public Device getDevice(DeviceIdentifier deviceId) throws DeviceManagementException { - return null; + Device device = null; + try { + if (log.isDebugEnabled()) { + log.debug("Getting the details of Windows device : '" + deviceId.getId() + "'"); + } + WindowsDAOFactory.openConnection(); + MobileDevice mobileDevice = daoFactory.getMobileDeviceDAO(). + getMobileDevice(deviceId.getId()); + device = MobileDeviceManagementUtil.convertToDevice(mobileDevice); + } catch (MobileDeviceManagementDAOException e) { + throw new DeviceManagementException( + "Error occurred while fetching the Windows device: '" + deviceId.getId() + "'", e); + } finally { + WindowsDAOFactory.closeConnection(); + } + return device; } @Override @@ -221,10 +259,14 @@ public class WindowsDeviceManager implements DeviceManager { boolean status; MobileDevice mobileDevice = MobileDeviceManagementUtil.convertToMobileDevice(device); try { + WindowsDAOFactory.getConnection(); status = daoFactory.getMobileDeviceDAO().addMobileDevice(mobileDevice); + WindowsDAOFactory.commitTransaction(); } catch (MobileDeviceManagementDAOException e) { throw new DeviceManagementException("Error while enrolling the Windows device '" + device.getDeviceIdentifier() + "'", e); + } finally { + WindowsDAOFactory.closeConnection(); } return status; } 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 62e10f6e2..c3fd864d9 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 @@ -36,8 +36,7 @@ public class WindowsDAOFactory extends MobileDeviceManagementDAOFactory private static ThreadLocal currentConnection = new ThreadLocal(); public WindowsDAOFactory() { - this.dataSource = getDataSourceMap().get(DeviceManagementConstants.MobileDeviceTypes - .MOBILE_DEVICE_TYPE_WINDOWS); + this.dataSource = getDataSourceMap().get(DeviceManagementConstants.MobileDeviceTypes.MOBILE_DEVICE_TYPE_WINDOWS); } @Override @@ -80,13 +79,26 @@ public class WindowsDAOFactory extends MobileDeviceManagementDAOFactory } } + public static void openConnection() throws MobileDeviceManagementDAOException { + if (currentConnection.get() == null) { + Connection conn; + try { + conn = dataSource.getConnection(); + currentConnection.set(conn); + } catch (SQLException e) { + throw new MobileDeviceManagementDAOException + ("Error occurred while retrieving data source connection", e); + } + } + } + public static Connection getConnection() throws MobileDeviceManagementDAOException { if (currentConnection.get() == null) { try { currentConnection.set(dataSource.getConnection()); } catch (SQLException e) { - throw new MobileDeviceManagementDAOException("Error occurred while retrieving data source connection", - e); + throw new MobileDeviceManagementDAOException + ("Error occurred while retrieving data source connection", e); } } return currentConnection.get(); From d5e583becc6c6d18f0497006b2eced310fa0c9cf Mon Sep 17 00:00:00 2001 From: hasuniea Date: Fri, 18 Sep 2015 15:50:03 +0530 Subject: [PATCH 8/8] refactored coding issues. --- .../device/mgt/mobile/impl/windows/WindowsDeviceManager.java | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) 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 51eabc60d..3a280812b 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 @@ -259,10 +259,11 @@ public class WindowsDeviceManager implements DeviceManager { boolean status; MobileDevice mobileDevice = MobileDeviceManagementUtil.convertToMobileDevice(device); try { - WindowsDAOFactory.getConnection(); + WindowsDAOFactory.beginTransaction(); status = daoFactory.getMobileDeviceDAO().addMobileDevice(mobileDevice); WindowsDAOFactory.commitTransaction(); } catch (MobileDeviceManagementDAOException e) { + WindowsDAOFactory.rollbackTransaction(); throw new DeviceManagementException("Error while enrolling the Windows device '" + device.getDeviceIdentifier() + "'", e); } finally {