From cdbdc86f42b350c24e9d059ff7c23e1f09242df4 Mon Sep 17 00:00:00 2001 From: prabathabey Date: Sat, 22 Aug 2015 03:56:44 +0530 Subject: [PATCH] Fixing transactional anomalies and code cleanup --- .../DeviceManagementProviderServiceImpl.java | 271 +++++++--- .../DeviceManagementProviderServiceTest.java | 63 ++- .../src/test/resources/testng.xml | 2 +- .../core/dao/PolicyManagementDAOFactory.java | 16 +- .../mgt/core/dao/impl/FeatureDAOImpl.java | 82 +-- .../mgt/core/dao/impl/MonitoringDAOImpl.java | 12 +- .../mgt/core/dao/impl/PolicyDAOImpl.java | 336 +++--------- .../mgt/core/dao/impl/ProfileDAOImpl.java | 12 - .../mgt/core/mgt/impl/FeatureManagerImpl.java | 188 +++---- .../core/mgt/impl/MonitoringManagerImpl.java | 156 ++---- .../mgt/core/mgt/impl/PolicyManagerImpl.java | 496 ++++++++---------- .../mgt/core/mgt/impl/ProfileManagerImpl.java | 160 +++--- 12 files changed, 704 insertions(+), 1090 deletions(-) diff --git a/components/device-mgt/org.wso2.carbon.device.mgt.core/src/main/java/org/wso2/carbon/device/mgt/core/service/DeviceManagementProviderServiceImpl.java b/components/device-mgt/org.wso2.carbon.device.mgt.core/src/main/java/org/wso2/carbon/device/mgt/core/service/DeviceManagementProviderServiceImpl.java index f50c146a84..197a9321e3 100644 --- a/components/device-mgt/org.wso2.carbon.device.mgt.core/src/main/java/org/wso2/carbon/device/mgt/core/service/DeviceManagementProviderServiceImpl.java +++ b/components/device-mgt/org.wso2.carbon.device.mgt.core/src/main/java/org/wso2/carbon/device/mgt/core/service/DeviceManagementProviderServiceImpl.java @@ -84,28 +84,48 @@ public class DeviceManagementProviderServiceImpl implements DeviceManagementProv } @Override - public TenantConfiguration getConfiguration(String type) throws DeviceManagementException { + public TenantConfiguration getConfiguration(String deviceType) throws DeviceManagementException { 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(); } @Override - public FeatureManager getFeatureManager(String type) { - DeviceManager dms = - this.getPluginRepository().getDeviceManagementService(type).getDeviceManager(); - return dms.getFeatureManager(); + public FeatureManager getFeatureManager(String deviceType) { + DeviceManager deviceManager = this.getDeviceManager(deviceType); + if (deviceManager == null) { + 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 public boolean enrollDevice(Device device) throws DeviceManagementException { boolean status = false; 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); } else { device.getEnrolmentInfo().setStatus(EnrolmentInfo.Status.ACTIVE); @@ -176,9 +196,15 @@ public class DeviceManagementProviderServiceImpl implements DeviceManagementProv @Override public boolean modifyEnrollment(Device device) throws DeviceManagementException { - DeviceManager dms = - this.getPluginRepository().getDeviceManagementService(device.getType()).getDeviceManager(); - boolean status = dms.modifyEnrollment(device); + 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 'modifyEnrolment'"); + } + return false; + } + boolean status = deviceManager.modifyEnrollment(device); try { int tenantId = this.getTenantId(); DeviceManagementDAOFactory.beginTransaction(); @@ -200,11 +226,16 @@ public class DeviceManagementProviderServiceImpl implements DeviceManagementProv @Override public boolean disenrollDevice(DeviceIdentifier deviceId) throws DeviceManagementException { - - int tenantId = this.getTenantId(); - DeviceManager dms = - this.getPluginRepository().getDeviceManagementService(deviceId.getType()).getDeviceManager(); + DeviceManager deviceManager = this.getDeviceManager(deviceId.getType()); + if (deviceManager == null) { + if (log.isDebugEnabled()) { + log.debug("Device Manager associated with the device type '" + deviceId.getType() + "' is null. " + + "Therefore, not attempting method 'dis-enrollDevice'"); + } + return false; + } try { + int tenantId = this.getTenantId(); DeviceManagementDAOFactory.beginTransaction(); Device device = deviceDAO.getDevice(deviceId, tenantId); @@ -218,33 +249,51 @@ public class DeviceManagementProviderServiceImpl implements DeviceManagementProv DeviceManagementDAOFactory.commitTransaction(); } catch (DeviceManagementDAOException | TransactionManagementException e) { 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); } finally { DeviceManagementDAOFactory.closeConnection(); } - return dms.disenrollDevice(deviceId); + return deviceManager.disenrollDevice(deviceId); } @Override public boolean isEnrolled(DeviceIdentifier deviceId) throws DeviceManagementException { - DeviceManager dms = - this.getPluginRepository().getDeviceManagementService(deviceId.getType()).getDeviceManager(); - return dms.isEnrolled(deviceId); + DeviceManager deviceManager = this.getDeviceManager(deviceId.getType()); + if (deviceManager == null) { + 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 public boolean isActive(DeviceIdentifier deviceId) throws DeviceManagementException { - DeviceManager dms = - this.getPluginRepository().getDeviceManagementService(deviceId.getType()).getDeviceManager(); - return dms.isActive(deviceId); + DeviceManager deviceManager = this.getDeviceManager(deviceId.getType()); + if (deviceManager == null) { + 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 public boolean setActive(DeviceIdentifier deviceId, boolean status) throws DeviceManagementException { - DeviceManager dms = - this.getPluginRepository().getDeviceManagementService(deviceId.getType()).getDeviceManager(); - return dms.setActive(deviceId, status); + DeviceManager deviceManager = this.getDeviceManager(deviceId.getType()); + if (deviceManager == null) { + 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 @@ -262,15 +311,20 @@ public class DeviceManagementProviderServiceImpl implements DeviceManagementProv DeviceManagementDAOFactory.closeConnection(); } for (Device device : allDevices) { - DeviceManagementService managementService = this.getPluginRepository(). - getDeviceManagementService(device.getType()); - if (managementService != null) { - Device dmsDevice = managementService.getDeviceManager().getDevice( - new DeviceIdentifier(device.getDeviceIdentifier(), device.getType())); - if (dmsDevice != null) { - device.setFeatures(dmsDevice.getFeatures()); - device.setProperties(dmsDevice.getProperties()); + 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 = + deviceManager.getDevice(new DeviceIdentifier(device.getDeviceIdentifier(), device.getType())); + if (dmsDevice != null) { + device.setFeatures(dmsDevice.getFeatures()); + device.setProperties(dmsDevice.getProperties()); } devices.add(device); } @@ -278,30 +332,34 @@ public class DeviceManagementProviderServiceImpl implements DeviceManagementProv } @Override - public List getAllDevices(String type) throws DeviceManagementException { + public List getAllDevices(String deviceType) throws DeviceManagementException { List devices = new ArrayList<>(); List allDevices; try { DeviceManagementDAOFactory.openConnection(); - - allDevices = deviceDAO.getDevices(type, this.getTenantId()); + allDevices = deviceDAO.getDevices(deviceType, this.getTenantId()); } catch (DeviceManagementDAOException | SQLException e) { 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 { DeviceManagementDAOFactory.closeConnection(); } for (Device device : allDevices) { - - DeviceManagementService service = this.getPluginRepository().getDeviceManagementService(device.getType()); - if (service != null) { - Device dmsDevice = service.getDeviceManager().getDevice( - new DeviceIdentifier(device.getDeviceIdentifier(), device.getType())); - if (dmsDevice != null) { - device.setFeatures(dmsDevice.getFeatures()); - device.setProperties(dmsDevice.getProperties()); + DeviceManager deviceManager = this.getDeviceManager(deviceType); + if (deviceManager == null) { + if (log.isDebugEnabled()) { + log.debug("Device Manager associated with the device type '" + deviceType + "' is null. " + + "Therefore, not attempting method 'isEnrolled'"); } + devices.add(device); + continue; + } + Device dmsDevice = + deviceManager.getDevice(new DeviceIdentifier(device.getDeviceIdentifier(), device.getType())); + if (dmsDevice != null) { + device.setFeatures(dmsDevice.getFeatures()); + device.setProperties(dmsDevice.getProperties()); } devices.add(device); } @@ -313,7 +371,6 @@ public class DeviceManagementProviderServiceImpl implements DeviceManagementProv throws DeviceManagementException { List notificationMessages = DeviceConfigurationManager.getInstance().getNotificationMessagesConfig().getNotificationMessagesList(); - String messageHeader = ""; String messageBody = ""; String messageFooter1 = ""; @@ -369,9 +426,8 @@ public class DeviceManagementProviderServiceImpl implements DeviceManagementProv @Override public void sendRegistrationEmail(EmailMessageProperties emailMessageProperties) throws DeviceManagementException { - List notificationMessages = DeviceConfigurationManager.getInstance() - .getNotificationMessagesConfig().getNotificationMessagesList(); - + List notificationMessages = + DeviceConfigurationManager.getInstance().getNotificationMessagesConfig().getNotificationMessagesList(); String messageHeader = ""; String messageBody = ""; String messageFooter1 = ""; @@ -446,38 +502,60 @@ public class DeviceManagementProviderServiceImpl implements DeviceManagementProv if (device != null) { // 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. - DeviceManagementService service = this.getPluginRepository().getDeviceManagementService(deviceId.getType()); - if (service != null) { - DeviceManager dms = service.getDeviceManager(); - Device pluginSpecificInfo = dms.getDevice(deviceId); - if (pluginSpecificInfo != null) { - device.setFeatures(pluginSpecificInfo.getFeatures()); - device.setProperties(pluginSpecificInfo.getProperties()); + DeviceManager deviceManager = this.getDeviceManager(deviceId.getType()); + if (deviceManager == null) { + if (log.isDebugEnabled()) { + 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) { + device.setFeatures(pluginSpecificInfo.getFeatures()); + device.setProperties(pluginSpecificInfo.getProperties()); } } return device; } @Override - public boolean updateDeviceInfo(DeviceIdentifier deviceIdentifier, Device device) throws DeviceManagementException { - DeviceManager dms = - this.getPluginRepository().getDeviceManagementService(device.getType()).getDeviceManager(); - return dms.updateDeviceInfo(deviceIdentifier, device); + public boolean updateDeviceInfo(DeviceIdentifier deviceId, Device device) throws DeviceManagementException { + DeviceManager deviceManager = this.getDeviceManager(deviceId.getType()); + if (deviceManager == null) { + 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 public boolean setOwnership(DeviceIdentifier deviceId, String ownershipType) throws DeviceManagementException { - DeviceManager dms = - this.getPluginRepository().getDeviceManagementService(deviceId.getType()).getDeviceManager(); - return dms.setOwnership(deviceId, ownershipType); + DeviceManager deviceManager = this.getDeviceManager(deviceId.getType()); + if (deviceManager == null) { + 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 public boolean isClaimable(DeviceIdentifier deviceId) throws DeviceManagementException { - DeviceManager dms = - this.getPluginRepository().getDeviceManagementService(deviceId.getType()).getDeviceManager(); - return dms.isClaimable(deviceId); + DeviceManager deviceManager = this.getDeviceManager(deviceId.getType()); + if (deviceManager == null) { + 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 @@ -502,10 +580,16 @@ public class DeviceManagementProviderServiceImpl implements DeviceManagementProv @Override public License getLicense(String deviceType, String languageCode) throws DeviceManagementException { - DeviceManager dms = - this.getPluginRepository().getDeviceManagementService(deviceType).getDeviceManager(); + DeviceManager deviceManager = this.getDeviceManager(deviceType); + 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 { - return dms.getLicense(languageCode); + return deviceManager.getLicense(languageCode); } catch (LicenseManagementException e) { throw new DeviceManagementException("Error occurred while retrieving license configured for " + "device type '" + deviceType + "' and language code '" + languageCode + "'", e); @@ -514,10 +598,16 @@ public class DeviceManagementProviderServiceImpl implements DeviceManagementProv @Override public void addLicense(String deviceType, License license) throws DeviceManagementException { - DeviceManager dms = - this.getPluginRepository().getDeviceManagementService(deviceType).getDeviceManager(); + DeviceManager deviceManager = this.getDeviceManager(deviceType); + 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 { - dms.addLicense(license); + deviceManager.addLicense(license); } catch (LicenseManagementException e) { throw new DeviceManagementException("Error occurred while adding license for " + "device type '" + deviceType + "'", e); @@ -569,10 +659,10 @@ public class DeviceManagementProviderServiceImpl implements DeviceManagementProv @Override public List getOperationsByDeviceAndStatus( - DeviceIdentifier identifier, + DeviceIdentifier deviceId, Operation.Status status) throws OperationManagementException, DeviceManagementException { return DeviceManagementDataHolder.getInstance().getOperationManager().getOperationsByDeviceAndStatus( - identifier, status); + deviceId, status); } @Override @@ -595,10 +685,17 @@ public class DeviceManagementProviderServiceImpl implements DeviceManagementProv } 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 = - this.getPluginRepository().getDeviceManagementService( - device.getType()).getDeviceManager().getDevice( - new DeviceIdentifier(device.getDeviceIdentifier(), device.getType())); + deviceManager.getDevice(new DeviceIdentifier(device.getDeviceIdentifier(), device.getType())); if (dmsDevice != null) { device.setFeatures(dmsDevice.getFeatures()); device.setProperties(dmsDevice.getProperties()); @@ -612,7 +709,6 @@ public class DeviceManagementProviderServiceImpl implements DeviceManagementProv @Override public List getAllDevicesOfRole(String role) throws DeviceManagementException { List devices = new ArrayList<>(); - String[] users; int tenantId = this.getTenantId(); try { @@ -756,4 +852,17 @@ public class DeviceManagementProviderServiceImpl implements DeviceManagementProv 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(); + } + } diff --git a/components/device-mgt/org.wso2.carbon.device.mgt.core/src/test/java/org/wso2/carbon/device/mgt/core/service/DeviceManagementProviderServiceTest.java b/components/device-mgt/org.wso2.carbon.device.mgt.core/src/test/java/org/wso2/carbon/device/mgt/core/service/DeviceManagementProviderServiceTest.java index 8b6c4cd5e4..50ac3d3fdd 100644 --- a/components/device-mgt/org.wso2.carbon.device.mgt.core/src/test/java/org/wso2/carbon/device/mgt/core/service/DeviceManagementProviderServiceTest.java +++ b/components/device-mgt/org.wso2.carbon.device.mgt.core/src/test/java/org/wso2/carbon/device/mgt/core/service/DeviceManagementProviderServiceTest.java @@ -23,6 +23,7 @@ import org.testng.annotations.BeforeClass; import org.testng.annotations.Test; import org.wso2.carbon.device.mgt.common.Device; 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.TestDeviceManagementService; 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 { 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 @Override 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 - public void testEnrollment() { + public void testGetFeatureManager() { 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; - } - + FeatureManager featureManager = providerService.getFeatureManager(NON_EXISTENT_DEVICE_TYPE); + Assert.assertNull(featureManager, "Feature manager retrieved is null, which is expected as the " + + "input device type provided is non existent"); } 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); Assert.fail(msg, e); - } finally { - DeviceManagementDAOFactory.closeConnection(); } } @AfterClass public void cleanResources() { } + } diff --git a/components/device-mgt/org.wso2.carbon.device.mgt.core/src/test/resources/testng.xml b/components/device-mgt/org.wso2.carbon.device.mgt.core/src/test/resources/testng.xml index 78c1493fe7..cf107283b8 100644 --- a/components/device-mgt/org.wso2.carbon.device.mgt.core/src/test/resources/testng.xml +++ b/components/device-mgt/org.wso2.carbon.device.mgt.core/src/test/resources/testng.xml @@ -33,7 +33,7 @@ - + diff --git a/components/policy-mgt/org.wso2.carbon.policy.mgt.core/src/main/java/org/wso2/carbon/policy/mgt/core/dao/PolicyManagementDAOFactory.java b/components/policy-mgt/org.wso2.carbon.policy.mgt.core/src/main/java/org/wso2/carbon/policy/mgt/core/dao/PolicyManagementDAOFactory.java index 3332246613..b488ddde40 100644 --- a/components/policy-mgt/org.wso2.carbon.policy.mgt.core/src/main/java/org/wso2/carbon/policy/mgt/core/dao/PolicyManagementDAOFactory.java +++ b/components/policy-mgt/org.wso2.carbon.policy.mgt.core/src/main/java/org/wso2/carbon/policy/mgt/core/dao/PolicyManagementDAOFactory.java @@ -126,12 +126,12 @@ public class PolicyManagementDAOFactory { return currentConnection.get(); } - public static void closeConnection() throws PolicyManagerDAOException { + public static void closeConnection() { Connection con = currentConnection.get(); try { con.close(); } catch (SQLException e) { - log.error("Error occurred while close the connection"); + log.warn("Error occurred while close the connection", e); } currentConnection.remove(); } @@ -149,12 +149,10 @@ public class PolicyManagementDAOFactory { } } catch (SQLException e) { throw new PolicyManagerDAOException("Error occurred while committing the transaction", e); - } finally { - closeConnection(); } } - public static void rollbackTransaction() throws PolicyManagerDAOException { + public static void rollbackTransaction() { try { Connection conn = currentConnection.get(); if (conn != null) { @@ -166,10 +164,12 @@ public class PolicyManagementDAOFactory { } } } catch (SQLException e) { - throw new PolicyManagerDAOException("Error occurred while roll-backing the transaction", e); - } finally { - closeConnection(); + log.warn("Error occurred while roll-backing the transaction", e); } } + public static void openConnection() throws SQLException { + currentConnection.set(dataSource.getConnection()); + } + } diff --git a/components/policy-mgt/org.wso2.carbon.policy.mgt.core/src/main/java/org/wso2/carbon/policy/mgt/core/dao/impl/FeatureDAOImpl.java b/components/policy-mgt/org.wso2.carbon.policy.mgt.core/src/main/java/org/wso2/carbon/policy/mgt/core/dao/impl/FeatureDAOImpl.java index 9bccc8e48a..37dde34099 100644 --- a/components/policy-mgt/org.wso2.carbon.policy.mgt.core/src/main/java/org/wso2/carbon/policy/mgt/core/dao/impl/FeatureDAOImpl.java +++ b/components/policy-mgt/org.wso2.carbon.policy.mgt.core/src/main/java/org/wso2/carbon/policy/mgt/core/dao/impl/FeatureDAOImpl.java @@ -201,14 +201,8 @@ public class FeatureDAOImpl implements FeatureDAO { i++; } - } catch (SQLException e) { - String msg = "Error occurred while adding the feature list to the database."; - 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); + } catch (SQLException | IOException e) { + throw new FeatureManagerDAOException("Error occurred while adding the feature list to the database.", e); } finally { PolicyManagementDAOUtil.cleanupResources(stmt, generatedKeys); } @@ -243,14 +237,8 @@ public class FeatureDAOImpl implements FeatureDAO { } stmt.executeBatch(); - } catch (SQLException e) { - String msg = "Error occurred while adding the feature list to the database."; - 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); + } catch (SQLException | IOException e) { + throw new FeatureManagerDAOException("Error occurred while adding the feature list to the database.", e); } finally { PolicyManagementDAOUtil.cleanupResources(stmt, null); } @@ -274,9 +262,7 @@ public class FeatureDAOImpl implements FeatureDAO { return true; } catch (SQLException e) { - String msg = "Error occurred while deleting the feature related to a profile."; - log.error(msg); - throw new FeatureManagerDAOException(msg, e); + throw new FeatureManagerDAOException("Error occurred while deleting the feature related to a profile.", e); } finally { PolicyManagementDAOUtil.cleanupResources(stmt, null); } @@ -298,18 +284,14 @@ public class FeatureDAOImpl implements FeatureDAO { return true; } catch (SQLException e) { - String msg = "Error occurred while deleting the feature related to a profile."; - log.error(msg); - throw new FeatureManagerDAOException(msg, e); + throw new FeatureManagerDAOException("Error occurred while deleting the feature related to a profile.", e); } finally { PolicyManagementDAOUtil.cleanupResources(stmt, null); } } - @Override public List getAllProfileFeatures() throws FeatureManagerDAOException { - Connection conn; PreparedStatement stmt = null; ResultSet resultSet = null; @@ -359,34 +341,24 @@ public class FeatureDAOImpl implements FeatureDAO { featureList.add(profileFeature); } - } catch (SQLException e) { - String msg = "Unable to get the list of the features from database."; - log.error(msg); - throw new FeatureManagerDAOException(msg, e); + throw new FeatureManagerDAOException("Unable to get the list of the features from database.", e); } catch (IOException e) { - String msg = "Unable to read the byte stream for content"; - log.error(msg); - throw new FeatureManagerDAOException(msg, e); + throw new FeatureManagerDAOException("Unable to read the byte stream for content", e); } catch (ClassNotFoundException e) { - String msg = "Class not found while converting the object"; - log.error(msg); - throw new FeatureManagerDAOException(msg, e); + throw new FeatureManagerDAOException("Class not found while converting the object", e); } finally { PolicyManagementDAOUtil.cleanupResources(stmt, resultSet); - this.closeConnection(); } return featureList; } @Override public List getAllFeatures(String deviceType) throws FeatureManagerDAOException { - Connection conn; PreparedStatement stmt = null; ResultSet resultSet = null; List featureList = new ArrayList(); - try { conn = this.getConnection(); 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")); featureList.add(feature); } - } catch (SQLException e) { - String msg = "Unable to get the list of the features related device type from database."; - log.error(msg); - throw new FeatureManagerDAOException(msg, e); + throw new FeatureManagerDAOException("Unable to get the list of the features related device type " + + "from database.", e); } finally { PolicyManagementDAOUtil.cleanupResources(stmt, resultSet); - this.closeConnection(); } return featureList; } @Override public List getFeaturesForProfile(int profileId) throws FeatureManagerDAOException { - Connection conn; PreparedStatement stmt = null; ResultSet resultSet = null; @@ -464,25 +432,16 @@ public class FeatureDAOImpl implements FeatureDAO { } } } - featureList.add(profileFeature); } - } catch (SQLException e) { - String msg = "Unable to get the list of the features from database."; - log.error(msg); - throw new FeatureManagerDAOException(msg, e); + throw new FeatureManagerDAOException("Unable to get the list of the features from database.", e); } catch (IOException e) { - String msg = "Unable to read the byte stream for content"; - log.error(msg); - throw new FeatureManagerDAOException(msg, e); + throw new FeatureManagerDAOException("Unable to read the byte stream for content", e); } catch (ClassNotFoundException e) { - String msg = "Class not found while converting the object"; - log.error(msg); - throw new FeatureManagerDAOException(msg, e); + throw new FeatureManagerDAOException("Class not found while converting the object", e); } finally { PolicyManagementDAOUtil.cleanupResources(stmt, resultSet); - this.closeConnection(); } return featureList; } @@ -504,9 +463,8 @@ public class FeatureDAOImpl implements FeatureDAO { return true; } catch (SQLException e) { - String msg = "Unable to delete the feature " + featureId + " (Feature ID) from database."; - log.error(msg); - throw new FeatureManagerDAOException(msg, e); + throw new FeatureManagerDAOException("Unable to delete the feature " + featureId + " (Feature ID) " + + "from database.", e); } finally { 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."); - } - } } diff --git a/components/policy-mgt/org.wso2.carbon.policy.mgt.core/src/main/java/org/wso2/carbon/policy/mgt/core/dao/impl/MonitoringDAOImpl.java b/components/policy-mgt/org.wso2.carbon.policy.mgt.core/src/main/java/org/wso2/carbon/policy/mgt/core/dao/impl/MonitoringDAOImpl.java index b1d8005fdd..959f05efb6 100644 --- a/components/policy-mgt/org.wso2.carbon.policy.mgt.core/src/main/java/org/wso2/carbon/policy/mgt/core/dao/impl/MonitoringDAOImpl.java +++ b/components/policy-mgt/org.wso2.carbon.policy.mgt.core/src/main/java/org/wso2/carbon/policy/mgt/core/dao/impl/MonitoringDAOImpl.java @@ -258,7 +258,6 @@ public class MonitoringDAOImpl implements MonitoringDAO { throw new MonitoringDAOException(msg, e); } finally { PolicyManagementDAOUtil.cleanupResources(stmt, resultSet); - this.closeConnection(); } } @@ -303,7 +302,6 @@ public class MonitoringDAOImpl implements MonitoringDAO { throw new MonitoringDAOException(msg, e); } finally { PolicyManagementDAOUtil.cleanupResources(stmt, resultSet); - this.closeConnection(); } } @@ -340,9 +338,7 @@ public class MonitoringDAOImpl implements MonitoringDAO { throw new MonitoringDAOException(msg, e); } finally { PolicyManagementDAOUtil.cleanupResources(stmt, resultSet); - this.closeConnection(); } - } @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."); - } - } + } diff --git a/components/policy-mgt/org.wso2.carbon.policy.mgt.core/src/main/java/org/wso2/carbon/policy/mgt/core/dao/impl/PolicyDAOImpl.java b/components/policy-mgt/org.wso2.carbon.policy.mgt.core/src/main/java/org/wso2/carbon/policy/mgt/core/dao/impl/PolicyDAOImpl.java index 67f31a386c..8b08ee968c 100644 --- a/components/policy-mgt/org.wso2.carbon.policy.mgt.core/src/main/java/org/wso2/carbon/policy/mgt/core/dao/impl/PolicyDAOImpl.java +++ b/components/policy-mgt/org.wso2.carbon.policy.mgt.core/src/main/java/org/wso2/carbon/policy/mgt/core/dao/impl/PolicyDAOImpl.java @@ -51,10 +51,8 @@ public class PolicyDAOImpl implements PolicyDAO { @Override public Policy addPolicy(String deviceType, Policy policy) throws PolicyManagerDAOException { - Connection conn; PreparedStatement stmt = null; - try { conn = this.getConnection(); 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(2, policy.getId()); stmt.executeQuery(); - } catch (SQLException e) { - String msg = "Error occurred while adding the device type policy to database."; - log.error(msg, e); - throw new PolicyManagerDAOException(msg, e); + throw new PolicyManagerDAOException("Error occurred while adding the device type policy to database.", e); } finally { PolicyManagementDAOUtil.cleanupResources(stmt, null); } @@ -76,10 +71,8 @@ public class PolicyDAOImpl implements PolicyDAO { @Override public Policy addPolicyToRole(List roleNames, Policy policy) throws PolicyManagerDAOException { - Connection conn; PreparedStatement stmt = null; - try { conn = this.getConnection(); String query = "INSERT INTO DM_ROLE_POLICY (ROLE_NAME, POLICY_ID) VALUES (?, ?)"; @@ -90,11 +83,8 @@ public class PolicyDAOImpl implements PolicyDAO { stmt.addBatch(); } stmt.executeBatch(); - } catch (SQLException e) { - String msg = "Error occurred while adding the role name with policy to database."; - log.error(msg, e); - throw new PolicyManagerDAOException(msg, e); + throw new PolicyManagerDAOException("Error occurred while adding the role name with policy to database", e); } finally { PolicyManagementDAOUtil.cleanupResources(stmt, null); } @@ -103,10 +93,8 @@ public class PolicyDAOImpl implements PolicyDAO { @Override public Policy addPolicyToUser(List usernameList, Policy policy) throws PolicyManagerDAOException { - Connection conn; PreparedStatement stmt = null; - try { conn = this.getConnection(); String query = "INSERT INTO DM_USER_POLICY (POLICY_ID, USERNAME) VALUES (?, ?)"; @@ -117,11 +105,8 @@ public class PolicyDAOImpl implements PolicyDAO { stmt.addBatch(); } stmt.executeBatch(); - } catch (SQLException e) { - String msg = "Error occurred while adding the user name with policy to database."; - log.error(msg, e); - throw new PolicyManagerDAOException(msg, e); + throw new PolicyManagerDAOException("Error occurred while adding the user name with policy to database", e); } finally { PolicyManagementDAOUtil.cleanupResources(stmt, null); } @@ -130,10 +115,8 @@ public class PolicyDAOImpl implements PolicyDAO { @Override public Policy addPolicyToDevice(List devices, Policy policy) throws PolicyManagerDAOException { - Connection conn; PreparedStatement stmt = null; - try { conn = this.getConnection(); String query = "INSERT INTO DM_DEVICE_POLICY (DEVICE_ID, POLICY_ID) VALUES (?, ?)"; @@ -145,9 +128,8 @@ public class PolicyDAOImpl implements PolicyDAO { } stmt.executeBatch(); } catch (SQLException e) { - String msg = "Error occurred while adding the device ids with policy to database."; - log.error(msg, e); - throw new PolicyManagerDAOException(msg, e); + throw new PolicyManagerDAOException("Error occurred while adding the device ids with policy to " + + "database", e); } finally { PolicyManagementDAOUtil.cleanupResources(stmt, null); } @@ -156,7 +138,6 @@ public class PolicyDAOImpl implements PolicyDAO { @Override public boolean updatePolicyPriorities(List policies) throws PolicyManagerDAOException { - Connection conn; PreparedStatement stmt = null; int tenantId = PrivilegedCarbonContext.getThreadLocalCarbonContext().getTenantId(); @@ -173,11 +154,8 @@ public class PolicyDAOImpl implements PolicyDAO { stmt.addBatch(); } stmt.executeBatch(); - } catch (SQLException e) { - String msg = "Error occurred while updating policy priorities in database."; - log.error(msg, e); - throw new PolicyManagerDAOException(msg, e); + throw new PolicyManagerDAOException("Error occurred while updating policy priorities in database", e); } finally { PolicyManagementDAOUtil.cleanupResources(stmt, null); } @@ -202,15 +180,12 @@ public class PolicyDAOImpl implements PolicyDAO { stmt.executeUpdate(); generatedKeys = stmt.getGeneratedKeys(); - while (generatedKeys.next()) { criteria.setId(generatedKeys.getInt(1)); } - } catch (SQLException e) { - String msg = "Error occurred while inserting the criterion (" + criteria.getName() + ") to database."; - log.error(msg, e); - throw new PolicyManagerDAOException(msg, e); + throw new PolicyManagerDAOException("Error occurred while inserting the criterion (" + criteria.getName() + + ") to database.", e); } finally { PolicyManagementDAOUtil.cleanupResources(stmt, null); } @@ -219,11 +194,9 @@ public class PolicyDAOImpl implements PolicyDAO { @Override public Criterion updateCriterion(Criterion criteria) throws PolicyManagerDAOException { - Connection conn; PreparedStatement stmt = null; int tenantId = PrivilegedCarbonContext.getThreadLocalCarbonContext().getTenantId(); - try { conn = this.getConnection(); 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(3, tenantId); stmt.executeUpdate(); - } catch (SQLException e) { - String msg = "Error occurred while inserting the criterion (" + criteria.getName() + ") to database."; - log.error(msg, e); - throw new PolicyManagerDAOException(msg, e); + throw new PolicyManagerDAOException("Error occurred while inserting the criterion (" + criteria.getName() + + ") to database", e); } finally { PolicyManagementDAOUtil.cleanupResources(stmt, null); } @@ -245,7 +216,6 @@ public class PolicyDAOImpl implements PolicyDAO { @Override public Criterion getCriterion(int id) throws PolicyManagerDAOException { - Connection conn; PreparedStatement stmt = null; ResultSet resultSet = null; @@ -265,26 +235,20 @@ public class PolicyDAOImpl implements PolicyDAO { criterion.setName(resultSet.getString("NAME")); } return criterion; - } catch (SQLException e) { - String msg = "Error occurred while reading the policies from the database."; - log.error(msg, e); - throw new PolicyManagerDAOException(msg, e); + throw new PolicyManagerDAOException("Error occurred while reading the policies from the database", e); } finally { PolicyManagementDAOUtil.cleanupResources(stmt, resultSet); - this.closeConnection(); } } @Override public Criterion getCriterion(String name) throws PolicyManagerDAOException { - Connection conn; PreparedStatement stmt = null; ResultSet resultSet = null; Criterion criterion = new Criterion(); int tenantId = PrivilegedCarbonContext.getThreadLocalCarbonContext().getTenantId(); - try { conn = this.getConnection(); String query = "SELECT * FROM DM_CRITERIA WHERE NAME= ? AND TENANT_ID = ?"; @@ -298,26 +262,20 @@ public class PolicyDAOImpl implements PolicyDAO { criterion.setName(resultSet.getString("NAME")); } return criterion; - } catch (SQLException e) { - String msg = "Error occurred while reading the policies from the database."; - log.error(msg, e); - throw new PolicyManagerDAOException(msg, e); + throw new PolicyManagerDAOException("Error occurred while reading the policies from the database", e); } finally { PolicyManagementDAOUtil.cleanupResources(stmt, resultSet); - this.closeConnection(); } } @Override public boolean checkCriterionExists(String name) throws PolicyManagerDAOException { - Connection conn; PreparedStatement stmt = null; ResultSet resultSet = null; int tenantId = PrivilegedCarbonContext.getThreadLocalCarbonContext().getTenantId(); boolean exist = false; - try { conn = this.getConnection(); String query = "SELECT * FROM DM_CRITERIA WHERE NAME = ? AND TENANT_ID = ?"; @@ -325,25 +283,24 @@ public class PolicyDAOImpl implements PolicyDAO { stmt.setString(1, name); stmt.setInt(2, tenantId); resultSet = stmt.executeQuery(); - exist = resultSet.next(); + if (resultSet.next()) { + //TODO: FIXME + exist = resultSet.getBoolean(1); + } } catch (SQLException e) { - String msg = "Error occurred while checking whether criterion (" + name + ") exists."; - log.error(msg, e); - throw new PolicyManagerDAOException(msg, e); + throw new PolicyManagerDAOException("Error occurred while checking whether criterion (" + name + + ") exists", e); } finally { PolicyManagementDAOUtil.cleanupResources(stmt, resultSet); - this.closeConnection(); } return exist; } @Override public boolean deleteCriterion(Criterion criteria) throws PolicyManagerDAOException { - Connection conn; PreparedStatement stmt = null; - try { conn = this.getConnection(); String query = "DELETE FROM DM_CRITERIA WHERE ID = ?"; @@ -356,9 +313,8 @@ public class PolicyDAOImpl implements PolicyDAO { } return true; } catch (SQLException e) { - String msg = "Unable to delete the policy (" + criteria.getName() + ") from database."; - log.error(msg); - throw new PolicyManagerDAOException(msg, e); + throw new PolicyManagerDAOException("Unable to delete the policy (" + criteria.getName() + + ") from database", e); } finally { PolicyManagementDAOUtil.cleanupResources(stmt, null); } @@ -366,13 +322,11 @@ public class PolicyDAOImpl implements PolicyDAO { @Override public List getAllPolicyCriteria() throws PolicyManagerDAOException { - Connection conn; PreparedStatement stmt = null; ResultSet resultSet = null; List criteria = new ArrayList(); int tenantId = PrivilegedCarbonContext.getThreadLocalCarbonContext().getTenantId(); - try { conn = this.getConnection(); String query = "SELECT * FROM DM_CRITERIA WHERE TENANT_ID = ?"; @@ -387,14 +341,10 @@ public class PolicyDAOImpl implements PolicyDAO { criteria.add(criterion); } return criteria; - } catch (SQLException e) { - String msg = "Error occurred while reading the policies from the database."; - log.error(msg, e); - throw new PolicyManagerDAOException(msg, e); + throw new PolicyManagerDAOException("Error occurred while reading the policies from the database", e); } finally { PolicyManagementDAOUtil.cleanupResources(stmt, resultSet); - this.closeConnection(); } } @@ -441,11 +391,8 @@ public class PolicyDAOImpl implements PolicyDAO { @Override public boolean addPolicyCriteriaProperties(List policyCriteria) throws PolicyManagerDAOException { - - Connection conn; PreparedStatement stmt = null; - try { conn = this.getConnection(); 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); for (PolicyCriterion criterion : policyCriteria) { - Properties prop = criterion.getProperties(); for (String name : prop.stringPropertyNames()) { @@ -467,30 +413,21 @@ public class PolicyDAOImpl implements PolicyDAO { } // stmt.executeUpdate(); - } catch (SQLException e) { - String msg = "Error occurred while inserting the criterion properties to database."; - log.error(msg, 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); + } catch (SQLException | IOException e) { + throw new PolicyManagerDAOException("Error occurred while inserting the criterion properties " + + "to database", e); } finally { PolicyManagementDAOUtil.cleanupResources(stmt, null); } - return false; } @Override public List getPolicyCriteria(int policyId) throws PolicyManagerDAOException { - - Connection conn; PreparedStatement stmt = null; ResultSet resultSet = null; List criteria = new ArrayList(); - try { conn = this.getConnection(); 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")); } } - } catch (SQLException e) { - String msg = "Error occurred while reading the criteria related to policies from the database."; - log.error(msg, e); - throw new PolicyManagerDAOException(msg, e); + throw new PolicyManagerDAOException("Error occurred while reading the criteria related to policies from " + + "the database", e); } finally { PolicyManagementDAOUtil.cleanupResources(stmt, resultSet); - this.closeConnection(); } return criteria; } @@ -555,9 +489,8 @@ public class PolicyDAOImpl implements PolicyDAO { stmt.executeUpdate(); } catch (SQLException e) { - String msg = "Error occurred while updating policy (" + policy.getPolicyName() + ") in database."; - log.error(msg, e); - throw new PolicyManagerDAOException(msg, e); + throw new PolicyManagerDAOException("Error occurred while updating policy (" + policy.getPolicyName() + + ") in database", e); } finally { PolicyManagementDAOUtil.cleanupResources(stmt, null); } @@ -566,13 +499,11 @@ public class PolicyDAOImpl implements PolicyDAO { @Override public Policy getPolicy(int policyId) throws PolicyManagerDAOException { - Connection conn; PreparedStatement stmt = null; ResultSet resultSet = null; Policy policy = new Policy(); int tenantId = PrivilegedCarbonContext.getThreadLocalCarbonContext().getTenantId(); - try { conn = this.getConnection(); String query = "SELECT * FROM DM_POLICY WHERE ID= ? AND TENANT_ID = ? "; @@ -592,19 +523,15 @@ public class PolicyDAOImpl implements PolicyDAO { return policy; } catch (SQLException e) { - String msg = "Error occurred while reading the policies from the database."; - log.error(msg, e); - throw new PolicyManagerDAOException(msg, e); + throw new PolicyManagerDAOException("Error occurred while reading the policies from the database", e); } finally { PolicyManagementDAOUtil.cleanupResources(stmt, resultSet); - this.closeConnection(); } } @Override public Policy getPolicyByProfileID(int profileId) throws PolicyManagerDAOException { - Connection conn; PreparedStatement stmt = null; ResultSet resultSet = null; @@ -620,7 +547,6 @@ public class PolicyDAOImpl implements PolicyDAO { resultSet = stmt.executeQuery(); while (resultSet.next()) { - policy.setId(resultSet.getInt("ID")); policy.setPolicyName(resultSet.getString("NAME")); policy.setTenantId(resultSet.getInt("TENANT_ID")); @@ -628,20 +554,15 @@ public class PolicyDAOImpl implements PolicyDAO { policy.setCompliance(resultSet.getString("COMPLIANCE")); } return policy; - } catch (SQLException e) { - String msg = "Error occurred while reading the policies from the database."; - log.error(msg, e); - throw new PolicyManagerDAOException(msg, e); + throw new PolicyManagerDAOException("Error occurred while reading the policies from the database", e); } finally { PolicyManagementDAOUtil.cleanupResources(stmt, resultSet); - this.closeConnection(); } } @Override public List getAllPolicies() throws PolicyManagerDAOException { - Connection conn; PreparedStatement stmt = null; ResultSet resultSet = null; @@ -657,7 +578,6 @@ public class PolicyDAOImpl implements PolicyDAO { while (resultSet.next()) { Policy policy = new Policy(); - policy.setId(resultSet.getInt("ID")); policy.setProfileId(resultSet.getInt("PROFILE_ID")); policy.setPolicyName(resultSet.getString("NAME")); @@ -668,14 +588,10 @@ public class PolicyDAOImpl implements PolicyDAO { policies.add(policy); } return policies; - } catch (SQLException e) { - String msg = "Error occurred while reading the policies from the database."; - log.error(msg, e); - throw new PolicyManagerDAOException(msg, e); + throw new PolicyManagerDAOException("Error occurred while reading the policies from the database", e); } finally { PolicyManagementDAOUtil.cleanupResources(stmt, resultSet); - this.closeConnection(); } } @@ -686,12 +602,10 @@ public class PolicyDAOImpl implements PolicyDAO { @Override public List getPolicyAppliedDevicesIds(int policyId) throws PolicyManagerDAOException { - Connection conn; PreparedStatement stmt = null; ResultSet resultSet = null; List deviceIdList = new ArrayList(); - try { conn = this.getConnection(); String query = "SELECT * FROM DM_DEVICE_POLICY WHERE POLICY_ID = ?"; @@ -700,28 +614,21 @@ public class PolicyDAOImpl implements PolicyDAO { resultSet = stmt.executeQuery(); while (resultSet.next()) { - deviceIdList.add(resultSet.getInt("DEVICE_ID")); } return deviceIdList; } catch (SQLException e) { - String msg = "Error occurred while getting the device related to policies."; - log.error(msg, e); - throw new PolicyManagerDAOException(msg, e); + throw new PolicyManagerDAOException("Error occurred while getting the device related to policies", e); } finally { PolicyManagementDAOUtil.cleanupResources(stmt, resultSet); - this.closeConnection(); } - } public List getPolicyAppliedRoles(int policyId) throws PolicyManagerDAOException { - Connection conn; PreparedStatement stmt = null; ResultSet resultSet = null; - List roleNames = new ArrayList(); try { conn = this.getConnection(); @@ -731,18 +638,13 @@ public class PolicyDAOImpl implements PolicyDAO { resultSet = stmt.executeQuery(); while (resultSet.next()) { - roleNames.add(resultSet.getString("ROLE_NAME")); } return roleNames; - } catch (SQLException e) { - String msg = "Error occurred while getting the roles related to policies."; - log.error(msg, e); - throw new PolicyManagerDAOException(msg, e); + throw new PolicyManagerDAOException("Error occurred while getting the roles related to policies", e); } finally { PolicyManagementDAOUtil.cleanupResources(stmt, resultSet); - this.closeConnection(); } } @@ -761,30 +663,23 @@ public class PolicyDAOImpl implements PolicyDAO { resultSet = stmt.executeQuery(); while (resultSet.next()) { - users.add(resultSet.getString("USERNAME")); } return users; - } catch (SQLException e) { - String msg = "Error occurred while getting the roles related to policies."; - log.error(msg, e); - throw new PolicyManagerDAOException(msg, e); + throw new PolicyManagerDAOException("Error occurred while getting the roles related to policies", e); } finally { PolicyManagementDAOUtil.cleanupResources(stmt, resultSet); - this.closeConnection(); } } @Override public void addEffectivePolicyToDevice(int deviceId, Policy policy) throws PolicyManagerDAOException { - Connection conn; PreparedStatement stmt = null; Timestamp currentTimestamp = new Timestamp(Calendar.getInstance().getTime().getTime()); int tenantId = PrivilegedCarbonContext.getThreadLocalCarbonContext().getTenantId(); - try { conn = this.getConnection(); String query = "INSERT INTO DM_DEVICE_POLICY_APPLIED (DEVICE_ID, POLICY_ID, POLICY_CONTENT, " + @@ -796,17 +691,9 @@ public class PolicyDAOImpl implements PolicyDAO { stmt.setTimestamp(4, currentTimestamp); stmt.setTimestamp(5, currentTimestamp); stmt.setInt(6, tenantId); - stmt.executeUpdate(); - - } catch (SQLException 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); + } catch (SQLException | IOException e) { + throw new PolicyManagerDAOException("Error occurred while adding the evaluated feature list to device", e); } finally { PolicyManagementDAOUtil.cleanupResources(stmt, null); } @@ -815,12 +702,10 @@ public class PolicyDAOImpl implements PolicyDAO { @Override public void setPolicyApplied(int deviceId) throws PolicyManagerDAOException { - Connection conn; PreparedStatement stmt = null; Timestamp currentTimestamp = new Timestamp(Calendar.getInstance().getTime().getTime()); int tenantId = PrivilegedCarbonContext.getThreadLocalCarbonContext().getTenantId(); - try { conn = this.getConnection(); String query = "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.setInt(3, deviceId); stmt.setInt(4, tenantId); - stmt.executeUpdate(); - } catch (SQLException e) { - String msg = "Error occurred while updating applied policy to device (" + deviceId + ")"; - log.error(msg, e); - throw new PolicyManagerDAOException(msg, e); + throw new PolicyManagerDAOException("Error occurred while updating applied policy to device (" + + deviceId + ")", e); } finally { PolicyManagementDAOUtil.cleanupResources(stmt, null); } @@ -844,14 +726,11 @@ public class PolicyDAOImpl implements PolicyDAO { @Override - public void updateEffectivePolicyToDevice(int deviceId, Policy policy) - throws PolicyManagerDAOException { - + public void updateEffectivePolicyToDevice(int deviceId, Policy policy) throws PolicyManagerDAOException { Connection conn; PreparedStatement stmt = null; Timestamp currentTimestamp = new Timestamp(Calendar.getInstance().getTime().getTime()); int tenantId = PrivilegedCarbonContext.getThreadLocalCarbonContext().getTenantId(); - try { conn = this.getConnection(); String query = "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.executeUpdate(); - } catch (SQLException e) { - String msg = "Error occurred while updating the evaluated feature list to device."; - log.error(msg, 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); + } catch (SQLException | IOException e) { + throw new PolicyManagerDAOException("Error occurred while updating the evaluated feature list " + + "to device", e); } finally { PolicyManagementDAOUtil.cleanupResources(stmt, null); } - } @Override public boolean checkPolicyAvailable(int deviceId) throws PolicyManagerDAOException { - Connection conn; PreparedStatement stmt = null; ResultSet resultSet = null; boolean exist = false; int tenantId = PrivilegedCarbonContext.getThreadLocalCarbonContext().getTenantId(); - try { conn = this.getConnection(); String query = "SELECT * FROM DM_DEVICE_POLICY_APPLIED WHERE DEVICE_ID = ? AND TENANT_ID = ?"; @@ -896,26 +767,21 @@ public class PolicyDAOImpl implements PolicyDAO { stmt.setInt(2, tenantId); resultSet = stmt.executeQuery(); exist = resultSet.next(); - } catch (SQLException e) { - String msg = "Error occurred while checking whether device (" + deviceId + ") has a policy to apply."; - log.error(msg, e); - throw new PolicyManagerDAOException(msg, e); + throw new PolicyManagerDAOException("Error occurred while checking whether device (" + deviceId + + ") has a policy to apply", e); } finally { PolicyManagementDAOUtil.cleanupResources(stmt, resultSet); - this.closeConnection(); } return exist; } @Override public List getPolicyIdsOfDevice(Device device) throws PolicyManagerDAOException { - Connection conn; PreparedStatement stmt = null; ResultSet resultSet = null; List policyIds = new ArrayList(); - try { conn = this.getConnection(); String query = "SELECT * FROM DM_DEVICE_POLICY WHERE DEVICE_ID = ? "; @@ -926,26 +792,20 @@ public class PolicyDAOImpl implements PolicyDAO { while (resultSet.next()) { policyIds.add(resultSet.getInt("POLICY_ID")); } - } catch (SQLException e) { - String msg = "Error occurred while reading the device policy table."; - log.error(msg, e); - throw new PolicyManagerDAOException(msg, e); + throw new PolicyManagerDAOException("Error occurred while reading the device policy table", e); } finally { PolicyManagementDAOUtil.cleanupResources(stmt, resultSet); - this.closeConnection(); } return policyIds; } @Override public List getPolicyOfRole(String roleName) throws PolicyManagerDAOException { - Connection conn; PreparedStatement stmt = null; ResultSet resultSet = null; List policyIds = new ArrayList(); - try { conn = this.getConnection(); String query = "SELECT * FROM DM_ROLE_POLICY WHERE ROLE_NAME = ? "; @@ -956,26 +816,20 @@ public class PolicyDAOImpl implements PolicyDAO { while (resultSet.next()) { policyIds.add(resultSet.getInt("POLICY_ID")); } - } catch (SQLException e) { - String msg = "Error occurred while reading the role policy table."; - log.error(msg, e); - throw new PolicyManagerDAOException(msg, e); + throw new PolicyManagerDAOException("Error occurred while reading the role policy table", e); } finally { PolicyManagementDAOUtil.cleanupResources(stmt, resultSet); - this.closeConnection(); } return policyIds; } @Override public List getPolicyOfUser(String username) throws PolicyManagerDAOException { - Connection conn; PreparedStatement stmt = null; ResultSet resultSet = null; List policyIds = new ArrayList(); - try { conn = this.getConnection(); String query = "SELECT * FROM DM_USER_POLICY WHERE USERNAME = ? "; @@ -986,25 +840,19 @@ public class PolicyDAOImpl implements PolicyDAO { while (resultSet.next()) { policyIds.add(resultSet.getInt("POLICY_ID")); } - } catch (SQLException e) { - String msg = "Error occurred while reading the user policy table."; - log.error(msg, e); - throw new PolicyManagerDAOException(msg, e); + throw new PolicyManagerDAOException("Error occurred while reading the user policy table", e); } finally { PolicyManagementDAOUtil.cleanupResources(stmt, resultSet); - this.closeConnection(); } return policyIds; } @Override public boolean deletePolicy(Policy policy) throws PolicyManagerDAOException { - Connection conn; PreparedStatement stmt = null; int tenantId = PrivilegedCarbonContext.getThreadLocalCarbonContext().getTenantId(); - try { conn = this.getConnection(); String query = "DELETE FROM DM_POLICY WHERE ID = ? AND TENANT_ID = ?"; @@ -1018,9 +866,8 @@ public class PolicyDAOImpl implements PolicyDAO { } return true; } catch (SQLException e) { - String msg = "Unable to delete the policy (" + policy.getPolicyName() + ") from database."; - log.error(msg); - throw new PolicyManagerDAOException(msg, e); + throw new PolicyManagerDAOException("Unable to delete the policy (" + policy.getPolicyName() + + ") from database", e); } finally { PolicyManagementDAOUtil.cleanupResources(stmt, null); } @@ -1028,11 +875,9 @@ public class PolicyDAOImpl implements PolicyDAO { @Override public boolean deletePolicy(int policyId) throws PolicyManagerDAOException { - Connection conn; PreparedStatement stmt = null; int tenantId = PrivilegedCarbonContext.getThreadLocalCarbonContext().getTenantId(); - try { conn = this.getConnection(); String query = "DELETE FROM DM_POLICY WHERE ID = ? AND TENANT_ID = ?"; @@ -1046,9 +891,7 @@ public class PolicyDAOImpl implements PolicyDAO { } return true; } catch (SQLException e) { - String msg = "Unable to delete the policy (" + policyId + ") from database."; - log.error(msg); - throw new PolicyManagerDAOException(msg, e); + throw new PolicyManagerDAOException("Unable to delete the policy (" + policyId + ") from database", e); } finally { PolicyManagementDAOUtil.cleanupResources(stmt, null); } @@ -1056,10 +899,8 @@ public class PolicyDAOImpl implements PolicyDAO { @Override public boolean deleteAllPolicyRelatedConfigs(int policyId) throws PolicyManagerDAOException { - Connection conn; PreparedStatement stmt = null; - try { conn = this.getConnection(); @@ -1073,27 +914,23 @@ public class PolicyDAOImpl implements PolicyDAO { stmt.setInt(1, policyId); stmt.executeUpdate(); - String devicePolicy = "DELETE FROM DM_DEVICE_POLICY WHERE POLICY_ID = ?"; stmt = conn.prepareStatement(devicePolicy); stmt.setInt(1, policyId); stmt.executeUpdate(); - String deleteCriteria = "DELETE FROM DM_POLICY_CRITERIA WHERE POLICY_ID = ?"; stmt = conn.prepareStatement(deleteCriteria); stmt.setInt(1, policyId); stmt.executeUpdate(); - if (log.isDebugEnabled()) { log.debug("Policy (" + policyId + ") related configs deleted from database."); } return true; } catch (SQLException e) { - String msg = "Unable to delete the policy (" + policyId + ") related configs from database."; - log.error(msg); - throw new PolicyManagerDAOException(msg, e); + throw new PolicyManagerDAOException("Unable to delete the policy (" + policyId + + ") related configs from database", e); } finally { PolicyManagementDAOUtil.cleanupResources(stmt, null); } @@ -1103,17 +940,7 @@ public class PolicyDAOImpl implements PolicyDAO { 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 { - Connection conn; PreparedStatement stmt = null; ResultSet generatedKeys = null; @@ -1149,9 +976,7 @@ public class PolicyDAOImpl implements PolicyDAO { } } catch (SQLException e) { - String msg = "Error occurred while adding policy to the database."; - log.error(msg, e); - throw new PolicyManagerDAOException(msg, e); + throw new PolicyManagerDAOException("Error occurred while adding policy to the database", e); } finally { PolicyManagementDAOUtil.cleanupResources(stmt, generatedKeys); } @@ -1167,7 +992,6 @@ public class PolicyDAOImpl implements PolicyDAO { * @throws PolicyManagerDAOException */ private int getDeviceTypeId(String deviceType) throws PolicyManagerDAOException { - Connection conn; PreparedStatement stmt = null; ResultSet resultSet = null; @@ -1182,21 +1006,16 @@ public class PolicyDAOImpl implements PolicyDAO { while (resultSet.next()) { deviceTypeId = resultSet.getInt("ID"); } - } catch (SQLException e) { - String msg = "Error occurred while selecting the device type id."; - log.error(msg, e); - throw new PolicyManagerDAOException(msg, e); + throw new PolicyManagerDAOException("Error occurred while selecting the device type id", e); } finally { PolicyManagementDAOUtil.cleanupResources(stmt, resultSet); - this.closeConnection(); } return deviceTypeId; } private int readHighestPriorityOfPolicies() throws PolicyManagerDAOException { - Connection conn; PreparedStatement stmt = null; ResultSet resultSet = null; @@ -1216,11 +1035,8 @@ public class PolicyDAOImpl implements PolicyDAO { if (log.isDebugEnabled()) { log.debug("Priority of the new policy added is (" + priority + ")"); } - } catch (SQLException e) { - String msg = "Error occurred while reading the highest priority of the policies."; - log.error(msg, e); - throw new PolicyManagerDAOException(msg, e); + throw new PolicyManagerDAOException("Error occurred while reading the highest priority of the policies", e); } finally { PolicyManagementDAOUtil.cleanupResources(stmt, resultSet); } @@ -1229,13 +1045,11 @@ public class PolicyDAOImpl implements PolicyDAO { @Override public int getPolicyCount() throws PolicyManagerDAOException { - Connection conn; PreparedStatement stmt = null; ResultSet resultSet = null; int tenantId = PrivilegedCarbonContext.getThreadLocalCarbonContext().getTenantId(); int policyCount = 0; - try { conn = this.getConnection(); String query = "SELECT COUNT(ID) AS POLICY_COUNT FROM DM_POLICY WHERE TENANT_ID = ?"; @@ -1247,25 +1061,19 @@ public class PolicyDAOImpl implements PolicyDAO { policyCount = resultSet.getInt("POLICY_COUNT"); } return policyCount; - } catch (SQLException e) { - String msg = "Error occurred while reading the policies from the database."; - log.error(msg, e); - throw new PolicyManagerDAOException(msg, e); + throw new PolicyManagerDAOException("Error occurred while reading the policies from the database", e); } finally { PolicyManagementDAOUtil.cleanupResources(stmt, resultSet); - this.closeConnection(); } } @Override public int getAppliedPolicyId(int deviceId) throws PolicyManagerDAOException { - Connection conn; PreparedStatement stmt = null; ResultSet resultSet = null; int tenantId = PrivilegedCarbonContext.getThreadLocalCarbonContext().getTenantId(); - try { conn = this.getConnection(); String query = "SELECT * FROM DM_DEVICE_POLICY_APPLIED WHERE DEVICE_ID = ? AND TENANT_ID = ?"; @@ -1277,28 +1085,21 @@ public class PolicyDAOImpl implements PolicyDAO { while (resultSet.next()) { return resultSet.getInt("POLICY_ID"); } - } catch (SQLException e) { - String msg = "Error occurred while getting the applied policy id."; - log.error(msg, e); - throw new PolicyManagerDAOException(msg, e); + throw new PolicyManagerDAOException("Error occurred while getting the applied policy id", e); } finally { PolicyManagementDAOUtil.cleanupResources(stmt, resultSet); - this.closeConnection(); } - return 0; } @Override public Policy getAppliedPolicy(int deviceId) throws PolicyManagerDAOException { - Connection conn; PreparedStatement stmt = null; ResultSet resultSet = null; int tenantId = PrivilegedCarbonContext.getThreadLocalCarbonContext().getTenantId(); Policy policy = null; - try { conn = this.getConnection(); String query = "SELECT * FROM DM_DEVICE_POLICY_APPLIED WHERE DEVICE_ID = ? AND TENANT_ID = ?"; @@ -1336,20 +1137,13 @@ public class PolicyDAOImpl implements PolicyDAO { } } catch (SQLException e) { - String msg = "Error occurred while getting the applied policy."; - log.error(msg, e); - throw new PolicyManagerDAOException(msg, e); + throw new PolicyManagerDAOException("Error occurred while getting the applied policy", e); } catch (IOException e) { - String msg = "Unable to read the byte stream for content"; - log.error(msg); - throw new PolicyManagerDAOException(msg, e); + throw new PolicyManagerDAOException("Unable to read the byte stream for content", e); } catch (ClassNotFoundException e) { - String msg = "Class not found while converting the object"; - log.error(msg); - throw new PolicyManagerDAOException(msg, e); + throw new PolicyManagerDAOException("Class not found while converting the object", e); } finally { PolicyManagementDAOUtil.cleanupResources(stmt, resultSet); - this.closeConnection(); } // // if (policy != null && log.isDebugEnabled()) { @@ -1365,14 +1159,11 @@ public class PolicyDAOImpl implements PolicyDAO { @Override public HashMap getAppliedPolicyIds(List deviceIds) throws PolicyManagerDAOException { - Connection conn; PreparedStatement stmt = null; ResultSet resultSet = null; - HashMap devicePolicyIds = new HashMap<>(); int tenantId = PrivilegedCarbonContext.getThreadLocalCarbonContext().getTenantId(); - try { conn = this.getConnection(); 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()) { devicePolicyIds.put(resultSet.getInt("DEVICE_ID"), resultSet.getInt("POLICY_ID")); } - } catch (SQLException e) { - String msg = "Error occurred while getting the applied policy."; - log.error(msg, e); - throw new PolicyManagerDAOException(msg, e); + throw new PolicyManagerDAOException("Error occurred while getting the applied policy", e); } finally { PolicyManagementDAOUtil.cleanupResources(stmt, resultSet); - this.closeConnection(); } - return devicePolicyIds; } diff --git a/components/policy-mgt/org.wso2.carbon.policy.mgt.core/src/main/java/org/wso2/carbon/policy/mgt/core/dao/impl/ProfileDAOImpl.java b/components/policy-mgt/org.wso2.carbon.policy.mgt.core/src/main/java/org/wso2/carbon/policy/mgt/core/dao/impl/ProfileDAOImpl.java index f6700942d9..58ceefba76 100644 --- a/components/policy-mgt/org.wso2.carbon.policy.mgt.core/src/main/java/org/wso2/carbon/policy/mgt/core/dao/impl/ProfileDAOImpl.java +++ b/components/policy-mgt/org.wso2.carbon.policy.mgt.core/src/main/java/org/wso2/carbon/policy/mgt/core/dao/impl/ProfileDAOImpl.java @@ -210,7 +210,6 @@ public class ProfileDAOImpl implements ProfileDAO { throw new ProfileManagerDAOException(msg, e); } finally { PolicyManagementDAOUtil.cleanupResources(stmt, resultSet); - this.closeConnection(); } return profile; } @@ -254,7 +253,6 @@ public class ProfileDAOImpl implements ProfileDAO { throw new ProfileManagerDAOException(msg, e); } finally { PolicyManagementDAOUtil.cleanupResources(stmt, resultSet); - this.closeConnection(); } return profileList; } @@ -292,7 +290,6 @@ public class ProfileDAOImpl implements ProfileDAO { throw new ProfileManagerDAOException(msg, e); } finally { PolicyManagementDAOUtil.cleanupResources(stmt, resultSet); - this.closeConnection(); } 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."); - } - } - } diff --git a/components/policy-mgt/org.wso2.carbon.policy.mgt.core/src/main/java/org/wso2/carbon/policy/mgt/core/mgt/impl/FeatureManagerImpl.java b/components/policy-mgt/org.wso2.carbon.policy.mgt.core/src/main/java/org/wso2/carbon/policy/mgt/core/mgt/impl/FeatureManagerImpl.java index 94ee440a10..ea3ed7cd7f 100644 --- a/components/policy-mgt/org.wso2.carbon.policy.mgt.core/src/main/java/org/wso2/carbon/policy/mgt/core/mgt/impl/FeatureManagerImpl.java +++ b/components/policy-mgt/org.wso2.carbon.policy.mgt.core/src/main/java/org/wso2/carbon/policy/mgt/core/mgt/impl/FeatureManagerImpl.java @@ -21,6 +21,7 @@ package org.wso2.carbon.policy.mgt.core.mgt.impl; import org.apache.commons.logging.Log; import org.apache.commons.logging.LogFactory; 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.Profile; 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.mgt.FeatureManager; +import java.sql.SQLException; import java.util.List; public class FeatureManagerImpl implements FeatureManager { @@ -137,23 +139,15 @@ public class FeatureManagerImpl implements FeatureManager { bool = featureDAO.deleteFeature(feature.getId()); PolicyManagementDAOFactory.commitTransaction(); } catch (FeatureManagerDAOException e) { - try { - PolicyManagementDAOFactory.rollbackTransaction(); - } catch (PolicyManagerDAOException e1) { - log.warn("Unable to roll back the transaction"); - } - String msg = "Error occurred while deleting the feature (" + feature.getName() + ")"; - log.error(msg, e); - throw new FeatureManagementException(msg, e); + PolicyManagementDAOFactory.rollbackTransaction(); + throw new FeatureManagementException("Error occurred while deleting the feature (" + feature.getName() + + ")", e); } catch (PolicyManagerDAOException e) { - try { - PolicyManagementDAOFactory.rollbackTransaction(); - } catch (PolicyManagerDAOException e1) { - log.warn("Unable to roll back the transaction"); - } - String msg = "Error occurred while deleting the feature (" + feature.getName() + ") from database"; - log.error(msg, e); - throw new FeatureManagementException(msg, e); + PolicyManagementDAOFactory.rollbackTransaction(); + throw new FeatureManagementException("Error occurred while deleting the feature (" + feature.getName() + + ") from database", e); + } finally { + PolicyManagementDAOFactory.closeConnection(); } return bool; } @@ -164,22 +158,11 @@ public class FeatureManagerImpl implements FeatureManager { PolicyManagementDAOFactory.beginTransaction(); feature = featureDAO.addProfileFeature(feature, profileId); PolicyManagementDAOFactory.commitTransaction(); - - } catch (PolicyManagerDAOException e) { - String msg = "Error occurred while adding profile feature (" + - 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 adding profile feature (" + - feature.getFeatureCode() + " - " + profileId + ") to database."; - log.error(msg, e); - throw new FeatureManagementException(msg, e); + } catch (PolicyManagerDAOException | FeatureManagerDAOException e) { + throw new FeatureManagementException("Error occurred while adding profile feature (" + + feature.getFeatureCode() + " - " + profileId + ")", e); + } finally { + PolicyManagementDAOFactory.closeConnection(); } return feature; } @@ -192,26 +175,12 @@ public class FeatureManagerImpl implements FeatureManager { feature = featureDAO.updateProfileFeature(feature, profileId); PolicyManagementDAOFactory.commitTransaction(); - } catch (PolicyManagerDAOException 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 + ")"; - 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); + } catch (FeatureManagerDAOException | PolicyManagerDAOException e) { + PolicyManagementDAOFactory.rollbackTransaction(); + throw new FeatureManagementException("Error occurred while updating feature (" + + feature.getFeatureCode() + " - " + profileId + ") in database.", e); + } finally { + PolicyManagementDAOFactory.closeConnection(); } return feature; } @@ -224,23 +193,15 @@ public class FeatureManagerImpl implements FeatureManager { features = featureDAO.addProfileFeatures(features, profileId); PolicyManagementDAOFactory.commitTransaction(); } catch (FeatureManagerDAOException e) { - try { - PolicyManagementDAOFactory.rollbackTransaction(); - } catch (PolicyManagerDAOException e1) { - log.warn("Unable to roll back the transaction"); - } - String msg = "Error occurred while adding the features to profile id (" + profileId + ")"; - log.error(msg, e); - throw new FeatureManagementException(msg, e); + PolicyManagementDAOFactory.rollbackTransaction(); + throw new FeatureManagementException("Error occurred while adding the features to profile id (" + + profileId + ")", e); } catch (PolicyManagerDAOException e) { - try { - PolicyManagementDAOFactory.rollbackTransaction(); - } catch (PolicyManagerDAOException e1) { - log.warn("Unable to roll back the transaction"); - } - String msg = "Error occurred while adding the features to profile id (" + profileId + ") to the database"; - log.error(msg, e); - throw new FeatureManagementException(msg, e); + PolicyManagementDAOFactory.rollbackTransaction(); + throw new FeatureManagementException("Error occurred while adding the features to profile id (" + + profileId + ") to the database", e); + } finally { + PolicyManagementDAOFactory.closeConnection(); } return features; } @@ -253,23 +214,15 @@ public class FeatureManagerImpl implements FeatureManager { features = featureDAO.updateProfileFeatures(features, profileId); PolicyManagementDAOFactory.commitTransaction(); } catch (FeatureManagerDAOException e) { - try { - PolicyManagementDAOFactory.rollbackTransaction(); - } catch (PolicyManagerDAOException e1) { - log.warn("Unable to roll back the transaction"); - } - String msg = "Error occurred while updating the features to profile id (" + profileId + ")"; - log.error(msg, e); - throw new FeatureManagementException(msg, e); + PolicyManagementDAOFactory.rollbackTransaction(); + throw new FeatureManagementException("Error occurred while updating the features to profile id (" + + profileId + ")", e); } catch (PolicyManagerDAOException e) { - try { - PolicyManagementDAOFactory.rollbackTransaction(); - } catch (PolicyManagerDAOException e1) { - log.warn("Unable to roll back the transaction"); - } - String msg = "Error occurred while updating the features to profile id (" + profileId + ") to the database"; - log.error(msg, e); - throw new FeatureManagementException(msg, e); + PolicyManagementDAOFactory.rollbackTransaction(); + throw new FeatureManagementException("Error occurred while updating the features to profile id (" + + profileId + ") to the database", e); + } finally { + PolicyManagementDAOFactory.closeConnection(); } return features; } @@ -278,22 +231,28 @@ public class FeatureManagerImpl implements FeatureManager { @Override public List getAllFeatures(String deviceType) throws FeatureManagementException { try { + PolicyManagementDAOFactory.openConnection(); return featureDAO.getAllFeatures(deviceType); } catch (FeatureManagerDAOException e) { - String msg = "Error occurred while getting the features."; - log.error(msg, e); - throw new FeatureManagementException(msg, e); + throw new FeatureManagementException("Error occurred while retrieving the features", e); + } catch (SQLException e) { + throw new FeatureManagementException("Error occurred while opening a connection to the data source", e); + } finally { + PolicyManagementDAOFactory.closeConnection(); } } @Override public List getFeaturesForProfile(int profileId) throws FeatureManagementException { try { + DeviceManagementDAOFactory.openConnection(); return featureDAO.getFeaturesForProfile(profileId); } catch (FeatureManagerDAOException e) { - String msg = "Error occurred while getting the features."; - log.error(msg, e); - throw new FeatureManagementException(msg, e); + throw new FeatureManagementException("Error occurred while getting the features", e); + } catch (SQLException 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); PolicyManagementDAOFactory.commitTransaction(); } catch (FeatureManagerDAOException e) { - try { - PolicyManagementDAOFactory.rollbackTransaction(); - } catch (PolicyManagerDAOException e1) { - log.warn("Unable to roll back the transaction"); - } - String msg = "Error occurred while deleting the feature - id (" + featureId + ")"; - log.error(msg, e); - throw new FeatureManagementException(msg, e); + PolicyManagementDAOFactory.rollbackTransaction(); + throw new FeatureManagementException("Error occurred while deleting the feature - id (" + + featureId + ")", e); } catch (PolicyManagerDAOException e) { - try { - PolicyManagementDAOFactory.rollbackTransaction(); - } catch (PolicyManagerDAOException e1) { - log.warn("Unable to roll back the transaction"); - } - String msg = "Error occurred while deleting the feature - id (" + featureId + ") from database."; - log.error(msg, e); - throw new FeatureManagementException(msg, e); + PolicyManagementDAOFactory.rollbackTransaction(); + throw new FeatureManagementException("Error occurred while deleting the feature - id (" + featureId + + ") from database.", e); + } finally { + PolicyManagementDAOFactory.closeConnection(); } return bool; } @@ -334,24 +285,15 @@ public class FeatureManagerImpl implements FeatureManager { bool = featureDAO.deleteFeaturesOfProfile(profile); PolicyManagementDAOFactory.commitTransaction(); } catch (FeatureManagerDAOException e) { - try { - PolicyManagementDAOFactory.rollbackTransaction(); - } catch (PolicyManagerDAOException e1) { - log.warn("Unable to roll back the transaction"); - } - String msg = "Error occurred while deleting the feature of - profile (" + profile.getProfileName() + ")"; - log.error(msg, e); - throw new FeatureManagementException(msg, e); + PolicyManagementDAOFactory.rollbackTransaction(); + throw new FeatureManagementException("Error occurred while deleting the feature of - profile (" + + profile.getProfileName() + ")", e); } catch (PolicyManagerDAOException e) { - try { - PolicyManagementDAOFactory.rollbackTransaction(); - } catch (PolicyManagerDAOException e1) { - log.warn("Unable to roll back the transaction"); - } - String msg = "Error occurred while deleting the feature of - profile (" + - profile.getProfileName() + ") from database"; - log.error(msg, e); - throw new FeatureManagementException(msg, e); + PolicyManagementDAOFactory.rollbackTransaction(); + throw new FeatureManagementException("Error occurred while deleting the feature of - profile (" + + profile.getProfileName() + ") from database", e); + } finally { + PolicyManagementDAOFactory.closeConnection(); } return bool; } diff --git a/components/policy-mgt/org.wso2.carbon.policy.mgt.core/src/main/java/org/wso2/carbon/policy/mgt/core/mgt/impl/MonitoringManagerImpl.java b/components/policy-mgt/org.wso2.carbon.policy.mgt.core/src/main/java/org/wso2/carbon/policy/mgt/core/mgt/impl/MonitoringManagerImpl.java index db31b6b727..a4b088e596 100644 --- a/components/policy-mgt/org.wso2.carbon.policy.mgt.core/src/main/java/org/wso2/carbon/policy/mgt/core/mgt/impl/MonitoringManagerImpl.java +++ b/components/policy-mgt/org.wso2.carbon.policy.mgt.core/src/main/java/org/wso2/carbon/policy/mgt/core/mgt/impl/MonitoringManagerImpl.java @@ -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.util.PolicyManagerUtil; +import java.sql.SQLException; import java.util.*; public class MonitoringManagerImpl implements MonitoringManager { @@ -77,8 +78,6 @@ public class MonitoringManagerImpl implements MonitoringManager { List complianceFeatures = new ArrayList<>(); try { - - DeviceManagementProviderService service = new DeviceManagementProviderServiceImpl(); PolicyManager manager = new PolicyManagerImpl(); Device device = service.getDevice(deviceIdentifier); @@ -91,14 +90,21 @@ public class MonitoringManagerImpl implements MonitoringManager { ComplianceData complianceData; // This was retrieved from database because compliance id must be present for other dao operations to // run. - ComplianceData cmd = monitoringDAO.getCompliance(device.getId()); - complianceData = monitoringService.checkPolicyCompliance(deviceIdentifier, - policy, deviceResponse); - complianceData.setId(cmd.getId()); - complianceData.setPolicy(policy); - complianceFeatures = complianceData.getComplianceFeatures(); - complianceData.setDeviceId(device.getId()); - complianceData.setPolicyId(policy.getId()); + try { + PolicyManagementDAOFactory.openConnection(); + ComplianceData cmd = monitoringDAO.getCompliance(device.getId()); + complianceData = monitoringService.checkPolicyCompliance(deviceIdentifier, + policy, deviceResponse); + complianceData.setId(cmd.getId()); + complianceData.setPolicy(policy); + complianceFeatures = complianceData.getComplianceFeatures(); + complianceData.setDeviceId(device.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(); //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 { monitoringDAO.setDeviceAsCompliance(device.getId(), policy.getId()); //complianceData.setId(cmf.getId()); monitoringDAO.deleteNoneComplianceData(complianceData.getId()); } PolicyManagementDAOFactory.commitTransaction(); - } else { if (log.isDebugEnabled()) { log.debug("There is no policy applied to this device, hence compliance monitoring was not called."); } } - } catch (DeviceManagementException e) { - try { - PolicyManagementDAOFactory.rollbackTransaction(); - } catch (PolicyManagerDAOException e1) { - log.warn("Error occurred while roll backing the transaction."); - } - 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(); - } 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); + PolicyManagementDAOFactory.rollbackTransaction(); + throw new PolicyComplianceException("Unable tor retrieve device data from DB for " + + deviceIdentifier.getId() + " - " + deviceIdentifier.getType(), e); + } catch (PolicyManagerDAOException | PolicyManagementException e) { + PolicyManagementDAOFactory.rollbackTransaction(); + throw new PolicyComplianceException("Unable tor retrieve policy data from DB for device " + + deviceIdentifier.getId() + " - " + deviceIdentifier.getType(), e); } catch (MonitoringDAOException e) { - try { - PolicyManagementDAOFactory.rollbackTransaction(); - } catch (PolicyManagerDAOException e1) { - log.warn("Error occurred while roll backing the transaction."); - } - String msg = "Unable to add the none compliance features to database for device " + deviceIdentifier. - 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); + PolicyManagementDAOFactory.rollbackTransaction(); + throw new PolicyComplianceException("Unable to add the none compliance features to database for device " + + deviceIdentifier.getId() + " - " + deviceIdentifier.getType(), e); + } finally { + PolicyManagementDAOFactory.closeConnection(); } return complianceFeatures; } - @Override public boolean isCompliance(DeviceIdentifier deviceIdentifier) throws PolicyComplianceException { - try { DeviceManagementProviderService service = new DeviceManagementProviderServiceImpl(); Device device = service.getDevice(deviceIdentifier); //deviceDAO.getDevice(deviceIdentifier, tenantId); + PolicyManagementDAOFactory.openConnection(); ComplianceData complianceData = monitoringDAO.getCompliance(device.getId()); if (complianceData == null || !complianceData.isStatus()) { return false; } - } catch (DeviceManagementException e) { - String msg = "Unable to retrieve device data for " + deviceIdentifier.getId() + " - " + - deviceIdentifier.getType(); - log.error(msg, e); - throw new PolicyComplianceException(msg, e); + throw new PolicyComplianceException("Unable to retrieve device data for " + deviceIdentifier.getId() + + " - " + deviceIdentifier.getType(), e); } catch (MonitoringDAOException e) { - String msg = "Unable to retrieve compliance status for " + deviceIdentifier.getId() + " - " + - deviceIdentifier.getType(); - log.error(msg, e); - throw new PolicyComplianceException(msg, e); + throw new PolicyComplianceException("Unable to retrieve compliance status for " + deviceIdentifier.getId() + + " - " + deviceIdentifier.getType(), e); + } catch (SQLException e) { + throw new PolicyComplianceException("Error occurred while opening a connection to the data source", e); + } finally { + PolicyManagementDAOFactory.closeConnection(); } return true; } @@ -212,6 +187,7 @@ public class MonitoringManagerImpl implements MonitoringManager { ComplianceData complianceData; try { + PolicyManagementDAOFactory.openConnection(); DeviceManagementProviderService service = new DeviceManagementProviderServiceImpl(); Device device = service.getDevice(deviceIdentifier); complianceData = monitoringDAO.getCompliance(device.getId()); @@ -220,26 +196,23 @@ public class MonitoringManagerImpl implements MonitoringManager { complianceData.setComplianceFeatures(complianceFeatures); } catch (DeviceManagementException e) { - String msg = "Unable to retrieve device data for " + deviceIdentifier.getId() + " - " + - deviceIdentifier.getType(); - log.error(msg, e); - throw new PolicyComplianceException(msg, e); + throw new PolicyComplianceException("Unable to retrieve device data for " + deviceIdentifier.getId() + + " - " + deviceIdentifier.getType(), e); } catch (MonitoringDAOException e) { - String msg = "Unable to retrieve compliance data for " + deviceIdentifier.getId() + " - " + - deviceIdentifier.getType(); - log.error(msg, e); - throw new PolicyComplianceException(msg, e); + throw new PolicyComplianceException("Unable to retrieve compliance data for " + deviceIdentifier.getId() + + " - " + deviceIdentifier.getType(), e); + } catch (SQLException e) { + throw new PolicyComplianceException("Error occurred while opening a connection to the data source", e); + } finally { + PolicyManagementDAOFactory.closeConnection(); } return complianceData; } @Override public void addMonitoringOperation(List devices) throws PolicyComplianceException { - try { - - ComplianceDecisionPoint decisionPoint = new ComplianceDecisionPointImpl(); //int tenantId = PolicyManagerUtil.getTenantId(); @@ -294,7 +267,6 @@ public class MonitoringManagerImpl implements MonitoringManager { } } - PolicyManagementDAOFactory.beginTransaction(); if (!deviceIdsToAddOperation.isEmpty()) { @@ -310,39 +282,20 @@ public class MonitoringManagerImpl implements MonitoringManager { } PolicyManagementDAOFactory.commitTransaction(); - } catch (MonitoringDAOException e) { - try { - PolicyManagementDAOFactory.rollbackTransaction(); - } catch (PolicyManagerDAOException e1) { - 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); + PolicyManagementDAOFactory.rollbackTransaction(); + throw new PolicyComplianceException("Error occurred from monitoring dao.", e); } catch (OperationManagementException e) { - try { - PolicyManagementDAOFactory.rollbackTransaction(); - } catch (PolicyManagerDAOException e1) { - 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); + PolicyManagementDAOFactory.rollbackTransaction(); + throw new PolicyComplianceException("Error occurred while adding monitoring operation to devices", e); } catch (PolicyManagerDAOException e) { - try { - PolicyManagementDAOFactory.rollbackTransaction(); - } catch (PolicyManagerDAOException e1) { - log.warn("Error occurred while roll backing the transaction."); - } - String msg = "Error occurred reading the applied policies to devices."; - log.error(msg, e); - throw new PolicyComplianceException(msg, e); + PolicyManagementDAOFactory.rollbackTransaction(); + throw new PolicyComplianceException("Error occurred reading the applied policies to devices.", e); + } finally { + PolicyManagementDAOFactory.closeConnection(); } - } - private void addMonitoringOperationsToDatabase(List devices) throws PolicyComplianceException, OperationManagementException { @@ -359,7 +312,6 @@ public class MonitoringManagerImpl implements MonitoringManager { } private List getDeviceIdentifiersFromDevices(List devices) { - List deviceIdentifiers = new ArrayList<>(); for (Device device : devices) { DeviceIdentifier identifier = new DeviceIdentifier(); diff --git a/components/policy-mgt/org.wso2.carbon.policy.mgt.core/src/main/java/org/wso2/carbon/policy/mgt/core/mgt/impl/PolicyManagerImpl.java b/components/policy-mgt/org.wso2.carbon.policy.mgt.core/src/main/java/org/wso2/carbon/policy/mgt/core/mgt/impl/PolicyManagerImpl.java index f3dc1c0638..cd777fb7b8 100644 --- a/components/policy-mgt/org.wso2.carbon.policy.mgt.core/src/main/java/org/wso2/carbon/policy/mgt/core/mgt/impl/PolicyManagerImpl.java +++ b/components/policy-mgt/org.wso2.carbon.policy.mgt.core/src/main/java/org/wso2/carbon/policy/mgt/core/mgt/impl/PolicyManagerImpl.java @@ -20,7 +20,6 @@ package org.wso2.carbon.policy.mgt.core.mgt.impl; import org.apache.commons.logging.Log; import org.apache.commons.logging.LogFactory; -import org.wso2.carbon.context.CarbonContext; import org.wso2.carbon.context.PrivilegedCarbonContext; import org.wso2.carbon.device.mgt.common.Device; 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.mgt.PolicyManager; 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.util.ArrayList; import java.util.Calendar; @@ -125,36 +124,20 @@ public class PolicyManagerImpl implements PolicyManager { PolicyManagementDAOFactory.commitTransaction(); } catch (PolicyManagerDAOException e) { - try { - PolicyManagementDAOFactory.rollbackTransaction(); - } catch (PolicyManagerDAOException e1) { - log.warn("Error occurred while roll backing the transaction."); - } - String msg = "Error occurred while adding the policy (" + - policy.getId() + " - " + policy.getPolicyName() + ")"; - log.error(msg, e); - throw new PolicyManagementException(msg, e); + PolicyManagementDAOFactory.rollbackTransaction(); + throw new PolicyManagementException("Error occurred while adding the policy (" + + policy.getId() + " - " + policy.getPolicyName() + ")", e); } catch (ProfileManagerDAOException e) { - try { - PolicyManagementDAOFactory.rollbackTransaction(); - } catch (PolicyManagerDAOException e1) { - log.warn("Error occurred while roll backing the transaction."); - } - String msg = "Error occurred while adding the profile related to policy (" + - policy.getId() + " - " + policy.getPolicyName() + ")"; - log.error(msg, e); - throw new PolicyManagementException(msg, e); + PolicyManagementDAOFactory.rollbackTransaction(); + throw new PolicyManagementException("Error occurred while adding the profile related to policy (" + + policy.getId() + " - " + policy.getPolicyName() + ")", e); } catch (FeatureManagerDAOException e) { - try { - PolicyManagementDAOFactory.rollbackTransaction(); - } catch (PolicyManagerDAOException e1) { - log.warn("Error occurred while roll backing the transaction."); - } - String msg = "Error occurred while adding the features of profile related to policy (" + - policy.getId() + " - " + policy.getPolicyName() + ")"; - log.error(msg, e); - throw new PolicyManagementException(msg, e); + PolicyManagementDAOFactory.rollbackTransaction(); + throw new PolicyManagementException("Error occurred while adding the features of profile related to " + + "policy (" + policy.getId() + " - " + policy.getPolicyName() + ")", e); + } finally { + PolicyManagementDAOFactory.closeConnection(); } return policy; } @@ -207,17 +190,12 @@ public class PolicyManagerImpl implements PolicyManager { // } PolicyManagementDAOFactory.commitTransaction(); - } catch (PolicyManagerDAOException e) { - try { - PolicyManagementDAOFactory.rollbackTransaction(); - } catch (PolicyManagerDAOException e1) { - log.warn("Error occurred while roll backing the transaction."); - } - String msg = "Error occurred while updating the policy (" - + policy.getId() + " - " + policy.getPolicyName() + ")"; - log.error(msg, e); - throw new PolicyManagementException(msg, e); + PolicyManagementDAOFactory.rollbackTransaction(); + throw new PolicyManagementException("Error occurred while updating the policy (" + + policy.getId() + " - " + policy.getPolicyName() + ")", e); + } finally { + PolicyManagementDAOFactory.closeConnection(); } return policy; } @@ -230,21 +208,16 @@ public class PolicyManagerImpl implements PolicyManager { bool = policyDAO.updatePolicyPriorities(policies); PolicyManagementDAOFactory.commitTransaction(); } catch (PolicyManagerDAOException e) { - try { - PolicyManagementDAOFactory.rollbackTransaction(); - } catch (PolicyManagerDAOException e1) { - log.warn("Error occurred while roll backing the transaction."); - } - String msg = "Error occurred while updating the policy priorities"; - log.error(msg, e); - throw new PolicyManagementException(msg, e); + PolicyManagementDAOFactory.rollbackTransaction(); + throw new PolicyManagementException("Error occurred while updating the policy priorities", e); + } finally { + PolicyManagementDAOFactory.closeConnection(); } return bool; } @Override public boolean deletePolicy(Policy policy) throws PolicyManagementException { - try { PolicyManagementDAOFactory.beginTransaction(); policyDAO.deleteAllPolicyRelatedConfigs(policy.getId()); @@ -254,46 +227,28 @@ public class PolicyManagerImpl implements PolicyManager { PolicyManagementDAOFactory.commitTransaction(); return true; } catch (PolicyManagerDAOException e) { - try { - PolicyManagementDAOFactory.rollbackTransaction(); - } catch (PolicyManagerDAOException e1) { - log.warn("Error occurred while roll backing the transaction."); - } - String msg = "Error occurred while deleting the policy (" - + policy.getId() + " - " + policy.getPolicyName() + ")"; - log.error(msg, e); - throw new PolicyManagementException(msg, e); + PolicyManagementDAOFactory.rollbackTransaction(); + throw new PolicyManagementException("Error occurred while deleting the policy (" + + policy.getId() + " - " + policy.getPolicyName() + ")", e); } catch (ProfileManagerDAOException e) { - try { - PolicyManagementDAOFactory.rollbackTransaction(); - } catch (PolicyManagerDAOException e1) { - log.warn("Error occurred while roll backing the transaction."); - } - String msg = "Error occurred while deleting the profile for policy (" - + policy.getId() + ")"; - log.error(msg, e); - throw new PolicyManagementException(msg, e); + PolicyManagementDAOFactory.rollbackTransaction(); + throw new PolicyManagementException("Error occurred while deleting the profile for policy (" + + policy.getId() + ")", e); } catch (FeatureManagerDAOException e) { - try { - PolicyManagementDAOFactory.rollbackTransaction(); - } catch (PolicyManagerDAOException e1) { - log.warn("Error occurred while roll backing the transaction."); - } - String msg = "Error occurred while deleting the profile features for policy (" - + policy.getId() + ")"; - log.error(msg, e); - throw new PolicyManagementException(msg, e); + PolicyManagementDAOFactory.rollbackTransaction(); + throw new PolicyManagementException("Error occurred while deleting the profile features for policy (" + + policy.getId() + ")", e); + } finally { + PolicyManagementDAOFactory.closeConnection(); } - } @Override public boolean deletePolicy(int policyId) throws PolicyManagementException { - try { - Policy policy = policyDAO.getPolicy(policyId); - PolicyManagementDAOFactory.beginTransaction(); + + Policy policy = policyDAO.getPolicy(policyId); policyDAO.deleteAllPolicyRelatedConfigs(policyId); policyDAO.deletePolicy(policyId); @@ -306,50 +261,31 @@ public class PolicyManagerImpl implements PolicyManager { profileDAO.deleteProfile(policy.getProfileId()); PolicyManagementDAOFactory.commitTransaction(); return true; - } catch (PolicyManagerDAOException e) { - try { - PolicyManagementDAOFactory.rollbackTransaction(); - } catch (PolicyManagerDAOException e1) { - 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); + PolicyManagementDAOFactory.rollbackTransaction(); + throw new PolicyManagementException("Error occurred while deleting the policy (" + policyId + ")", e); } catch (ProfileManagerDAOException e) { - try { - PolicyManagementDAOFactory.rollbackTransaction(); - } catch (PolicyManagerDAOException e1) { - log.warn("Error occurred while roll backing the transaction."); - } - String msg = "Error occurred while deleting the profile for policy (" - + policyId + ")"; - log.error(msg, e); - throw new PolicyManagementException(msg, e); + PolicyManagementDAOFactory.rollbackTransaction(); + throw new PolicyManagementException("Error occurred while deleting the profile for policy (" + + policyId + ")", e); } catch (FeatureManagerDAOException e) { - try { - PolicyManagementDAOFactory.rollbackTransaction(); - } catch (PolicyManagerDAOException e1) { - log.warn("Error occurred while roll backing the transaction."); - } - String msg = "Error occurred while deleting the profile features for policy (" - + policyId + ")"; - log.error(msg, e); - throw new PolicyManagementException(msg, e); + PolicyManagementDAOFactory.rollbackTransaction(); + throw new PolicyManagementException("Error occurred while deleting the profile features for policy (" + + policyId + ")", e); + } finally { + PolicyManagementDAOFactory.closeConnection(); } } @Override - public Policy addPolicyToDevice(List deviceIdentifierList, Policy policy) - throws PolicyManagementException { - + public Policy addPolicyToDevice(List deviceIdentifierList, + Policy policy) throws PolicyManagementException { try { PolicyManagementDAOFactory.beginTransaction(); if (policy.getId() == 0) { policyDAO.addPolicy(policy); } - List deviceList = new ArrayList(); + List deviceList = new ArrayList<>(); DeviceManagementProviderService service = new DeviceManagementProviderServiceImpl(); for (DeviceIdentifier deviceIdentifier : deviceIdentifierList) { deviceList.add(service.getDevice(deviceIdentifier)); @@ -368,34 +304,21 @@ public class PolicyManagerImpl implements PolicyManager { } policy.setDevices(deviceList); } - } catch (PolicyManagerDAOException e) { - try { - PolicyManagementDAOFactory.rollbackTransaction(); - } catch (PolicyManagerDAOException e1) { - log.warn("Error occurred while roll backing the transaction."); - } - String msg = "Error occurred while adding the policy (" - + policy.getId() + " - " + policy.getPolicyName() + ")"; - log.error(msg, e); - throw new PolicyManagementException(msg, e); + PolicyManagementDAOFactory.rollbackTransaction(); + throw new PolicyManagementException("Error occurred while adding the policy (" + + policy.getId() + " - " + policy.getPolicyName() + ")", e); } catch (DeviceManagementException e) { - try { - PolicyManagementDAOFactory.rollbackTransaction(); - } catch (PolicyManagerDAOException e1) { - log.warn("Error occurred while roll backing the transaction."); - } - String msg = "Error occurred while adding the policy to device list"; - log.error(msg, e); - throw new PolicyManagementException(msg, e); + PolicyManagementDAOFactory.rollbackTransaction(); + throw new PolicyManagementException("Error occurred while adding the policy to device list", e); + } finally { + PolicyManagementDAOFactory.closeConnection(); } - return policy; } @Override public Policy addPolicyToRole(List roleNames, Policy policy) throws PolicyManagementException { - try { PolicyManagementDAOFactory.beginTransaction(); if (policy.getId() == 0) { @@ -415,19 +338,13 @@ public class PolicyManagerImpl implements PolicyManager { } policy.setRoles(roleNames); } - } catch (PolicyManagerDAOException e) { - try { - PolicyManagementDAOFactory.rollbackTransaction(); - } catch (PolicyManagerDAOException e1) { - log.warn("Error occurred while roll backing the transaction."); - } - String msg = "Error occurred while adding the policy (" - + policy.getId() + " - " + policy.getPolicyName() + ")"; - log.error(msg, e); - throw new PolicyManagementException(msg, e); + PolicyManagementDAOFactory.rollbackTransaction(); + throw new PolicyManagementException("Error occurred while adding the policy (" + + policy.getId() + " - " + policy.getPolicyName() + ")", e); + } finally { + PolicyManagementDAOFactory.closeConnection(); } - return policy; } @@ -453,17 +370,12 @@ public class PolicyManagerImpl implements PolicyManager { } policy.setRoles(usernameList); } - } catch (PolicyManagerDAOException e) { - try { - PolicyManagementDAOFactory.rollbackTransaction(); - } catch (PolicyManagerDAOException e1) { - log.warn("Error occurred while roll backing the transaction."); - } - String msg = "Error occurred while adding the policy (" - + policy.getId() + " - " + policy.getPolicyName() + ") to user list."; - log.error(msg, e); - throw new PolicyManagementException(msg, e); + PolicyManagementDAOFactory.rollbackTransaction(); + throw new PolicyManagementException("Error occurred while adding the policy (" + + policy.getId() + " - " + policy.getPolicyName() + ") to user list.", e); + } finally { + PolicyManagementDAOFactory.closeConnection(); } return policy; } @@ -477,6 +389,7 @@ public class PolicyManagerImpl implements PolicyManager { List roleNames; try { + PolicyManagementDAOFactory.openConnection(); policy = policyDAO.getPolicyByProfileID(profileId); deviceList = getPolicyAppliedDevicesIds(policy.getId()); roleNames = policyDAO.getPolicyAppliedRoles(policy.getId()); @@ -491,13 +404,15 @@ public class PolicyManagerImpl implements PolicyManager { policy.setDevices(deviceList); } catch (PolicyManagerDAOException e) { - String msg = "Error occurred while getting the policy related to profile ID (" + profileId + ")"; - log.error(msg, e); - throw new PolicyManagementException(msg, e); + throw new PolicyManagementException("Error occurred while getting the policy related to profile ID (" + + profileId + ")", e); } catch (ProfileManagerDAOException e) { - String msg = "Error occurred while getting the profile related to profile ID (" + profileId + ")"; - log.error(msg, e); - throw new PolicyManagementException(msg, e); + throw new PolicyManagementException("Error occurred while getting the profile related to profile ID (" + + profileId + ")", e); + } catch (SQLException e) { + throw new PolicyManagementException("Error occurred while opening a connection to the data source", e); + } finally { + PolicyManagementDAOFactory.closeConnection(); } return policy; } @@ -510,6 +425,7 @@ public class PolicyManagerImpl implements PolicyManager { List roleNames; try { + PolicyManagementDAOFactory.openConnection(); policy = policyDAO.getPolicy(policyId); deviceList = getPolicyAppliedDevicesIds(policyId); roleNames = policyDAO.getPolicyAppliedRoles(policyId); @@ -524,13 +440,15 @@ public class PolicyManagerImpl implements PolicyManager { policy.setDevices(deviceList); } catch (PolicyManagerDAOException e) { - String msg = "Error occurred while getting the policy related to policy ID (" + policyId + ")"; - log.error(msg, e); - throw new PolicyManagementException(msg, e); + throw new PolicyManagementException("Error occurred while getting the policy related to policy ID (" + + policyId + ")", e); } catch (ProfileManagerDAOException e) { - String msg = "Error occurred while getting the profile related to policy ID (" + policyId + ")"; - log.error(msg, e); - throw new PolicyManagementException(msg, e); + throw new PolicyManagementException("Error occurred while getting the profile related to policy ID (" + + policyId + ")", e); + } catch (SQLException e) { + throw new PolicyManagementException("Error occurred while opening a connection to the data source", e); + } finally { + PolicyManagementDAOFactory.closeConnection(); } return policy; } @@ -541,6 +459,7 @@ public class PolicyManagerImpl implements PolicyManager { List policyList; try { + PolicyManagementDAOFactory.openConnection(); policyList = policyDAO.getAllPolicies(); // List profileList = profileDAO.getAllProfiles(); List profileList = profileManager.getAllProfiles(); @@ -560,15 +479,14 @@ public class PolicyManagerImpl implements PolicyManager { // policyDAO.getLocationsOfPolicy(policy); } Collections.sort(policyList); - } catch (PolicyManagerDAOException e) { - String msg = "Error occurred while getting all the policies."; - log.error(msg, e); - throw new PolicyManagementException(msg, e); + throw new PolicyManagementException("Error occurred while getting all the policies.", e); } catch (ProfileManagementException e) { - String msg = "Error occurred while getting all the profiles."; - log.error(msg, e); - throw new PolicyManagementException(msg, e); + throw new PolicyManagementException("Error occurred while getting all the profiles.", e); + } catch (SQLException e) { + throw new PolicyManagementException("Error occurred while opening a connection to the data source", e); + } finally { + PolicyManagementDAOFactory.closeConnection(); } return policyList; } @@ -577,41 +495,41 @@ public class PolicyManagerImpl implements PolicyManager { public List getPoliciesOfDevice(DeviceIdentifier deviceIdentifier) throws PolicyManagementException { List policyIdList; - List policies = new ArrayList(); + List policies = new ArrayList<>(); try { + PolicyManagementDAOFactory.openConnection(); DeviceManagementProviderService service = new DeviceManagementProviderServiceImpl(); Device device = service.getDevice(deviceIdentifier); policyIdList = policyDAO.getPolicyIdsOfDevice(device); - List tempPolicyList = this.getPolicies(); + } 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(); + } - for (Policy policy : tempPolicyList) { - for (Integer i : policyIdList) { - if (policy.getId() == i) { - policies.add(policy); - } + List tempPolicyList = this.getPolicies(); + + for (Policy policy : tempPolicyList) { + for (Integer i : policyIdList) { + if (policy.getId() == i) { + policies.add(policy); } } - - 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); } + + Collections.sort(policies); return policies; } @Override public List getPoliciesOfDeviceType(String deviceTypeName) throws PolicyManagementException { - - List policies = new ArrayList(); - + List policies = new ArrayList<>(); try { List profileList = profileManager.getProfilesOfDeviceType(deviceTypeName); List allPolicies = this.getPolicies(); @@ -626,9 +544,7 @@ public class PolicyManagerImpl implements PolicyManager { } Collections.sort(policies); } catch (ProfileManagementException e) { - String msg = "Error occurred while getting all the profile features."; - log.error(msg, e); - throw new PolicyManagementException(msg, e); + throw new PolicyManagementException("Error occurred while getting all the profile features.", e); } return policies; } @@ -636,25 +552,29 @@ public class PolicyManagerImpl implements PolicyManager { @Override public List getPoliciesOfRole(String roleName) throws PolicyManagementException { - List policies = new ArrayList(); + List policies = new ArrayList<>(); List policyIdList; try { + PolicyManagementDAOFactory.openConnection(); policyIdList = policyDAO.getPolicyOfRole(roleName); - List tempPolicyList = this.getPolicies(); - for (Policy policy : tempPolicyList) { - for (Integer i : policyIdList) { - if (policy.getId() == i) { - policies.add(policy); - } + } 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 tempPolicyList = this.getPolicies(); + + for (Policy policy : tempPolicyList) { + for (Integer i : policyIdList) { + if (policy.getId() == i) { + policies.add(policy); } } - - } catch (PolicyManagerDAOException e) { - String msg = "Error occurred while getting the policies."; - log.error(msg, e); - throw new PolicyManagementException(msg, e); } Collections.sort(policies); return policies; @@ -663,25 +583,27 @@ public class PolicyManagerImpl implements PolicyManager { @Override public List getPoliciesOfUser(String username) throws PolicyManagementException { - List policies = new ArrayList(); + List policies = new ArrayList<>(); List policyIdList; try { + PolicyManagementDAOFactory.openConnection(); policyIdList = policyDAO.getPolicyOfUser(username); - List tempPolicyList = this.getPolicies(); + } 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 tempPolicyList = this.getPolicies(); - for (Policy policy : tempPolicyList) { - for (Integer i : policyIdList) { - if (policy.getId() == i) { - policies.add(policy); - } + for (Policy policy : tempPolicyList) { + for (Integer i : policyIdList) { + if (policy.getId() == i) { + policies.add(policy); } } - - } catch (PolicyManagerDAOException e) { - String msg = "Error occurred while getting the policies."; - log.error(msg, e); - throw new PolicyManagementException(msg, e); } Collections.sort(policies); return policies; @@ -689,41 +611,41 @@ public class PolicyManagerImpl implements PolicyManager { @Override public List getPolicyAppliedDevicesIds(int policyId) throws PolicyManagementException { - - List deviceList = new ArrayList(); + List deviceList = new ArrayList<>(); List deviceIds; - try { + PolicyManagementDAOFactory.openConnection(); int tenantId = PrivilegedCarbonContext.getThreadLocalCarbonContext().getTenantId(); deviceIds = policyDAO.getPolicyAppliedDevicesIds(policyId); for (int deviceId : deviceIds) { //TODO FIX ME deviceList.add(deviceDAO.getDevice(new DeviceIdentifier(Integer.toString(deviceId), ""), tenantId)); } - } catch (PolicyManagerDAOException e) { - String msg = "Error occurred while getting the device ids related to policy id (" + policyId + ")"; - log.error(msg, e); - throw new PolicyManagementException(msg, e); + throw new PolicyManagementException("Error occurred while getting the device ids related to policy id (" + + policyId + ")", e); } catch (DeviceManagementDAOException e) { - String msg = "Error occurred while getting the devices related to policy id (" + policyId + ")"; - log.error(msg, e); - throw new PolicyManagementException(msg, e); + throw new PolicyManagementException("Error occurred while getting the devices related to policy id (" + + policyId + ")", e); + } catch (SQLException e) { + throw new PolicyManagementException("Error occurred while opening a connection to the data source", e); + } finally { + PolicyManagementDAOFactory.closeConnection(); } return deviceList; } @Override - public void addAppliedPolicyFeaturesToDevice(DeviceIdentifier deviceIdentifier, Policy policy) - throws PolicyManagementException { - + public void addAppliedPolicyFeaturesToDevice(DeviceIdentifier deviceIdentifier, + Policy policy) throws PolicyManagementException { int deviceId = -1; try { DeviceManagementProviderService service = new DeviceManagementProviderServiceImpl(); Device device = service.getDevice(deviceIdentifier); deviceId = device.getId(); - boolean exist = policyDAO.checkPolicyAvailable(deviceId); + PolicyManagementDAOFactory.beginTransaction(); + boolean exist = policyDAO.checkPolicyAvailable(deviceId); if (exist) { policyDAO.updateEffectivePolicyToDevice(deviceId, policy); } else { @@ -731,21 +653,15 @@ public class PolicyManagerImpl implements PolicyManager { } PolicyManagementDAOFactory.commitTransaction(); } catch (PolicyManagerDAOException e) { - try { - PolicyManagementDAOFactory.rollbackTransaction(); - } catch (PolicyManagerDAOException e1) { - log.warn("Error occurred while roll backing the transaction."); - } - String msg = "Error occurred while adding the evaluated policy to device (" + - deviceId + " - " + policy.getId() + ")"; - log.error(msg, e); - throw new PolicyManagementException(msg, e); + PolicyManagementDAOFactory.rollbackTransaction(); + throw new PolicyManagementException("Error occurred while adding the evaluated policy to device (" + + deviceId + " - " + policy.getId() + ")", e); } catch (DeviceManagementException e) { - String msg = "Error occurred while getting the device details (" + deviceIdentifier.getId() + ")"; - log.error(msg, e); - throw new PolicyManagementException(msg, e); + throw new PolicyManagementException("Error occurred while getting the device details (" + + deviceIdentifier.getId() + ")", e); + } finally { + PolicyManagementDAOFactory.closeConnection(); } - } @Override @@ -758,9 +674,10 @@ public class PolicyManagerImpl implements PolicyManager { Device device = service.getDevice(deviceIdentifier); deviceId = device.getId(); // boolean exist = policyDAO.checkPolicyAvailable(deviceId); - Policy policySaved = policyDAO.getAppliedPolicy(deviceId); PolicyManagementDAOFactory.beginTransaction(); + + Policy policySaved = policyDAO.getAppliedPolicy(deviceId); if (policySaved != null && policySaved.getId() != 0) { if (policy.getId() != policySaved.getId()){ policyDAO.updateEffectivePolicyToDevice(deviceId, policy); @@ -770,24 +687,15 @@ public class PolicyManagerImpl implements PolicyManager { } PolicyManagementDAOFactory.commitTransaction(); } catch (PolicyManagerDAOException e) { - try { - PolicyManagementDAOFactory.rollbackTransaction(); - } catch (PolicyManagerDAOException e1) { - log.warn("Error occurred while roll backing the transaction."); - } - String msg = "Error occurred while adding the evaluated policy to device (" + - deviceId + " - " + policy.getId() + ")"; - log.error(msg, e); - throw new PolicyManagementException(msg, e); + PolicyManagementDAOFactory.rollbackTransaction(); + throw new PolicyManagementException("Error occurred while adding the evaluated policy to device (" + + deviceId + " - " + policy.getId() + ")", e); } catch (DeviceManagementException e) { - try { - PolicyManagementDAOFactory.rollbackTransaction(); - } catch (PolicyManagerDAOException e1) { - log.warn("Error occurred while roll backing the transaction."); - } - String msg = "Error occurred while getting the device details (" + deviceIdentifier.getId() + ")"; - log.error(msg, e); - throw new PolicyManagementException(msg, e); + PolicyManagementDAOFactory.rollbackTransaction(); + throw new PolicyManagementException("Error occurred while getting the device details (" + + deviceIdentifier.getId() + ")", e); + } finally { + PolicyManagementDAOFactory.closeConnection(); } } @@ -796,74 +704,82 @@ public class PolicyManagerImpl implements PolicyManager { boolean exist; try { + PolicyManagementDAOFactory.openConnection(); DeviceManagementProviderService service = new DeviceManagementProviderServiceImpl(); Device device = service.getDevice(deviceIdentifier); exist = policyDAO.checkPolicyAvailable(device.getId()); } catch (PolicyManagerDAOException e) { - String msg = "Error occurred while checking whether device has a policy to apply."; - log.error(msg, e); - throw new PolicyManagementException(msg, e); + throw new PolicyManagementException("Error occurred while checking whether device has a policy " + + "to apply.", e); } catch (DeviceManagementException e) { - String msg = "Error occurred while getting the device details (" + deviceIdentifier.getId() + ")"; - log.error(msg, e); - throw new PolicyManagementException(msg, e); + throw new PolicyManagementException("Error occurred while getting the device details (" + + deviceIdentifier.getId() + ")", e); + } catch (SQLException e) { + throw new PolicyManagementException("Error occurred while opening a connection to the data source", e); + } finally { + PolicyManagementDAOFactory.closeConnection(); } return exist; } @Override public boolean setPolicyApplied(DeviceIdentifier deviceIdentifier) throws PolicyManagementException { - try { + PolicyManagementDAOFactory.openConnection(); DeviceManagementProviderService service = new DeviceManagementProviderServiceImpl(); Device device = service.getDevice(deviceIdentifier); policyDAO.setPolicyApplied(device.getId()); return true; } catch (PolicyManagerDAOException e) { - String msg = "Error occurred while setting the policy has applied to device (" + - deviceIdentifier.getId() + ")"; - log.error(msg, e); - throw new PolicyManagementException(msg, e); + throw new PolicyManagementException("Error occurred while setting the policy has applied to device (" + + deviceIdentifier.getId() + ")", e); } catch (DeviceManagementException e) { - String msg = "Error occurred while getting the device details (" + deviceIdentifier.getId() + ")"; - log.error(msg, e); - throw new PolicyManagementException(msg, e); + throw new PolicyManagementException("Error occurred while getting the device details (" + + deviceIdentifier.getId() + ")", e); + } catch (SQLException e) { + throw new PolicyManagementException("Error occurred while opening a connection to the data source", e); + } finally { + PolicyManagementDAOFactory.closeConnection(); } } @Override public int getPolicyCount() throws PolicyManagementException { - int policyCount = 0; + int policyCount; try { + PolicyManagementDAOFactory.openConnection(); policyCount = policyDAO.getPolicyCount(); return policyCount; } catch (PolicyManagerDAOException e) { - String msg = "Error occurred while getting policy count"; - log.error(msg, e); - throw new PolicyManagementException(msg, e); + throw new PolicyManagementException("Error occurred while getting policy count", e); + } catch (SQLException e) { + throw new PolicyManagementException("Error occurred while opening a connection to the data source", e); + } finally { + PolicyManagementDAOFactory.closeConnection(); } } @Override public Policy getAppliedPolicyToDevice(DeviceIdentifier deviceIdentifier) throws PolicyManagementException { - Policy policy; try { + PolicyManagementDAOFactory.openConnection(); DeviceManagementProviderService service = new DeviceManagementProviderServiceImpl(); Device device = service.getDevice(deviceIdentifier); //int policyId = policyDAO.getAppliedPolicyId(device.getId()); policy = policyDAO.getAppliedPolicy(device.getId()); } catch (DeviceManagementException e) { - String msg = "Error occurred while getting device id."; - log.error(msg, e); - throw new PolicyManagementException(msg, e); + throw new PolicyManagementException("Error occurred while getting device id.", e); } catch (PolicyManagerDAOException e) { - String msg = "Error occurred while getting policy id or policy."; - log.error(msg, e); - throw new PolicyManagementException(msg, e); + throw new PolicyManagementException("Error occurred while getting policy id or policy.", e); + } catch (SQLException e) { + throw new PolicyManagementException("Error occurred while opening a connection to the data source", e); + } finally { + PolicyManagementDAOFactory.closeConnection(); } return policy; } + } diff --git a/components/policy-mgt/org.wso2.carbon.policy.mgt.core/src/main/java/org/wso2/carbon/policy/mgt/core/mgt/impl/ProfileManagerImpl.java b/components/policy-mgt/org.wso2.carbon.policy.mgt.core/src/main/java/org/wso2/carbon/policy/mgt/core/mgt/impl/ProfileManagerImpl.java index ac6b484815..2dc77739cd 100644 --- a/components/policy-mgt/org.wso2.carbon.policy.mgt.core/src/main/java/org/wso2/carbon/policy/mgt/core/mgt/impl/ProfileManagerImpl.java +++ b/components/policy-mgt/org.wso2.carbon.policy.mgt.core/src/main/java/org/wso2/carbon/policy/mgt/core/mgt/impl/ProfileManagerImpl.java @@ -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.mgt.ProfileManager; +import java.sql.SQLException; import java.sql.Timestamp; import java.util.ArrayList; import java.util.Calendar; @@ -64,32 +65,19 @@ public class ProfileManagerImpl implements ProfileManager { featureDAO.addProfileFeatures(profile.getProfileFeaturesList(), profile.getProfileId()); PolicyManagementDAOFactory.commitTransaction(); } catch (ProfileManagerDAOException e) { - try { - PolicyManagementDAOFactory.rollbackTransaction(); - } catch (PolicyManagerDAOException e1) { - log.warn("Error occurred while roll backing the transaction."); - } - String msg = "Error occurred while adding the profile (" + profile.getProfileName() + ")"; - log.error(msg, e); - throw new ProfileManagementException(msg, e); + PolicyManagementDAOFactory.rollbackTransaction(); + throw new ProfileManagementException("Error occurred while adding the profile (" + + profile.getProfileName() + ")", e); } catch (FeatureManagerDAOException e) { - try { - PolicyManagementDAOFactory.rollbackTransaction(); - } catch (PolicyManagerDAOException e1) { - log.warn("Error occurred while roll backing the transaction."); - } - String msg = "Error occurred while adding the profile features (" + profile.getProfileName() + ")"; - log.error(msg, e); - throw new ProfileManagementException(msg, e); + PolicyManagementDAOFactory.rollbackTransaction(); + throw new ProfileManagementException("Error occurred while adding the profile features (" + + profile.getProfileName() + ")", e); } catch (PolicyManagerDAOException e) { - try { - PolicyManagementDAOFactory.rollbackTransaction(); - } catch (PolicyManagerDAOException e1) { - log.warn("Error occurred while roll backing the transaction."); - } - String msg = "Error occurred while adding the profile (" + profile.getProfileName() + ") to the database"; - log.error(msg, e); - throw new ProfileManagementException(msg, e); + PolicyManagementDAOFactory.rollbackTransaction(); + throw new ProfileManagementException("Error occurred while adding the profile (" + + profile.getProfileName() + ") to the database", e); + } finally { + PolicyManagementDAOFactory.closeConnection(); } return profile; @@ -107,32 +95,19 @@ public class ProfileManagerImpl implements ProfileManager { featureDAO.updateProfileFeatures(profile.getProfileFeaturesList(), profile.getProfileId()); PolicyManagementDAOFactory.commitTransaction(); } catch (ProfileManagerDAOException e) { - try { - PolicyManagementDAOFactory.rollbackTransaction(); - } catch (PolicyManagerDAOException e1) { - log.warn("Error occurred while roll backing the transaction."); - } - String msg = "Error occurred while updating the profile (" + profile.getProfileName() + ")"; - log.error(msg, e); - throw new ProfileManagementException(msg, e); + PolicyManagementDAOFactory.rollbackTransaction(); + throw new ProfileManagementException("Error occurred while updating the profile (" + + profile.getProfileName() + ")", e); } catch (FeatureManagerDAOException e) { - try { - PolicyManagementDAOFactory.rollbackTransaction(); - } catch (PolicyManagerDAOException e1) { - log.warn("Error occurred while roll backing the transaction."); - } - String msg = "Error occurred while updating the profile features (" + profile.getProfileName() + ")"; - log.error(msg, e); - throw new ProfileManagementException(msg, e); + PolicyManagementDAOFactory.rollbackTransaction(); + throw new ProfileManagementException("Error occurred while updating the profile features (" + + profile.getProfileName() + ")", e); } catch (PolicyManagerDAOException e) { - try { - PolicyManagementDAOFactory.rollbackTransaction(); - } catch (PolicyManagerDAOException e1) { - log.warn("Error occurred while roll backing the transaction."); - } - String msg = "Error occurred while updating the profile (" + profile.getProfileName() + ") to the database"; - log.error(msg, e); - throw new ProfileManagementException(msg, e); + PolicyManagementDAOFactory.rollbackTransaction(); + throw new ProfileManagementException("Error occurred while updating the profile (" + + profile.getProfileName() + ") to the database", e); + } finally { + PolicyManagementDAOFactory.closeConnection(); } return profile; @@ -147,32 +122,19 @@ public class ProfileManagerImpl implements ProfileManager { bool = profileDAO.deleteProfile(profile); PolicyManagementDAOFactory.commitTransaction(); } catch (ProfileManagerDAOException e) { - try { - PolicyManagementDAOFactory.rollbackTransaction(); - } catch (PolicyManagerDAOException e1) { - log.warn("Error occurred while roll backing the transaction."); - } - String msg = "Error occurred while deleting the profile (" + profile.getProfileName() + ")"; - log.error(msg, e); - throw new ProfileManagementException(msg, e); + PolicyManagementDAOFactory.rollbackTransaction(); + throw new ProfileManagementException("Error occurred while deleting the profile (" + + profile.getProfileName() + ")", e); } catch (FeatureManagerDAOException e) { - try { - PolicyManagementDAOFactory.rollbackTransaction(); - } catch (PolicyManagerDAOException e1) { - log.warn("Error occurred while roll backing the transaction."); - } - String msg = "Error occurred while deleting the features from profile (" + profile.getProfileName() + ")"; - log.error(msg, e); - throw new ProfileManagementException(msg, e); + PolicyManagementDAOFactory.rollbackTransaction(); + throw new ProfileManagementException("Error occurred while deleting the features from profile (" + + profile.getProfileName() + ")", e); } catch (PolicyManagerDAOException e) { - try { - PolicyManagementDAOFactory.rollbackTransaction(); - } catch (PolicyManagerDAOException e1) { - log.warn("Error occurred while roll backing the transaction."); - } - String msg = "Error occurred while deleting the profile (" + profile.getProfileName() + ") from database"; - log.error(msg, e); - throw new ProfileManagementException(msg, e); + PolicyManagementDAOFactory.rollbackTransaction(); + throw new ProfileManagementException("Error occurred while deleting the profile (" + + profile.getProfileName() + ") from database", e); + } finally { + PolicyManagementDAOFactory.closeConnection(); } return bool; } @@ -184,6 +146,7 @@ public class ProfileManagerImpl implements ProfileManager { DeviceType deviceType; try { + PolicyManagementDAOFactory.openConnection(); profile = profileDAO.getProfiles(profileId); featureList = featureDAO.getFeaturesForProfile(profileId); deviceType = deviceTypeDAO.getDeviceType(profile.getDeviceType().getId()); @@ -192,17 +155,17 @@ public class ProfileManagerImpl implements ProfileManager { profile.setDeviceType(deviceType); } catch (ProfileManagerDAOException e) { - String msg = "Error occurred while getting profile id (" + profileId + ")"; - log.error(msg, e); - throw new ProfileManagementException(msg, e); + throw new ProfileManagementException("Error occurred while getting profile id (" + profileId + ")", e); } catch (FeatureManagerDAOException e) { - String msg = "Error occurred while getting features related profile id (" + profileId + ")"; - log.error(msg, e); - throw new ProfileManagementException(msg, e); + throw new ProfileManagementException("Error occurred while getting features related profile id (" + + profileId + ")", e); } catch (DeviceManagementDAOException e) { - String msg = "Error occurred while getting device type related profile id (" + profileId + ")"; - log.error(msg, e); - throw new ProfileManagementException(msg, e); + throw new ProfileManagementException("Error occurred while getting device type related profile id (" + + profileId + ")", e); + } catch (SQLException e) { + throw new ProfileManagementException("Error occurred while opening a connection to the data source", e); + } finally { + PolicyManagementDAOFactory.closeConnection(); } return profile; } @@ -211,6 +174,7 @@ public class ProfileManagerImpl implements ProfileManager { public List getAllProfiles() throws ProfileManagementException { List profileList; try { + PolicyManagementDAOFactory.openConnection(); profileList = profileDAO.getAllProfiles(); List featureList = featureDAO.getAllProfileFeatures(); List deviceTypes = deviceTypeDAO.getDeviceTypes(); @@ -231,17 +195,15 @@ public class ProfileManagerImpl implements ProfileManager { } } } catch (ProfileManagerDAOException e) { - String msg = "Error occurred while getting profiles"; - log.error(msg, e); - throw new ProfileManagementException(msg, e); + throw new ProfileManagementException("Error occurred while getting profiles", e); } catch (FeatureManagerDAOException e) { - String msg = "Error occurred while getting features related to profiles"; - log.error(msg, e); - throw new ProfileManagementException(msg, e); + throw new ProfileManagementException("Error occurred while getting features related to profiles", e); } catch (DeviceManagementDAOException e) { - String msg = "Error occurred while getting device types related to profiles"; - log.error(msg, e); - throw new ProfileManagementException(msg, e); + throw new ProfileManagementException("Error occurred while getting device types related to profiles", e); + } catch (SQLException e) { + throw new ProfileManagementException("Error occurred while opening a connection to the data source", e); + } finally { + PolicyManagementDAOFactory.closeConnection(); } return profileList; } @@ -251,6 +213,7 @@ public class ProfileManagerImpl implements ProfileManager { List profileList; List featureList; try { + PolicyManagementDAOFactory.openConnection(); DeviceType deviceType = deviceTypeDAO.getDeviceType(deviceTypeName); profileList = profileDAO.getProfilesOfDeviceType(deviceType); featureList = featureDAO.getAllProfileFeatures(); @@ -266,18 +229,17 @@ public class ProfileManagerImpl implements ProfileManager { } } catch (ProfileManagerDAOException e) { - String msg = "Error occurred while getting profiles"; - log.error(msg, e); - throw new ProfileManagementException(msg, e); + throw new ProfileManagementException("Error occurred while getting profiles", e); } catch (DeviceManagementDAOException e) { - String msg = "Error occurred while getting device types"; - log.error(msg, e); - throw new ProfileManagementException(msg, e); + throw new ProfileManagementException("Error occurred while getting device types", e); } catch (FeatureManagerDAOException e) { - String msg = "Error occurred while getting profile features types"; - log.error(msg, e); - throw new ProfileManagementException(msg, e); + throw new ProfileManagementException("Error occurred while getting profile features types", e); + } catch (SQLException e) { + throw new ProfileManagementException("Error occurred while opening a connection to the data source", e); + } finally { + PolicyManagementDAOFactory.closeConnection(); } return profileList; } + }