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 42c5c05447..51eabc60d7 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 62e10f6e27..c3fd864d9c 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();