revert-70aa11f8
Kamidu Sachith 9 years ago
commit 74a3556cf4

@ -171,6 +171,7 @@ public class Policy implements Comparable<Policy>, Serializable {
this.updated = updated; this.updated = updated;
} }
@XmlElement
public String getDescription() { public String getDescription() {
return description; return description;
} }

@ -41,6 +41,13 @@ public interface PolicyDAO {
*/ */
Policy addPolicyToRole(List<String> roleNames, Policy policy) throws PolicyManagerDAOException; Policy addPolicyToRole(List<String> roleNames, Policy policy) throws PolicyManagerDAOException;
/**
* This method is used to add/update the users associated with the policy.
* @param usernameList - List of the users that needs to be applied
* @param policy - policy object with the current role list
* @return
* @throws PolicyManagerDAOException
*/
Policy addPolicyToUser(List<String> usernameList, Policy policy) throws PolicyManagerDAOException; Policy addPolicyToUser(List<String> usernameList, Policy policy) throws PolicyManagerDAOException;
Policy addPolicyToDevice(List<Device> devices, Policy policy) throws PolicyManagerDAOException; Policy addPolicyToDevice(List<Device> devices, Policy policy) throws PolicyManagerDAOException;

@ -110,23 +110,45 @@ public class PolicyDAOImpl implements PolicyDAO {
} }
@Override @Override
public Policy addPolicyToUser(List<String> usernameList, Policy policy) throws PolicyManagerDAOException { public Policy addPolicyToUser(List<String> usersToAdd, Policy policy) throws PolicyManagerDAOException {
Connection conn; Connection conn;
PreparedStatement stmt = null; PreparedStatement insertStmt = null;
PreparedStatement deleteStmt = null;
final List<String> currentUsers = policy.getUsers();
SetReferenceTransformer<String> transformer = new SetReferenceTransformer<String>();
transformer.transform(currentUsers, usersToAdd);
usersToAdd = transformer.getObjectsToAdd();
List<String> usersToDelete = transformer.getObjectsToRemove();
try { try {
conn = this.getConnection(); conn = this.getConnection();
if (usersToAdd.size() > 0){
String query = "INSERT INTO DM_USER_POLICY (POLICY_ID, USERNAME) VALUES (?, ?)"; String query = "INSERT INTO DM_USER_POLICY (POLICY_ID, USERNAME) VALUES (?, ?)";
stmt = conn.prepareStatement(query); insertStmt = conn.prepareStatement(query);
for (String username : usernameList) { for (String username : usersToAdd) {
stmt.setInt(1, policy.getId()); insertStmt.setInt(1, policy.getId());
stmt.setString(2, username); insertStmt.setString(2, username);
stmt.addBatch(); insertStmt.addBatch();
} }
stmt.executeBatch(); insertStmt.executeBatch();
}
if (usersToDelete.size() > 0){
String deleteQuery = "DELETE FROM DM_USER_POLICY WHERE USERNAME=? AND POLICY_ID=?";
deleteStmt = conn.prepareStatement(deleteQuery);
for (String username : usersToDelete) {
deleteStmt.setString(1, username);
deleteStmt.setInt(2, policy.getId());
deleteStmt.addBatch();
}
deleteStmt.executeBatch();
}
} catch (SQLException e) { } catch (SQLException e) {
throw new PolicyManagerDAOException("Error occurred while adding the user name with policy to database", e); throw new PolicyManagerDAOException("Error occurred while adding the user name with policy to database", e);
} finally { } finally {
PolicyManagementDAOUtil.cleanupResources(stmt, null); PolicyManagementDAOUtil.cleanupResources(insertStmt, null);
PolicyManagementDAOUtil.cleanupResources(deleteStmt, null);
} }
return policy; return policy;
} }
@ -744,6 +766,8 @@ public class PolicyDAOImpl implements PolicyDAO {
policy.setProfileId(resultSet.getInt("PROFILE_ID")); policy.setProfileId(resultSet.getInt("PROFILE_ID"));
policy.setCompliance(resultSet.getString("COMPLIANCE")); policy.setCompliance(resultSet.getString("COMPLIANCE"));
policy.setDescription(resultSet.getString("DESCRIPTION")); policy.setDescription(resultSet.getString("DESCRIPTION"));
policy.setUpdated(PolicyManagerUtil.convertIntToBoolean(resultSet.getInt("UPDATED")));
policy.setActive(PolicyManagerUtil.convertIntToBoolean(resultSet.getInt("ACTIVE")));
} }
return policy; return policy;
@ -778,6 +802,8 @@ public class PolicyDAOImpl implements PolicyDAO {
policy.setPriorityId(resultSet.getInt("PRIORITY")); policy.setPriorityId(resultSet.getInt("PRIORITY"));
policy.setCompliance(resultSet.getString("COMPLIANCE")); policy.setCompliance(resultSet.getString("COMPLIANCE"));
policy.setDescription(resultSet.getString("DESCRIPTION")); policy.setDescription(resultSet.getString("DESCRIPTION"));
policy.setUpdated(PolicyManagerUtil.convertIntToBoolean(resultSet.getInt("UPDATED")));
policy.setActive(PolicyManagerUtil.convertIntToBoolean(resultSet.getInt("ACTIVE")));
} }
return policy; return policy;
} catch (SQLException e) { } catch (SQLException e) {

@ -35,6 +35,7 @@ import org.wso2.carbon.policy.mgt.core.config.datasource.DataSourceConfig;
import org.wso2.carbon.policy.mgt.core.dao.PolicyManagementDAOFactory; import org.wso2.carbon.policy.mgt.core.dao.PolicyManagementDAOFactory;
import org.wso2.carbon.policy.mgt.core.task.TaskScheduleService; import org.wso2.carbon.policy.mgt.core.task.TaskScheduleService;
import org.wso2.carbon.policy.mgt.core.task.TaskScheduleServiceImpl; import org.wso2.carbon.policy.mgt.core.task.TaskScheduleServiceImpl;
import org.wso2.carbon.policy.mgt.core.util.PolicyManagerUtil;
import org.wso2.carbon.user.core.service.RealmService; import org.wso2.carbon.user.core.service.RealmService;
/** /**
@ -86,11 +87,13 @@ public class PolicyManagementServiceComponent {
componentContext.getBundleContext().registerService( componentContext.getBundleContext().registerService(
PolicyManagerService.class.getName(), new PolicyManagerServiceImpl(), null); PolicyManagerService.class.getName(), new PolicyManagerServiceImpl(), null);
PolicyConfiguration policyConfiguration = DeviceConfigurationManager.getInstance().getDeviceManagementConfig(). PolicyConfiguration policyConfiguration = DeviceConfigurationManager.getInstance().getDeviceManagementConfig().
getDeviceManagementConfigRepository().getPolicyConfiguration(); getDeviceManagementConfigRepository().getPolicyConfiguration();
if(policyConfiguration.getMonitoringEnable()) { if(policyConfiguration.getMonitoringEnable()) {
TaskScheduleService taskScheduleService = new TaskScheduleServiceImpl(); TaskScheduleService taskScheduleService = new TaskScheduleServiceImpl();
taskScheduleService.startTask(policyConfiguration.getMonitoringFrequency()); taskScheduleService.startTask(PolicyManagerUtil.getMonitoringFequency());
} }
} catch (Throwable t) { } catch (Throwable t) {

@ -471,17 +471,18 @@ public class PolicyManagerImpl implements PolicyManager {
Policy policy; Policy policy;
List<Device> deviceList; List<Device> deviceList;
List<String> roleNames; List<String> roleNames;
List<String> userNames;
try { try {
PolicyManagementDAOFactory.openConnection(); PolicyManagementDAOFactory.openConnection();
policy = policyDAO.getPolicy(policyId); policy = policyDAO.getPolicy(policyId);
roleNames = policyDAO.getPolicyAppliedRoles(policyId); roleNames = policyDAO.getPolicyAppliedRoles(policyId);
userNames = policyDAO.getPolicyAppliedUsers(policyId);
Profile profile = profileDAO.getProfile(policy.getProfileId()); Profile profile = profileDAO.getProfile(policy.getProfileId());
policy.setProfile(profile); policy.setProfile(profile);
policy.setRoles(roleNames); policy.setRoles(roleNames);
policy.setUsers(userNames);
} catch (PolicyManagerDAOException e) { } catch (PolicyManagerDAOException e) {
throw new PolicyManagementException("Error occurred while getting the policy related to policy ID (" + throw new PolicyManagementException("Error occurred while getting the policy related to policy ID (" +

@ -22,7 +22,14 @@ import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory; import org.apache.commons.logging.LogFactory;
import org.w3c.dom.Document; import org.w3c.dom.Document;
import org.wso2.carbon.device.mgt.common.Device; import org.wso2.carbon.device.mgt.common.Device;
import org.wso2.carbon.device.mgt.common.configuration.mgt.ConfigurationEntry;
import org.wso2.carbon.device.mgt.common.configuration.mgt.ConfigurationManagementException;
import org.wso2.carbon.device.mgt.common.configuration.mgt.TenantConfiguration;
import org.wso2.carbon.device.mgt.common.configuration.mgt.TenantConfigurationManagementService;
import org.wso2.carbon.device.mgt.common.operation.mgt.Operation; import org.wso2.carbon.device.mgt.common.operation.mgt.Operation;
import org.wso2.carbon.device.mgt.core.config.DeviceConfigurationManager;
import org.wso2.carbon.device.mgt.core.config.policy.PolicyConfiguration;
import org.wso2.carbon.device.mgt.core.config.tenant.TenantConfigurationManagementServiceImpl;
import org.wso2.carbon.device.mgt.core.operation.mgt.PolicyOperation; import org.wso2.carbon.device.mgt.core.operation.mgt.PolicyOperation;
import org.wso2.carbon.device.mgt.core.operation.mgt.ProfileOperation; import org.wso2.carbon.device.mgt.core.operation.mgt.ProfileOperation;
import org.wso2.carbon.policy.mgt.common.Policy; import org.wso2.carbon.policy.mgt.common.Policy;
@ -47,6 +54,10 @@ public class PolicyManagerUtil {
private static final Log log = LogFactory.getLog(PolicyManagerUtil.class); private static final Log log = LogFactory.getLog(PolicyManagerUtil.class);
public static final String GENERAL_CONFIG_RESOURCE_PATH = "general";
public static final String MONITORING_FREQUENCY = "notifierFrequency";
public static Document convertToDocument(File file) throws PolicyManagementException { public static Document convertToDocument(File file) throws PolicyManagementException {
DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance(); DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();
factory.setNamespaceAware(true); factory.setNamespaceAware(true);
@ -156,13 +167,13 @@ public class PolicyManagerUtil {
// } // }
public static Cache<Integer, Policy> getPolicyCache(String name){ public static Cache<Integer, Policy> getPolicyCache(String name) {
CacheManager manager = getCacheManager(); CacheManager manager = getCacheManager();
return (manager != null) ? manager.<Integer, Policy>getCache(name) : return (manager != null) ? manager.<Integer, Policy>getCache(name) :
Caching.getCacheManager().<Integer, Policy>getCache(name); Caching.getCacheManager().<Integer, Policy>getCache(name);
} }
public static Cache<Integer, List<Policy>> getPolicyListCache(String name){ public static Cache<Integer, List<Policy>> getPolicyListCache(String name) {
CacheManager manager = getCacheManager(); CacheManager manager = getCacheManager();
return (manager != null) ? manager.<Integer, List<Policy>>getCache(name) : return (manager != null) ? manager.<Integer, List<Policy>>getCache(name) :
Caching.getCacheManager().<Integer, List<Policy>>getCache(name); Caching.getCacheManager().<Integer, List<Policy>>getCache(name);
@ -182,4 +193,35 @@ public class PolicyManagerUtil {
} }
return deviceHashMap; return deviceHashMap;
} }
public static int getMonitoringFequency() {
TenantConfigurationManagementService configMgtService = new TenantConfigurationManagementServiceImpl();
TenantConfiguration tenantConfiguration = null;
int monitoringFrequency = 0;
try {
tenantConfiguration = configMgtService.getConfiguration(GENERAL_CONFIG_RESOURCE_PATH);
List<ConfigurationEntry> configuration = tenantConfiguration.getConfiguration();
if (configuration != null && !configuration.isEmpty()) {
for (ConfigurationEntry cEntry : configuration) {
if (cEntry.getName().equalsIgnoreCase(MONITORING_FREQUENCY)) {
monitoringFrequency = (int) cEntry.getValue();
}
}
}
} catch (ConfigurationManagementException e) {
log.error("Error while getting the configurations from registry.", e);
}
if (monitoringFrequency == 0) {
PolicyConfiguration policyConfiguration = DeviceConfigurationManager.getInstance().
getDeviceManagementConfig().getDeviceManagementConfigRepository().getPolicyConfiguration();
monitoringFrequency = policyConfiguration.getMonitoringFrequency();
}
return monitoringFrequency;
}
} }

@ -311,7 +311,6 @@ public class PolicyDAOTestCase extends BasePolicyManagementDAOTest {
policy = pap.addPolicy(policy); policy = pap.addPolicy(policy);
pap.activatePolicy(policy.getId()); pap.activatePolicy(policy.getId());
List<String> users = new ArrayList<>(); List<String> users = new ArrayList<>();
log.debug(policy.getRoles().size());
users.add("Udara"); users.add("Udara");
users.add("Dileesha"); users.add("Dileesha");
policy.setUsers(users); policy.setUsers(users);

@ -317,6 +317,10 @@
<groupId>commons-pool.wso2</groupId> <groupId>commons-pool.wso2</groupId>
<artifactId>commons-pool</artifactId> <artifactId>commons-pool</artifactId>
</exclusion> </exclusion>
<exclusion>
<groupId>javax.servlet</groupId>
<artifactId>servlet-api</artifactId>
</exclusion>
</exclusions> </exclusions>
</dependency> </dependency>
<dependency> <dependency>
@ -391,6 +395,10 @@
<groupId>org.wso2.carbon.registry</groupId> <groupId>org.wso2.carbon.registry</groupId>
<artifactId>org.wso2.carbon.registry.extensions</artifactId> <artifactId>org.wso2.carbon.registry.extensions</artifactId>
</exclusion> </exclusion>
<exclusion>
<groupId>javax.servlet</groupId>
<artifactId>servlet-api</artifactId>
</exclusion>
</exclusions> </exclusions>
</dependency> </dependency>
<!-- End of Governance dependencies --> <!-- End of Governance dependencies -->
@ -813,6 +821,10 @@
<groupId>org.wso2.carbon.registry</groupId> <groupId>org.wso2.carbon.registry</groupId>
<artifactId>org.wso2.carbon.registry.ws.client</artifactId> <artifactId>org.wso2.carbon.registry.ws.client</artifactId>
</exclusion> </exclusion>
<exclusion>
<groupId>javax.servlet</groupId>
<artifactId>servlet-api</artifactId>
</exclusion>
</exclusions> </exclusions>
</dependency> </dependency>
<dependency> <dependency>

Loading…
Cancel
Save