Fix incorrect instantiations of Provider Services

revert-70ac1926
Charitha Goonetilleke 5 years ago
parent 30fb93d511
commit eeacb455d5

@ -95,7 +95,7 @@ public class FCMNotificationStrategy implements NotificationStrategy {
OutputStream os = null; OutputStream os = null;
byte[] bytes = getFCMRequest(message, getFCMToken(device.getProperties())).getBytes(); byte[] bytes = getFCMRequest(message, getFCMToken(device.getProperties())).getBytes();
HttpURLConnection conn; HttpURLConnection conn = null;
try { try {
conn = (HttpURLConnection) new URL(FCM_ENDPOINT).openConnection(); conn = (HttpURLConnection) new URL(FCM_ENDPOINT).openConnection();
conn.setRequestProperty("Content-Type", "application/json"); conn.setRequestProperty("Content-Type", "application/json");
@ -108,6 +108,9 @@ public class FCMNotificationStrategy implements NotificationStrategy {
if (os != null) { if (os != null) {
os.close(); os.close();
} }
if (conn != null) {
conn.disconnect();
}
} }
int status = conn.getResponseCode(); int status = conn.getResponseCode();
if (log.isDebugEnabled()) { if (log.isDebugEnabled()) {

@ -270,8 +270,7 @@ public class DeviceManagementPluginRepository implements DeviceManagerStartupLis
return tenantProviders; return tenantProviders;
} }
private void registerPushNotificationStrategy(DeviceManagementService deviceManagementService) private void registerPushNotificationStrategy(DeviceManagementService deviceManagementService) {
throws DeviceManagementException {
PrivilegedCarbonContext.startTenantFlow(); PrivilegedCarbonContext.startTenantFlow();
PrivilegedCarbonContext.getThreadLocalCarbonContext().setTenantDomain( PrivilegedCarbonContext.getThreadLocalCarbonContext().setTenantDomain(
deviceManagementService.getProvisioningConfig().getProviderTenantDomain(), true); deviceManagementService.getProvisioningConfig().getProviderTenantDomain(), true);

@ -66,7 +66,7 @@ import java.util.Map;
public class DeviceInformationManagerImpl implements DeviceInformationManager { public class DeviceInformationManagerImpl implements DeviceInformationManager {
private final DeviceDetailsDAO deviceDetailsDAO; private final DeviceDetailsDAO deviceDetailsDAO;
private DeviceDAO deviceDAO; private final DeviceDAO deviceDAO;
private static final Log log = LogFactory.getLog(DeviceInformationManagerImpl.class); private static final Log log = LogFactory.getLog(DeviceInformationManagerImpl.class);
private static final String LOCATION_EVENT_STREAM_DEFINITION = "org.wso2.iot.LocationStream"; private static final String LOCATION_EVENT_STREAM_DEFINITION = "org.wso2.iot.LocationStream";
private static final String DEVICE_INFO_EVENT_STREAM_DEFINITION = "org.wso2.iot.DeviceInfoStream"; private static final String DEVICE_INFO_EVENT_STREAM_DEFINITION = "org.wso2.iot.DeviceInfoStream";

@ -27,6 +27,7 @@ import org.wso2.carbon.device.mgt.common.operation.mgt.OperationManager;
import org.wso2.carbon.device.mgt.common.spi.DeviceTypeGeneratorService; import org.wso2.carbon.device.mgt.common.spi.DeviceTypeGeneratorService;
import org.wso2.carbon.device.mgt.core.app.mgt.config.AppManagementConfig; import org.wso2.carbon.device.mgt.core.app.mgt.config.AppManagementConfig;
import org.wso2.carbon.device.mgt.core.config.license.LicenseConfig; import org.wso2.carbon.device.mgt.core.config.license.LicenseConfig;
import org.wso2.carbon.device.mgt.core.device.details.mgt.DeviceInformationManager;
import org.wso2.carbon.device.mgt.core.dto.DeviceType; import org.wso2.carbon.device.mgt.core.dto.DeviceType;
import org.wso2.carbon.device.mgt.core.dto.DeviceTypeServiceIdentifier; import org.wso2.carbon.device.mgt.core.dto.DeviceTypeServiceIdentifier;
import org.wso2.carbon.device.mgt.core.privacy.PrivacyComplianceProvider; import org.wso2.carbon.device.mgt.core.privacy.PrivacyComplianceProvider;
@ -48,10 +49,11 @@ import java.util.Map;
public class DeviceManagementDataHolder { public class DeviceManagementDataHolder {
private static DeviceManagementDataHolder thisInstance = new DeviceManagementDataHolder(); private static final DeviceManagementDataHolder thisInstance = new DeviceManagementDataHolder();
private RealmService realmService; private RealmService realmService;
private TenantManager tenantManager; private TenantManager tenantManager;
private DeviceManagementProviderService deviceManagerProvider; private DeviceManagementProviderService deviceManagerProvider;
private DeviceInformationManager deviceInformationManager;
private LicenseManager licenseManager; private LicenseManager licenseManager;
private RegistryService registryService; private RegistryService registryService;
private LicenseConfig licenseConfig; private LicenseConfig licenseConfig;
@ -59,7 +61,7 @@ public class DeviceManagementDataHolder {
private AppManagementConfig appManagerConfig; private AppManagementConfig appManagerConfig;
private OperationManager operationManager; private OperationManager operationManager;
private ConfigurationContextService configurationContextService; private ConfigurationContextService configurationContextService;
private HashMap<String,Boolean> requireDeviceAuthorization = new HashMap<>(); private final HashMap<String,Boolean> requireDeviceAuthorization = new HashMap<>();
private DeviceAccessAuthorizationService deviceAccessAuthorizationService; private DeviceAccessAuthorizationService deviceAccessAuthorizationService;
private GroupManagementProviderService groupManagementProviderService; private GroupManagementProviderService groupManagementProviderService;
private TaskService taskService; private TaskService taskService;
@ -69,10 +71,10 @@ public class DeviceManagementDataHolder {
private DeviceStatusTaskManagerService deviceStatusTaskManagerService; private DeviceStatusTaskManagerService deviceStatusTaskManagerService;
private DeviceTypeGeneratorService deviceTypeGeneratorService; private DeviceTypeGeneratorService deviceTypeGeneratorService;
private PrivacyComplianceProvider privacyComplianceProvider; private PrivacyComplianceProvider privacyComplianceProvider;
private Map<DeviceType, DeviceStatusTaskPluginConfig> deviceStatusTaskPluginConfigs = Collections.synchronizedMap( private final Map<DeviceType, DeviceStatusTaskPluginConfig> deviceStatusTaskPluginConfigs = Collections.synchronizedMap(
new HashMap<DeviceType, DeviceStatusTaskPluginConfig>()); new HashMap<>());
private Map<String, OperationMonitoringTaskConfig> map = new HashMap<>(); private final Map<String, OperationMonitoringTaskConfig> map = new HashMap<>();
public Map<String, OperationMonitoringTaskConfig> getMap(){ public Map<String, OperationMonitoringTaskConfig> getMap(){
return this.map; return this.map;
@ -276,4 +278,12 @@ public class DeviceManagementDataHolder {
public void setPrivacyComplianceProvider(PrivacyComplianceProvider privacyComplianceProvider) { public void setPrivacyComplianceProvider(PrivacyComplianceProvider privacyComplianceProvider) {
this.privacyComplianceProvider = privacyComplianceProvider; this.privacyComplianceProvider = privacyComplianceProvider;
} }
public DeviceInformationManager getDeviceInformationManager() {
return deviceInformationManager;
}
public void setDeviceInformationManager(DeviceInformationManager deviceInformationManager) {
this.deviceInformationManager = deviceInformationManager;
}
} }

@ -133,10 +133,10 @@ import java.util.concurrent.TimeUnit;
public class DeviceManagementServiceComponent { public class DeviceManagementServiceComponent {
private static final Object LOCK = new Object(); private static final Object LOCK = new Object();
private static Log log = LogFactory.getLog(DeviceManagementServiceComponent.class); private static final Log log = LogFactory.getLog(DeviceManagementServiceComponent.class);
private static List<PluginInitializationListener> listeners = new ArrayList<>(); private static final List<PluginInitializationListener> listeners = new ArrayList<>();
private static List<DeviceManagementService> deviceManagers = new ArrayList<>(); private static final List<DeviceManagementService> deviceManagers = new ArrayList<>();
private static List<DeviceManagerStartupListener> startupListeners = new ArrayList<>(); private static final List<DeviceManagerStartupListener> startupListeners = new ArrayList<>();
public static void registerPluginInitializationListener(PluginInitializationListener listener) { public static void registerPluginInitializationListener(PluginInitializationListener listener) {
synchronized (LOCK) { synchronized (LOCK) {
@ -340,7 +340,10 @@ public class DeviceManagementServiceComponent {
PermissionManagerService permissionManagerService = PermissionManagerServiceImpl.getInstance(); PermissionManagerService permissionManagerService = PermissionManagerServiceImpl.getInstance();
bundleContext.registerService(PermissionManagerService.class.getName(), permissionManagerService, null); bundleContext.registerService(PermissionManagerService.class.getName(), permissionManagerService, null);
bundleContext.registerService(DeviceInformationManager.class, new DeviceInformationManagerImpl(), null); DeviceInformationManager deviceInformationManager = new DeviceInformationManagerImpl();
bundleContext.registerService(DeviceInformationManager.class, deviceInformationManager, null);
DeviceManagementDataHolder.getInstance().setDeviceInformationManager(deviceInformationManager);
bundleContext.registerService(SearchManagerService.class, new SearchManagerServiceImpl(), null); bundleContext.registerService(SearchManagerService.class, new SearchManagerServiceImpl(), null);
} }

@ -63,7 +63,6 @@ import org.wso2.carbon.device.mgt.common.device.details.DeviceLocationHistorySna
import org.wso2.carbon.device.mgt.common.enrollment.notification.EnrollmentNotificationConfiguration; import org.wso2.carbon.device.mgt.common.enrollment.notification.EnrollmentNotificationConfiguration;
import org.wso2.carbon.device.mgt.common.enrollment.notification.EnrollmentNotifier; import org.wso2.carbon.device.mgt.common.enrollment.notification.EnrollmentNotifier;
import org.wso2.carbon.device.mgt.common.enrollment.notification.EnrollmentNotifierException; import org.wso2.carbon.device.mgt.common.enrollment.notification.EnrollmentNotifierException;
import org.wso2.carbon.device.mgt.common.exceptions.BadRequestException;
import org.wso2.carbon.device.mgt.common.exceptions.DeviceManagementException; import org.wso2.carbon.device.mgt.common.exceptions.DeviceManagementException;
import org.wso2.carbon.device.mgt.common.exceptions.DeviceNotFoundException; import org.wso2.carbon.device.mgt.common.exceptions.DeviceNotFoundException;
import org.wso2.carbon.device.mgt.common.exceptions.DeviceTypeNotFoundException; import org.wso2.carbon.device.mgt.common.exceptions.DeviceTypeNotFoundException;
@ -103,7 +102,6 @@ import org.wso2.carbon.device.mgt.core.dao.EnrollmentDAO;
import org.wso2.carbon.device.mgt.core.dao.util.DeviceManagementDAOUtil; import org.wso2.carbon.device.mgt.core.dao.util.DeviceManagementDAOUtil;
import org.wso2.carbon.device.mgt.core.device.details.mgt.DeviceDetailsMgtException; import org.wso2.carbon.device.mgt.core.device.details.mgt.DeviceDetailsMgtException;
import org.wso2.carbon.device.mgt.core.device.details.mgt.DeviceInformationManager; import org.wso2.carbon.device.mgt.core.device.details.mgt.DeviceInformationManager;
import org.wso2.carbon.device.mgt.core.device.details.mgt.impl.DeviceInformationManagerImpl;
import org.wso2.carbon.device.mgt.core.dto.DeviceType; import org.wso2.carbon.device.mgt.core.dto.DeviceType;
import org.wso2.carbon.device.mgt.core.dto.DeviceTypeServiceIdentifier; import org.wso2.carbon.device.mgt.core.dto.DeviceTypeServiceIdentifier;
import org.wso2.carbon.device.mgt.core.dto.DeviceTypeVersion; import org.wso2.carbon.device.mgt.core.dto.DeviceTypeVersion;
@ -149,7 +147,6 @@ public class DeviceManagementProviderServiceImpl implements DeviceManagementProv
private static final String OPERATION_RESPONSE_EVENT_STREAM_DEFINITION = "org.wso2.iot.OperationResponseStream"; private static final String OPERATION_RESPONSE_EVENT_STREAM_DEFINITION = "org.wso2.iot.OperationResponseStream";
private final DeviceManagementPluginRepository pluginRepository; private final DeviceManagementPluginRepository pluginRepository;
private final DeviceInformationManager deviceInformationManager;
private final DeviceDAO deviceDAO; private final DeviceDAO deviceDAO;
private final DeviceTypeDAO deviceTypeDAO; private final DeviceTypeDAO deviceTypeDAO;
private final EnrollmentDAO enrollmentDAO; private final EnrollmentDAO enrollmentDAO;
@ -157,8 +154,6 @@ public class DeviceManagementProviderServiceImpl implements DeviceManagementProv
public DeviceManagementProviderServiceImpl() { public DeviceManagementProviderServiceImpl() {
this.pluginRepository = new DeviceManagementPluginRepository(); this.pluginRepository = new DeviceManagementPluginRepository();
this.deviceInformationManager = new DeviceInformationManagerImpl();
this.deviceDAO = DeviceManagementDAOFactory.getDeviceDAO(); this.deviceDAO = DeviceManagementDAOFactory.getDeviceDAO();
this.applicationDAO = DeviceManagementDAOFactory.getApplicationDAO(); this.applicationDAO = DeviceManagementDAOFactory.getApplicationDAO();
this.deviceTypeDAO = DeviceManagementDAOFactory.getDeviceTypeDAO(); this.deviceTypeDAO = DeviceManagementDAOFactory.getDeviceTypeDAO();
@ -399,6 +394,8 @@ public class DeviceManagementProviderServiceImpl implements DeviceManagementProv
extractDeviceLocationToUpdate(device); extractDeviceLocationToUpdate(device);
try { try {
if (device.getDeviceInfo() != null) { if (device.getDeviceInfo() != null) {
DeviceInformationManager deviceInformationManager = DeviceManagementDataHolder
.getInstance().getDeviceInformationManager();
deviceInformationManager.addDeviceInfo(device, device.getDeviceInfo()); deviceInformationManager.addDeviceInfo(device, device.getDeviceInfo());
} }
} catch (DeviceDetailsMgtException e) { } catch (DeviceDetailsMgtException e) {
@ -2838,7 +2835,8 @@ public class DeviceManagementProviderServiceImpl implements DeviceManagementProv
if (log.isDebugEnabled()) { if (log.isDebugEnabled()) {
log.debug("Add device:" + deviceIdentifier.getId() + " to default group"); log.debug("Add device:" + deviceIdentifier.getId() + " to default group");
} }
GroupManagementProviderService groupManagementProviderService = new GroupManagementProviderServiceImpl(); GroupManagementProviderService groupManagementProviderService = DeviceManagementDataHolder
.getInstance().getGroupManagementProviderService();
try { try {
DeviceGroup defaultGroup = createDefaultGroup(groupManagementProviderService, ownership.toString()); DeviceGroup defaultGroup = createDefaultGroup(groupManagementProviderService, ownership.toString());
if (defaultGroup != null) { if (defaultGroup != null) {
@ -3103,6 +3101,8 @@ public class DeviceManagementProviderServiceImpl implements DeviceManagementProv
} }
DeviceInfo info; DeviceInfo info;
try { try {
DeviceInformationManager deviceInformationManager = DeviceManagementDataHolder
.getInstance().getDeviceInformationManager();
info = deviceInformationManager.getDeviceInfo(device); info = deviceInformationManager.getDeviceInfo(device);
} catch (DeviceDetailsMgtException e) { } catch (DeviceDetailsMgtException e) {
String msg = "Error occurred while retrieving advance info of '" + device.getType() + String msg = "Error occurred while retrieving advance info of '" + device.getType() +
@ -3402,6 +3402,8 @@ public class DeviceManagementProviderServiceImpl implements DeviceManagementProv
* @param device Device object * @param device Device object
*/ */
private void extractDeviceLocationToUpdate(Device device) { private void extractDeviceLocationToUpdate(Device device) {
DeviceInformationManager deviceInformationManager = DeviceManagementDataHolder
.getInstance().getDeviceInformationManager();
List<Device.Property> properties = device.getProperties(); List<Device.Property> properties = device.getProperties();
if (properties != null) { if (properties != null) {
String latitude = null; String latitude = null;

@ -55,10 +55,10 @@ import java.util.Map;
public class GroupManagementProviderServiceImpl implements GroupManagementProviderService { public class GroupManagementProviderServiceImpl implements GroupManagementProviderService {
private static Log log = LogFactory.getLog(GroupManagementProviderServiceImpl.class); private static final Log log = LogFactory.getLog(GroupManagementProviderServiceImpl.class);
private GroupDAO groupDAO; private final GroupDAO groupDAO;
private DeviceDAO deviceDAO; private final DeviceDAO deviceDAO;
/** /**
* Set groupDAO from GroupManagementDAOFactory when class instantiate. * Set groupDAO from GroupManagementDAOFactory when class instantiate.
@ -912,9 +912,21 @@ public class GroupManagementProviderServiceImpl implements GroupManagementProvid
log.debug("Get groups of device " + deviceIdentifier.getId()); log.debug("Get groups of device " + deviceIdentifier.getId());
} }
int tenantId = PrivilegedCarbonContext.getThreadLocalCarbonContext().getTenantId(); int tenantId = PrivilegedCarbonContext.getThreadLocalCarbonContext().getTenantId();
DeviceManagementProviderService managementProviderService = new DeviceManagementProviderServiceImpl(); DeviceManagementProviderService managementProviderService = DeviceManagementDataHolder
.getInstance().getDeviceManagementProvider();
Device device;
try {
device = managementProviderService.getDevice(deviceIdentifier, false);
} catch (DeviceManagementException e) {
String msg = "Error occurred while retrieving device groups.";
log.error(msg, e);
throw new GroupManagementException(msg, e);
}
return getDeviceGroups(requireGroupProps, tenantId, device);
}
private List<DeviceGroup> getDeviceGroups(boolean requireGroupProps, int tenantId, Device device) throws GroupManagementException {
try { try {
Device device = managementProviderService.getDevice(deviceIdentifier, false);
GroupManagementDAOFactory.openConnection(); GroupManagementDAOFactory.openConnection();
List<DeviceGroup> deviceGroups = groupDAO.getGroups(device.getId(), tenantId); List<DeviceGroup> deviceGroups = groupDAO.getGroups(device.getId(), tenantId);
if (requireGroupProps) { if (requireGroupProps) {
@ -925,7 +937,7 @@ public class GroupManagementProviderServiceImpl implements GroupManagementProvid
} }
} }
return deviceGroups; return deviceGroups;
} catch (DeviceManagementException | GroupManagementDAOException | SQLException e) { } catch (GroupManagementDAOException | SQLException e) {
String msg = "Error occurred while retrieving device groups."; String msg = "Error occurred while retrieving device groups.";
log.error(msg, e); log.error(msg, e);
throw new GroupManagementException(msg, e); throw new GroupManagementException(msg, e);
@ -950,28 +962,7 @@ public class GroupManagementProviderServiceImpl implements GroupManagementProvid
log.debug("Get groups of device " + device.getDeviceIdentifier()); log.debug("Get groups of device " + device.getDeviceIdentifier());
} }
int tenantId = PrivilegedCarbonContext.getThreadLocalCarbonContext().getTenantId(); int tenantId = PrivilegedCarbonContext.getThreadLocalCarbonContext().getTenantId();
try { return getDeviceGroups(requireGroupProps, tenantId, device);
GroupManagementDAOFactory.openConnection();
List<DeviceGroup> deviceGroups = groupDAO.getGroups(device.getId(), tenantId);
if (requireGroupProps) {
if (deviceGroups != null && !deviceGroups.isEmpty()) {
for (DeviceGroup group : deviceGroups) {
populateGroupProperties(group, tenantId);
}
}
}
return deviceGroups;
} catch (GroupManagementDAOException | SQLException e) {
String msg = "Error occurred while retrieving device groups.";
log.error(msg, e);
throw new GroupManagementException(msg, e);
} catch (Exception e) {
String msg = "Error occurred in getGroups";
log.error(msg, e);
throw new GroupManagementException(msg, e);
} finally {
GroupManagementDAOFactory.closeConnection();
}
} }
/** /**

@ -77,6 +77,7 @@ import org.wso2.carbon.device.mgt.core.config.DeviceConfigurationManager;
import org.wso2.carbon.device.mgt.core.dao.DeviceManagementDAOFactory; import org.wso2.carbon.device.mgt.core.dao.DeviceManagementDAOFactory;
import org.wso2.carbon.device.mgt.core.device.details.mgt.dao.DeviceDetailsDAO; import org.wso2.carbon.device.mgt.core.device.details.mgt.dao.DeviceDetailsDAO;
import org.wso2.carbon.device.mgt.core.device.details.mgt.dao.DeviceDetailsMgtDAOException; import org.wso2.carbon.device.mgt.core.device.details.mgt.dao.DeviceDetailsMgtDAOException;
import org.wso2.carbon.device.mgt.core.device.details.mgt.impl.DeviceInformationManagerImpl;
import org.wso2.carbon.device.mgt.core.dto.DeviceType; import org.wso2.carbon.device.mgt.core.dto.DeviceType;
import org.wso2.carbon.device.mgt.core.internal.DeviceManagementDataHolder; import org.wso2.carbon.device.mgt.core.internal.DeviceManagementDataHolder;
import org.wso2.carbon.device.mgt.core.internal.DeviceManagementServiceComponent; import org.wso2.carbon.device.mgt.core.internal.DeviceManagementServiceComponent;
@ -124,6 +125,7 @@ public class DeviceManagementProviderServiceTest extends BaseDeviceManagementTes
DeviceManagementDataHolder.getInstance().setRegistryService(getRegistryService()); DeviceManagementDataHolder.getInstance().setRegistryService(getRegistryService());
DeviceManagementDataHolder.getInstance().setDeviceAccessAuthorizationService(new DeviceAccessAuthorizationServiceImpl()); DeviceManagementDataHolder.getInstance().setDeviceAccessAuthorizationService(new DeviceAccessAuthorizationServiceImpl());
DeviceManagementDataHolder.getInstance().setGroupManagementProviderService(new GroupManagementProviderServiceImpl()); DeviceManagementDataHolder.getInstance().setGroupManagementProviderService(new GroupManagementProviderServiceImpl());
DeviceManagementDataHolder.getInstance().setDeviceInformationManager(new DeviceInformationManagerImpl());
DeviceManagementDataHolder.getInstance().setDeviceTaskManagerService(null); DeviceManagementDataHolder.getInstance().setDeviceTaskManagerService(null);
deviceMgtService.registerDeviceType(new TestDeviceManagementService(DEVICE_TYPE, deviceMgtService.registerDeviceType(new TestDeviceManagementService(DEVICE_TYPE,
MultitenantConstants.SUPER_TENANT_DOMAIN_NAME)); MultitenantConstants.SUPER_TENANT_DOMAIN_NAME));

@ -69,9 +69,9 @@ public class PolicyManagerServiceImpl implements PolicyManagerService {
private static final Log log = LogFactory.getLog(PolicyManagerServiceImpl.class); private static final Log log = LogFactory.getLog(PolicyManagerServiceImpl.class);
PolicyAdministratorPoint policyAdministratorPoint; private final PolicyAdministratorPoint policyAdministratorPoint;
MonitoringManager monitoringManager; private final MonitoringManager monitoringManager;
private PolicyManager policyManager; private final PolicyManager policyManager;
public PolicyManagerServiceImpl() { public PolicyManagerServiceImpl() {
policyAdministratorPoint = new PolicyAdministratorPointImpl(); policyAdministratorPoint = new PolicyAdministratorPointImpl();

@ -41,15 +41,14 @@ import org.wso2.carbon.device.mgt.common.DeviceIdentifier;
import org.wso2.carbon.device.mgt.common.exceptions.InvalidDeviceException; import org.wso2.carbon.device.mgt.common.exceptions.InvalidDeviceException;
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.common.operation.mgt.OperationManagementException; import org.wso2.carbon.device.mgt.common.operation.mgt.OperationManagementException;
import org.wso2.carbon.device.mgt.common.policy.mgt.Policy;
import org.wso2.carbon.device.mgt.core.operation.mgt.CommandOperation; import org.wso2.carbon.device.mgt.core.operation.mgt.CommandOperation;
import org.wso2.carbon.device.mgt.core.operation.mgt.OperationMgtConstants; import org.wso2.carbon.device.mgt.core.operation.mgt.OperationMgtConstants;
import org.wso2.carbon.device.mgt.common.policy.mgt.Policy;
import org.wso2.carbon.policy.mgt.common.PolicyAdministratorPoint; import org.wso2.carbon.policy.mgt.common.PolicyAdministratorPoint;
import org.wso2.carbon.policy.mgt.common.PolicyEvaluationException; import org.wso2.carbon.policy.mgt.common.PolicyEvaluationException;
import org.wso2.carbon.policy.mgt.common.PolicyManagementException; import org.wso2.carbon.policy.mgt.common.PolicyManagementException;
import org.wso2.carbon.policy.mgt.common.PolicyTransformException; import org.wso2.carbon.policy.mgt.common.PolicyTransformException;
import org.wso2.carbon.policy.mgt.core.PolicyManagerService; import org.wso2.carbon.policy.mgt.core.PolicyManagerService;
import org.wso2.carbon.policy.mgt.core.PolicyManagerServiceImpl;
import org.wso2.carbon.policy.mgt.core.internal.PolicyManagementDataHolder; import org.wso2.carbon.policy.mgt.core.internal.PolicyManagementDataHolder;
import org.wso2.carbon.policy.mgt.core.util.PolicyManagerUtil; import org.wso2.carbon.policy.mgt.core.util.PolicyManagerUtil;
@ -60,8 +59,8 @@ public class PolicyEnforcementDelegatorImpl implements PolicyEnforcementDelegato
private static final Log log = LogFactory.getLog(PolicyEnforcementDelegatorImpl.class); private static final Log log = LogFactory.getLog(PolicyEnforcementDelegatorImpl.class);
private List<Device> devices; private final List<Device> devices;
private List<Integer> updatedPolicyIds; private final List<Integer> updatedPolicyIds;
public PolicyEnforcementDelegatorImpl(List<Device> devices, List<Integer> updatedPolicyIds) { public PolicyEnforcementDelegatorImpl(List<Device> devices, List<Integer> updatedPolicyIds) {
@ -75,7 +74,6 @@ public class PolicyEnforcementDelegatorImpl implements PolicyEnforcementDelegato
} }
this.devices = devices; this.devices = devices;
this.updatedPolicyIds = updatedPolicyIds; this.updatedPolicyIds = updatedPolicyIds;
} }
@Override @Override
@ -111,7 +109,8 @@ public class PolicyEnforcementDelegatorImpl implements PolicyEnforcementDelegato
@Override @Override
public Policy getEffectivePolicy(DeviceIdentifier identifier) throws PolicyDelegationException { public Policy getEffectivePolicy(DeviceIdentifier identifier) throws PolicyDelegationException {
try { try {
PolicyManagerService policyManagerService = new PolicyManagerServiceImpl(); PolicyManagerService policyManagerService = PolicyManagementDataHolder.getInstance()
.getPolicyManagerService();
PolicyAdministratorPoint policyAdministratorPoint; PolicyAdministratorPoint policyAdministratorPoint;
Policy policy = policyManagerService.getPEP().getEffectivePolicy(identifier); Policy policy = policyManagerService.getPEP().getEffectivePolicy(identifier);
@ -124,11 +123,7 @@ public class PolicyEnforcementDelegatorImpl implements PolicyEnforcementDelegato
} }
return policy; return policy;
//return PolicyManagementDataHolder.getInstance().getPolicyEvaluationPoint().getEffectivePolicy(identifier); //return PolicyManagementDataHolder.getInstance().getPolicyEvaluationPoint().getEffectivePolicy(identifier);
} catch (PolicyEvaluationException e) { } catch (PolicyEvaluationException | PolicyManagementException e) {
String msg = "Error occurred while retrieving the effective policy for devices.";
log.error(msg, e);
throw new PolicyDelegationException(msg, e);
} catch (PolicyManagementException e) {
String msg = "Error occurred while retrieving the effective policy for devices."; String msg = "Error occurred while retrieving the effective policy for devices.";
log.error(msg, e); log.error(msg, e);
throw new PolicyDelegationException(msg, e); throw new PolicyDelegationException(msg, e);
@ -199,7 +194,8 @@ public class PolicyEnforcementDelegatorImpl implements PolicyEnforcementDelegato
*/ */
public Policy getAppliedPolicyToDevice(Device device) throws PolicyDelegationException { public Policy getAppliedPolicyToDevice(Device device) throws PolicyDelegationException {
try { try {
PolicyManagerService policyManagerService = new PolicyManagerServiceImpl(); PolicyManagerService policyManagerService = PolicyManagementDataHolder.getInstance()
.getPolicyManagerService();
return policyManagerService.getAppliedPolicyToDevice(device); return policyManagerService.getAppliedPolicyToDevice(device);
} catch (PolicyManagementException e) { } catch (PolicyManagementException e) {
String msg = "Error occurred while retrieving the applied policy for devices."; String msg = "Error occurred while retrieving the applied policy for devices.";

@ -40,17 +40,19 @@ import org.apache.commons.logging.LogFactory;
import org.wso2.carbon.context.CarbonContext; import org.wso2.carbon.context.CarbonContext;
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.Feature;
import org.wso2.carbon.device.mgt.common.exceptions.DeviceManagementException; import org.wso2.carbon.device.mgt.common.exceptions.DeviceManagementException;
import org.wso2.carbon.device.mgt.common.group.mgt.DeviceGroup; import org.wso2.carbon.device.mgt.common.group.mgt.DeviceGroup;
import org.wso2.carbon.device.mgt.common.group.mgt.GroupManagementException; import org.wso2.carbon.device.mgt.common.group.mgt.GroupManagementException;
import org.wso2.carbon.device.mgt.common.policy.mgt.Policy; import org.wso2.carbon.device.mgt.common.policy.mgt.Policy;
import org.wso2.carbon.device.mgt.core.dto.DeviceType; import org.wso2.carbon.device.mgt.core.dto.DeviceType;
import org.wso2.carbon.device.mgt.core.service.DeviceManagementProviderService; import org.wso2.carbon.device.mgt.core.service.DeviceManagementProviderService;
import org.wso2.carbon.device.mgt.common.Feature;
import org.wso2.carbon.device.mgt.core.service.DeviceManagementProviderServiceImpl;
import org.wso2.carbon.device.mgt.core.service.GroupManagementProviderService; import org.wso2.carbon.device.mgt.core.service.GroupManagementProviderService;
import org.wso2.carbon.device.mgt.core.service.GroupManagementProviderServiceImpl; import org.wso2.carbon.policy.mgt.common.FeatureManagementException;
import org.wso2.carbon.policy.mgt.common.*; import org.wso2.carbon.policy.mgt.common.PIPDevice;
import org.wso2.carbon.policy.mgt.common.PolicyFilter;
import org.wso2.carbon.policy.mgt.common.PolicyInformationPoint;
import org.wso2.carbon.policy.mgt.common.PolicyManagementException;
import org.wso2.carbon.policy.mgt.core.internal.PolicyManagementDataHolder; 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.FeatureManager;
import org.wso2.carbon.policy.mgt.core.mgt.PolicyManager; import org.wso2.carbon.policy.mgt.core.mgt.PolicyManager;
@ -68,9 +70,9 @@ public class PolicyInformationPointImpl implements PolicyInformationPoint {
private static final Log log = LogFactory.getLog(PolicyInformationPointImpl.class); private static final Log log = LogFactory.getLog(PolicyInformationPointImpl.class);
PolicyManager policyManager; private final PolicyManager policyManager;
FeatureManager featureManager; private final FeatureManager featureManager;
DeviceManagementProviderService deviceManagementService; private final DeviceManagementProviderService deviceManagementService;
public PolicyInformationPointImpl() { public PolicyInformationPointImpl() {
deviceManagementService = deviceManagementService =
@ -85,8 +87,8 @@ public class PolicyInformationPointImpl implements PolicyInformationPoint {
Device device; Device device;
DeviceType deviceType = new DeviceType(); DeviceType deviceType = new DeviceType();
deviceType.setName(deviceIdentifier.getType()); deviceType.setName(deviceIdentifier.getType());
DeviceManagementProviderService deviceManagementService = new DeviceManagementProviderServiceImpl(); GroupManagementProviderService groupManagementProviderService = PolicyManagementDataHolder
GroupManagementProviderService groupManagementProviderService = new GroupManagementProviderServiceImpl(); .getInstance().getGroupManagementService();
try { try {
device = deviceManagementService.getDevice(deviceIdentifier, false); device = deviceManagementService.getDevice(deviceIdentifier, false);
@ -184,11 +186,10 @@ public class PolicyInformationPointImpl implements PolicyInformationPoint {
} }
} }
private List<Policy> removeDuplicatePolicies(List<List<Policy>> policies) { private List<Policy> removeDuplicatePolicies(List<List<Policy>> policies) {
Map<Integer, Policy> map = new HashMap<Integer, Policy>(); Map<Integer, Policy> map = new HashMap<>();
List<Policy> finalPolicies = new ArrayList<Policy>(); List<Policy> finalPolicies = new ArrayList<>();
for (List<Policy> policyList : policies) { for (List<Policy> policyList : policies) {
for (Policy policy : policyList) { for (Policy policy : policyList) {
if (!map.containsKey(policy.getId())) { if (!map.containsKey(policy.getId())) {
@ -200,8 +201,4 @@ public class PolicyInformationPointImpl implements PolicyInformationPoint {
return finalPolicies; return finalPolicies;
} }
private DeviceManagementProviderService getDeviceManagementService() {
return new DeviceManagementProviderServiceImpl();
}
} }

@ -18,16 +18,18 @@
package org.wso2.carbon.policy.mgt.core.internal; package org.wso2.carbon.policy.mgt.core.internal;
import org.wso2.carbon.context.PrivilegedCarbonContext;
import org.wso2.carbon.device.mgt.core.config.DeviceConfigurationManager; 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.policy.PolicyConfiguration;
import org.wso2.carbon.device.mgt.core.service.DeviceManagementProviderService; import org.wso2.carbon.device.mgt.core.service.DeviceManagementProviderService;
import org.wso2.carbon.device.mgt.core.service.GroupManagementProviderService;
import org.wso2.carbon.ntask.core.service.TaskService; import org.wso2.carbon.ntask.core.service.TaskService;
import org.wso2.carbon.policy.mgt.common.PolicyEvaluationPoint; import org.wso2.carbon.policy.mgt.common.PolicyEvaluationPoint;
import org.wso2.carbon.policy.mgt.common.PolicyInformationPoint; import org.wso2.carbon.policy.mgt.common.PolicyInformationPoint;
import org.wso2.carbon.policy.mgt.core.PolicyManagerService;
import org.wso2.carbon.policy.mgt.core.mgt.MonitoringManager; import org.wso2.carbon.policy.mgt.core.mgt.MonitoringManager;
import org.wso2.carbon.policy.mgt.core.mgt.PolicyManager; import org.wso2.carbon.policy.mgt.core.mgt.PolicyManager;
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 java.util.HashMap; import java.util.HashMap;
import java.util.Map; import java.util.Map;
@ -35,16 +37,16 @@ import java.util.Map;
public class PolicyManagementDataHolder { public class PolicyManagementDataHolder {
private RealmService realmService; private RealmService realmService;
private TenantManager tenantManager; private final Map<String, PolicyEvaluationPoint> policyEvaluationPoints = new HashMap<>();
private PolicyEvaluationPoint policyEvaluationPoint;
private Map<String, PolicyEvaluationPoint> policyEvaluationPoints = new HashMap<>();
private PolicyInformationPoint policyInformationPoint; private PolicyInformationPoint policyInformationPoint;
private DeviceManagementProviderService deviceManagementService; private DeviceManagementProviderService deviceManagementService;
private GroupManagementProviderService groupManagementService;
private PolicyManagerService policyManagerService;
private MonitoringManager monitoringManager; private MonitoringManager monitoringManager;
private PolicyManager policyManager; private PolicyManager policyManager;
private TaskService taskService; private TaskService taskService;
private static PolicyManagementDataHolder thisInstance = new PolicyManagementDataHolder(); private static final PolicyManagementDataHolder thisInstance = new PolicyManagementDataHolder();
private PolicyManagementDataHolder() {} private PolicyManagementDataHolder() {}
@ -74,18 +76,6 @@ public class PolicyManagementDataHolder {
public void setRealmService(RealmService realmService) { public void setRealmService(RealmService realmService) {
this.realmService = realmService; this.realmService = realmService;
this.setTenantManager(realmService);
}
private void setTenantManager(RealmService realmService) {
if (realmService == null) {
throw new IllegalStateException("Realm service is not initialized properly");
}
this.tenantManager = realmService.getTenantManager();
}
public TenantManager getTenantManager() {
return tenantManager;
} }
public PolicyEvaluationPoint getPolicyEvaluationPoint() { public PolicyEvaluationPoint getPolicyEvaluationPoint() {
@ -127,4 +117,25 @@ public class PolicyManagementDataHolder {
public void setTaskService(TaskService taskService) { public void setTaskService(TaskService taskService) {
this.taskService = taskService; this.taskService = taskService;
} }
public synchronized GroupManagementProviderService getGroupManagementService() {
if (groupManagementService == null) {
PrivilegedCarbonContext ctx = PrivilegedCarbonContext.getThreadLocalCarbonContext();
groupManagementService = (GroupManagementProviderService)
ctx.getOSGiService(GroupManagementProviderService.class, null);
if (groupManagementService == null) {
String msg = "GroupImpl Management service has not initialized.";
throw new IllegalStateException(msg);
}
}
return groupManagementService;
}
public PolicyManagerService getPolicyManagerService() {
return policyManagerService;
}
public void setPolicyManagerService(PolicyManagerService policyManagerService) {
this.policyManagerService = policyManagerService;
}
} }

@ -67,7 +67,7 @@ import org.wso2.carbon.user.core.service.RealmService;
@SuppressWarnings("unused") @SuppressWarnings("unused")
public class PolicyManagementServiceComponent { public class PolicyManagementServiceComponent {
private static Log log = LogFactory.getLog(PolicyManagementServiceComponent.class); private static final Log log = LogFactory.getLog(PolicyManagementServiceComponent.class);
protected void activate(ComponentContext componentContext) { protected void activate(ComponentContext componentContext) {
@ -77,8 +77,10 @@ public class PolicyManagementServiceComponent {
DataSourceConfig dsConfig = config.getPolicyManagementRepository().getDataSourceConfig(); DataSourceConfig dsConfig = config.getPolicyManagementRepository().getDataSourceConfig();
PolicyManagementDAOFactory.init(dsConfig); PolicyManagementDAOFactory.init(dsConfig);
PolicyManagerService policyManagerService = new PolicyManagerServiceImpl();
componentContext.getBundleContext().registerService( componentContext.getBundleContext().registerService(
PolicyManagerService.class.getName(), new PolicyManagerServiceImpl(), null); PolicyManagerService.class.getName(), policyManagerService, null);
PolicyManagementDataHolder.getInstance().setPolicyManagerService(policyManagerService);
PolicyConfiguration policyConfiguration = PolicyConfiguration policyConfiguration =
DeviceConfigurationManager.getInstance().getDeviceManagementConfig().getPolicyConfiguration(); DeviceConfigurationManager.getInstance().getDeviceManagementConfig().getPolicyConfiguration();

@ -56,9 +56,7 @@ import org.wso2.carbon.device.mgt.core.config.policy.PolicyConfiguration;
import org.wso2.carbon.device.mgt.core.operation.mgt.CommandOperation; import org.wso2.carbon.device.mgt.core.operation.mgt.CommandOperation;
import org.wso2.carbon.device.mgt.core.operation.mgt.OperationMgtConstants; import org.wso2.carbon.device.mgt.core.operation.mgt.OperationMgtConstants;
import org.wso2.carbon.device.mgt.core.service.DeviceManagementProviderService; import org.wso2.carbon.device.mgt.core.service.DeviceManagementProviderService;
import org.wso2.carbon.device.mgt.core.service.DeviceManagementProviderServiceImpl;
import org.wso2.carbon.device.mgt.core.service.GroupManagementProviderService; import org.wso2.carbon.device.mgt.core.service.GroupManagementProviderService;
import org.wso2.carbon.device.mgt.core.service.GroupManagementProviderServiceImpl;
import org.wso2.carbon.policy.mgt.common.*; import org.wso2.carbon.policy.mgt.common.*;
import org.wso2.carbon.policy.mgt.core.cache.impl.PolicyCacheManagerImpl; import org.wso2.carbon.policy.mgt.core.cache.impl.PolicyCacheManagerImpl;
import org.wso2.carbon.policy.mgt.core.dao.*; import org.wso2.carbon.policy.mgt.core.dao.*;
@ -74,12 +72,12 @@ import java.util.*;
public class PolicyManagerImpl implements PolicyManager { public class PolicyManagerImpl implements PolicyManager {
private PolicyDAO policyDAO; private final PolicyDAO policyDAO;
private ProfileDAO profileDAO; private final ProfileDAO profileDAO;
private FeatureDAO featureDAO; private final FeatureDAO featureDAO;
private ProfileManager profileManager; private final ProfileManager profileManager;
private PolicyConfiguration policyConfiguration; private final PolicyConfiguration policyConfiguration;
private static Log log = LogFactory.getLog(PolicyManagerImpl.class); private static final Log log = LogFactory.getLog(PolicyManagerImpl.class);
public PolicyManagerImpl() { public PolicyManagerImpl() {
this.policyDAO = PolicyManagementDAOFactory.getPolicyDAO(); this.policyDAO = PolicyManagementDAOFactory.getPolicyDAO();
@ -495,10 +493,11 @@ public class PolicyManagerImpl implements PolicyManager {
Policy policy) throws PolicyManagementException { Policy policy) throws PolicyManagementException {
List<Device> deviceList = new ArrayList<>(); List<Device> deviceList = new ArrayList<>();
DeviceManagementProviderService service = new DeviceManagementProviderServiceImpl(); DeviceManagementProviderService deviceManagementService = PolicyManagementDataHolder
.getInstance().getDeviceManagementService();
for (DeviceIdentifier deviceIdentifier : deviceIdentifierList) { for (DeviceIdentifier deviceIdentifier : deviceIdentifierList) {
try { try {
Device device = service.getDevice(deviceIdentifier, false); Device device = deviceManagementService.getDevice(deviceIdentifier, false);
deviceList.add(device); deviceList.add(device);
} catch (DeviceManagementException e) { } catch (DeviceManagementException e) {
throw new PolicyManagementException("Error occurred while retrieving device information", e); throw new PolicyManagementException("Error occurred while retrieving device information", e);
@ -732,19 +731,23 @@ public class PolicyManagerImpl implements PolicyManager {
List<Integer> policyIdList; List<Integer> policyIdList;
List<Policy> policies = new ArrayList<>(); List<Policy> policies = new ArrayList<>();
try {
DeviceManagementProviderService service = new DeviceManagementProviderServiceImpl(); DeviceManagementProviderService deviceManagementService = PolicyManagementDataHolder
Device device = service.getDevice(deviceIdentifier, false); .getInstance().getDeviceManagementService();
Device device;
try {
device = deviceManagementService.getDevice(deviceIdentifier, false);
} catch (DeviceManagementException e) {
throw new PolicyManagementException("Error occurred while getting device related to device identifier (" +
deviceIdentifier.getId() + " - " + deviceIdentifier.getType() + ")", e);
}
try {
PolicyManagementDAOFactory.openConnection(); PolicyManagementDAOFactory.openConnection();
policyIdList = policyDAO.getPolicyIdsOfDevice(device); policyIdList = policyDAO.getPolicyIdsOfDevice(device);
} catch (PolicyManagerDAOException e) { } catch (PolicyManagerDAOException e) {
throw new PolicyManagementException("Error occurred while getting the policies for device identifier (" + throw new PolicyManagementException("Error occurred while getting the policies for device identifier (" +
deviceIdentifier.getId() + " - " + deviceIdentifier.getType() + ")", e); deviceIdentifier.getId() + " - " + deviceIdentifier.getType() + ")", e);
} catch (DeviceManagementException e) {
throw new PolicyManagementException("Error occurred while getting device related to device identifier (" +
deviceIdentifier.getId() + " - " + deviceIdentifier.getType() + ")", e);
} catch (SQLException e) { } catch (SQLException e) {
throw new PolicyManagementException("Error occurred while open a data source connection", e); throw new PolicyManagementException("Error occurred while open a data source connection", e);
} finally { } finally {
@ -814,7 +817,6 @@ public class PolicyManagerImpl implements PolicyManager {
try { try {
PolicyManagementDAOFactory.openConnection(); PolicyManagementDAOFactory.openConnection();
policyIdList = policyDAO.getPolicyOfRole(roleName); policyIdList = policyDAO.getPolicyOfRole(roleName);
} catch (PolicyManagerDAOException e) { } catch (PolicyManagerDAOException e) {
throw new PolicyManagementException("Error occurred while getting the policies.", e); throw new PolicyManagementException("Error occurred while getting the policies.", e);
} catch (SQLException e) { } catch (SQLException e) {
@ -882,9 +884,16 @@ public class PolicyManagerImpl implements PolicyManager {
List<Device> deviceList = new ArrayList<>(); List<Device> deviceList = new ArrayList<>();
List<Integer> deviceIds; List<Integer> deviceIds;
DeviceManagementProviderService deviceManagementService = PolicyManagementDataHolder
.getInstance().getDeviceManagementService();
List<Device> allDevices;
try {
allDevices = deviceManagementService.getAllDevices();
} catch (DeviceManagementException e) {
throw new PolicyManagementException("Error occurred while getting the devices related to policy id (" +
policyId + ")", e);
}
try { try {
DeviceManagementProviderService service = PolicyManagementDataHolder.getInstance().getDeviceManagementService();
List<Device> allDevices = service.getAllDevices();
PolicyManagementDAOFactory.openConnection(); PolicyManagementDAOFactory.openConnection();
deviceIds = policyDAO.getPolicyAppliedDevicesIds(policyId); deviceIds = policyDAO.getPolicyAppliedDevicesIds(policyId);
HashMap<Integer, Device> allDeviceMap = new HashMap<>(); HashMap<Integer, Device> allDeviceMap = new HashMap<>();
@ -906,9 +915,6 @@ public class PolicyManagerImpl implements PolicyManager {
policyId + ")", e); policyId + ")", e);
} catch (SQLException e) { } catch (SQLException e) {
throw new PolicyManagementException("Error occurred while opening a connection to the data source", e); throw new PolicyManagementException("Error occurred while opening a connection to the data source", e);
} catch (DeviceManagementException e) {
throw new PolicyManagementException("Error occurred while getting the devices related to policy id (" +
policyId + ")", e);
} finally { } finally {
PolicyManagementDAOFactory.closeConnection(); PolicyManagementDAOFactory.closeConnection();
} }
@ -918,12 +924,17 @@ public class PolicyManagerImpl implements PolicyManager {
@Override @Override
public void addAppliedPolicyFeaturesToDevice(DeviceIdentifier deviceIdentifier, public void addAppliedPolicyFeaturesToDevice(DeviceIdentifier deviceIdentifier,
Policy policy) throws PolicyManagementException { Policy policy) throws PolicyManagementException {
int deviceId = -1; DeviceManagementProviderService deviceManagementService = PolicyManagementDataHolder
.getInstance().getDeviceManagementService();
Device device;
try {
device = deviceManagementService.getDevice(deviceIdentifier, false);
} catch (DeviceManagementException e) {
throw new PolicyManagementException("Error occurred while getting the device details (" +
deviceIdentifier.getId() + ")", e);
}
int deviceId = device.getId();
try { try {
DeviceManagementProviderService service = new DeviceManagementProviderServiceImpl();
Device device = service.getDevice(deviceIdentifier, false);
deviceId = device.getId();
PolicyManagementDAOFactory.beginTransaction(); PolicyManagementDAOFactory.beginTransaction();
boolean exist = policyDAO.checkPolicyAvailable(deviceId, device.getEnrolmentInfo().getId()); boolean exist = policyDAO.checkPolicyAvailable(deviceId, device.getEnrolmentInfo().getId());
if (exist) { if (exist) {
@ -936,9 +947,6 @@ public class PolicyManagerImpl implements PolicyManager {
PolicyManagementDAOFactory.rollbackTransaction(); PolicyManagementDAOFactory.rollbackTransaction();
throw new PolicyManagementException("Error occurred while adding the evaluated policy to device (" + throw new PolicyManagementException("Error occurred while adding the evaluated policy to device (" +
deviceId + " - " + policy.getId() + ")", e); deviceId + " - " + policy.getId() + ")", e);
} catch (DeviceManagementException e) {
throw new PolicyManagementException("Error occurred while getting the device details (" +
deviceIdentifier.getId() + ")", e);
} finally { } finally {
PolicyManagementDAOFactory.closeConnection(); PolicyManagementDAOFactory.closeConnection();
} }
@ -997,12 +1005,18 @@ public class PolicyManagerImpl implements PolicyManager {
@Override @Override
public void addAppliedPolicyToDevice(DeviceIdentifier deviceIdentifier, Policy policy) public void addAppliedPolicyToDevice(DeviceIdentifier deviceIdentifier, Policy policy)
throws PolicyManagementException { throws PolicyManagementException {
DeviceManagementProviderService deviceManagementService = PolicyManagementDataHolder
int deviceId = -1; .getInstance().getDeviceManagementService();
Device device;
try {
device = deviceManagementService.getDevice(deviceIdentifier, false);
} catch (DeviceManagementException e) {
PolicyManagementDAOFactory.rollbackTransaction();
throw new PolicyManagementException("Error occurred while getting the device details (" +
deviceIdentifier.getId() + ")", e);
}
int deviceId = device.getId();
try { try {
DeviceManagementProviderService service = new DeviceManagementProviderServiceImpl();
Device device = service.getDevice(deviceIdentifier, false);
deviceId = device.getId();
PolicyManagementDAOFactory.beginTransaction(); PolicyManagementDAOFactory.beginTransaction();
Policy policySaved = policyDAO.getAppliedPolicy(deviceId, device.getEnrolmentInfo().getId()); Policy policySaved = policyDAO.getAppliedPolicy(deviceId, device.getEnrolmentInfo().getId());
@ -1016,10 +1030,6 @@ public class PolicyManagerImpl implements PolicyManager {
PolicyManagementDAOFactory.rollbackTransaction(); PolicyManagementDAOFactory.rollbackTransaction();
throw new PolicyManagementException("Error occurred while adding the evaluated policy to device (" + throw new PolicyManagementException("Error occurred while adding the evaluated policy to device (" +
deviceId + " - " + policy.getId() + ")", e); deviceId + " - " + policy.getId() + ")", e);
} catch (DeviceManagementException e) {
PolicyManagementDAOFactory.rollbackTransaction();
throw new PolicyManagementException("Error occurred while getting the device details (" +
deviceIdentifier.getId() + ")", e);
} finally { } finally {
PolicyManagementDAOFactory.closeConnection(); PolicyManagementDAOFactory.closeConnection();
} }
@ -1027,12 +1037,18 @@ public class PolicyManagerImpl implements PolicyManager {
@Override @Override
public void removeAppliedPolicyToDevice(DeviceIdentifier deviceIdentifier) throws PolicyManagementException { public void removeAppliedPolicyToDevice(DeviceIdentifier deviceIdentifier) throws PolicyManagementException {
DeviceManagementProviderService deviceManagementService = PolicyManagementDataHolder
int deviceId = -1; .getInstance().getDeviceManagementService();
Device device;
try {
device = deviceManagementService.getDevice(deviceIdentifier, false);
} catch (DeviceManagementException e) {
PolicyManagementDAOFactory.rollbackTransaction();
throw new PolicyManagementException("Error occurred while getting the device details (" +
deviceIdentifier.getId() + ")", e);
}
int deviceId = device.getId();
try { try {
DeviceManagementProviderService service = new DeviceManagementProviderServiceImpl();
Device device = service.getDevice(deviceIdentifier, false);
deviceId = device.getId();
PolicyManagementDAOFactory.beginTransaction(); PolicyManagementDAOFactory.beginTransaction();
Policy policySaved = policyDAO.getAppliedPolicy(deviceId, device.getEnrolmentInfo().getId()); Policy policySaved = policyDAO.getAppliedPolicy(deviceId, device.getEnrolmentInfo().getId());
@ -1044,10 +1060,6 @@ public class PolicyManagerImpl implements PolicyManager {
PolicyManagementDAOFactory.rollbackTransaction(); PolicyManagementDAOFactory.rollbackTransaction();
throw new PolicyManagementException("Error occurred while removing the applied policy to device (" + throw new PolicyManagementException("Error occurred while removing the applied policy to device (" +
deviceId + ")", e); deviceId + ")", e);
} catch (DeviceManagementException e) {
PolicyManagementDAOFactory.rollbackTransaction();
throw new PolicyManagementException("Error occurred while getting the device details (" +
deviceIdentifier.getId() + ")", e);
} finally { } finally {
PolicyManagementDAOFactory.closeConnection(); PolicyManagementDAOFactory.closeConnection();
} }
@ -1057,17 +1069,21 @@ public class PolicyManagerImpl implements PolicyManager {
public boolean checkPolicyAvailable(DeviceIdentifier deviceIdentifier) throws PolicyManagementException { public boolean checkPolicyAvailable(DeviceIdentifier deviceIdentifier) throws PolicyManagementException {
boolean exist; boolean exist;
DeviceManagementProviderService deviceManagementService = PolicyManagementDataHolder
.getInstance().getDeviceManagementService();
Device device;
try {
device = deviceManagementService.getDevice(deviceIdentifier, false);
} catch (DeviceManagementException e) {
throw new PolicyManagementException("Error occurred while getting the device details (" +
deviceIdentifier.getId() + ")", e);
}
try { try {
DeviceManagementProviderService service = new DeviceManagementProviderServiceImpl();
Device device = service.getDevice(deviceIdentifier, false);
PolicyManagementDAOFactory.openConnection(); PolicyManagementDAOFactory.openConnection();
exist = policyDAO.checkPolicyAvailable(device.getId(), device.getEnrolmentInfo().getId()); exist = policyDAO.checkPolicyAvailable(device.getId(), device.getEnrolmentInfo().getId());
} catch (PolicyManagerDAOException e) { } catch (PolicyManagerDAOException e) {
throw new PolicyManagementException("Error occurred while checking whether device has a policy " + throw new PolicyManagementException("Error occurred while checking whether device has a policy " +
"to apply.", e); "to apply.", e);
} catch (DeviceManagementException e) {
throw new PolicyManagementException("Error occurred while getting the device details (" +
deviceIdentifier.getId() + ")", e);
} catch (SQLException e) { } catch (SQLException e) {
throw new PolicyManagementException("Error occurred while opening a connection to the data source", e); throw new PolicyManagementException("Error occurred while opening a connection to the data source", e);
} finally { } finally {
@ -1078,19 +1094,24 @@ public class PolicyManagerImpl implements PolicyManager {
@Override @Override
public boolean setPolicyApplied(DeviceIdentifier deviceIdentifier) throws PolicyManagementException { public boolean setPolicyApplied(DeviceIdentifier deviceIdentifier) throws PolicyManagementException {
DeviceManagementProviderService deviceManagementService = PolicyManagementDataHolder
.getInstance().getDeviceManagementService();
Device device;
try { try {
DeviceManagementProviderService service = new DeviceManagementProviderServiceImpl(); device = deviceManagementService.getDevice(deviceIdentifier, false);
Device device = service.getDevice(deviceIdentifier, false); } catch (DeviceManagementException e) {
throw new PolicyManagementException("Error occurred while getting the device details (" +
deviceIdentifier.getId() + ")", e);
}
try {
PolicyManagementDAOFactory.openConnection(); PolicyManagementDAOFactory.openConnection();
policyDAO.setPolicyApplied(device.getId(), device.getEnrolmentInfo().getId()); policyDAO.setPolicyApplied(device.getId(), device.getEnrolmentInfo().getId());
return true; return true;
} catch (PolicyManagerDAOException e) { } catch (PolicyManagerDAOException e) {
throw new PolicyManagementException("Error occurred while setting the policy has applied to device (" + throw new PolicyManagementException("Error occurred while setting the policy has applied to device (" +
deviceIdentifier.getId() + ")", e); deviceIdentifier.getId() + ")", e);
} catch (DeviceManagementException e) {
throw new PolicyManagementException("Error occurred while getting the device details (" +
deviceIdentifier.getId() + ")", e);
} catch (SQLException e) { } catch (SQLException e) {
throw new PolicyManagementException("Error occurred while opening a connection to the data source", e); throw new PolicyManagementException("Error occurred while opening a connection to the data source", e);
} finally { } finally {
@ -1115,10 +1136,11 @@ public class PolicyManagerImpl implements PolicyManager {
@Override @Override
@Deprecated @Deprecated
public Policy getAppliedPolicyToDevice(DeviceIdentifier deviceId) throws PolicyManagementException { public Policy getAppliedPolicyToDevice(DeviceIdentifier deviceId) throws PolicyManagementException {
DeviceManagementProviderService service = new DeviceManagementProviderServiceImpl(); DeviceManagementProviderService deviceManagementService = PolicyManagementDataHolder
.getInstance().getDeviceManagementService();
Device device; Device device;
try { try {
device = service.getDevice(deviceId, false); device = deviceManagementService.getDevice(deviceId, false);
if (device == null) { if (device == null) {
if (log.isDebugEnabled()) { if (log.isDebugEnabled()) {
log.debug("No device is found upon the device identifier '" + deviceId.getId() + log.debug("No device is found upon the device identifier '" + deviceId.getId() +
@ -1163,17 +1185,18 @@ public class PolicyManagerImpl implements PolicyManager {
} }
} }
private List<DeviceGroupWrapper> getDeviceGroupNames(List<DeviceGroupWrapper> groupWrappers) throws GroupManagementException { private List<DeviceGroupWrapper> getDeviceGroupNames(List<DeviceGroupWrapper> groupWrappers)
GroupManagementProviderService groupManagementProviderService = new GroupManagementProviderServiceImpl(); throws GroupManagementException {
GroupManagementProviderService groupManagementService = PolicyManagementDataHolder
.getInstance().getGroupManagementService();
for (DeviceGroupWrapper wrapper : groupWrappers) { for (DeviceGroupWrapper wrapper : groupWrappers) {
DeviceGroup deviceGroup = groupManagementProviderService.getGroup(wrapper.getId(), false); DeviceGroup deviceGroup = groupManagementService.getGroup(wrapper.getId(), false);
wrapper.setName(deviceGroup.getName()); wrapper.setName(deviceGroup.getName());
wrapper.setOwner(deviceGroup.getOwner()); wrapper.setOwner(deviceGroup.getOwner());
} }
return groupWrappers; return groupWrappers;
} }
private List<DeviceIdentifier> convertDevices(List<Device> devices) { private List<DeviceIdentifier> convertDevices(List<Device> devices) {
List<DeviceIdentifier> deviceIdentifiers = new ArrayList<>(); List<DeviceIdentifier> deviceIdentifiers = new ArrayList<>();
for (Device device : devices) { for (Device device : devices) {
@ -1185,7 +1208,6 @@ public class PolicyManagerImpl implements PolicyManager {
return deviceIdentifiers; return deviceIdentifiers;
} }
private void addPolicyRevokeOperation(List<DeviceIdentifier> deviceIdentifiers) throws PolicyManagementException { private void addPolicyRevokeOperation(List<DeviceIdentifier> deviceIdentifiers) throws PolicyManagementException {
try { try {
String type = null; String type = null;

@ -16,7 +16,6 @@
* under the License. * under the License.
*/ */
package org.wso2.carbon.policy.mgt.core.task; package org.wso2.carbon.policy.mgt.core.task;
import org.apache.commons.logging.Log; import org.apache.commons.logging.Log;
@ -29,11 +28,9 @@ import org.wso2.carbon.device.mgt.common.exceptions.DeviceManagementException;
import org.wso2.carbon.device.mgt.common.policy.mgt.PolicyMonitoringManager; import org.wso2.carbon.device.mgt.common.policy.mgt.PolicyMonitoringManager;
import org.wso2.carbon.device.mgt.common.policy.mgt.monitor.PolicyComplianceException; import org.wso2.carbon.device.mgt.common.policy.mgt.monitor.PolicyComplianceException;
import org.wso2.carbon.device.mgt.core.service.DeviceManagementProviderService; import org.wso2.carbon.device.mgt.core.service.DeviceManagementProviderService;
import org.wso2.carbon.device.mgt.core.service.DeviceManagementProviderServiceImpl;
import org.wso2.carbon.ntask.core.Task; import org.wso2.carbon.ntask.core.Task;
import org.wso2.carbon.policy.mgt.core.internal.PolicyManagementDataHolder; import org.wso2.carbon.policy.mgt.core.internal.PolicyManagementDataHolder;
import org.wso2.carbon.policy.mgt.core.mgt.MonitoringManager; import org.wso2.carbon.policy.mgt.core.mgt.MonitoringManager;
import org.wso2.carbon.user.api.UserStoreException;
import java.util.ArrayList; import java.util.ArrayList;
import java.util.List; import java.util.List;
@ -43,11 +40,8 @@ public class MonitoringTask implements Task {
private static final Log log = LogFactory.getLog(MonitoringTask.class); private static final Log log = LogFactory.getLog(MonitoringTask.class);
Map<String, String> properties;
@Override @Override
public void setProperties(Map<String, String> map) { public void setProperties(Map<String, String> map) {
this.properties = map;
} }
@Override @Override
@ -84,7 +78,8 @@ public class MonitoringTask implements Task {
log.debug("Monitoring task started to run for all tenants."); log.debug("Monitoring task started to run for all tenants.");
} }
try { try {
DeviceManagementProviderService deviceManagementService = new DeviceManagementProviderServiceImpl(); DeviceManagementProviderService deviceManagementService = PolicyManagementDataHolder
.getInstance().getDeviceManagementService();
List<Integer> tenants = deviceManagementService.getDeviceEnrolledTenants(); List<Integer> tenants = deviceManagementService.getDeviceEnrolledTenants();
for (Integer tenant : tenants) { for (Integer tenant : tenants) {
if (MultitenantConstants.SUPER_TENANT_ID == tenant) { if (MultitenantConstants.SUPER_TENANT_ID == tenant) {

@ -39,6 +39,7 @@ import org.wso2.carbon.device.mgt.core.authorization.DeviceAccessAuthorizationSe
import org.wso2.carbon.device.mgt.core.config.DeviceConfigurationManager; import org.wso2.carbon.device.mgt.core.config.DeviceConfigurationManager;
import org.wso2.carbon.device.mgt.core.dao.DeviceManagementDAOFactory; import org.wso2.carbon.device.mgt.core.dao.DeviceManagementDAOFactory;
import org.wso2.carbon.device.mgt.core.dao.GroupManagementDAOFactory; import org.wso2.carbon.device.mgt.core.dao.GroupManagementDAOFactory;
import org.wso2.carbon.device.mgt.core.device.details.mgt.impl.DeviceInformationManagerImpl;
import org.wso2.carbon.device.mgt.core.internal.DeviceManagementDataHolder; import org.wso2.carbon.device.mgt.core.internal.DeviceManagementDataHolder;
import org.wso2.carbon.device.mgt.core.internal.DeviceManagementServiceComponent; import org.wso2.carbon.device.mgt.core.internal.DeviceManagementServiceComponent;
import org.wso2.carbon.device.mgt.core.operation.mgt.dao.OperationManagementDAOFactory; import org.wso2.carbon.device.mgt.core.operation.mgt.dao.OperationManagementDAOFactory;
@ -112,11 +113,17 @@ public abstract class BasePolicyManagementDAOTest {
DeviceManagementDataHolder.getInstance().setDeviceAccessAuthorizationService( DeviceManagementDataHolder.getInstance().setDeviceAccessAuthorizationService(
new DeviceAccessAuthorizationServiceImpl()); new DeviceAccessAuthorizationServiceImpl());
DeviceManagementDataHolder.getInstance().setGroupManagementProviderService(groupMgtService); DeviceManagementDataHolder.getInstance().setGroupManagementProviderService(groupMgtService);
DeviceManagementDataHolder.getInstance().setDeviceInformationManager(new DeviceInformationManagerImpl());
DeviceManagementDataHolder.getInstance().setDeviceTaskManagerService(null); DeviceManagementDataHolder.getInstance().setDeviceTaskManagerService(null);
PolicyEvaluationPoint policyEvaluationPoint = new SimplePolicyEvaluationTest(); PolicyEvaluationPoint policyEvaluationPoint = new SimplePolicyEvaluationTest();
PolicyManagementDataHolder.getInstance().setPolicyEvaluationPoint("Simple", policyEvaluationPoint); PolicyManagementDataHolder.getInstance().setPolicyEvaluationPoint("Simple", policyEvaluationPoint);
PolicyManagementDataHolder.getInstance().setDeviceManagementService(deviceMgtService); PolicyManagementDataHolder.getInstance().setDeviceManagementService(deviceMgtService);
PolicyManagementDataHolder.getInstance().setPolicyManagerService(new PolicyManagerServiceImpl());
Field groupManagementService = PolicyManagementDataHolder.class.getDeclaredField("groupManagementService");
groupManagementService.setAccessible(true);
groupManagementService.set(PolicyManagementDataHolder.getInstance(), groupMgtService);
profileManager = new ProfileManagerImpl(); profileManager = new ProfileManagerImpl();
} }

@ -22,16 +22,19 @@ package org.wso2.carbon.policy.mgt.core;
import junit.framework.Assert; import junit.framework.Assert;
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.testng.annotations.BeforeClass;
import org.testng.annotations.Test; import org.testng.annotations.Test;
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.exceptions.DeviceManagementException; import org.wso2.carbon.device.mgt.common.exceptions.DeviceManagementException;
import org.wso2.carbon.device.mgt.common.operation.mgt.OperationManager; import org.wso2.carbon.device.mgt.common.operation.mgt.OperationManager;
import org.wso2.carbon.device.mgt.core.device.details.mgt.impl.DeviceInformationManagerImpl;
import org.wso2.carbon.device.mgt.core.internal.DeviceManagementDataHolder; import org.wso2.carbon.device.mgt.core.internal.DeviceManagementDataHolder;
import org.wso2.carbon.device.mgt.core.operation.mgt.OperationManagerImpl; import org.wso2.carbon.device.mgt.core.operation.mgt.OperationManagerImpl;
import org.wso2.carbon.device.mgt.core.service.DeviceManagementProviderService; import org.wso2.carbon.device.mgt.core.service.DeviceManagementProviderService;
import org.wso2.carbon.device.mgt.core.service.DeviceManagementProviderServiceImpl; import org.wso2.carbon.device.mgt.core.service.DeviceManagementProviderServiceImpl;
import org.wso2.carbon.device.mgt.common.policy.mgt.Policy; import org.wso2.carbon.device.mgt.common.policy.mgt.Policy;
import org.wso2.carbon.device.mgt.core.service.GroupManagementProviderServiceImpl;
import org.wso2.carbon.policy.mgt.common.PolicyManagementException; import org.wso2.carbon.policy.mgt.common.PolicyManagementException;
import org.wso2.carbon.device.mgt.common.policy.mgt.monitor.PolicyComplianceException; import org.wso2.carbon.device.mgt.common.policy.mgt.monitor.PolicyComplianceException;
import org.wso2.carbon.policy.mgt.core.internal.PolicyManagementDataHolder; import org.wso2.carbon.policy.mgt.core.internal.PolicyManagementDataHolder;
@ -55,7 +58,11 @@ public class MonitoringTestCase extends BasePolicyManagementDAOTest {
public void testMonitorDao() { public void testMonitorDao() {
DeviceManagementProviderService service = new DeviceManagementProviderServiceImpl(); DeviceManagementProviderService service = new DeviceManagementProviderServiceImpl();
DeviceManagementDataHolder.getInstance().setDeviceManagementProvider(service);
DeviceManagementDataHolder.getInstance().setDeviceInformationManager(new DeviceInformationManagerImpl());
DeviceManagementDataHolder.getInstance().setGroupManagementProviderService(new GroupManagementProviderServiceImpl());
PolicyManagerService policyManagerService = new PolicyManagerServiceImpl(); PolicyManagerService policyManagerService = new PolicyManagerServiceImpl();
PolicyManagementDataHolder.getInstance().setPolicyManagerService(policyManagerService);
List<Policy> policies = null; List<Policy> policies = null;
List<Device> devices = null; List<Device> devices = null;

@ -55,6 +55,8 @@ public class PolicyDAOTestCase extends BasePolicyManagementDAOTest {
@BeforeClass @BeforeClass
public void init() throws Exception { public void init() throws Exception {
log.info("Initializing policy tests");
super.initializeServices();
initDatSource(); initDatSource();
// System.setProperty("GetTenantIDForTest", "Super"); // System.setProperty("GetTenantIDForTest", "Super");
initiatePrivilegedCaronContext(); initiatePrivilegedCaronContext();

@ -46,6 +46,8 @@ public class PolicyEvaluationTestCase extends BasePolicyManagementDAOTest {
@BeforeClass @BeforeClass
public void init() throws Exception { public void init() throws Exception {
log.info("Initializing policy tests");
super.initializeServices();
PolicyEvaluationPoint evaluationPoint = new SimplePolicyEvaluationTest(); PolicyEvaluationPoint evaluationPoint = new SimplePolicyEvaluationTest();
PolicyManagementDataHolder.getInstance().setPolicyEvaluationPoint(evaluationPoint.getName(), evaluationPoint); PolicyManagementDataHolder.getInstance().setPolicyEvaluationPoint(evaluationPoint.getName(), evaluationPoint);
} }

@ -89,6 +89,7 @@ public class PolicyManagerServiceImplTest extends BasePolicyManagementDAOTest {
public void addPolicy() throws DeviceManagementException, GroupManagementException, PolicyManagementException { public void addPolicy() throws DeviceManagementException, GroupManagementException, PolicyManagementException {
int tenantId = PrivilegedCarbonContext.getThreadLocalCarbonContext().getTenantId(); int tenantId = PrivilegedCarbonContext.getThreadLocalCarbonContext().getTenantId();
policyManagerService = new PolicyManagerServiceImpl(); policyManagerService = new PolicyManagerServiceImpl();
PolicyManagementDataHolder.getInstance().setPolicyManagerService(policyManagerService);
DeviceManagementService deviceManagementService = new TypeXDeviceManagementService(DEVICE_TYPE_A); DeviceManagementService deviceManagementService = new TypeXDeviceManagementService(DEVICE_TYPE_A);
deviceMgtService.registerDeviceType(deviceManagementService); deviceMgtService.registerDeviceType(deviceManagementService);
operationManager = new OperationManagerImpl(DEVICE_TYPE_A, deviceManagementService); operationManager = new OperationManagerImpl(DEVICE_TYPE_A, deviceManagementService);

Loading…
Cancel
Save