Fixing transactional anomalies and code cleanup

4.x.x
prabathabey 9 years ago
parent d7e363cef7
commit cdbdc86f42

@ -84,28 +84,48 @@ public class DeviceManagementProviderServiceImpl implements DeviceManagementProv
} }
@Override @Override
public TenantConfiguration getConfiguration(String type) throws DeviceManagementException { public TenantConfiguration getConfiguration(String deviceType) throws DeviceManagementException {
DeviceManager dms = DeviceManager dms =
this.getPluginRepository().getDeviceManagementService(type).getDeviceManager(); this.getPluginRepository().getDeviceManagementService(deviceType).getDeviceManager();
if (dms == null) {
if (log.isDebugEnabled()) {
log.debug("Device type '" + deviceType + "' does not have an associated device management " +
"plugin registered within the framework. Therefore, not attempting getConfiguration method");
}
return null;
}
return dms.getConfiguration(); return dms.getConfiguration();
} }
@Override @Override
public FeatureManager getFeatureManager(String type) { public FeatureManager getFeatureManager(String deviceType) {
DeviceManager dms = DeviceManager deviceManager = this.getDeviceManager(deviceType);
this.getPluginRepository().getDeviceManagementService(type).getDeviceManager(); if (deviceManager == null) {
return dms.getFeatureManager(); if (log.isDebugEnabled()) {
log.debug("Device Manager associated with the device type '" + deviceType + "' is null. " +
"Therefore, not attempting method 'getFeatureManager'");
}
return null;
}
return deviceManager.getFeatureManager();
} }
@Override @Override
public boolean enrollDevice(Device device) throws DeviceManagementException { public boolean enrollDevice(Device device) throws DeviceManagementException {
boolean status = false; boolean status = false;
DeviceIdentifier deviceIdentifier = new DeviceIdentifier(device.getDeviceIdentifier(), device.getType()); DeviceIdentifier deviceIdentifier = new DeviceIdentifier(device.getDeviceIdentifier(), device.getType());
DeviceManager dms =
this.getPluginRepository().getDeviceManagementService(device.getType()).getDeviceManager();
dms.enrollDevice(device);
if (dms.isClaimable(deviceIdentifier)) { DeviceManager deviceManager = this.getDeviceManager(device.getType());
if (deviceManager == null) {
if (log.isDebugEnabled()) {
log.debug("Device Manager associated with the device type '" + device.getType() + "' is null. " +
"Therefore, not attempting method 'enrollDevice'");
}
return false;
}
deviceManager.enrollDevice(device);
if (deviceManager.isClaimable(deviceIdentifier)) {
device.getEnrolmentInfo().setStatus(EnrolmentInfo.Status.INACTIVE); device.getEnrolmentInfo().setStatus(EnrolmentInfo.Status.INACTIVE);
} else { } else {
device.getEnrolmentInfo().setStatus(EnrolmentInfo.Status.ACTIVE); device.getEnrolmentInfo().setStatus(EnrolmentInfo.Status.ACTIVE);
@ -176,9 +196,15 @@ public class DeviceManagementProviderServiceImpl implements DeviceManagementProv
@Override @Override
public boolean modifyEnrollment(Device device) throws DeviceManagementException { public boolean modifyEnrollment(Device device) throws DeviceManagementException {
DeviceManager dms = DeviceManager deviceManager = this.getDeviceManager(device.getType());
this.getPluginRepository().getDeviceManagementService(device.getType()).getDeviceManager(); if (deviceManager == null) {
boolean status = dms.modifyEnrollment(device); if (log.isDebugEnabled()) {
log.debug("Device Manager associated with the device type '" + device.getType() + "' is null. " +
"Therefore, not attempting method 'modifyEnrolment'");
}
return false;
}
boolean status = deviceManager.modifyEnrollment(device);
try { try {
int tenantId = this.getTenantId(); int tenantId = this.getTenantId();
DeviceManagementDAOFactory.beginTransaction(); DeviceManagementDAOFactory.beginTransaction();
@ -200,11 +226,16 @@ public class DeviceManagementProviderServiceImpl implements DeviceManagementProv
@Override @Override
public boolean disenrollDevice(DeviceIdentifier deviceId) throws DeviceManagementException { public boolean disenrollDevice(DeviceIdentifier deviceId) throws DeviceManagementException {
DeviceManager deviceManager = this.getDeviceManager(deviceId.getType());
int tenantId = this.getTenantId(); if (deviceManager == null) {
DeviceManager dms = if (log.isDebugEnabled()) {
this.getPluginRepository().getDeviceManagementService(deviceId.getType()).getDeviceManager(); log.debug("Device Manager associated with the device type '" + deviceId.getType() + "' is null. " +
"Therefore, not attempting method 'dis-enrollDevice'");
}
return false;
}
try { try {
int tenantId = this.getTenantId();
DeviceManagementDAOFactory.beginTransaction(); DeviceManagementDAOFactory.beginTransaction();
Device device = deviceDAO.getDevice(deviceId, tenantId); Device device = deviceDAO.getDevice(deviceId, tenantId);
@ -218,33 +249,51 @@ public class DeviceManagementProviderServiceImpl implements DeviceManagementProv
DeviceManagementDAOFactory.commitTransaction(); DeviceManagementDAOFactory.commitTransaction();
} catch (DeviceManagementDAOException | TransactionManagementException e) { } catch (DeviceManagementDAOException | TransactionManagementException e) {
DeviceManagementDAOFactory.rollbackTransaction(); DeviceManagementDAOFactory.rollbackTransaction();
throw new DeviceManagementException("Error occurred while disenrolling '" + deviceId.getType() + throw new DeviceManagementException("Error occurred while dis-enrolling '" + deviceId.getType() +
"' device with the identifier '" + deviceId.getId() + "'", e); "' device with the identifier '" + deviceId.getId() + "'", e);
} finally { } finally {
DeviceManagementDAOFactory.closeConnection(); DeviceManagementDAOFactory.closeConnection();
} }
return dms.disenrollDevice(deviceId); return deviceManager.disenrollDevice(deviceId);
} }
@Override @Override
public boolean isEnrolled(DeviceIdentifier deviceId) throws DeviceManagementException { public boolean isEnrolled(DeviceIdentifier deviceId) throws DeviceManagementException {
DeviceManager dms = DeviceManager deviceManager = this.getDeviceManager(deviceId.getType());
this.getPluginRepository().getDeviceManagementService(deviceId.getType()).getDeviceManager(); if (deviceManager == null) {
return dms.isEnrolled(deviceId); if (log.isDebugEnabled()) {
log.debug("Device Manager associated with the device type '" + deviceId.getType() + "' is null. " +
"Therefore, not attempting method 'isEnrolled'");
}
return false;
}
return deviceManager.isEnrolled(deviceId);
} }
@Override @Override
public boolean isActive(DeviceIdentifier deviceId) throws DeviceManagementException { public boolean isActive(DeviceIdentifier deviceId) throws DeviceManagementException {
DeviceManager dms = DeviceManager deviceManager = this.getDeviceManager(deviceId.getType());
this.getPluginRepository().getDeviceManagementService(deviceId.getType()).getDeviceManager(); if (deviceManager == null) {
return dms.isActive(deviceId); if (log.isDebugEnabled()) {
log.debug("Device Manager associated with the device type '" + deviceId.getType() + "' is null. " +
"Therefore, not attempting method 'isActive'");
}
return false;
}
return deviceManager.isActive(deviceId);
} }
@Override @Override
public boolean setActive(DeviceIdentifier deviceId, boolean status) throws DeviceManagementException { public boolean setActive(DeviceIdentifier deviceId, boolean status) throws DeviceManagementException {
DeviceManager dms = DeviceManager deviceManager = this.getDeviceManager(deviceId.getType());
this.getPluginRepository().getDeviceManagementService(deviceId.getType()).getDeviceManager(); if (deviceManager == null) {
return dms.setActive(deviceId, status); if (log.isDebugEnabled()) {
log.debug("Device Manager associated with the device type '" + deviceId.getType() + "' is null. " +
"Therefore, not attempting method 'setActive'");
}
return false;
}
return deviceManager.setActive(deviceId, status);
} }
@Override @Override
@ -262,47 +311,56 @@ public class DeviceManagementProviderServiceImpl implements DeviceManagementProv
DeviceManagementDAOFactory.closeConnection(); DeviceManagementDAOFactory.closeConnection();
} }
for (Device device : allDevices) { for (Device device : allDevices) {
DeviceManagementService managementService = this.getPluginRepository(). DeviceManager deviceManager = this.getDeviceManager(device.getType());
getDeviceManagementService(device.getType()); if (deviceManager == null) {
if (managementService != null) { if (log.isDebugEnabled()) {
Device dmsDevice = managementService.getDeviceManager().getDevice( log.debug("Device Manager associated with the device type '" + device.getType() + "' is null. " +
new DeviceIdentifier(device.getDeviceIdentifier(), device.getType())); "Therefore, not attempting method 'isEnrolled'");
}
devices.add(device);
continue;
}
Device dmsDevice =
deviceManager.getDevice(new DeviceIdentifier(device.getDeviceIdentifier(), device.getType()));
if (dmsDevice != null) { if (dmsDevice != null) {
device.setFeatures(dmsDevice.getFeatures()); device.setFeatures(dmsDevice.getFeatures());
device.setProperties(dmsDevice.getProperties()); device.setProperties(dmsDevice.getProperties());
} }
}
devices.add(device); devices.add(device);
} }
return devices; return devices;
} }
@Override @Override
public List<Device> getAllDevices(String type) throws DeviceManagementException { public List<Device> getAllDevices(String deviceType) throws DeviceManagementException {
List<Device> devices = new ArrayList<>(); List<Device> devices = new ArrayList<>();
List<Device> allDevices; List<Device> allDevices;
try { try {
DeviceManagementDAOFactory.openConnection(); DeviceManagementDAOFactory.openConnection();
allDevices = deviceDAO.getDevices(deviceType, this.getTenantId());
allDevices = deviceDAO.getDevices(type, this.getTenantId());
} catch (DeviceManagementDAOException | SQLException e) { } catch (DeviceManagementDAOException | SQLException e) {
throw new DeviceManagementException("Error occurred while retrieving all devices of type '" + throw new DeviceManagementException("Error occurred while retrieving all devices of type '" +
type + "' that are being managed within the scope of current tenant", e); deviceType + "' that are being managed within the scope of current tenant", e);
} finally { } finally {
DeviceManagementDAOFactory.closeConnection(); DeviceManagementDAOFactory.closeConnection();
} }
for (Device device : allDevices) { for (Device device : allDevices) {
DeviceManager deviceManager = this.getDeviceManager(deviceType);
DeviceManagementService service = this.getPluginRepository().getDeviceManagementService(device.getType()); if (deviceManager == null) {
if (service != null) { if (log.isDebugEnabled()) {
Device dmsDevice = service.getDeviceManager().getDevice( log.debug("Device Manager associated with the device type '" + deviceType + "' is null. " +
new DeviceIdentifier(device.getDeviceIdentifier(), device.getType())); "Therefore, not attempting method 'isEnrolled'");
}
devices.add(device);
continue;
}
Device dmsDevice =
deviceManager.getDevice(new DeviceIdentifier(device.getDeviceIdentifier(), device.getType()));
if (dmsDevice != null) { if (dmsDevice != null) {
device.setFeatures(dmsDevice.getFeatures()); device.setFeatures(dmsDevice.getFeatures());
device.setProperties(dmsDevice.getProperties()); device.setProperties(dmsDevice.getProperties());
} }
}
devices.add(device); devices.add(device);
} }
return devices; return devices;
@ -313,7 +371,6 @@ public class DeviceManagementProviderServiceImpl implements DeviceManagementProv
throws DeviceManagementException { throws DeviceManagementException {
List<NotificationMessages> notificationMessages = List<NotificationMessages> notificationMessages =
DeviceConfigurationManager.getInstance().getNotificationMessagesConfig().getNotificationMessagesList(); DeviceConfigurationManager.getInstance().getNotificationMessagesConfig().getNotificationMessagesList();
String messageHeader = ""; String messageHeader = "";
String messageBody = ""; String messageBody = "";
String messageFooter1 = ""; String messageFooter1 = "";
@ -369,9 +426,8 @@ public class DeviceManagementProviderServiceImpl implements DeviceManagementProv
@Override @Override
public void sendRegistrationEmail(EmailMessageProperties emailMessageProperties) throws DeviceManagementException { public void sendRegistrationEmail(EmailMessageProperties emailMessageProperties) throws DeviceManagementException {
List<NotificationMessages> notificationMessages = DeviceConfigurationManager.getInstance() List<NotificationMessages> notificationMessages =
.getNotificationMessagesConfig().getNotificationMessagesList(); DeviceConfigurationManager.getInstance().getNotificationMessagesConfig().getNotificationMessagesList();
String messageHeader = ""; String messageHeader = "";
String messageBody = ""; String messageBody = "";
String messageFooter1 = ""; String messageFooter1 = "";
@ -446,38 +502,60 @@ public class DeviceManagementProviderServiceImpl implements DeviceManagementProv
if (device != null) { if (device != null) {
// The changes made here to prevent unit tests getting failed. They failed because when running the unit // The changes made here to prevent unit tests getting failed. They failed because when running the unit
// tests there is no osgi services. So getDeviceManager() returns a null. // tests there is no osgi services. So getDeviceManager() returns a null.
DeviceManagementService service = this.getPluginRepository().getDeviceManagementService(deviceId.getType()); DeviceManager deviceManager = this.getDeviceManager(deviceId.getType());
if (service != null) { if (deviceManager == null) {
DeviceManager dms = service.getDeviceManager(); if (log.isDebugEnabled()) {
Device pluginSpecificInfo = dms.getDevice(deviceId); log.debug("Device Manager associated with the device type '" + deviceId.getType() + "' is null. " +
"Therefore, not attempting method 'getDevice'");
}
return device;
}
Device pluginSpecificInfo = deviceManager.getDevice(deviceId);
if (pluginSpecificInfo != null) { if (pluginSpecificInfo != null) {
device.setFeatures(pluginSpecificInfo.getFeatures()); device.setFeatures(pluginSpecificInfo.getFeatures());
device.setProperties(pluginSpecificInfo.getProperties()); device.setProperties(pluginSpecificInfo.getProperties());
} }
} }
}
return device; return device;
} }
@Override @Override
public boolean updateDeviceInfo(DeviceIdentifier deviceIdentifier, Device device) throws DeviceManagementException { public boolean updateDeviceInfo(DeviceIdentifier deviceId, Device device) throws DeviceManagementException {
DeviceManager dms = DeviceManager deviceManager = this.getDeviceManager(deviceId.getType());
this.getPluginRepository().getDeviceManagementService(device.getType()).getDeviceManager(); if (deviceManager == null) {
return dms.updateDeviceInfo(deviceIdentifier, device); if (log.isDebugEnabled()) {
log.debug("Device Manager associated with the device type '" + deviceId.getType() + "' is null. " +
"Therefore, not attempting method 'updateDeviceInfo'");
}
return false;
}
return deviceManager.updateDeviceInfo(deviceId, device);
} }
@Override @Override
public boolean setOwnership(DeviceIdentifier deviceId, String ownershipType) throws DeviceManagementException { public boolean setOwnership(DeviceIdentifier deviceId, String ownershipType) throws DeviceManagementException {
DeviceManager dms = DeviceManager deviceManager = this.getDeviceManager(deviceId.getType());
this.getPluginRepository().getDeviceManagementService(deviceId.getType()).getDeviceManager(); if (deviceManager == null) {
return dms.setOwnership(deviceId, ownershipType); if (log.isDebugEnabled()) {
log.debug("Device Manager associated with the device type '" + deviceId.getType() + "' is null. " +
"Therefore, not attempting method 'setOwnership'");
}
return false;
}
return deviceManager.setOwnership(deviceId, ownershipType);
} }
@Override @Override
public boolean isClaimable(DeviceIdentifier deviceId) throws DeviceManagementException { public boolean isClaimable(DeviceIdentifier deviceId) throws DeviceManagementException {
DeviceManager dms = DeviceManager deviceManager = this.getDeviceManager(deviceId.getType());
this.getPluginRepository().getDeviceManagementService(deviceId.getType()).getDeviceManager(); if (deviceManager == null) {
return dms.isClaimable(deviceId); if (log.isDebugEnabled()) {
log.debug("Device Manager associated with the device type '" + deviceId.getType() + "' is null. " +
"Therefore, not attempting method 'isClaimable'");
}
return false;
}
return deviceManager.isClaimable(deviceId);
} }
@Override @Override
@ -502,10 +580,16 @@ public class DeviceManagementProviderServiceImpl implements DeviceManagementProv
@Override @Override
public License getLicense(String deviceType, String languageCode) throws DeviceManagementException { public License getLicense(String deviceType, String languageCode) throws DeviceManagementException {
DeviceManager dms = DeviceManager deviceManager = this.getDeviceManager(deviceType);
this.getPluginRepository().getDeviceManagementService(deviceType).getDeviceManager(); if (deviceManager == null) {
if (log.isDebugEnabled()) {
log.debug("Device Manager associated with the device type '" + deviceType + "' is null. " +
"Therefore, not attempting method 'getLicense'");
}
return null;
}
try { try {
return dms.getLicense(languageCode); return deviceManager.getLicense(languageCode);
} catch (LicenseManagementException e) { } catch (LicenseManagementException e) {
throw new DeviceManagementException("Error occurred while retrieving license configured for " + throw new DeviceManagementException("Error occurred while retrieving license configured for " +
"device type '" + deviceType + "' and language code '" + languageCode + "'", e); "device type '" + deviceType + "' and language code '" + languageCode + "'", e);
@ -514,10 +598,16 @@ public class DeviceManagementProviderServiceImpl implements DeviceManagementProv
@Override @Override
public void addLicense(String deviceType, License license) throws DeviceManagementException { public void addLicense(String deviceType, License license) throws DeviceManagementException {
DeviceManager dms = DeviceManager deviceManager = this.getDeviceManager(deviceType);
this.getPluginRepository().getDeviceManagementService(deviceType).getDeviceManager(); if (deviceManager == null) {
if (log.isDebugEnabled()) {
log.debug("Device Manager associated with the device type '" + deviceType + "' is null. " +
"Therefore, not attempting method 'isEnrolled'");
}
return;
}
try { try {
dms.addLicense(license); deviceManager.addLicense(license);
} catch (LicenseManagementException e) { } catch (LicenseManagementException e) {
throw new DeviceManagementException("Error occurred while adding license for " + throw new DeviceManagementException("Error occurred while adding license for " +
"device type '" + deviceType + "'", e); "device type '" + deviceType + "'", e);
@ -569,10 +659,10 @@ public class DeviceManagementProviderServiceImpl implements DeviceManagementProv
@Override @Override
public List<? extends Operation> getOperationsByDeviceAndStatus( public List<? extends Operation> getOperationsByDeviceAndStatus(
DeviceIdentifier identifier, DeviceIdentifier deviceId,
Operation.Status status) throws OperationManagementException, DeviceManagementException { Operation.Status status) throws OperationManagementException, DeviceManagementException {
return DeviceManagementDataHolder.getInstance().getOperationManager().getOperationsByDeviceAndStatus( return DeviceManagementDataHolder.getInstance().getOperationManager().getOperationsByDeviceAndStatus(
identifier, status); deviceId, status);
} }
@Override @Override
@ -595,10 +685,17 @@ public class DeviceManagementProviderServiceImpl implements DeviceManagementProv
} }
for (Device device : userDevices) { for (Device device : userDevices) {
DeviceManager deviceManager = this.getDeviceManager(device.getType());
if (deviceManager == null) {
if (log.isDebugEnabled()) {
log.debug("Device Manager associated with the device type '" + device.getType() + "' is null. " +
"Therefore, not attempting method 'isEnrolled'");
}
devices.add(device);
continue;
}
Device dmsDevice = Device dmsDevice =
this.getPluginRepository().getDeviceManagementService( deviceManager.getDevice(new DeviceIdentifier(device.getDeviceIdentifier(), device.getType()));
device.getType()).getDeviceManager().getDevice(
new DeviceIdentifier(device.getDeviceIdentifier(), device.getType()));
if (dmsDevice != null) { if (dmsDevice != null) {
device.setFeatures(dmsDevice.getFeatures()); device.setFeatures(dmsDevice.getFeatures());
device.setProperties(dmsDevice.getProperties()); device.setProperties(dmsDevice.getProperties());
@ -612,7 +709,6 @@ public class DeviceManagementProviderServiceImpl implements DeviceManagementProv
@Override @Override
public List<Device> getAllDevicesOfRole(String role) throws DeviceManagementException { public List<Device> getAllDevicesOfRole(String role) throws DeviceManagementException {
List<Device> devices = new ArrayList<>(); List<Device> devices = new ArrayList<>();
String[] users; String[] users;
int tenantId = this.getTenantId(); int tenantId = this.getTenantId();
try { try {
@ -756,4 +852,17 @@ public class DeviceManagementProviderServiceImpl implements DeviceManagementProv
return CarbonContext.getThreadLocalCarbonContext().getTenantId(); return CarbonContext.getThreadLocalCarbonContext().getTenantId();
} }
private DeviceManager getDeviceManager(String deviceType) {
DeviceManagementService deviceManagementService =
this.getPluginRepository().getDeviceManagementService(deviceType);
if (deviceManagementService == null) {
if (log.isDebugEnabled()) {
log.debug("Device type '" + deviceType + "' does not have an associated device management " +
"plugin registered within the framework. Therefore, returning null");
}
return null;
}
return deviceManagementService.getDeviceManager();
}
} }

@ -23,6 +23,7 @@ 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.DeviceManagementException; import org.wso2.carbon.device.mgt.common.DeviceManagementException;
import org.wso2.carbon.device.mgt.common.FeatureManager;
import org.wso2.carbon.device.mgt.core.DeviceManagementPluginRepository; import org.wso2.carbon.device.mgt.core.DeviceManagementPluginRepository;
import org.wso2.carbon.device.mgt.core.TestDeviceManagementService; import org.wso2.carbon.device.mgt.core.TestDeviceManagementService;
import org.wso2.carbon.device.mgt.core.common.BaseDeviceManagementTest; import org.wso2.carbon.device.mgt.core.common.BaseDeviceManagementTest;
@ -33,44 +34,62 @@ import org.wso2.carbon.device.mgt.core.util.DeviceManagerUtil;
public class DeviceManagementProviderServiceTest extends BaseDeviceManagementTest { public class DeviceManagementProviderServiceTest extends BaseDeviceManagementTest {
private static final Log log = LogFactory.getLog(DeviceManagementProviderServiceTest.class); private static final Log log = LogFactory.getLog(DeviceManagementProviderServiceTest.class);
DeviceManagementProviderService deviceManagementProviderService = null; private DeviceManagementProviderService providerService;
private static final String NON_EXISTENT_DEVICE_TYPE = "Test";
@BeforeClass @BeforeClass
@Override @Override
public void init() throws Exception { public void init() throws Exception {
initDatSource(); this.initDatSource();
this.providerService = new DeviceManagementProviderServiceImpl();
} }
// @Test
// public void testEnrollment() {
// try {
// DeviceManagementPluginRepository deviceManagementPluginRepository = new DeviceManagementPluginRepository();
// TestDeviceManagementService testDeviceManagementService =
// new TestDeviceManagementService(TestDataHolder.TEST_DEVICE_TYPE);
// deviceManagementPluginRepository.addDeviceManagementProvider(testDeviceManagementService);
//
// deviceManagementProviderService = new DeviceManagementProviderServiceImpl();
// DeviceManagerUtil.registerDeviceType(TestDataHolder.TEST_DEVICE_TYPE);
//
// Device device = TestDataHolder.generateDummyDeviceData(TestDataHolder.TEST_DEVICE_TYPE);
// boolean isEnrolled = deviceManagementProviderService.enrollDevice(device);
//
// Assert.assertEquals(isEnrolled, true, "Enrolment fail");
// if (isEnrolled) {
// TestDataHolder.initialTestDevice = device;
// }
//
// } catch (DeviceManagementException e) {
// String msg = "Error occurred while adding device type '" + TestDataHolder.TEST_DEVICE_TYPE + "'";
// log.error(msg, e);
// Assert.fail(msg, e);
// } finally {
// DeviceManagementDAOFactory.closeConnection();
// }
// }
@Test @Test
public void testEnrollment() { public void testGetFeatureManager() {
try { try {
DeviceManagementPluginRepository deviceManagementPluginRepository = new DeviceManagementPluginRepository(); FeatureManager featureManager = providerService.getFeatureManager(NON_EXISTENT_DEVICE_TYPE);
TestDeviceManagementService testDeviceManagementService = Assert.assertNull(featureManager, "Feature manager retrieved is null, which is expected as the " +
new TestDeviceManagementService(TestDataHolder.TEST_DEVICE_TYPE); "input device type provided is non existent");
deviceManagementPluginRepository.addDeviceManagementProvider(testDeviceManagementService);
deviceManagementProviderService = new DeviceManagementProviderServiceImpl();
DeviceManagerUtil.registerDeviceType(TestDataHolder.TEST_DEVICE_TYPE);
Device device = TestDataHolder.generateDummyDeviceData(TestDataHolder.TEST_DEVICE_TYPE);
boolean isEnrolled = deviceManagementProviderService.enrollDevice(device);
Assert.assertEquals(isEnrolled, true, "Enrolment fail");
if (isEnrolled) {
TestDataHolder.initialTestDevice = device;
}
} catch (DeviceManagementException e) { } catch (DeviceManagementException e) {
String msg = "Error occurred while adding device type '" + TestDataHolder.TEST_DEVICE_TYPE + "'"; String msg = "Error occurred while retrieving feature manager associated with device type '" +
NON_EXISTENT_DEVICE_TYPE + "'";
log.error(msg, e); log.error(msg, e);
Assert.fail(msg, e); Assert.fail(msg, e);
} finally {
DeviceManagementDAOFactory.closeConnection();
} }
} }
@AfterClass @AfterClass
public void cleanResources() { public void cleanResources() {
} }
} }

@ -33,7 +33,7 @@
</test> </test>
<test name="Service Unit Tests" preserve-order="true"> <test name="Service Unit Tests" preserve-order="true">
<classes> <classes>
<!--<class name="org.wso2.carbon.device.mgt.core.service.DeviceManagementProviderServiceTest"/>--> <class name="org.wso2.carbon.device.mgt.core.service.DeviceManagementProviderServiceTest"/>
<class name="org.wso2.carbon.device.mgt.core.app.mgt.ApplicationManagementProviderServiceTest"/> <class name="org.wso2.carbon.device.mgt.core.app.mgt.ApplicationManagementProviderServiceTest"/>
</classes> </classes>
</test> </test>

@ -126,12 +126,12 @@ public class PolicyManagementDAOFactory {
return currentConnection.get(); return currentConnection.get();
} }
public static void closeConnection() throws PolicyManagerDAOException { public static void closeConnection() {
Connection con = currentConnection.get(); Connection con = currentConnection.get();
try { try {
con.close(); con.close();
} catch (SQLException e) { } catch (SQLException e) {
log.error("Error occurred while close the connection"); log.warn("Error occurred while close the connection", e);
} }
currentConnection.remove(); currentConnection.remove();
} }
@ -149,12 +149,10 @@ public class PolicyManagementDAOFactory {
} }
} catch (SQLException e) { } catch (SQLException e) {
throw new PolicyManagerDAOException("Error occurred while committing the transaction", e); throw new PolicyManagerDAOException("Error occurred while committing the transaction", e);
} finally {
closeConnection();
} }
} }
public static void rollbackTransaction() throws PolicyManagerDAOException { public static void rollbackTransaction() {
try { try {
Connection conn = currentConnection.get(); Connection conn = currentConnection.get();
if (conn != null) { if (conn != null) {
@ -166,10 +164,12 @@ public class PolicyManagementDAOFactory {
} }
} }
} catch (SQLException e) { } catch (SQLException e) {
throw new PolicyManagerDAOException("Error occurred while roll-backing the transaction", e); log.warn("Error occurred while roll-backing the transaction", e);
} finally {
closeConnection();
} }
} }
public static void openConnection() throws SQLException {
currentConnection.set(dataSource.getConnection());
}
} }

@ -201,14 +201,8 @@ public class FeatureDAOImpl implements FeatureDAO {
i++; i++;
} }
} catch (SQLException e) { } catch (SQLException | IOException e) {
String msg = "Error occurred while adding the feature list to the database."; throw new FeatureManagerDAOException("Error occurred while adding the feature list to the database.", e);
log.error(msg, e);
throw new FeatureManagerDAOException(msg, e);
} catch (IOException e) {
String msg = "Error occurred while adding the feature list to the database.";
log.error(msg, e);
throw new FeatureManagerDAOException(msg, e);
} finally { } finally {
PolicyManagementDAOUtil.cleanupResources(stmt, generatedKeys); PolicyManagementDAOUtil.cleanupResources(stmt, generatedKeys);
} }
@ -243,14 +237,8 @@ public class FeatureDAOImpl implements FeatureDAO {
} }
stmt.executeBatch(); stmt.executeBatch();
} catch (SQLException e) { } catch (SQLException | IOException e) {
String msg = "Error occurred while adding the feature list to the database."; throw new FeatureManagerDAOException("Error occurred while adding the feature list to the database.", e);
log.error(msg, e);
throw new FeatureManagerDAOException(msg, e);
} catch (IOException e) {
String msg = "Error occurred while adding the feature list to the database.";
log.error(msg, e);
throw new FeatureManagerDAOException(msg, e);
} finally { } finally {
PolicyManagementDAOUtil.cleanupResources(stmt, null); PolicyManagementDAOUtil.cleanupResources(stmt, null);
} }
@ -274,9 +262,7 @@ public class FeatureDAOImpl implements FeatureDAO {
return true; return true;
} catch (SQLException e) { } catch (SQLException e) {
String msg = "Error occurred while deleting the feature related to a profile."; throw new FeatureManagerDAOException("Error occurred while deleting the feature related to a profile.", e);
log.error(msg);
throw new FeatureManagerDAOException(msg, e);
} finally { } finally {
PolicyManagementDAOUtil.cleanupResources(stmt, null); PolicyManagementDAOUtil.cleanupResources(stmt, null);
} }
@ -298,18 +284,14 @@ public class FeatureDAOImpl implements FeatureDAO {
return true; return true;
} catch (SQLException e) { } catch (SQLException e) {
String msg = "Error occurred while deleting the feature related to a profile."; throw new FeatureManagerDAOException("Error occurred while deleting the feature related to a profile.", e);
log.error(msg);
throw new FeatureManagerDAOException(msg, e);
} finally { } finally {
PolicyManagementDAOUtil.cleanupResources(stmt, null); PolicyManagementDAOUtil.cleanupResources(stmt, null);
} }
} }
@Override @Override
public List<ProfileFeature> getAllProfileFeatures() throws FeatureManagerDAOException { public List<ProfileFeature> getAllProfileFeatures() throws FeatureManagerDAOException {
Connection conn; Connection conn;
PreparedStatement stmt = null; PreparedStatement stmt = null;
ResultSet resultSet = null; ResultSet resultSet = null;
@ -359,34 +341,24 @@ public class FeatureDAOImpl implements FeatureDAO {
featureList.add(profileFeature); featureList.add(profileFeature);
} }
} catch (SQLException e) { } catch (SQLException e) {
String msg = "Unable to get the list of the features from database."; throw new FeatureManagerDAOException("Unable to get the list of the features from database.", e);
log.error(msg);
throw new FeatureManagerDAOException(msg, e);
} catch (IOException e) { } catch (IOException e) {
String msg = "Unable to read the byte stream for content"; throw new FeatureManagerDAOException("Unable to read the byte stream for content", e);
log.error(msg);
throw new FeatureManagerDAOException(msg, e);
} catch (ClassNotFoundException e) { } catch (ClassNotFoundException e) {
String msg = "Class not found while converting the object"; throw new FeatureManagerDAOException("Class not found while converting the object", e);
log.error(msg);
throw new FeatureManagerDAOException(msg, e);
} finally { } finally {
PolicyManagementDAOUtil.cleanupResources(stmt, resultSet); PolicyManagementDAOUtil.cleanupResources(stmt, resultSet);
this.closeConnection();
} }
return featureList; return featureList;
} }
@Override @Override
public List<Feature> getAllFeatures(String deviceType) throws FeatureManagerDAOException { public List<Feature> getAllFeatures(String deviceType) throws FeatureManagerDAOException {
Connection conn; Connection conn;
PreparedStatement stmt = null; PreparedStatement stmt = null;
ResultSet resultSet = null; ResultSet resultSet = null;
List<Feature> featureList = new ArrayList<Feature>(); List<Feature> featureList = new ArrayList<Feature>();
try { try {
conn = this.getConnection(); conn = this.getConnection();
String query = "SELECT f.ID ID, f.NAME NAME, f.CODE CODE, f.DEVICE_TYPE_ID DEVICE_TYPE_ID," + String query = "SELECT f.ID ID, f.NAME NAME, f.CODE CODE, f.DEVICE_TYPE_ID DEVICE_TYPE_ID," +
@ -404,21 +376,17 @@ public class FeatureDAOImpl implements FeatureDAO {
feature.setName(resultSet.getString("NAME")); feature.setName(resultSet.getString("NAME"));
featureList.add(feature); featureList.add(feature);
} }
} catch (SQLException e) { } catch (SQLException e) {
String msg = "Unable to get the list of the features related device type from database."; throw new FeatureManagerDAOException("Unable to get the list of the features related device type " +
log.error(msg); "from database.", e);
throw new FeatureManagerDAOException(msg, e);
} finally { } finally {
PolicyManagementDAOUtil.cleanupResources(stmt, resultSet); PolicyManagementDAOUtil.cleanupResources(stmt, resultSet);
this.closeConnection();
} }
return featureList; return featureList;
} }
@Override @Override
public List<ProfileFeature> getFeaturesForProfile(int profileId) throws FeatureManagerDAOException { public List<ProfileFeature> getFeaturesForProfile(int profileId) throws FeatureManagerDAOException {
Connection conn; Connection conn;
PreparedStatement stmt = null; PreparedStatement stmt = null;
ResultSet resultSet = null; ResultSet resultSet = null;
@ -464,25 +432,16 @@ public class FeatureDAOImpl implements FeatureDAO {
} }
} }
} }
featureList.add(profileFeature); featureList.add(profileFeature);
} }
} catch (SQLException e) { } catch (SQLException e) {
String msg = "Unable to get the list of the features from database."; throw new FeatureManagerDAOException("Unable to get the list of the features from database.", e);
log.error(msg);
throw new FeatureManagerDAOException(msg, e);
} catch (IOException e) { } catch (IOException e) {
String msg = "Unable to read the byte stream for content"; throw new FeatureManagerDAOException("Unable to read the byte stream for content", e);
log.error(msg);
throw new FeatureManagerDAOException(msg, e);
} catch (ClassNotFoundException e) { } catch (ClassNotFoundException e) {
String msg = "Class not found while converting the object"; throw new FeatureManagerDAOException("Class not found while converting the object", e);
log.error(msg);
throw new FeatureManagerDAOException(msg, e);
} finally { } finally {
PolicyManagementDAOUtil.cleanupResources(stmt, resultSet); PolicyManagementDAOUtil.cleanupResources(stmt, resultSet);
this.closeConnection();
} }
return featureList; return featureList;
} }
@ -504,9 +463,8 @@ public class FeatureDAOImpl implements FeatureDAO {
return true; return true;
} catch (SQLException e) { } catch (SQLException e) {
String msg = "Unable to delete the feature " + featureId + " (Feature ID) from database."; throw new FeatureManagerDAOException("Unable to delete the feature " + featureId + " (Feature ID) " +
log.error(msg); "from database.", e);
throw new FeatureManagerDAOException(msg, e);
} finally { } finally {
PolicyManagementDAOUtil.cleanupResources(stmt, null); PolicyManagementDAOUtil.cleanupResources(stmt, null);
} }
@ -522,12 +480,4 @@ public class FeatureDAOImpl implements FeatureDAO {
} }
} }
private void closeConnection() {
try {
PolicyManagementDAOFactory.closeConnection();
} catch (PolicyManagerDAOException e) {
log.warn("Unable to close the database connection.");
}
}
} }

