@ -20,6 +20,7 @@ package org.wso2.carbon.policy.mgt.core.mgt.impl;
import org.apache.commons.logging.Log ;
import org.apache.commons.logging.Log ;
import org.apache.commons.logging.LogFactory ;
import org.apache.commons.logging.LogFactory ;
import org.wso2.carbon.context.PrivilegedCarbonContext ;
import org.wso2.carbon.device.mgt.common.Device ;
import org.wso2.carbon.device.mgt.common.Device ;
import org.wso2.carbon.device.mgt.common.DeviceIdentifier ;
import org.wso2.carbon.device.mgt.common.DeviceIdentifier ;
import org.wso2.carbon.device.mgt.common.DeviceManagementException ;
import org.wso2.carbon.device.mgt.common.DeviceManagementException ;
@ -92,19 +93,21 @@ public class MonitoringManagerImpl implements MonitoringManager {
PolicyManagementDAOFactory . openConnection ( ) ;
PolicyManagementDAOFactory . openConnection ( ) ;
NonComplianceData cmd = monitoringDAO . getCompliance ( device . getId ( ) , device . getEnrolmentInfo ( ) . getId ( ) ) ;
NonComplianceData cmd = monitoringDAO . getCompliance ( device . getId ( ) , device . getEnrolmentInfo ( ) . getId ( ) ) ;
complianceData = monitoringService . checkPolicyCompliance ( deviceIdentifier ,
complianceData = monitoringService . checkPolicyCompliance ( deviceIdentifier ,
policy , deviceResponse ) ;
policy , deviceResponse ) ;
if ( cmd ! = null ) {
complianceData . setId ( cmd . getId ( ) ) ;
complianceData . setPolicy ( policy ) ;
complianceFeatures = complianceData . getComplianceFeatures ( ) ;
complianceData . setDeviceId ( device . getId ( ) ) ;
complianceData . setPolicyId ( policy . getId ( ) ) ;
}
complianceData . setId ( cmd . getId ( ) ) ;
complianceData . setPolicy ( policy ) ;
complianceFeatures = complianceData . getComplianceFeatures ( ) ;
complianceData . setDeviceId ( device . getId ( ) ) ;
complianceData . setPolicyId ( policy . getId ( ) ) ;
} catch ( SQLException e ) {
} catch ( SQLException e ) {
throw new PolicyComplianceException ( "Error occurred while opening a data source connection" , e ) ;
throw new PolicyComplianceException ( "Error occurred while opening a data source connection" , e ) ;
} catch ( MonitoringDAOException e ) {
} catch ( MonitoringDAOException e ) {
throw new PolicyComplianceException (
throw new PolicyComplianceException (
"Unable to add the none compliance features to database for device " +
"Unable to add the none compliance features to database for device " +
deviceIdentifier . getId ( ) + " - " + deviceIdentifier . getType ( ) , e ) ;
deviceIdentifier . getId ( ) + " - " + deviceIdentifier . getType ( ) , e ) ;
} finally {
} finally {
PolicyManagementDAOFactory . closeConnection ( ) ;
PolicyManagementDAOFactory . closeConnection ( ) ;
}
}
@ -115,7 +118,7 @@ public class MonitoringManagerImpl implements MonitoringManager {
try {
try {
PolicyManagementDAOFactory . beginTransaction ( ) ;
PolicyManagementDAOFactory . beginTransaction ( ) ;
monitoringDAO . setDeviceAsNoneCompliance ( device . getId ( ) , device . getEnrolmentInfo ( ) . getId ( ) ,
monitoringDAO . setDeviceAsNoneCompliance ( device . getId ( ) , device . getEnrolmentInfo ( ) . getId ( ) ,
policy . getId ( ) ) ;
policy . getId ( ) ) ;
if ( log . isDebugEnabled ( ) ) {
if ( log . isDebugEnabled ( ) ) {
log . debug ( "Compliance status primary key " + complianceData . getId ( ) ) ;
log . debug ( "Compliance status primary key " + complianceData . getId ( ) ) ;
}
}
@ -181,19 +184,19 @@ public class MonitoringManagerImpl implements MonitoringManager {
device = service . getDevice ( deviceIdentifier , false ) ;
device = service . getDevice ( deviceIdentifier , false ) ;
} catch ( DeviceManagementException e ) {
} catch ( DeviceManagementException e ) {
throw new PolicyComplianceException ( "Unable to retrieve device data for " + deviceIdentifier . getId ( ) +
throw new PolicyComplianceException ( "Unable to retrieve device data for " + deviceIdentifier . getId ( ) +
" - " + deviceIdentifier . getType ( ) , e ) ;
" - " + deviceIdentifier . getType ( ) , e ) ;
}
}
try {
try {
PolicyManagementDAOFactory . openConnection ( ) ;
PolicyManagementDAOFactory . openConnection ( ) ;
NonComplianceData complianceData = monitoringDAO . getCompliance ( device . getId ( ) , device . getEnrolmentInfo ( )
NonComplianceData complianceData = monitoringDAO . getCompliance ( device . getId ( ) , device . getEnrolmentInfo ( )
. getId ( ) ) ;
. getId ( ) ) ;
if ( complianceData == null | | ! complianceData . isStatus ( ) ) {
if ( complianceData != null & & ! complianceData . isStatus ( ) ) {
return false ;
return false ;
}
}
} catch ( MonitoringDAOException e ) {
} catch ( MonitoringDAOException e ) {
throw new PolicyComplianceException ( "Unable to retrieve compliance status for " + deviceIdentifier . getId ( ) +
throw new PolicyComplianceException ( "Unable to retrieve compliance status for " + deviceIdentifier . getId ( ) +
" - " + deviceIdentifier . getType ( ) , e ) ;
" - " + deviceIdentifier . getType ( ) , e ) ;
} catch ( SQLException e ) {
} catch ( SQLException e ) {
throw new PolicyComplianceException ( "Error occurred while opening a connection to the data source" , e ) ;
throw new PolicyComplianceException ( "Error occurred while opening a connection to the data source" , e ) ;
} finally {
} finally {
@ -204,7 +207,7 @@ public class MonitoringManagerImpl implements MonitoringManager {
@Override
@Override
public NonComplianceData getDevicePolicyCompliance ( DeviceIdentifier deviceIdentifier ) throws
public NonComplianceData getDevicePolicyCompliance ( DeviceIdentifier deviceIdentifier ) throws
PolicyComplianceException {
PolicyComplianceException {
NonComplianceData complianceData ;
NonComplianceData complianceData ;
try {
try {
PolicyManagementDAOFactory . openConnection ( ) ;
PolicyManagementDAOFactory . openConnection ( ) ;
@ -218,11 +221,11 @@ public class MonitoringManagerImpl implements MonitoringManager {
} catch ( DeviceManagementException e ) {
} catch ( DeviceManagementException e ) {
throw new PolicyComplianceException ( "Unable to retrieve device data for " + deviceIdentifier . getId ( ) +
throw new PolicyComplianceException ( "Unable to retrieve device data for " + deviceIdentifier . getId ( ) +
" - " + deviceIdentifier . getType ( ) , e ) ;
" - " + deviceIdentifier . getType ( ) , e ) ;
} catch ( MonitoringDAOException e ) {
} catch ( MonitoringDAOException e ) {
throw new PolicyComplianceException ( "Unable to retrieve compliance data for " + deviceIdentifier . getId ( ) +
throw new PolicyComplianceException ( "Unable to retrieve compliance data for " + deviceIdentifier . getId ( ) +
" - " + deviceIdentifier . getType ( ) , e ) ;
" - " + deviceIdentifier . getType ( ) , e ) ;
} catch ( SQLException e ) {
} catch ( SQLException e ) {
throw new PolicyComplianceException ( "Error occurred while opening a connection to the data source" , e ) ;
throw new PolicyComplianceException ( "Error occurred while opening a connection to the data source" , e ) ;
} finally {
} finally {
@ -288,10 +291,10 @@ public class MonitoringManagerImpl implements MonitoringManager {
if ( complianceData . getAttempts ( ) = = 0 ) {
if ( complianceData . getAttempts ( ) = = 0 ) {
deviceIdsToAddOperation . put ( complianceData . getDeviceId ( ) ,
deviceIdsToAddOperation . put ( complianceData . getDeviceId ( ) ,
deviceIds . get ( complianceData . getDeviceId ( ) ) ) ;
deviceIds . get ( complianceData . getDeviceId ( ) ) ) ;
} else {
} else {
deviceIdsWithExistingOperation . put ( complianceData . getDeviceId ( ) ,
deviceIdsWithExistingOperation . put ( complianceData . getDeviceId ( ) ,
deviceIds . get ( complianceData . getDeviceId ( ) ) ) ;
deviceIds . get ( complianceData . getDeviceId ( ) ) ) ;
}
}
}
}
}
}
@ -315,7 +318,7 @@ public class MonitoringManagerImpl implements MonitoringManager {
log . debug ( "These devices are in the system for the first time" ) ;
log . debug ( "These devices are in the system for the first time" ) ;
for ( PolicyDeviceWrapper wrapper : firstTimeDevices ) {
for ( PolicyDeviceWrapper wrapper : firstTimeDevices ) {
log . debug ( "First time device primary key : " + wrapper . getDeviceId ( ) + " & policy id " +
log . debug ( "First time device primary key : " + wrapper . getDeviceId ( ) + " & policy id " +
wrapper . getPolicyId ( ) ) ;
wrapper . getPolicyId ( ) ) ;
}
}
}
}
@ -358,11 +361,11 @@ public class MonitoringManagerImpl implements MonitoringManager {
List < String > deviceTypes = new ArrayList < > ( ) ;
List < String > deviceTypes = new ArrayList < > ( ) ;
try {
try {
//when shutdown, it sets DeviceManagementService to null, therefore need to have a null check
//when shutdown, it sets DeviceManagementService to null, therefore need to have a null check
if ( PolicyManagementDataHolder . getInstance ( ) . getDeviceManagementService ( ) ! = null ) {
if ( PolicyManagementDataHolder . getInstance ( ) . getDeviceManagementService ( ) ! = null ) {
deviceTypes =
deviceTypes =
PolicyManagementDataHolder . getInstance ( ) . getDeviceManagementService ( ) . get Avail ableDeviceTypes( ) ;
PolicyManagementDataHolder . getInstance ( ) . getDeviceManagementService ( ) . get PolicyMonitoringEn ableDeviceTypes( ) ;
}
}
} catch ( DeviceManagementException e ) {
} catch ( DeviceManagementException e ) {
throw new PolicyComplianceException ( "Error occurred while getting the device types." , e ) ;
throw new PolicyComplianceException ( "Error occurred while getting the device types." , e ) ;
}
}