From 892b9681f53881024b56a4f3590aaba188a80917 Mon Sep 17 00:00:00 2001 From: prabathabey Date: Tue, 1 Sep 2015 12:51:18 +0530 Subject: [PATCH 1/2] Fixing transaction handling issues --- .../IllegalTransactionStateException.java | 59 ++++++++++++ ...ApplicationManagerProviderServiceImpl.java | 17 +++- .../core/dao/DeviceManagementDAOFactory.java | 80 +++++++++------- .../device/mgt/core/dao/EnrolmentDAO.java | 2 +- .../dao/impl/ApplicationMappingDAOImpl.java | 6 +- .../mgt/core/dao/impl/DeviceDAOImpl.java | 1 + .../dao/OperationManagementDAOFactory.java | 5 +- .../DeviceManagementProviderServiceImpl.java | 82 ++++++++++------ ...licationManagementProviderServiceTest.java | 42 +++++---- .../core/common/BaseDeviceManagementTest.java | 57 +++--------- .../mgt/core/dao/DevicePersistTests.java | 31 +++++-- .../src/test/resources/testng.xml | 2 +- .../core/dao/PolicyManagementDAOFactory.java | 93 ++++++++++--------- .../mgt/core/dao/impl/FeatureDAOImpl.java | 8 +- .../mgt/core/dao/impl/MonitoringDAOImpl.java | 7 +- .../mgt/core/dao/impl/PolicyDAOImpl.java | 2 +- .../mgt/core/dao/impl/ProfileDAOImpl.java | 15 +-- .../core/mgt/impl/MonitoringManagerImpl.java | 18 ++-- .../mgt/core/mgt/impl/PolicyManagerImpl.java | 46 +++++---- .../mgt/core/mgt/impl/ProfileManagerImpl.java | 50 +++++++--- .../policy/mgt/core/MonitoringTestCase.java | 6 +- .../policy/mgt/core/PolicyDAOTestCase.java | 63 ++++++++++--- 22 files changed, 425 insertions(+), 267 deletions(-) create mode 100644 components/device-mgt/org.wso2.carbon.device.mgt.common/src/main/java/org/wso2/carbon/device/mgt/common/IllegalTransactionStateException.java diff --git a/components/device-mgt/org.wso2.carbon.device.mgt.common/src/main/java/org/wso2/carbon/device/mgt/common/IllegalTransactionStateException.java b/components/device-mgt/org.wso2.carbon.device.mgt.common/src/main/java/org/wso2/carbon/device/mgt/common/IllegalTransactionStateException.java new file mode 100644 index 0000000000..049d7e166c --- /dev/null +++ b/components/device-mgt/org.wso2.carbon.device.mgt.common/src/main/java/org/wso2/carbon/device/mgt/common/IllegalTransactionStateException.java @@ -0,0 +1,59 @@ +/* + * Copyright (c) 2015, WSO2 Inc. (http://www.wso2.org) All Rights Reserved. + * + * WSO2 Inc. licenses this file to you under the Apache License, + * Version 2.0 (the "License"); you may not use this file except + * in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, + * software distributed under the License is distributed on an + * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY + * KIND, either express or implied. See the License for the + * specific language governing permissions and limitations + * under the License. + * + */ +package org.wso2.carbon.device.mgt.common; + +public class IllegalTransactionStateException extends RuntimeException { + + private static final long serialVersionUID = -3151279331929070297L; + + private String errorMessage; + + public String getErrorMessage() { + return errorMessage; + } + + public void setErrorMessage(String errorMessage) { + this.errorMessage = errorMessage; + } + + public IllegalTransactionStateException(String msg, Exception nestedEx) { + super(msg, nestedEx); + setErrorMessage(msg); + } + + public IllegalTransactionStateException(String message, Throwable cause) { + super(message, cause); + setErrorMessage(message); + } + + public IllegalTransactionStateException(String msg) { + super(msg); + setErrorMessage(msg); + } + + public IllegalTransactionStateException() { + super(); + } + + public IllegalTransactionStateException(Throwable cause) { + super(cause); + } + + +} diff --git a/components/device-mgt/org.wso2.carbon.device.mgt.core/src/main/java/org/wso2/carbon/device/mgt/core/app/mgt/ApplicationManagerProviderServiceImpl.java b/components/device-mgt/org.wso2.carbon.device.mgt.core/src/main/java/org/wso2/carbon/device/mgt/core/app/mgt/ApplicationManagerProviderServiceImpl.java index 78e81dbe1a..ea0ce55365 100644 --- a/components/device-mgt/org.wso2.carbon.device.mgt.core/src/main/java/org/wso2/carbon/device/mgt/core/app/mgt/ApplicationManagerProviderServiceImpl.java +++ b/components/device-mgt/org.wso2.carbon.device.mgt.core/src/main/java/org/wso2/carbon/device/mgt/core/app/mgt/ApplicationManagerProviderServiceImpl.java @@ -45,6 +45,7 @@ import org.wso2.carbon.identity.oauth.stub.OAuthAdminServiceStub; import org.wso2.carbon.identity.oauth.stub.dto.OAuthConsumerAppDTO; import java.rmi.RemoteException; +import java.sql.SQLException; import java.util.ArrayList; import java.util.List; @@ -173,6 +174,7 @@ public class ApplicationManagerProviderServiceImpl implements ApplicationManagem @Override public void updateApplicationListInstalledInDevice( DeviceIdentifier deviceIdentifier, List applications) throws ApplicationManagementException { + List installedAppList = getApplicationListForDevice(deviceIdentifier); try { int tenantId = CarbonContext.getThreadLocalCarbonContext().getTenantId(); DeviceManagementDAOFactory.beginTransaction(); @@ -182,8 +184,6 @@ public class ApplicationManagerProviderServiceImpl implements ApplicationManagem log.debug("Device:" + device.getId() + ":identifier:" + deviceIdentifier.getId()); } - List installedAppList = getApplicationListForDevice(deviceIdentifier); - if (log.isDebugEnabled()) { log.debug("num of apps installed:" + installedAppList.size()); } @@ -227,9 +227,13 @@ public class ApplicationManagerProviderServiceImpl implements ApplicationManagem } applicationMappingDAO.removeApplicationMapping(device.getId(), appIdsToRemove, tenantId); DeviceManagementDAOFactory.commitTransaction(); - } catch (DeviceManagementDAOException | TransactionManagementException e) { + } catch (DeviceManagementDAOException e) { DeviceManagementDAOFactory.rollbackTransaction(); throw new ApplicationManagementException("Error occurred saving application list to the device", e); + } catch (TransactionManagementException e) { + throw new ApplicationManagementException("Error occurred while initializing transaction", e); + } finally { + DeviceManagementDAOFactory.closeConnection(); } } @@ -239,11 +243,16 @@ public class ApplicationManagerProviderServiceImpl implements ApplicationManagem Device device; try { int tenantId = CarbonContext.getThreadLocalCarbonContext().getTenantId(); + DeviceManagementDAOFactory.openConnection(); device = deviceDAO.getDevice(deviceId, tenantId); return applicationDAO.getInstalledApplications(device.getId()); } catch (DeviceManagementDAOException e) { - throw new ApplicationManagementException("Error occured while fetching the Application List of '" + + throw new ApplicationManagementException("Error occurred while fetching the Application List of '" + deviceId.getType() + "' device carrying the identifier'" + deviceId.getId(), e); + } catch (SQLException e) { + throw new ApplicationManagementException("Error occurred while opening a connection to the data source", e); + } finally { + DeviceManagementDAOFactory.closeConnection(); } } diff --git a/components/device-mgt/org.wso2.carbon.device.mgt.core/src/main/java/org/wso2/carbon/device/mgt/core/dao/DeviceManagementDAOFactory.java b/components/device-mgt/org.wso2.carbon.device.mgt.core/src/main/java/org/wso2/carbon/device/mgt/core/dao/DeviceManagementDAOFactory.java index 8d28f17825..670935f3bb 100644 --- a/components/device-mgt/org.wso2.carbon.device.mgt.core/src/main/java/org/wso2/carbon/device/mgt/core/dao/DeviceManagementDAOFactory.java +++ b/components/device-mgt/org.wso2.carbon.device.mgt.core/src/main/java/org/wso2/carbon/device/mgt/core/dao/DeviceManagementDAOFactory.java @@ -20,6 +20,7 @@ package org.wso2.carbon.device.mgt.core.dao; import org.apache.commons.logging.Log; import org.apache.commons.logging.LogFactory; +import org.wso2.carbon.device.mgt.common.IllegalTransactionStateException; import org.wso2.carbon.device.mgt.common.TransactionManagementException; import org.wso2.carbon.device.mgt.core.config.datasource.DataSourceConfig; import org.wso2.carbon.device.mgt.core.config.datasource.JNDILookupDefinition; @@ -113,8 +114,14 @@ public class DeviceManagementDAOFactory { } public static void beginTransaction() throws TransactionManagementException { + Connection conn = currentConnection.get(); + if (conn != null) { + throw new IllegalTransactionStateException("A transaction is already active within the context of " + + "this particular thread. Therefore, calling 'beginTransaction/openConnection' while another " + + "transaction is already active is a sign of improper transaction handling"); + } try { - Connection conn = dataSource.getConnection(); + conn = dataSource.getConnection(); conn.setAutoCommit(false); currentConnection.set(conn); } catch (SQLException e) { @@ -123,58 +130,67 @@ public class DeviceManagementDAOFactory { } public static void openConnection() throws SQLException { - currentConnection.set(dataSource.getConnection()); + Connection conn = currentConnection.get(); + if (conn != null) { + throw new IllegalTransactionStateException("A transaction is already active within the context of " + + "this particular thread. Therefore, calling 'beginTransaction/openConnection' while another " + + "transaction is already active is a sign of improper transaction handling"); + } + conn = dataSource.getConnection(); + currentConnection.set(conn); } public static Connection getConnection() throws SQLException { - if (currentConnection.get() == null) { - currentConnection.set(dataSource.getConnection()); + Connection conn = currentConnection.get(); + if (conn == null) { + throw new IllegalTransactionStateException("No connection is associated with the current transaction. " + + "This might have ideally been caused by not properly initiating the transaction via " + + "'beginTransaction'/'openConnection' methods"); } - return currentConnection.get(); + return conn; } public static void commitTransaction() { + Connection conn = currentConnection.get(); + if (conn == null) { + throw new IllegalTransactionStateException("No connection is associated with the current transaction. " + + "This might have ideally been caused by not properly initiating the transaction via " + + "'beginTransaction'/'openConnection' methods"); + } try { - Connection conn = currentConnection.get(); - if (conn != null) { - conn.commit(); - } else { - if (log.isDebugEnabled()) { - log.debug("Datasource connection associated with the current thread is null, hence commit " + - "has not been attempted"); - } - } + conn.commit(); } catch (SQLException e) { log.error("Error occurred while committing the transaction", e); } } public static void rollbackTransaction() { + Connection conn = currentConnection.get(); + if (conn == null) { + throw new IllegalTransactionStateException("No connection is associated with the current transaction. " + + "This might have ideally been caused by not properly initiating the transaction via " + + "'beginTransaction'/'openConnection' methods"); + } try { - Connection conn = currentConnection.get(); - if (conn != null) { - conn.rollback(); - } else { - if (log.isDebugEnabled()) { - log.debug("Datasource connection associated with the current thread is null, hence rollback " + - "has not been attempted"); - } - } + conn.rollback(); } catch (SQLException e) { - log.warn("Error occurred while rollbacking the transaction", e); + log.warn("Error occurred while roll-backing the transaction", e); } } public static void closeConnection() { - Connection con = currentConnection.get(); - if (con != null) { - try { - con.close(); - } catch (SQLException e) { - log.warn("Error occurred while close the connection"); - } - currentConnection.remove(); + Connection conn = currentConnection.get(); + if (conn == null) { + throw new IllegalTransactionStateException("No connection is associated with the current transaction. " + + "This might have ideally been caused by not properly initiating the transaction via " + + "'beginTransaction'/'openConnection' methods"); + } + try { + conn.close(); + } catch (SQLException e) { + log.warn("Error occurred while close the connection"); } + currentConnection.remove(); } diff --git a/components/device-mgt/org.wso2.carbon.device.mgt.core/src/main/java/org/wso2/carbon/device/mgt/core/dao/EnrolmentDAO.java b/components/device-mgt/org.wso2.carbon.device.mgt.core/src/main/java/org/wso2/carbon/device/mgt/core/dao/EnrolmentDAO.java index 6978854e6b..1384d677d4 100644 --- a/components/device-mgt/org.wso2.carbon.device.mgt.core/src/main/java/org/wso2/carbon/device/mgt/core/dao/EnrolmentDAO.java +++ b/components/device-mgt/org.wso2.carbon.device.mgt.core/src/main/java/org/wso2/carbon/device/mgt/core/dao/EnrolmentDAO.java @@ -26,7 +26,7 @@ public interface EnrolmentDAO { int addEnrollment(int deviceId, EnrolmentInfo enrolmentInfo, int tenantId) throws DeviceManagementDAOException; int updateEnrollment(int deviceId, EnrolmentInfo enrolmentInfo, - int tenantId) throws DeviceManagementDAOException; + int tenantId) throws DeviceManagementDAOException; int removeEnrollment(int deviceId, String currentOwner, int tenantId) throws DeviceManagementDAOException; diff --git a/components/device-mgt/org.wso2.carbon.device.mgt.core/src/main/java/org/wso2/carbon/device/mgt/core/dao/impl/ApplicationMappingDAOImpl.java b/components/device-mgt/org.wso2.carbon.device.mgt.core/src/main/java/org/wso2/carbon/device/mgt/core/dao/impl/ApplicationMappingDAOImpl.java index 981db85305..8e56611751 100644 --- a/components/device-mgt/org.wso2.carbon.device.mgt.core/src/main/java/org/wso2/carbon/device/mgt/core/dao/impl/ApplicationMappingDAOImpl.java +++ b/components/device-mgt/org.wso2.carbon.device.mgt.core/src/main/java/org/wso2/carbon/device/mgt/core/dao/impl/ApplicationMappingDAOImpl.java @@ -111,16 +111,16 @@ public class ApplicationMappingDAOImpl implements ApplicationMappingDAO { "APPLICATION_ID = ? AND TENANT_ID = ?"; stmt = conn.prepareStatement(sql, Statement.RETURN_GENERATED_KEYS); - for(Integer appId:appIdList){ + for (Integer appId : appIdList) { stmt.setInt(1, deviceId); stmt.setInt(2, appId); stmt.setInt(3, tenantId); stmt.addBatch(); } stmt.executeBatch(); - } catch (SQLException e) { + } catch (SQLException e) { throw new DeviceManagementDAOException("Error occurred while adding device application mapping", e); - }finally { + } finally { DeviceManagementDAOUtil.cleanupResources(stmt, null); } } diff --git a/components/device-mgt/org.wso2.carbon.device.mgt.core/src/main/java/org/wso2/carbon/device/mgt/core/dao/impl/DeviceDAOImpl.java b/components/device-mgt/org.wso2.carbon.device.mgt.core/src/main/java/org/wso2/carbon/device/mgt/core/dao/impl/DeviceDAOImpl.java index 6d8d4107bf..b39b40fa4f 100644 --- a/components/device-mgt/org.wso2.carbon.device.mgt.core/src/main/java/org/wso2/carbon/device/mgt/core/dao/impl/DeviceDAOImpl.java +++ b/components/device-mgt/org.wso2.carbon.device.mgt.core/src/main/java/org/wso2/carbon/device/mgt/core/dao/impl/DeviceDAOImpl.java @@ -515,4 +515,5 @@ public class DeviceDAOImpl implements DeviceDAO { } return devices; } + } diff --git a/components/device-mgt/org.wso2.carbon.device.mgt.core/src/main/java/org/wso2/carbon/device/mgt/core/operation/mgt/dao/OperationManagementDAOFactory.java b/components/device-mgt/org.wso2.carbon.device.mgt.core/src/main/java/org/wso2/carbon/device/mgt/core/operation/mgt/dao/OperationManagementDAOFactory.java index 6ed2e443ad..e43843a474 100644 --- a/components/device-mgt/org.wso2.carbon.device.mgt.core/src/main/java/org/wso2/carbon/device/mgt/core/operation/mgt/dao/OperationManagementDAOFactory.java +++ b/components/device-mgt/org.wso2.carbon.device.mgt.core/src/main/java/org/wso2/carbon/device/mgt/core/operation/mgt/dao/OperationManagementDAOFactory.java @@ -20,6 +20,7 @@ package org.wso2.carbon.device.mgt.core.operation.mgt.dao; import org.apache.commons.logging.Log; import org.apache.commons.logging.LogFactory; +import org.wso2.carbon.device.mgt.common.IllegalTransactionStateException; import org.wso2.carbon.device.mgt.common.TransactionManagementException; import org.wso2.carbon.device.mgt.core.config.datasource.DataSourceConfig; import org.wso2.carbon.device.mgt.core.config.datasource.JNDILookupDefinition; @@ -87,7 +88,9 @@ public class OperationManagementDAOFactory { public static Connection getConnection() throws SQLException { if (currentConnection.get() == null) { - currentConnection.set(dataSource.getConnection()); + throw new IllegalTransactionStateException("No connection is associated with the current transaction. " + + "This might have ideally caused by not properly initiating the transaction via " + + "'beginTransaction'/'openConnection' methods"); } return currentConnection.get(); } diff --git a/components/device-mgt/org.wso2.carbon.device.mgt.core/src/main/java/org/wso2/carbon/device/mgt/core/service/DeviceManagementProviderServiceImpl.java b/components/device-mgt/org.wso2.carbon.device.mgt.core/src/main/java/org/wso2/carbon/device/mgt/core/service/DeviceManagementProviderServiceImpl.java index 197a9321e3..f9f382185e 100644 --- a/components/device-mgt/org.wso2.carbon.device.mgt.core/src/main/java/org/wso2/carbon/device/mgt/core/service/DeviceManagementProviderServiceImpl.java +++ b/components/device-mgt/org.wso2.carbon.device.mgt.core/src/main/java/org/wso2/carbon/device/mgt/core/service/DeviceManagementProviderServiceImpl.java @@ -156,9 +156,11 @@ public class DeviceManagementProviderServiceImpl implements DeviceManagementProv device.getDeviceIdentifier() + "', which belongs to " + "platform '" + device.getType() + " upon the user '" + device.getEnrolmentInfo().getOwner() + "'"); } - } catch (TransactionManagementException | DeviceManagementDAOException e) { + } catch (DeviceManagementDAOException e) { DeviceManagementDAOFactory.rollbackTransaction(); - log.error("Error occurred while adding enrolment related metadata", e); + throw new DeviceManagementException("Error occurred while adding enrolment related metadata", e); + } catch (TransactionManagementException e) { + throw new DeviceManagementException("Error occurred while initiating transaction", e); } finally { DeviceManagementDAOFactory.closeConnection(); } @@ -173,10 +175,12 @@ public class DeviceManagementProviderServiceImpl implements DeviceManagementProv int deviceId = deviceDAO.addDevice(type.getId(), device, tenantId); enrolmentId = enrolmentDAO.addEnrollment(deviceId, device.getEnrolmentInfo(), tenantId); DeviceManagementDAOFactory.commitTransaction(); - } catch (DeviceManagementDAOException | TransactionManagementException e) { + } catch (DeviceManagementDAOException e) { DeviceManagementDAOFactory.rollbackTransaction(); - log.error("Error occurred while adding metadata of '" + device.getType() + "' device carrying " + - "the identifier '" + device.getDeviceIdentifier() + "'", e); + throw new DeviceManagementException("Error occurred while adding metadata of '" + device.getType() + + "' device carrying the identifier '" + device.getDeviceIdentifier() + "'", e); + } catch (TransactionManagementException e) { + throw new DeviceManagementException("Error occurred while initiating transaction", e); } finally { DeviceManagementDAOFactory.closeConnection(); } @@ -214,10 +218,12 @@ public class DeviceManagementProviderServiceImpl implements DeviceManagementProv enrolmentDAO.updateEnrollment(deviceId, device.getEnrolmentInfo(), tenantId); DeviceManagementDAOFactory.commitTransaction(); - } catch (DeviceManagementDAOException | TransactionManagementException e) { + } catch (DeviceManagementDAOException e) { DeviceManagementDAOFactory.rollbackTransaction(); throw new DeviceManagementException("Error occurred while modifying the device " + "'" + device.getId() + "'", e); + } catch (TransactionManagementException e) { + throw new DeviceManagementException("Error occurred while initiating transaction", e); } finally { DeviceManagementDAOFactory.closeConnection(); } @@ -247,10 +253,12 @@ public class DeviceManagementProviderServiceImpl implements DeviceManagementProv deviceDAO.updateDevice(deviceType.getId(), device, tenantId); DeviceManagementDAOFactory.commitTransaction(); - } catch (DeviceManagementDAOException | TransactionManagementException e) { + } catch (DeviceManagementDAOException e) { DeviceManagementDAOFactory.rollbackTransaction(); throw new DeviceManagementException("Error occurred while dis-enrolling '" + deviceId.getType() + "' device with the identifier '" + deviceId.getId() + "'", e); + } catch (TransactionManagementException e) { + throw new DeviceManagementException("Error occurred while initiating transaction", e); } finally { DeviceManagementDAOFactory.closeConnection(); } @@ -302,14 +310,16 @@ public class DeviceManagementProviderServiceImpl implements DeviceManagementProv List allDevices; try { DeviceManagementDAOFactory.openConnection(); - allDevices = deviceDAO.getDevices(this.getTenantId()); - } catch (DeviceManagementDAOException | SQLException e) { + } catch (DeviceManagementDAOException e) { throw new DeviceManagementException("Error occurred while retrieving device list pertaining to " + "the current tenant", e); + } catch (SQLException e) { + throw new DeviceManagementException("Error occurred while opening a connection to the data source", e); } finally { DeviceManagementDAOFactory.closeConnection(); } + for (Device device : allDevices) { DeviceManager deviceManager = this.getDeviceManager(device.getType()); if (deviceManager == null) { @@ -338,9 +348,11 @@ public class DeviceManagementProviderServiceImpl implements DeviceManagementProv try { DeviceManagementDAOFactory.openConnection(); allDevices = deviceDAO.getDevices(deviceType, this.getTenantId()); - } catch (DeviceManagementDAOException | SQLException e) { + } catch (DeviceManagementDAOException e) { throw new DeviceManagementException("Error occurred while retrieving all devices of type '" + deviceType + "' that are being managed within the scope of current tenant", e); + } catch (SQLException e) { + throw new DeviceManagementException("Error occurred while opening a connection to the data source", e); } finally { DeviceManagementDAOFactory.closeConnection(); } @@ -415,7 +427,6 @@ public class DeviceManagementProviderServiceImpl implements DeviceManagementProv .getProperty("line.separator")).append(messageFooter3.trim()); } catch (IOException e) { - log.error("IO error in processing enrol email message " + emailMessageProperties); throw new DeviceManagementException("Error replacing tags in email template '" + emailMessageProperties.getSubject() + "'", e); } @@ -479,7 +490,6 @@ public class DeviceManagementProviderServiceImpl implements DeviceManagementProv messageBuilder.append(System.getProperty("line.separator")).append(messageFooter3.trim()); } catch (IOException e) { - log.error("IO error in processing enrol email message " + emailMessageProperties); throw new DeviceManagementException("Error replacing tags in email template '" + emailMessageProperties.getSubject() + "'", e); } @@ -492,10 +502,13 @@ public class DeviceManagementProviderServiceImpl implements DeviceManagementProv public Device getDevice(DeviceIdentifier deviceId) throws DeviceManagementException { Device device; try { + DeviceManagementDAOFactory.openConnection(); device = deviceDAO.getDevice(deviceId, this.getTenantId()); } catch (DeviceManagementDAOException e) { throw new DeviceManagementException("Error occurred while obtaining the device for id " + "'" + deviceId.getId() + "'", e); + } catch (SQLException e) { + throw new DeviceManagementException("Error occurred while opening a connection to the data source", e); } finally { DeviceManagementDAOFactory.closeConnection(); } @@ -570,9 +583,11 @@ public class DeviceManagementProviderServiceImpl implements DeviceManagementProv DeviceManagementDAOFactory.commitTransaction(); return success; - } catch (DeviceManagementDAOException | TransactionManagementException e) { + } catch (DeviceManagementDAOException e) { DeviceManagementDAOFactory.rollbackTransaction(); throw new DeviceManagementException("Error occurred while setting enrollment status", e); + } catch (TransactionManagementException e) { + throw new DeviceManagementException("Error occurred while initiating transaction", e); } finally { DeviceManagementDAOFactory.closeConnection(); } @@ -619,8 +634,8 @@ public class DeviceManagementProviderServiceImpl implements DeviceManagementProv } @Override - public int addOperation(Operation operation, List devices) throws - OperationManagementException { + public int addOperation(Operation operation, + List devices) throws OperationManagementException { return DeviceManagementDataHolder.getInstance().getOperationManager().addOperation(operation, devices); } @@ -651,8 +666,8 @@ public class DeviceManagementProviderServiceImpl implements DeviceManagementProv } @Override - public Operation getOperationByDeviceAndOperationId(DeviceIdentifier deviceId, int operationId) - throws OperationManagementException { + public Operation getOperationByDeviceAndOperationId(DeviceIdentifier deviceId, + int operationId) throws OperationManagementException { return DeviceManagementDataHolder.getInstance().getOperationManager().getOperationByDeviceAndOperationId( deviceId, operationId); } @@ -677,9 +692,11 @@ public class DeviceManagementProviderServiceImpl implements DeviceManagementProv try { DeviceManagementDAOFactory.openConnection(); userDevices = deviceDAO.getDevicesOfUser(username, this.getTenantId()); - } catch (DeviceManagementDAOException | SQLException e) { + } catch (DeviceManagementDAOException e) { throw new DeviceManagementException("Error occurred while retrieving the list of devices that " + "belong to the user '" + username + "'", e); + } catch (SQLException e) { + throw new DeviceManagementException("Error occurred while opening a connection to the data source", e); } finally { DeviceManagementDAOFactory.closeConnection(); } @@ -750,8 +767,10 @@ public class DeviceManagementProviderServiceImpl implements DeviceManagementProv try { DeviceManagementDAOFactory.openConnection(); return deviceDAO.getDeviceCount(this.getTenantId()); - } catch (DeviceManagementDAOException | SQLException e) { + } catch (DeviceManagementDAOException e) { throw new DeviceManagementException("Error occurred while retrieving the device count", e); + } catch (SQLException e) { + throw new DeviceManagementException("Error occurred while opening a connection to the data source", e); } finally { DeviceManagementDAOFactory.closeConnection(); } @@ -764,9 +783,11 @@ public class DeviceManagementProviderServiceImpl implements DeviceManagementProv try { DeviceManagementDAOFactory.openConnection(); allDevices = deviceDAO.getDevicesByName(deviceName, this.getTenantId()); - } catch (DeviceManagementDAOException | SQLException e) { + } catch (DeviceManagementDAOException e) { throw new DeviceManagementException("Error occurred while fetching the list of devices that matches to '" + deviceName + "'", e); + } catch (SQLException e) { + throw new DeviceManagementException("Error occurred while opening a connection to the data source", e); } finally { DeviceManagementDAOFactory.closeConnection(); } @@ -788,14 +809,22 @@ public class DeviceManagementProviderServiceImpl implements DeviceManagementProv @Override public void updateDeviceEnrolmentInfo(Device device, EnrolmentInfo.Status status) throws DeviceManagementException { try { + DeviceManagementDAOFactory.beginTransaction(); + DeviceType deviceType = deviceTypeDAO.getDeviceType(device.getType()); device.getEnrolmentInfo().setDateOfLastUpdate(new Date().getTime()); device.getEnrolmentInfo().setStatus(status); deviceDAO.updateDevice(deviceType.getId(), device, this.getTenantId()); - } catch (DeviceManagementDAOException deviceDaoEx) { - String errorMsg = "Error occured update device enrolment status : " + device.getId(); - log.error(errorMsg, deviceDaoEx); - throw new DeviceManagementException(errorMsg, deviceDaoEx); + + DeviceManagementDAOFactory.commitTransaction(); + } catch (DeviceManagementDAOException e) { + DeviceManagementDAOFactory.rollbackTransaction(); + throw new DeviceManagementException("Error occurred update device enrolment status : '" + + device.getId() + "'", e); + } catch (TransactionManagementException e) { + throw new DeviceManagementException("Error occurred while initiating transaction", e); + } finally { + DeviceManagementDAOFactory.closeConnection(); } } @@ -826,12 +855,13 @@ public class DeviceManagementProviderServiceImpl implements DeviceManagementProv try { DeviceManagementDAOFactory.openConnection(); allDevices = deviceDAO.getDevicesByStatus(status, this.getTenantId()); - } catch (DeviceManagementDAOException | SQLException e) { + } catch (DeviceManagementDAOException e) { throw new DeviceManagementException( "Error occurred while fetching the list of devices that matches to status: '" + status + "'", e); + } catch (SQLException e) { + throw new DeviceManagementException("Error occurred while opening a connection to the data source", e); } finally { DeviceManagementDAOFactory.closeConnection(); - } for (Device device : allDevices) { diff --git a/components/device-mgt/org.wso2.carbon.device.mgt.core/src/test/java/org/wso2/carbon/device/mgt/core/app/mgt/ApplicationManagementProviderServiceTest.java b/components/device-mgt/org.wso2.carbon.device.mgt.core/src/test/java/org/wso2/carbon/device/mgt/core/app/mgt/ApplicationManagementProviderServiceTest.java index edab3737a2..c5b69b3a17 100644 --- a/components/device-mgt/org.wso2.carbon.device.mgt.core/src/test/java/org/wso2/carbon/device/mgt/core/app/mgt/ApplicationManagementProviderServiceTest.java +++ b/components/device-mgt/org.wso2.carbon.device.mgt.core/src/test/java/org/wso2/carbon/device/mgt/core/app/mgt/ApplicationManagementProviderServiceTest.java @@ -42,7 +42,8 @@ public class ApplicationManagementProviderServiceTest { @BeforeClass public void init() { deviceManagementPluginRepository = new DeviceManagementPluginRepository(); - TestDeviceManagementService testDeviceManagementService = new TestDeviceManagementService(TestDataHolder.TEST_DEVICE_TYPE); + TestDeviceManagementService testDeviceManagementService = + new TestDeviceManagementService(TestDataHolder.TEST_DEVICE_TYPE); try { deviceManagementPluginRepository.addDeviceManagementProvider(testDeviceManagementService); } catch (DeviceManagementException e) { @@ -53,11 +54,11 @@ public class ApplicationManagementProviderServiceTest { } @Test - public void updateApplicationTest(){ + public void updateApplicationTest() { - List applications = new ArrayList(); + List applications = new ArrayList<>(); - Application application1 = TestDataHolder.generateApplicationDummyData("org.wso2.app1"); + Application application1 = TestDataHolder.generateApplicationDummyData("org.wso2.app1"); Application application2 = TestDataHolder.generateApplicationDummyData("org.wso2.app2"); Application application3 = TestDataHolder.generateApplicationDummyData("org.wso2.app3"); Application application4 = TestDataHolder.generateApplicationDummyData("org.wso2.app4"); @@ -67,34 +68,43 @@ public class ApplicationManagementProviderServiceTest { applications.add(application3); applications.add(application4); - Device device = TestDataHolder.initialTestDevice; - DeviceIdentifier deviceIdentifier = new DeviceIdentifier(); - deviceIdentifier.setId(TestDataHolder.initialDeviceIdentifier); - deviceIdentifier.setType(device.getType()); + Device device = TestDataHolder.initialTestDevice; + + if (device == null) { + throw new IllegalStateException("Device information is not available"); + } + DeviceIdentifier deviceId = new DeviceIdentifier(); + + String deviceIdentifier = TestDataHolder.initialDeviceIdentifier; + if (deviceIdentifier == null) { + throw new IllegalStateException("Device identifier is not available"); + } + deviceId.setId(deviceIdentifier); + deviceId.setType(device.getType()); AppManagementConfig appManagementConfig = new AppManagementConfig(); appMgtProvider = new ApplicationManagerProviderServiceImpl(deviceManagementPluginRepository); try { - appMgtProvider.updateApplicationListInstalledInDevice(deviceIdentifier, applications); - } catch (ApplicationManagementException appMgtEx){ + appMgtProvider.updateApplicationListInstalledInDevice(deviceId, applications); + } catch (ApplicationManagementException appMgtEx) { String msg = "Error occurred while updating app list '" + TestDataHolder.TEST_DEVICE_TYPE + "'"; log.error(msg, appMgtEx); Assert.fail(msg, appMgtEx); } Application application5 = TestDataHolder.generateApplicationDummyData("org.wso2.app5"); - applications = new ArrayList(); + applications = new ArrayList<>(); applications.add(application4); applications.add(application3); applications.add(application5); try { - appMgtProvider.updateApplicationListInstalledInDevice(deviceIdentifier, applications); - List installedApps = appMgtProvider.getApplicationListForDevice(deviceIdentifier); - log.info("Number of installed applications:"+installedApps.size()); - Assert.assertEquals(installedApps.size(),3,"Num of installed applications should be two"); - } catch (ApplicationManagementException appMgtEx){ + appMgtProvider.updateApplicationListInstalledInDevice(deviceId, applications); + List installedApps = appMgtProvider.getApplicationListForDevice(deviceId); + log.info("Number of installed applications:" + installedApps.size()); + Assert.assertEquals(installedApps.size(), 3, "Num of installed applications should be two"); + } catch (ApplicationManagementException appMgtEx) { String msg = "Error occurred while updating app list '" + TestDataHolder.TEST_DEVICE_TYPE + "'"; log.error(msg, appMgtEx); Assert.fail(msg, appMgtEx); diff --git a/components/device-mgt/org.wso2.carbon.device.mgt.core/src/test/java/org/wso2/carbon/device/mgt/core/common/BaseDeviceManagementTest.java b/components/device-mgt/org.wso2.carbon.device.mgt.core/src/test/java/org/wso2/carbon/device/mgt/core/common/BaseDeviceManagementTest.java index aba662e3ef..ba0a666846 100644 --- a/components/device-mgt/org.wso2.carbon.device.mgt.core/src/test/java/org/wso2/carbon/device/mgt/core/common/BaseDeviceManagementTest.java +++ b/components/device-mgt/org.wso2.carbon.device.mgt.core/src/test/java/org/wso2/carbon/device/mgt/core/common/BaseDeviceManagementTest.java @@ -72,7 +72,7 @@ public abstract class BaseDeviceManagementTest { return new org.apache.tomcat.jdbc.pool.DataSource(properties); } - private void initializeCarbonContext(){ + private void initializeCarbonContext() { if (System.getProperty("carbon.home") == null) { File file = new File("src/test/resources/carbon-home"); @@ -129,11 +129,12 @@ public abstract class BaseDeviceManagementTest { conn = getDataSource().getConnection(); conn.setAutoCommit(false); - this.cleanupEnrolmentData(conn); - this.cleanApplicationMappingData(conn); - this.cleanApplicationData(conn); - this.cleanupDeviceData(conn); - this.cleanupDeviceTypeData(conn); + //TODO:FIX ME +// this.cleanupEnrolmentData(conn); +// this.cleanApplicationMappingData(conn); +// this.cleanApplicationData(conn); +// this.cleanupDeviceData(conn); +// this.cleanupDeviceTypeData(conn); conn.commit(); } catch (SQLException e) { @@ -158,64 +159,34 @@ public abstract class BaseDeviceManagementTest { } } - private void cleanApplicationMappingData(Connection conn) throws SQLException{ - PreparedStatement stmt = null; - try { - stmt = conn.prepareStatement("DELETE FROM DM_DEVICE_APPLICATION_MAPPING"); + private void cleanApplicationMappingData(Connection conn) throws SQLException { + try (PreparedStatement stmt = conn.prepareStatement("DELETE FROM DM_DEVICE_APPLICATION_MAPPING")) { stmt.execute(); - } finally { - if (stmt != null) { - stmt.close(); - } } } - private void cleanApplicationData(Connection conn) throws SQLException{ - PreparedStatement stmt = null; - try { - stmt = conn.prepareStatement("DELETE FROM DM_APPLICATION"); + private void cleanApplicationData(Connection conn) throws SQLException { + try (PreparedStatement stmt = conn.prepareStatement("DELETE FROM DM_APPLICATION")) { stmt.execute(); - } finally { - if (stmt != null) { - stmt.close(); - } } } private void cleanupEnrolmentData(Connection conn) throws SQLException { - PreparedStatement stmt = null; - try { - stmt = conn.prepareStatement("DELETE FROM DM_ENROLMENT"); + try (PreparedStatement stmt = conn.prepareStatement("DELETE FROM DM_ENROLMENT")) { stmt.execute(); - } finally { - if (stmt != null) { - stmt.close(); - } } } private void cleanupDeviceData(Connection conn) throws SQLException { - PreparedStatement stmt = null; - try { - stmt = conn.prepareStatement("DELETE FROM DM_DEVICE"); + try (PreparedStatement stmt = conn.prepareStatement("DELETE FROM DM_DEVICE")) { stmt.execute(); - } finally { - if (stmt != null) { - stmt.close(); - } } } private void cleanupDeviceTypeData(Connection conn) throws SQLException { - PreparedStatement stmt = null; - try { - stmt = conn.prepareStatement("DELETE FROM DM_DEVICE_TYPE"); + try (PreparedStatement stmt = conn.prepareStatement("DELETE FROM DM_DEVICE_TYPE")) { stmt.execute(); - } finally { - if (stmt != null) { - stmt.close(); - } } } diff --git a/components/device-mgt/org.wso2.carbon.device.mgt.core/src/test/java/org/wso2/carbon/device/mgt/core/dao/DevicePersistTests.java b/components/device-mgt/org.wso2.carbon.device.mgt.core/src/test/java/org/wso2/carbon/device/mgt/core/dao/DevicePersistTests.java index 603954ea76..0798f28115 100644 --- a/components/device-mgt/org.wso2.carbon.device.mgt.core/src/test/java/org/wso2/carbon/device/mgt/core/dao/DevicePersistTests.java +++ b/components/device-mgt/org.wso2.carbon.device.mgt.core/src/test/java/org/wso2/carbon/device/mgt/core/dao/DevicePersistTests.java @@ -26,6 +26,7 @@ import org.testng.annotations.Test; import org.wso2.carbon.device.mgt.common.Device; import org.wso2.carbon.device.mgt.common.DeviceIdentifier; import org.wso2.carbon.device.mgt.common.EnrolmentInfo.Status; +import org.wso2.carbon.device.mgt.common.TransactionManagementException; import org.wso2.carbon.device.mgt.core.TestUtils; import org.wso2.carbon.device.mgt.core.common.BaseDeviceManagementTest; import org.wso2.carbon.device.mgt.core.common.TestDataHolder; @@ -50,12 +51,18 @@ public class DevicePersistTests extends BaseDeviceManagementTest { public void testAddDeviceTypeTest() { DeviceType deviceType = TestDataHolder.generateDeviceTypeData(TestDataHolder.TEST_DEVICE_TYPE); try { - DeviceManagementDAOFactory.openConnection(); + DeviceManagementDAOFactory.beginTransaction(); deviceTypeDAO.addDeviceType(deviceType); - } catch (DeviceManagementDAOException | SQLException e) { + } catch (DeviceManagementDAOException e) { + DeviceManagementDAOFactory.rollbackTransaction(); String msg = "Error occurred while adding device type '" + deviceType.getName() + "'"; log.error(msg, e); Assert.fail(msg, e); + } catch (TransactionManagementException e) { + String msg = "Error occurred while initiating transaction to persist device type '" + + deviceType.getName() + "'"; + log.error(msg, e); + Assert.fail(msg, e); } finally { DeviceManagementDAOFactory.closeConnection(); } @@ -75,21 +82,26 @@ public class DevicePersistTests extends BaseDeviceManagementTest { @Test(dependsOnMethods = {"testAddDeviceTypeTest"}) public void testAddDeviceTest() { - int tenantId = TestDataHolder.SUPER_TENANT_ID; Device device = TestDataHolder.generateDummyDeviceData(TestDataHolder.TEST_DEVICE_TYPE); try { - DeviceManagementDAOFactory.openConnection(); + DeviceManagementDAOFactory.beginTransaction(); int deviceId = deviceDAO.addDevice(TestDataHolder.initialTestDeviceType.getId(), device, tenantId); device.setId(deviceId); deviceDAO.addEnrollment(device, tenantId); + DeviceManagementDAOFactory.commitTransaction(); TestDataHolder.initialTestDevice = device; - } catch (DeviceManagementDAOException | SQLException e) { + } catch (DeviceManagementDAOException e) { + DeviceManagementDAOFactory.rollbackTransaction(); String msg = "Error occurred while adding '" + device.getType() + "' device with the identifier '" + device.getDeviceIdentifier() + "'"; log.error(msg, e); Assert.fail(msg, e); + } catch (TransactionManagementException e) { + String msg = "Error occurred while initiating transaction"; + log.error(msg, e); + Assert.fail(msg, e); } finally { DeviceManagementDAOFactory.closeConnection(); } @@ -164,14 +176,19 @@ public class DevicePersistTests extends BaseDeviceManagementTest { Device device = TestDataHolder.initialTestDevice; try { - DeviceManagementDAOFactory.openConnection(); + DeviceManagementDAOFactory.beginTransaction(); DeviceIdentifier deviceId = new DeviceIdentifier(device.getDeviceIdentifier(), device.getType()); deviceDAO.setEnrolmentStatus(deviceId, device.getEnrolmentInfo().getOwner(), Status.ACTIVE, TestDataHolder.SUPER_TENANT_ID); - } catch (DeviceManagementDAOException | SQLException e) { + } catch (DeviceManagementDAOException e) { + DeviceManagementDAOFactory.rollbackTransaction(); String msg = "Error occurred while setting enrolment status"; log.error(msg, e); Assert.fail(msg, e); + } catch (TransactionManagementException e) { + String msg = "Error occurred while initiating transaction"; + log.error(msg, e); + Assert.fail(msg, e); } finally { DeviceManagementDAOFactory.closeConnection(); } diff --git a/components/device-mgt/org.wso2.carbon.device.mgt.core/src/test/resources/testng.xml b/components/device-mgt/org.wso2.carbon.device.mgt.core/src/test/resources/testng.xml index cf107283b8..4622a58873 100644 --- a/components/device-mgt/org.wso2.carbon.device.mgt.core/src/test/resources/testng.xml +++ b/components/device-mgt/org.wso2.carbon.device.mgt.core/src/test/resources/testng.xml @@ -37,4 +37,4 @@ - \ No newline at end of file + diff --git a/components/policy-mgt/org.wso2.carbon.policy.mgt.core/src/main/java/org/wso2/carbon/policy/mgt/core/dao/PolicyManagementDAOFactory.java b/components/policy-mgt/org.wso2.carbon.policy.mgt.core/src/main/java/org/wso2/carbon/policy/mgt/core/dao/PolicyManagementDAOFactory.java index b488ddde40..0b00f90b14 100644 --- a/components/policy-mgt/org.wso2.carbon.policy.mgt.core/src/main/java/org/wso2/carbon/policy/mgt/core/dao/PolicyManagementDAOFactory.java +++ b/components/policy-mgt/org.wso2.carbon.policy.mgt.core/src/main/java/org/wso2/carbon/policy/mgt/core/dao/PolicyManagementDAOFactory.java @@ -20,6 +20,7 @@ package org.wso2.carbon.policy.mgt.core.dao; import org.apache.commons.logging.Log; import org.apache.commons.logging.LogFactory; +import org.wso2.carbon.device.mgt.common.IllegalTransactionStateException; import org.wso2.carbon.policy.mgt.core.config.datasource.DataSourceConfig; import org.wso2.carbon.policy.mgt.core.config.datasource.JNDILookupDefinition; import org.wso2.carbon.policy.mgt.core.dao.impl.FeatureDAOImpl; @@ -38,7 +39,7 @@ public class PolicyManagementDAOFactory { private static DataSource dataSource; private static final Log log = LogFactory.getLog(PolicyManagementDAOFactory.class); - private static ThreadLocal currentConnection = new ThreadLocal(); + private static ThreadLocal currentConnection = new ThreadLocal<>(); public static void init(DataSourceConfig config) { dataSource = resolveDataSource(config); @@ -48,13 +49,6 @@ public class PolicyManagementDAOFactory { dataSource = dtSource; } - public static DataSource getDataSource() { - if (dataSource != null) { - return dataSource; - } - throw new RuntimeException("Data source is not yet configured."); - } - public static PolicyDAO getPolicyDAO() { return new PolicyDAOImpl(); } @@ -81,7 +75,7 @@ public class PolicyManagementDAOFactory { DataSource dataSource = null; if (config == null) { throw new RuntimeException("Device Management Repository data source configuration is null and thus," + - " is not initialized"); + " is not initialized"); } JNDILookupDefinition jndiConfig = config.getJndiLookupDefinition(); if (jndiConfig != null) { @@ -91,7 +85,7 @@ public class PolicyManagementDAOFactory { List jndiPropertyList = jndiConfig.getJndiProperties(); if (jndiPropertyList != null) { - Hashtable jndiProperties = new Hashtable(); + Hashtable jndiProperties = new Hashtable<>(); for (JNDILookupDefinition.JNDIProperty prop : jndiPropertyList) { jndiProperties.put(prop.getName(), prop.getValue()); } @@ -104,8 +98,14 @@ public class PolicyManagementDAOFactory { } public static void beginTransaction() throws PolicyManagerDAOException { + Connection conn = currentConnection.get(); + if (conn != null) { + throw new IllegalTransactionStateException("A transaction is already active within the context of " + + "this particular thread. Therefore, calling 'beginTransaction/openConnection' while another " + + "transaction is already active is a sign of improper transaction handling"); + } try { - Connection conn = dataSource.getConnection(); + conn = dataSource.getConnection(); conn.setAutoCommit(false); currentConnection.set(conn); } catch (SQLException e) { @@ -113,63 +113,68 @@ public class PolicyManagementDAOFactory { } } - public static Connection getConnection() throws PolicyManagerDAOException { - if (currentConnection.get() == null) { - try { - Connection conn = dataSource.getConnection(); - conn.setAutoCommit(false); - currentConnection.set(conn); - } catch (SQLException e) { - throw new PolicyManagerDAOException("Error occurred while retrieving data source connection", e); - } + public static Connection getConnection() { + Connection conn = currentConnection.get(); + if (conn == null) { + throw new IllegalTransactionStateException("No connection is associated with the current transaction. " + + "This might have ideally been caused by not properly initiating the transaction via " + + "'beginTransaction'/'openConnection' methods"); } - return currentConnection.get(); + return conn; } public static void closeConnection() { - Connection con = currentConnection.get(); + Connection conn = currentConnection.get(); + if (conn == null) { + throw new IllegalTransactionStateException("No connection is associated with the current transaction. " + + "This might have ideally been caused by not properly initiating the transaction via " + + "'beginTransaction'/'openConnection' methods"); + } try { - con.close(); + conn.close(); } catch (SQLException e) { log.warn("Error occurred while close the connection", e); } currentConnection.remove(); } - public static void commitTransaction() throws PolicyManagerDAOException { + public static void commitTransaction() { + Connection conn = currentConnection.get(); + if (conn == null) { + throw new IllegalTransactionStateException("No connection is associated with the current transaction. " + + "This might have ideally been caused by not properly initiating the transaction via " + + "'beginTransaction'/'openConnection' methods"); + } try { - Connection conn = currentConnection.get(); - if (conn != null) { - conn.commit(); - } else { - if (log.isDebugEnabled()) { - log.debug("Datasource connection associated with the current thread is null, hence commit " + - "has not been attempted"); - } - } + conn.commit(); } catch (SQLException e) { - throw new PolicyManagerDAOException("Error occurred while committing the transaction", e); + log.error("Error occurred while committing the transaction", e); } } public static void rollbackTransaction() { + Connection conn = currentConnection.get(); + if (conn == null) { + throw new IllegalTransactionStateException("No connection is associated with the current transaction. " + + "This might have ideally been caused by not properly initiating the transaction via " + + "'beginTransaction'/'openConnection' methods"); + } try { - Connection conn = currentConnection.get(); - if (conn != null) { - conn.rollback(); - } else { - if (log.isDebugEnabled()) { - log.debug("Datasource connection associated with the current thread is null, hence rollback " + - "has not been attempted"); - } - } + conn.rollback(); } catch (SQLException e) { log.warn("Error occurred while roll-backing the transaction", e); } } public static void openConnection() throws SQLException { - currentConnection.set(dataSource.getConnection()); + Connection conn = currentConnection.get(); + if (conn != null) { + throw new IllegalTransactionStateException("A transaction is already active within the context of " + + "this particular thread. Therefore, calling 'beginTransaction/openConnection' while another " + + "transaction is already active is a sign of improper transaction handling"); + } + conn = dataSource.getConnection(); + currentConnection.set(conn); } } diff --git a/components/policy-mgt/org.wso2.carbon.policy.mgt.core/src/main/java/org/wso2/carbon/policy/mgt/core/dao/impl/FeatureDAOImpl.java b/components/policy-mgt/org.wso2.carbon.policy.mgt.core/src/main/java/org/wso2/carbon/policy/mgt/core/dao/impl/FeatureDAOImpl.java index 37dde34099..c1092d0d88 100644 --- a/components/policy-mgt/org.wso2.carbon.policy.mgt.core/src/main/java/org/wso2/carbon/policy/mgt/core/dao/impl/FeatureDAOImpl.java +++ b/components/policy-mgt/org.wso2.carbon.policy.mgt.core/src/main/java/org/wso2/carbon/policy/mgt/core/dao/impl/FeatureDAOImpl.java @@ -471,13 +471,7 @@ public class FeatureDAOImpl implements FeatureDAO { } private Connection getConnection() throws FeatureManagerDAOException { - - try { - return PolicyManagementDAOFactory.getConnection(); - } catch (PolicyManagerDAOException e) { - throw new FeatureManagerDAOException("Error occurred while obtaining a connection from the policy " + - "management metadata repository config.datasource", e); - } + return PolicyManagementDAOFactory.getConnection(); } } diff --git a/components/policy-mgt/org.wso2.carbon.policy.mgt.core/src/main/java/org/wso2/carbon/policy/mgt/core/dao/impl/MonitoringDAOImpl.java b/components/policy-mgt/org.wso2.carbon.policy.mgt.core/src/main/java/org/wso2/carbon/policy/mgt/core/dao/impl/MonitoringDAOImpl.java index 0a2587fa27..312988732b 100644 --- a/components/policy-mgt/org.wso2.carbon.policy.mgt.core/src/main/java/org/wso2/carbon/policy/mgt/core/dao/impl/MonitoringDAOImpl.java +++ b/components/policy-mgt/org.wso2.carbon.policy.mgt.core/src/main/java/org/wso2/carbon/policy/mgt/core/dao/impl/MonitoringDAOImpl.java @@ -382,12 +382,7 @@ public class MonitoringDAOImpl implements MonitoringDAO { } private Connection getConnection() throws MonitoringDAOException { - try { - return PolicyManagementDAOFactory.getConnection(); - } catch (PolicyManagerDAOException e) { - throw new MonitoringDAOException("Error occurred while obtaining a connection from the policy " + - "management metadata repository config.datasource", e); - } + return PolicyManagementDAOFactory.getConnection(); } } diff --git a/components/policy-mgt/org.wso2.carbon.policy.mgt.core/src/main/java/org/wso2/carbon/policy/mgt/core/dao/impl/PolicyDAOImpl.java b/components/policy-mgt/org.wso2.carbon.policy.mgt.core/src/main/java/org/wso2/carbon/policy/mgt/core/dao/impl/PolicyDAOImpl.java index 8b08ee968c..68aa6fa8bd 100644 --- a/components/policy-mgt/org.wso2.carbon.policy.mgt.core/src/main/java/org/wso2/carbon/policy/mgt/core/dao/impl/PolicyDAOImpl.java +++ b/components/policy-mgt/org.wso2.carbon.policy.mgt.core/src/main/java/org/wso2/carbon/policy/mgt/core/dao/impl/PolicyDAOImpl.java @@ -1082,7 +1082,7 @@ public class PolicyDAOImpl implements PolicyDAO { stmt.setInt(2, tenantId); resultSet = stmt.executeQuery(); - while (resultSet.next()) { + if (resultSet.next()) { return resultSet.getInt("POLICY_ID"); } } catch (SQLException e) { diff --git a/components/policy-mgt/org.wso2.carbon.policy.mgt.core/src/main/java/org/wso2/carbon/policy/mgt/core/dao/impl/ProfileDAOImpl.java b/components/policy-mgt/org.wso2.carbon.policy.mgt.core/src/main/java/org/wso2/carbon/policy/mgt/core/dao/impl/ProfileDAOImpl.java index 58ceefba76..d8164cffe4 100644 --- a/components/policy-mgt/org.wso2.carbon.policy.mgt.core/src/main/java/org/wso2/carbon/policy/mgt/core/dao/impl/ProfileDAOImpl.java +++ b/components/policy-mgt/org.wso2.carbon.policy.mgt.core/src/main/java/org/wso2/carbon/policy/mgt/core/dao/impl/ProfileDAOImpl.java @@ -28,7 +28,6 @@ import org.wso2.carbon.policy.mgt.core.dao.PolicyManagerDAOException; import org.wso2.carbon.policy.mgt.core.dao.ProfileDAO; import org.wso2.carbon.policy.mgt.core.dao.ProfileManagerDAOException; import org.wso2.carbon.policy.mgt.core.dao.util.PolicyManagementDAOUtil; -import org.wso2.carbon.policy.mgt.core.util.PolicyManagerUtil; import java.sql.Connection; import java.sql.PreparedStatement; @@ -221,7 +220,7 @@ public class ProfileDAOImpl implements ProfileDAO { Connection conn; PreparedStatement stmt = null; ResultSet resultSet = null; - List profileList = new ArrayList(); + List profileList = new ArrayList<>(); try { //TODO : Fix with TenantID. @@ -259,12 +258,10 @@ public class ProfileDAOImpl implements ProfileDAO { @Override public List getProfilesOfDeviceType(DeviceType deviceType) throws ProfileManagerDAOException { - Connection conn; PreparedStatement stmt = null; ResultSet resultSet = null; - List profileList = new ArrayList(); - + List profileList = new ArrayList<>(); try { conn = this.getConnection(); String query = "SELECT * FROM DM_PROFILE WHERE DEVICE_TYPE_ID = ?"; @@ -283,7 +280,6 @@ public class ProfileDAOImpl implements ProfileDAO { profileList.add(profile); } - } catch (SQLException e) { String msg = "Error occurred while reading the profile list from the database."; log.error(msg, e); @@ -296,12 +292,7 @@ public class ProfileDAOImpl implements ProfileDAO { private Connection getConnection() throws ProfileManagerDAOException { - try { - return PolicyManagementDAOFactory.getConnection(); - } catch (PolicyManagerDAOException e) { - throw new ProfileManagerDAOException("Error occurred while obtaining a connection from the policy " + - "management metadata repository config.datasource", e); - } + return PolicyManagementDAOFactory.getConnection(); } } diff --git a/components/policy-mgt/org.wso2.carbon.policy.mgt.core/src/main/java/org/wso2/carbon/policy/mgt/core/mgt/impl/MonitoringManagerImpl.java b/components/policy-mgt/org.wso2.carbon.policy.mgt.core/src/main/java/org/wso2/carbon/policy/mgt/core/mgt/impl/MonitoringManagerImpl.java index 34a9ad9aaa..09425753fc 100644 --- a/components/policy-mgt/org.wso2.carbon.policy.mgt.core/src/main/java/org/wso2/carbon/policy/mgt/core/mgt/impl/MonitoringManagerImpl.java +++ b/components/policy-mgt/org.wso2.carbon.policy.mgt.core/src/main/java/org/wso2/carbon/policy/mgt/core/mgt/impl/MonitoringManagerImpl.java @@ -21,39 +21,35 @@ 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.PrivilegedCarbonContext; import org.wso2.carbon.device.mgt.common.Device; import org.wso2.carbon.device.mgt.common.DeviceIdentifier; import org.wso2.carbon.device.mgt.common.DeviceManagementException; import org.wso2.carbon.device.mgt.common.operation.mgt.Operation; import org.wso2.carbon.device.mgt.common.operation.mgt.OperationManagementException; import org.wso2.carbon.device.mgt.core.dao.DeviceDAO; -import org.wso2.carbon.device.mgt.core.dao.DeviceManagementDAOException; import org.wso2.carbon.device.mgt.core.dao.DeviceManagementDAOFactory; import org.wso2.carbon.device.mgt.core.operation.mgt.CommandOperation; import org.wso2.carbon.device.mgt.core.service.DeviceManagementProviderService; import org.wso2.carbon.device.mgt.core.service.DeviceManagementProviderServiceImpl; +import org.wso2.carbon.policy.mgt.common.Policy; import org.wso2.carbon.policy.mgt.common.PolicyManagementException; +import org.wso2.carbon.policy.mgt.common.ProfileFeature; import org.wso2.carbon.policy.mgt.common.monitor.ComplianceData; import org.wso2.carbon.policy.mgt.common.monitor.ComplianceDecisionPoint; import org.wso2.carbon.policy.mgt.common.monitor.ComplianceFeature; import org.wso2.carbon.policy.mgt.common.monitor.PolicyComplianceException; -import org.wso2.carbon.policy.mgt.common.Policy; -import org.wso2.carbon.policy.mgt.common.ProfileFeature; import org.wso2.carbon.policy.mgt.common.spi.PolicyMonitoringService; -import org.wso2.carbon.policy.mgt.core.dao.MonitoringDAO; -import org.wso2.carbon.policy.mgt.core.dao.MonitoringDAOException; -import org.wso2.carbon.policy.mgt.core.dao.PolicyDAO; -import org.wso2.carbon.policy.mgt.core.dao.PolicyManagementDAOFactory; -import org.wso2.carbon.policy.mgt.core.dao.PolicyManagerDAOException; +import org.wso2.carbon.policy.mgt.core.dao.*; import org.wso2.carbon.policy.mgt.core.impl.ComplianceDecisionPointImpl; import org.wso2.carbon.policy.mgt.core.internal.PolicyManagementDataHolder; import org.wso2.carbon.policy.mgt.core.mgt.MonitoringManager; import org.wso2.carbon.policy.mgt.core.mgt.PolicyManager; -import org.wso2.carbon.policy.mgt.core.util.PolicyManagerUtil; import java.sql.SQLException; -import java.util.*; +import java.util.ArrayList; +import java.util.HashMap; +import java.util.List; +import java.util.Map; public class MonitoringManagerImpl implements MonitoringManager { diff --git a/components/policy-mgt/org.wso2.carbon.policy.mgt.core/src/main/java/org/wso2/carbon/policy/mgt/core/mgt/impl/PolicyManagerImpl.java b/components/policy-mgt/org.wso2.carbon.policy.mgt.core/src/main/java/org/wso2/carbon/policy/mgt/core/mgt/impl/PolicyManagerImpl.java index 7456d57a8c..71daa41ec8 100644 --- a/components/policy-mgt/org.wso2.carbon.policy.mgt.core/src/main/java/org/wso2/carbon/policy/mgt/core/mgt/impl/PolicyManagerImpl.java +++ b/components/policy-mgt/org.wso2.carbon.policy.mgt.core/src/main/java/org/wso2/carbon/policy/mgt/core/mgt/impl/PolicyManagerImpl.java @@ -278,18 +278,21 @@ public class PolicyManagerImpl implements PolicyManager { } @Override - public Policy addPolicyToDevice(List deviceIdentifierList, - Policy policy) throws PolicyManagementException { + public Policy addPolicyToDevice(List deviceIds, Policy policy) throws PolicyManagementException { + List deviceList = new ArrayList<>(); + DeviceManagementProviderService service = new DeviceManagementProviderServiceImpl(); + for (DeviceIdentifier deviceIdentifier : deviceIds) { + try { + deviceList.add(service.getDevice(deviceIdentifier)); + } catch (DeviceManagementException e) { + throw new PolicyManagementException("Error occurred while adding the policy to device list", e); + } + } try { PolicyManagementDAOFactory.beginTransaction(); if (policy.getId() == 0) { policyDAO.addPolicy(policy); } - List deviceList = new ArrayList<>(); - DeviceManagementProviderService service = new DeviceManagementProviderServiceImpl(); - for (DeviceIdentifier deviceIdentifier : deviceIdentifierList) { - deviceList.add(service.getDevice(deviceIdentifier)); - } policy = policyDAO.addPolicyToDevice(deviceList, policy); PolicyManagementDAOFactory.commitTransaction(); @@ -308,9 +311,6 @@ public class PolicyManagerImpl implements PolicyManager { PolicyManagementDAOFactory.rollbackTransaction(); throw new PolicyManagementException("Error occurred while adding the policy (" + policy.getId() + " - " + policy.getPolicyName() + ")", e); - } catch (DeviceManagementException e) { - PolicyManagementDAOFactory.rollbackTransaction(); - throw new PolicyManagementException("Error occurred while adding the policy to device list", e); } finally { PolicyManagementDAOFactory.closeConnection(); } @@ -614,25 +614,33 @@ public class PolicyManagerImpl implements PolicyManager { public List getPolicyAppliedDevicesIds(int policyId) throws PolicyManagementException { List deviceList = new ArrayList<>(); List deviceIds; + int tenantId = PrivilegedCarbonContext.getThreadLocalCarbonContext().getTenantId(); 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) { throw new PolicyManagementException("Error occurred while getting the device ids related to policy id (" + policyId + ")", e); - } catch (DeviceManagementDAOException 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(); } + + try { + DeviceManagementDAOFactory.openConnection(); + for (int deviceId : deviceIds) { + //TODO FIX ME + deviceList.add(deviceDAO.getDevice(new DeviceIdentifier(Integer.toString(deviceId), ""), tenantId)); + } + } catch (SQLException e) { + throw new PolicyManagementException("Error occurred while opening a connection to the data source", e); + } catch (DeviceManagementDAOException e) { + throw new PolicyManagementException("Error occurred while retrieving device metadata", e); + } finally { + DeviceManagementDAOFactory.closeConnection(); + } + return deviceList; } @@ -680,7 +688,7 @@ public class PolicyManagerImpl implements PolicyManager { Policy policySaved = policyDAO.getAppliedPolicy(deviceId); if (policySaved != null && policySaved.getId() != 0) { - if (policy.getId() != policySaved.getId()){ + if (policy.getId() != policySaved.getId()) { policyDAO.updateEffectivePolicyToDevice(deviceId, policy); } } else { diff --git a/components/policy-mgt/org.wso2.carbon.policy.mgt.core/src/main/java/org/wso2/carbon/policy/mgt/core/mgt/impl/ProfileManagerImpl.java b/components/policy-mgt/org.wso2.carbon.policy.mgt.core/src/main/java/org/wso2/carbon/policy/mgt/core/mgt/impl/ProfileManagerImpl.java index 2dc77739cd..2b0eacb1a2 100644 --- a/components/policy-mgt/org.wso2.carbon.policy.mgt.core/src/main/java/org/wso2/carbon/policy/mgt/core/mgt/impl/ProfileManagerImpl.java +++ b/components/policy-mgt/org.wso2.carbon.policy.mgt.core/src/main/java/org/wso2/carbon/policy/mgt/core/mgt/impl/ProfileManagerImpl.java @@ -143,41 +143,58 @@ public class ProfileManagerImpl implements ProfileManager { public Profile getProfile(int profileId) throws ProfileManagementException { Profile profile; List featureList; - DeviceType deviceType; + DeviceType deviceType = null; try { PolicyManagementDAOFactory.openConnection(); profile = profileDAO.getProfiles(profileId); featureList = featureDAO.getFeaturesForProfile(profileId); - deviceType = deviceTypeDAO.getDeviceType(profile.getDeviceType().getId()); - profile.setProfileFeaturesList(featureList); profile.setDeviceType(deviceType); - } catch (ProfileManagerDAOException e) { throw new ProfileManagementException("Error occurred while getting profile id (" + profileId + ")", e); } catch (FeatureManagerDAOException e) { throw new ProfileManagementException("Error occurred while getting features related profile id (" + profileId + ")", e); - } catch (DeviceManagementDAOException 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(); } + + try { + DeviceManagementDAOFactory.openConnection(); + deviceType = deviceTypeDAO.getDeviceType(profile.getDeviceType().getId()); + profile.setDeviceType(deviceType); + } catch (SQLException e) { + throw new ProfileManagementException("Error occurred while opening a connection to the data source", e); + } catch (DeviceManagementDAOException e) { + throw new ProfileManagementException("Error occurred while getting device type related profile id (" + + profileId + ")", e); + } finally { + DeviceManagementDAOFactory.closeConnection(); + } return profile; } @Override public List getAllProfiles() throws ProfileManagementException { List profileList; + List deviceTypes; + try { + DeviceManagementDAOFactory.openConnection(); + deviceTypes = deviceTypeDAO.getDeviceTypes(); + } catch (SQLException e) { + throw new ProfileManagementException("Error occurred while opening a connection to the data source", e); + } catch (DeviceManagementDAOException e) { + throw new ProfileManagementException("Error occurred while getting device types related to profiles", e); + } finally { + DeviceManagementDAOFactory.closeConnection(); + } try { PolicyManagementDAOFactory.openConnection(); profileList = profileDAO.getAllProfiles(); List featureList = featureDAO.getAllProfileFeatures(); - List deviceTypes = deviceTypeDAO.getDeviceTypes(); for (Profile profile : profileList) { List list = new ArrayList(); @@ -198,8 +215,6 @@ public class ProfileManagerImpl implements ProfileManager { throw new ProfileManagementException("Error occurred while getting profiles", e); } catch (FeatureManagerDAOException e) { throw new ProfileManagementException("Error occurred while getting features related to profiles", e); - } catch (DeviceManagementDAOException 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 { @@ -212,9 +227,19 @@ public class ProfileManagerImpl implements ProfileManager { public List getProfilesOfDeviceType(String deviceTypeName) throws ProfileManagementException { List profileList; List featureList; + DeviceType deviceType; + try { + DeviceManagementDAOFactory.openConnection(); + deviceType = deviceTypeDAO.getDeviceType(deviceTypeName); + } catch (SQLException e) { + throw new ProfileManagementException("Error occurred while opening a connection to the data source", e); + } catch (DeviceManagementDAOException e) { + throw new ProfileManagementException("Error occurred while getting device types", e); + } finally { + DeviceManagementDAOFactory.closeConnection(); + } try { PolicyManagementDAOFactory.openConnection(); - DeviceType deviceType = deviceTypeDAO.getDeviceType(deviceTypeName); profileList = profileDAO.getProfilesOfDeviceType(deviceType); featureList = featureDAO.getAllProfileFeatures(); @@ -226,12 +251,9 @@ public class ProfileManagerImpl implements ProfileManager { } } profile.setProfileFeaturesList(profileFeatureList); - } } catch (ProfileManagerDAOException e) { throw new ProfileManagementException("Error occurred while getting profiles", e); - } catch (DeviceManagementDAOException e) { - throw new ProfileManagementException("Error occurred while getting device types", e); } catch (FeatureManagerDAOException e) { throw new ProfileManagementException("Error occurred while getting profile features types", e); } catch (SQLException e) { diff --git a/components/policy-mgt/org.wso2.carbon.policy.mgt.core/src/test/java/org/wso2/carbon/policy/mgt/core/MonitoringTestCase.java b/components/policy-mgt/org.wso2.carbon.policy.mgt.core/src/test/java/org/wso2/carbon/policy/mgt/core/MonitoringTestCase.java index 986f642e8f..9c0e62e46a 100644 --- a/components/policy-mgt/org.wso2.carbon.policy.mgt.core/src/test/java/org/wso2/carbon/policy/mgt/core/MonitoringTestCase.java +++ b/components/policy-mgt/org.wso2.carbon.policy.mgt.core/src/test/java/org/wso2/carbon/policy/mgt/core/MonitoringTestCase.java @@ -29,7 +29,6 @@ import org.wso2.carbon.device.mgt.common.DeviceManagementException; import org.wso2.carbon.device.mgt.common.operation.mgt.OperationManager; import org.wso2.carbon.device.mgt.core.internal.DeviceManagementDataHolder; import org.wso2.carbon.device.mgt.core.operation.mgt.OperationManagerImpl; -import org.wso2.carbon.device.mgt.core.service.DeviceManagementAdminService; import org.wso2.carbon.device.mgt.core.service.DeviceManagementProviderService; import org.wso2.carbon.device.mgt.core.service.DeviceManagementProviderServiceImpl; import org.wso2.carbon.policy.mgt.common.Policy; @@ -43,7 +42,6 @@ import org.wso2.carbon.policy.mgt.core.mgt.PolicyManager; import org.wso2.carbon.policy.mgt.core.mgt.impl.MonitoringManagerImpl; import org.wso2.carbon.policy.mgt.core.mgt.impl.PolicyManagerImpl; import org.wso2.carbon.policy.mgt.core.services.PolicyMonitoringServiceTest; -import org.wso2.carbon.policy.mgt.core.task.MonitoringTask; import java.util.List; @@ -53,11 +51,12 @@ public class MonitoringTestCase extends BasePolicyManagementDAOTest { private static final String ANDROID = "android"; - DeviceIdentifier identifier = new DeviceIdentifier(); + private DeviceIdentifier identifier = new DeviceIdentifier(); @BeforeClass @Override public void init() throws Exception { + } @Test @@ -77,7 +76,6 @@ public class MonitoringTestCase extends BasePolicyManagementDAOTest { log.debug(device.getDeviceIdentifier() + " ----- D"); } - identifier.setType(ANDROID); identifier.setId(devices.get(0).getDeviceIdentifier()); diff --git a/components/policy-mgt/org.wso2.carbon.policy.mgt.core/src/test/java/org/wso2/carbon/policy/mgt/core/PolicyDAOTestCase.java b/components/policy-mgt/org.wso2.carbon.policy.mgt.core/src/test/java/org/wso2/carbon/policy/mgt/core/PolicyDAOTestCase.java index e14ea1a1b3..ca85ab1c98 100644 --- a/components/policy-mgt/org.wso2.carbon.policy.mgt.core/src/test/java/org/wso2/carbon/policy/mgt/core/PolicyDAOTestCase.java +++ b/components/policy-mgt/org.wso2.carbon.policy.mgt.core/src/test/java/org/wso2/carbon/policy/mgt/core/PolicyDAOTestCase.java @@ -21,15 +21,16 @@ import org.apache.commons.logging.Log; import org.apache.commons.logging.LogFactory; 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.DeviceIdentifier; -import org.wso2.carbon.device.mgt.common.DeviceManagementException; +import org.wso2.carbon.device.mgt.common.*; import org.wso2.carbon.device.mgt.common.Feature; import org.wso2.carbon.device.mgt.core.dao.*; import org.wso2.carbon.device.mgt.core.dto.DeviceType; import org.wso2.carbon.device.mgt.core.service.DeviceManagementProviderService; import org.wso2.carbon.device.mgt.core.service.DeviceManagementProviderServiceImpl; import org.wso2.carbon.policy.mgt.common.*; +import org.wso2.carbon.policy.mgt.common.FeatureManagementException; +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.impl.PolicyAdministratorPointImpl; import org.wso2.carbon.policy.mgt.core.internal.PolicyManagementDataHolder; import org.wso2.carbon.policy.mgt.core.mgt.FeatureManager; @@ -40,6 +41,7 @@ import org.wso2.carbon.policy.mgt.core.mgt.impl.PolicyManagerImpl; import org.wso2.carbon.policy.mgt.core.mgt.impl.ProfileManagerImpl; import org.wso2.carbon.policy.mgt.core.util.*; +import java.sql.SQLException; import java.util.ArrayList; import java.util.List; import java.util.Properties; @@ -64,8 +66,19 @@ public class PolicyDAOTestCase extends BasePolicyManagementDAOTest { @Test public void addDeviceType() throws DeviceManagementDAOException { - DeviceTypeDAO deviceTypeDAO = DeviceManagementDAOFactory.getDeviceTypeDAO(); - deviceTypeDAO.addDeviceType(DeviceTypeCreator.getDeviceType()); + try { + DeviceManagementDAOFactory.beginTransaction(); + DeviceTypeDAO deviceTypeDAO = DeviceManagementDAOFactory.getDeviceTypeDAO(); + deviceTypeDAO.addDeviceType(DeviceTypeCreator.getDeviceType()); + } catch (DeviceManagementDAOException e) { + DeviceManagementDAOFactory.rollbackTransaction(); + throw new DeviceManagementDAOException("Error occurred while adding dummy device type", e); + } catch (TransactionManagementException e) { + throw new DeviceManagementDAOException("Error occurred while initiating a transaction to add dummy " + + "device type", e); + } finally { + DeviceManagementDAOFactory.closeConnection(); + } } @@ -74,23 +87,43 @@ public class PolicyDAOTestCase extends BasePolicyManagementDAOTest { DeviceDAO deviceDAO = DeviceManagementDAOFactory.getDeviceDAO(); EnrolmentDAO enrolmentDAO = DeviceManagementDAOFactory.getEnrollmentDAO(); + DeviceType type = DeviceTypeCreator.getDeviceType(); devices = DeviceCreator.getDeviceList(type); - for (Device device : devices) { - int id = deviceDAO.addDevice(type.getId(), device, -1234); - enrolmentDAO.addEnrollment(id, device.getEnrolmentInfo(), -1234); - } - - List devices = deviceDAO.getDevices(-1234); - log.debug("--- Printing device taken by calling the device dao layer by tenant id."); - for (Device device : devices) { - log.debug(device.getDeviceIdentifier()); + try { + DeviceManagementDAOFactory.beginTransaction(); + for (Device device : devices) { + int id = deviceDAO.addDevice(type.getId(), device, -1234); + enrolmentDAO.addEnrollment(id, device.getEnrolmentInfo(), -1234); + } + } catch (DeviceManagementDAOException e) { + DeviceManagementDAOFactory.rollbackTransaction(); + throw new DeviceManagementException("Error occurred while adding dummy device info", e); + } catch (TransactionManagementException e) { + throw new DeviceManagementException("Error occurred while initiating a transaction to add dummy " + + "device info", e); + } finally { + DeviceManagementDAOFactory.closeConnection(); } log.debug("--- Printing device taken by calling the device dao layer by tenant id and device type."); - List devices2 = deviceDAO.getDevices("android", -1234); + List devices2 = null; + try { + DeviceManagementDAOFactory.openConnection(); + List devices = deviceDAO.getDevices(-1234); + + log.debug("--- Printing device taken by calling the device dao layer by tenant id."); + for (Device device : devices) { + log.debug(device.getDeviceIdentifier()); + } + devices2 = deviceDAO.getDevices("android", -1234); + } catch (SQLException e) { + throw new DeviceManagementException("Error occurred while opening a connection to the data source", e); + } finally { + DeviceManagementDAOFactory.closeConnection(); + } for (Device device : devices2) { log.debug(device.getDeviceIdentifier()); From 044995ed971f257dac016950ce8f3bb5bd83e681 Mon Sep 17 00:00:00 2001 From: geethkokila Date: Tue, 1 Sep 2015 15:03:09 +0530 Subject: [PATCH 2/2] Adding the activate method when policies are added. --- .../policy/mgt/core/mgt/impl/MonitoringManagerImpl.java | 9 ++++----- .../policy/mgt/core/mgt/impl/PolicyManagerImpl.java | 4 +++- 2 files changed, 7 insertions(+), 6 deletions(-) diff --git a/components/policy-mgt/org.wso2.carbon.policy.mgt.core/src/main/java/org/wso2/carbon/policy/mgt/core/mgt/impl/MonitoringManagerImpl.java b/components/policy-mgt/org.wso2.carbon.policy.mgt.core/src/main/java/org/wso2/carbon/policy/mgt/core/mgt/impl/MonitoringManagerImpl.java index 6b52b04e7a..3a0dd3d78d 100644 --- a/components/policy-mgt/org.wso2.carbon.policy.mgt.core/src/main/java/org/wso2/carbon/policy/mgt/core/mgt/impl/MonitoringManagerImpl.java +++ b/components/policy-mgt/org.wso2.carbon.policy.mgt.core/src/main/java/org/wso2/carbon/policy/mgt/core/mgt/impl/MonitoringManagerImpl.java @@ -59,7 +59,7 @@ import java.util.Map; public class MonitoringManagerImpl implements MonitoringManager { private PolicyDAO policyDAO; -// private DeviceDAO deviceDAO; + // private DeviceDAO deviceDAO; private DeviceTypeDAO deviceTypeDAO; private MonitoringDAO monitoringDAO; private ComplianceDecisionPoint complianceDecisionPoint; @@ -226,8 +226,6 @@ public class MonitoringManagerImpl implements MonitoringManager { @Override public void addMonitoringOperation(List devices) throws PolicyComplianceException { - ComplianceDecisionPoint decisionPoint = new ComplianceDecisionPointImpl(); - //int tenantId = PolicyManagerUtil.getTenantId(); Map deviceIds = new HashMap<>(); List complianceDatas; @@ -327,10 +325,11 @@ public class MonitoringManagerImpl implements MonitoringManager { } } - // TODO : This should be uncommented, this is to mark the device as unreachable, But given the current implementation - // we are not able to do so. + // TODO : This should be uncommented, this is to mark the device as unreachable, But given the current + // implementation we are not able to do so. // if(!deviceToMarkUnreachable.isEmpty()) { +// ComplianceDecisionPoint decisionPoint = new ComplianceDecisionPointImpl(); // decisionPoint.setDevicesAsUnreachable(this.getDeviceIdentifiersFromDevices( // new ArrayList<>(deviceToMarkUnreachable.values()))); // } diff --git a/components/policy-mgt/org.wso2.carbon.policy.mgt.core/src/main/java/org/wso2/carbon/policy/mgt/core/mgt/impl/PolicyManagerImpl.java b/components/policy-mgt/org.wso2.carbon.policy.mgt.core/src/main/java/org/wso2/carbon/policy/mgt/core/mgt/impl/PolicyManagerImpl.java index 53c322236f..ffc3373fba 100644 --- a/components/policy-mgt/org.wso2.carbon.policy.mgt.core/src/main/java/org/wso2/carbon/policy/mgt/core/mgt/impl/PolicyManagerImpl.java +++ b/components/policy-mgt/org.wso2.carbon.policy.mgt.core/src/main/java/org/wso2/carbon/policy/mgt/core/mgt/impl/PolicyManagerImpl.java @@ -46,7 +46,6 @@ public class PolicyManagerImpl implements PolicyManager { private ProfileDAO profileDAO; private FeatureDAO featureDAO; private ProfileManager profileManager; - private PolicyCacheManager policyCacheManager; private static Log log = LogFactory.getLog(PolicyManagerImpl.class); public PolicyManagerImpl() { @@ -105,6 +104,9 @@ public class PolicyManagerImpl implements PolicyManager { policyDAO.addPolicyCriteriaProperties(policy.getPolicyCriterias()); } + if(policy.isActive()){ + policyDAO.activatePolicy(policy.getId()); + } PolicyManagementDAOFactory.commitTransaction(); } catch (PolicyManagerDAOException e) {