Setting payload method refactoring

merge-requests/7/head
Dilshan Edirisuriya 10 years ago
parent 749f2b782e
commit 8daf620b0c

@ -20,10 +20,11 @@ package org.wso2.carbon.device.mgt.common.operation.mgt;
import javax.xml.bind.annotation.XmlElement; import javax.xml.bind.annotation.XmlElement;
import javax.xml.bind.annotation.XmlRootElement; import javax.xml.bind.annotation.XmlRootElement;
import java.io.Serializable;
import java.util.Properties; import java.util.Properties;
@XmlRootElement @XmlRootElement
public class Operation { public class Operation implements Serializable {
public enum Type { public enum Type {
CONFIG, MESSAGE, INFO, COMMAND, PROFILE CONFIG, MESSAGE, INFO, COMMAND, PROFILE

@ -43,38 +43,16 @@ public class ProfileOperationDAOImpl extends OperationDAOImpl {
Connection conn = OperationManagementDAOFactory.openConnection(); Connection conn = OperationManagementDAOFactory.openConnection();
PreparedStatement stmt = null; PreparedStatement stmt = null;
ByteArrayOutputStream bao = null;
ObjectOutputStream oos = null;
try { try {
bao = new ByteArrayOutputStream();
oos = new ObjectOutputStream(bao);
oos.writeObject(profileOp);
stmt = conn.prepareStatement("INSERT INTO DM_PROFILE_OPERATION(OPERATION_ID, OPERATION_DETAILS) " + stmt = conn.prepareStatement("INSERT INTO DM_PROFILE_OPERATION(OPERATION_ID, OPERATION_DETAILS) " +
"VALUES(?, ?)"); "VALUES(?, ?)");
stmt.setInt(1, operationId); stmt.setInt(1, operationId);
stmt.setBytes(2, bao.toByteArray()); stmt.setObject(2, profileOp);
stmt.executeUpdate(); stmt.executeUpdate();
} catch (SQLException e) { } catch (SQLException e) {
throw new OperationManagementDAOException("Error occurred while adding profile operation", 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 { } 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); OperationManagementDAOUtil.cleanupResources(stmt);
} }
return operationId; return operationId;
@ -132,8 +110,8 @@ public class ProfileOperationDAOImpl extends OperationDAOImpl {
PreparedStatement stmt = null; PreparedStatement stmt = null;
ResultSet rs = null; ResultSet rs = null;
ByteArrayInputStream bais; ByteArrayInputStream bais = null;
ObjectOutputStream objectOutputStream; ObjectInputStream ois = null;
try { try {
Connection connection = OperationManagementDAOFactory.openConnection(); Connection connection = OperationManagementDAOFactory.openConnection();
@ -149,21 +127,13 @@ public class ProfileOperationDAOImpl extends OperationDAOImpl {
stmt.setString(2, deviceId.getId()); stmt.setString(2, deviceId.getId());
rs = stmt.executeQuery(); rs = stmt.executeQuery();
byte[] operationObjbyteArr; byte[] operationDetails = new byte[0];
Blob operationBlob;
ByteArrayInputStream in;
ObjectInputStream is;
if (rs.next()) { if (rs.next()) {
operationBlob = rs.getBlob("OPERATIONDETAILS"); operationDetails = rs.getBytes("OPERATIONDETAILS");
operationObjbyteArr = operationBlob.getBytes(1, (int) operationBlob.length());
in = new ByteArrayInputStream(operationObjbyteArr);
is = new ObjectInputStream(in);
return (ProfileOperation) is.readObject();
}else{
return null;
} }
bais = new ByteArrayInputStream(operationDetails);
ois = new ObjectInputStream(bais);
return (ProfileOperation) ois.readObject();
} catch (SQLException e) { } catch (SQLException e) {
log.error("SQL error occurred while retrieving profile operation", e); log.error("SQL error occurred while retrieving profile operation", e);
throw new OperationManagementDAOException("Error occurred while adding operation metadata", e); throw new OperationManagementDAOException("Error occurred while adding operation metadata", e);
@ -175,6 +145,20 @@ public class ProfileOperationDAOImpl extends OperationDAOImpl {
log.error("IO error occurred while de serialize profile operation", e); log.error("IO error occurred while de serialize profile operation", e);
throw new OperationManagementDAOException("Error occurred while serializing profile operation object", e); throw new OperationManagementDAOException("Error occurred while serializing profile operation object", e);
} finally { } 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); OperationManagementDAOUtil.cleanupResources(stmt, rs);
OperationManagementDAOFactory.closeConnection(); OperationManagementDAOFactory.closeConnection();
} }

Loading…
Cancel
Save