From eb680da5726e2791f92be3a00c9c0cecf8b35f0b Mon Sep 17 00:00:00 2001 From: Dileesha Rajapakse Date: Thu, 17 Dec 2015 14:57:28 +0530 Subject: [PATCH] Fixed EMM-1103 --- .../mgt/core/dao/impl/ApplicationDAOImpl.java | 54 +++++++++++++++++-- 1 file changed, 49 insertions(+), 5 deletions(-) diff --git a/components/device-mgt/org.wso2.carbon.device.mgt.core/src/main/java/org/wso2/carbon/device/mgt/core/dao/impl/ApplicationDAOImpl.java b/components/device-mgt/org.wso2.carbon.device.mgt.core/src/main/java/org/wso2/carbon/device/mgt/core/dao/impl/ApplicationDAOImpl.java index b2f79dbbd6..290ddacf1f 100644 --- a/components/device-mgt/org.wso2.carbon.device.mgt.core/src/main/java/org/wso2/carbon/device/mgt/core/dao/impl/ApplicationDAOImpl.java +++ b/components/device-mgt/org.wso2.carbon.device.mgt.core/src/main/java/org/wso2/carbon/device/mgt/core/dao/impl/ApplicationDAOImpl.java @@ -26,9 +26,7 @@ import org.wso2.carbon.device.mgt.core.dao.DeviceManagementDAOException; import org.wso2.carbon.device.mgt.core.dao.DeviceManagementDAOFactory; import org.wso2.carbon.device.mgt.core.dao.util.DeviceManagementDAOUtil; -import java.io.ByteArrayInputStream; -import java.io.IOException; -import java.io.ObjectInputStream; +import java.io.*; import java.sql.*; import java.util.ArrayList; import java.util.List; @@ -43,6 +41,8 @@ public class ApplicationDAOImpl implements ApplicationDAO { Connection conn; PreparedStatement stmt = null; ResultSet rs = null; + ByteArrayOutputStream bao = null; + ObjectOutputStream oos = null; int applicationId = -1; try { conn = this.getConnection(); @@ -58,7 +58,12 @@ public class ApplicationDAOImpl implements ApplicationDAO { stmt.setString(6, application.getLocationUrl()); stmt.setString(7, application.getImageUrl()); stmt.setInt(8, tenantId); - stmt.setObject(9, application.getAppProperties()); + + bao = new ByteArrayOutputStream(); + oos = new ObjectOutputStream(bao); + oos.writeObject(application.getAppProperties()); + stmt.setBytes(9, bao.toByteArray()); + stmt.setString(10, application.getApplicationIdentifier()); stmt.execute(); @@ -70,7 +75,23 @@ public class ApplicationDAOImpl implements ApplicationDAO { } catch (SQLException e) { throw new DeviceManagementDAOException("Error occurred while adding application '" + application.getName() + "'", e); + } catch (IOException e) { + throw new DeviceManagementDAOException("Error occurred while serializing application properties object", e); } finally { + if (bao != null) { + try { + bao.close(); + } catch (IOException e) { + log.warn("Error occurred while closing ByteArrayOutputStream", e); + } + } + if (oos != null) { + try { + oos.close(); + } catch (IOException e) { + log.warn("Error occurred while closing ObjectOutputStream", e); + } + } DeviceManagementDAOUtil.cleanupResources(stmt, rs); } } @@ -81,6 +102,8 @@ public class ApplicationDAOImpl implements ApplicationDAO { Connection conn; PreparedStatement stmt = null; ResultSet rs; + ByteArrayOutputStream bao = null; + ObjectOutputStream oos = null; List applicationIds = new ArrayList<>(); try { conn = this.getConnection(); @@ -99,7 +122,12 @@ public class ApplicationDAOImpl implements ApplicationDAO { stmt.setString(6, application.getLocationUrl()); stmt.setString(7, application.getImageUrl()); stmt.setInt(8, tenantId); - stmt.setObject(9, application.getAppProperties()); + + bao = new ByteArrayOutputStream(); + oos = new ObjectOutputStream(bao); + oos.writeObject(application.getAppProperties()); + stmt.setBytes(9, bao.toByteArray()); + stmt.setString(10, application.getApplicationIdentifier()); stmt.executeUpdate(); @@ -111,7 +139,23 @@ public class ApplicationDAOImpl implements ApplicationDAO { return applicationIds; } catch (SQLException e) { throw new DeviceManagementDAOException("Error occurred while adding bulk application list", e); + } catch (IOException e) { + throw new DeviceManagementDAOException("Error occurred while serializing application properties object", e); } finally { + if (bao != null) { + try { + bao.close(); + } catch (IOException e) { + log.warn("Error occurred while closing ByteArrayOutputStream", e); + } + } + if (oos != null) { + try { + oos.close(); + } catch (IOException e) { + log.warn("Error occurred while closing ObjectOutputStream", e); + } + } DeviceManagementDAOUtil.cleanupResources(stmt, null); } }