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 c30e8c478b..5f141c44d5 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 @@ -43,7 +43,7 @@ public class Operation implements Serializable { private String createdTimeStamp; private boolean isEnabled; private Object payLoad; - private Object operationResponse; + private String operationResponse; @Override public boolean equals(Object o) { @@ -183,11 +183,11 @@ public class Operation implements Serializable { this.payLoad = payLoad; } - public Object getOperationResponse() { + public String getOperationResponse() { return operationResponse; } - public void setOperationResponse(Object operationResponse) { + public void setOperationResponse(String operationResponse) { this.operationResponse = operationResponse; } diff --git a/components/policy-mgt/org.wso2.carbon.policy.mgt.core/pom.xml b/components/policy-mgt/org.wso2.carbon.policy.mgt.core/pom.xml index ec4ef11cd7..c818c48bef 100644 --- a/components/policy-mgt/org.wso2.carbon.policy.mgt.core/pom.xml +++ b/components/policy-mgt/org.wso2.carbon.policy.mgt.core/pom.xml @@ -61,6 +61,7 @@ org.osgi.service.component, org.apache.commons.logging, javax.sql, + javax.cache, javax.naming, javax.xml.bind.*, javax.xml.parsers.*, diff --git a/components/policy-mgt/org.wso2.carbon.policy.mgt.core/src/main/java/org/wso2/carbon/policy/mgt/core/cache/PolicyCacheException.java b/components/policy-mgt/org.wso2.carbon.policy.mgt.core/src/main/java/org/wso2/carbon/policy/mgt/core/cache/PolicyCacheException.java new file mode 100644 index 0000000000..c4382a3aa7 --- /dev/null +++ b/components/policy-mgt/org.wso2.carbon.policy.mgt.core/src/main/java/org/wso2/carbon/policy/mgt/core/cache/PolicyCacheException.java @@ -0,0 +1,59 @@ +/* + * Copyright (c) 2015, WSO2 Inc. (http://www.wso2.org) All Rights Reserved. + * + * WSO2 Inc. licenses this file to you under the Apache License, + * Version 2.0 (the "License"); you may not use this file except + * in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, + * software distributed under the License is distributed on an + * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY + * KIND, either express or implied. See the License for the + * specific language governing permissions and limitations + * under the License. + */ + + +package org.wso2.carbon.policy.mgt.core.cache; + +public class PolicyCacheException extends Exception { + + private static final long serialVersionUID = -4234546127441192907L; + + private String errorMessage; + + public String getErrorMessage() { + return errorMessage; + } + + public void setErrorMessage(String errorMessage) { + this.errorMessage = errorMessage; + } + + public PolicyCacheException(String msg, Exception nestedEx) { + super(msg, nestedEx); + setErrorMessage(msg); + } + + public PolicyCacheException(String message, Throwable cause) { + super(message, cause); + setErrorMessage(message); + } + + public PolicyCacheException(String msg) { + super(msg); + setErrorMessage(msg); + } + + public PolicyCacheException() { + super(); + } + + public PolicyCacheException(Throwable cause) { + super(cause); + } + +} diff --git a/components/policy-mgt/org.wso2.carbon.policy.mgt.core/src/main/java/org/wso2/carbon/policy/mgt/core/cache/PolicyCacheManager.java b/components/policy-mgt/org.wso2.carbon.policy.mgt.core/src/main/java/org/wso2/carbon/policy/mgt/core/cache/PolicyCacheManager.java new file mode 100644 index 0000000000..573df93e38 --- /dev/null +++ b/components/policy-mgt/org.wso2.carbon.policy.mgt.core/src/main/java/org/wso2/carbon/policy/mgt/core/cache/PolicyCacheManager.java @@ -0,0 +1,49 @@ +/* + * Copyright (c) 2015, WSO2 Inc. (http://www.wso2.org) All Rights Reserved. + * + * WSO2 Inc. licenses this file to you under the Apache License, + * Version 2.0 (the "License"); you may not use this file except + * in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, + * software distributed under the License is distributed on an + * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY + * KIND, either express or implied. See the License for the + * specific language governing permissions and limitations + * under the License. + */ + + +package org.wso2.carbon.policy.mgt.core.cache; + +import org.wso2.carbon.device.mgt.core.policy.mgt.policy.Policy; + +import java.util.List; + +public interface PolicyCacheManager { + + void addAllPolicies(List policies); + + void updateAllPolicies(List policies); + + List getAllPolicies(); + + void removeAllPolicies(); + + void addPolicy(Policy policy); + + void updatePolicy(Policy policy); + + void removePolicy(int policyId); + + Policy getPolicy(int policyId); + + void addPolicyToDevice(int deviceId, int policyId); + + List getPolicyAppliedDeviceIds(int policyId); + + int getPolicyIdOfDevice(int deviceId); +} diff --git a/components/policy-mgt/org.wso2.carbon.policy.mgt.core/src/main/java/org/wso2/carbon/policy/mgt/core/cache/impl/PolicyCacheManagerImpl.java b/components/policy-mgt/org.wso2.carbon.policy.mgt.core/src/main/java/org/wso2/carbon/policy/mgt/core/cache/impl/PolicyCacheManagerImpl.java new file mode 100644 index 0000000000..d7c8046755 --- /dev/null +++ b/components/policy-mgt/org.wso2.carbon.policy.mgt.core/src/main/java/org/wso2/carbon/policy/mgt/core/cache/impl/PolicyCacheManagerImpl.java @@ -0,0 +1,83 @@ +/* + * Copyright (c) 2015, WSO2 Inc. (http://www.wso2.org) All Rights Reserved. + * + * WSO2 Inc. licenses this file to you under the Apache License, + * Version 2.0 (the "License"); you may not use this file except + * in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, + * software distributed under the License is distributed on an + * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY + * KIND, either express or implied. See the License for the + * specific language governing permissions and limitations + * under the License. + */ + + +package org.wso2.carbon.policy.mgt.core.cache.impl; + +import org.wso2.carbon.device.mgt.core.policy.mgt.policy.Policy; +import org.wso2.carbon.policy.mgt.core.cache.PolicyCacheManager; + +import java.util.List; + +public class PolicyCacheManagerImpl implements PolicyCacheManager { + + @Override + public void addAllPolicies(List policies) { + + } + + @Override + public void updateAllPolicies(List policies) { + + } + + @Override + public List getAllPolicies() { + return null; + } + + @Override + public void removeAllPolicies() { + + } + + @Override + public void addPolicy(Policy policy) { + + } + + @Override + public void updatePolicy(Policy policy) { + + } + + @Override + public void removePolicy(int policyId) { + + } + + @Override + public Policy getPolicy(int policyId) { + return null; + } + + @Override + public void addPolicyToDevice(int deviceId, int policyId) { + + } + + @Override + public List getPolicyAppliedDeviceIds(int policyId) { + return null; + } + + @Override + public int getPolicyIdOfDevice(int deviceId) { + return 0; + } +} diff --git a/components/policy-mgt/org.wso2.carbon.policy.mgt.core/src/main/java/org/wso2/carbon/policy/mgt/core/dao/impl/FeatureDAOImpl.java b/components/policy-mgt/org.wso2.carbon.policy.mgt.core/src/main/java/org/wso2/carbon/policy/mgt/core/dao/impl/FeatureDAOImpl.java index aafb6dbdd5..9bccc8e48a 100644 --- a/components/policy-mgt/org.wso2.carbon.policy.mgt.core/src/main/java/org/wso2/carbon/policy/mgt/core/dao/impl/FeatureDAOImpl.java +++ b/components/policy-mgt/org.wso2.carbon.policy.mgt.core/src/main/java/org/wso2/carbon/policy/mgt/core/dao/impl/FeatureDAOImpl.java @@ -21,6 +21,7 @@ package org.wso2.carbon.policy.mgt.core.dao.impl; import org.apache.commons.logging.Log; import org.apache.commons.logging.LogFactory; +import org.wso2.carbon.context.PrivilegedCarbonContext; import org.wso2.carbon.device.mgt.common.Feature; import org.wso2.carbon.policy.mgt.common.Profile; import org.wso2.carbon.policy.mgt.common.ProfileFeature; @@ -140,7 +141,8 @@ public class FeatureDAOImpl implements FeatureDAO { stmt.executeUpdate(); } catch (SQLException e) { - String msg = "Error occurred while updating feature " + feature.getName() + " (Feature Name) to the database."; + String msg = "Error occurred while updating feature " + feature.getName() + " (Feature Name) to the + database."; log.error(msg, e); throw new FeatureManagerDAOException(msg, e); } finally { @@ -156,32 +158,36 @@ public class FeatureDAOImpl implements FeatureDAO { } @Override - public ProfileFeature updateProfileFeature(ProfileFeature feature, int profileId) throws FeatureManagerDAOException { + public ProfileFeature updateProfileFeature(ProfileFeature feature, int profileId) throws + FeatureManagerDAOException { return null; } @Override - public List addProfileFeatures(List features, int profileId) throws FeatureManagerDAOException { + public List addProfileFeatures(List features, int profileId) throws + FeatureManagerDAOException { Connection conn; PreparedStatement stmt = null; ResultSet generatedKeys = null; + int tenantId = PrivilegedCarbonContext.getThreadLocalCarbonContext().getTenantId(); try { conn = this.getConnection(); - String query = "INSERT INTO DM_PROFILE_FEATURES (PROFILE_ID, FEATURE_CODE, DEVICE_TYPE_ID, CONTENT) " + - "VALUES (?, ?, ?, ?)"; + String query = "INSERT INTO DM_PROFILE_FEATURES (PROFILE_ID, FEATURE_CODE, DEVICE_TYPE_ID, CONTENT, " + + "TENANT_ID) VALUES (?, ?, ?, ?, ?)"; stmt = conn.prepareStatement(query, PreparedStatement.RETURN_GENERATED_KEYS); for (ProfileFeature feature : features) { stmt.setInt(1, profileId); stmt.setString(2, feature.getFeatureCode()); stmt.setInt(3, feature.getDeviceTypeId()); - if (conn.getMetaData().getDriverName().contains("H2")) { - stmt.setBytes(4, PolicyManagerUtil.getBytes(feature.getContent())); - } else { + // if (conn.getMetaData().getDriverName().contains("H2")) { + // stmt.setBytes(4, PolicyManagerUtil.getBytes(feature.getContent())); + // } else { stmt.setBytes(4, PolicyManagerUtil.getBytes(feature.getContent())); - } + //} + stmt.setInt(5, tenantId); stmt.addBatch(); //Not adding the logic to check the size of the stmt and execute if the size records added is over 1000 } @@ -210,14 +216,17 @@ public class FeatureDAOImpl implements FeatureDAO { } @Override - public List updateProfileFeatures(List features, int profileId) throws FeatureManagerDAOException { + public List updateProfileFeatures(List features, int profileId) throws + FeatureManagerDAOException { Connection conn; PreparedStatement stmt = null; + int tenantId = PrivilegedCarbonContext.getThreadLocalCarbonContext().getTenantId(); try { conn = this.getConnection(); - String query = "UPDATE DM_PROFILE_FEATURES SET CONTENT = ? WHERE PROFILE_ID = ?, FEATURE_CODE = ?"; + String query = "UPDATE DM_PROFILE_FEATURES SET CONTENT = ? WHERE PROFILE_ID = ? AND FEATURE_CODE = ? AND" + + " TENANT_ID = ?"; stmt = conn.prepareStatement(query, PreparedStatement.RETURN_GENERATED_KEYS); for (ProfileFeature feature : features) { @@ -228,6 +237,7 @@ public class FeatureDAOImpl implements FeatureDAO { } stmt.setInt(2, profileId); stmt.setString(3, feature.getFeatureCode()); + stmt.setInt(4, tenantId); stmt.addBatch(); //Not adding the logic to check the size of the stmt and execute if the size records added is over 1000 } @@ -252,12 +262,14 @@ public class FeatureDAOImpl implements FeatureDAO { Connection conn; PreparedStatement stmt = null; + int tenantId = PrivilegedCarbonContext.getThreadLocalCarbonContext().getTenantId(); try { conn = this.getConnection(); - String query = "DELETE FROM DM_PROFILE_FEATURES WHERE PROFILE_ID = ?"; + String query = "DELETE FROM DM_PROFILE_FEATURES WHERE PROFILE_ID = ? AND TENANT_ID = ?"; stmt = conn.prepareStatement(query); stmt.setInt(1, profile.getProfileId()); + stmt.setInt(2, tenantId); stmt.executeUpdate(); return true; @@ -272,14 +284,16 @@ public class FeatureDAOImpl implements FeatureDAO { @Override public boolean deleteFeaturesOfProfile(int profileId) throws FeatureManagerDAOException { + Connection conn; PreparedStatement stmt = null; - + int tenantId = PrivilegedCarbonContext.getThreadLocalCarbonContext().getTenantId(); try { conn = this.getConnection(); - String query = "DELETE FROM DM_PROFILE_FEATURES WHERE PROFILE_ID = ?"; + String query = "DELETE FROM DM_PROFILE_FEATURES WHERE PROFILE_ID = ? AND TENANT_ID = ?"; stmt = conn.prepareStatement(query); stmt.setInt(1, profileId); + stmt.setInt(2, tenantId); stmt.executeUpdate(); return true; @@ -300,11 +314,14 @@ public class FeatureDAOImpl implements FeatureDAO { PreparedStatement stmt = null; ResultSet resultSet = null; List featureList = new ArrayList(); + int tenantId = PrivilegedCarbonContext.getThreadLocalCarbonContext().getTenantId(); try { conn = this.getConnection(); - String query = "SELECT ID, PROFILE_ID, FEATURE_CODE, DEVICE_TYPE_ID, CONTENT FROM DM_PROFILE_FEATURES"; + String query = "SELECT ID, PROFILE_ID, FEATURE_CODE, DEVICE_TYPE_ID, CONTENT FROM DM_PROFILE_FEATURES " + + "WHERE TENANT_ID = ?"; stmt = conn.prepareStatement(query); + stmt.setInt(1, tenantId); resultSet = stmt.executeQuery(); while (resultSet.next()) { @@ -406,13 +423,15 @@ public class FeatureDAOImpl implements FeatureDAO { PreparedStatement stmt = null; ResultSet resultSet = null; List featureList = new ArrayList(); + int tenantId = PrivilegedCarbonContext.getThreadLocalCarbonContext().getTenantId(); try { conn = this.getConnection(); String query = "SELECT ID, FEATURE_CODE, DEVICE_TYPE_ID, CONTENT FROM DM_PROFILE_FEATURES " + - "WHERE PROFILE_ID = ?"; + "WHERE PROFILE_ID = ? AND TENANT_ID = ?"; stmt = conn.prepareStatement(query); stmt.setInt(1, profileId); + stmt.setInt(2, tenantId); resultSet = stmt.executeQuery(); while (resultSet.next()) { @@ -473,12 +492,14 @@ public class FeatureDAOImpl implements FeatureDAO { Connection conn; PreparedStatement stmt = null; + int tenantId = PrivilegedCarbonContext.getThreadLocalCarbonContext().getTenantId(); try { conn = this.getConnection(); - String query = "DELETE FROM DM_FEATURES WHERE ID = ?"; + String query = "DELETE FROM DM_FEATURES WHERE ID = ? AND TENANT_ID = ?"; stmt = conn.prepareStatement(query); stmt.setInt(1, featureId); + stmt.setInt(2, tenantId); stmt.executeUpdate(); return true; diff --git a/components/policy-mgt/org.wso2.carbon.policy.mgt.core/src/main/java/org/wso2/carbon/policy/mgt/core/dao/impl/MonitoringDAOImpl.java b/components/policy-mgt/org.wso2.carbon.policy.mgt.core/src/main/java/org/wso2/carbon/policy/mgt/core/dao/impl/MonitoringDAOImpl.java index 7549688e5d..b1d8005fdd 100644 --- a/components/policy-mgt/org.wso2.carbon.policy.mgt.core/src/main/java/org/wso2/carbon/policy/mgt/core/dao/impl/MonitoringDAOImpl.java +++ b/components/policy-mgt/org.wso2.carbon.policy.mgt.core/src/main/java/org/wso2/carbon/policy/mgt/core/dao/impl/MonitoringDAOImpl.java @@ -21,6 +21,7 @@ package org.wso2.carbon.policy.mgt.core.dao.impl; import org.apache.commons.logging.Log; import org.apache.commons.logging.LogFactory; +import org.wso2.carbon.context.PrivilegedCarbonContext; import org.wso2.carbon.policy.mgt.common.monitor.ComplianceData; import org.wso2.carbon.policy.mgt.common.monitor.ComplianceFeature; import org.wso2.carbon.policy.mgt.core.dao.MonitoringDAO; @@ -39,20 +40,23 @@ public class MonitoringDAOImpl implements MonitoringDAO { @Override public int addComplianceDetails(int deviceId, int policyId) throws MonitoringDAOException { + Connection conn; PreparedStatement stmt = null; ResultSet generatedKeys = null; Timestamp currentTimestamp = new Timestamp(Calendar.getInstance().getTime().getTime()); + int tenantId = PrivilegedCarbonContext.getThreadLocalCarbonContext().getTenantId(); try { conn = this.getConnection(); String query = "INSERT INTO DM_POLICY_COMPLIANCE_STATUS (DEVICE_ID, POLICY_ID, STATUS, ATTEMPTS, " + - "LAST_REQUESTED_TIME) VALUES (?, ?, ?,?, ?) "; + "LAST_REQUESTED_TIME, TENANT_ID) VALUES (?, ?, ?,?, ?, ?) "; stmt = conn.prepareStatement(query, PreparedStatement.RETURN_GENERATED_KEYS); stmt.setInt(1, deviceId); stmt.setInt(2, policyId); stmt.setInt(3, 1); stmt.setInt(4, 1); stmt.setTimestamp(5, currentTimestamp); + stmt.setInt(6, tenantId); stmt.executeUpdate(); generatedKeys = stmt.getGeneratedKeys(); @@ -74,10 +78,13 @@ public class MonitoringDAOImpl implements MonitoringDAO { @Override public void addComplianceDetails(Map devicePolicyMap) throws MonitoringDAOException { + Connection conn; PreparedStatement stmt = null; ResultSet generatedKeys = null; Timestamp currentTimestamp = new Timestamp(Calendar.getInstance().getTime().getTime()); + int tenantId = PrivilegedCarbonContext.getThreadLocalCarbonContext().getTenantId(); + if (log.isDebugEnabled()) { log.debug("Adding the compliance details for devices and policies"); for (Map.Entry map : devicePolicyMap.entrySet()) { @@ -88,7 +95,7 @@ public class MonitoringDAOImpl implements MonitoringDAO { try { conn = this.getConnection(); String query = "INSERT INTO DM_POLICY_COMPLIANCE_STATUS (DEVICE_ID, POLICY_ID, STATUS, ATTEMPTS, " + - "LAST_REQUESTED_TIME) VALUES (?, ?, ?,?, ?) "; + "LAST_REQUESTED_TIME, TENANT_ID) VALUES (?, ?, ?,?, ?, ?) "; stmt = conn.prepareStatement(query); for (Map.Entry map : devicePolicyMap.entrySet()) { stmt.setInt(1, map.getKey()); @@ -96,6 +103,7 @@ public class MonitoringDAOImpl implements MonitoringDAO { stmt.setInt(3, 1); stmt.setInt(4, 1); stmt.setTimestamp(5, currentTimestamp); + stmt.setInt(6, tenantId); stmt.addBatch(); } stmt.executeBatch(); @@ -118,18 +126,20 @@ public class MonitoringDAOImpl implements MonitoringDAO { PreparedStatement stmt = null; ResultSet generatedKeys = null; Timestamp currentTimestamp = new Timestamp(Calendar.getInstance().getTime().getTime()); + int tenantId = PrivilegedCarbonContext.getThreadLocalCarbonContext().getTenantId(); + try { conn = this.getConnection(); String query = "UPDATE DM_POLICY_COMPLIANCE_STATUS SET STATUS = 0, LAST_FAILED_TIME = ?, POLICY_ID = ?," + - " ATTEMPTS=0 WHERE DEVICE_ID = ?"; + " ATTEMPTS=0 WHERE DEVICE_ID = ? AND TENANT_ID = ?"; stmt = conn.prepareStatement(query); stmt.setTimestamp(1, currentTimestamp); stmt.setInt(2, policyId); stmt.setInt(3, deviceId); + stmt.setInt(4, tenantId); stmt.executeUpdate(); - } catch (SQLException e) { String msg = "Error occurred while updating the none compliance to the database."; log.error(msg, e); @@ -147,15 +157,17 @@ public class MonitoringDAOImpl implements MonitoringDAO { PreparedStatement stmt = null; ResultSet generatedKeys = null; Timestamp currentTimestamp = new Timestamp(Calendar.getInstance().getTime().getTime()); + int tenantId = PrivilegedCarbonContext.getThreadLocalCarbonContext().getTenantId(); + try { conn = this.getConnection(); String query = "UPDATE DM_POLICY_COMPLIANCE_STATUS SET STATUS = ?, ATTEMPTS=0, LAST_SUCCESS_TIME = ?" + - " WHERE DEVICE_ID = ?"; + " WHERE DEVICE_ID = ? AND TENANT_ID = ?"; stmt = conn.prepareStatement(query); stmt.setInt(1, 1); stmt.setTimestamp(2, currentTimestamp); stmt.setInt(3, deviceId); - + stmt.setInt(4, tenantId); stmt.executeUpdate(); // generatedKeys = stmt.getGeneratedKeys(); @@ -180,10 +192,12 @@ public class MonitoringDAOImpl implements MonitoringDAO { Connection conn; PreparedStatement stmt = null; + int tenantId = PrivilegedCarbonContext.getThreadLocalCarbonContext().getTenantId(); + try { conn = this.getConnection(); - String query = "INSERT INTO DM_POLICY_COMPLIANCE_FEATURES (COMPLIANCE_STATUS_ID, FEATURE_CODE, STATUS) " + - "VALUES (?, ?, ?) "; + String query = "INSERT INTO DM_POLICY_COMPLIANCE_FEATURES (COMPLIANCE_STATUS_ID, FEATURE_CODE, STATUS, " + + "TENANT_ID) VALUES (?, ?, ?, ?) "; stmt = conn.prepareStatement(query, PreparedStatement.RETURN_GENERATED_KEYS); for (ComplianceFeature feature : complianceFeatures) { @@ -194,6 +208,7 @@ public class MonitoringDAOImpl implements MonitoringDAO { } else { stmt.setInt(3, 0); } + stmt.setInt(4, tenantId); stmt.addBatch(); } stmt.executeBatch(); @@ -214,11 +229,14 @@ public class MonitoringDAOImpl implements MonitoringDAO { PreparedStatement stmt = null; ResultSet resultSet = null; ComplianceData complianceData = new ComplianceData(); + int tenantId = PrivilegedCarbonContext.getThreadLocalCarbonContext().getTenantId(); + try { conn = this.getConnection(); - String query = "SELECT * FROM DM_POLICY_COMPLIANCE_STATUS WHERE DEVICE_ID = ?"; + String query = "SELECT * FROM DM_POLICY_COMPLIANCE_STATUS WHERE DEVICE_ID = ? AND TENANT_ID = ?"; stmt = conn.prepareStatement(query); stmt.setInt(1, deviceId); + stmt.setInt(2, tenantId); resultSet = stmt.executeQuery(); @@ -246,16 +264,19 @@ public class MonitoringDAOImpl implements MonitoringDAO { @Override public List getCompliance(List deviceIds) throws MonitoringDAOException { + Connection conn; PreparedStatement stmt = null; ResultSet resultSet = null; List complianceDataList = new ArrayList<>(); + int tenantId = PrivilegedCarbonContext.getThreadLocalCarbonContext().getTenantId(); try { conn = this.getConnection(); - String query = "SELECT * FROM DM_POLICY_COMPLIANCE_STATUS WHERE DEVICE_ID IN (?)"; + String query = "SELECT * FROM DM_POLICY_COMPLIANCE_STATUS WHERE TENANT_ID = ? AND DEVICE_ID IN (?)"; stmt = conn.prepareStatement(query); - stmt.setString(1, PolicyManagerUtil.makeString(deviceIds)); + stmt.setInt(1, tenantId); + stmt.setString(2, PolicyManagerUtil.makeString(deviceIds)); resultSet = stmt.executeQuery(); @@ -294,11 +315,14 @@ public class MonitoringDAOImpl implements MonitoringDAO { PreparedStatement stmt = null; ResultSet resultSet = null; List complianceFeatures = new ArrayList(); + int tenantId = PrivilegedCarbonContext.getThreadLocalCarbonContext().getTenantId(); + try { conn = this.getConnection(); - String query = "SELECT * FROM DM_POLICY_COMPLIANCE_FEATURES WHERE COMPLIANCE_STATUS_ID = ?"; + String query = "SELECT * FROM DM_POLICY_COMPLIANCE_FEATURES WHERE COMPLIANCE_STATUS_ID = ? AND TENANT_ID = ?"; stmt = conn.prepareStatement(query); stmt.setInt(1, policyComplianceStatusId); + stmt.setInt(2, tenantId); resultSet = stmt.executeQuery(); @@ -326,11 +350,14 @@ public class MonitoringDAOImpl implements MonitoringDAO { Connection conn; PreparedStatement stmt = null; + int tenantId = PrivilegedCarbonContext.getThreadLocalCarbonContext().getTenantId(); + try { conn = this.getConnection(); - String query = "DELETE FROM DM_POLICY_COMPLIANCE_FEATURES WHERE COMPLIANCE_STATUS_ID = ?"; + String query = "DELETE FROM DM_POLICY_COMPLIANCE_FEATURES WHERE COMPLIANCE_STATUS_ID = ? AND TENANT_ID = ?"; stmt = conn.prepareStatement(query); stmt.setInt(1, policyComplianceStatusId); + stmt.setInt(2, tenantId); stmt.executeUpdate(); } catch (SQLException e) { @@ -345,23 +372,27 @@ public class MonitoringDAOImpl implements MonitoringDAO { @Override public void updateAttempts(int deviceId, boolean reset) throws MonitoringDAOException { + Connection conn; PreparedStatement stmt = null; Timestamp currentTimestamp = new Timestamp(Calendar.getInstance().getTime().getTime()); + int tenantId = PrivilegedCarbonContext.getThreadLocalCarbonContext().getTenantId(); + try { conn = this.getConnection(); String query = ""; if (reset) { query = "UPDATE DM_POLICY_COMPLIANCE_STATUS SET ATTEMPTS = 0, LAST_REQUESTED_TIME = ? " + - "WHERE DEVICE_ID = ?"; + "WHERE DEVICE_ID = ? AND TENANT_ID = ?"; } else { query = "UPDATE DM_POLICY_COMPLIANCE_STATUS SET ATTEMPTS = ATTEMPTS + 1, LAST_REQUESTED_TIME = ? " + - "WHERE DEVICE_ID = ?"; + "WHERE DEVICE_ID = ? AND TENANT_ID = ?"; } stmt = conn.prepareStatement(query); stmt.setTimestamp(1, currentTimestamp); stmt.setInt(2, deviceId); + stmt.setInt(3, tenantId); stmt.executeUpdate(); } catch (SQLException e) { @@ -376,24 +407,28 @@ public class MonitoringDAOImpl implements MonitoringDAO { @Override public void updateAttempts(List deviceIds, boolean reset) throws MonitoringDAOException { + Connection conn; PreparedStatement stmt = null; Timestamp currentTimestamp = new Timestamp(Calendar.getInstance().getTime().getTime()); + int tenantId = PrivilegedCarbonContext.getThreadLocalCarbonContext().getTenantId(); + try { conn = this.getConnection(); String query = ""; if (reset) { query = "UPDATE DM_POLICY_COMPLIANCE_STATUS SET ATTEMPTS = 0, LAST_REQUESTED_TIME = ? " + - "WHERE DEVICE_ID = ?"; + "WHERE DEVICE_ID = ? AND TENANT_ID = ?"; } else { query = "UPDATE DM_POLICY_COMPLIANCE_STATUS SET ATTEMPTS = ATTEMPTS + 1, LAST_REQUESTED_TIME = ? " + - "WHERE DEVICE_ID = ?"; + "WHERE DEVICE_ID = ? AND TENANT_ID = ?"; } stmt = conn.prepareStatement(query); for (int deviceId : deviceIds) { stmt.setTimestamp(1, currentTimestamp); stmt.setInt(2, deviceId); + stmt.setInt(3, tenantId); stmt.addBatch(); } stmt.executeBatch(); diff --git a/components/policy-mgt/org.wso2.carbon.policy.mgt.core/src/main/java/org/wso2/carbon/policy/mgt/core/dao/impl/PolicyDAOImpl.java b/components/policy-mgt/org.wso2.carbon.policy.mgt.core/src/main/java/org/wso2/carbon/policy/mgt/core/dao/impl/PolicyDAOImpl.java index 2b21f36942..67f31a386c 100644 --- a/components/policy-mgt/org.wso2.carbon.policy.mgt.core/src/main/java/org/wso2/carbon/policy/mgt/core/dao/impl/PolicyDAOImpl.java +++ b/components/policy-mgt/org.wso2.carbon.policy.mgt.core/src/main/java/org/wso2/carbon/policy/mgt/core/dao/impl/PolicyDAOImpl.java @@ -54,6 +54,7 @@ public class PolicyDAOImpl implements PolicyDAO { Connection conn; PreparedStatement stmt = null; + try { conn = this.getConnection(); String query = "INSERT INTO DM_DEVICE_TYPE_POLICY (DEVICE_TYPE_ID, POLICY_ID) VALUES (?, ?)"; @@ -78,6 +79,7 @@ public class PolicyDAOImpl implements PolicyDAO { Connection conn; PreparedStatement stmt = null; + try { conn = this.getConnection(); String query = "INSERT INTO DM_ROLE_POLICY (ROLE_NAME, POLICY_ID) VALUES (?, ?)"; @@ -104,6 +106,7 @@ public class PolicyDAOImpl implements PolicyDAO { Connection conn; PreparedStatement stmt = null; + try { conn = this.getConnection(); String query = "INSERT INTO DM_USER_POLICY (POLICY_ID, USERNAME) VALUES (?, ?)"; @@ -130,6 +133,7 @@ public class PolicyDAOImpl implements PolicyDAO { Connection conn; PreparedStatement stmt = null; + try { conn = this.getConnection(); String query = "INSERT INTO DM_DEVICE_POLICY (DEVICE_ID, POLICY_ID) VALUES (?, ?)"; @@ -155,14 +159,17 @@ public class PolicyDAOImpl implements PolicyDAO { Connection conn; PreparedStatement stmt = null; + int tenantId = PrivilegedCarbonContext.getThreadLocalCarbonContext().getTenantId(); + try { conn = this.getConnection(); - String query = "UPDATE DM_POLICY SET PRIORITY = ? WHERE ID = ?"; + String query = "UPDATE DM_POLICY SET PRIORITY = ? WHERE ID = ? AND TENANT_ID = ?"; stmt = conn.prepareStatement(query); for (Policy policy : policies) { stmt.setInt(1, policy.getPriorityId()); stmt.setInt(2, policy.getId()); + stmt.setInt(3, tenantId); stmt.addBatch(); } stmt.executeBatch(); @@ -184,8 +191,8 @@ public class PolicyDAOImpl implements PolicyDAO { Connection conn; PreparedStatement stmt = null; ResultSet generatedKeys; - int tenantId = PrivilegedCarbonContext.getThreadLocalCarbonContext().getTenantId(); + try { conn = this.getConnection(); String query = "INSERT INTO DM_CRITERIA (TENANT_ID, NAME) VALUES (?, ?)"; @@ -216,13 +223,14 @@ public class PolicyDAOImpl implements PolicyDAO { Connection conn; PreparedStatement stmt = null; int tenantId = PrivilegedCarbonContext.getThreadLocalCarbonContext().getTenantId(); + try { conn = this.getConnection(); - String query = "UPDATE DM_CRITERIA SET TENANT_ID = ?, NAME = ? WHERE ID = ?"; + String query = "UPDATE DM_CRITERIA SET NAME = ? WHERE ID = ? AND TENANT_ID = ?"; stmt = conn.prepareStatement(query); - stmt.setInt(1, tenantId); - stmt.setString(2, criteria.getName()); - stmt.setInt(3, criteria.getId()); + stmt.setString(1, criteria.getName()); + stmt.setInt(2, criteria.getId()); + stmt.setInt(3, tenantId); stmt.executeUpdate(); } catch (SQLException e) { @@ -242,12 +250,14 @@ public class PolicyDAOImpl implements PolicyDAO { PreparedStatement stmt = null; ResultSet resultSet = null; Criterion criterion = new Criterion(); + int tenantId = PrivilegedCarbonContext.getThreadLocalCarbonContext().getTenantId(); try { conn = this.getConnection(); - String query = "SELECT * FROM DM_CRITERIA WHERE ID= ?"; + String query = "SELECT * FROM DM_CRITERIA WHERE ID= ? AND TENANT_ID = ?"; stmt = conn.prepareStatement(query); stmt.setInt(1, id); + stmt.setInt(2, tenantId); resultSet = stmt.executeQuery(); while (resultSet.next()) { @@ -273,12 +283,14 @@ public class PolicyDAOImpl implements PolicyDAO { PreparedStatement stmt = null; ResultSet resultSet = null; Criterion criterion = new Criterion(); + int tenantId = PrivilegedCarbonContext.getThreadLocalCarbonContext().getTenantId(); try { conn = this.getConnection(); - String query = "SELECT * FROM DM_CRITERIA WHERE NAME= ?"; + String query = "SELECT * FROM DM_CRITERIA WHERE NAME= ? AND TENANT_ID = ?"; stmt = conn.prepareStatement(query); stmt.setString(1, name); + stmt.setInt(2, tenantId); resultSet = stmt.executeQuery(); while (resultSet.next()) { @@ -303,13 +315,15 @@ public class PolicyDAOImpl implements PolicyDAO { Connection conn; PreparedStatement stmt = null; ResultSet resultSet = null; + int tenantId = PrivilegedCarbonContext.getThreadLocalCarbonContext().getTenantId(); boolean exist = false; try { conn = this.getConnection(); - String query = "SELECT * FROM DM_CRITERIA WHERE NAME = ?"; + String query = "SELECT * FROM DM_CRITERIA WHERE NAME = ? AND TENANT_ID = ?"; stmt = conn.prepareStatement(query); stmt.setString(1, name); + stmt.setInt(2, tenantId); resultSet = stmt.executeQuery(); exist = resultSet.next(); @@ -357,11 +371,13 @@ public class PolicyDAOImpl implements PolicyDAO { PreparedStatement stmt = null; ResultSet resultSet = null; List criteria = new ArrayList(); + int tenantId = PrivilegedCarbonContext.getThreadLocalCarbonContext().getTenantId(); try { conn = this.getConnection(); - String query = "SELECT * FROM DM_CRITERIA"; + String query = "SELECT * FROM DM_CRITERIA WHERE TENANT_ID = ?"; stmt = conn.prepareStatement(query); + stmt.setInt(1, tenantId); resultSet = stmt.executeQuery(); while (resultSet.next()) { @@ -523,17 +539,19 @@ public class PolicyDAOImpl implements PolicyDAO { Connection conn; PreparedStatement stmt = null; + int tenantId = PrivilegedCarbonContext.getThreadLocalCarbonContext().getTenantId(); + try { conn = this.getConnection(); - String query = "UPDATE DM_POLICY SET NAME= ?, TENANT_ID = ?, PROFILE_ID = ?, PRIORITY = ?, COMPLIANCE = ?" + - " WHERE ID = ?"; + String query = "UPDATE DM_POLICY SET NAME= ?, PROFILE_ID = ?, PRIORITY = ?, COMPLIANCE = ?" + + " WHERE ID = ? AND TENANT_ID = ?"; stmt = conn.prepareStatement(query); stmt.setString(1, policy.getPolicyName()); - stmt.setInt(2, policy.getTenantId()); - stmt.setInt(3, policy.getProfile().getProfileId()); - stmt.setInt(4, policy.getPriorityId()); - stmt.setString(5, policy.getCompliance()); - stmt.setInt(6, policy.getId()); + stmt.setInt(2, policy.getProfile().getProfileId()); + stmt.setInt(3, policy.getPriorityId()); + stmt.setString(4, policy.getCompliance()); + stmt.setInt(5, policy.getId()); + stmt.setInt(6, tenantId); stmt.executeUpdate(); } catch (SQLException e) { @@ -553,11 +571,14 @@ public class PolicyDAOImpl implements PolicyDAO { PreparedStatement stmt = null; ResultSet resultSet = null; Policy policy = new Policy(); + int tenantId = PrivilegedCarbonContext.getThreadLocalCarbonContext().getTenantId(); + try { conn = this.getConnection(); - String query = "SELECT * FROM DM_POLICY WHERE ID= ?"; + String query = "SELECT * FROM DM_POLICY WHERE ID= ? AND TENANT_ID = ? "; stmt = conn.prepareStatement(query); stmt.setInt(1, policyId); + stmt.setInt(2, tenantId); resultSet = stmt.executeQuery(); while (resultSet.next()) { @@ -576,6 +597,7 @@ public class PolicyDAOImpl implements PolicyDAO { throw new PolicyManagerDAOException(msg, e); } finally { PolicyManagementDAOUtil.cleanupResources(stmt, resultSet); + this.closeConnection(); } } @@ -587,11 +609,14 @@ public class PolicyDAOImpl implements PolicyDAO { PreparedStatement stmt = null; ResultSet resultSet = null; Policy policy = new Policy(); + int tenantId = PrivilegedCarbonContext.getThreadLocalCarbonContext().getTenantId(); + try { conn = this.getConnection(); - String query = "SELECT * FROM DM_POLICY WHERE PROFILE_ID= ?"; + String query = "SELECT * FROM DM_POLICY WHERE PROFILE_ID= ? AND TENANT_ID = ?"; stmt = conn.prepareStatement(query); stmt.setInt(1, profileId); + stmt.setInt(2, tenantId); resultSet = stmt.executeQuery(); while (resultSet.next()) { @@ -621,10 +646,13 @@ public class PolicyDAOImpl implements PolicyDAO { PreparedStatement stmt = null; ResultSet resultSet = null; List policies = new ArrayList(); + int tenantId = PrivilegedCarbonContext.getThreadLocalCarbonContext().getTenantId(); + try { conn = this.getConnection(); - String query = "SELECT * FROM DM_POLICY"; + String query = "SELECT * FROM DM_POLICY WHERE TENANT_ID = ?"; stmt = conn.prepareStatement(query); + stmt.setInt(1, tenantId); resultSet = stmt.executeQuery(); while (resultSet.next()) { @@ -633,7 +661,7 @@ public class PolicyDAOImpl implements PolicyDAO { policy.setId(resultSet.getInt("ID")); policy.setProfileId(resultSet.getInt("PROFILE_ID")); policy.setPolicyName(resultSet.getString("NAME")); - policy.setTenantId(resultSet.getInt("TENANT_ID")); + policy.setTenantId(tenantId); policy.setPriorityId(resultSet.getInt("PRIORITY")); policy.setCompliance(resultSet.getString("COMPLIANCE")); policy.setOwnershipType(resultSet.getString("OWNERSHIP_TYPE")); @@ -750,13 +778,13 @@ public class PolicyDAOImpl implements PolicyDAO { @Override - public void addEffectivePolicyToDevice(int deviceId, Policy policy) - throws PolicyManagerDAOException { + public void addEffectivePolicyToDevice(int deviceId, Policy policy) throws PolicyManagerDAOException { Connection conn; PreparedStatement stmt = null; Timestamp currentTimestamp = new Timestamp(Calendar.getInstance().getTime().getTime()); int tenantId = PrivilegedCarbonContext.getThreadLocalCarbonContext().getTenantId(); + try { conn = this.getConnection(); String query = "INSERT INTO DM_DEVICE_POLICY_APPLIED (DEVICE_ID, POLICY_ID, POLICY_CONTENT, " + @@ -791,13 +819,17 @@ public class PolicyDAOImpl implements PolicyDAO { Connection conn; PreparedStatement stmt = null; Timestamp currentTimestamp = new Timestamp(Calendar.getInstance().getTime().getTime()); + int tenantId = PrivilegedCarbonContext.getThreadLocalCarbonContext().getTenantId(); + try { conn = this.getConnection(); - String query = "UPDATE DM_DEVICE_POLICY_APPLIED SET APPLIED_TIME = ?, APPLIED = ? WHERE DEVICE_ID = ? "; + String query = "UPDATE DM_DEVICE_POLICY_APPLIED SET APPLIED_TIME = ?, APPLIED = ? WHERE DEVICE_ID = ? AND" + + " TENANT_ID = ?"; stmt = conn.prepareStatement(query); stmt.setTimestamp(1, currentTimestamp); stmt.setBoolean(2, true); stmt.setInt(3, deviceId); + stmt.setInt(4, tenantId); stmt.executeUpdate(); @@ -818,16 +850,19 @@ public class PolicyDAOImpl implements PolicyDAO { Connection conn; PreparedStatement stmt = null; Timestamp currentTimestamp = new Timestamp(Calendar.getInstance().getTime().getTime()); + int tenantId = PrivilegedCarbonContext.getThreadLocalCarbonContext().getTenantId(); + try { conn = this.getConnection(); String query = "UPDATE DM_DEVICE_POLICY_APPLIED SET POLICY_ID = ?, POLICY_CONTENT = ?, UPDATED_TIME = ?, " + - "APPLIED = ? WHERE DEVICE_ID = ?"; + "APPLIED = ? WHERE DEVICE_ID = ? AND TENANT_ID = ?"; stmt = conn.prepareStatement(query); stmt.setInt(1, policy.getId()); stmt.setBytes(2, PolicyManagerUtil.getBytes(policy)); stmt.setTimestamp(3, currentTimestamp); stmt.setBoolean(4, false); stmt.setInt(5, deviceId); + stmt.setInt(6, tenantId); stmt.executeUpdate(); } catch (SQLException e) { @@ -846,16 +881,19 @@ public class PolicyDAOImpl implements PolicyDAO { @Override public boolean checkPolicyAvailable(int deviceId) throws PolicyManagerDAOException { + Connection conn; PreparedStatement stmt = null; ResultSet resultSet = null; boolean exist = false; + int tenantId = PrivilegedCarbonContext.getThreadLocalCarbonContext().getTenantId(); try { conn = this.getConnection(); - String query = "SELECT * FROM DM_DEVICE_POLICY_APPLIED WHERE DEVICE_ID = ?"; + String query = "SELECT * FROM DM_DEVICE_POLICY_APPLIED WHERE DEVICE_ID = ? AND TENANT_ID = ?"; stmt = conn.prepareStatement(query); stmt.setInt(1, deviceId); + stmt.setInt(2, tenantId); resultSet = stmt.executeQuery(); exist = resultSet.next(); @@ -965,12 +1003,14 @@ public class PolicyDAOImpl implements PolicyDAO { Connection conn; PreparedStatement stmt = null; + int tenantId = PrivilegedCarbonContext.getThreadLocalCarbonContext().getTenantId(); try { conn = this.getConnection(); - String query = "DELETE FROM DM_POLICY WHERE ID = ?"; + String query = "DELETE FROM DM_POLICY WHERE ID = ? AND TENANT_ID = ?"; stmt = conn.prepareStatement(query); stmt.setInt(1, policy.getId()); + stmt.setInt(2, tenantId); stmt.executeUpdate(); if (log.isDebugEnabled()) { @@ -988,14 +1028,17 @@ public class PolicyDAOImpl implements PolicyDAO { @Override public boolean deletePolicy(int policyId) throws PolicyManagerDAOException { + Connection conn; PreparedStatement stmt = null; + int tenantId = PrivilegedCarbonContext.getThreadLocalCarbonContext().getTenantId(); try { conn = this.getConnection(); - String query = "DELETE FROM DM_POLICY WHERE ID = ?"; + String query = "DELETE FROM DM_POLICY WHERE ID = ? AND TENANT_ID = ?"; stmt = conn.prepareStatement(query); stmt.setInt(1, policyId); + stmt.setInt(2, tenantId); stmt.executeUpdate(); if (log.isDebugEnabled()) { @@ -1158,11 +1201,13 @@ public class PolicyDAOImpl implements PolicyDAO { PreparedStatement stmt = null; ResultSet resultSet = null; int priority = 0; + int tenantId = PrivilegedCarbonContext.getThreadLocalCarbonContext().getTenantId(); try { conn = this.getConnection(); - String query = "SELECT MAX(PRIORITY) PRIORITY FROM DM_POLICY;"; + String query = "SELECT MAX(PRIORITY) PRIORITY FROM DM_POLICY WHERE TENANT_ID = ?"; stmt = conn.prepareStatement(query); + stmt.setInt(1, tenantId); resultSet = stmt.executeQuery(); while (resultSet.next()) { @@ -1188,11 +1233,14 @@ public class PolicyDAOImpl implements PolicyDAO { Connection conn; PreparedStatement stmt = null; ResultSet resultSet = null; + int tenantId = PrivilegedCarbonContext.getThreadLocalCarbonContext().getTenantId(); int policyCount = 0; + try { conn = this.getConnection(); - String query = "SELECT COUNT(ID) AS POLICY_COUNT FROM DM_POLICY"; + String query = "SELECT COUNT(ID) AS POLICY_COUNT FROM DM_POLICY WHERE TENANT_ID = ?"; stmt = conn.prepareStatement(query); + stmt.setInt(1, tenantId); resultSet = stmt.executeQuery(); while (resultSet.next()) { @@ -1216,12 +1264,14 @@ public class PolicyDAOImpl implements PolicyDAO { Connection conn; PreparedStatement stmt = null; ResultSet resultSet = null; + int tenantId = PrivilegedCarbonContext.getThreadLocalCarbonContext().getTenantId(); try { conn = this.getConnection(); - String query = "SELECT * FROM DM_DEVICE_POLICY_APPLIED WHERE DEVICE_ID = ?"; + String query = "SELECT * FROM DM_DEVICE_POLICY_APPLIED WHERE DEVICE_ID = ? AND TENANT_ID = ?"; stmt = conn.prepareStatement(query); stmt.setInt(1, deviceId); + stmt.setInt(2, tenantId); resultSet = stmt.executeQuery(); while (resultSet.next()) { @@ -1246,21 +1296,16 @@ public class PolicyDAOImpl implements PolicyDAO { Connection conn; PreparedStatement stmt = null; ResultSet resultSet = null; - + int tenantId = PrivilegedCarbonContext.getThreadLocalCarbonContext().getTenantId(); Policy policy = null; try { conn = this.getConnection(); - String query = "SELECT * FROM DM_DEVICE_POLICY_APPLIED WHERE DEVICE_ID = ?"; + String query = "SELECT * FROM DM_DEVICE_POLICY_APPLIED WHERE DEVICE_ID = ? AND TENANT_ID = ?"; stmt = conn.prepareStatement(query); stmt.setInt(1, deviceId); + stmt.setInt(2, tenantId); resultSet = stmt.executeQuery(); -// log.debug("Logging the statement................." + stmt.toString()); -// log.debug("+++++++++++++++++++++++++++++"); -// log.debug(conn.toString()); - - -// log.debug("+++++++++++++++++++++++++++++"); while (resultSet.next()) { ByteArrayInputStream bais = null; @@ -1320,6 +1365,7 @@ public class PolicyDAOImpl implements PolicyDAO { @Override public HashMap getAppliedPolicyIds(List deviceIds) throws PolicyManagerDAOException { + Connection conn; PreparedStatement stmt = null; ResultSet resultSet = null; diff --git a/components/policy-mgt/org.wso2.carbon.policy.mgt.core/src/main/java/org/wso2/carbon/policy/mgt/core/dao/impl/ProfileDAOImpl.java b/components/policy-mgt/org.wso2.carbon.policy.mgt.core/src/main/java/org/wso2/carbon/policy/mgt/core/dao/impl/ProfileDAOImpl.java index eccb9eedc3..f6700942d9 100644 --- a/components/policy-mgt/org.wso2.carbon.policy.mgt.core/src/main/java/org/wso2/carbon/policy/mgt/core/dao/impl/ProfileDAOImpl.java +++ b/components/policy-mgt/org.wso2.carbon.policy.mgt.core/src/main/java/org/wso2/carbon/policy/mgt/core/dao/impl/ProfileDAOImpl.java @@ -225,6 +225,7 @@ public class ProfileDAOImpl implements ProfileDAO { List profileList = new ArrayList(); try { + //TODO : Fix with TenantID. conn = this.getConnection(); String query = "SELECT * FROM DM_PROFILE"; stmt = conn.prepareStatement(query); diff --git a/components/policy-mgt/org.wso2.carbon.policy.mgt.core/src/main/java/org/wso2/carbon/policy/mgt/core/impl/ComplianceDecisionPointImpl.java b/components/policy-mgt/org.wso2.carbon.policy.mgt.core/src/main/java/org/wso2/carbon/policy/mgt/core/impl/ComplianceDecisionPointImpl.java index 3036be5050..971d2e268a 100644 --- a/components/policy-mgt/org.wso2.carbon.policy.mgt.core/src/main/java/org/wso2/carbon/policy/mgt/core/impl/ComplianceDecisionPointImpl.java +++ b/components/policy-mgt/org.wso2.carbon.policy.mgt.core/src/main/java/org/wso2/carbon/policy/mgt/core/impl/ComplianceDecisionPointImpl.java @@ -140,15 +140,22 @@ public class ComplianceDecisionPointImpl implements ComplianceDecisionPoint { } } else { List noneComplianceFeatures = complianceData.getComplianceFeatures(); + List effectiveFeatures = policy.getProfile().getProfileFeaturesList(); for (ComplianceFeature feature : noneComplianceFeatures) { - ProfileOperation profileOperation = new ProfileOperation(); - profileOperation.setCode(feature.getFeatureCode()); - profileOperation.setEnabled(true); - profileOperation.setStatus(Operation.Status.PENDING); - profileOperation.setType(Operation.Type.PROFILE); - profileOperation.setPayLoad(feature.getFeature().getContent()); - profileOperationList.add(profileOperation); + for (ProfileFeature pf : effectiveFeatures) { + if (pf.getFeatureCode().equalsIgnoreCase(feature.getFeatureCode())) { + + ProfileOperation profileOperation = new ProfileOperation(); + + profileOperation.setCode(feature.getFeatureCode()); + profileOperation.setEnabled(true); + profileOperation.setStatus(Operation.Status.PENDING); + profileOperation.setType(Operation.Type.PROFILE); + profileOperation.setPayLoad(pf.getContent()); + profileOperationList.add(profileOperation); + } + } } } policyOperation.setProfileOperations(profileOperationList); diff --git a/components/policy-mgt/org.wso2.carbon.policy.mgt.core/src/main/java/org/wso2/carbon/policy/mgt/core/impl/PolicyAdministratorPointImpl.java b/components/policy-mgt/org.wso2.carbon.policy.mgt.core/src/main/java/org/wso2/carbon/policy/mgt/core/impl/PolicyAdministratorPointImpl.java index 32d734d64e..e406e9fbaf 100644 --- a/components/policy-mgt/org.wso2.carbon.policy.mgt.core/src/main/java/org/wso2/carbon/policy/mgt/core/impl/PolicyAdministratorPointImpl.java +++ b/components/policy-mgt/org.wso2.carbon.policy.mgt.core/src/main/java/org/wso2/carbon/policy/mgt/core/impl/PolicyAdministratorPointImpl.java @@ -58,22 +58,22 @@ public class PolicyAdministratorPointImpl implements PolicyAdministratorPoint { @Override public Policy addPolicy(Policy policy) throws PolicyManagementException { Policy resultantPolicy = policyManager.addPolicy(policy); - try { - delegator.delegate(resultantPolicy, resultantPolicy.getDevices()); - } catch (PolicyDelegationException e) { - throw new PolicyManagementException("Error occurred while delegating policy operation to the devices", e); - } +// try { +// delegator.delegate(resultantPolicy, resultantPolicy.getDevices()); +// } catch (PolicyDelegationException e) { +// throw new PolicyManagementException("Error occurred while delegating policy operation to the devices", e); +// } return resultantPolicy; } @Override public Policy updatePolicy(Policy policy) throws PolicyManagementException { Policy resultantPolicy = policyManager.updatePolicy(policy); - try { - delegator.delegate(resultantPolicy, resultantPolicy.getDevices()); - } catch (PolicyDelegationException e) { - throw new PolicyManagementException("Error occurred while delegating policy operation to the devices", e); - } +// try { +// delegator.delegate(resultantPolicy, resultantPolicy.getDevices()); +// } catch (PolicyDelegationException e) { +// throw new PolicyManagementException("Error occurred while delegating policy operation to the devices", e); +// } return resultantPolicy; } diff --git a/components/policy-mgt/org.wso2.carbon.policy.mgt.core/src/main/java/org/wso2/carbon/policy/mgt/core/impl/PolicyFilterImpl.java b/components/policy-mgt/org.wso2.carbon.policy.mgt.core/src/main/java/org/wso2/carbon/policy/mgt/core/impl/PolicyFilterImpl.java index 0676b68a04..f6448508aa 100644 --- a/components/policy-mgt/org.wso2.carbon.policy.mgt.core/src/main/java/org/wso2/carbon/policy/mgt/core/impl/PolicyFilterImpl.java +++ b/components/policy-mgt/org.wso2.carbon.policy.mgt.core/src/main/java/org/wso2/carbon/policy/mgt/core/impl/PolicyFilterImpl.java @@ -89,13 +89,19 @@ public class PolicyFilterImpl implements PolicyFilter { List temp = new ArrayList(); for (Policy policy : policies) { + List users = policy.getUsers(); - if(users.contains(PolicyManagementConstants.ANY)) { + + if (users.isEmpty()) { + temp.add(policy); + continue; + } + if (users.contains(PolicyManagementConstants.ANY)) { temp.add(policy); continue; } for (String user : users) { - if(username.equalsIgnoreCase(user)) { + if (username.equalsIgnoreCase(user)) { temp.add(policy); } } diff --git a/components/policy-mgt/org.wso2.carbon.policy.mgt.core/src/main/java/org/wso2/carbon/policy/mgt/core/mgt/impl/MonitoringManagerImpl.java b/components/policy-mgt/org.wso2.carbon.policy.mgt.core/src/main/java/org/wso2/carbon/policy/mgt/core/mgt/impl/MonitoringManagerImpl.java index 030043a1e9..db31b6b727 100644 --- a/components/policy-mgt/org.wso2.carbon.policy.mgt.core/src/main/java/org/wso2/carbon/policy/mgt/core/mgt/impl/MonitoringManagerImpl.java +++ b/components/policy-mgt/org.wso2.carbon.policy.mgt.core/src/main/java/org/wso2/carbon/policy/mgt/core/mgt/impl/MonitoringManagerImpl.java @@ -304,7 +304,7 @@ public class MonitoringManagerImpl implements MonitoringManager { if (!deviceIdsWithExistingOperation.isEmpty()) { monitoringDAO.updateAttempts(new ArrayList<>(deviceIdsWithExistingOperation.keySet()), false); - //TODO uncomment with proper fix + //TODO: Add attempts. This has to be fixed in the get pending operation tables too. This will be // decisionPoint.setDevicesAsUnreachable(this.getDeviceIdentifiersFromDevices( // new ArrayList<>(deviceIdsWithExistingOperation.values()))); } diff --git a/components/policy-mgt/org.wso2.carbon.policy.mgt.core/src/main/java/org/wso2/carbon/policy/mgt/core/mgt/impl/PolicyManagerImpl.java b/components/policy-mgt/org.wso2.carbon.policy.mgt.core/src/main/java/org/wso2/carbon/policy/mgt/core/mgt/impl/PolicyManagerImpl.java index e32de22c95..f3dc1c0638 100644 --- a/components/policy-mgt/org.wso2.carbon.policy.mgt.core/src/main/java/org/wso2/carbon/policy/mgt/core/mgt/impl/PolicyManagerImpl.java +++ b/components/policy-mgt/org.wso2.carbon.policy.mgt.core/src/main/java/org/wso2/carbon/policy/mgt/core/mgt/impl/PolicyManagerImpl.java @@ -291,8 +291,9 @@ public class PolicyManagerImpl implements PolicyManager { public boolean deletePolicy(int policyId) throws PolicyManagementException { try { - PolicyManagementDAOFactory.beginTransaction(); Policy policy = policyDAO.getPolicy(policyId); + + PolicyManagementDAOFactory.beginTransaction(); policyDAO.deleteAllPolicyRelatedConfigs(policyId); policyDAO.deletePolicy(policyId); diff --git a/components/policy-mgt/org.wso2.carbon.policy.mgt.core/src/main/java/org/wso2/carbon/policy/mgt/core/util/PolicyManagementConstants.java b/components/policy-mgt/org.wso2.carbon.policy.mgt.core/src/main/java/org/wso2/carbon/policy/mgt/core/util/PolicyManagementConstants.java index e6fe25c9a4..2c033151ca 100644 --- a/components/policy-mgt/org.wso2.carbon.policy.mgt.core/src/main/java/org/wso2/carbon/policy/mgt/core/util/PolicyManagementConstants.java +++ b/components/policy-mgt/org.wso2.carbon.policy.mgt.core/src/main/java/org/wso2/carbon/policy/mgt/core/util/PolicyManagementConstants.java @@ -37,4 +37,9 @@ public final class PolicyManagementConstants { public static final String TASK_CLAZZ = "org.wso2.carbon.policy.mgt.core.task.MonitoringTask"; + public static final String DM_CACHE_MANAGER = "DM_CACHE_MANAGER"; + public static final String DM_CACHE = "DM_CACHE"; + + + } diff --git a/components/policy-mgt/org.wso2.carbon.policy.mgt.core/src/main/java/org/wso2/carbon/policy/mgt/core/util/PolicyManagerUtil.java b/components/policy-mgt/org.wso2.carbon.policy.mgt.core/src/main/java/org/wso2/carbon/policy/mgt/core/util/PolicyManagerUtil.java index d63a0083d0..1beea7f39f 100644 --- a/components/policy-mgt/org.wso2.carbon.policy.mgt.core/src/main/java/org/wso2/carbon/policy/mgt/core/util/PolicyManagerUtil.java +++ b/components/policy-mgt/org.wso2.carbon.policy.mgt.core/src/main/java/org/wso2/carbon/policy/mgt/core/util/PolicyManagerUtil.java @@ -31,6 +31,9 @@ import org.wso2.carbon.policy.mgt.core.config.datasource.DataSourceConfig; import org.wso2.carbon.policy.mgt.core.config.datasource.JNDILookupDefinition; import org.wso2.carbon.policy.mgt.core.dao.util.PolicyManagementDAOUtil; +import javax.cache.Cache; +import javax.cache.CacheManager; +import javax.cache.Caching; import javax.sql.DataSource; import javax.xml.parsers.DocumentBuilder; import javax.xml.parsers.DocumentBuilderFactory; @@ -136,4 +139,10 @@ public class PolicyManagerUtil { byte[] data = bos.toByteArray(); return data; } + + + public static Cache getCacheManagerImpl(){ + return Caching.getCacheManagerFactory() + .getCacheManager(PolicyManagementConstants.DM_CACHE_MANAGER).getCache(PolicyManagementConstants.DM_CACHE); + } } diff --git a/components/policy-mgt/org.wso2.carbon.policy.mgt.core/src/test/java/org/wso2/carbon/policy/mgt/core/PolicyDAOTestCase.java b/components/policy-mgt/org.wso2.carbon.policy.mgt.core/src/test/java/org/wso2/carbon/policy/mgt/core/PolicyDAOTestCase.java index 4c0ecc1727..e14ea1a1b3 100644 --- a/components/policy-mgt/org.wso2.carbon.policy.mgt.core/src/test/java/org/wso2/carbon/policy/mgt/core/PolicyDAOTestCase.java +++ b/components/policy-mgt/org.wso2.carbon.policy.mgt.core/src/test/java/org/wso2/carbon/policy/mgt/core/PolicyDAOTestCase.java @@ -31,6 +31,7 @@ import org.wso2.carbon.device.mgt.core.service.DeviceManagementProviderService; import org.wso2.carbon.device.mgt.core.service.DeviceManagementProviderServiceImpl; import org.wso2.carbon.policy.mgt.common.*; import org.wso2.carbon.policy.mgt.core.impl.PolicyAdministratorPointImpl; +import org.wso2.carbon.policy.mgt.core.internal.PolicyManagementDataHolder; import org.wso2.carbon.policy.mgt.core.mgt.FeatureManager; import org.wso2.carbon.policy.mgt.core.mgt.PolicyManager; import org.wso2.carbon.policy.mgt.core.mgt.ProfileManager; @@ -97,6 +98,8 @@ public class PolicyDAOTestCase extends BasePolicyManagementDAOTest { DeviceManagementProviderService service = new DeviceManagementProviderServiceImpl(); + PolicyManagementDataHolder.getInstance().setDeviceManagementService(service); + log.debug("Printing device taken by calling the service layer with device type."); List devices3 = service.getAllDevices("android"); diff --git a/components/policy-mgt/org.wso2.carbon.policy.mgt.core/src/test/java/org/wso2/carbon/policy/mgt/core/util/PolicyCreator.java b/components/policy-mgt/org.wso2.carbon.policy.mgt.core/src/test/java/org/wso2/carbon/policy/mgt/core/util/PolicyCreator.java index 811f482bb9..7ef7ab4a37 100644 --- a/components/policy-mgt/org.wso2.carbon.policy.mgt.core/src/test/java/org/wso2/carbon/policy/mgt/core/util/PolicyCreator.java +++ b/components/policy-mgt/org.wso2.carbon.policy.mgt.core/src/test/java/org/wso2/carbon/policy/mgt/core/util/PolicyCreator.java @@ -37,7 +37,7 @@ public class PolicyCreator { List users = new ArrayList(); users.add("Dilshan"); policy.setUsers(users); - policy.setCompliance("ENFORCE"); + policy.setCompliance("NOTIFY"); policy.setOwnershipType("COPE"); return policy; @@ -52,7 +52,7 @@ public class PolicyCreator { policy.setProfile(profile); policy.setDevices(DeviceCreator.getDeviceList(DeviceTypeCreator.getDeviceType())); - policy.setCompliance("NOTIFY"); + policy.setCompliance("ENFORCE"); List roles = new ArrayList(); roles.add("Role_01"); diff --git a/components/policy-mgt/org.wso2.carbon.policy.mgt.core/src/test/resources/sql/CreateH2TestDB.sql b/components/policy-mgt/org.wso2.carbon.policy.mgt.core/src/test/resources/sql/CreateH2TestDB.sql index 219386f226..3263fa8290 100644 --- a/components/policy-mgt/org.wso2.carbon.policy.mgt.core/src/test/resources/sql/CreateH2TestDB.sql +++ b/components/policy-mgt/org.wso2.carbon.policy.mgt.core/src/test/resources/sql/CreateH2TestDB.sql @@ -201,6 +201,7 @@ CREATE TABLE IF NOT EXISTS DM_PROFILE_FEATURES ( PROFILE_ID INT(11) NOT NULL, FEATURE_CODE VARCHAR(30) NOT NULL, DEVICE_TYPE_ID INT NOT NULL, + TENANT_ID INT(11) NOT NULL , CONTENT BLOB NULL DEFAULT NULL, PRIMARY KEY (ID), CONSTRAINT FK_DM_PROFILE_DM_POLICY_FEATURES @@ -314,6 +315,7 @@ 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, + TENANT_ID INT NOT NULL, STATUS INT NULL, LAST_SUCCESS_TIME TIMESTAMP NULL, LAST_REQUESTED_TIME TIMESTAMP NULL, @@ -332,6 +334,7 @@ CREATE TABLE IF NOT EXISTS DM_POLICY_COMPLIANCE_STATUS ( CREATE TABLE IF NOT EXISTS DM_POLICY_COMPLIANCE_FEATURES ( ID INT NOT NULL AUTO_INCREMENT, COMPLIANCE_STATUS_ID INT NOT NULL, + TENANT_ID INT NOT NULL, FEATURE_CODE VARCHAR(15) NOT NULL, STATUS INT NULL, PRIMARY KEY (ID), diff --git a/features/device-mgt/org.wso2.carbon.device.mgt.server.feature/src/main/resources/dbscripts/cdm/h2.sql b/features/device-mgt/org.wso2.carbon.device.mgt.server.feature/src/main/resources/dbscripts/cdm/h2.sql index 20545e5e9d..44a178ef51 100644 --- a/features/device-mgt/org.wso2.carbon.device.mgt.server.feature/src/main/resources/dbscripts/cdm/h2.sql +++ b/features/device-mgt/org.wso2.carbon.device.mgt.server.feature/src/main/resources/dbscripts/cdm/h2.sql @@ -191,6 +191,7 @@ CREATE TABLE IF NOT EXISTS DM_PROFILE_FEATURES ( PROFILE_ID INT(11) NOT NULL, FEATURE_CODE VARCHAR(30) NOT NULL, DEVICE_TYPE_ID INT NOT NULL, + TENANT_ID INT(11) NOT NULL , CONTENT BLOB NULL DEFAULT NULL, PRIMARY KEY (ID), CONSTRAINT FK_DM_PROFILE_DM_POLICY_FEATURES @@ -300,6 +301,7 @@ 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, + TENANT_ID INT NOT NULL, STATUS INT NULL, LAST_SUCCESS_TIME TIMESTAMP NULL, LAST_REQUESTED_TIME TIMESTAMP NULL, @@ -318,6 +320,7 @@ CREATE TABLE IF NOT EXISTS DM_POLICY_COMPLIANCE_STATUS ( CREATE TABLE IF NOT EXISTS DM_POLICY_COMPLIANCE_FEATURES ( ID INT NOT NULL AUTO_INCREMENT, COMPLIANCE_STATUS_ID INT NOT NULL, + TENANT_ID INT NOT NULL, FEATURE_CODE VARCHAR(15) NOT NULL, STATUS INT NULL, PRIMARY KEY (ID), diff --git a/pom.xml b/pom.xml index 0607803120..9a8b1c673d 100644 --- a/pom.xml +++ b/pom.xml @@ -414,10 +414,10 @@ org.eclipse.equinox org.eclipse.equinox.http.helper - + org.wso2.carbon org.wso2.carbon.registry.core