Add policy content store as Json

APPM_Imp
Lasantha Dharmakeerthi 4 months ago
commit cbbac7e37a

@ -180,6 +180,10 @@
<groupId>io.entgra.device.mgt.core</groupId>
<artifactId>io.entgra.device.mgt.core.policy.mgt.common</artifactId>
</dependency>
<dependency>
<groupId>com.google.code.gson</groupId>
<artifactId>gson</artifactId>
</dependency>
<!--Test Case -->

@ -18,6 +18,7 @@
package io.entgra.device.mgt.core.policy.mgt.core.dao.impl.policy;
import com.google.gson.Gson;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.wso2.carbon.context.PrivilegedCarbonContext;
@ -54,6 +55,7 @@ import java.util.Properties;
*/
public abstract class AbstractPolicyDAOImpl implements PolicyDAO {
private static final Gson gson = new Gson();
private static final Log log = LogFactory.getLog(AbstractPolicyDAOImpl.class);
@Override
@ -1187,13 +1189,13 @@ public abstract class AbstractPolicyDAOImpl implements PolicyDAO {
stmt = conn.prepareStatement(query);
stmt.setInt(1, deviceId);
stmt.setInt(2, policy.getId());
stmt.setBytes(3, PolicyManagerUtil.getBytes(policy));
stmt.setString(3, PolicyManagerUtil.convertToJson(policy));
stmt.setTimestamp(4, currentTimestamp);
stmt.setTimestamp(5, currentTimestamp);
stmt.setInt(6, tenantId);
stmt.setInt(7, enrolmentId);
stmt.executeUpdate();
} catch (SQLException | IOException e) {
} catch (SQLException e) {
throw new PolicyManagerDAOException("Error occurred while adding the evaluated feature list to device", e);
} finally {
PolicyManagementDAOUtil.cleanupResources(stmt, null);
@ -1240,7 +1242,7 @@ public abstract class AbstractPolicyDAOImpl implements PolicyDAO {
"APPLIED = ? WHERE DEVICE_ID = ? AND TENANT_ID = ? AND ENROLMENT_ID = ?";
stmt = conn.prepareStatement(query);
stmt.setInt(1, policy.getId());
stmt.setBytes(2, PolicyManagerUtil.getBytes(policy));
stmt.setString(2, PolicyManagerUtil.convertToJson(policy));
stmt.setTimestamp(3, currentTimestamp);
stmt.setBoolean(4, false);
stmt.setInt(5, deviceId);
@ -1248,7 +1250,7 @@ public abstract class AbstractPolicyDAOImpl implements PolicyDAO {
stmt.setInt(7, enrolmentId);
stmt.executeUpdate();
} catch (SQLException | IOException e) {
} catch (SQLException e) {
throw new PolicyManagerDAOException("Error occurred while updating the evaluated feature list " +
"to device", e);
} finally {
@ -1699,39 +1701,12 @@ public abstract class AbstractPolicyDAOImpl implements PolicyDAO {
resultSet = stmt.executeQuery();
while (resultSet.next()) {
ByteArrayInputStream bais = null;
ObjectInputStream ois = null;
byte[] contentBytes;
try {
contentBytes = resultSet.getBytes("POLICY_CONTENT");
bais = new ByteArrayInputStream(contentBytes);
ois = new ObjectInputStream(bais);
policy = (Policy) ois.readObject();
} 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);
}
}
}
String contentString = resultSet.getString("POLICY_CONTENT");
policy = gson.fromJson(contentString, Policy.class);
}
} catch (SQLException e) {
throw new PolicyManagerDAOException("Error occurred while getting the applied policy", e);
} catch (IOException e) {
throw new PolicyManagerDAOException("Unable to read the byte stream for content", e);
} catch (ClassNotFoundException e) {
throw new PolicyManagerDAOException("Class not found while converting the object", e);
} finally {
PolicyManagementDAOUtil.cleanupResources(stmt, resultSet);
}

@ -62,7 +62,7 @@ import java.io.ObjectOutputStream;
import java.util.*;
public class PolicyManagerUtil {
private static final Gson gson = new Gson();
public static final String GENERAL_CONFIG_RESOURCE_PATH = "general";
public static final String MONITORING_FREQUENCY = "notifierFrequency";
private static final Log log = LogFactory.getLog(PolicyManagerUtil.class);
@ -355,6 +355,15 @@ public class PolicyManagerUtil {
return data;
}
/**
* Using for converting policy objects into Json strings
* @param obj
* @return
*/
public static String convertToJson(Object obj) {
return gson.toJson(obj);
}
public static boolean convertIntToBoolean(int x) {
return x == 1;

Loading…
Cancel
Save