@ -258,7 +258,6 @@ public class MonitoringDAOImpl implements MonitoringDAO {
throw new MonitoringDAOException(msg, e); throw new MonitoringDAOException(msg, e);
} finally { } finally {
PolicyManagementDAOUtil.cleanupResources(stmt, resultSet); PolicyManagementDAOUtil.cleanupResources(stmt, resultSet);
this.closeConnection();
} }
} }
@ -303,7 +302,6 @@ public class MonitoringDAOImpl implements MonitoringDAO {
throw new MonitoringDAOException(msg, e); throw new MonitoringDAOException(msg, e);
} finally { } finally {
PolicyManagementDAOUtil.cleanupResources(stmt, resultSet); PolicyManagementDAOUtil.cleanupResources(stmt, resultSet);
this.closeConnection();
} }
} }
@ -340,9 +338,7 @@ public class MonitoringDAOImpl implements MonitoringDAO {
throw new MonitoringDAOException(msg, e); throw new MonitoringDAOException(msg, e);
} finally { } finally {
PolicyManagementDAOUtil.cleanupResources(stmt, resultSet); PolicyManagementDAOUtil.cleanupResources(stmt, resultSet);
this.closeConnection();
} }
} }
@Override @Override
@ -453,13 +449,7 @@ public class MonitoringDAOImpl implements MonitoringDAO {
} }
private void closeConnection() {
try {
PolicyManagementDAOFactory.closeConnection();
} catch (PolicyManagerDAOException e) {
log.warn("Unable to close the database connection.");
}
}
} }

