Fixed NPE issues in policy-mgt

revert-70aa11f8
milanperera 9 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.XmlRootElement;
import java.io.Serializable;
import java.sql.Timestamp;
import java.util.List;
@XmlRootElement
public class Profile {
public class Profile implements Serializable {
private int profileId;
private String profileName;

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

@ -749,7 +749,7 @@ public class PolicyDAOImpl implements PolicyDAO {
@Override
public void addEffectivePolicyToDevice(int deviceId, int policyId, List<ProfileFeature> profileFeatures)
public void addEffectivePolicyToDevice(int deviceId, Policy policy)
throws PolicyManagerDAOException {
Connection conn;
@ -761,8 +761,8 @@ public class PolicyDAOImpl implements PolicyDAO {
"(DEVICE_ID, POLICY_ID, POLICY_CONTENT, CREATED_TIME, UPDATED_TIME) VALUES (?, ?, ?, ?, ?)";
stmt = conn.prepareStatement(query);
stmt.setInt(1, deviceId);
stmt.setInt(2, policyId);
stmt.setObject(3, profileFeatures);
stmt.setInt(2, policy.getId());
stmt.setObject(3, policy);
stmt.setTimestamp(4, currentTimestamp);
stmt.setTimestamp(5, currentTimestamp);
@ -805,7 +805,7 @@ public class PolicyDAOImpl implements PolicyDAO {
@Override
public void updateEffectivePolicyToDevice(int deviceId, int policyId, List<ProfileFeature> profileFeatures)
public void updateEffectivePolicyToDevice(int deviceId, Policy policy)
throws PolicyManagerDAOException {
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 = ?, " +
"APPLIED = ? WHERE DEVICE_ID = ?";
stmt = conn.prepareStatement(query);
stmt.setInt(1, policyId);
stmt.setObject(2, profileFeatures);
stmt.setInt(1, policy.getId());
stmt.setObject(2, policy);
stmt.setTimestamp(3, currentTimestamp);
stmt.setBoolean(4, false);
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.tenant.TenantManager;
import java.util.HashMap;
import java.util.Map;
public class PolicyManagementDataHolder {
@ -35,7 +36,7 @@ public class PolicyManagementDataHolder {
private PolicyEvaluationPoint policyEvaluationPoint;
private PolicyInformationPoint policyInformationPoint;
private DeviceManagementProviderService deviceManagementService;
private Map<String, PolicyMonitoringService> policyMonitoringServiceMap;
private Map<String, PolicyMonitoringService> policyMonitoringServiceMap = new HashMap<>();
private TaskService taskService;
private static PolicyManagementDataHolder thisInstance = new PolicyManagementDataHolder();

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

@ -214,21 +214,22 @@ public class MonitoringManagerImpl implements MonitoringManager {
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(),
deviceIds.get(complianceData.getDeviceId()));
} else {
deviceIdsWithExistingOperation.put(complianceData.getDeviceId(),
deviceIds.get(complianceData.getDeviceId()));
}
if (complianceData.getAttempts() >= 20) {
inactiveDeviceIds.put(complianceData.getDeviceId(),
deviceIds.get(complianceData.getDeviceId()));
if (complianceData.getAttempts() == 0) {
deviceIdsToAddOperation.put(complianceData.getDeviceId(),
deviceIds.get(complianceData.getDeviceId()));
} else {
deviceIdsWithExistingOperation.put(complianceData.getDeviceId(),
deviceIds.get(complianceData.getDeviceId()));
}
if (complianceData.getAttempts() >= 20) {
inactiveDeviceIds.put(complianceData.getDeviceId(),
deviceIds.get(complianceData.getDeviceId()));
}
}
}

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

@ -295,6 +295,51 @@ CREATE TABLE IF NOT EXISTS DM_POLICY_CRITERIA_PROPERTIES (
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 (
ID INTEGER AUTO_INCREMENT 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
);
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 --

Loading…
Cancel
Save