Fixed NPE issues in policy-mgt

revert-70aa11f8
milanperera 10 years ago
commit f0ec3dbe4c

@ -24,10 +24,11 @@ import org.wso2.carbon.device.mgt.core.dto.DeviceType;
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.sql.Timestamp; import java.sql.Timestamp;
import java.util.List; import java.util.List;
@XmlRootElement @XmlRootElement
public class Profile { public class Profile implements Serializable {
private int profileId; private int profileId;
private String profileName; private String profileName;

@ -89,12 +89,12 @@ public interface PolicyDAO {
List<String> getPolicyAppliedUsers(int policyId) throws PolicyManagerDAOException; List<String> getPolicyAppliedUsers(int policyId) throws PolicyManagerDAOException;
void addEffectivePolicyToDevice(int deviceId, int policyId, List<ProfileFeature> profileFeatures) void addEffectivePolicyToDevice(int deviceId, Policy policy)
throws PolicyManagerDAOException; throws PolicyManagerDAOException;
void setPolicyApplied(int deviceId) throws PolicyManagerDAOException; void setPolicyApplied(int deviceId) throws PolicyManagerDAOException;
void updateEffectivePolicyToDevice(int deviceId, int policyId, List<ProfileFeature> profileFeatures) void updateEffectivePolicyToDevice(int deviceId, Policy policy)
throws PolicyManagerDAOException; throws PolicyManagerDAOException;
boolean checkPolicyAvailable(int deviceId) throws PolicyManagerDAOException; boolean checkPolicyAvailable(int deviceId) throws PolicyManagerDAOException;

@ -749,7 +749,7 @@ public class PolicyDAOImpl implements PolicyDAO {
@Override @Override
public void addEffectivePolicyToDevice(int deviceId, int policyId, List<ProfileFeature> profileFeatures) public void addEffectivePolicyToDevice(int deviceId, Policy policy)
throws PolicyManagerDAOException { throws PolicyManagerDAOException {
Connection conn; Connection conn;
@ -761,8 +761,8 @@ public class PolicyDAOImpl implements PolicyDAO {
"(DEVICE_ID, POLICY_ID, POLICY_CONTENT, CREATED_TIME, UPDATED_TIME) VALUES (?, ?, ?, ?, ?)"; "(DEVICE_ID, POLICY_ID, POLICY_CONTENT, CREATED_TIME, UPDATED_TIME) VALUES (?, ?, ?, ?, ?)";
stmt = conn.prepareStatement(query); stmt = conn.prepareStatement(query);
stmt.setInt(1, deviceId); stmt.setInt(1, deviceId);
stmt.setInt(2, policyId); stmt.setInt(2, policy.getId());
stmt.setObject(3, profileFeatures); stmt.setObject(3, policy);
stmt.setTimestamp(4, currentTimestamp); stmt.setTimestamp(4, currentTimestamp);
stmt.setTimestamp(5, currentTimestamp); stmt.setTimestamp(5, currentTimestamp);
@ -805,7 +805,7 @@ public class PolicyDAOImpl implements PolicyDAO {
@Override @Override
public void updateEffectivePolicyToDevice(int deviceId, int policyId, List<ProfileFeature> profileFeatures) public void updateEffectivePolicyToDevice(int deviceId, Policy policy)
throws PolicyManagerDAOException { throws PolicyManagerDAOException {
Connection conn; Connection conn;
@ -816,8 +816,8 @@ public class PolicyDAOImpl implements PolicyDAO {
String query = "UPDATE DM_DEVICE_POLICY_APPLIED SET POLICY_ID = ?, POLICY_CONTENT = ?, UPDATED_TIME = ?, " + String query = "UPDATE DM_DEVICE_POLICY_APPLIED SET POLICY_ID = ?, POLICY_CONTENT = ?, UPDATED_TIME = ?, " +
"APPLIED = ? WHERE DEVICE_ID = ?"; "APPLIED = ? WHERE DEVICE_ID = ?";
stmt = conn.prepareStatement(query); stmt = conn.prepareStatement(query);
stmt.setInt(1, policyId); stmt.setInt(1, policy.getId());
stmt.setObject(2, profileFeatures); stmt.setObject(2, policy);
stmt.setTimestamp(3, currentTimestamp); stmt.setTimestamp(3, currentTimestamp);
stmt.setBoolean(4, false); stmt.setBoolean(4, false);
stmt.setInt(5, deviceId); stmt.setInt(5, deviceId);

@ -26,6 +26,7 @@ import org.wso2.carbon.policy.mgt.common.spi.PolicyMonitoringService;
import org.wso2.carbon.user.core.service.RealmService; import org.wso2.carbon.user.core.service.RealmService;
import org.wso2.carbon.user.core.tenant.TenantManager; import org.wso2.carbon.user.core.tenant.TenantManager;
import java.util.HashMap;
import java.util.Map; import java.util.Map;
public class PolicyManagementDataHolder { public class PolicyManagementDataHolder {
@ -35,7 +36,7 @@ public class PolicyManagementDataHolder {
private PolicyEvaluationPoint policyEvaluationPoint; private PolicyEvaluationPoint policyEvaluationPoint;
private PolicyInformationPoint policyInformationPoint; private PolicyInformationPoint policyInformationPoint;
private DeviceManagementProviderService deviceManagementService; private DeviceManagementProviderService deviceManagementService;
private Map<String, PolicyMonitoringService> policyMonitoringServiceMap; private Map<String, PolicyMonitoringService> policyMonitoringServiceMap = new HashMap<>();
private TaskService taskService; private TaskService taskService;
private static PolicyManagementDataHolder thisInstance = new PolicyManagementDataHolder(); private static PolicyManagementDataHolder thisInstance = new PolicyManagementDataHolder();

@ -38,7 +38,7 @@ public interface PolicyManager {
boolean deletePolicy(int policyId) throws PolicyManagementException; boolean deletePolicy(int policyId) throws PolicyManagementException;
Policy addPolicyToDevice(List<DeviceIdentifier> deviceIdentifierList, Policy policy) throws Policy addPolicyToDevice(List<DeviceIdentifier> deviceIdentifierList, Policy policy) throws
PolicyManagementException; PolicyManagementException;
Policy addPolicyToRole(List<String> roleNames, Policy policy) throws PolicyManagementException; Policy addPolicyToRole(List<String> roleNames, Policy policy) throws PolicyManagementException;
@ -60,10 +60,10 @@ public interface PolicyManager {
List<Device> getPolicyAppliedDevicesIds(int policyId) throws PolicyManagementException; List<Device> getPolicyAppliedDevicesIds(int policyId) throws PolicyManagementException;
void addAppliedPolicyFeaturesToDevice(DeviceIdentifier deviceIdentifier, int policyId, List<ProfileFeature> void addAppliedPolicyFeaturesToDevice(DeviceIdentifier deviceIdentifier, Policy policy)
profileFeatures) throws PolicyManagementException; throws PolicyManagementException;
void addAppliedPolicyToDevice(DeviceIdentifier deviceIdentifier, Policy policy) throws PolicyManagementException; void addAppliedPolicyToDevice(DeviceIdentifier deviceIdentifier, Policy policy) throws PolicyManagementException;
boolean checkPolicyAvailable(DeviceIdentifier deviceIdentifier) throws PolicyManagementException; boolean checkPolicyAvailable(DeviceIdentifier deviceIdentifier) throws PolicyManagementException;

@ -214,21 +214,22 @@ public class MonitoringManagerImpl implements MonitoringManager {
Map<Integer, ComplianceData> tempMap = new HashMap<>(); Map<Integer, ComplianceData> tempMap = new HashMap<>();
if (complianceDatas != null) {
for (ComplianceData complianceData : complianceDatas) {
for (ComplianceData complianceData : complianceDatas) { tempMap.put(complianceData.getDeviceId(), complianceData);
tempMap.put(complianceData.getDeviceId(), complianceData); if (complianceData.getAttempts() == 0) {
deviceIdsToAddOperation.put(complianceData.getDeviceId(),
if (complianceData.getAttempts() == 0) { deviceIds.get(complianceData.getDeviceId()));
deviceIdsToAddOperation.put(complianceData.getDeviceId(), } else {
deviceIds.get(complianceData.getDeviceId())); deviceIdsWithExistingOperation.put(complianceData.getDeviceId(),
} else { deviceIds.get(complianceData.getDeviceId()));
deviceIdsWithExistingOperation.put(complianceData.getDeviceId(), }
deviceIds.get(complianceData.getDeviceId())); if (complianceData.getAttempts() >= 20) {
} inactiveDeviceIds.put(complianceData.getDeviceId(),
if (complianceData.getAttempts() >= 20) { deviceIds.get(complianceData.getDeviceId()));
inactiveDeviceIds.put(complianceData.getDeviceId(), }
deviceIds.get(complianceData.getDeviceId()));
} }
} }

@ -708,8 +708,8 @@ public class PolicyManagerImpl implements PolicyManager {
} }
@Override @Override
public void addAppliedPolicyFeaturesToDevice(DeviceIdentifier deviceIdentifier, int policyId, public void addAppliedPolicyFeaturesToDevice(DeviceIdentifier deviceIdentifier, Policy policy)
List<ProfileFeature> profileFeatures) throws PolicyManagementException { throws PolicyManagementException {
int deviceId = -1; int deviceId = -1;
try { try {
@ -719,9 +719,9 @@ public class PolicyManagerImpl implements PolicyManager {
boolean exist = policyDAO.checkPolicyAvailable(deviceId); boolean exist = policyDAO.checkPolicyAvailable(deviceId);
PolicyManagementDAOFactory.beginTransaction(); PolicyManagementDAOFactory.beginTransaction();
if (exist) { if (exist) {
policyDAO.updateEffectivePolicyToDevice(deviceId, policyId, profileFeatures); policyDAO.updateEffectivePolicyToDevice(deviceId, policy);
} else { } else {
policyDAO.addEffectivePolicyToDevice(deviceId, policyId, profileFeatures); policyDAO.addEffectivePolicyToDevice(deviceId, policy);
} }
PolicyManagementDAOFactory.commitTransaction(); PolicyManagementDAOFactory.commitTransaction();
} catch (PolicyManagerDAOException e) { } catch (PolicyManagerDAOException e) {
@ -731,7 +731,7 @@ public class PolicyManagerImpl implements PolicyManager {
log.warn("Error occurred while roll backing the transaction."); log.warn("Error occurred while roll backing the transaction.");
} }
String msg = "Error occurred while adding the evaluated policy to device (" + String msg = "Error occurred while adding the evaluated policy to device (" +
deviceId + " - " + policyId + ")"; deviceId + " - " + policy.getId() + ")";
log.error(msg, e); log.error(msg, e);
throw new PolicyManagementException(msg, e); throw new PolicyManagementException(msg, e);
} catch (DeviceManagementException e) { } catch (DeviceManagementException e) {
@ -756,12 +756,10 @@ public class PolicyManagerImpl implements PolicyManager {
if (exist) { if (exist) {
Policy policySaved = policyDAO.getAppliedPolicy(deviceId); Policy policySaved = policyDAO.getAppliedPolicy(deviceId);
if (!policy.equals(policySaved)) { if (!policy.equals(policySaved)) {
policyDAO.updateEffectivePolicyToDevice(deviceId, policy.getId(), policy.getProfile(). policyDAO.updateEffectivePolicyToDevice(deviceId, policy);
getProfileFeaturesList());
} }
} else { } else {
policyDAO.addEffectivePolicyToDevice(deviceId, policy.getId(), policy.getProfile(). policyDAO.addEffectivePolicyToDevice(deviceId, policy);
getProfileFeaturesList());
} }
PolicyManagementDAOFactory.commitTransaction(); PolicyManagementDAOFactory.commitTransaction();
} catch (PolicyManagerDAOException e) { } catch (PolicyManagerDAOException e) {

@ -308,6 +308,38 @@ CREATE TABLE IF NOT EXISTS DM_POLICY_CRITERIA_PROPERTIES (
); );
CREATE TABLE IF NOT EXISTS DM_POLICY_COMPLIANCE_STATUS (
ID INT NOT NULL AUTO_INCREMENT,
DEVICE_ID INT NOT NULL,
POLICY_ID INT NOT NULL,
STATUS INT NULL,
LAST_SUCCESS_TIME TIMESTAMP NULL,
LAST_REQUESTED_TIME TIMESTAMP NULL,
LAST_FAILED_TIME TIMESTAMP NULL,
ATTEMPTS INT NULL,
PRIMARY KEY (ID),
CONSTRAINT FK_POLICY_COMPLIANCE_STATUS_POLICY
FOREIGN KEY (POLICY_ID)
REFERENCES DM_POLICY (ID)
ON DELETE NO ACTION
ON UPDATE NO ACTION
);
CREATE TABLE IF NOT EXISTS DM_POLICY_COMPLIANCE_FEATURES (
ID INT NOT NULL AUTO_INCREMENT,
COMPLIANCE_STATUS_ID INT NOT NULL,
FEATURE_CODE INT NOT NULL,
STATUS INT NULL,
PRIMARY KEY (ID),
CONSTRAINT FK_COMPLIANCE_FEATURES_STATUS
FOREIGN KEY (COMPLIANCE_STATUS_ID)
REFERENCES DM_POLICY_COMPLIANCE_STATUS (ID)
ON DELETE NO ACTION
ON UPDATE NO ACTION
);
-- POLICY RELATED TABLES FINISHED -- -- POLICY RELATED TABLES FINISHED --

@ -295,6 +295,51 @@ CREATE TABLE IF NOT EXISTS DM_POLICY_CRITERIA_PROPERTIES (
ON UPDATE NO ACTION ON UPDATE NO ACTION
); );
CREATE TABLE IF NOT EXISTS DM_POLICY_COMPLIANCE_STATUS (
ID INT NOT NULL AUTO_INCREMENT,
DEVICE_ID INT NOT NULL,
POLICY_ID INT NOT NULL,
STATUS INT NULL,
LAST_SUCCESS_TIME TIMESTAMP NULL,
LAST_REQUESTED_TIME TIMESTAMP NULL,
LAST_FAILED_TIME TIMESTAMP NULL,
ATTEMPTS INT NULL,
PRIMARY KEY (ID),
CONSTRAINT FK_POLICY_COMPLIANCE_STATUS_POLICY
FOREIGN KEY (POLICY_ID)
REFERENCES DM_POLICY (ID)
ON DELETE NO ACTION
ON UPDATE NO ACTION
);
CREATE TABLE IF NOT EXISTS DM_POLICY_COMPLIANCE_FEATURES (
ID INT NOT NULL AUTO_INCREMENT,
COMPLIANCE_STATUS_ID INT NOT NULL,
FEATURE_CODE INT NOT NULL,
STATUS INT NULL,
PRIMARY KEY (ID),
CONSTRAINT FK_COMPLIANCE_FEATURES_STATUS
FOREIGN KEY (COMPLIANCE_STATUS_ID)
REFERENCES DM_POLICY_COMPLIANCE_STATUS (ID)
ON DELETE NO ACTION
ON UPDATE NO ACTION
);
CREATE TABLE IF NOT EXISTS DM_ENROLMENT (
ID INTEGER AUTO_INCREMENT NOT NULL,
DEVICE_ID INTEGER NOT NULL,
OWNER VARCHAR(50) NOT NULL,
OWNERSHIP VARCHAR(45) NULL DEFAULT NULL,
STATUS VARCHAR(50) NULL,
DATE_OF_ENROLMENT TIMESTAMP NULL DEFAULT NULL,
DATE_OF_LAST_UPDATE TIMESTAMP NULL DEFAULT NULL,
TENANT_ID INT NOT NULL,
PRIMARY KEY (ID),
CONSTRAINT fk_dm_device_enrolment FOREIGN KEY (DEVICE_ID) REFERENCES
DM_DEVICE (ID) ON DELETE NO ACTION ON UPDATE NO ACTION
);
CREATE TABLE IF NOT EXISTS DM_APPLICATION ( CREATE TABLE IF NOT EXISTS DM_APPLICATION (
ID INTEGER AUTO_INCREMENT NOT NULL, ID INTEGER AUTO_INCREMENT NOT NULL,
NAME VARCHAR(50) NOT NULL, NAME VARCHAR(50) NOT NULL,
@ -322,36 +367,6 @@ CREATE TABLE IF NOT EXISTS DM_DEVICE_APPLICATION_MAPPING (
DM_APPLICATION (ID) ON DELETE NO ACTION ON UPDATE NO ACTION DM_APPLICATION (ID) ON DELETE NO ACTION ON UPDATE NO ACTION
); );
CREATE TABLE IF NOT EXISTS DM_POLICY_COMPLIANCE_STATUS (
ID INT NOT NULL AUTO_INCREMENT,
DEVICE_ID INT NOT NULL,
POLICY_ID INT NOT NULL,
STATUS INT NULL,
LAST_SUCCESS_TIME TIMESTAMP NULL,
LAST_REQUESTED_TIME TIMESTAMP NULL,
LAST_FAILED_TIME TIMESTAMP NULL,
ATTEMPTS INT NULL,
PRIMARY KEY (ID),
CONSTRAINT FK_POLICY_COMPLIANCE_STATUS_POLICY
FOREIGN KEY (POLICY_ID)
REFERENCES DM_POLICY (ID)
ON DELETE NO ACTION
ON UPDATE NO ACTION
);
CREATE TABLE IF NOT EXISTS DM_POLICY_COMPLIANCE_FEATURES (
ID INT NOT NULL AUTO_INCREMENT,
COMPLIANCE_STATUS_ID INT NOT NULL,
FEATURE_CODE INT NOT NULL,
STATUS INT NULL,
PRIMARY KEY (ID),
CONSTRAINT FK_COMPLIANCE_FEATURES_STATUS
FOREIGN KEY (COMPLIANCE_STATUS_ID)
REFERENCES DM_POLICY_COMPLIANCE_STATUS (ID)
ON DELETE NO ACTION
ON UPDATE NO ACTION
);
-- POLICY RELATED TABLES FINISHED -- -- POLICY RELATED TABLES FINISHED --

Loading…
Cancel
Save