@ -51,10 +51,8 @@ public class PolicyDAOImpl implements PolicyDAO {
@Override @Override
public Policy addPolicy(String deviceType, Policy policy) throws PolicyManagerDAOException { public Policy addPolicy(String deviceType, Policy policy) throws PolicyManagerDAOException {
Connection conn; Connection conn;
PreparedStatement stmt = null; PreparedStatement stmt = null;
try { try {
conn = this.getConnection(); conn = this.getConnection();
String query = "INSERT INTO DM_DEVICE_TYPE_POLICY (DEVICE_TYPE_ID, POLICY_ID) VALUES (?, ?)"; String query = "INSERT INTO DM_DEVICE_TYPE_POLICY (DEVICE_TYPE_ID, POLICY_ID) VALUES (?, ?)";
@ -62,11 +60,8 @@ public class PolicyDAOImpl implements PolicyDAO {
stmt.setInt(1, getDeviceTypeId(deviceType)); stmt.setInt(1, getDeviceTypeId(deviceType));
stmt.setInt(2, policy.getId()); stmt.setInt(2, policy.getId());
stmt.executeQuery(); stmt.executeQuery();
} catch (SQLException e) { } catch (SQLException e) {
String msg = "Error occurred while adding the device type policy to database."; throw new PolicyManagerDAOException("Error occurred while adding the device type policy to database.", e);
log.error(msg, e);
throw new PolicyManagerDAOException(msg, e);
} finally { } finally {
PolicyManagementDAOUtil.cleanupResources(stmt, null); PolicyManagementDAOUtil.cleanupResources(stmt, null);
} }
@ -76,10 +71,8 @@ public class PolicyDAOImpl implements PolicyDAO {
@Override @Override
public Policy addPolicyToRole(List<String> roleNames, Policy policy) throws PolicyManagerDAOException { public Policy addPolicyToRole(List<String> roleNames, Policy policy) throws PolicyManagerDAOException {
Connection conn; Connection conn;
PreparedStatement stmt = null; PreparedStatement stmt = null;
try { try {
conn = this.getConnection(); conn = this.getConnection();
String query = "INSERT INTO DM_ROLE_POLICY (ROLE_NAME, POLICY_ID) VALUES (?, ?)"; String query = "INSERT INTO DM_ROLE_POLICY (ROLE_NAME, POLICY_ID) VALUES (?, ?)";
@ -90,11 +83,8 @@ public class PolicyDAOImpl implements PolicyDAO {
stmt.addBatch(); stmt.addBatch();
} }
stmt.executeBatch(); stmt.executeBatch();
} catch (SQLException e) { } catch (SQLException e) {
String msg = "Error occurred while adding the role name with policy to database."; throw new PolicyManagerDAOException("Error occurred while adding the role name with policy to database", e);
log.error(msg, e);
throw new PolicyManagerDAOException(msg, e);
} finally { } finally {
PolicyManagementDAOUtil.cleanupResources(stmt, null); PolicyManagementDAOUtil.cleanupResources(stmt, null);
} }
@ -103,10 +93,8 @@ public class PolicyDAOImpl implements PolicyDAO {
@Override @Override
public Policy addPolicyToUser(List<String> usernameList, Policy policy) throws PolicyManagerDAOException { public Policy addPolicyToUser(List<String> usernameList, Policy policy) throws PolicyManagerDAOException {
Connection conn; Connection conn;
PreparedStatement stmt = null; PreparedStatement stmt = null;
try { try {
conn = this.getConnection(); conn = this.getConnection();
String query = "INSERT INTO DM_USER_POLICY (POLICY_ID, USERNAME) VALUES (?, ?)"; String query = "INSERT INTO DM_USER_POLICY (POLICY_ID, USERNAME) VALUES (?, ?)";
@ -117,11 +105,8 @@ public class PolicyDAOImpl implements PolicyDAO {
stmt.addBatch(); stmt.addBatch();
} }
stmt.executeBatch(); stmt.executeBatch();
} catch (SQLException e) { } catch (SQLException e) {
String msg = "Error occurred while adding the user name with policy to database."; throw new PolicyManagerDAOException("Error occurred while adding the user name with policy to database", e);
log.error(msg, e);
throw new PolicyManagerDAOException(msg, e);
} finally { } finally {
PolicyManagementDAOUtil.cleanupResources(stmt, null); PolicyManagementDAOUtil.cleanupResources(stmt, null);
} }
@ -130,10 +115,8 @@ public class PolicyDAOImpl implements PolicyDAO {
@Override @Override
public Policy addPolicyToDevice(List<Device> devices, Policy policy) throws PolicyManagerDAOException { public Policy addPolicyToDevice(List<Device> devices, Policy policy) throws PolicyManagerDAOException {
Connection conn; Connection conn;
PreparedStatement stmt = null; PreparedStatement stmt = null;
try { try {
conn = this.getConnection(); conn = this.getConnection();
String query = "INSERT INTO DM_DEVICE_POLICY (DEVICE_ID, POLICY_ID) VALUES (?, ?)"; String query = "INSERT INTO DM_DEVICE_POLICY (DEVICE_ID, POLICY_ID) VALUES (?, ?)";
@ -145,9 +128,8 @@ public class PolicyDAOImpl implements PolicyDAO {
} }
stmt.executeBatch(); stmt.executeBatch();
} catch (SQLException e) { } catch (SQLException e) {
String msg = "Error occurred while adding the device ids with policy to database."; throw new PolicyManagerDAOException("Error occurred while adding the device ids with policy to " +
log.error(msg, e); "database", e);
throw new PolicyManagerDAOException(msg, e);
} finally { } finally {
PolicyManagementDAOUtil.cleanupResources(stmt, null); PolicyManagementDAOUtil.cleanupResources(stmt, null);
} }
@ -156,7 +138,6 @@ public class PolicyDAOImpl implements PolicyDAO {
@Override @Override
public boolean updatePolicyPriorities(List<Policy> policies) throws PolicyManagerDAOException { public boolean updatePolicyPriorities(List<Policy> policies) throws PolicyManagerDAOException {
Connection conn; Connection conn;
PreparedStatement stmt = null; PreparedStatement stmt = null;
int tenantId = PrivilegedCarbonContext.getThreadLocalCarbonContext().getTenantId(); int tenantId = PrivilegedCarbonContext.getThreadLocalCarbonContext().getTenantId();
@ -173,11 +154,8 @@ public class PolicyDAOImpl implements PolicyDAO {
stmt.addBatch(); stmt.addBatch();
} }
stmt.executeBatch(); stmt.executeBatch();
} catch (SQLException e) { } catch (SQLException e) {
String msg = "Error occurred while updating policy priorities in database."; throw new PolicyManagerDAOException("Error occurred while updating policy priorities in database", e);
log.error(msg, e);
throw new PolicyManagerDAOException(msg, e);
} finally { } finally {
PolicyManagementDAOUtil.cleanupResources(stmt, null); PolicyManagementDAOUtil.cleanupResources(stmt, null);
} }
@ -202,15 +180,12 @@ public class PolicyDAOImpl implements PolicyDAO {
stmt.executeUpdate(); stmt.executeUpdate();
generatedKeys = stmt.getGeneratedKeys(); generatedKeys = stmt.getGeneratedKeys();
while (generatedKeys.next()) { while (generatedKeys.next()) {
criteria.setId(generatedKeys.getInt(1)); criteria.setId(generatedKeys.getInt(1));
} }
} catch (SQLException e) { } catch (SQLException e) {
String msg = "Error occurred while inserting the criterion (" + criteria.getName() + ") to database."; throw new PolicyManagerDAOException("Error occurred while inserting the criterion (" + criteria.getName() +
log.error(msg, e); ") to database.", e);
throw new PolicyManagerDAOException(msg, e);
} finally { } finally {
PolicyManagementDAOUtil.cleanupResources(stmt, null); PolicyManagementDAOUtil.cleanupResources(stmt, null);
} }
@ -219,11 +194,9 @@ public class PolicyDAOImpl implements PolicyDAO {
@Override @Override
public Criterion updateCriterion(Criterion criteria) throws PolicyManagerDAOException { public Criterion updateCriterion(Criterion criteria) throws PolicyManagerDAOException {
Connection conn; Connection conn;
PreparedStatement stmt = null; PreparedStatement stmt = null;
int tenantId = PrivilegedCarbonContext.getThreadLocalCarbonContext().getTenantId(); int tenantId = PrivilegedCarbonContext.getThreadLocalCarbonContext().getTenantId();
try { try {
conn = this.getConnection(); conn = this.getConnection();
String query = "UPDATE DM_CRITERIA SET NAME = ? WHERE ID = ? AND TENANT_ID = ?"; String query = "UPDATE DM_CRITERIA SET NAME = ? WHERE ID = ? AND TENANT_ID = ?";
@ -232,11 +205,9 @@ public class PolicyDAOImpl implements PolicyDAO {
stmt.setInt(2, criteria.getId()); stmt.setInt(2, criteria.getId());
stmt.setInt(3, tenantId); stmt.setInt(3, tenantId);
stmt.executeUpdate(); stmt.executeUpdate();
} catch (SQLException e) { } catch (SQLException e) {
String msg = "Error occurred while inserting the criterion (" + criteria.getName() + ") to database."; throw new PolicyManagerDAOException("Error occurred while inserting the criterion (" + criteria.getName() +
log.error(msg, e); ") to database", e);
throw new PolicyManagerDAOException(msg, e);
} finally { } finally {
PolicyManagementDAOUtil.cleanupResources(stmt, null); PolicyManagementDAOUtil.cleanupResources(stmt, null);
} }
@ -245,7 +216,6 @@ public class PolicyDAOImpl implements PolicyDAO {
@Override @Override
public Criterion getCriterion(int id) throws PolicyManagerDAOException { public Criterion getCriterion(int id) throws PolicyManagerDAOException {
Connection conn; Connection conn;
PreparedStatement stmt = null; PreparedStatement stmt = null;
ResultSet resultSet = null; ResultSet resultSet = null;
@ -265,26 +235,20 @@ public class PolicyDAOImpl implements PolicyDAO {
criterion.setName(resultSet.getString("NAME")); criterion.setName(resultSet.getString("NAME"));
} }
return criterion; return criterion;
} catch (SQLException e) { } catch (SQLException e) {
String msg = "Error occurred while reading the policies from the database."; throw new PolicyManagerDAOException("Error occurred while reading the policies from the database", e);
log.error(msg, e);
throw new PolicyManagerDAOException(msg, e);
} finally { } finally {
PolicyManagementDAOUtil.cleanupResources(stmt, resultSet); PolicyManagementDAOUtil.cleanupResources(stmt, resultSet);
this.closeConnection();
} }
} }
@Override @Override
public Criterion getCriterion(String name) throws PolicyManagerDAOException { public Criterion getCriterion(String name) throws PolicyManagerDAOException {
Connection conn; Connection conn;
PreparedStatement stmt = null; PreparedStatement stmt = null;
ResultSet resultSet = null; ResultSet resultSet = null;
Criterion criterion = new Criterion(); Criterion criterion = new Criterion();
int tenantId = PrivilegedCarbonContext.getThreadLocalCarbonContext().getTenantId(); int tenantId = PrivilegedCarbonContext.getThreadLocalCarbonContext().getTenantId();
try { try {
conn = this.getConnection(); conn = this.getConnection();
String query = "SELECT * FROM DM_CRITERIA WHERE NAME= ? AND TENANT_ID = ?"; String query = "SELECT * FROM DM_CRITERIA WHERE NAME= ? AND TENANT_ID = ?";
@ -298,26 +262,20 @@ public class PolicyDAOImpl implements PolicyDAO {
criterion.setName(resultSet.getString("NAME")); criterion.setName(resultSet.getString("NAME"));
} }
return criterion; return criterion;
} catch (SQLException e) { } catch (SQLException e) {
String msg = "Error occurred while reading the policies from the database."; throw new PolicyManagerDAOException("Error occurred while reading the policies from the database", e);
log.error(msg, e);
throw new PolicyManagerDAOException(msg, e);
} finally { } finally {
PolicyManagementDAOUtil.cleanupResources(stmt, resultSet); PolicyManagementDAOUtil.cleanupResources(stmt, resultSet);
this.closeConnection();
} }
} }
@Override @Override
public boolean checkCriterionExists(String name) throws PolicyManagerDAOException { public boolean checkCriterionExists(String name) throws PolicyManagerDAOException {
Connection conn; Connection conn;
PreparedStatement stmt = null; PreparedStatement stmt = null;
ResultSet resultSet = null; ResultSet resultSet = null;
int tenantId = PrivilegedCarbonContext.getThreadLocalCarbonContext().getTenantId(); int tenantId = PrivilegedCarbonContext.getThreadLocalCarbonContext().getTenantId();
boolean exist = false; boolean exist = false;
try { try {
conn = this.getConnection(); conn = this.getConnection();
String query = "SELECT * FROM DM_CRITERIA WHERE NAME = ? AND TENANT_ID = ?"; String query = "SELECT * FROM DM_CRITERIA WHERE NAME = ? AND TENANT_ID = ?";
@ -325,25 +283,24 @@ public class PolicyDAOImpl implements PolicyDAO {
stmt.setString(1, name); stmt.setString(1, name);
stmt.setInt(2, tenantId); stmt.setInt(2, tenantId);
resultSet = stmt.executeQuery(); resultSet = stmt.executeQuery();
exist = resultSet.next();
if (resultSet.next()) {
//TODO: FIXME
exist = resultSet.getBoolean(1);
}
} catch (SQLException e) { } catch (SQLException e) {
String msg = "Error occurred while checking whether criterion (" + name + ") exists."; throw new PolicyManagerDAOException("Error occurred while checking whether criterion (" + name +
log.error(msg, e); ") exists", e);
throw new PolicyManagerDAOException(msg, e);
} finally { } finally {
PolicyManagementDAOUtil.cleanupResources(stmt, resultSet); PolicyManagementDAOUtil.cleanupResources(stmt, resultSet);
this.closeConnection();
} }
return exist; return exist;
} }
@Override @Override
public boolean deleteCriterion(Criterion criteria) throws PolicyManagerDAOException { public boolean deleteCriterion(Criterion criteria) throws PolicyManagerDAOException {
Connection conn; Connection conn;
PreparedStatement stmt = null; PreparedStatement stmt = null;
try { try {
conn = this.getConnection(); conn = this.getConnection();
String query = "DELETE FROM DM_CRITERIA WHERE ID = ?"; String query = "DELETE FROM DM_CRITERIA WHERE ID = ?";
@ -356,9 +313,8 @@ public class PolicyDAOImpl implements PolicyDAO {
} }
return true; return true;
} catch (SQLException e) { } catch (SQLException e) {
String msg = "Unable to delete the policy (" + criteria.getName() + ") from database."; throw new PolicyManagerDAOException("Unable to delete the policy (" + criteria.getName() +
log.error(msg); ") from database", e);
throw new PolicyManagerDAOException(msg, e);
} finally { } finally {
PolicyManagementDAOUtil.cleanupResources(stmt, null); PolicyManagementDAOUtil.cleanupResources(stmt, null);
} }
@ -366,13 +322,11 @@ public class PolicyDAOImpl implements PolicyDAO {
@Override @Override
public List<Criterion> getAllPolicyCriteria() throws PolicyManagerDAOException { public List<Criterion> getAllPolicyCriteria() throws PolicyManagerDAOException {
Connection conn; Connection conn;
PreparedStatement stmt = null; PreparedStatement stmt = null;
ResultSet resultSet = null; ResultSet resultSet = null;
List<Criterion> criteria = new ArrayList<Criterion>(); List<Criterion> criteria = new ArrayList<Criterion>();
int tenantId = PrivilegedCarbonContext.getThreadLocalCarbonContext().getTenantId(); int tenantId = PrivilegedCarbonContext.getThreadLocalCarbonContext().getTenantId();
try { try {
conn = this.getConnection(); conn = this.getConnection();
String query = "SELECT * FROM DM_CRITERIA WHERE TENANT_ID = ?"; String query = "SELECT * FROM DM_CRITERIA WHERE TENANT_ID = ?";
@ -387,14 +341,10 @@ public class PolicyDAOImpl implements PolicyDAO {
criteria.add(criterion); criteria.add(criterion);
} }
return criteria; return criteria;
} catch (SQLException e) { } catch (SQLException e) {
String msg = "Error occurred while reading the policies from the database."; throw new PolicyManagerDAOException("Error occurred while reading the policies from the database", e);
log.error(msg, e);
throw new PolicyManagerDAOException(msg, e);
} finally { } finally {
PolicyManagementDAOUtil.cleanupResources(stmt, resultSet); PolicyManagementDAOUtil.cleanupResources(stmt, resultSet);
this.closeConnection();
} }
} }
@ -441,11 +391,8 @@ public class PolicyDAOImpl implements PolicyDAO {
@Override @Override
public boolean addPolicyCriteriaProperties(List<PolicyCriterion> policyCriteria) throws PolicyManagerDAOException { public boolean addPolicyCriteriaProperties(List<PolicyCriterion> policyCriteria) throws PolicyManagerDAOException {
Connection conn; Connection conn;
PreparedStatement stmt = null; PreparedStatement stmt = null;
try { try {
conn = this.getConnection(); conn = this.getConnection();
String query = "INSERT INTO DM_POLICY_CRITERIA_PROPERTIES (POLICY_CRITERION_ID, PROP_KEY, PROP_VALUE, " + String query = "INSERT INTO DM_POLICY_CRITERIA_PROPERTIES (POLICY_CRITERION_ID, PROP_KEY, PROP_VALUE, " +
@ -453,7 +400,6 @@ public class PolicyDAOImpl implements PolicyDAO {
stmt = conn.prepareStatement(query); stmt = conn.prepareStatement(query);
for (PolicyCriterion criterion : policyCriteria) { for (PolicyCriterion criterion : policyCriteria) {
Properties prop = criterion.getProperties(); Properties prop = criterion.getProperties();
for (String name : prop.stringPropertyNames()) { for (String name : prop.stringPropertyNames()) {
@ -467,30 +413,21 @@ public class PolicyDAOImpl implements PolicyDAO {
} }
// stmt.executeUpdate(); // stmt.executeUpdate();
} catch (SQLException e) { } catch (SQLException | IOException e) {
String msg = "Error occurred while inserting the criterion properties to database."; throw new PolicyManagerDAOException("Error occurred while inserting the criterion properties " +
log.error(msg, e); "to database", e);
throw new PolicyManagerDAOException(msg, e);
} catch (IOException e) {
String msg = "Error occurred while inserting the criterion properties to database.";
log.error(msg, e);
throw new PolicyManagerDAOException(msg, e);
} finally { } finally {
PolicyManagementDAOUtil.cleanupResources(stmt, null); PolicyManagementDAOUtil.cleanupResources(stmt, null);
} }
return false; return false;
} }
@Override @Override
public List<PolicyCriterion> getPolicyCriteria(int policyId) throws PolicyManagerDAOException { public List<PolicyCriterion> getPolicyCriteria(int policyId) throws PolicyManagerDAOException {
Connection conn; Connection conn;
PreparedStatement stmt = null; PreparedStatement stmt = null;
ResultSet resultSet = null; ResultSet resultSet = null;
List<PolicyCriterion> criteria = new ArrayList<PolicyCriterion>(); List<PolicyCriterion> criteria = new ArrayList<PolicyCriterion>();
try { try {
conn = this.getConnection(); conn = this.getConnection();
String query = "SELECT DPC.ID, DPC.CRITERIA_ID, DPCP.PROP_KEY, DPCP.PROP_VALUE, DPCP.CONTENT FROM " + String query = "SELECT DPC.ID, DPC.CRITERIA_ID, DPCP.PROP_KEY, DPCP.PROP_VALUE, DPCP.CONTENT FROM " +
@ -522,14 +459,11 @@ public class PolicyDAOImpl implements PolicyDAO {
prop.setProperty(resultSet.getString("PROP_KEY"), resultSet.getString("PROP_VALUE")); prop.setProperty(resultSet.getString("PROP_KEY"), resultSet.getString("PROP_VALUE"));
} }
} }
} catch (SQLException e) { } catch (SQLException e) {
String msg = "Error occurred while reading the criteria related to policies from the database."; throw new PolicyManagerDAOException("Error occurred while reading the criteria related to policies from " +
log.error(msg, e); "the database", e);
throw new PolicyManagerDAOException(msg, e);
} finally { } finally {
PolicyManagementDAOUtil.cleanupResources(stmt, resultSet); PolicyManagementDAOUtil.cleanupResources(stmt, resultSet);
this.closeConnection();
} }
return criteria; return criteria;
} }
@ -555,9 +489,8 @@ public class PolicyDAOImpl implements PolicyDAO {
stmt.executeUpdate(); stmt.executeUpdate();
} catch (SQLException e) { } catch (SQLException e) {
String msg = "Error occurred while updating policy (" + policy.getPolicyName() + ") in database."; throw new PolicyManagerDAOException("Error occurred while updating policy (" + policy.getPolicyName() +
log.error(msg, e); ") in database", e);
throw new PolicyManagerDAOException(msg, e);
} finally { } finally {
PolicyManagementDAOUtil.cleanupResources(stmt, null); PolicyManagementDAOUtil.cleanupResources(stmt, null);
} }
@ -566,13 +499,11 @@ public class PolicyDAOImpl implements PolicyDAO {
@Override @Override
public Policy getPolicy(int policyId) throws PolicyManagerDAOException { public Policy getPolicy(int policyId) throws PolicyManagerDAOException {
Connection conn; Connection conn;
PreparedStatement stmt = null; PreparedStatement stmt = null;
ResultSet resultSet = null; ResultSet resultSet = null;
Policy policy = new Policy(); Policy policy = new Policy();
int tenantId = PrivilegedCarbonContext.getThreadLocalCarbonContext().getTenantId(); int tenantId = PrivilegedCarbonContext.getThreadLocalCarbonContext().getTenantId();
try { try {
conn = this.getConnection(); conn = this.getConnection();
String query = "SELECT * FROM DM_POLICY WHERE ID= ? AND TENANT_ID = ? "; String query = "SELECT * FROM DM_POLICY WHERE ID= ? AND TENANT_ID = ? ";
@ -592,19 +523,15 @@ public class PolicyDAOImpl implements PolicyDAO {
return policy; return policy;
} catch (SQLException e) { } catch (SQLException e) {
String msg = "Error occurred while reading the policies from the database."; throw new PolicyManagerDAOException("Error occurred while reading the policies from the database", e);
log.error(msg, e);
throw new PolicyManagerDAOException(msg, e);
} finally { } finally {
PolicyManagementDAOUtil.cleanupResources(stmt, resultSet); PolicyManagementDAOUtil.cleanupResources(stmt, resultSet);
this.closeConnection();
} }
} }
@Override @Override
public Policy getPolicyByProfileID(int profileId) throws PolicyManagerDAOException { public Policy getPolicyByProfileID(int profileId) throws PolicyManagerDAOException {
Connection conn; Connection conn;
PreparedStatement stmt = null; PreparedStatement stmt = null;
ResultSet resultSet = null; ResultSet resultSet = null;
@ -620,7 +547,6 @@ public class PolicyDAOImpl implements PolicyDAO {
resultSet = stmt.executeQuery(); resultSet = stmt.executeQuery();
while (resultSet.next()) { while (resultSet.next()) {
policy.setId(resultSet.getInt("ID")); policy.setId(resultSet.getInt("ID"));
policy.setPolicyName(resultSet.getString("NAME")); policy.setPolicyName(resultSet.getString("NAME"));
policy.setTenantId(resultSet.getInt("TENANT_ID")); policy.setTenantId(resultSet.getInt("TENANT_ID"));
@ -628,20 +554,15 @@ public class PolicyDAOImpl implements PolicyDAO {
policy.setCompliance(resultSet.getString("COMPLIANCE")); policy.setCompliance(resultSet.getString("COMPLIANCE"));
} }
return policy; return policy;
} catch (SQLException e) { } catch (SQLException e) {
String msg = "Error occurred while reading the policies from the database."; throw new PolicyManagerDAOException("Error occurred while reading the policies from the database", e);
log.error(msg, e);
throw new PolicyManagerDAOException(msg, e);
} finally { } finally {
PolicyManagementDAOUtil.cleanupResources(stmt, resultSet); PolicyManagementDAOUtil.cleanupResources(stmt, resultSet);
this.closeConnection();
} }
} }
@Override @Override
public List<Policy> getAllPolicies() throws PolicyManagerDAOException { public List<Policy> getAllPolicies() throws PolicyManagerDAOException {
Connection conn; Connection conn;
PreparedStatement stmt = null; PreparedStatement stmt = null;
ResultSet resultSet = null; ResultSet resultSet = null;
@ -657,7 +578,6 @@ public class PolicyDAOImpl implements PolicyDAO {
while (resultSet.next()) { while (resultSet.next()) {
Policy policy = new Policy(); Policy policy = new Policy();
policy.setId(resultSet.getInt("ID")); policy.setId(resultSet.getInt("ID"));
policy.setProfileId(resultSet.getInt("PROFILE_ID")); policy.setProfileId(resultSet.getInt("PROFILE_ID"));
policy.setPolicyName(resultSet.getString("NAME")); policy.setPolicyName(resultSet.getString("NAME"));
@ -668,14 +588,10 @@ public class PolicyDAOImpl implements PolicyDAO {
policies.add(policy); policies.add(policy);
} }
return policies; return policies;
} catch (SQLException e) { } catch (SQLException e) {
String msg = "Error occurred while reading the policies from the database."; throw new PolicyManagerDAOException("Error occurred while reading the policies from the database", e);
log.error(msg, e);
throw new PolicyManagerDAOException(msg, e);
} finally { } finally {
PolicyManagementDAOUtil.cleanupResources(stmt, resultSet); PolicyManagementDAOUtil.cleanupResources(stmt, resultSet);
this.closeConnection();
} }
} }
@ -686,12 +602,10 @@ public class PolicyDAOImpl implements PolicyDAO {
@Override @Override
public List<Integer> getPolicyAppliedDevicesIds(int policyId) throws PolicyManagerDAOException { public List<Integer> getPolicyAppliedDevicesIds(int policyId) throws PolicyManagerDAOException {
Connection conn; Connection conn;
PreparedStatement stmt = null; PreparedStatement stmt = null;
ResultSet resultSet = null; ResultSet resultSet = null;
List<Integer> deviceIdList = new ArrayList<Integer>(); List<Integer> deviceIdList = new ArrayList<Integer>();
try { try {
conn = this.getConnection(); conn = this.getConnection();
String query = "SELECT * FROM DM_DEVICE_POLICY WHERE POLICY_ID = ?"; String query = "SELECT * FROM DM_DEVICE_POLICY WHERE POLICY_ID = ?";
@ -700,28 +614,21 @@ public class PolicyDAOImpl implements PolicyDAO {
resultSet = stmt.executeQuery(); resultSet = stmt.executeQuery();
while (resultSet.next()) { while (resultSet.next()) {
deviceIdList.add(resultSet.getInt("DEVICE_ID")); deviceIdList.add(resultSet.getInt("DEVICE_ID"));
} }
return deviceIdList; return deviceIdList;
} catch (SQLException e) { } catch (SQLException e) {
String msg = "Error occurred while getting the device related to policies."; throw new PolicyManagerDAOException("Error occurred while getting the device related to policies", e);
log.error(msg, e);
throw new PolicyManagerDAOException(msg, e);
} finally { } finally {
PolicyManagementDAOUtil.cleanupResources(stmt, resultSet); PolicyManagementDAOUtil.cleanupResources(stmt, resultSet);
this.closeConnection();
} }
} }
public List<String> getPolicyAppliedRoles(int policyId) throws PolicyManagerDAOException { public List<String> getPolicyAppliedRoles(int policyId) throws PolicyManagerDAOException {
Connection conn; Connection conn;
PreparedStatement stmt = null; PreparedStatement stmt = null;
ResultSet resultSet = null; ResultSet resultSet = null;
List<String> roleNames = new ArrayList<String>(); List<String> roleNames = new ArrayList<String>();
try { try {
conn = this.getConnection(); conn = this.getConnection();
@ -731,18 +638,13 @@ public class PolicyDAOImpl implements PolicyDAO {
resultSet = stmt.executeQuery(); resultSet = stmt.executeQuery();
while (resultSet.next()) { while (resultSet.next()) {
roleNames.add(resultSet.getString("ROLE_NAME")); roleNames.add(resultSet.getString("ROLE_NAME"));
} }
return roleNames; return roleNames;
} catch (SQLException e) { } catch (SQLException e) {
String msg = "Error occurred while getting the roles related to policies."; throw new PolicyManagerDAOException("Error occurred while getting the roles related to policies", e);
log.error(msg, e);
throw new PolicyManagerDAOException(msg, e);
} finally { } finally {
PolicyManagementDAOUtil.cleanupResources(stmt, resultSet); PolicyManagementDAOUtil.cleanupResources(stmt, resultSet);
this.closeConnection();
} }
} }
@ -761,30 +663,23 @@ public class PolicyDAOImpl implements PolicyDAO {
resultSet = stmt.executeQuery(); resultSet = stmt.executeQuery();
while (resultSet.next()) { while (resultSet.next()) {
users.add(resultSet.getString("USERNAME")); users.add(resultSet.getString("USERNAME"));
} }
return users; return users;
} catch (SQLException e) { } catch (SQLException e) {
String msg = "Error occurred while getting the roles related to policies."; throw new PolicyManagerDAOException("Error occurred while getting the roles related to policies", e);
log.error(msg, e);
throw new PolicyManagerDAOException(msg, e);
} finally { } finally {
PolicyManagementDAOUtil.cleanupResources(stmt, resultSet); PolicyManagementDAOUtil.cleanupResources(stmt, resultSet);
this.closeConnection();
} }
} }
@Override @Override
public void addEffectivePolicyToDevice(int deviceId, Policy policy) throws PolicyManagerDAOException { public void addEffectivePolicyToDevice(int deviceId, Policy policy) throws PolicyManagerDAOException {
Connection conn; Connection conn;
PreparedStatement stmt = null; PreparedStatement stmt = null;
Timestamp currentTimestamp = new Timestamp(Calendar.getInstance().getTime().getTime()); Timestamp currentTimestamp = new Timestamp(Calendar.getInstance().getTime().getTime());
int tenantId = PrivilegedCarbonContext.getThreadLocalCarbonContext().getTenantId(); int tenantId = PrivilegedCarbonContext.getThreadLocalCarbonContext().getTenantId();
try { try {
conn = this.getConnection(); conn = this.getConnection();
String query = "INSERT INTO DM_DEVICE_POLICY_APPLIED (DEVICE_ID, POLICY_ID, POLICY_CONTENT, " + String query = "INSERT INTO DM_DEVICE_POLICY_APPLIED (DEVICE_ID, POLICY_ID, POLICY_CONTENT, " +
@ -796,17 +691,9 @@ public class PolicyDAOImpl implements PolicyDAO {
stmt.setTimestamp(4, currentTimestamp); stmt.setTimestamp(4, currentTimestamp);
stmt.setTimestamp(5, currentTimestamp); stmt.setTimestamp(5, currentTimestamp);
stmt.setInt(6, tenantId); stmt.setInt(6, tenantId);
stmt.executeUpdate(); stmt.executeUpdate();
} catch (SQLException | IOException e) {
} catch (SQLException e) { throw new PolicyManagerDAOException("Error occurred while adding the evaluated feature list to device", e);
String msg = "Error occurred while adding the evaluated feature list to device.";
log.error(msg, e);
throw new PolicyManagerDAOException(msg, e);
} catch (IOException e) {
String msg = "Error occurred while adding the evaluated feature list to device.";
log.error(msg, e);
throw new PolicyManagerDAOException(msg, e);
} finally { } finally {
PolicyManagementDAOUtil.cleanupResources(stmt, null); PolicyManagementDAOUtil.cleanupResources(stmt, null);
} }
@ -815,12 +702,10 @@ public class PolicyDAOImpl implements PolicyDAO {
@Override @Override
public void setPolicyApplied(int deviceId) throws PolicyManagerDAOException { public void setPolicyApplied(int deviceId) throws PolicyManagerDAOException {
Connection conn; Connection conn;
PreparedStatement stmt = null; PreparedStatement stmt = null;
Timestamp currentTimestamp = new Timestamp(Calendar.getInstance().getTime().getTime()); Timestamp currentTimestamp = new Timestamp(Calendar.getInstance().getTime().getTime());
int tenantId = PrivilegedCarbonContext.getThreadLocalCarbonContext().getTenantId(); int tenantId = PrivilegedCarbonContext.getThreadLocalCarbonContext().getTenantId();
try { try {
conn = this.getConnection(); conn = this.getConnection();
String query = "UPDATE DM_DEVICE_POLICY_APPLIED SET APPLIED_TIME = ?, APPLIED = ? WHERE DEVICE_ID = ? AND" + String query = "UPDATE DM_DEVICE_POLICY_APPLIED SET APPLIED_TIME = ?, APPLIED = ? WHERE DEVICE_ID = ? AND" +
@ -830,13 +715,10 @@ public class PolicyDAOImpl implements PolicyDAO {
stmt.setBoolean(2, true); stmt.setBoolean(2, true);
stmt.setInt(3, deviceId); stmt.setInt(3, deviceId);
stmt.setInt(4, tenantId); stmt.setInt(4, tenantId);
stmt.executeUpdate(); stmt.executeUpdate();
} catch (SQLException e) { } catch (SQLException e) {
String msg = "Error occurred while updating applied policy to device (" + deviceId + ")"; throw new PolicyManagerDAOException("Error occurred while updating applied policy to device (" +
log.error(msg, e); deviceId + ")", e);
throw new PolicyManagerDAOException(msg, e);
} finally { } finally {
PolicyManagementDAOUtil.cleanupResources(stmt, null); PolicyManagementDAOUtil.cleanupResources(stmt, null);
} }
@ -844,14 +726,11 @@ public class PolicyDAOImpl implements PolicyDAO {
@Override @Override
public void updateEffectivePolicyToDevice(int deviceId, Policy policy) public void updateEffectivePolicyToDevice(int deviceId, Policy policy) throws PolicyManagerDAOException {
throws PolicyManagerDAOException {
Connection conn; Connection conn;
PreparedStatement stmt = null; PreparedStatement stmt = null;
Timestamp currentTimestamp = new Timestamp(Calendar.getInstance().getTime().getTime()); Timestamp currentTimestamp = new Timestamp(Calendar.getInstance().getTime().getTime());
int tenantId = PrivilegedCarbonContext.getThreadLocalCarbonContext().getTenantId(); int tenantId = PrivilegedCarbonContext.getThreadLocalCarbonContext().getTenantId();
try { try {
conn = this.getConnection(); conn = this.getConnection();
String query = "UPDATE DM_DEVICE_POLICY_APPLIED SET POLICY_ID = ?, POLICY_CONTENT = ?, UPDATED_TIME = ?, " + String query = "UPDATE DM_DEVICE_POLICY_APPLIED SET POLICY_ID = ?, POLICY_CONTENT = ?, UPDATED_TIME = ?, " +
@ -865,29 +744,21 @@ public class PolicyDAOImpl implements PolicyDAO {
stmt.setInt(6, tenantId); stmt.setInt(6, tenantId);
stmt.executeUpdate(); stmt.executeUpdate();
} catch (SQLException e) { } catch (SQLException | IOException e) {
String msg = "Error occurred while updating the evaluated feature list to device."; throw new PolicyManagerDAOException("Error occurred while updating the evaluated feature list " +
log.error(msg, e); "to device", e);
throw new PolicyManagerDAOException(msg, e);
} catch (IOException e) {
String msg = "Error occurred while updating the evaluated feature list to device.";
log.error(msg, e);
throw new PolicyManagerDAOException(msg, e);
} finally { } finally {
PolicyManagementDAOUtil.cleanupResources(stmt, null); PolicyManagementDAOUtil.cleanupResources(stmt, null);
} }
} }
@Override @Override
public boolean checkPolicyAvailable(int deviceId) throws PolicyManagerDAOException { public boolean checkPolicyAvailable(int deviceId) throws PolicyManagerDAOException {
Connection conn; Connection conn;
PreparedStatement stmt = null; PreparedStatement stmt = null;
ResultSet resultSet = null; ResultSet resultSet = null;
boolean exist = false; boolean exist = false;
int tenantId = PrivilegedCarbonContext.getThreadLocalCarbonContext().getTenantId(); int tenantId = PrivilegedCarbonContext.getThreadLocalCarbonContext().getTenantId();
try { try {
conn = this.getConnection(); conn = this.getConnection();
String query = "SELECT * FROM DM_DEVICE_POLICY_APPLIED WHERE DEVICE_ID = ? AND TENANT_ID = ?"; String query = "SELECT * FROM DM_DEVICE_POLICY_APPLIED WHERE DEVICE_ID = ? AND TENANT_ID = ?";
@ -896,26 +767,21 @@ public class PolicyDAOImpl implements PolicyDAO {
stmt.setInt(2, tenantId); stmt.setInt(2, tenantId);
resultSet = stmt.executeQuery(); resultSet = stmt.executeQuery();
exist = resultSet.next(); exist = resultSet.next();
} catch (SQLException e) { } catch (SQLException e) {
String msg = "Error occurred while checking whether device (" + deviceId + ") has a policy to apply."; throw new PolicyManagerDAOException("Error occurred while checking whether device (" + deviceId +
log.error(msg, e); ") has a policy to apply", e);
throw new PolicyManagerDAOException(msg, e);
} finally { } finally {
PolicyManagementDAOUtil.cleanupResources(stmt, resultSet); PolicyManagementDAOUtil.cleanupResources(stmt, resultSet);
this.closeConnection();
} }
return exist; return exist;
} }
@Override @Override
public List<Integer> getPolicyIdsOfDevice(Device device) throws PolicyManagerDAOException { public List<Integer> getPolicyIdsOfDevice(Device device) throws PolicyManagerDAOException {
Connection conn; Connection conn;
PreparedStatement stmt = null; PreparedStatement stmt = null;
ResultSet resultSet = null; ResultSet resultSet = null;
List<Integer> policyIds = new ArrayList<Integer>(); List<Integer> policyIds = new ArrayList<Integer>();
try { try {
conn = this.getConnection(); conn = this.getConnection();
String query = "SELECT * FROM DM_DEVICE_POLICY WHERE DEVICE_ID = ? "; String query = "SELECT * FROM DM_DEVICE_POLICY WHERE DEVICE_ID = ? ";
@ -926,26 +792,20 @@ public class PolicyDAOImpl implements PolicyDAO {
while (resultSet.next()) { while (resultSet.next()) {
policyIds.add(resultSet.getInt("POLICY_ID")); policyIds.add(resultSet.getInt("POLICY_ID"));
} }
} catch (SQLException e) { } catch (SQLException e) {
String msg = "Error occurred while reading the device policy table."; throw new PolicyManagerDAOException("Error occurred while reading the device policy table", e);
log.error(msg, e);
throw new PolicyManagerDAOException(msg, e);
} finally { } finally {
PolicyManagementDAOUtil.cleanupResources(stmt, resultSet); PolicyManagementDAOUtil.cleanupResources(stmt, resultSet);
this.closeConnection();
} }
return policyIds; return policyIds;
} }
@Override @Override
public List<Integer> getPolicyOfRole(String roleName) throws PolicyManagerDAOException { public List<Integer> getPolicyOfRole(String roleName) throws PolicyManagerDAOException {
Connection conn; Connection conn;
PreparedStatement stmt = null; PreparedStatement stmt = null;
ResultSet resultSet = null; ResultSet resultSet = null;
List<Integer> policyIds = new ArrayList<Integer>(); List<Integer> policyIds = new ArrayList<Integer>();
try { try {
conn = this.getConnection(); conn = this.getConnection();
String query = "SELECT * FROM DM_ROLE_POLICY WHERE ROLE_NAME = ? "; String query = "SELECT * FROM DM_ROLE_POLICY WHERE ROLE_NAME = ? ";
@ -956,26 +816,20 @@ public class PolicyDAOImpl implements PolicyDAO {
while (resultSet.next()) { while (resultSet.next()) {
policyIds.add(resultSet.getInt("POLICY_ID")); policyIds.add(resultSet.getInt("POLICY_ID"));
} }
} catch (SQLException e) { } catch (SQLException e) {
String msg = "Error occurred while reading the role policy table."; throw new PolicyManagerDAOException("Error occurred while reading the role policy table", e);
log.error(msg, e);
throw new PolicyManagerDAOException(msg, e);
} finally { } finally {
PolicyManagementDAOUtil.cleanupResources(stmt, resultSet); PolicyManagementDAOUtil.cleanupResources(stmt, resultSet);
this.closeConnection();
} }
return policyIds; return policyIds;
} }
@Override @Override
public List<Integer> getPolicyOfUser(String username) throws PolicyManagerDAOException { public List<Integer> getPolicyOfUser(String username) throws PolicyManagerDAOException {
Connection conn; Connection conn;
PreparedStatement stmt = null; PreparedStatement stmt = null;
ResultSet resultSet = null; ResultSet resultSet = null;
List<Integer> policyIds = new ArrayList<Integer>(); List<Integer> policyIds = new ArrayList<Integer>();
try { try {
conn = this.getConnection(); conn = this.getConnection();
String query = "SELECT * FROM DM_USER_POLICY WHERE USERNAME = ? "; String query = "SELECT * FROM DM_USER_POLICY WHERE USERNAME = ? ";
@ -986,25 +840,19 @@ public class PolicyDAOImpl implements PolicyDAO {
while (resultSet.next()) { while (resultSet.next()) {
policyIds.add(resultSet.getInt("POLICY_ID")); policyIds.add(resultSet.getInt("POLICY_ID"));
} }
} catch (SQLException e) { } catch (SQLException e) {
String msg = "Error occurred while reading the user policy table."; throw new PolicyManagerDAOException("Error occurred while reading the user policy table", e);
log.error(msg, e);
throw new PolicyManagerDAOException(msg, e);
} finally { } finally {
PolicyManagementDAOUtil.cleanupResources(stmt, resultSet); PolicyManagementDAOUtil.cleanupResources(stmt, resultSet);
this.closeConnection();
} }
return policyIds; return policyIds;
} }
@Override @Override
public boolean deletePolicy(Policy policy) throws PolicyManagerDAOException { public boolean deletePolicy(Policy policy) throws PolicyManagerDAOException {
Connection conn; Connection conn;
PreparedStatement stmt = null; PreparedStatement stmt = null;
int tenantId = PrivilegedCarbonContext.getThreadLocalCarbonContext().getTenantId(); int tenantId = PrivilegedCarbonContext.getThreadLocalCarbonContext().getTenantId();
try { try {
conn = this.getConnection(); conn = this.getConnection();
String query = "DELETE FROM DM_POLICY WHERE ID = ? AND TENANT_ID = ?"; String query = "DELETE FROM DM_POLICY WHERE ID = ? AND TENANT_ID = ?";
@ -1018,9 +866,8 @@ public class PolicyDAOImpl implements PolicyDAO {
} }
return true; return true;
} catch (SQLException e) { } catch (SQLException e) {
String msg = "Unable to delete the policy (" + policy.getPolicyName() + ") from database."; throw new PolicyManagerDAOException("Unable to delete the policy (" + policy.getPolicyName() +
log.error(msg); ") from database", e);
throw new PolicyManagerDAOException(msg, e);
} finally { } finally {
PolicyManagementDAOUtil.cleanupResources(stmt, null); PolicyManagementDAOUtil.cleanupResources(stmt, null);
} }
@ -1028,11 +875,9 @@ public class PolicyDAOImpl implements PolicyDAO {
@Override @Override
public boolean deletePolicy(int policyId) throws PolicyManagerDAOException { public boolean deletePolicy(int policyId) throws PolicyManagerDAOException {
Connection conn; Connection conn;
PreparedStatement stmt = null; PreparedStatement stmt = null;
int tenantId = PrivilegedCarbonContext.getThreadLocalCarbonContext().getTenantId(); int tenantId = PrivilegedCarbonContext.getThreadLocalCarbonContext().getTenantId();
try { try {
conn = this.getConnection(); conn = this.getConnection();
String query = "DELETE FROM DM_POLICY WHERE ID = ? AND TENANT_ID = ?"; String query = "DELETE FROM DM_POLICY WHERE ID = ? AND TENANT_ID = ?";
@ -1046,9 +891,7 @@ public class PolicyDAOImpl implements PolicyDAO {
} }
return true; return true;
} catch (SQLException e) { } catch (SQLException e) {
String msg = "Unable to delete the policy (" + policyId + ") from database."; throw new PolicyManagerDAOException("Unable to delete the policy (" + policyId + ") from database", e);
log.error(msg);
throw new PolicyManagerDAOException(msg, e);
} finally { } finally {
PolicyManagementDAOUtil.cleanupResources(stmt, null); PolicyManagementDAOUtil.cleanupResources(stmt, null);
} }
@ -1056,10 +899,8 @@ public class PolicyDAOImpl implements PolicyDAO {
@Override @Override
public boolean deleteAllPolicyRelatedConfigs(int policyId) throws PolicyManagerDAOException { public boolean deleteAllPolicyRelatedConfigs(int policyId) throws PolicyManagerDAOException {
Connection conn; Connection conn;
PreparedStatement stmt = null; PreparedStatement stmt = null;
try { try {
conn = this.getConnection(); conn = this.getConnection();
@ -1073,27 +914,23 @@ public class PolicyDAOImpl implements PolicyDAO {
stmt.setInt(1, policyId); stmt.setInt(1, policyId);
stmt.executeUpdate(); stmt.executeUpdate();
String devicePolicy = "DELETE FROM DM_DEVICE_POLICY WHERE POLICY_ID = ?"; String devicePolicy = "DELETE FROM DM_DEVICE_POLICY WHERE POLICY_ID = ?";
stmt = conn.prepareStatement(devicePolicy); stmt = conn.prepareStatement(devicePolicy);
stmt.setInt(1, policyId); stmt.setInt(1, policyId);
stmt.executeUpdate(); stmt.executeUpdate();
String deleteCriteria = "DELETE FROM DM_POLICY_CRITERIA WHERE POLICY_ID = ?"; String deleteCriteria = "DELETE FROM DM_POLICY_CRITERIA WHERE POLICY_ID = ?";
stmt = conn.prepareStatement(deleteCriteria); stmt = conn.prepareStatement(deleteCriteria);
stmt.setInt(1, policyId); stmt.setInt(1, policyId);
stmt.executeUpdate(); stmt.executeUpdate();
if (log.isDebugEnabled()) { if (log.isDebugEnabled()) {
log.debug("Policy (" + policyId + ") related configs deleted from database."); log.debug("Policy (" + policyId + ") related configs deleted from database.");
} }
return true; return true;
} catch (SQLException e) { } catch (SQLException e) {
String msg = "Unable to delete the policy (" + policyId + ") related configs from database."; throw new PolicyManagerDAOException("Unable to delete the policy (" + policyId +
log.error(msg); ") related configs from database", e);
throw new PolicyManagerDAOException(msg, e);
} finally { } finally {
PolicyManagementDAOUtil.cleanupResources(stmt, null); PolicyManagementDAOUtil.cleanupResources(stmt, null);
} }
@ -1103,17 +940,7 @@ public class PolicyDAOImpl implements PolicyDAO {
return PolicyManagementDAOFactory.getConnection(); return PolicyManagementDAOFactory.getConnection();
} }
private void closeConnection() {
try {
PolicyManagementDAOFactory.closeConnection();
} catch (PolicyManagerDAOException e) {
log.warn("Unable to close the database connection.");
}
}
private Policy persistPolicy(Policy policy) throws PolicyManagerDAOException { private Policy persistPolicy(Policy policy) throws PolicyManagerDAOException {
Connection conn; Connection conn;
PreparedStatement stmt = null; PreparedStatement stmt = null;
ResultSet generatedKeys = null; ResultSet generatedKeys = null;
@ -1149,9 +976,7 @@ public class PolicyDAOImpl implements PolicyDAO {
} }
} catch (SQLException e) { } catch (SQLException e) {
String msg = "Error occurred while adding policy to the database."; throw new PolicyManagerDAOException("Error occurred while adding policy to the database", e);
log.error(msg, e);
throw new PolicyManagerDAOException(msg, e);
} finally { } finally {
PolicyManagementDAOUtil.cleanupResources(stmt, generatedKeys); PolicyManagementDAOUtil.cleanupResources(stmt, generatedKeys);
} }
@ -1167,7 +992,6 @@ public class PolicyDAOImpl implements PolicyDAO {
* @throws PolicyManagerDAOException * @throws PolicyManagerDAOException
*/ */
private int getDeviceTypeId(String deviceType) throws PolicyManagerDAOException { private int getDeviceTypeId(String deviceType) throws PolicyManagerDAOException {
Connection conn; Connection conn;
PreparedStatement stmt = null; PreparedStatement stmt = null;
ResultSet resultSet = null; ResultSet resultSet = null;
@ -1182,21 +1006,16 @@ public class PolicyDAOImpl implements PolicyDAO {
while (resultSet.next()) { while (resultSet.next()) {
deviceTypeId = resultSet.getInt("ID"); deviceTypeId = resultSet.getInt("ID");
} }
} catch (SQLException e) { } catch (SQLException e) {
String msg = "Error occurred while selecting the device type id."; throw new PolicyManagerDAOException("Error occurred while selecting the device type id", e);
log.error(msg, e);
throw new PolicyManagerDAOException(msg, e);
} finally { } finally {
PolicyManagementDAOUtil.cleanupResources(stmt, resultSet); PolicyManagementDAOUtil.cleanupResources(stmt, resultSet);
this.closeConnection();
} }
return deviceTypeId; return deviceTypeId;
} }
private int readHighestPriorityOfPolicies() throws PolicyManagerDAOException { private int readHighestPriorityOfPolicies() throws PolicyManagerDAOException {
Connection conn; Connection conn;
PreparedStatement stmt = null; PreparedStatement stmt = null;
ResultSet resultSet = null; ResultSet resultSet = null;
@ -1216,11 +1035,8 @@ public class PolicyDAOImpl implements PolicyDAO {
if (log.isDebugEnabled()) { if (log.isDebugEnabled()) {
log.debug("Priority of the new policy added is (" + priority + ")"); log.debug("Priority of the new policy added is (" + priority + ")");
} }
} catch (SQLException e) { } catch (SQLException e) {
String msg = "Error occurred while reading the highest priority of the policies."; throw new PolicyManagerDAOException("Error occurred while reading the highest priority of the policies", e);
log.error(msg, e);
throw new PolicyManagerDAOException(msg, e);
} finally { } finally {
PolicyManagementDAOUtil.cleanupResources(stmt, resultSet); PolicyManagementDAOUtil.cleanupResources(stmt, resultSet);
} }
@ -1229,13 +1045,11 @@ public class PolicyDAOImpl implements PolicyDAO {
@Override @Override
public int getPolicyCount() throws PolicyManagerDAOException { public int getPolicyCount() throws PolicyManagerDAOException {
Connection conn; Connection conn;
PreparedStatement stmt = null; PreparedStatement stmt = null;
ResultSet resultSet = null; ResultSet resultSet = null;
int tenantId = PrivilegedCarbonContext.getThreadLocalCarbonContext().getTenantId(); int tenantId = PrivilegedCarbonContext.getThreadLocalCarbonContext().getTenantId();
int policyCount = 0; int policyCount = 0;
try { try {
conn = this.getConnection(); conn = this.getConnection();
String query = "SELECT COUNT(ID) AS POLICY_COUNT FROM DM_POLICY WHERE TENANT_ID = ?"; String query = "SELECT COUNT(ID) AS POLICY_COUNT FROM DM_POLICY WHERE TENANT_ID = ?";
@ -1247,25 +1061,19 @@ public class PolicyDAOImpl implements PolicyDAO {
policyCount = resultSet.getInt("POLICY_COUNT"); policyCount = resultSet.getInt("POLICY_COUNT");
} }
return policyCount; return policyCount;
} catch (SQLException e) { } catch (SQLException e) {
String msg = "Error occurred while reading the policies from the database."; throw new PolicyManagerDAOException("Error occurred while reading the policies from the database", e);
log.error(msg, e);
throw new PolicyManagerDAOException(msg, e);
} finally { } finally {
PolicyManagementDAOUtil.cleanupResources(stmt, resultSet); PolicyManagementDAOUtil.cleanupResources(stmt, resultSet);
this.closeConnection();
} }
} }
@Override @Override
public int getAppliedPolicyId(int deviceId) throws PolicyManagerDAOException { public int getAppliedPolicyId(int deviceId) throws PolicyManagerDAOException {
Connection conn; Connection conn;
PreparedStatement stmt = null; PreparedStatement stmt = null;
ResultSet resultSet = null; ResultSet resultSet = null;
int tenantId = PrivilegedCarbonContext.getThreadLocalCarbonContext().getTenantId(); int tenantId = PrivilegedCarbonContext.getThreadLocalCarbonContext().getTenantId();
try { try {
conn = this.getConnection(); conn = this.getConnection();
String query = "SELECT * FROM DM_DEVICE_POLICY_APPLIED WHERE DEVICE_ID = ? AND TENANT_ID = ?"; String query = "SELECT * FROM DM_DEVICE_POLICY_APPLIED WHERE DEVICE_ID = ? AND TENANT_ID = ?";
@ -1277,28 +1085,21 @@ public class PolicyDAOImpl implements PolicyDAO {
while (resultSet.next()) { while (resultSet.next()) {
return resultSet.getInt("POLICY_ID"); return resultSet.getInt("POLICY_ID");
} }
} catch (SQLException e) { } catch (SQLException e) {
String msg = "Error occurred while getting the applied policy id."; throw new PolicyManagerDAOException("Error occurred while getting the applied policy id", e);
log.error(msg, e);
throw new PolicyManagerDAOException(msg, e);
} finally { } finally {
PolicyManagementDAOUtil.cleanupResources(stmt, resultSet); PolicyManagementDAOUtil.cleanupResources(stmt, resultSet);
this.closeConnection();
} }
return 0; return 0;
} }
@Override @Override
public Policy getAppliedPolicy(int deviceId) throws PolicyManagerDAOException { public Policy getAppliedPolicy(int deviceId) throws PolicyManagerDAOException {
Connection conn; Connection conn;
PreparedStatement stmt = null; PreparedStatement stmt = null;
ResultSet resultSet = null; ResultSet resultSet = null;
int tenantId = PrivilegedCarbonContext.getThreadLocalCarbonContext().getTenantId(); int tenantId = PrivilegedCarbonContext.getThreadLocalCarbonContext().getTenantId();
Policy policy = null; Policy policy = null;
try { try {
conn = this.getConnection(); conn = this.getConnection();
String query = "SELECT * FROM DM_DEVICE_POLICY_APPLIED WHERE DEVICE_ID = ? AND TENANT_ID = ?"; String query = "SELECT * FROM DM_DEVICE_POLICY_APPLIED WHERE DEVICE_ID = ? AND TENANT_ID = ?";
@ -1336,20 +1137,13 @@ public class PolicyDAOImpl implements PolicyDAO {
} }
} catch (SQLException e) { } catch (SQLException e) {
String msg = "Error occurred while getting the applied policy."; throw new PolicyManagerDAOException("Error occurred while getting the applied policy", e);
log.error(msg, e);
throw new PolicyManagerDAOException(msg, e);
} catch (IOException e) { } catch (IOException e) {
String msg = "Unable to read the byte stream for content"; throw new PolicyManagerDAOException("Unable to read the byte stream for content", e);
log.error(msg);
throw new PolicyManagerDAOException(msg, e);
} catch (ClassNotFoundException e) { } catch (ClassNotFoundException e) {
String msg = "Class not found while converting the object"; throw new PolicyManagerDAOException("Class not found while converting the object", e);
log.error(msg);
throw new PolicyManagerDAOException(msg, e);
} finally { } finally {
PolicyManagementDAOUtil.cleanupResources(stmt, resultSet); PolicyManagementDAOUtil.cleanupResources(stmt, resultSet);
this.closeConnection();
} }
// //
// if (policy != null && log.isDebugEnabled()) { // if (policy != null && log.isDebugEnabled()) {
@ -1365,14 +1159,11 @@ public class PolicyDAOImpl implements PolicyDAO {
@Override @Override
public HashMap<Integer, Integer> getAppliedPolicyIds(List<Integer> deviceIds) throws PolicyManagerDAOException { public HashMap<Integer, Integer> getAppliedPolicyIds(List<Integer> deviceIds) throws PolicyManagerDAOException {
Connection conn; Connection conn;
PreparedStatement stmt = null; PreparedStatement stmt = null;
ResultSet resultSet = null; ResultSet resultSet = null;
HashMap<Integer, Integer> devicePolicyIds = new HashMap<>(); HashMap<Integer, Integer> devicePolicyIds = new HashMap<>();
int tenantId = PrivilegedCarbonContext.getThreadLocalCarbonContext().getTenantId(); int tenantId = PrivilegedCarbonContext.getThreadLocalCarbonContext().getTenantId();
try { try {
conn = this.getConnection(); conn = this.getConnection();
String query = "SELECT * FROM DM_DEVICE_POLICY_APPLIED WHERE DEVICE_ID = ? AND TENANT_ID = ?"; String query = "SELECT * FROM DM_DEVICE_POLICY_APPLIED WHERE DEVICE_ID = ? AND TENANT_ID = ?";
@ -1384,16 +1175,11 @@ public class PolicyDAOImpl implements PolicyDAO {
while (resultSet.next()) { while (resultSet.next()) {
devicePolicyIds.put(resultSet.getInt("DEVICE_ID"), resultSet.getInt("POLICY_ID")); devicePolicyIds.put(resultSet.getInt("DEVICE_ID"), resultSet.getInt("POLICY_ID"));
} }
} catch (SQLException e) { } catch (SQLException e) {
String msg = "Error occurred while getting the applied policy."; throw new PolicyManagerDAOException("Error occurred while getting the applied policy", e);
log.error(msg, e);
throw new PolicyManagerDAOException(msg, e);
} finally { } finally {
PolicyManagementDAOUtil.cleanupResources(stmt, resultSet); PolicyManagementDAOUtil.cleanupResources(stmt, resultSet);
this.closeConnection();
} }
return devicePolicyIds; return devicePolicyIds;
} }

@ -210,7 +210,6 @@ public class ProfileDAOImpl implements ProfileDAO {
throw new ProfileManagerDAOException(msg, e); throw new ProfileManagerDAOException(msg, e);
} finally { } finally {
PolicyManagementDAOUtil.cleanupResources(stmt, resultSet); PolicyManagementDAOUtil.cleanupResources(stmt, resultSet);
this.closeConnection();
} }
return profile; return profile;
} }
@ -254,7 +253,6 @@ public class ProfileDAOImpl implements ProfileDAO {
throw new ProfileManagerDAOException(msg, e); throw new ProfileManagerDAOException(msg, e);
} finally { } finally {
PolicyManagementDAOUtil.cleanupResources(stmt, resultSet); PolicyManagementDAOUtil.cleanupResources(stmt, resultSet);
this.closeConnection();
} }
return profileList; return profileList;
} }
@ -292,7 +290,6 @@ public class ProfileDAOImpl implements ProfileDAO {
throw new ProfileManagerDAOException(msg, e); throw new ProfileManagerDAOException(msg, e);
} finally { } finally {
PolicyManagementDAOUtil.cleanupResources(stmt, resultSet); PolicyManagementDAOUtil.cleanupResources(stmt, resultSet);
this.closeConnection();
} }
return profileList; return profileList;
} }
@ -307,13 +304,4 @@ public class ProfileDAOImpl implements ProfileDAO {
} }
} }
private void closeConnection() {
try {
PolicyManagementDAOFactory.closeConnection();
} catch (PolicyManagerDAOException e) {
log.warn("Unable to close the database connection.");
}
}
} }

@ -21,6 +21,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.device.mgt.common.Feature; import org.wso2.carbon.device.mgt.common.Feature;
import org.wso2.carbon.device.mgt.core.dao.DeviceManagementDAOFactory;
import org.wso2.carbon.policy.mgt.common.FeatureManagementException; import org.wso2.carbon.policy.mgt.common.FeatureManagementException;
import org.wso2.carbon.policy.mgt.common.Profile; import org.wso2.carbon.policy.mgt.common.Profile;
import org.wso2.carbon.policy.mgt.common.ProfileFeature; import org.wso2.carbon.policy.mgt.common.ProfileFeature;
@ -30,6 +31,7 @@ import org.wso2.carbon.policy.mgt.core.dao.PolicyManagementDAOFactory;
import org.wso2.carbon.policy.mgt.core.dao.PolicyManagerDAOException; import org.wso2.carbon.policy.mgt.core.dao.PolicyManagerDAOException;
import org.wso2.carbon.policy.mgt.core.mgt.FeatureManager; import org.wso2.carbon.policy.mgt.core.mgt.FeatureManager;
import java.sql.SQLException;
import java.util.List; import java.util.List;
public class FeatureManagerImpl implements FeatureManager { public class FeatureManagerImpl implements FeatureManager {
@ -137,23 +139,15 @@ public class FeatureManagerImpl implements FeatureManager {
bool = featureDAO.deleteFeature(feature.getId()); bool = featureDAO.deleteFeature(feature.getId());
PolicyManagementDAOFactory.commitTransaction(); PolicyManagementDAOFactory.commitTransaction();
} catch (FeatureManagerDAOException e) { } catch (FeatureManagerDAOException e) {
try {
PolicyManagementDAOFactory.rollbackTransaction(); PolicyManagementDAOFactory.rollbackTransaction();
} catch (PolicyManagerDAOException e1) { throw new FeatureManagementException("Error occurred while deleting the feature (" + feature.getName() +
log.warn("Unable to roll back the transaction"); ")", e);
}
String msg = "Error occurred while deleting the feature (" + feature.getName() + ")";
log.error(msg, e);
throw new FeatureManagementException(msg, e);
} catch (PolicyManagerDAOException e) { } catch (PolicyManagerDAOException e) {
try {
PolicyManagementDAOFactory.rollbackTransaction(); PolicyManagementDAOFactory.rollbackTransaction();
} catch (PolicyManagerDAOException e1) { throw new FeatureManagementException("Error occurred while deleting the feature (" + feature.getName() +
log.warn("Unable to roll back the transaction"); ") from database", e);
} } finally {
String msg = "Error occurred while deleting the feature (" + feature.getName() + ") from database"; PolicyManagementDAOFactory.closeConnection();
log.error(msg, e);
throw new FeatureManagementException(msg, e);
} }
return bool; return bool;
} }
@ -164,22 +158,11 @@ public class FeatureManagerImpl implements FeatureManager {
PolicyManagementDAOFactory.beginTransaction(); PolicyManagementDAOFactory.beginTransaction();
feature = featureDAO.addProfileFeature(feature, profileId); feature = featureDAO.addProfileFeature(feature, profileId);
PolicyManagementDAOFactory.commitTransaction(); PolicyManagementDAOFactory.commitTransaction();
} catch (PolicyManagerDAOException | FeatureManagerDAOException e) {
} catch (PolicyManagerDAOException e) { throw new FeatureManagementException("Error occurred while adding profile feature (" +
String msg = "Error occurred while adding profile feature (" + feature.getFeatureCode() + " - " + profileId + ")", e);
feature.getFeatureCode() + " - " + profileId + ")"; } finally {
log.error(msg, e); PolicyManagementDAOFactory.closeConnection();
throw new FeatureManagementException(msg, e);
} catch (FeatureManagerDAOException e) {
try {
PolicyManagementDAOFactory.rollbackTransaction();
} catch (PolicyManagerDAOException e1) {
log.warn("Unable to roll back the transaction");
}
String msg = "Error occurred while adding profile feature (" +
feature.getFeatureCode() + " - " + profileId + ") to database.";
log.error(msg, e);
throw new FeatureManagementException(msg, e);
} }
return feature; return feature;
} }
@ -192,26 +175,12 @@ public class FeatureManagerImpl implements FeatureManager {
feature = featureDAO.updateProfileFeature(feature, profileId); feature = featureDAO.updateProfileFeature(feature, profileId);
PolicyManagementDAOFactory.commitTransaction(); PolicyManagementDAOFactory.commitTransaction();
} catch (PolicyManagerDAOException e) { } catch (FeatureManagerDAOException | PolicyManagerDAOException e) {
try {
PolicyManagementDAOFactory.rollbackTransaction(); PolicyManagementDAOFactory.rollbackTransaction();
} catch (PolicyManagerDAOException e1) { throw new FeatureManagementException("Error occurred while updating feature (" +
log.warn("Unable to roll back the transaction"); feature.getFeatureCode() + " - " + profileId + ") in database.", e);
} } finally {
String msg = "Error occurred while updating feature (" + PolicyManagementDAOFactory.closeConnection();
feature.getFeatureCode() + " - " + profileId + ")";
log.error(msg, e);
throw new FeatureManagementException(msg, e);
} catch (FeatureManagerDAOException e) {
try {
PolicyManagementDAOFactory.rollbackTransaction();
} catch (PolicyManagerDAOException e1) {
log.warn("Unable to roll back the transaction");
}
String msg = "Error occurred while updating feature (" +
feature.getFeatureCode() + " - " + profileId + ") in database.";
log.error(msg, e);
throw new FeatureManagementException(msg, e);
} }
return feature; return feature;
} }
@ -224,23 +193,15 @@ public class FeatureManagerImpl implements FeatureManager {
features = featureDAO.addProfileFeatures(features, profileId); features = featureDAO.addProfileFeatures(features, profileId);
PolicyManagementDAOFactory.commitTransaction(); PolicyManagementDAOFactory.commitTransaction();
} catch (FeatureManagerDAOException e) { } catch (FeatureManagerDAOException e) {
try {
PolicyManagementDAOFactory.rollbackTransaction(); PolicyManagementDAOFactory.rollbackTransaction();
} catch (PolicyManagerDAOException e1) { throw new FeatureManagementException("Error occurred while adding the features to profile id (" +
log.warn("Unable to roll back the transaction"); profileId + ")", e);
}
String msg = "Error occurred while adding the features to profile id (" + profileId + ")";
log.error(msg, e);
throw new FeatureManagementException(msg, e);
} catch (PolicyManagerDAOException e) { } catch (PolicyManagerDAOException e) {
try {
PolicyManagementDAOFactory.rollbackTransaction(); PolicyManagementDAOFactory.rollbackTransaction();
} catch (PolicyManagerDAOException e1) { throw new FeatureManagementException("Error occurred while adding the features to profile id (" +
log.warn("Unable to roll back the transaction"); profileId + ") to the database", e);
} } finally {
String msg = "Error occurred while adding the features to profile id (" + profileId + ") to the database"; PolicyManagementDAOFactory.closeConnection();
log.error(msg, e);
throw new FeatureManagementException(msg, e);
} }
return features; return features;
} }
@ -253,23 +214,15 @@ public class FeatureManagerImpl implements FeatureManager {
features = featureDAO.updateProfileFeatures(features, profileId); features = featureDAO.updateProfileFeatures(features, profileId);
PolicyManagementDAOFactory.commitTransaction(); PolicyManagementDAOFactory.commitTransaction();
} catch (FeatureManagerDAOException e) { } catch (FeatureManagerDAOException e) {
try {
PolicyManagementDAOFactory.rollbackTransaction(); PolicyManagementDAOFactory.rollbackTransaction();
} catch (PolicyManagerDAOException e1) { throw new FeatureManagementException("Error occurred while updating the features to profile id (" +
log.warn("Unable to roll back the transaction"); profileId + ")", e);
}
String msg = "Error occurred while updating the features to profile id (" + profileId + ")";
log.error(msg, e);
throw new FeatureManagementException(msg, e);
} catch (PolicyManagerDAOException e) { } catch (PolicyManagerDAOException e) {
try {
PolicyManagementDAOFactory.rollbackTransaction(); PolicyManagementDAOFactory.rollbackTransaction();
} catch (PolicyManagerDAOException e1) { throw new FeatureManagementException("Error occurred while updating the features to profile id (" +
log.warn("Unable to roll back the transaction"); profileId + ") to the database", e);
} } finally {
String msg = "Error occurred while updating the features to profile id (" + profileId + ") to the database"; PolicyManagementDAOFactory.closeConnection();
log.error(msg, e);
throw new FeatureManagementException(msg, e);
} }
return features; return features;
} }
@ -278,22 +231,28 @@ public class FeatureManagerImpl implements FeatureManager {
@Override @Override
public List<Feature> getAllFeatures(String deviceType) throws FeatureManagementException { public List<Feature> getAllFeatures(String deviceType) throws FeatureManagementException {
try { try {
PolicyManagementDAOFactory.openConnection();
return featureDAO.getAllFeatures(deviceType); return featureDAO.getAllFeatures(deviceType);
} catch (FeatureManagerDAOException e) { } catch (FeatureManagerDAOException e) {
String msg = "Error occurred while getting the features."; throw new FeatureManagementException("Error occurred while retrieving the features", e);
log.error(msg, e); } catch (SQLException e) {
throw new FeatureManagementException(msg, e); throw new FeatureManagementException("Error occurred while opening a connection to the data source", e);
} finally {
PolicyManagementDAOFactory.closeConnection();
} }
} }
@Override @Override
public List<ProfileFeature> getFeaturesForProfile(int profileId) throws FeatureManagementException { public List<ProfileFeature> getFeaturesForProfile(int profileId) throws FeatureManagementException {
try { try {
DeviceManagementDAOFactory.openConnection();
return featureDAO.getFeaturesForProfile(profileId); return featureDAO.getFeaturesForProfile(profileId);
} catch (FeatureManagerDAOException e) { } catch (FeatureManagerDAOException e) {
String msg = "Error occurred while getting the features."; throw new FeatureManagementException("Error occurred while getting the features", e);
log.error(msg, e); } catch (SQLException e) {
throw new FeatureManagementException(msg, e); throw new FeatureManagementException("Error occurred while opening a connection to the data source", e);
} finally {
PolicyManagementDAOFactory.closeConnection();
} }
} }
@ -305,23 +264,15 @@ public class FeatureManagerImpl implements FeatureManager {
bool = featureDAO.deleteFeature(featureId); bool = featureDAO.deleteFeature(featureId);
PolicyManagementDAOFactory.commitTransaction(); PolicyManagementDAOFactory.commitTransaction();
} catch (FeatureManagerDAOException e) { } catch (FeatureManagerDAOException e) {
try {
PolicyManagementDAOFactory.rollbackTransaction(); PolicyManagementDAOFactory.rollbackTransaction();
} catch (PolicyManagerDAOException e1) { throw new FeatureManagementException("Error occurred while deleting the feature - id (" +
log.warn("Unable to roll back the transaction"); featureId + ")", e);
}
String msg = "Error occurred while deleting the feature - id (" + featureId + ")";
log.error(msg, e);
throw new FeatureManagementException(msg, e);
} catch (PolicyManagerDAOException e) { } catch (PolicyManagerDAOException e) {
try {
PolicyManagementDAOFactory.rollbackTransaction(); PolicyManagementDAOFactory.rollbackTransaction();
} catch (PolicyManagerDAOException e1) { throw new FeatureManagementException("Error occurred while deleting the feature - id (" + featureId +
log.warn("Unable to roll back the transaction"); ") from database.", e);
} } finally {
String msg = "Error occurred while deleting the feature - id (" + featureId + ") from database."; PolicyManagementDAOFactory.closeConnection();
log.error(msg, e);
throw new FeatureManagementException(msg, e);
} }
return bool; return bool;
} }
@ -334,24 +285,15 @@ public class FeatureManagerImpl implements FeatureManager {
bool = featureDAO.deleteFeaturesOfProfile(profile); bool = featureDAO.deleteFeaturesOfProfile(profile);
PolicyManagementDAOFactory.commitTransaction(); PolicyManagementDAOFactory.commitTransaction();
} catch (FeatureManagerDAOException e) { } catch (FeatureManagerDAOException e) {
try {
PolicyManagementDAOFactory.rollbackTransaction(); PolicyManagementDAOFactory.rollbackTransaction();
} catch (PolicyManagerDAOException e1) { throw new FeatureManagementException("Error occurred while deleting the feature of - profile (" +
log.warn("Unable to roll back the transaction"); profile.getProfileName() + ")", e);
}
String msg = "Error occurred while deleting the feature of - profile (" + profile.getProfileName() + ")";
log.error(msg, e);
throw new FeatureManagementException(msg, e);
} catch (PolicyManagerDAOException e) { } catch (PolicyManagerDAOException e) {
try {
PolicyManagementDAOFactory.rollbackTransaction(); PolicyManagementDAOFactory.rollbackTransaction();
} catch (PolicyManagerDAOException e1) { throw new FeatureManagementException("Error occurred while deleting the feature of - profile (" +
log.warn("Unable to roll back the transaction"); profile.getProfileName() + ") from database", e);
} } finally {
String msg = "Error occurred while deleting the feature of - profile (" + PolicyManagementDAOFactory.closeConnection();
profile.getProfileName() + ") from database";
log.error(msg, e);
throw new FeatureManagementException(msg, e);
} }
return bool; return bool;
} }

@ -52,6 +52,7 @@ 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.policy.mgt.core.util.PolicyManagerUtil; import org.wso2.carbon.policy.mgt.core.util.PolicyManagerUtil;
import java.sql.SQLException;
import java.util.*; import java.util.*;
public class MonitoringManagerImpl implements MonitoringManager { public class MonitoringManagerImpl implements MonitoringManager {
@ -77,8 +78,6 @@ public class MonitoringManagerImpl implements MonitoringManager {
List<ComplianceFeature> complianceFeatures = new ArrayList<>(); List<ComplianceFeature> complianceFeatures = new ArrayList<>();
try { try {
DeviceManagementProviderService service = new DeviceManagementProviderServiceImpl(); DeviceManagementProviderService service = new DeviceManagementProviderServiceImpl();
PolicyManager manager = new PolicyManagerImpl(); PolicyManager manager = new PolicyManagerImpl();
Device device = service.getDevice(deviceIdentifier); Device device = service.getDevice(deviceIdentifier);
@ -91,6 +90,8 @@ public class MonitoringManagerImpl implements MonitoringManager {
ComplianceData complianceData; ComplianceData complianceData;
// This was retrieved from database because compliance id must be present for other dao operations to // This was retrieved from database because compliance id must be present for other dao operations to
// run. // run.
try {
PolicyManagementDAOFactory.openConnection();
ComplianceData cmd = monitoringDAO.getCompliance(device.getId()); ComplianceData cmd = monitoringDAO.getCompliance(device.getId());
complianceData = monitoringService.checkPolicyCompliance(deviceIdentifier, complianceData = monitoringService.checkPolicyCompliance(deviceIdentifier,
policy, deviceResponse); policy, deviceResponse);
@ -99,6 +100,11 @@ public class MonitoringManagerImpl implements MonitoringManager {
complianceFeatures = complianceData.getComplianceFeatures(); complianceFeatures = complianceData.getComplianceFeatures();
complianceData.setDeviceId(device.getId()); complianceData.setDeviceId(device.getId());
complianceData.setPolicyId(policy.getId()); complianceData.setPolicyId(policy.getId());
} catch (SQLException e) {
throw new PolicyComplianceException("Error occurred while opening a data source connection", e);
} finally {
PolicyManagementDAOFactory.closeConnection();
}
PolicyManagementDAOFactory.beginTransaction(); PolicyManagementDAOFactory.beginTransaction();
//This was added because update query below that did not return the update table primary key. //This was added because update query below that did not return the update table primary key.
@ -120,88 +126,57 @@ public class MonitoringManagerImpl implements MonitoringManager {
} }
} }
} }
} else { } else {
monitoringDAO.setDeviceAsCompliance(device.getId(), policy.getId()); monitoringDAO.setDeviceAsCompliance(device.getId(), policy.getId());
//complianceData.setId(cmf.getId()); //complianceData.setId(cmf.getId());
monitoringDAO.deleteNoneComplianceData(complianceData.getId()); monitoringDAO.deleteNoneComplianceData(complianceData.getId());
} }
PolicyManagementDAOFactory.commitTransaction(); PolicyManagementDAOFactory.commitTransaction();
} else { } else {
if (log.isDebugEnabled()) { if (log.isDebugEnabled()) {
log.debug("There is no policy applied to this device, hence compliance monitoring was not called."); log.debug("There is no policy applied to this device, hence compliance monitoring was not called.");
} }
} }
} catch (DeviceManagementException e) { } catch (DeviceManagementException e) {
try {
PolicyManagementDAOFactory.rollbackTransaction(); PolicyManagementDAOFactory.rollbackTransaction();
} catch (PolicyManagerDAOException e1) { throw new PolicyComplianceException("Unable tor retrieve device data from DB for " +
log.warn("Error occurred while roll backing the transaction."); deviceIdentifier.getId() + " - " + deviceIdentifier.getType(), e);
} } catch (PolicyManagerDAOException | PolicyManagementException e) {
String msg = "Unable tor retrieve device data from DB for " + deviceIdentifier.getId() + " - " +
deviceIdentifier.getType();
log.error(msg, e);
throw new PolicyComplianceException(msg, e);
} catch (PolicyManagerDAOException e) {
try {
PolicyManagementDAOFactory.rollbackTransaction(); PolicyManagementDAOFactory.rollbackTransaction();
} catch (PolicyManagerDAOException e1) { throw new PolicyComplianceException("Unable tor retrieve policy data from DB for device " +
log.warn("Error occurred while roll backing the transaction."); deviceIdentifier.getId() + " - " + deviceIdentifier.getType(), e);
}
String msg = "Unable tor retrieve policy data from DB for device " + deviceIdentifier.getId() + " - " +
deviceIdentifier.getType();
log.error(msg, e);
throw new PolicyComplianceException(msg, e);
} catch (MonitoringDAOException e) { } catch (MonitoringDAOException e) {
try {
PolicyManagementDAOFactory.rollbackTransaction(); PolicyManagementDAOFactory.rollbackTransaction();
} catch (PolicyManagerDAOException e1) { throw new PolicyComplianceException("Unable to add the none compliance features to database for device " +
log.warn("Error occurred while roll backing the transaction."); deviceIdentifier.getId() + " - " + deviceIdentifier.getType(), e);
} } finally {
String msg = "Unable to add the none compliance features to database for device " + deviceIdentifier. PolicyManagementDAOFactory.closeConnection();
getId() + " - " + deviceIdentifier.getType();
log.error(msg, e);
throw new PolicyComplianceException(msg, e);
} catch (PolicyManagementException e) {
try {
PolicyManagementDAOFactory.rollbackTransaction();
} catch (PolicyManagerDAOException e1) {
log.warn("Error occurred while roll backing the transaction.");
}
String msg = "Unable tor retrieve policy data from DB for device " + deviceIdentifier.getId() + " - " +
deviceIdentifier.getType();
log.error(msg, e);
throw new PolicyComplianceException(msg, e);
} }
return complianceFeatures; return complianceFeatures;
} }
@Override @Override
public boolean isCompliance(DeviceIdentifier deviceIdentifier) throws PolicyComplianceException { public boolean isCompliance(DeviceIdentifier deviceIdentifier) throws PolicyComplianceException {
try { try {
DeviceManagementProviderService service = new DeviceManagementProviderServiceImpl(); DeviceManagementProviderService service = new DeviceManagementProviderServiceImpl();
Device device = service.getDevice(deviceIdentifier); Device device = service.getDevice(deviceIdentifier);
//deviceDAO.getDevice(deviceIdentifier, tenantId); //deviceDAO.getDevice(deviceIdentifier, tenantId);
PolicyManagementDAOFactory.openConnection();
ComplianceData complianceData = monitoringDAO.getCompliance(device.getId()); ComplianceData complianceData = monitoringDAO.getCompliance(device.getId());
if (complianceData == null || !complianceData.isStatus()) { if (complianceData == null || !complianceData.isStatus()) {
return false; return false;
} }
} catch (DeviceManagementException e) { } catch (DeviceManagementException e) {
String msg = "Unable to retrieve device data for " + deviceIdentifier.getId() + " - " + throw new PolicyComplianceException("Unable to retrieve device data for " + deviceIdentifier.getId() +
deviceIdentifier.getType(); " - " + deviceIdentifier.getType(), e);
log.error(msg, e);
throw new PolicyComplianceException(msg, e);
} catch (MonitoringDAOException e) { } catch (MonitoringDAOException e) {
String msg = "Unable to retrieve compliance status for " + deviceIdentifier.getId() + " - " + throw new PolicyComplianceException("Unable to retrieve compliance status for " + deviceIdentifier.getId() +
deviceIdentifier.getType(); " - " + deviceIdentifier.getType(), e);
log.error(msg, e); } catch (SQLException e) {
throw new PolicyComplianceException(msg, e); throw new PolicyComplianceException("Error occurred while opening a connection to the data source", e);
} finally {
PolicyManagementDAOFactory.closeConnection();
} }
return true; return true;
} }
@ -212,6 +187,7 @@ public class MonitoringManagerImpl implements MonitoringManager {
ComplianceData complianceData; ComplianceData complianceData;
try { try {
PolicyManagementDAOFactory.openConnection();
DeviceManagementProviderService service = new DeviceManagementProviderServiceImpl(); DeviceManagementProviderService service = new DeviceManagementProviderServiceImpl();
Device device = service.getDevice(deviceIdentifier); Device device = service.getDevice(deviceIdentifier);
complianceData = monitoringDAO.getCompliance(device.getId()); complianceData = monitoringDAO.getCompliance(device.getId());
@ -220,26 +196,23 @@ public class MonitoringManagerImpl implements MonitoringManager {
complianceData.setComplianceFeatures(complianceFeatures); complianceData.setComplianceFeatures(complianceFeatures);
} catch (DeviceManagementException e) { } catch (DeviceManagementException e) {
String msg = "Unable to retrieve device data for " + deviceIdentifier.getId() + " - " + throw new PolicyComplianceException("Unable to retrieve device data for " + deviceIdentifier.getId() +
deviceIdentifier.getType(); " - " + deviceIdentifier.getType(), e);
log.error(msg, e);
throw new PolicyComplianceException(msg, e);
} catch (MonitoringDAOException e) { } catch (MonitoringDAOException e) {
String msg = "Unable to retrieve compliance data for " + deviceIdentifier.getId() + " - " + throw new PolicyComplianceException("Unable to retrieve compliance data for " + deviceIdentifier.getId() +
deviceIdentifier.getType(); " - " + deviceIdentifier.getType(), e);
log.error(msg, e); } catch (SQLException e) {
throw new PolicyComplianceException(msg, e); throw new PolicyComplianceException("Error occurred while opening a connection to the data source", e);
} finally {
PolicyManagementDAOFactory.closeConnection();
} }
return complianceData; return complianceData;
} }
@Override @Override
public void addMonitoringOperation(List<Device> devices) throws PolicyComplianceException { public void addMonitoringOperation(List<Device> devices) throws PolicyComplianceException {
try { try {
ComplianceDecisionPoint decisionPoint = new ComplianceDecisionPointImpl(); ComplianceDecisionPoint decisionPoint = new ComplianceDecisionPointImpl();
//int tenantId = PolicyManagerUtil.getTenantId(); //int tenantId = PolicyManagerUtil.getTenantId();
@ -294,7 +267,6 @@ public class MonitoringManagerImpl implements MonitoringManager {
} }
} }
PolicyManagementDAOFactory.beginTransaction(); PolicyManagementDAOFactory.beginTransaction();
if (!deviceIdsToAddOperation.isEmpty()) { if (!deviceIdsToAddOperation.isEmpty()) {
@ -310,39 +282,20 @@ public class MonitoringManagerImpl implements MonitoringManager {
} }
PolicyManagementDAOFactory.commitTransaction(); PolicyManagementDAOFactory.commitTransaction();
} catch (MonitoringDAOException e) { } catch (MonitoringDAOException e) {
try {
PolicyManagementDAOFactory.rollbackTransaction(); PolicyManagementDAOFactory.rollbackTransaction();
} catch (PolicyManagerDAOException e1) { throw new PolicyComplianceException("Error occurred from monitoring dao.", e);
log.warn("Error occurred while roll backing the transaction.");
}
String msg = "Error occurred from monitoring dao.";
log.error(msg, e);
throw new PolicyComplianceException(msg, e);
} catch (OperationManagementException e) { } catch (OperationManagementException e) {
try {
PolicyManagementDAOFactory.rollbackTransaction(); PolicyManagementDAOFactory.rollbackTransaction();
} catch (PolicyManagerDAOException e1) { throw new PolicyComplianceException("Error occurred while adding monitoring operation to devices", e);
log.warn("Error occurred while roll backing the transaction.");
}
String msg = "Error occurred while adding monitoring operation to devices";
log.error(msg, e);
throw new PolicyComplianceException(msg, e);
} catch (PolicyManagerDAOException e) { } catch (PolicyManagerDAOException e) {
try {
PolicyManagementDAOFactory.rollbackTransaction(); PolicyManagementDAOFactory.rollbackTransaction();
} catch (PolicyManagerDAOException e1) { throw new PolicyComplianceException("Error occurred reading the applied policies to devices.", e);
log.warn("Error occurred while roll backing the transaction."); } finally {
PolicyManagementDAOFactory.closeConnection();
} }
String msg = "Error occurred reading the applied policies to devices.";
log.error(msg, e);
throw new PolicyComplianceException(msg, e);
} }
}
private void addMonitoringOperationsToDatabase(List<Device> devices) private void addMonitoringOperationsToDatabase(List<Device> devices)
throws PolicyComplianceException, OperationManagementException { throws PolicyComplianceException, OperationManagementException {
@ -359,7 +312,6 @@ public class MonitoringManagerImpl implements MonitoringManager {
} }
private List<DeviceIdentifier> getDeviceIdentifiersFromDevices(List<Device> devices) { private List<DeviceIdentifier> getDeviceIdentifiersFromDevices(List<Device> devices) {
List<DeviceIdentifier> deviceIdentifiers = new ArrayList<>(); List<DeviceIdentifier> deviceIdentifiers = new ArrayList<>();
for (Device device : devices) { for (Device device : devices) {
DeviceIdentifier identifier = new DeviceIdentifier(); DeviceIdentifier identifier = new DeviceIdentifier();

@ -20,7 +20,6 @@ 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.CarbonContext;
import org.wso2.carbon.context.PrivilegedCarbonContext; 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;
@ -34,8 +33,8 @@ import org.wso2.carbon.policy.mgt.common.*;
import org.wso2.carbon.policy.mgt.core.dao.*; import org.wso2.carbon.policy.mgt.core.dao.*;
import org.wso2.carbon.policy.mgt.core.mgt.PolicyManager; import org.wso2.carbon.policy.mgt.core.mgt.PolicyManager;
import org.wso2.carbon.policy.mgt.core.mgt.ProfileManager; import org.wso2.carbon.policy.mgt.core.mgt.ProfileManager;
import org.wso2.carbon.policy.mgt.core.util.PolicyManagerUtil;
import java.sql.SQLException;
import java.sql.Timestamp; import java.sql.Timestamp;
import java.util.ArrayList; import java.util.ArrayList;
import java.util.Calendar; import java.util.Calendar;
@ -125,36 +124,20 @@ public class PolicyManagerImpl implements PolicyManager {
PolicyManagementDAOFactory.commitTransaction(); PolicyManagementDAOFactory.commitTransaction();
} catch (PolicyManagerDAOException e) { } catch (PolicyManagerDAOException e) {
try {
PolicyManagementDAOFactory.rollbackTransaction(); PolicyManagementDAOFactory.rollbackTransaction();
} catch (PolicyManagerDAOException e1) { throw new PolicyManagementException("Error occurred while adding the policy (" +
log.warn("Error occurred while roll backing the transaction."); policy.getId() + " - " + policy.getPolicyName() + ")", e);
}
String msg = "Error occurred while adding the policy (" +
policy.getId() + " - " + policy.getPolicyName() + ")";
log.error(msg, e);
throw new PolicyManagementException(msg, e);
} catch (ProfileManagerDAOException e) { } catch (ProfileManagerDAOException e) {
try {
PolicyManagementDAOFactory.rollbackTransaction(); PolicyManagementDAOFactory.rollbackTransaction();
} catch (PolicyManagerDAOException e1) { throw new PolicyManagementException("Error occurred while adding the profile related to policy (" +
log.warn("Error occurred while roll backing the transaction."); policy.getId() + " - " + policy.getPolicyName() + ")", e);
}
String msg = "Error occurred while adding the profile related to policy (" +
policy.getId() + " - " + policy.getPolicyName() + ")";
log.error(msg, e);
throw new PolicyManagementException(msg, e);
} catch (FeatureManagerDAOException e) { } catch (FeatureManagerDAOException e) {
try {
PolicyManagementDAOFactory.rollbackTransaction(); PolicyManagementDAOFactory.rollbackTransaction();
} catch (PolicyManagerDAOException e1) { throw new PolicyManagementException("Error occurred while adding the features of profile related to " +
log.warn("Error occurred while roll backing the transaction."); "policy (" + policy.getId() + " - " + policy.getPolicyName() + ")", e);
} } finally {
String msg = "Error occurred while adding the features of profile related to policy (" + PolicyManagementDAOFactory.closeConnection();
policy.getId() + " - " + policy.getPolicyName() + ")";
log.error(msg, e);
throw new PolicyManagementException(msg, e);
} }
return policy; return policy;
} }
@ -207,17 +190,12 @@ public class PolicyManagerImpl implements PolicyManager {
// } // }
PolicyManagementDAOFactory.commitTransaction(); PolicyManagementDAOFactory.commitTransaction();
} catch (PolicyManagerDAOException e) { } catch (PolicyManagerDAOException e) {
try {
PolicyManagementDAOFactory.rollbackTransaction(); PolicyManagementDAOFactory.rollbackTransaction();
} catch (PolicyManagerDAOException e1) { throw new PolicyManagementException("Error occurred while updating the policy ("
log.warn("Error occurred while roll backing the transaction."); + policy.getId() + " - " + policy.getPolicyName() + ")", e);
} } finally {
String msg = "Error occurred while updating the policy (" PolicyManagementDAOFactory.closeConnection();
+ policy.getId() + " - " + policy.getPolicyName() + ")";
log.error(msg, e);
throw new PolicyManagementException(msg, e);
} }
return policy; return policy;
} }
@ -230,21 +208,16 @@ public class PolicyManagerImpl implements PolicyManager {
bool = policyDAO.updatePolicyPriorities(policies); bool = policyDAO.updatePolicyPriorities(policies);
PolicyManagementDAOFactory.commitTransaction(); PolicyManagementDAOFactory.commitTransaction();
} catch (PolicyManagerDAOException e) { } catch (PolicyManagerDAOException e) {
try {
PolicyManagementDAOFactory.rollbackTransaction(); PolicyManagementDAOFactory.rollbackTransaction();
} catch (PolicyManagerDAOException e1) { throw new PolicyManagementException("Error occurred while updating the policy priorities", e);
log.warn("Error occurred while roll backing the transaction."); } finally {
} PolicyManagementDAOFactory.closeConnection();
String msg = "Error occurred while updating the policy priorities";
log.error(msg, e);
throw new PolicyManagementException(msg, e);
} }
return bool; return bool;
} }
@Override @Override
public boolean deletePolicy(Policy policy) throws PolicyManagementException { public boolean deletePolicy(Policy policy) throws PolicyManagementException {
try { try {
PolicyManagementDAOFactory.beginTransaction(); PolicyManagementDAOFactory.beginTransaction();
policyDAO.deleteAllPolicyRelatedConfigs(policy.getId()); policyDAO.deleteAllPolicyRelatedConfigs(policy.getId());
@ -254,46 +227,28 @@ public class PolicyManagerImpl implements PolicyManager {
PolicyManagementDAOFactory.commitTransaction(); PolicyManagementDAOFactory.commitTransaction();
return true; return true;
} catch (PolicyManagerDAOException e) { } catch (PolicyManagerDAOException e) {
try {
PolicyManagementDAOFactory.rollbackTransaction(); PolicyManagementDAOFactory.rollbackTransaction();
} catch (PolicyManagerDAOException e1) { throw new PolicyManagementException("Error occurred while deleting the policy ("
log.warn("Error occurred while roll backing the transaction."); + policy.getId() + " - " + policy.getPolicyName() + ")", e);
}
String msg = "Error occurred while deleting the policy ("
+ policy.getId() + " - " + policy.getPolicyName() + ")";
log.error(msg, e);
throw new PolicyManagementException(msg, e);
} catch (ProfileManagerDAOException e) { } catch (ProfileManagerDAOException e) {
try {
PolicyManagementDAOFactory.rollbackTransaction(); PolicyManagementDAOFactory.rollbackTransaction();
} catch (PolicyManagerDAOException e1) { throw new PolicyManagementException("Error occurred while deleting the profile for policy ("
log.warn("Error occurred while roll backing the transaction."); + policy.getId() + ")", e);
}
String msg = "Error occurred while deleting the profile for policy ("
+ policy.getId() + ")";
log.error(msg, e);
throw new PolicyManagementException(msg, e);
} catch (FeatureManagerDAOException e) { } catch (FeatureManagerDAOException e) {
try {
PolicyManagementDAOFactory.rollbackTransaction(); PolicyManagementDAOFactory.rollbackTransaction();
} catch (PolicyManagerDAOException e1) { throw new PolicyManagementException("Error occurred while deleting the profile features for policy ("
log.warn("Error occurred while roll backing the transaction."); + policy.getId() + ")", e);
} finally {
PolicyManagementDAOFactory.closeConnection();
} }
String msg = "Error occurred while deleting the profile features for policy ("
+ policy.getId() + ")";
log.error(msg, e);
throw new PolicyManagementException(msg, e);
}
} }
@Override @Override
public boolean deletePolicy(int policyId) throws PolicyManagementException { public boolean deletePolicy(int policyId) throws PolicyManagementException {
try { try {
Policy policy = policyDAO.getPolicy(policyId);
PolicyManagementDAOFactory.beginTransaction(); PolicyManagementDAOFactory.beginTransaction();
Policy policy = policyDAO.getPolicy(policyId);
policyDAO.deleteAllPolicyRelatedConfigs(policyId); policyDAO.deleteAllPolicyRelatedConfigs(policyId);
policyDAO.deletePolicy(policyId); policyDAO.deletePolicy(policyId);
@ -306,50 +261,31 @@ public class PolicyManagerImpl implements PolicyManager {
profileDAO.deleteProfile(policy.getProfileId()); profileDAO.deleteProfile(policy.getProfileId());
PolicyManagementDAOFactory.commitTransaction(); PolicyManagementDAOFactory.commitTransaction();
return true; return true;
} catch (PolicyManagerDAOException e) { } catch (PolicyManagerDAOException e) {
try {
PolicyManagementDAOFactory.rollbackTransaction(); PolicyManagementDAOFactory.rollbackTransaction();
} catch (PolicyManagerDAOException e1) { throw new PolicyManagementException("Error occurred while deleting the policy (" + policyId + ")", e);
log.warn("Error occurred while roll backing the transaction.");
}
String msg = "Error occurred while deleting the policy ("
+ policyId + ")";
log.error(msg, e);
throw new PolicyManagementException(msg, e);
} catch (ProfileManagerDAOException e) { } catch (ProfileManagerDAOException e) {
try {
PolicyManagementDAOFactory.rollbackTransaction(); PolicyManagementDAOFactory.rollbackTransaction();
} catch (PolicyManagerDAOException e1) { throw new PolicyManagementException("Error occurred while deleting the profile for policy ("
log.warn("Error occurred while roll backing the transaction."); + policyId + ")", e);
}
String msg = "Error occurred while deleting the profile for policy ("
+ policyId + ")";
log.error(msg, e);
throw new PolicyManagementException(msg, e);
} catch (FeatureManagerDAOException e) { } catch (FeatureManagerDAOException e) {
try {
PolicyManagementDAOFactory.rollbackTransaction(); PolicyManagementDAOFactory.rollbackTransaction();
} catch (PolicyManagerDAOException e1) { throw new PolicyManagementException("Error occurred while deleting the profile features for policy ("
log.warn("Error occurred while roll backing the transaction."); + policyId + ")", e);
} } finally {
String msg = "Error occurred while deleting the profile features for policy (" PolicyManagementDAOFactory.closeConnection();
+ policyId + ")";
log.error(msg, e);
throw new PolicyManagementException(msg, e);
} }
} }
@Override @Override
public Policy addPolicyToDevice(List<DeviceIdentifier> deviceIdentifierList, Policy policy) public Policy addPolicyToDevice(List<DeviceIdentifier> deviceIdentifierList,
throws PolicyManagementException { Policy policy) throws PolicyManagementException {
try { try {
PolicyManagementDAOFactory.beginTransaction(); PolicyManagementDAOFactory.beginTransaction();
if (policy.getId() == 0) { if (policy.getId() == 0) {
policyDAO.addPolicy(policy); policyDAO.addPolicy(policy);
} }
List<Device> deviceList = new ArrayList<Device>(); List<Device> deviceList = new ArrayList<>();
DeviceManagementProviderService service = new DeviceManagementProviderServiceImpl(); DeviceManagementProviderService service = new DeviceManagementProviderServiceImpl();
for (DeviceIdentifier deviceIdentifier : deviceIdentifierList) { for (DeviceIdentifier deviceIdentifier : deviceIdentifierList) {
deviceList.add(service.getDevice(deviceIdentifier)); deviceList.add(service.getDevice(deviceIdentifier));
@ -368,34 +304,21 @@ public class PolicyManagerImpl implements PolicyManager {
} }
policy.setDevices(deviceList); policy.setDevices(deviceList);
} }
} catch (PolicyManagerDAOException e) { } catch (PolicyManagerDAOException e) {
try {
PolicyManagementDAOFactory.rollbackTransaction(); PolicyManagementDAOFactory.rollbackTransaction();
} catch (PolicyManagerDAOException e1) { throw new PolicyManagementException("Error occurred while adding the policy ("
log.warn("Error occurred while roll backing the transaction."); + policy.getId() + " - " + policy.getPolicyName() + ")", e);
}
String msg = "Error occurred while adding the policy ("
+ policy.getId() + " - " + policy.getPolicyName() + ")";
log.error(msg, e);
throw new PolicyManagementException(msg, e);
} catch (DeviceManagementException e) { } catch (DeviceManagementException e) {
try {
PolicyManagementDAOFactory.rollbackTransaction(); PolicyManagementDAOFactory.rollbackTransaction();
} catch (PolicyManagerDAOException e1) { throw new PolicyManagementException("Error occurred while adding the policy to device list", e);
log.warn("Error occurred while roll backing the transaction."); } finally {
PolicyManagementDAOFactory.closeConnection();
} }
String msg = "Error occurred while adding the policy to device list";
log.error(msg, e);
throw new PolicyManagementException(msg, e);
}
return policy; return policy;
} }
@Override @Override
public Policy addPolicyToRole(List<String> roleNames, Policy policy) throws PolicyManagementException { public Policy addPolicyToRole(List<String> roleNames, Policy policy) throws PolicyManagementException {
try { try {
PolicyManagementDAOFactory.beginTransaction(); PolicyManagementDAOFactory.beginTransaction();
if (policy.getId() == 0) { if (policy.getId() == 0) {
@ -415,19 +338,13 @@ public class PolicyManagerImpl implements PolicyManager {
} }
policy.setRoles(roleNames); policy.setRoles(roleNames);
} }
} catch (PolicyManagerDAOException e) { } catch (PolicyManagerDAOException e) {
try {
PolicyManagementDAOFactory.rollbackTransaction(); PolicyManagementDAOFactory.rollbackTransaction();
} catch (PolicyManagerDAOException e1) { throw new PolicyManagementException("Error occurred while adding the policy ("
log.warn("Error occurred while roll backing the transaction."); + policy.getId() + " - " + policy.getPolicyName() + ")", e);
} } finally {
String msg = "Error occurred while adding the policy (" PolicyManagementDAOFactory.closeConnection();
+ policy.getId() + " - " + policy.getPolicyName() + ")";
log.error(msg, e);
throw new PolicyManagementException(msg, e);
} }
return policy; return policy;
} }
@ -453,17 +370,12 @@ public class PolicyManagerImpl implements PolicyManager {
} }
policy.setRoles(usernameList); policy.setRoles(usernameList);
} }
} catch (PolicyManagerDAOException e) { } catch (PolicyManagerDAOException e) {
try {
PolicyManagementDAOFactory.rollbackTransaction(); PolicyManagementDAOFactory.rollbackTransaction();
} catch (PolicyManagerDAOException e1) { throw new PolicyManagementException("Error occurred while adding the policy ("
log.warn("Error occurred while roll backing the transaction."); + policy.getId() + " - " + policy.getPolicyName() + ") to user list.", e);
} } finally {
String msg = "Error occurred while adding the policy (" PolicyManagementDAOFactory.closeConnection();
+ policy.getId() + " - " + policy.getPolicyName() + ") to user list.";
log.error(msg, e);
throw new PolicyManagementException(msg, e);
} }
return policy; return policy;
} }
@ -477,6 +389,7 @@ public class PolicyManagerImpl implements PolicyManager {
List<String> roleNames; List<String> roleNames;
try { try {
PolicyManagementDAOFactory.openConnection();
policy = policyDAO.getPolicyByProfileID(profileId); policy = policyDAO.getPolicyByProfileID(profileId);
deviceList = getPolicyAppliedDevicesIds(policy.getId()); deviceList = getPolicyAppliedDevicesIds(policy.getId());
roleNames = policyDAO.getPolicyAppliedRoles(policy.getId()); roleNames = policyDAO.getPolicyAppliedRoles(policy.getId());
@ -491,13 +404,15 @@ public class PolicyManagerImpl implements PolicyManager {
policy.setDevices(deviceList); policy.setDevices(deviceList);
} catch (PolicyManagerDAOException e) { } catch (PolicyManagerDAOException e) {
String msg = "Error occurred while getting the policy related to profile ID (" + profileId + ")"; throw new PolicyManagementException("Error occurred while getting the policy related to profile ID (" +
log.error(msg, e); profileId + ")", e);
throw new PolicyManagementException(msg, e);
} catch (ProfileManagerDAOException e) { } catch (ProfileManagerDAOException e) {
String msg = "Error occurred while getting the profile related to profile ID (" + profileId + ")"; throw new PolicyManagementException("Error occurred while getting the profile related to profile ID (" +
log.error(msg, e); profileId + ")", e);
throw new PolicyManagementException(msg, e); } catch (SQLException e) {
throw new PolicyManagementException("Error occurred while opening a connection to the data source", e);
} finally {
PolicyManagementDAOFactory.closeConnection();
} }
return policy; return policy;
} }
@ -510,6 +425,7 @@ public class PolicyManagerImpl implements PolicyManager {
List<String> roleNames; List<String> roleNames;
try { try {
PolicyManagementDAOFactory.openConnection();
policy = policyDAO.getPolicy(policyId); policy = policyDAO.getPolicy(policyId);
deviceList = getPolicyAppliedDevicesIds(policyId); deviceList = getPolicyAppliedDevicesIds(policyId);
roleNames = policyDAO.getPolicyAppliedRoles(policyId); roleNames = policyDAO.getPolicyAppliedRoles(policyId);
@ -524,13 +440,15 @@ public class PolicyManagerImpl implements PolicyManager {
policy.setDevices(deviceList); policy.setDevices(deviceList);
} catch (PolicyManagerDAOException e) { } catch (PolicyManagerDAOException e) {
String msg = "Error occurred while getting the policy related to policy ID (" + policyId + ")"; throw new PolicyManagementException("Error occurred while getting the policy related to policy ID (" +
log.error(msg, e); policyId + ")", e);
throw new PolicyManagementException(msg, e);
} catch (ProfileManagerDAOException e) { } catch (ProfileManagerDAOException e) {
String msg = "Error occurred while getting the profile related to policy ID (" + policyId + ")"; throw new PolicyManagementException("Error occurred while getting the profile related to policy ID (" +
log.error(msg, e); policyId + ")", e);
throw new PolicyManagementException(msg, e); } catch (SQLException e) {
throw new PolicyManagementException("Error occurred while opening a connection to the data source", e);
} finally {
PolicyManagementDAOFactory.closeConnection();
} }
return policy; return policy;
} }
@ -541,6 +459,7 @@ public class PolicyManagerImpl implements PolicyManager {
List<Policy> policyList; List<Policy> policyList;
try { try {
PolicyManagementDAOFactory.openConnection();
policyList = policyDAO.getAllPolicies(); policyList = policyDAO.getAllPolicies();
// List<Profile> profileList = profileDAO.getAllProfiles(); // List<Profile> profileList = profileDAO.getAllProfiles();
List<Profile> profileList = profileManager.getAllProfiles(); List<Profile> profileList = profileManager.getAllProfiles();
@ -560,15 +479,14 @@ public class PolicyManagerImpl implements PolicyManager {
// policyDAO.getLocationsOfPolicy(policy); // policyDAO.getLocationsOfPolicy(policy);
} }
Collections.sort(policyList); Collections.sort(policyList);
} catch (PolicyManagerDAOException e) { } catch (PolicyManagerDAOException e) {
String msg = "Error occurred while getting all the policies."; throw new PolicyManagementException("Error occurred while getting all the policies.", e);
log.error(msg, e);
throw new PolicyManagementException(msg, e);
} catch (ProfileManagementException e) { } catch (ProfileManagementException e) {
String msg = "Error occurred while getting all the profiles."; throw new PolicyManagementException("Error occurred while getting all the profiles.", e);
log.error(msg, e); } catch (SQLException e) {
throw new PolicyManagementException(msg, e); throw new PolicyManagementException("Error occurred while opening a connection to the data source", e);
} finally {
PolicyManagementDAOFactory.closeConnection();
} }
return policyList; return policyList;
} }
@ -577,11 +495,24 @@ public class PolicyManagerImpl implements PolicyManager {
public List<Policy> getPoliciesOfDevice(DeviceIdentifier deviceIdentifier) throws PolicyManagementException { public List<Policy> getPoliciesOfDevice(DeviceIdentifier deviceIdentifier) throws PolicyManagementException {
List<Integer> policyIdList; List<Integer> policyIdList;
List<Policy> policies = new ArrayList<Policy>(); List<Policy> policies = new ArrayList<>();
try { try {
PolicyManagementDAOFactory.openConnection();
DeviceManagementProviderService service = new DeviceManagementProviderServiceImpl(); DeviceManagementProviderService service = new DeviceManagementProviderServiceImpl();
Device device = service.getDevice(deviceIdentifier); Device device = service.getDevice(deviceIdentifier);
policyIdList = policyDAO.getPolicyIdsOfDevice(device); policyIdList = policyDAO.getPolicyIdsOfDevice(device);
} catch (PolicyManagerDAOException e) {
throw new PolicyManagementException("Error occurred while getting the policies for device identifier (" +
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) {
throw new PolicyManagementException("Error occurred while open a data source connection", e);
} finally {
PolicyManagementDAOFactory.closeConnection();
}
List<Policy> tempPolicyList = this.getPolicies(); List<Policy> tempPolicyList = this.getPolicies();
for (Policy policy : tempPolicyList) { for (Policy policy : tempPolicyList) {
@ -593,25 +524,12 @@ public class PolicyManagerImpl implements PolicyManager {
} }
Collections.sort(policies); Collections.sort(policies);
} catch (PolicyManagerDAOException e) {
String msg = "Error occurred while getting the policies for device identifier (" +
deviceIdentifier.getId() + " - " + deviceIdentifier.getType() + ")";
log.error(msg, e);
throw new PolicyManagementException(msg, e);
} catch (DeviceManagementException e) {
String msg = "Error occurred while getting device related to device identifier (" +
deviceIdentifier.getId() + " - " + deviceIdentifier.getType() + ")";
log.error(msg, e);
throw new PolicyManagementException(msg, e);
}
return policies; return policies;
} }
@Override @Override
public List<Policy> getPoliciesOfDeviceType(String deviceTypeName) throws PolicyManagementException { public List<Policy> getPoliciesOfDeviceType(String deviceTypeName) throws PolicyManagementException {
List<Policy> policies = new ArrayList<>();
List<Policy> policies = new ArrayList<Policy>();
try { try {
List<Profile> profileList = profileManager.getProfilesOfDeviceType(deviceTypeName); List<Profile> profileList = profileManager.getProfilesOfDeviceType(deviceTypeName);
List<Policy> allPolicies = this.getPolicies(); List<Policy> allPolicies = this.getPolicies();
@ -626,9 +544,7 @@ public class PolicyManagerImpl implements PolicyManager {
} }
Collections.sort(policies); Collections.sort(policies);
} catch (ProfileManagementException e) { } catch (ProfileManagementException e) {
String msg = "Error occurred while getting all the profile features."; throw new PolicyManagementException("Error occurred while getting all the profile features.", e);
log.error(msg, e);
throw new PolicyManagementException(msg, e);
} }
return policies; return policies;
} }
@ -636,11 +552,21 @@ public class PolicyManagerImpl implements PolicyManager {
@Override @Override
public List<Policy> getPoliciesOfRole(String roleName) throws PolicyManagementException { public List<Policy> getPoliciesOfRole(String roleName) throws PolicyManagementException {
List<Policy> policies = new ArrayList<Policy>(); List<Policy> policies = new ArrayList<>();
List<Integer> policyIdList; List<Integer> policyIdList;
try { try {
PolicyManagementDAOFactory.openConnection();
policyIdList = policyDAO.getPolicyOfRole(roleName); policyIdList = policyDAO.getPolicyOfRole(roleName);
} catch (PolicyManagerDAOException e) {
throw new PolicyManagementException("Error occurred while getting the policies.", e);
} catch (SQLException e) {
throw new PolicyManagementException("Error occurred while open a data source connection", e);
} finally {
PolicyManagementDAOFactory.closeConnection();
}
List<Policy> tempPolicyList = this.getPolicies(); List<Policy> tempPolicyList = this.getPolicies();
for (Policy policy : tempPolicyList) { for (Policy policy : tempPolicyList) {
@ -650,12 +576,6 @@ public class PolicyManagerImpl implements PolicyManager {
} }
} }
} }
} catch (PolicyManagerDAOException e) {
String msg = "Error occurred while getting the policies.";
log.error(msg, e);
throw new PolicyManagementException(msg, e);
}
Collections.sort(policies); Collections.sort(policies);
return policies; return policies;
} }
@ -663,11 +583,19 @@ public class PolicyManagerImpl implements PolicyManager {
@Override @Override
public List<Policy> getPoliciesOfUser(String username) throws PolicyManagementException { public List<Policy> getPoliciesOfUser(String username) throws PolicyManagementException {
List<Policy> policies = new ArrayList<Policy>(); List<Policy> policies = new ArrayList<>();
List<Integer> policyIdList; List<Integer> policyIdList;
try { try {
PolicyManagementDAOFactory.openConnection();
policyIdList = policyDAO.getPolicyOfUser(username); policyIdList = policyDAO.getPolicyOfUser(username);
} catch (PolicyManagerDAOException e) {
throw new PolicyManagementException("Error occurred while getting the policies.", e);
} catch (SQLException e) {
throw new PolicyManagementException("Error occurred while open a data source connection", e);
} finally {
PolicyManagementDAOFactory.closeConnection();
}
List<Policy> tempPolicyList = this.getPolicies(); List<Policy> tempPolicyList = this.getPolicies();
for (Policy policy : tempPolicyList) { for (Policy policy : tempPolicyList) {
@ -677,53 +605,47 @@ public class PolicyManagerImpl implements PolicyManager {
} }
} }
} }
} catch (PolicyManagerDAOException e) {
String msg = "Error occurred while getting the policies.";
log.error(msg, e);
throw new PolicyManagementException(msg, e);
}
Collections.sort(policies); Collections.sort(policies);
return policies; return policies;
} }
@Override @Override
public List<Device> getPolicyAppliedDevicesIds(int policyId) throws PolicyManagementException { public List<Device> getPolicyAppliedDevicesIds(int policyId) throws PolicyManagementException {
List<Device> deviceList = new ArrayList<>();
List<Device> deviceList = new ArrayList<Device>();
List<Integer> deviceIds; List<Integer> deviceIds;
try { try {
PolicyManagementDAOFactory.openConnection();
int tenantId = PrivilegedCarbonContext.getThreadLocalCarbonContext().getTenantId(); int tenantId = PrivilegedCarbonContext.getThreadLocalCarbonContext().getTenantId();
deviceIds = policyDAO.getPolicyAppliedDevicesIds(policyId); deviceIds = policyDAO.getPolicyAppliedDevicesIds(policyId);
for (int deviceId : deviceIds) { for (int deviceId : deviceIds) {
//TODO FIX ME //TODO FIX ME
deviceList.add(deviceDAO.getDevice(new DeviceIdentifier(Integer.toString(deviceId), ""), tenantId)); deviceList.add(deviceDAO.getDevice(new DeviceIdentifier(Integer.toString(deviceId), ""), tenantId));
} }
} catch (PolicyManagerDAOException e) { } catch (PolicyManagerDAOException e) {
String msg = "Error occurred while getting the device ids related to policy id (" + policyId + ")"; throw new PolicyManagementException("Error occurred while getting the device ids related to policy id (" +
log.error(msg, e); policyId + ")", e);
throw new PolicyManagementException(msg, e);
} catch (DeviceManagementDAOException e) { } catch (DeviceManagementDAOException e) {
String msg = "Error occurred while getting the devices related to policy id (" + policyId + ")"; throw new PolicyManagementException("Error occurred while getting the devices related to policy id (" +
log.error(msg, e); policyId + ")", e);
throw new PolicyManagementException(msg, e); } catch (SQLException e) {
throw new PolicyManagementException("Error occurred while opening a connection to the data source", e);
} finally {
PolicyManagementDAOFactory.closeConnection();
} }
return deviceList; return deviceList;
} }
@Override @Override
public void addAppliedPolicyFeaturesToDevice(DeviceIdentifier deviceIdentifier, Policy policy) public void addAppliedPolicyFeaturesToDevice(DeviceIdentifier deviceIdentifier,
throws PolicyManagementException { Policy policy) throws PolicyManagementException {
int deviceId = -1; int deviceId = -1;
try { try {
DeviceManagementProviderService service = new DeviceManagementProviderServiceImpl(); DeviceManagementProviderService service = new DeviceManagementProviderServiceImpl();
Device device = service.getDevice(deviceIdentifier); Device device = service.getDevice(deviceIdentifier);
deviceId = device.getId(); deviceId = device.getId();
boolean exist = policyDAO.checkPolicyAvailable(deviceId);
PolicyManagementDAOFactory.beginTransaction(); PolicyManagementDAOFactory.beginTransaction();
boolean exist = policyDAO.checkPolicyAvailable(deviceId);
if (exist) { if (exist) {
policyDAO.updateEffectivePolicyToDevice(deviceId, policy); policyDAO.updateEffectivePolicyToDevice(deviceId, policy);
} else { } else {
@ -731,21 +653,15 @@ public class PolicyManagerImpl implements PolicyManager {
} }
PolicyManagementDAOFactory.commitTransaction(); PolicyManagementDAOFactory.commitTransaction();
} catch (PolicyManagerDAOException e) { } catch (PolicyManagerDAOException e) {
try {
PolicyManagementDAOFactory.rollbackTransaction(); PolicyManagementDAOFactory.rollbackTransaction();
} catch (PolicyManagerDAOException e1) { throw new PolicyManagementException("Error occurred while adding the evaluated policy to device (" +
log.warn("Error occurred while roll backing the transaction."); deviceId + " - " + policy.getId() + ")", e);
}
String msg = "Error occurred while adding the evaluated policy to device (" +
deviceId + " - " + policy.getId() + ")";
log.error(msg, e);
throw new PolicyManagementException(msg, e);
} catch (DeviceManagementException e) { } catch (DeviceManagementException e) {
String msg = "Error occurred while getting the device details (" + deviceIdentifier.getId() + ")"; throw new PolicyManagementException("Error occurred while getting the device details (" +
log.error(msg, e); deviceIdentifier.getId() + ")", e);
throw new PolicyManagementException(msg, e); } finally {
PolicyManagementDAOFactory.closeConnection();
} }
} }
@Override @Override
@ -758,9 +674,10 @@ public class PolicyManagerImpl implements PolicyManager {
Device device = service.getDevice(deviceIdentifier); Device device = service.getDevice(deviceIdentifier);
deviceId = device.getId(); deviceId = device.getId();
// boolean exist = policyDAO.checkPolicyAvailable(deviceId); // boolean exist = policyDAO.checkPolicyAvailable(deviceId);
Policy policySaved = policyDAO.getAppliedPolicy(deviceId);
PolicyManagementDAOFactory.beginTransaction(); PolicyManagementDAOFactory.beginTransaction();
Policy policySaved = policyDAO.getAppliedPolicy(deviceId);
if (policySaved != null && policySaved.getId() != 0) { if (policySaved != null && policySaved.getId() != 0) {
if (policy.getId() != policySaved.getId()){ if (policy.getId() != policySaved.getId()){
policyDAO.updateEffectivePolicyToDevice(deviceId, policy); policyDAO.updateEffectivePolicyToDevice(deviceId, policy);
@ -770,24 +687,15 @@ public class PolicyManagerImpl implements PolicyManager {
} }
PolicyManagementDAOFactory.commitTransaction(); PolicyManagementDAOFactory.commitTransaction();
} catch (PolicyManagerDAOException e) { } catch (PolicyManagerDAOException e) {
try {
PolicyManagementDAOFactory.rollbackTransaction(); PolicyManagementDAOFactory.rollbackTransaction();
} catch (PolicyManagerDAOException e1) { throw new PolicyManagementException("Error occurred while adding the evaluated policy to device (" +
log.warn("Error occurred while roll backing the transaction."); deviceId + " - " + policy.getId() + ")", e);
}
String msg = "Error occurred while adding the evaluated policy to device (" +
deviceId + " - " + policy.getId() + ")";
log.error(msg, e);
throw new PolicyManagementException(msg, e);
} catch (DeviceManagementException e) { } catch (DeviceManagementException e) {
try {
PolicyManagementDAOFactory.rollbackTransaction(); PolicyManagementDAOFactory.rollbackTransaction();
} catch (PolicyManagerDAOException e1) { throw new PolicyManagementException("Error occurred while getting the device details (" +
log.warn("Error occurred while roll backing the transaction."); deviceIdentifier.getId() + ")", e);
} } finally {
String msg = "Error occurred while getting the device details (" + deviceIdentifier.getId() + ")"; PolicyManagementDAOFactory.closeConnection();
log.error(msg, e);
throw new PolicyManagementException(msg, e);
} }
} }
@ -796,74 +704,82 @@ public class PolicyManagerImpl implements PolicyManager {
boolean exist; boolean exist;
try { try {
PolicyManagementDAOFactory.openConnection();
DeviceManagementProviderService service = new DeviceManagementProviderServiceImpl(); DeviceManagementProviderService service = new DeviceManagementProviderServiceImpl();
Device device = service.getDevice(deviceIdentifier); Device device = service.getDevice(deviceIdentifier);
exist = policyDAO.checkPolicyAvailable(device.getId()); exist = policyDAO.checkPolicyAvailable(device.getId());
} catch (PolicyManagerDAOException e) { } catch (PolicyManagerDAOException e) {
String msg = "Error occurred while checking whether device has a policy to apply."; throw new PolicyManagementException("Error occurred while checking whether device has a policy " +
log.error(msg, e); "to apply.", e);
throw new PolicyManagementException(msg, e);
} catch (DeviceManagementException e) { } catch (DeviceManagementException e) {
String msg = "Error occurred while getting the device details (" + deviceIdentifier.getId() + ")"; throw new PolicyManagementException("Error occurred while getting the device details (" +
log.error(msg, e); deviceIdentifier.getId() + ")", e);
throw new PolicyManagementException(msg, e); } catch (SQLException e) {
throw new PolicyManagementException("Error occurred while opening a connection to the data source", e);
} finally {
PolicyManagementDAOFactory.closeConnection();
} }
return exist; return exist;
} }
@Override @Override
public boolean setPolicyApplied(DeviceIdentifier deviceIdentifier) throws PolicyManagementException { public boolean setPolicyApplied(DeviceIdentifier deviceIdentifier) throws PolicyManagementException {
try { try {
PolicyManagementDAOFactory.openConnection();
DeviceManagementProviderService service = new DeviceManagementProviderServiceImpl(); DeviceManagementProviderService service = new DeviceManagementProviderServiceImpl();
Device device = service.getDevice(deviceIdentifier); Device device = service.getDevice(deviceIdentifier);
policyDAO.setPolicyApplied(device.getId()); policyDAO.setPolicyApplied(device.getId());
return true; return true;
} catch (PolicyManagerDAOException e) { } catch (PolicyManagerDAOException e) {
String msg = "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() + ")"; deviceIdentifier.getId() + ")", e);
log.error(msg, e);
throw new PolicyManagementException(msg, e);
} catch (DeviceManagementException e) { } catch (DeviceManagementException e) {
String msg = "Error occurred while getting the device details (" + deviceIdentifier.getId() + ")"; throw new PolicyManagementException("Error occurred while getting the device details (" +
log.error(msg, e); deviceIdentifier.getId() + ")", e);
throw new PolicyManagementException(msg, e); } catch (SQLException e) {
throw new PolicyManagementException("Error occurred while opening a connection to the data source", e);
} finally {
PolicyManagementDAOFactory.closeConnection();
} }
} }
@Override @Override
public int getPolicyCount() throws PolicyManagementException { public int getPolicyCount() throws PolicyManagementException {
int policyCount = 0; int policyCount;
try { try {
PolicyManagementDAOFactory.openConnection();
policyCount = policyDAO.getPolicyCount(); policyCount = policyDAO.getPolicyCount();
return policyCount; return policyCount;
} catch (PolicyManagerDAOException e) { } catch (PolicyManagerDAOException e) {
String msg = "Error occurred while getting policy count"; throw new PolicyManagementException("Error occurred while getting policy count", e);
log.error(msg, e); } catch (SQLException e) {
throw new PolicyManagementException(msg, e); throw new PolicyManagementException("Error occurred while opening a connection to the data source", e);
} finally {
PolicyManagementDAOFactory.closeConnection();
} }
} }
@Override @Override
public Policy getAppliedPolicyToDevice(DeviceIdentifier deviceIdentifier) throws PolicyManagementException { public Policy getAppliedPolicyToDevice(DeviceIdentifier deviceIdentifier) throws PolicyManagementException {
Policy policy; Policy policy;
try { try {
PolicyManagementDAOFactory.openConnection();
DeviceManagementProviderService service = new DeviceManagementProviderServiceImpl(); DeviceManagementProviderService service = new DeviceManagementProviderServiceImpl();
Device device = service.getDevice(deviceIdentifier); Device device = service.getDevice(deviceIdentifier);
//int policyId = policyDAO.getAppliedPolicyId(device.getId()); //int policyId = policyDAO.getAppliedPolicyId(device.getId());
policy = policyDAO.getAppliedPolicy(device.getId()); policy = policyDAO.getAppliedPolicy(device.getId());
} catch (DeviceManagementException e) { } catch (DeviceManagementException e) {
String msg = "Error occurred while getting device id."; throw new PolicyManagementException("Error occurred while getting device id.", e);
log.error(msg, e);
throw new PolicyManagementException(msg, e);
} catch (PolicyManagerDAOException e) { } catch (PolicyManagerDAOException e) {
String msg = "Error occurred while getting policy id or policy."; throw new PolicyManagementException("Error occurred while getting policy id or policy.", e);
log.error(msg, e); } catch (SQLException e) {
throw new PolicyManagementException(msg, e); throw new PolicyManagementException("Error occurred while opening a connection to the data source", e);
} finally {
PolicyManagementDAOFactory.closeConnection();
} }
return policy; return policy;
} }
} }

