revert-70aa11f8
harshanl 9 years ago
commit 423c8e2727

@ -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,47 +311,56 @@ 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()));
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);
}
return devices;
}
@Override
public List<Device> getAllDevices(String type) throws DeviceManagementException {
public List<Device> getAllDevices(String deviceType) throws DeviceManagementException {
List<Device> devices = new ArrayList<>();
List<Device> 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()));
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);
}
return devices;
@ -313,7 +371,6 @@ public class DeviceManagementProviderServiceImpl implements DeviceManagementProv
throws DeviceManagementException {
List<NotificationMessages> 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> notificationMessages = DeviceConfigurationManager.getInstance()
.getNotificationMessagesConfig().getNotificationMessagesList();
List<NotificationMessages> 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);
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<? extends Operation> 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<Device> getAllDevicesOfRole(String role) throws DeviceManagementException {
List<Device> 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();
}
}

@ -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() {
}
}

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

@ -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());
}
}

@ -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<ProfileFeature> 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<Feature> getAllFeatures(String deviceType) throws FeatureManagerDAOException {
Connection conn;
PreparedStatement stmt = null;
ResultSet resultSet = null;
List<Feature> featureList = new ArrayList<Feature>();
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<ProfileFeature> 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.");
}
}
}

@ -40,7 +40,6 @@ public class MonitoringDAOImpl implements MonitoringDAO {
@Override
public int addComplianceDetails(int deviceId, int policyId) throws MonitoringDAOException {
Connection conn;
PreparedStatement stmt = null;
ResultSet generatedKeys = null;
@ -65,11 +64,8 @@ public class MonitoringDAOImpl implements MonitoringDAO {
} else {
return 0;
}
} catch (SQLException e) {
String msg = "Error occurred while adding the none compliance to the database.";
log.error(msg, e);
throw new MonitoringDAOException(msg, e);
throw new MonitoringDAOException("Error occurred while adding the none compliance to the database.", e);
} finally {
PolicyManagementDAOUtil.cleanupResources(stmt, generatedKeys);
}
@ -78,7 +74,6 @@ public class MonitoringDAOImpl implements MonitoringDAO {
@Override
public void addComplianceDetails(Map<Integer, Integer> devicePolicyMap) throws MonitoringDAOException {
Connection conn;
PreparedStatement stmt = null;
ResultSet generatedKeys = null;
@ -91,7 +86,6 @@ public class MonitoringDAOImpl implements MonitoringDAO {
log.debug(map.getKey() + " -- " + map.getValue());
}
}
try {
conn = this.getConnection();
String query = "INSERT INTO DM_POLICY_COMPLIANCE_STATUS (DEVICE_ID, POLICY_ID, STATUS, ATTEMPTS, " +
@ -107,30 +101,22 @@ public class MonitoringDAOImpl implements MonitoringDAO {
stmt.addBatch();
}
stmt.executeBatch();
} catch (SQLException e) {
String msg = "Error occurred while adding the none compliance to the database.";
log.error(msg, e);
throw new MonitoringDAOException(msg, e);
throw new MonitoringDAOException("Error occurred while adding the none compliance to the database.", e);
} finally {
PolicyManagementDAOUtil.cleanupResources(stmt, generatedKeys);
}
}
@Override
public void setDeviceAsNoneCompliance(int deviceId, int policyId) throws
MonitoringDAOException {
public void setDeviceAsNoneCompliance(int deviceId, int policyId) throws MonitoringDAOException {
Connection conn;
PreparedStatement stmt = null;
ResultSet generatedKeys = null;
Timestamp currentTimestamp = new Timestamp(Calendar.getInstance().getTime().getTime());
int tenantId = PrivilegedCarbonContext.getThreadLocalCarbonContext().getTenantId();
try {
conn = this.getConnection();
String query = "UPDATE DM_POLICY_COMPLIANCE_STATUS SET STATUS = 0, LAST_FAILED_TIME = ?, POLICY_ID = ?," +
" ATTEMPTS=0 WHERE DEVICE_ID = ? AND TENANT_ID = ?";
stmt = conn.prepareStatement(query);
@ -141,9 +127,7 @@ public class MonitoringDAOImpl implements MonitoringDAO {
stmt.executeUpdate();
} catch (SQLException e) {
String msg = "Error occurred while updating the none compliance to the database.";
log.error(msg, e);
throw new MonitoringDAOException(msg, e);
throw new MonitoringDAOException("Error occurred while updating the none compliance to the database.", e);
} finally {
PolicyManagementDAOUtil.cleanupResources(stmt, generatedKeys);
}
@ -152,13 +136,11 @@ public class MonitoringDAOImpl implements MonitoringDAO {
@Override
public void setDeviceAsCompliance(int deviceId, int policyId) throws MonitoringDAOException {
Connection conn;
PreparedStatement stmt = null;
ResultSet generatedKeys = null;
Timestamp currentTimestamp = new Timestamp(Calendar.getInstance().getTime().getTime());
int tenantId = PrivilegedCarbonContext.getThreadLocalCarbonContext().getTenantId();
try {
conn = this.getConnection();
String query = "UPDATE DM_POLICY_COMPLIANCE_STATUS SET STATUS = ?, ATTEMPTS=0, LAST_SUCCESS_TIME = ?" +
@ -178,9 +160,7 @@ public class MonitoringDAOImpl implements MonitoringDAO {
// }
} catch (SQLException e) {
String msg = "Error occurred while deleting the none compliance to the database.";
log.error(msg, e);
throw new MonitoringDAOException(msg, e);
throw new MonitoringDAOException("Error occurred while deleting the none compliance to the database.", e);
} finally {
PolicyManagementDAOUtil.cleanupResources(stmt, generatedKeys);
}
@ -189,21 +169,18 @@ public class MonitoringDAOImpl implements MonitoringDAO {
@Override
public void addNoneComplianceFeatures(int policyComplianceStatusId, int deviceId, List<ComplianceFeature>
complianceFeatures) throws MonitoringDAOException {
Connection conn;
PreparedStatement stmt = null;
int tenantId = PrivilegedCarbonContext.getThreadLocalCarbonContext().getTenantId();
try {
conn = this.getConnection();
String query = "INSERT INTO DM_POLICY_COMPLIANCE_FEATURES (COMPLIANCE_STATUS_ID, FEATURE_CODE, STATUS, " +
"TENANT_ID) VALUES (?, ?, ?, ?) ";
stmt = conn.prepareStatement(query, PreparedStatement.RETURN_GENERATED_KEYS);
for (ComplianceFeature feature : complianceFeatures) {
stmt.setInt(1, policyComplianceStatusId);
stmt.setString(2, feature.getFeatureCode());
if (feature.isCompliance() == true) {
if (feature.isCompliance()) {
stmt.setInt(3, 1);
} else {
stmt.setInt(3, 0);
@ -212,11 +189,9 @@ public class MonitoringDAOImpl implements MonitoringDAO {
stmt.addBatch();
}
stmt.executeBatch();
} catch (SQLException e) {
String msg = "Error occurred while adding the none compliance features to the database.";
log.error(msg, e);
throw new MonitoringDAOException(msg, e);
throw new MonitoringDAOException("Error occurred while adding the none compliance features to the " +
"database.", e);
} finally {
PolicyManagementDAOUtil.cleanupResources(stmt, null);
}
@ -253,24 +228,19 @@ public class MonitoringDAOImpl implements MonitoringDAO {
return complianceData;
} catch (SQLException e) {
String msg = "Unable to retrieve compliance data from database.";
log.error(msg, e);
throw new MonitoringDAOException(msg, e);
throw new MonitoringDAOException("Unable to retrieve compliance data from database.", e);
} finally {
PolicyManagementDAOUtil.cleanupResources(stmt, resultSet);
this.closeConnection();
}
}
@Override
public List<ComplianceData> getCompliance(List<Integer> deviceIds) throws MonitoringDAOException {
Connection conn;
PreparedStatement stmt = null;
ResultSet resultSet = null;
List<ComplianceData> complianceDataList = new ArrayList<>();
int tenantId = PrivilegedCarbonContext.getThreadLocalCarbonContext().getTenantId();
try {
conn = this.getConnection();
String query = "SELECT * FROM DM_POLICY_COMPLIANCE_STATUS WHERE TENANT_ID = ? AND DEVICE_ID IN (?)";
@ -279,11 +249,8 @@ public class MonitoringDAOImpl implements MonitoringDAO {
stmt.setString(2, PolicyManagerUtil.makeString(deviceIds));
resultSet = stmt.executeQuery();
while (resultSet.next()) {
ComplianceData complianceData = new ComplianceData();
complianceData.setId(resultSet.getInt("ID"));
complianceData.setDeviceId(resultSet.getInt("DEVICE_ID"));
complianceData.setPolicyId(resultSet.getInt("POLICY_ID"));
@ -296,21 +263,16 @@ public class MonitoringDAOImpl implements MonitoringDAO {
complianceDataList.add(complianceData);
}
return complianceDataList;
} catch (SQLException e) {
String msg = "Unable to retrieve compliance data from database.";
log.error(msg, e);
throw new MonitoringDAOException(msg, e);
throw new MonitoringDAOException("Unable to retrieve compliance data from database.", e);
} finally {
PolicyManagementDAOUtil.cleanupResources(stmt, resultSet);
this.closeConnection();
}
}
@Override
public List<ComplianceFeature> getNoneComplianceFeatures(int policyComplianceStatusId) throws
MonitoringDAOException {
Connection conn;
PreparedStatement stmt = null;
ResultSet resultSet = null;
@ -333,25 +295,18 @@ public class MonitoringDAOImpl implements MonitoringDAO {
complianceFeatures.add(feature);
}
return complianceFeatures;
} catch (SQLException e) {
String msg = "Unable to retrieve compliance features data from database.";
log.error(msg, e);
throw new MonitoringDAOException(msg, e);
throw new MonitoringDAOException("Unable to retrieve compliance features data from database.", e);
} finally {
PolicyManagementDAOUtil.cleanupResources(stmt, resultSet);
this.closeConnection();
}
}
@Override
public void deleteNoneComplianceData(int policyComplianceStatusId) throws MonitoringDAOException {
Connection conn;
PreparedStatement stmt = null;
int tenantId = PrivilegedCarbonContext.getThreadLocalCarbonContext().getTenantId();
try {
conn = this.getConnection();
String query = "DELETE FROM DM_POLICY_COMPLIANCE_FEATURES WHERE COMPLIANCE_STATUS_ID = ? AND TENANT_ID = ?";
@ -359,11 +314,8 @@ public class MonitoringDAOImpl implements MonitoringDAO {
stmt.setInt(1, policyComplianceStatusId);
stmt.setInt(2, tenantId);
stmt.executeUpdate();
} catch (SQLException e) {
String msg = "Unable to delete compliance data from database.";
log.error(msg, e);
throw new MonitoringDAOException(msg, e);
throw new MonitoringDAOException("Unable to delete compliance data from database.", e);
} finally {
PolicyManagementDAOUtil.cleanupResources(stmt, null);
}
@ -372,14 +324,11 @@ public class MonitoringDAOImpl implements MonitoringDAO {
@Override
public void updateAttempts(int deviceId, boolean reset) throws MonitoringDAOException {
Connection conn;
PreparedStatement stmt = null;
Timestamp currentTimestamp = new Timestamp(Calendar.getInstance().getTime().getTime());
int tenantId = PrivilegedCarbonContext.getThreadLocalCarbonContext().getTenantId();
try {
conn = this.getConnection();
String query = "";
if (reset) {
@ -394,27 +343,20 @@ public class MonitoringDAOImpl implements MonitoringDAO {
stmt.setInt(2, deviceId);
stmt.setInt(3, tenantId);
stmt.executeUpdate();
} catch (SQLException e) {
String msg = "Unable to update the attempts data in database.";
log.error(msg, e);
throw new MonitoringDAOException(msg, e);
throw new MonitoringDAOException("Unable to update the attempts data in database.", e);
} finally {
PolicyManagementDAOUtil.cleanupResources(stmt, null);
}
}
@Override
public void updateAttempts(List<Integer> deviceIds, boolean reset) throws MonitoringDAOException {
Connection conn;
PreparedStatement stmt = null;
Timestamp currentTimestamp = new Timestamp(Calendar.getInstance().getTime().getTime());
int tenantId = PrivilegedCarbonContext.getThreadLocalCarbonContext().getTenantId();
try {
conn = this.getConnection();
String query = "";
if (reset) {
@ -432,17 +374,13 @@ public class MonitoringDAOImpl implements MonitoringDAO {
stmt.addBatch();
}
stmt.executeBatch();
} catch (SQLException e) {
String msg = "Unable to update the attempts data in database.";
log.error(msg, e);
throw new MonitoringDAOException(msg, e);
throw new MonitoringDAOException("Unable to update the attempts data in database.", e);
} finally {
PolicyManagementDAOUtil.cleanupResources(stmt, null);
}
}
private Connection getConnection() throws MonitoringDAOException {
try {
return PolicyManagementDAOFactory.getConnection();
@ -452,14 +390,4 @@ public class MonitoringDAOImpl implements MonitoringDAO {
}
}
private void closeConnection() {
try {
PolicyManagementDAOFactory.closeConnection();
} catch (PolicyManagerDAOException e) {
log.warn("Unable to close the database connection.");
}
}
}

@ -51,10 +51,8 @@ public class PolicyDAOImpl implements PolicyDAO {
@Override
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<String> 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<String> 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<Device> 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<Policy> 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<Criterion> getAllPolicyCriteria() throws PolicyManagerDAOException {
Connection conn;
PreparedStatement stmt = null;
ResultSet resultSet = null;
List<Criterion> criteria = new ArrayList<Criterion>();
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<PolicyCriterion> 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<PolicyCriterion> getPolicyCriteria(int policyId) throws PolicyManagerDAOException {
Connection conn;
PreparedStatement stmt = null;
ResultSet resultSet = null;
List<PolicyCriterion> criteria = new ArrayList<PolicyCriterion>();
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<Policy> 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<Integer> getPolicyAppliedDevicesIds(int policyId) throws PolicyManagerDAOException {
Connection conn;
PreparedStatement stmt = null;
ResultSet resultSet = null;
List<Integer> deviceIdList = new ArrayList<Integer>();
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<String> getPolicyAppliedRoles(int policyId) throws PolicyManagerDAOException {
Connection conn;
PreparedStatement stmt = null;
ResultSet resultSet = null;
List<String> roleNames = new ArrayList<String>();
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<Integer> getPolicyIdsOfDevice(Device device) throws PolicyManagerDAOException {
Connection conn;
PreparedStatement stmt = null;
ResultSet resultSet = null;
List<Integer> policyIds = new ArrayList<Integer>();
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<Integer> getPolicyOfRole(String roleName) throws PolicyManagerDAOException {
Connection conn;
PreparedStatement stmt = null;
ResultSet resultSet = null;
List<Integer> policyIds = new ArrayList<Integer>();
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<Integer> getPolicyOfUser(String username) throws PolicyManagerDAOException {
Connection conn;
PreparedStatement stmt = null;
ResultSet resultSet = null;
List<Integer> policyIds = new ArrayList<Integer>();
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<Integer, Integer> getAppliedPolicyIds(List<Integer> deviceIds) throws PolicyManagerDAOException {
Connection conn;
PreparedStatement stmt = null;
ResultSet resultSet = null;
HashMap<Integer, Integer> 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;
}

@ -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.");
}
}
}

@ -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);
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);
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 {
} catch (FeatureManagerDAOException | PolicyManagerDAOException e) {
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);
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);
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);
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);
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);
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<Feature> 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<ProfileFeature> 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);
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);
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);
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);
throw new FeatureManagementException("Error occurred while deleting the feature of - profile (" +
profile.getProfileName() + ") from database", e);
} finally {
PolicyManagementDAOFactory.closeConnection();
}
return bool;
}

@ -52,6 +52,7 @@ import org.wso2.carbon.policy.mgt.core.mgt.MonitoringManager;
import org.wso2.carbon.policy.mgt.core.mgt.PolicyManager;
import org.wso2.carbon.policy.mgt.core.util.PolicyManagerUtil;
import java.sql.SQLException;
import java.util.*;
public class MonitoringManagerImpl implements MonitoringManager {
@ -77,8 +78,6 @@ public class MonitoringManagerImpl implements MonitoringManager {
List<ComplianceFeature> complianceFeatures = new ArrayList<>();
try {
DeviceManagementProviderService service = new DeviceManagementProviderServiceImpl();
PolicyManager manager = new PolicyManagerImpl();
Device device = service.getDevice(deviceIdentifier);
@ -91,26 +90,34 @@ public class MonitoringManagerImpl implements MonitoringManager {
ComplianceData complianceData;
// This was retrieved from database because compliance id must be present for other dao operations to
// run.
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.
if (!complianceFeatures.isEmpty()) {
if (complianceFeatures != null && !complianceFeatures.isEmpty()) {
PolicyManagementDAOFactory.beginTransaction();
monitoringDAO.setDeviceAsNoneCompliance(device.getId(), policy.getId());
if (log.isDebugEnabled()) {
log.debug("Compliance status primary key " + complianceData.getId());
}
// complianceData.setId(cmf.getId());
monitoringDAO.addNoneComplianceFeatures(complianceData.getId(), device.getId(), complianceFeatures);
PolicyManagementDAOFactory.commitTransaction();
complianceDecisionPoint.validateDevicePolicyCompliance(deviceIdentifier, complianceData);
List<ProfileFeature> profileFeatures = policy.getProfile().getProfileFeaturesList();
for (ComplianceFeature compFeature : complianceFeatures) {
@ -120,88 +127,58 @@ public class MonitoringManagerImpl implements MonitoringManager {
}
}
}
} else {
PolicyManagementDAOFactory.beginTransaction();
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 {
throw new PolicyComplianceException("Unable tor retrieve device data from DB for " +
deviceIdentifier.getId() + " - " + deviceIdentifier.getType(), e);
} catch (PolicyManagerDAOException | PolicyManagementException e) {
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);
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);
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 +189,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 +198,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<Device> devices) throws PolicyComplianceException {
try {
ComplianceDecisionPoint decisionPoint = new ComplianceDecisionPointImpl();
//int tenantId = PolicyManagerUtil.getTenantId();
@ -294,7 +269,6 @@ public class MonitoringManagerImpl implements MonitoringManager {
}
}
PolicyManagementDAOFactory.beginTransaction();
if (!deviceIdsToAddOperation.isEmpty()) {
@ -310,39 +284,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);
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);
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);
throw new PolicyComplianceException("Error occurred reading the applied policies to devices.", e);
} finally {
PolicyManagementDAOFactory.closeConnection();
}
}
private void addMonitoringOperationsToDatabase(List<Device> devices)
throws PolicyComplianceException, OperationManagementException {
@ -359,7 +314,6 @@ public class MonitoringManagerImpl implements MonitoringManager {
}
private List<DeviceIdentifier> getDeviceIdentifiersFromDevices(List<Device> devices) {
List<DeviceIdentifier> deviceIdentifiers = new ArrayList<>();
for (Device device : devices) {
DeviceIdentifier identifier = new DeviceIdentifier();

@ -20,7 +20,6 @@ package org.wso2.carbon.policy.mgt.core.mgt.impl;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.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);
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);
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);
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);
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);
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);
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);
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);
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);
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);
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);
throw new PolicyManagementException("Error occurred while deleting the profile features for policy ("
+ policyId + ")", e);
} finally {
PolicyManagementDAOFactory.closeConnection();
}
}
@Override
public Policy addPolicyToDevice(List<DeviceIdentifier> deviceIdentifierList, Policy policy)
throws PolicyManagementException {
public Policy addPolicyToDevice(List<DeviceIdentifier> deviceIdentifierList,
Policy policy) throws PolicyManagementException {
try {
PolicyManagementDAOFactory.beginTransaction();
if (policy.getId() == 0) {
policyDAO.addPolicy(policy);
}
List<Device> deviceList = new ArrayList<Device>();
List<Device> 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);
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.");
throw new PolicyManagementException("Error occurred while adding the policy to device list", e);
} finally {
PolicyManagementDAOFactory.closeConnection();
}
String msg = "Error occurred while adding the policy to device list";
log.error(msg, e);
throw new PolicyManagementException(msg, e);
}
return policy;
}
@Override
public Policy addPolicyToRole(List<String> 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);
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);
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<String> 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<String> roleNames;
try {
PolicyManagementDAOFactory.openConnection();
policy = policyDAO.getPolicy(policyId);
deviceList = getPolicyAppliedDevicesIds(policyId);
roleNames = policyDAO.getPolicyAppliedRoles(policyId);
@ -524,26 +440,32 @@ 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;
}
@Override
public List<Policy> getPolicies() throws PolicyManagementException {
List<Policy> policyList;
List<Profile> profileList;
try {
profileList = profileManager.getAllProfiles();
} catch (ProfileManagementException e) {
throw new PolicyManagementException("Error occurred while getting all the profiles.", e);
}
try {
PolicyManagementDAOFactory.openConnection();
policyList = policyDAO.getAllPolicies();
// List<Profile> profileList = profileDAO.getAllProfiles();
List<Profile> profileList = profileManager.getAllProfiles();
for (Policy policy : policyList) {
for (Profile profile : profileList) {
@ -560,15 +482,12 @@ 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);
} 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 policies.", e);
} catch (SQLException e) {
throw new PolicyManagementException("Error occurred while opening a connection to the data source", e);
} finally {
PolicyManagementDAOFactory.closeConnection();
}
return policyList;
}
@ -577,11 +496,24 @@ public class PolicyManagerImpl implements PolicyManager {
public List<Policy> getPoliciesOfDevice(DeviceIdentifier deviceIdentifier) throws PolicyManagementException {
List<Integer> policyIdList;
List<Policy> policies = new ArrayList<Policy>();
List<Policy> policies = new ArrayList<>();
try {
PolicyManagementDAOFactory.openConnection();
DeviceManagementProviderService service = new DeviceManagementProviderServiceImpl();
Device device = service.getDevice(deviceIdentifier);
policyIdList = policyDAO.getPolicyIdsOfDevice(device);
} catch (PolicyManagerDAOException e) {
throw new PolicyManagementException("Error occurred while getting the policies for device identifier (" +
deviceIdentifier.getId() + " - " + deviceIdentifier.getType() + ")", e);
} catch (DeviceManagementException e) {
throw new PolicyManagementException("Error occurred while getting device related to device identifier (" +
deviceIdentifier.getId() + " - " + deviceIdentifier.getType() + ")", e);
} catch (SQLException e) {
throw new PolicyManagementException("Error occurred while open a data source connection", e);
} finally {
PolicyManagementDAOFactory.closeConnection();
}
List<Policy> tempPolicyList = this.getPolicies();
for (Policy policy : tempPolicyList) {
@ -593,25 +525,12 @@ public class PolicyManagerImpl implements PolicyManager {
}
Collections.sort(policies);
} catch (PolicyManagerDAOException e) {
String msg = "Error occurred while getting the policies for device identifier (" +
deviceIdentifier.getId() + " - " + deviceIdentifier.getType() + ")";
log.error(msg, e);
throw new PolicyManagementException(msg, e);
} catch (DeviceManagementException e) {
String msg = "Error occurred while getting device related to device identifier (" +
deviceIdentifier.getId() + " - " + deviceIdentifier.getType() + ")";
log.error(msg, e);
throw new PolicyManagementException(msg, e);
}
return policies;
}
@Override
public List<Policy> getPoliciesOfDeviceType(String deviceTypeName) throws PolicyManagementException {
List<Policy> policies = new ArrayList<Policy>();
List<Policy> policies = new ArrayList<>();
try {
List<Profile> profileList = profileManager.getProfilesOfDeviceType(deviceTypeName);
List<Policy> allPolicies = this.getPolicies();
@ -626,9 +545,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,11 +553,21 @@ public class PolicyManagerImpl implements PolicyManager {
@Override
public List<Policy> getPoliciesOfRole(String roleName) throws PolicyManagementException {
List<Policy> policies = new ArrayList<Policy>();
List<Policy> policies = new ArrayList<>();
List<Integer> policyIdList;
try {
PolicyManagementDAOFactory.openConnection();
policyIdList = policyDAO.getPolicyOfRole(roleName);
} catch (PolicyManagerDAOException e) {
throw new PolicyManagementException("Error occurred while getting the policies.", e);
} catch (SQLException e) {
throw new PolicyManagementException("Error occurred while open a data source connection", e);
} finally {
PolicyManagementDAOFactory.closeConnection();
}
List<Policy> tempPolicyList = this.getPolicies();
for (Policy policy : tempPolicyList) {
@ -650,12 +577,6 @@ public class PolicyManagerImpl implements PolicyManager {
}
}
}
} catch (PolicyManagerDAOException e) {
String msg = "Error occurred while getting the policies.";
log.error(msg, e);
throw new PolicyManagementException(msg, e);
}
Collections.sort(policies);
return policies;
}
@ -663,11 +584,19 @@ public class PolicyManagerImpl implements PolicyManager {
@Override
public List<Policy> getPoliciesOfUser(String username) throws PolicyManagementException {
List<Policy> policies = new ArrayList<Policy>();
List<Policy> policies = new ArrayList<>();
List<Integer> policyIdList;
try {
PolicyManagementDAOFactory.openConnection();
policyIdList = policyDAO.getPolicyOfUser(username);
} catch (PolicyManagerDAOException e) {
throw new PolicyManagementException("Error occurred while getting the policies.", e);
} catch (SQLException e) {
throw new PolicyManagementException("Error occurred while open a data source connection", e);
} finally {
PolicyManagementDAOFactory.closeConnection();
}
List<Policy> tempPolicyList = this.getPolicies();
for (Policy policy : tempPolicyList) {
@ -677,53 +606,47 @@ public class PolicyManagerImpl implements PolicyManager {
}
}
}
} catch (PolicyManagerDAOException e) {
String msg = "Error occurred while getting the policies.";
log.error(msg, e);
throw new PolicyManagementException(msg, e);
}
Collections.sort(policies);
return policies;
}
@Override
public List<Device> getPolicyAppliedDevicesIds(int policyId) throws PolicyManagementException {
List<Device> deviceList = new ArrayList<Device>();
List<Device> deviceList = new ArrayList<>();
List<Integer> 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 +654,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);
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 +675,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 +688,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);
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);
throw new PolicyManagementException("Error occurred while getting the device details (" +
deviceIdentifier.getId() + ")", e);
} finally {
PolicyManagementDAOFactory.closeConnection();
}
}
@ -796,74 +705,81 @@ 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 {
DeviceManagementProviderService service = new DeviceManagementProviderServiceImpl();
Device device = service.getDevice(deviceIdentifier);
//int policyId = policyDAO.getAppliedPolicyId(device.getId());
PolicyManagementDAOFactory.openConnection();
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;
}
}

@ -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);
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);
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);
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);
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);
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);
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);
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);
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);
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<Profile> getAllProfiles() throws ProfileManagementException {
List<Profile> profileList;
try {
PolicyManagementDAOFactory.openConnection();
profileList = profileDAO.getAllProfiles();
List<ProfileFeature> featureList = featureDAO.getAllProfileFeatures();
List<DeviceType> 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<Profile> profileList;
List<ProfileFeature> 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;
}
}

Loading…
Cancel
Save