diff --git a/components/device-mgt/org.wso2.carbon.device.mgt.common/src/main/java/org/wso2/carbon/device/mgt/common/operation/mgt/Operation.java b/components/device-mgt/org.wso2.carbon.device.mgt.common/src/main/java/org/wso2/carbon/device/mgt/common/operation/mgt/Operation.java index 6c52182760..d56dc0daa9 100644 --- a/components/device-mgt/org.wso2.carbon.device.mgt.common/src/main/java/org/wso2/carbon/device/mgt/common/operation/mgt/Operation.java +++ b/components/device-mgt/org.wso2.carbon.device.mgt.common/src/main/java/org/wso2/carbon/device/mgt/common/operation/mgt/Operation.java @@ -20,10 +20,11 @@ package org.wso2.carbon.device.mgt.common.operation.mgt; import javax.xml.bind.annotation.XmlElement; import javax.xml.bind.annotation.XmlRootElement; +import java.io.Serializable; import java.util.Properties; @XmlRootElement -public class Operation { +public class Operation implements Serializable { public enum Type { CONFIG, MESSAGE, INFO, COMMAND, PROFILE diff --git a/components/device-mgt/org.wso2.carbon.device.mgt.core/src/main/java/org/wso2/carbon/device/mgt/core/operation/mgt/dao/impl/ProfileOperationDAOImpl.java b/components/device-mgt/org.wso2.carbon.device.mgt.core/src/main/java/org/wso2/carbon/device/mgt/core/operation/mgt/dao/impl/ProfileOperationDAOImpl.java index b1b4acb462..494b3d3336 100644 --- a/components/device-mgt/org.wso2.carbon.device.mgt.core/src/main/java/org/wso2/carbon/device/mgt/core/operation/mgt/dao/impl/ProfileOperationDAOImpl.java +++ b/components/device-mgt/org.wso2.carbon.device.mgt.core/src/main/java/org/wso2/carbon/device/mgt/core/operation/mgt/dao/impl/ProfileOperationDAOImpl.java @@ -43,38 +43,16 @@ public class ProfileOperationDAOImpl extends OperationDAOImpl { Connection conn = OperationManagementDAOFactory.openConnection(); PreparedStatement stmt = null; - ByteArrayOutputStream bao = null; - ObjectOutputStream oos = null; try { - bao = new ByteArrayOutputStream(); - oos = new ObjectOutputStream(bao); - oos.writeObject(profileOp); - stmt = conn.prepareStatement("INSERT INTO DM_PROFILE_OPERATION(OPERATION_ID, OPERATION_DETAILS) " + "VALUES(?, ?)"); stmt.setInt(1, operationId); - stmt.setObject(2, bao.toByteArray()); + stmt.setObject(2, profileOp); stmt.executeUpdate(); } catch (SQLException e) { throw new OperationManagementDAOException("Error occurred while adding profile operation", e); - } catch (IOException e) { - throw new OperationManagementDAOException("Error occurred while serializing profile operation 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); - } - } OperationManagementDAOUtil.cleanupResources(stmt); } return operationId; @@ -132,38 +110,32 @@ public class ProfileOperationDAOImpl extends OperationDAOImpl { PreparedStatement stmt = null; ResultSet rs = null; - ByteArrayInputStream bais; - ObjectOutputStream objectOutputStream; + ByteArrayInputStream bais = null; + ObjectInputStream ois = null; try { Connection connection = OperationManagementDAOFactory.openConnection(); stmt = connection.prepareStatement( "SELECT po.OPERATION_DETAILS AS OPERATIONDETAILS " + "FROM DM_OPERATION o " + - "INNER JOIN DM_PROFILE_OPERATION po ON o.ID = po.OPERATION_ID AND o.ID IN (" + + "INNER JOIN DM_PROFILE_OPERATION po ON o.ID = po.OPERATION_ID AND o.STATUS =? AND o.ID IN (" + "SELECT dom.OPERATION_ID FROM (SELECT d.ID FROM DM_DEVICE d INNER JOIN " + "DM_DEVICE_TYPE dm ON d.DEVICE_TYPE_ID = dm.ID AND dm.NAME = ? AND " + "d.DEVICE_IDENTIFICATION = ?) d1 INNER JOIN DM_DEVICE_OPERATION_MAPPING dom " + "ON d1.ID = dom.DEVICE_ID) ORDER BY o.CREATED_TIMESTAMP ASC LIMIT 1"); - stmt.setString(1, deviceId.getType()); - stmt.setString(2, deviceId.getId()); - rs = stmt.executeQuery(); - byte[] operationObjbyteArr; - Blob operationBlob; - ByteArrayInputStream in; - ObjectInputStream is; + stmt.setString(1,Operation.Status.PENDING.toString()); + stmt.setString(2, deviceId.getType()); + stmt.setString(3, deviceId.getId()); + rs = stmt.executeQuery(); + byte[] operationDetails = new byte[0]; if (rs.next()) { - operationBlob = rs.getBlob("OPERATIONDETAILS"); - operationObjbyteArr = operationBlob.getBytes(1, (int) operationBlob.length()); - in = new ByteArrayInputStream(operationObjbyteArr); - is = new ObjectInputStream(in); - return (ProfileOperation) is.readObject(); - }else{ - return null; + operationDetails = rs.getBytes("OPERATIONDETAILS"); } - + bais = new ByteArrayInputStream(operationDetails); + ois = new ObjectInputStream(bais); + return (ProfileOperation) ois.readObject(); } catch (SQLException e) { log.error("SQL error occurred while retrieving profile operation", e); throw new OperationManagementDAOException("Error occurred while adding operation metadata", e); @@ -175,6 +147,20 @@ public class ProfileOperationDAOImpl extends OperationDAOImpl { log.error("IO error occurred while de serialize profile operation", e); throw new OperationManagementDAOException("Error occurred while serializing profile operation object", e); } finally { + if (bais != null) { + try { + bais.close(); + } catch (IOException e) { + log.warn("Error occurred while closing ByteArrayOutputStream", e); + } + } + if (ois != null) { + try { + ois.close(); + } catch (IOException e) { + log.warn("Error occurred while closing ObjectOutputStream", e); + } + } OperationManagementDAOUtil.cleanupResources(stmt, rs); OperationManagementDAOFactory.closeConnection(); }