@ -31,6 +31,7 @@ import org.wso2.carbon.policy.mgt.common.ProfileManagementException;
import org.wso2.carbon.policy.mgt.core.dao.*; import org.wso2.carbon.policy.mgt.core.dao.*;
import org.wso2.carbon.policy.mgt.core.mgt.ProfileManager; import org.wso2.carbon.policy.mgt.core.mgt.ProfileManager;
import java.sql.SQLException;
import java.sql.Timestamp; import java.sql.Timestamp;
import java.util.ArrayList; import java.util.ArrayList;
import java.util.Calendar; import java.util.Calendar;
@ -64,32 +65,19 @@ public class ProfileManagerImpl implements ProfileManager {
featureDAO.addProfileFeatures(profile.getProfileFeaturesList(), profile.getProfileId()); featureDAO.addProfileFeatures(profile.getProfileFeaturesList(), profile.getProfileId());
PolicyManagementDAOFactory.commitTransaction(); PolicyManagementDAOFactory.commitTransaction();
} catch (ProfileManagerDAOException e) { } catch (ProfileManagerDAOException e) {
try {
PolicyManagementDAOFactory.rollbackTransaction(); PolicyManagementDAOFactory.rollbackTransaction();
} catch (PolicyManagerDAOException e1) { throw new ProfileManagementException("Error occurred while adding the profile (" +
log.warn("Error occurred while roll backing the transaction."); profile.getProfileName() + ")", e);
}
String msg = "Error occurred while adding the profile (" + profile.getProfileName() + ")";
log.error(msg, e);
throw new ProfileManagementException(msg, e);
} catch (FeatureManagerDAOException e) { } catch (FeatureManagerDAOException e) {
try {
PolicyManagementDAOFactory.rollbackTransaction(); PolicyManagementDAOFactory.rollbackTransaction();
} catch (PolicyManagerDAOException e1) { throw new ProfileManagementException("Error occurred while adding the profile features (" +
log.warn("Error occurred while roll backing the transaction."); profile.getProfileName() + ")", e);
}
String msg = "Error occurred while adding the profile features (" + profile.getProfileName() + ")";
log.error(msg, e);
throw new ProfileManagementException(msg, e);
} catch (PolicyManagerDAOException e) { } catch (PolicyManagerDAOException e) {
try {
PolicyManagementDAOFactory.rollbackTransaction(); PolicyManagementDAOFactory.rollbackTransaction();
} catch (PolicyManagerDAOException e1) { throw new ProfileManagementException("Error occurred while adding the profile (" +
log.warn("Error occurred while roll backing the transaction."); profile.getProfileName() + ") to the database", e);
} } finally {
String msg = "Error occurred while adding the profile (" + profile.getProfileName() + ") to the database"; PolicyManagementDAOFactory.closeConnection();
log.error(msg, e);
throw new ProfileManagementException(msg, e);
} }
return profile; return profile;
@ -107,32 +95,19 @@ public class ProfileManagerImpl implements ProfileManager {
featureDAO.updateProfileFeatures(profile.getProfileFeaturesList(), profile.getProfileId()); featureDAO.updateProfileFeatures(profile.getProfileFeaturesList(), profile.getProfileId());
PolicyManagementDAOFactory.commitTransaction(); PolicyManagementDAOFactory.commitTransaction();
} catch (ProfileManagerDAOException e) { } catch (ProfileManagerDAOException e) {
try {
PolicyManagementDAOFactory.rollbackTransaction(); PolicyManagementDAOFactory.rollbackTransaction();
} catch (PolicyManagerDAOException e1) { throw new ProfileManagementException("Error occurred while updating the profile (" +
log.warn("Error occurred while roll backing the transaction."); profile.getProfileName() + ")", e);
}
String msg = "Error occurred while updating the profile (" + profile.getProfileName() + ")";
log.error(msg, e);
throw new ProfileManagementException(msg, e);
} catch (FeatureManagerDAOException e) { } catch (FeatureManagerDAOException e) {
try {
PolicyManagementDAOFactory.rollbackTransaction(); PolicyManagementDAOFactory.rollbackTransaction();
} catch (PolicyManagerDAOException e1) { throw new ProfileManagementException("Error occurred while updating the profile features (" +
log.warn("Error occurred while roll backing the transaction."); profile.getProfileName() + ")", e);
}
String msg = "Error occurred while updating the profile features (" + profile.getProfileName() + ")";
log.error(msg, e);
throw new ProfileManagementException(msg, e);
} catch (PolicyManagerDAOException e) { } catch (PolicyManagerDAOException e) {
try {
PolicyManagementDAOFactory.rollbackTransaction(); PolicyManagementDAOFactory.rollbackTransaction();
} catch (PolicyManagerDAOException e1) { throw new ProfileManagementException("Error occurred while updating the profile (" +
log.warn("Error occurred while roll backing the transaction."); profile.getProfileName() + ") to the database", e);
} } finally {
String msg = "Error occurred while updating the profile (" + profile.getProfileName() + ") to the database"; PolicyManagementDAOFactory.closeConnection();
log.error(msg, e);
throw new ProfileManagementException(msg, e);
} }
return profile; return profile;
@ -147,32 +122,19 @@ public class ProfileManagerImpl implements ProfileManager {
bool = profileDAO.deleteProfile(profile); bool = profileDAO.deleteProfile(profile);
PolicyManagementDAOFactory.commitTransaction(); PolicyManagementDAOFactory.commitTransaction();
} catch (ProfileManagerDAOException e) { } catch (ProfileManagerDAOException e) {
try {
PolicyManagementDAOFactory.rollbackTransaction(); PolicyManagementDAOFactory.rollbackTransaction();
} catch (PolicyManagerDAOException e1) { throw new ProfileManagementException("Error occurred while deleting the profile (" +
log.warn("Error occurred while roll backing the transaction."); profile.getProfileName() + ")", e);
}
String msg = "Error occurred while deleting the profile (" + profile.getProfileName() + ")";
log.error(msg, e);
throw new ProfileManagementException(msg, e);
} catch (FeatureManagerDAOException e) { } catch (FeatureManagerDAOException e) {
try {
PolicyManagementDAOFactory.rollbackTransaction(); PolicyManagementDAOFactory.rollbackTransaction();
} catch (PolicyManagerDAOException e1) { throw new ProfileManagementException("Error occurred while deleting the features from profile (" +
log.warn("Error occurred while roll backing the transaction."); profile.getProfileName() + ")", e);
}
String msg = "Error occurred while deleting the features from profile (" + profile.getProfileName() + ")";
log.error(msg, e);
throw new ProfileManagementException(msg, e);
} catch (PolicyManagerDAOException e) { } catch (PolicyManagerDAOException e) {
try {
PolicyManagementDAOFactory.rollbackTransaction(); PolicyManagementDAOFactory.rollbackTransaction();
} catch (PolicyManagerDAOException e1) { throw new ProfileManagementException("Error occurred while deleting the profile (" +
log.warn("Error occurred while roll backing the transaction."); profile.getProfileName() + ") from database", e);
} } finally {
String msg = "Error occurred while deleting the profile (" + profile.getProfileName() + ") from database"; PolicyManagementDAOFactory.closeConnection();
log.error(msg, e);
throw new ProfileManagementException(msg, e);
} }
return bool; return bool;
} }
@ -184,6 +146,7 @@ public class ProfileManagerImpl implements ProfileManager {
DeviceType deviceType; DeviceType deviceType;
try { try {
PolicyManagementDAOFactory.openConnection();
profile = profileDAO.getProfiles(profileId); profile = profileDAO.getProfiles(profileId);
featureList = featureDAO.getFeaturesForProfile(profileId); featureList = featureDAO.getFeaturesForProfile(profileId);
deviceType = deviceTypeDAO.getDeviceType(profile.getDeviceType().getId()); deviceType = deviceTypeDAO.getDeviceType(profile.getDeviceType().getId());
@ -192,17 +155,17 @@ public class ProfileManagerImpl implements ProfileManager {
profile.setDeviceType(deviceType); profile.setDeviceType(deviceType);
} catch (ProfileManagerDAOException e) { } catch (ProfileManagerDAOException e) {
String msg = "Error occurred while getting profile id (" + profileId + ")"; throw new ProfileManagementException("Error occurred while getting profile id (" + profileId + ")", e);
log.error(msg, e);
throw new ProfileManagementException(msg, e);
} catch (FeatureManagerDAOException e) { } catch (FeatureManagerDAOException e) {
String msg = "Error occurred while getting features related profile id (" + profileId + ")"; throw new ProfileManagementException("Error occurred while getting features related profile id (" +
log.error(msg, e); profileId + ")", e);
throw new ProfileManagementException(msg, e);
} catch (DeviceManagementDAOException e) { } catch (DeviceManagementDAOException e) {
String msg = "Error occurred while getting device type related profile id (" + profileId + ")"; throw new ProfileManagementException("Error occurred while getting device type related profile id (" +
log.error(msg, e); profileId + ")", e);
throw new ProfileManagementException(msg, e); } catch (SQLException e) {
throw new ProfileManagementException("Error occurred while opening a connection to the data source", e);
} finally {
PolicyManagementDAOFactory.closeConnection();
} }
return profile; return profile;
} }
@ -211,6 +174,7 @@ public class ProfileManagerImpl implements ProfileManager {
public List<Profile> getAllProfiles() throws ProfileManagementException { public List<Profile> getAllProfiles() throws ProfileManagementException {
List<Profile> profileList; List<Profile> profileList;
try { try {
PolicyManagementDAOFactory.openConnection();
profileList = profileDAO.getAllProfiles(); profileList = profileDAO.getAllProfiles();
List<ProfileFeature> featureList = featureDAO.getAllProfileFeatures(); List<ProfileFeature> featureList = featureDAO.getAllProfileFeatures();
List<DeviceType> deviceTypes = deviceTypeDAO.getDeviceTypes(); List<DeviceType> deviceTypes = deviceTypeDAO.getDeviceTypes();
@ -231,17 +195,15 @@ public class ProfileManagerImpl implements ProfileManager {
} }
} }
} catch (ProfileManagerDAOException e) { } catch (ProfileManagerDAOException e) {
String msg = "Error occurred while getting profiles"; throw new ProfileManagementException("Error occurred while getting profiles", e);
log.error(msg, e);
throw new ProfileManagementException(msg, e);
} catch (FeatureManagerDAOException e) { } catch (FeatureManagerDAOException e) {
String msg = "Error occurred while getting features related to profiles"; throw new ProfileManagementException("Error occurred while getting features related to profiles", e);
log.error(msg, e);
throw new ProfileManagementException(msg, e);
} catch (DeviceManagementDAOException e) { } catch (DeviceManagementDAOException e) {
String msg = "Error occurred while getting device types related to profiles"; throw new ProfileManagementException("Error occurred while getting device types related to profiles", e);
log.error(msg, e); } catch (SQLException e) {
throw new ProfileManagementException(msg, e); throw new ProfileManagementException("Error occurred while opening a connection to the data source", e);
} finally {
PolicyManagementDAOFactory.closeConnection();
} }
return profileList; return profileList;
} }
@ -251,6 +213,7 @@ public class ProfileManagerImpl implements ProfileManager {
List<Profile> profileList; List<Profile> profileList;
List<ProfileFeature> featureList; List<ProfileFeature> featureList;
try { try {
PolicyManagementDAOFactory.openConnection();
DeviceType deviceType = deviceTypeDAO.getDeviceType(deviceTypeName); DeviceType deviceType = deviceTypeDAO.getDeviceType(deviceTypeName);
profileList = profileDAO.getProfilesOfDeviceType(deviceType); profileList = profileDAO.getProfilesOfDeviceType(deviceType);
featureList = featureDAO.getAllProfileFeatures(); featureList = featureDAO.getAllProfileFeatures();
@ -266,18 +229,17 @@ public class ProfileManagerImpl implements ProfileManager {
} }
} catch (ProfileManagerDAOException e) { } catch (ProfileManagerDAOException e) {
String msg = "Error occurred while getting profiles"; throw new ProfileManagementException("Error occurred while getting profiles", e);
log.error(msg, e);
throw new ProfileManagementException(msg, e);
} catch (DeviceManagementDAOException e) { } catch (DeviceManagementDAOException e) {
String msg = "Error occurred while getting device types"; throw new ProfileManagementException("Error occurred while getting device types", e);
log.error(msg, e);
throw new ProfileManagementException(msg, e);
} catch (FeatureManagerDAOException e) { } catch (FeatureManagerDAOException e) {
String msg = "Error occurred while getting profile features types"; throw new ProfileManagementException("Error occurred while getting profile features types", e);
log.error(msg, e); } catch (SQLException e) {
throw new ProfileManagementException(msg, e); throw new ProfileManagementException("Error occurred while opening a connection to the data source", e);
} finally {
PolicyManagementDAOFactory.closeConnection();
} }
return profileList; return profileList;
} }
} }

Loading…
Cancel
Save