From e7f02365a8c8dd1506509935e751a457f35a188a Mon Sep 17 00:00:00 2001 From: Dilshan Edirisuriya Date: Tue, 28 Jul 2015 16:42:20 +0530 Subject: [PATCH 1/4] Importing axis2.transport.mail --- components/device-mgt/org.wso2.carbon.device.mgt.core/pom.xml | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/components/device-mgt/org.wso2.carbon.device.mgt.core/pom.xml b/components/device-mgt/org.wso2.carbon.device.mgt.core/pom.xml index 6194adc77dc..fd8d7301426 100644 --- a/components/device-mgt/org.wso2.carbon.device.mgt.core/pom.xml +++ b/components/device-mgt/org.wso2.carbon.device.mgt.core/pom.xml @@ -76,7 +76,8 @@ org.wso2.carbon.identity.oauth.stub.dto, org.wso2.carbon.ndatasource.core, org.wso2.carbon.apimgt.impl, - org.wso2.carbon.ndatasource.core + org.wso2.carbon.ndatasource.core, + org.apache.axis2.transport.mail !org.wso2.carbon.device.mgt.core.internal, From 134b4ae53c7ce9ce65318a5e67418c0420bcd89e Mon Sep 17 00:00:00 2001 From: prabathabey Date: Tue, 28 Jul 2015 17:25:41 +0530 Subject: [PATCH 2/4] Code cleanup --- .../operation/mgt/OperationManagerImpl.java | 122 ++++++-------- .../mgt/dao/impl/CommandOperationDAOImpl.java | 57 ++----- .../mgt/dao/impl/ConfigOperationDAOImpl.java | 78 ++++----- .../mgt/dao/impl/OperationDAOImpl.java | 149 ++++++------------ .../mgt/dao/impl/PolicyOperationDAOImpl.java | 65 +++----- 5 files changed, 162 insertions(+), 309 deletions(-) diff --git a/components/device-mgt/org.wso2.carbon.device.mgt.core/src/main/java/org/wso2/carbon/device/mgt/core/operation/mgt/OperationManagerImpl.java b/components/device-mgt/org.wso2.carbon.device.mgt.core/src/main/java/org/wso2/carbon/device/mgt/core/operation/mgt/OperationManagerImpl.java index 419fe5e9baf..c8235a3f210 100644 --- a/components/device-mgt/org.wso2.carbon.device.mgt.core/src/main/java/org/wso2/carbon/device/mgt/core/operation/mgt/OperationManagerImpl.java +++ b/components/device-mgt/org.wso2.carbon.device.mgt.core/src/main/java/org/wso2/carbon/device/mgt/core/operation/mgt/OperationManagerImpl.java @@ -22,7 +22,6 @@ import org.apache.commons.logging.Log; import org.apache.commons.logging.LogFactory; import org.wso2.carbon.context.CarbonContext; import org.wso2.carbon.device.mgt.common.DeviceIdentifier; -import org.wso2.carbon.device.mgt.common.DeviceManagementException; import org.wso2.carbon.device.mgt.common.EnrolmentInfo; import org.wso2.carbon.device.mgt.common.operation.mgt.Operation; import org.wso2.carbon.device.mgt.common.operation.mgt.OperationManagementException; @@ -30,7 +29,6 @@ import org.wso2.carbon.device.mgt.common.operation.mgt.OperationManager; 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.dao.DeviceTypeDAO; import org.wso2.carbon.device.mgt.core.operation.mgt.dao.OperationDAO; import org.wso2.carbon.device.mgt.core.operation.mgt.dao.OperationManagementDAOException; import org.wso2.carbon.device.mgt.core.operation.mgt.dao.OperationManagementDAOFactory; @@ -58,7 +56,6 @@ public class OperationManagerImpl implements OperationManager { private OperationMappingDAO operationMappingDAO; private OperationDAO operationDAO; private DeviceDAO deviceDAO; - private DeviceTypeDAO deviceTypeDAO; public OperationManagerImpl() { commandOperationDAO = OperationManagementDAOFactory.getCommandOperationDAO(); @@ -68,7 +65,6 @@ public class OperationManagerImpl implements OperationManager { operationMappingDAO = OperationManagementDAOFactory.getOperationMappingDAO(); operationDAO = OperationManagementDAOFactory.getOperationDAO(); deviceDAO = DeviceManagementDAOFactory.getDeviceDAO(); - deviceTypeDAO = DeviceManagementDAOFactory.getDeviceTypeDAO(); } @Override @@ -79,7 +75,7 @@ public class OperationManagerImpl implements OperationManager { log.debug("operation:[" + operation.toString() + "]"); for (DeviceIdentifier deviceIdentifier : deviceIds) { log.debug("device identifier id:[" + deviceIdentifier.getId() + "] type:[" + deviceIdentifier.getType() - + "]"); + + "]"); } } try { @@ -95,8 +91,8 @@ public class OperationManagerImpl implements OperationManager { enrolmentId = deviceDAO.getEnrolmentByStatus(deviceId, EnrolmentInfo.Status.ACTIVE, tenantId); if (enrolmentId < 0) { String errorMsg = "The operation not added for device.The device not found for " + - "device Identifier type -'" + deviceId.getType() + "' and device Id '" + - deviceId.getId(); + "device Identifier type -'" + deviceId.getType() + "' and device Id '" + + deviceId.getId(); log.error(errorMsg); } else { operationMappingDAO.addOperationMapping(operationId, enrolmentId); @@ -123,11 +119,8 @@ public class OperationManagerImpl implements OperationManager { @Override public List getOperations(DeviceIdentifier deviceId) throws OperationManagementException { - - - List operations = new ArrayList(); - int enrolmentId = -1; - + int enrolmentId; + List operations = new ArrayList<>(); try { OperationManagementDAOFactory.getConnection(); @@ -136,25 +129,22 @@ public class OperationManagerImpl implements OperationManager { if (enrolmentId < 0) { throw new OperationManagementException("Device not found for given device " + - "Identifier:" + deviceId.getId() + " and given type" + deviceId.getType()); + "Identifier:" + deviceId.getId() + " and given type" + deviceId.getType()); } - List operationList = operationDAO - .getOperationsForDevice(enrolmentId); - - Operation operation; + List operationList = + operationDAO.getOperationsForDevice(enrolmentId); for (org.wso2.carbon.device.mgt.core.dto.operation.mgt.Operation dtoOperation : operationList) { - operation = OperationDAOUtil.convertOperation(dtoOperation); + Operation operation = OperationDAOUtil.convertOperation(dtoOperation); operations.add(operation); } return operations; } catch (OperationManagementDAOException e) { throw new OperationManagementException("Error occurred while retrieving the list of " + - "operations assigned for '" + deviceId.getType() + "' device '" + deviceId.getId() - + "'", e); + "operations assigned for '" + deviceId.getType() + "' device '" + deviceId.getId() + "'", e); } catch (DeviceManagementDAOException e) { throw new OperationManagementException("Error occurred while retrieving metadata of '" + - deviceId.getType() + "' device carrying the identifier '" + deviceId.getId() + "'"); + deviceId.getType() + "' device carrying the identifier '" + deviceId.getId() + "'"); } finally { try { OperationManagementDAOFactory.closeConnection(); @@ -168,18 +158,13 @@ public class OperationManagerImpl implements OperationManager { @Override public List getPendingOperations( DeviceIdentifier deviceId) throws OperationManagementException { - if (log.isDebugEnabled()) { log.debug("Device identifier id:[" + deviceId.getId() + "] type:[" + deviceId.getType() - + "]"); + + "]"); } - - int enrolmentId = -1; - List operations = new ArrayList(); - - List dtoOperationList = - new ArrayList(); - + int enrolmentId; + List operations = new ArrayList<>(); + List dtoOperationList = new ArrayList<>(); try { OperationManagementDAOFactory.getConnection(); @@ -188,7 +173,7 @@ public class OperationManagerImpl implements OperationManager { if (enrolmentId < 0) { throw new OperationManagementException("Device not found for given device " + - "Identifier:" + deviceId.getId() + " and given type:" + deviceId.getType()); + "Identifier:" + deviceId.getId() + " and given type:" + deviceId.getType()); } dtoOperationList.addAll(commandOperationDAO.getOperationsByDeviceAndStatus( @@ -212,14 +197,12 @@ public class OperationManagerImpl implements OperationManager { return operations; } catch (OperationManagementDAOException e) { throw new OperationManagementException("Error occurred while retrieving the list of " + - "pending operations assigned for '" + deviceId.getType() + "' device '" + - deviceId.getId() + "'", e); + "pending operations assigned for '" + deviceId.getType() + "' device '" + + deviceId.getId() + "'", e); } catch (DeviceManagementDAOException e) { - String errorMsg = "Error occurred while retrieving the device " + - "for device Identifier type -'" + deviceId.getType() + "' and device Id '" - + deviceId.getId(); - log.error(errorMsg, e); - throw new OperationManagementException(errorMsg, e); + throw new OperationManagementException("Error occurred while retrieving the device " + + "for device Identifier type -'" + deviceId.getType() + "' and device Id '" + + deviceId.getId() + "'", e); } finally { try { OperationManagementDAOFactory.closeConnection(); @@ -233,10 +216,10 @@ public class OperationManagerImpl implements OperationManager { public Operation getNextPendingOperation(DeviceIdentifier deviceId) throws OperationManagementException { if (log.isDebugEnabled()) { log.debug("device identifier id:[" + deviceId.getId() + "] type:[" + deviceId.getType() - + "]"); + + "]"); } Operation operation = null; - int enrolmentId = -1; + int enrolmentId; try { OperationManagementDAOFactory.getConnection(); @@ -245,7 +228,7 @@ public class OperationManagerImpl implements OperationManager { if (enrolmentId < 0) { throw new OperationManagementException("Device not found for given device " + - "Identifier:" + deviceId.getId() + " and given type" + deviceId.getType()); + "Identifier:" + deviceId.getId() + " and given type" + deviceId.getType()); } org.wso2.carbon.device.mgt.core.dto.operation.mgt.Operation dtoOperation = operationDAO .getNextOperation(enrolmentId); @@ -275,7 +258,7 @@ public class OperationManagerImpl implements OperationManager { throw new OperationManagementException("Error occurred while retrieving next pending operation", e); } catch (DeviceManagementDAOException e) { throw new OperationManagementException("Error occurred while retrieving the device " + - "for device Identifier type -'" + deviceId.getType() + "' and device Id '" + deviceId.getId(), e); + "for device Identifier type -'" + deviceId.getType() + "' and device Id '" + deviceId.getId(), e); } finally { try { OperationManagementDAOFactory.closeConnection(); @@ -302,8 +285,8 @@ public class OperationManagerImpl implements OperationManager { if (operation.getStatus() != null) { OperationManagementDAOFactory.beginTransaction(); operationDAO.updateOperationStatus(enrolmentId, operationId, - org.wso2.carbon.device.mgt.core.dto.operation.mgt.Operation.Status - .valueOf(operation.getStatus().toString())); + org.wso2.carbon.device.mgt.core.dto.operation.mgt.Operation.Status + .valueOf(operation.getStatus().toString())); } @@ -319,7 +302,7 @@ public class OperationManagerImpl implements OperationManager { log.warn("Error occurred while roll-backing the update operation transaction", e1); } throw new OperationManagementException("Error occurred while updating the operation: " + operationId + - " status:" + operation.getStatus(), e); + " status:" + operation.getStatus(), e); } catch (DeviceManagementDAOException e) { try { OperationManagementDAOFactory.rollbackTransaction(); @@ -327,7 +310,7 @@ public class OperationManagerImpl implements OperationManager { log.warn("Error occurred while roll-backing the update operation transaction", e1); } throw new OperationManagementException("Error occurred while fetching the device for device identifier: " + - deviceId.getId() + "type:" + deviceId.getType(), e); + deviceId.getId() + "type:" + deviceId.getType(), e); } finally { try { OperationManagementDAOFactory.closeConnection(); @@ -365,14 +348,13 @@ public class OperationManagerImpl implements OperationManager { @Override public Operation getOperationByDeviceAndOperationId(DeviceIdentifier deviceId, int operationId) throws OperationManagementException { - int enrolmentId = -1; + int enrolmentId; Operation operation; if (log.isDebugEnabled()) { log.debug("Operation Id:" + operationId + " Device Type:" + deviceId.getType() + " Device Identifier:" + - deviceId.getId()); + deviceId.getId()); } - try { OperationManagementDAOFactory.getConnection(); @@ -380,7 +362,7 @@ public class OperationManagerImpl implements OperationManager { enrolmentId = deviceDAO.getEnrolmentByStatus(deviceId, EnrolmentInfo.Status.ACTIVE, tenantId); if (enrolmentId < 0) { throw new OperationManagementException("Device not found for given device identifier:" + - deviceId.getId() + " type:" + deviceId.getType()); + deviceId.getId() + " type:" + deviceId.getType()); } org.wso2.carbon.device.mgt.core.dto.operation.mgt.Operation dtoOperation = operationDAO .getOperationByDeviceAndId(enrolmentId, operationId); @@ -396,29 +378,27 @@ public class OperationManagerImpl implements OperationManager { .equals(org.wso2.carbon.device.mgt.core.dto.operation.mgt.Operation.Type.CONFIG)) { dtoOperation = configOperationDAO.getOperation(dtoOperation.getId()); } else if (dtoOperation.getType().equals(org.wso2.carbon.device.mgt.core.dto.operation.mgt.Operation.Type - .PROFILE)) { + .PROFILE)) { dtoOperation = profileOperationDAO.getOperation(dtoOperation.getId()); } else if (dtoOperation.getType().equals(org.wso2.carbon.device.mgt.core.dto.operation.mgt.Operation.Type - .POLICY)) { + .POLICY)) { dtoOperation = policyOperationDAO.getOperation(dtoOperation.getId()); } if (dtoOperation == null) { throw new OperationManagementException("Operation not found for operation Id:" + operationId + - " device id:" + deviceId.getId()); + " device id:" + deviceId.getId()); } operation = OperationDAOUtil.convertOperation(dtoOperation); } catch (OperationManagementDAOException e) { throw new OperationManagementException("Error occurred while retrieving the list of " + - "operations assigned for '" + deviceId.getType() + "' device '" + deviceId.getId() - + "'", e); + "operations assigned for '" + deviceId.getType() + "' device '" + deviceId.getId() + + "'", e); } catch (DeviceManagementDAOException e) { - String errorMsg = "Error occurred while retrieving the device " + - "for device Identifier type -'" + deviceId.getType() + "' and device Id '" - + deviceId.getId(); - log.error(errorMsg, e); - throw new OperationManagementException(errorMsg, e); + throw new OperationManagementException("Error occurred while retrieving the device " + + "for device Identifier type -'" + deviceId.getType() + "' and device Id '" + + deviceId.getId() + "'", e); } finally { try { OperationManagementDAOFactory.closeConnection(); @@ -432,9 +412,9 @@ public class OperationManagerImpl implements OperationManager { @Override public List getOperationsByDeviceAndStatus( DeviceIdentifier deviceId, Operation.Status status) throws OperationManagementException { - List operations = new ArrayList(); + List operations = new ArrayList<>(); List dtoOperationList = - new ArrayList(); + new ArrayList<>(); try { OperationManagementDAOFactory.getConnection(); @@ -443,7 +423,7 @@ public class OperationManagerImpl implements OperationManager { if (enrolmentId < 0) { throw new OperationManagementException("Device not found for device id:" + deviceId.getId() + " " + - "type:" + deviceId.getType()); + "type:" + deviceId.getType()); } org.wso2.carbon.device.mgt.core.dto.operation.mgt.Operation.Status dtoOpStatus = org.wso2.carbon.device @@ -452,15 +432,15 @@ public class OperationManagerImpl implements OperationManager { dtoOperationList.addAll( configOperationDAO.getOperationsByDeviceAndStatus(enrolmentId, - org.wso2.carbon.device.mgt.core.dto.operation.mgt.Operation.Status.PENDING)); + org.wso2.carbon.device.mgt.core.dto.operation.mgt.Operation.Status.PENDING)); dtoOperationList.addAll( profileOperationDAO.getOperationsByDeviceAndStatus(enrolmentId, - org.wso2.carbon.device.mgt.core.dto.operation.mgt.Operation.Status.PENDING)); + org.wso2.carbon.device.mgt.core.dto.operation.mgt.Operation.Status.PENDING)); dtoOperationList.addAll( policyOperationDAO.getOperationsByDeviceAndStatus(enrolmentId, - org.wso2.carbon.device.mgt.core.dto.operation.mgt.Operation.Status.PENDING)); + org.wso2.carbon.device.mgt.core.dto.operation.mgt.Operation.Status.PENDING)); Operation operation; @@ -471,11 +451,11 @@ public class OperationManagerImpl implements OperationManager { return operations; } catch (OperationManagementDAOException e) { throw new OperationManagementException("Error occurred while retrieving the list of " + - "operations assigned for '" + deviceId.getType() + "' device '" + - deviceId.getId() + "' and status:" + status.toString(), e); + "operations assigned for '" + deviceId.getType() + "' device '" + + deviceId.getId() + "' and status:" + status.toString(), e); } catch (DeviceManagementDAOException e) { throw new OperationManagementException("Error occurred while retrieving the device " + - "for device Identifier type -'" + deviceId.getType() + "' and device Id '" + deviceId.getId(), e); + "for device Identifier type -'" + deviceId.getType() + "' and device Id '" + deviceId.getId(), e); } finally { try { OperationManagementDAOFactory.closeConnection(); @@ -508,10 +488,10 @@ public class OperationManagerImpl implements OperationManager { .equals(org.wso2.carbon.device.mgt.core.dto.operation.mgt.Operation.Type.CONFIG)) { dtoOperation = configOperationDAO.getOperation(dtoOperation.getId()); } else if (dtoOperation.getType().equals(org.wso2.carbon.device.mgt.core.dto.operation.mgt.Operation.Type - .PROFILE)) { + .PROFILE)) { dtoOperation = profileOperationDAO.getOperation(dtoOperation.getId()); } else if (dtoOperation.getType().equals(org.wso2.carbon.device.mgt.core.dto.operation.mgt.Operation.Type - .POLICY)) { + .POLICY)) { dtoOperation = policyOperationDAO.getOperation(dtoOperation.getId()); } diff --git a/components/device-mgt/org.wso2.carbon.device.mgt.core/src/main/java/org/wso2/carbon/device/mgt/core/operation/mgt/dao/impl/CommandOperationDAOImpl.java b/components/device-mgt/org.wso2.carbon.device.mgt.core/src/main/java/org/wso2/carbon/device/mgt/core/operation/mgt/dao/impl/CommandOperationDAOImpl.java index 82514bfa8ee..ccc5ce811fd 100644 --- a/components/device-mgt/org.wso2.carbon.device.mgt.core/src/main/java/org/wso2/carbon/device/mgt/core/operation/mgt/dao/impl/CommandOperationDAOImpl.java +++ b/components/device-mgt/org.wso2.carbon.device.mgt.core/src/main/java/org/wso2/carbon/device/mgt/core/operation/mgt/dao/impl/CommandOperationDAOImpl.java @@ -18,8 +18,6 @@ */ package org.wso2.carbon.device.mgt.core.operation.mgt.dao.impl; -import org.apache.commons.logging.Log; -import org.apache.commons.logging.LogFactory; import org.wso2.carbon.device.mgt.core.dto.operation.mgt.CommandOperation; import org.wso2.carbon.device.mgt.core.dto.operation.mgt.Operation; import org.wso2.carbon.device.mgt.core.operation.mgt.dao.OperationManagementDAOException; @@ -35,15 +33,11 @@ import java.util.List; public class CommandOperationDAOImpl extends OperationDAOImpl { - private static final Log log = LogFactory.getLog(CommandOperationDAOImpl.class); - @Override public int addOperation(Operation operation) throws OperationManagementDAOException { - int operationId = super.addOperation(operation); CommandOperation commandOp = (CommandOperation) operation; PreparedStatement stmt = null; - try { Connection conn = OperationManagementDAOFactory.getConnection(); stmt = conn.prepareStatement("INSERT INTO DM_COMMAND_OPERATION(OPERATION_ID, ENABLED) VALUES(?, ?)"); @@ -60,17 +54,14 @@ public class CommandOperationDAOImpl extends OperationDAOImpl { @Override public void updateOperation(Operation operation) throws OperationManagementDAOException { - PreparedStatement stmt = null; try { Connection connection = OperationManagementDAOFactory.getConnection(); stmt = connection.prepareStatement( "UPDATE DM_COMMAND_OPERATION O SET O.ENABLED=? WHERE O.OPERATION_ID=?"); - stmt.setBoolean(1, operation.isEnabled()); stmt.setInt(2, operation.getId()); stmt.executeUpdate(); - } catch (SQLException e) { throw new OperationManagementDAOException("Error occurred while adding operation metadata", e); } finally { @@ -81,12 +72,11 @@ public class CommandOperationDAOImpl extends OperationDAOImpl { @Override public void deleteOperation(int id) throws OperationManagementDAOException { - super.deleteOperation(id); PreparedStatement stmt = null; try { Connection connection = OperationManagementDAOFactory.getConnection(); - stmt = connection.prepareStatement("DELETE DM_COMMAND_OPERATION WHERE OPERATION_ID=?"); + stmt = connection.prepareStatement("DELETE DM_COMMAND_OPERATION WHERE OPERATION_ID = ?"); stmt.setInt(1, id); stmt.executeUpdate(); } catch (SQLException e) { @@ -98,30 +88,23 @@ public class CommandOperationDAOImpl extends OperationDAOImpl { } public CommandOperation getOperation(int id) throws OperationManagementDAOException { - PreparedStatement stmt = null; ResultSet rs = null; CommandOperation commandOperation = null; - try { Connection conn = OperationManagementDAOFactory.getConnection(); String sql = "SELECT OPERATION_ID, ENABLED FROM DM_COMMAND_OPERATION WHERE OPERATION_ID=?"; - stmt = conn.prepareStatement(sql); stmt.setInt(1, id); rs = stmt.executeQuery(); if (rs.next()) { commandOperation = new CommandOperation(); - commandOperation.setEnabled(rs.getInt("ENABLED") == 0 ? false : true); + commandOperation.setEnabled(rs.getInt("ENABLED") != 0); } - } catch (SQLException e) { - String errorMsg = "SQL Error occurred while retrieving the command operation object " + "available for " + - "the id '" - + id; - log.error(errorMsg, e); - throw new OperationManagementDAOException(errorMsg, e); + throw new OperationManagementDAOException("SQL Error occurred while retrieving the command operation " + + "object available for the id '" + id, e); } finally { OperationManagementDAOUtil.cleanupResources(stmt, rs); OperationManagementDAOFactory.closeConnection(); @@ -132,23 +115,19 @@ public class CommandOperationDAOImpl extends OperationDAOImpl { @Override public List getOperationsByDeviceAndStatus(int enrolmentId, Operation.Status status) throws OperationManagementDAOException { - PreparedStatement stmt = null; ResultSet rs = null; Operation operation; - List operationList = new ArrayList(); - List commandOperationList = new ArrayList(); - - CommandOperation commandOperation = null; + List operations = new ArrayList<>(); + List commandOperations = new ArrayList<>(); + CommandOperation commandOperation; try { Connection conn = OperationManagementDAOFactory.getConnection(); - String sql = "Select co.OPERATION_ID,ENABLED from DM_COMMAND_OPERATION co " + - "INNER JOIN " + - "(Select * From DM_ENROLMENT_OPERATION_MAPPING WHERE ENROLMENT_ID=? " + + String sql = "SELECT co.OPERATION_ID,ENABLED FROM DM_COMMAND_OPERATION co " + + "INNER JOIN (SELECT * FROM DM_ENROLMENT_OPERATION_MAPPING WHERE ENROLMENT_ID=? " + "AND STATUS=? ) dm ON dm.OPERATION_ID = co.OPERATION_ID"; - stmt = conn.prepareStatement(sql); stmt.setInt(1, enrolmentId); stmt.setString(2, status.toString()); @@ -156,27 +135,25 @@ public class CommandOperationDAOImpl extends OperationDAOImpl { rs = stmt.executeQuery(); while (rs.next()) { commandOperation = new CommandOperation(); - commandOperation.setEnabled(rs.getInt("ENABLED") == 0 ? false : true); + commandOperation.setEnabled(rs.getInt("ENABLED") != 0); commandOperation.setId(rs.getInt("OPERATION_ID")); - commandOperationList.add(commandOperation); + commandOperations.add(commandOperation); } - for(CommandOperation cmOperation:commandOperationList){ + for(CommandOperation cmOperation : commandOperations){ operation = super.getOperation(cmOperation.getId()); operation.setEnabled(cmOperation.isEnabled()); operation.setStatus(status); - operationList.add(operation); + operations.add(operation); } - } catch (SQLException e) { - String errorMsg = "SQL error occurred while retrieving the operation available for the device'" + enrolmentId + - "' with status '" + status.toString(); - log.error(errorMsg); - throw new OperationManagementDAOException(errorMsg, e); + throw new OperationManagementDAOException("SQL error occurred while retrieving the operation available " + + "for the device'" + enrolmentId + "' with status '" + status.toString(), e); } finally { OperationManagementDAOUtil.cleanupResources(stmt, rs); OperationManagementDAOFactory.closeConnection(); } - return operationList; + return operations; } + } diff --git a/components/device-mgt/org.wso2.carbon.device.mgt.core/src/main/java/org/wso2/carbon/device/mgt/core/operation/mgt/dao/impl/ConfigOperationDAOImpl.java b/components/device-mgt/org.wso2.carbon.device.mgt.core/src/main/java/org/wso2/carbon/device/mgt/core/operation/mgt/dao/impl/ConfigOperationDAOImpl.java index 48e0c5f3a83..c05e80fce6d 100644 --- a/components/device-mgt/org.wso2.carbon.device.mgt.core/src/main/java/org/wso2/carbon/device/mgt/core/operation/mgt/dao/impl/ConfigOperationDAOImpl.java +++ b/components/device-mgt/org.wso2.carbon.device.mgt.core/src/main/java/org/wso2/carbon/device/mgt/core/operation/mgt/dao/impl/ConfigOperationDAOImpl.java @@ -21,10 +21,8 @@ package org.wso2.carbon.device.mgt.core.operation.mgt.dao.impl; import org.apache.commons.logging.Log; import org.apache.commons.logging.LogFactory; -import org.osgi.service.component.annotations.ConfigurationPolicy; import org.wso2.carbon.device.mgt.core.dto.operation.mgt.ConfigOperation; import org.wso2.carbon.device.mgt.core.dto.operation.mgt.Operation; -import org.wso2.carbon.device.mgt.core.dto.operation.mgt.PolicyOperation; import org.wso2.carbon.device.mgt.core.operation.mgt.dao.OperationManagementDAOException; import org.wso2.carbon.device.mgt.core.operation.mgt.dao.OperationManagementDAOFactory; import org.wso2.carbon.device.mgt.core.operation.mgt.dao.OperationManagementDAOUtil; @@ -43,12 +41,11 @@ public class ConfigOperationDAOImpl extends OperationDAOImpl { @Override public int addOperation(Operation operation) throws OperationManagementDAOException { - int operationId = super.addOperation(operation); PreparedStatement stmt = null; try { Connection conn = OperationManagementDAOFactory.getConnection(); - stmt = conn.prepareStatement("INSERT INTO DM_CONFIG_OPERATION(OPERATION_ID, OPERATION_CONFIG) VALUES(?,?)"); + stmt = conn.prepareStatement("INSERT INTO DM_CONFIG_OPERATION(OPERATION_ID, OPERATION_CONFIG) VALUES(?, ?)"); stmt.setInt(1, operationId); stmt.setObject(2, operation); stmt.executeUpdate(); @@ -62,12 +59,11 @@ public class ConfigOperationDAOImpl extends OperationDAOImpl { @Override public void deleteOperation(int id) throws OperationManagementDAOException { - super.deleteOperation(id); PreparedStatement stmt = null; try { Connection connection = OperationManagementDAOFactory.getConnection(); - stmt = connection.prepareStatement("DELETE DM_CONFIG_OPERATION WHERE OPERATION_ID=?") ; + stmt = connection.prepareStatement("DELETE DM_CONFIG_OPERATION WHERE OPERATION_ID = ?"); stmt.setInt(1, id); stmt.executeUpdate(); } catch (SQLException e) { @@ -79,17 +75,14 @@ public class ConfigOperationDAOImpl extends OperationDAOImpl { @Override public void updateOperation(Operation operation) throws OperationManagementDAOException { - PreparedStatement stmt = null; ByteArrayOutputStream bao = null; ObjectOutputStream oos = null; super.updateOperation(operation); - try { Connection connection = OperationManagementDAOFactory.getConnection(); - stmt = connection.prepareStatement("UPDATE DM_CONFIG_OPERATION O SET O.OPERATION_CONFIG=? " + - "WHERE O.OPERATION_ID=?"); - + stmt = connection.prepareStatement("UPDATE DM_CONFIG_OPERATION O SET O.OPERATION_CONFIG = ? " + + "WHERE O.OPERATION_ID = ?"); bao = new ByteArrayOutputStream(); oos = new ObjectOutputStream(bao); oos.writeObject(operation); @@ -97,7 +90,6 @@ public class ConfigOperationDAOImpl extends OperationDAOImpl { stmt.setBytes(1, bao.toByteArray()); stmt.setInt(2, operation.getId()); stmt.executeUpdate(); - } catch (SQLException e) { throw new OperationManagementDAOException("Error occurred while update policy operation metadata", e); } catch (IOException e) { @@ -123,18 +115,15 @@ public class ConfigOperationDAOImpl extends OperationDAOImpl { @Override public Operation getOperation(int operationId) throws OperationManagementDAOException { - PreparedStatement stmt = null; ResultSet rs = null; ConfigOperation configOperation = null; ByteArrayInputStream bais; ObjectInputStream ois; - try { Connection conn = OperationManagementDAOFactory.getConnection(); - String sql = "SELECT OPERATION_ID, ENABLED, OPERATION_CONFIG FROM DM_CONFIG_OPERATION WHERE OPERATION_ID=?"; - + String sql = "SELECT OPERATION_ID, ENABLED, OPERATION_CONFIG FROM DM_CONFIG_OPERATION WHERE OPERATION_ID = ?"; stmt = conn.prepareStatement(sql); stmt.setInt(1, operationId); rs = stmt.executeQuery(); @@ -145,21 +134,16 @@ public class ConfigOperationDAOImpl extends OperationDAOImpl { ois = new ObjectInputStream(bais); configOperation = (ConfigOperation) ois.readObject(); } - } catch (IOException e) { - String errorMsg = "IO Error occurred while de serialize the policy operation object"; - log.error(errorMsg, e); - throw new OperationManagementDAOException(errorMsg, e); + throw new OperationManagementDAOException("IO Error occurred while de serialize the policy operation " + + "object", e); } catch (ClassNotFoundException e) { - String errorMsg = "Class not found error occurred while de serialize the policy operation object"; - log.error(errorMsg, e); - throw new OperationManagementDAOException(errorMsg, e); + throw new OperationManagementDAOException("Class not found error occurred while de serialize the policy " + + "operation object", e); } catch (SQLException e) { - String errorMsg = "SQL Error occurred while retrieving the policy operation object " + "available for " + - "the id '" - + operationId; - log.error(errorMsg, e); - throw new OperationManagementDAOException(errorMsg, e); + throw new OperationManagementDAOException("SQL Error occurred while retrieving the policy operation " + + "object available for the id '" + + operationId, e); } finally { OperationManagementDAOUtil.cleanupResources(stmt, rs); OperationManagementDAOFactory.closeConnection(); @@ -168,24 +152,20 @@ public class ConfigOperationDAOImpl extends OperationDAOImpl { } @Override - public List getOperationsByDeviceAndStatus(int enrolmentId, - Operation.Status status) throws OperationManagementDAOException { - + public List getOperationsByDeviceAndStatus( + int enrolmentId, Operation.Status status) throws OperationManagementDAOException { PreparedStatement stmt = null; ResultSet rs = null; ConfigOperation configOperation; - - List operationList = new ArrayList(); + List operations = new ArrayList<>(); ByteArrayInputStream bais = null; ObjectInputStream ois = null; - try { Connection conn = OperationManagementDAOFactory.getConnection(); - String sql = "Select co.OPERATION_ID, co.OPERATION_CONFIG from DM_CONFIG_OPERATION co " + - "INNER JOIN " + - "(Select * From DM_ENROLMENT_OPERATION_MAPPING WHERE ENROLMENT_ID=? " + - "AND STATUS=?) dm ON dm.OPERATION_ID = co.OPERATION_ID"; + String sql = "SELECT co.OPERATION_ID, co.OPERATION_CONFIG FROM DM_CONFIG_OPERATION co " + + "INNER JOIN (SELECT * FROM DM_ENROLMENT_OPERATION_MAPPING WHERE ENROLMENT_ID = ? " + + "AND STATUS = ?) dm ON dm.OPERATION_ID = co.OPERATION_ID"; stmt = conn.prepareStatement(sql); stmt.setInt(1, enrolmentId); @@ -199,22 +179,17 @@ public class ConfigOperationDAOImpl extends OperationDAOImpl { ois = new ObjectInputStream(bais); configOperation = (ConfigOperation) ois.readObject(); configOperation.setStatus(status); - operationList.add(configOperation); + operations.add(configOperation); } - } catch (IOException e) { - String errorMsg = "IO Error occurred while de serialize the configuration operation object"; - log.error(errorMsg, e); - throw new OperationManagementDAOException(errorMsg, e); + throw new OperationManagementDAOException("IO Error occurred while de serialize the configuration " + + "operation object", e); } catch (ClassNotFoundException e) { - String errorMsg = "Class not found error occurred while de serialize the configuration operation object"; - log.error(errorMsg, e); - throw new OperationManagementDAOException(errorMsg, e); + throw new OperationManagementDAOException("Class not found error occurred while de serialize the " + + "configuration operation object", e); } catch (SQLException e) { - String errorMsg = "SQL error occurred while retrieving the operation available for the device'" + enrolmentId + - "' with status '" + status.toString(); - log.error(errorMsg); - throw new OperationManagementDAOException(errorMsg, e); + throw new OperationManagementDAOException("SQL error occurred while retrieving the operation available " + + "for the device'" + enrolmentId + "' with status '" + status.toString(), e); } finally { if (bais != null) { try { @@ -233,6 +208,7 @@ public class ConfigOperationDAOImpl extends OperationDAOImpl { OperationManagementDAOUtil.cleanupResources(stmt, rs); OperationManagementDAOFactory.closeConnection(); } - return operationList; + return operations; } + } diff --git a/components/device-mgt/org.wso2.carbon.device.mgt.core/src/main/java/org/wso2/carbon/device/mgt/core/operation/mgt/dao/impl/OperationDAOImpl.java b/components/device-mgt/org.wso2.carbon.device.mgt.core/src/main/java/org/wso2/carbon/device/mgt/core/operation/mgt/dao/impl/OperationDAOImpl.java index 278199989ec..438e9a88e92 100644 --- a/components/device-mgt/org.wso2.carbon.device.mgt.core/src/main/java/org/wso2/carbon/device/mgt/core/operation/mgt/dao/impl/OperationDAOImpl.java +++ b/components/device-mgt/org.wso2.carbon.device.mgt.core/src/main/java/org/wso2/carbon/device/mgt/core/operation/mgt/dao/impl/OperationDAOImpl.java @@ -39,7 +39,6 @@ public class OperationDAOImpl implements OperationDAO { private static final Log log = LogFactory.getLog(OperationDAOImpl.class); public int addOperation(Operation operation) throws OperationManagementDAOException { - PreparedStatement stmt = null; ResultSet rs = null; try { @@ -68,17 +67,14 @@ public class OperationDAOImpl implements OperationDAO { @Override public void updateOperation(Operation operation) throws OperationManagementDAOException { - PreparedStatement stmt = null; try { Connection connection = OperationManagementDAOFactory.getConnection(); - stmt = connection.prepareStatement("UPDATE DM_OPERATION O SET O.RECEIVED_TIMESTAMP=? " + - "WHERE O.ID=?"); - + stmt = connection.prepareStatement("UPDATE DM_OPERATION O SET O.RECEIVED_TIMESTAMP = ? " + + "WHERE O.ID = ?"); stmt.setTimestamp(1, new Timestamp(new Date().getTime())); stmt.setInt(2, operation.getId()); stmt.executeUpdate(); - } catch (SQLException e) { throw new OperationManagementDAOException("Error occurred while update operation metadata", e); } finally { @@ -88,13 +84,11 @@ public class OperationDAOImpl implements OperationDAO { public void updateOperationStatus(int enrolmentId, int operationId, Operation.Status status) throws OperationManagementDAOException { - PreparedStatement stmt = null; try { Connection connection = OperationManagementDAOFactory.getConnection(); - stmt = connection.prepareStatement("UPDATE DM_ENROLMENT_OPERATION_MAPPING O SET O.STATUS=? " + - "WHERE O.ENROLMENT_ID=? and O.OPERATION_ID=?"); - + stmt = connection.prepareStatement("UPDATE DM_ENROLMENT_OPERATION_MAPPING O SET O.STATUS = ? " + + "WHERE O.ENROLMENT_ID = ? and O.OPERATION_ID = ?"); stmt.setString(1, status.toString()); stmt.setInt(2, enrolmentId); stmt.setInt(3, operationId); @@ -102,28 +96,22 @@ public class OperationDAOImpl implements OperationDAO { } catch (SQLException e) { throw new OperationManagementDAOException("Error occurred while update device mapping operation status " + - "metadata", - e); + "metadata", e); } finally { OperationManagementDAOUtil.cleanupResources(stmt); } - } @Override public void addOperationResponse(int enrolmentId, int operationId, Object operationResponse) throws OperationManagementDAOException { - PreparedStatement stmt = null; ByteArrayOutputStream bao = null; ObjectOutputStream oos = null; - try { Connection connection = OperationManagementDAOFactory.getConnection(); - - stmt = connection.prepareStatement("INSERT INTO DM_DEVICE_OPERATION_RESPONSE(OPERATION_ID,DEVICE_ID," + + stmt = connection.prepareStatement("INSERT INTO DM_DEVICE_OPERATION_RESPONSE(OPERATION_ID, DEVICE_ID, " + "OPERATION_RESPONSE) VALUES(?, ?, ?)"); - bao = new ByteArrayOutputStream(); oos = new ObjectOutputStream(bao); oos.writeObject(operationResponse); @@ -132,12 +120,11 @@ public class OperationDAOImpl implements OperationDAO { stmt.setInt(2, enrolmentId); stmt.setBytes(3, bao.toByteArray()); stmt.executeUpdate(); - } catch (SQLException e) { throw new OperationManagementDAOException("Error occurred while inserting operation response", e); - }catch (IOException e) { + } catch (IOException e) { throw new OperationManagementDAOException("Error occurred while serializing policy operation object", e); - }finally { + } finally { if (bao != null) { try { bao.close(); @@ -158,11 +145,10 @@ public class OperationDAOImpl implements OperationDAO { @Override public void deleteOperation(int id) throws OperationManagementDAOException { - PreparedStatement stmt = null; try { Connection connection = OperationManagementDAOFactory.getConnection(); - stmt = connection.prepareStatement("DELETE DM_OPERATION WHERE ID=?"); + stmt = connection.prepareStatement("DELETE DM_OPERATION WHERE ID = ?"); stmt.setInt(1, id); stmt.executeUpdate(); } catch (SQLException e) { @@ -174,16 +160,13 @@ public class OperationDAOImpl implements OperationDAO { @Override public Operation getOperation(int id) throws OperationManagementDAOException { - PreparedStatement stmt = null; ResultSet rs = null; Operation operation = null; - try { Connection conn = OperationManagementDAOFactory.getConnection(); String sql = "SELECT ID, TYPE, CREATED_TIMESTAMP, RECEIVED_TIMESTAMP, OPERATION_CODE FROM " + - "DM_OPERATION WHERE id=?"; - + "DM_OPERATION WHERE id = ?"; stmt = conn.prepareStatement(sql); stmt.setInt(1, id); rs = stmt.executeQuery(); @@ -202,10 +185,8 @@ public class OperationDAOImpl implements OperationDAO { } } catch (SQLException e) { - String errorMsg = "SQL Error occurred while retrieving the operation object " + "available for the id '" - + id; - log.error(errorMsg, e); - throw new OperationManagementDAOException(errorMsg, e); + throw new OperationManagementDAOException("SQL Error occurred while retrieving the operation object " + + "available for the id '" + id, e); } finally { OperationManagementDAOUtil.cleanupResources(stmt, rs); OperationManagementDAOFactory.closeConnection(); @@ -215,19 +196,16 @@ public class OperationDAOImpl implements OperationDAO { @Override public Operation getOperationByDeviceAndId(int enrolmentId, int operationId) throws OperationManagementDAOException { - PreparedStatement stmt = null; ResultSet rs = null; Operation operation = null; - try { Connection conn = OperationManagementDAOFactory.getConnection(); String sql = "SELECT o.ID, o.TYPE, o.CREATED_TIMESTAMP, o.RECEIVED_TIMESTAMP, o.STATUS, o.OPERATION_CODE " + - " From (SELECT ID, TYPE, CREATED_TIMESTAMP, RECEIVED_TIMESTAMP, STATUS," + - "OPERATION_CODE FROM DM_OPERATION WHERE id=?) o INNER JOIN (Select * from " + - "DM_ENROLMENT_OPERATION_MAPPING dm where dm.OPERATION_ID=? AND dm.ENROLMENT_ID=?) om " + + " FROM (SELECT ID, TYPE, CREATED_TIMESTAMP, RECEIVED_TIMESTAMP, STATUS," + + "OPERATION_CODE FROM DM_OPERATION WHERE id = ?) o INNER JOIN (SELECT * FROM " + + "DM_ENROLMENT_OPERATION_MAPPING dm where dm.OPERATION_ID = ? AND dm.ENROLMENT_ID = ?) om " + "ON o.ID = om.OPERATION_ID "; - stmt = conn.prepareStatement(sql); stmt.setInt(1, operationId); stmt.setInt(2, operationId); @@ -247,10 +225,8 @@ public class OperationDAOImpl implements OperationDAO { operation.setCode(rs.getString("OPERATION_CODE")); } } catch (SQLException e) { - String errorMsg = "SQL error occurred while retrieving the operation available for the device'" + enrolmentId + - "' with id '" + operationId; - log.error(errorMsg); - throw new OperationManagementDAOException(errorMsg, e); + throw new OperationManagementDAOException("SQL error occurred while retrieving the operation " + + "available for the device'" + enrolmentId + "' with id '" + operationId, e); } finally { OperationManagementDAOUtil.cleanupResources(stmt, rs); OperationManagementDAOFactory.closeConnection(); @@ -259,26 +235,21 @@ public class OperationDAOImpl implements OperationDAO { } @Override - public List getOperationsByDeviceAndStatus(int enrolmentId, - Operation.Status status) throws OperationManagementDAOException { - + public List getOperationsByDeviceAndStatus( + int enrolmentId, Operation.Status status) throws OperationManagementDAOException { PreparedStatement stmt = null; ResultSet rs = null; Operation operation; - - List operationList = new ArrayList(); - + List operations = new ArrayList<>(); try { Connection conn = OperationManagementDAOFactory.getConnection(); String sql = "SELECT o.ID, TYPE, CREATED_TIMESTAMP, RECEIVED_TIMESTAMP, OPERATION_CODE " + "FROM DM_OPERATION o " + - "INNER JOIN (Select * from DM_ENROLMENT_OPERATION_MAPPING dm " + - "where dm.ENROLMENT_ID=? and dm.STATUS=?) om ON o.ID = om.OPERATION_ID ORDER BY o.CREATED_TIMESTAMP ASC"; - + "INNER JOIN (SELECT * FROM DM_ENROLMENT_OPERATION_MAPPING dm " + + "WHERE dm.ENROLMENT_ID = ? AND dm.STATUS = ?) om ON o.ID = om.OPERATION_ID ORDER BY o.CREATED_TIMESTAMP ASC"; stmt = conn.prepareStatement(sql); stmt.setInt(1, enrolmentId); stmt.setString(2, status.toString()); - rs = stmt.executeQuery(); while (rs.next()) { @@ -293,40 +264,32 @@ public class OperationDAOImpl implements OperationDAO { } operation.setCode(rs.getString("OPERATION_CODE")); operation.setStatus(status); - operationList.add(operation); + operations.add(operation); } } catch (SQLException e) { - String errorMsg = "SQL error occurred while retrieving the operation available for the device'" + enrolmentId + - "' with status '" + status.toString(); - log.error(errorMsg); - throw new OperationManagementDAOException(errorMsg, e); + throw new OperationManagementDAOException("SQL error occurred while retrieving the operation " + + "available for the device'" + enrolmentId + "' with status '" + status.toString(), e); } finally { OperationManagementDAOUtil.cleanupResources(stmt, rs); OperationManagementDAOFactory.closeConnection(); } - return operationList; + return operations; } @Override - public List getOperationsForDevice(int enrolmentId) - throws OperationManagementDAOException { - + public List getOperationsForDevice(int enrolmentId) throws OperationManagementDAOException { PreparedStatement stmt = null; ResultSet rs = null; Operation operation; - - List operationList = new ArrayList(); - + List operations = new ArrayList<>(); try { Connection conn = OperationManagementDAOFactory.getConnection(); String sql = "SELECT o.ID, TYPE, CREATED_TIMESTAMP, RECEIVED_TIMESTAMP, " + - "OPERATION_CODE,dm.STATUS FROM DM_OPERATION o " + - "INNER JOIN (Select * from DM_ENROLMENT_OPERATION_MAPPING dm " + - "where dm.ENROLMENT_ID=?) om ON o.ID = om.OPERATION_ID ORDER BY o.CREATED_TIMESTAMP ASC"; - + "OPERATION_CODE, dm.STATUS FROM DM_OPERATION o " + + "INNER JOIN (SELECT * FROM DM_ENROLMENT_OPERATION_MAPPING dm " + + "WHERE dm.ENROLMENT_ID = ?) om ON o.ID = om.OPERATION_ID ORDER BY o.CREATED_TIMESTAMP ASC"; stmt = conn.prepareStatement(sql); stmt.setInt(1, enrolmentId); - rs = stmt.executeQuery(); while (rs.next()) { @@ -341,40 +304,34 @@ public class OperationDAOImpl implements OperationDAO { } operation.setCode(rs.getString("OPERATION_CODE")); operation.setStatus(Operation.Status.valueOf(rs.getString("STATUS"))); - operationList.add(operation); + operations.add(operation); } } catch (SQLException e) { - String errorMsg = "SQL error occurred while retrieving the operation available for the device'" + enrolmentId + - "' with status '"; - log.error(errorMsg); - throw new OperationManagementDAOException(errorMsg, e); + throw new OperationManagementDAOException("SQL error occurred while retrieving the operation " + + "available for the device'" + enrolmentId + "' with status '", e); } finally { OperationManagementDAOUtil.cleanupResources(stmt, rs); OperationManagementDAOFactory.closeConnection(); } - return operationList; + return operations; } @Override public Operation getNextOperation(int enrolmentId) throws OperationManagementDAOException { - PreparedStatement stmt = null; ResultSet rs = null; - try { Connection connection = OperationManagementDAOFactory.getConnection(); stmt = connection.prepareStatement("SELECT o.ID, TYPE, CREATED_TIMESTAMP, RECEIVED_TIMESTAMP, " + "OPERATION_CODE FROM DM_OPERATION o " + - "INNER JOIN (Select * from DM_ENROLMENT_OPERATION_MAPPING dm " + - "where dm.ENROLMENT_ID=? AND dm.STATUS=?) om ON o.ID = om.OPERATION_ID " + + "INNER JOIN (SELECT * FROM DM_ENROLMENT_OPERATION_MAPPING dm " + + "WHERE dm.ENROLMENT_ID = ? AND dm.STATUS = ?) om ON o.ID = om.OPERATION_ID " + "ORDER BY o.CREATED_TIMESTAMP ASC LIMIT 1"); - stmt.setInt(1, enrolmentId); stmt.setString(2, Operation.Status.PENDING.toString()); - rs = stmt.executeQuery(); - Operation operation = null; + Operation operation = null; if (rs.next()) { operation = new Operation(); operation.setType(this.getType(rs.getString("TYPE"))); @@ -398,28 +355,24 @@ public class OperationDAOImpl implements OperationDAO { } - public List getOperationsByDeviceStatusAndType(int enrolmentId, - Operation.Status status, Operation.Type type) throws OperationManagementDAOException { - + public List getOperationsByDeviceStatusAndType( + int enrolmentId, Operation.Status status, Operation.Type type) throws OperationManagementDAOException { PreparedStatement stmt = null; ResultSet rs = null; Operation operation; - - List operationList = new ArrayList(); - + List operations = new ArrayList<>(); try { Connection conn = OperationManagementDAOFactory.getConnection(); String sql = "SELECT o.ID, TYPE, CREATED_TIMESTAMP, RECEIVED_TIMESTAMP, OPERATION_CODE FROM " + "(SELECT o.ID, TYPE, CREATED_TIMESTAMP, RECEIVED_TIMESTAMP, OPERATION_CODE " + - "FROM DM_OPERATION o WHERE o.TYPE=?) o " + - "INNER JOIN (Select * from DM_ENROLMENT_OPERATION_MAPPING dm " + - "where dm.ENROLMENT_ID=? and dm.STATUS=?) om ON o.ID = om.OPERATION_ID ORDER BY o.CREATED_TIMESTAMP ASC"; + "FROM DM_OPERATION o WHERE o.TYPE = ?) o " + + "INNER JOIN (SELECT * FROM DM_ENROLMENT_OPERATION_MAPPING dm " + + "WHERE dm.ENROLMENT_ID = ? AND dm.STATUS = ?) om ON o.ID = om.OPERATION_ID ORDER BY o.CREATED_TIMESTAMP ASC"; stmt = conn.prepareStatement(sql); stmt.setString(1, type.toString()); stmt.setInt(2, enrolmentId); stmt.setString(3, status.toString()); - rs = stmt.executeQuery(); while (rs.next()) { @@ -433,22 +386,16 @@ public class OperationDAOImpl implements OperationDAO { operation.setReceivedTimeStamp(rs.getTimestamp("RECEIVED_TIMESTAMP").toString()); } operation.setCode(rs.getString("OPERATION_CODE")); - operationList.add(operation); + operations.add(operation); } } catch (SQLException e) { - String errorMsg = "SQL error occurred while retrieving the operation available for the device'" + enrolmentId + - "' with status '" + status.toString(); - log.error(errorMsg); - throw new OperationManagementDAOException(errorMsg, e); + throw new OperationManagementDAOException("SQL error occurred while retrieving the operation available " + + "for the device'" + enrolmentId + "' with status '" + status.toString(), e); } finally { OperationManagementDAOUtil.cleanupResources(stmt, rs); OperationManagementDAOFactory.closeConnection(); } - return operationList; - } - - private Operation.Status getStatus(String status) { - return Operation.Status.valueOf(status); + return operations; } private Operation.Type getType(String type) { diff --git a/components/device-mgt/org.wso2.carbon.device.mgt.core/src/main/java/org/wso2/carbon/device/mgt/core/operation/mgt/dao/impl/PolicyOperationDAOImpl.java b/components/device-mgt/org.wso2.carbon.device.mgt.core/src/main/java/org/wso2/carbon/device/mgt/core/operation/mgt/dao/impl/PolicyOperationDAOImpl.java index 59f758a4738..cd4822ced0c 100644 --- a/components/device-mgt/org.wso2.carbon.device.mgt.core/src/main/java/org/wso2/carbon/device/mgt/core/operation/mgt/dao/impl/PolicyOperationDAOImpl.java +++ b/components/device-mgt/org.wso2.carbon.device.mgt.core/src/main/java/org/wso2/carbon/device/mgt/core/operation/mgt/dao/impl/PolicyOperationDAOImpl.java @@ -22,7 +22,6 @@ import org.apache.commons.logging.Log; import org.apache.commons.logging.LogFactory; import org.wso2.carbon.device.mgt.core.dto.operation.mgt.Operation; import org.wso2.carbon.device.mgt.core.dto.operation.mgt.PolicyOperation; -import org.wso2.carbon.device.mgt.core.dto.operation.mgt.ProfileOperation; import org.wso2.carbon.device.mgt.core.operation.mgt.dao.OperationManagementDAOException; import org.wso2.carbon.device.mgt.core.operation.mgt.dao.OperationManagementDAOFactory; import org.wso2.carbon.device.mgt.core.operation.mgt.dao.OperationManagementDAOUtil; @@ -38,7 +37,6 @@ public class PolicyOperationDAOImpl extends OperationDAOImpl { @Override public int addOperation(Operation operation) throws OperationManagementDAOException { - int operationId = super.addOperation(operation); operation.setCreatedTimeStamp(new Timestamp(new java.util.Date().getTime()).toString()); operation.setId(operationId); @@ -47,7 +45,6 @@ public class PolicyOperationDAOImpl extends OperationDAOImpl { Connection conn = OperationManagementDAOFactory.getConnection(); PreparedStatement stmt = null; - try { stmt = conn.prepareStatement("INSERT INTO DM_POLICY_OPERATION(OPERATION_ID, OPERATION_DETAILS) " + "VALUES(?, ?)"); @@ -60,22 +57,18 @@ public class PolicyOperationDAOImpl extends OperationDAOImpl { OperationManagementDAOUtil.cleanupResources(stmt); } return operationId; - } @Override public void updateOperation(Operation operation) throws OperationManagementDAOException { - PreparedStatement stmt = null; ByteArrayOutputStream bao = null; ObjectOutputStream oos = null; super.updateOperation(operation); - try { Connection connection = OperationManagementDAOFactory.getConnection(); stmt = connection.prepareStatement("UPDATE DM_POLICY_OPERATION O SET O.OPERATION_DETAILS=? " + "WHERE O.OPERATION_ID=?"); - bao = new ByteArrayOutputStream(); oos = new ObjectOutputStream(bao); oos.writeObject(operation); @@ -83,7 +76,6 @@ public class PolicyOperationDAOImpl extends OperationDAOImpl { stmt.setBytes(1, bao.toByteArray()); stmt.setInt(2, operation.getId()); stmt.executeUpdate(); - } catch (SQLException e) { throw new OperationManagementDAOException("Error occurred while update policy operation metadata", e); } catch (IOException e) { @@ -109,7 +101,6 @@ public class PolicyOperationDAOImpl extends OperationDAOImpl { @Override public void deleteOperation(int operationId) throws OperationManagementDAOException { - super.deleteOperation(operationId); PreparedStatement stmt = null; try { @@ -126,18 +117,15 @@ public class PolicyOperationDAOImpl extends OperationDAOImpl { @Override public Operation getOperation(int operationId) throws OperationManagementDAOException { - PreparedStatement stmt = null; ResultSet rs = null; PolicyOperation policyOperation = null; ByteArrayInputStream bais; ObjectInputStream ois; - try { Connection conn = OperationManagementDAOFactory.getConnection(); String sql = "SELECT OPERATION_ID, ENABLED, OPERATION_DETAILS FROM DM_POLICY_OPERATION WHERE OPERATION_ID=?"; - stmt = conn.prepareStatement(sql); stmt.setInt(1, operationId); rs = stmt.executeQuery(); @@ -148,21 +136,15 @@ public class PolicyOperationDAOImpl extends OperationDAOImpl { ois = new ObjectInputStream(bais); policyOperation = (PolicyOperation) ois.readObject(); } - } catch (IOException e) { - String errorMsg = "IO Error occurred while de serialize the policy operation object"; - log.error(errorMsg, e); - throw new OperationManagementDAOException(errorMsg, e); + throw new OperationManagementDAOException("IO Error occurred while de serialize the policy operation " + + "object", e); } catch (ClassNotFoundException e) { - String errorMsg = "Class not found error occurred while de serialize the policy operation object"; - log.error(errorMsg, e); - throw new OperationManagementDAOException(errorMsg, e); + throw new OperationManagementDAOException("Class not found error occurred while de serialize the " + + "policy operation object", e); } catch (SQLException e) { - String errorMsg = "SQL Error occurred while retrieving the policy operation object " + "available for " + - "the id '" - + operationId; - log.error(errorMsg, e); - throw new OperationManagementDAOException(errorMsg, e); + throw new OperationManagementDAOException("SQL Error occurred while retrieving the policy operation " + + "object available for the id '" + operationId + "'", e); } finally { OperationManagementDAOUtil.cleanupResources(stmt, rs); OperationManagementDAOFactory.closeConnection(); @@ -173,27 +155,22 @@ public class PolicyOperationDAOImpl extends OperationDAOImpl { @Override public List getOperationsByDeviceAndStatus(int enrolmentId, Operation.Status status) throws OperationManagementDAOException { - PreparedStatement stmt = null; ResultSet rs = null; PolicyOperation policyOperation; - - List operationList = new ArrayList(); + List operations = new ArrayList<>(); ByteArrayInputStream bais = null; ObjectInputStream ois = null; - try { Connection conn = OperationManagementDAOFactory.getConnection(); - String sql = "Select po.OPERATION_ID, ENABLED, OPERATION_DETAILS from DM_POLICY_OPERATION po " + - "INNER JOIN " + - "(Select * From DM_ENROLMENT_OPERATION_MAPPING WHERE ENROLMENT_ID=? " + - "AND STATUS=?) dm ON dm.OPERATION_ID = po.OPERATION_ID"; + String sql = "SELECT po.OPERATION_ID, ENABLED, OPERATION_DETAILS FROM DM_POLICY_OPERATION po " + + "INNER JOIN (SELECT * FROM DM_ENROLMENT_OPERATION_MAPPING WHERE ENROLMENT_ID = ? " + + "AND STATUS = ?) dm ON dm.OPERATION_ID = po.OPERATION_ID"; stmt = conn.prepareStatement(sql); stmt.setInt(1, enrolmentId); stmt.setString(2, status.toString()); - rs = stmt.executeQuery(); while (rs.next()) { @@ -202,22 +179,17 @@ public class PolicyOperationDAOImpl extends OperationDAOImpl { ois = new ObjectInputStream(bais); policyOperation = (PolicyOperation) ois.readObject(); policyOperation.setStatus(status); - operationList.add(policyOperation); + operations.add(policyOperation); } - } catch (IOException e) { - String errorMsg = "IO Error occurred while de serialize the profile operation object"; - log.error(errorMsg, e); - throw new OperationManagementDAOException(errorMsg, e); + throw new OperationManagementDAOException("IO Error occurred while de serialize the profile " + + "operation object", e); } catch (ClassNotFoundException e) { - String errorMsg = "Class not found error occurred while de serialize the profile operation object"; - log.error(errorMsg, e); - throw new OperationManagementDAOException(errorMsg, e); + throw new OperationManagementDAOException("Class not found error occurred while de serialize the " + + "profile operation object", e); } catch (SQLException e) { - String errorMsg = "SQL error occurred while retrieving the operation available for the device'" + enrolmentId + - "' with status '" + status.toString(); - log.error(errorMsg); - throw new OperationManagementDAOException(errorMsg, e); + throw new OperationManagementDAOException("SQL error occurred while retrieving the operation " + + "available for the device'" + enrolmentId + "' with status '" + status.toString(), e); } finally { if (bais != null) { try { @@ -236,6 +208,7 @@ public class PolicyOperationDAOImpl extends OperationDAOImpl { OperationManagementDAOUtil.cleanupResources(stmt, rs); OperationManagementDAOFactory.closeConnection(); } - return operationList; + return operations; } + } From ca38cc9f518e6030238510b4d01d9099b15f5bec Mon Sep 17 00:00:00 2001 From: prabathabey Date: Tue, 28 Jul 2015 18:09:49 +0530 Subject: [PATCH 3/4] Removing URL printer start-up handler from device.mgt.core --- .../DeviceManagementServiceComponent.java | 8 +-- .../handler/URLPrinterStartupHandler.java | 64 ------------------- 2 files changed, 2 insertions(+), 70 deletions(-) delete mode 100644 components/device-mgt/org.wso2.carbon.device.mgt.core/src/main/java/org/wso2/carbon/device/mgt/core/startup/handler/URLPrinterStartupHandler.java diff --git a/components/device-mgt/org.wso2.carbon.device.mgt.core/src/main/java/org/wso2/carbon/device/mgt/core/internal/DeviceManagementServiceComponent.java b/components/device-mgt/org.wso2.carbon.device.mgt.core/src/main/java/org/wso2/carbon/device/mgt/core/internal/DeviceManagementServiceComponent.java index 84ff9553f26..4da10c562f4 100644 --- a/components/device-mgt/org.wso2.carbon.device.mgt.core/src/main/java/org/wso2/carbon/device/mgt/core/internal/DeviceManagementServiceComponent.java +++ b/components/device-mgt/org.wso2.carbon.device.mgt.core/src/main/java/org/wso2/carbon/device/mgt/core/internal/DeviceManagementServiceComponent.java @@ -22,7 +22,6 @@ import org.apache.commons.logging.LogFactory; import org.osgi.framework.BundleContext; import org.osgi.service.component.ComponentContext; import org.wso2.carbon.apimgt.impl.APIManagerConfigurationService; -import org.wso2.carbon.core.ServerStartupObserver; import org.wso2.carbon.device.mgt.common.DeviceManagementException; import org.wso2.carbon.device.mgt.common.app.mgt.ApplicationManagementException; import org.wso2.carbon.device.mgt.common.operation.mgt.OperationManagementException; @@ -42,7 +41,6 @@ import org.wso2.carbon.device.mgt.core.operation.mgt.OperationManagerImpl; import org.wso2.carbon.device.mgt.core.operation.mgt.dao.OperationManagementDAOFactory; import org.wso2.carbon.device.mgt.core.service.DeviceManagementProviderService; import org.wso2.carbon.device.mgt.core.service.DeviceManagementProviderServiceImpl; -import org.wso2.carbon.device.mgt.core.startup.handler.URLPrinterStartupHandler; import org.wso2.carbon.device.mgt.core.util.DeviceManagementSchemaInitializer; import org.wso2.carbon.ndatasource.core.DataSourceService; import org.wso2.carbon.registry.core.service.RegistryService; @@ -176,11 +174,9 @@ public class DeviceManagementServiceComponent { AppManagementConfigurationManager.getInstance().getAppManagementConfig(); bundleContext.registerService(ApplicationManagementProviderService.class.getName(), new ApplicationManagerProviderServiceImpl(appConfig, pluginRepository), null); - } catch (ApplicationManagementException appMgtEx) { - log.error("Application management service not registered."); + } catch (ApplicationManagementException e) { + log.error("Application management service not registered.", e); } - - bundleContext.registerService(ServerStartupObserver.class, new URLPrinterStartupHandler(), null); } private void setupDeviceManagementSchema(DataSourceConfig config) throws DeviceManagementException { diff --git a/components/device-mgt/org.wso2.carbon.device.mgt.core/src/main/java/org/wso2/carbon/device/mgt/core/startup/handler/URLPrinterStartupHandler.java b/components/device-mgt/org.wso2.carbon.device.mgt.core/src/main/java/org/wso2/carbon/device/mgt/core/startup/handler/URLPrinterStartupHandler.java deleted file mode 100644 index 83250c0d22b..00000000000 --- a/components/device-mgt/org.wso2.carbon.device.mgt.core/src/main/java/org/wso2/carbon/device/mgt/core/startup/handler/URLPrinterStartupHandler.java +++ /dev/null @@ -1,64 +0,0 @@ -/* - * 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.core.startup.handler; - -import org.apache.commons.logging.Log; -import org.apache.commons.logging.LogFactory; -import org.wso2.carbon.core.ServerStartupObserver; -import org.wso2.carbon.device.mgt.core.internal.DeviceManagementDataHolder; -import org.wso2.carbon.utils.CarbonUtils; -import org.wso2.carbon.utils.ConfigurationContextService; -import org.wso2.carbon.utils.NetworkUtils; - -public class URLPrinterStartupHandler implements ServerStartupObserver { - - private static final Log log = LogFactory.getLog(URLPrinterStartupHandler.class); - - @Override - public void completingServerStartup() { - - } - - @Override - public void completedServerStartup() { - log.info("EMM Console URL : " + this.getEmmUrl()); - } - - private String getEmmUrl() { - // Hostname - String hostName = "localhost"; - try { - hostName = NetworkUtils.getMgtHostName(); - } catch (Exception ignored) { - } - // HTTPS port - String mgtConsoleTransport = CarbonUtils.getManagementTransport(); - ConfigurationContextService configContextService = - DeviceManagementDataHolder.getInstance().getConfigurationContextService(); - int port = CarbonUtils.getTransportPort(configContextService, mgtConsoleTransport); - int httpsProxyPort = - CarbonUtils.getTransportProxyPort(configContextService.getServerConfigContext(), - mgtConsoleTransport); - if (httpsProxyPort > 0) { - port = httpsProxyPort; - } - return "https://" + hostName + ":" + port + "/mdm"; - } - -} From c134cc7e25c46bfe817712680900e4c3098f559c Mon Sep 17 00:00:00 2001 From: prabathabey Date: Tue, 28 Jul 2015 20:38:02 +0530 Subject: [PATCH 4/4] Fixing license manager initialization related issues and code cleanup --- .../DeviceManagementPluginRepository.java | 54 +++++++++++++------ .../DeviceManagementServiceComponent.java | 18 ++++++- .../DeviceManagerStartupListener.java} | 6 +-- .../pom.xml | 10 ++++ .../mgt/GenericArtifactManagerFactory.java | 8 +-- .../license/mgt/LicenseManagementService.java | 38 ------------- .../mgt/RegistryBasedLicenseManager.java | 7 ++- 7 files changed, 78 insertions(+), 63 deletions(-) rename components/device-mgt/{org.wso2.carbon.device.mgt.extensions/src/main/java/org/wso2/carbon/device/mgt/extensions/license/mgt/LicenseManagementUtil.java => org.wso2.carbon.device.mgt.core/src/main/java/org/wso2/carbon/device/mgt/core/internal/DeviceManagerStartupListener.java} (84%) delete mode 100644 components/device-mgt/org.wso2.carbon.device.mgt.extensions/src/main/java/org/wso2/carbon/device/mgt/extensions/license/mgt/LicenseManagementService.java diff --git a/components/device-mgt/org.wso2.carbon.device.mgt.core/src/main/java/org/wso2/carbon/device/mgt/core/DeviceManagementPluginRepository.java b/components/device-mgt/org.wso2.carbon.device.mgt.core/src/main/java/org/wso2/carbon/device/mgt/core/DeviceManagementPluginRepository.java index 4c276405ffe..ae94f528118 100644 --- a/components/device-mgt/org.wso2.carbon.device.mgt.core/src/main/java/org/wso2/carbon/device/mgt/core/DeviceManagementPluginRepository.java +++ b/components/device-mgt/org.wso2.carbon.device.mgt.core/src/main/java/org/wso2/carbon/device/mgt/core/DeviceManagementPluginRepository.java @@ -17,46 +17,70 @@ */ package org.wso2.carbon.device.mgt.core; +import org.apache.commons.logging.Log; +import org.apache.commons.logging.LogFactory; import org.wso2.carbon.device.mgt.common.DeviceManagementException; import org.wso2.carbon.device.mgt.common.spi.DeviceManagementService; +import org.wso2.carbon.device.mgt.core.internal.DeviceManagementServiceComponent; +import org.wso2.carbon.device.mgt.core.internal.DeviceManagerStartupListener; import org.wso2.carbon.device.mgt.core.util.DeviceManagerUtil; -import java.util.Collection; +import java.util.Collections; import java.util.HashMap; import java.util.Map; -public class DeviceManagementPluginRepository { +public class DeviceManagementPluginRepository implements DeviceManagerStartupListener { private Map providers; + private boolean isInited; + private static final Log log = LogFactory.getLog(DeviceManagementPluginRepository.class); public DeviceManagementPluginRepository() { - providers = new HashMap(); + providers = Collections.synchronizedMap(new HashMap()); + DeviceManagementServiceComponent.registerStartupListener(this); } public void addDeviceManagementProvider(DeviceManagementService provider) throws DeviceManagementException { String deviceType = provider.getType(); - try { - /* Initializing Device Management Service Provider */ - provider.init(); - DeviceManagerUtil.registerDeviceType(deviceType); - } catch (DeviceManagementException e) { - throw new DeviceManagementException("Error occurred while adding device management provider '" + - deviceType + "'"); + synchronized (providers) { + try { + if (isInited) { + /* Initializing Device Management Service Provider */ + provider.init(); + DeviceManagerUtil.registerDeviceType(deviceType); + } + } catch (DeviceManagementException e) { + throw new DeviceManagementException("Error occurred while adding device management provider '" + + deviceType + "'", e); + } + providers.put(deviceType, provider); } - providers.put(deviceType, provider); } public void removeDeviceManagementProvider(DeviceManagementService provider) throws DeviceManagementException { - String deviceType = provider.getType(); - providers.remove(deviceType); + providers.remove(provider.getType()); } public DeviceManagementService getDeviceManagementService(String type) { return providers.get(type); } - public Collection getDeviceManagementProviders(){ - return providers.values(); + @Override + public void notifyObserver() { + synchronized (providers) { + for (DeviceManagementService provider : providers.values()) { + try { + provider.init(); + DeviceManagerUtil.registerDeviceType(provider.getType()); + } catch (Throwable e) { + /* Throwable is caught intentionally as failure of one plugin - due to invalid start up parameters, + etc - should not block the initialization of other device management providers */ + log.error("Error occurred while initializing device management provider '" + + provider.getType() + "'", e); + } + } + this.isInited = true; + } } } diff --git a/components/device-mgt/org.wso2.carbon.device.mgt.core/src/main/java/org/wso2/carbon/device/mgt/core/internal/DeviceManagementServiceComponent.java b/components/device-mgt/org.wso2.carbon.device.mgt.core/src/main/java/org/wso2/carbon/device/mgt/core/internal/DeviceManagementServiceComponent.java index 4da10c562f4..6acdd01d5ba 100644 --- a/components/device-mgt/org.wso2.carbon.device.mgt.core/src/main/java/org/wso2/carbon/device/mgt/core/internal/DeviceManagementServiceComponent.java +++ b/components/device-mgt/org.wso2.carbon.device.mgt.core/src/main/java/org/wso2/carbon/device/mgt/core/internal/DeviceManagementServiceComponent.java @@ -95,8 +95,9 @@ public class DeviceManagementServiceComponent { private DeviceManagementPluginRepository pluginRepository = new DeviceManagementPluginRepository(); private static final Object LOCK = new Object(); - private static List listeners = new ArrayList(); - private static List deviceManagers = new ArrayList(); + private static List listeners = new ArrayList<>(); + private static List deviceManagers = new ArrayList<>(); + private static List startupListeners = new ArrayList<>(); @SuppressWarnings("unused") protected void activate(ComponentContext componentContext) { @@ -130,6 +131,9 @@ public class DeviceManagementServiceComponent { /* Registering declarative service instances exposed by DeviceManagementServiceComponent */ this.registerServices(componentContext); + /* This is a workaround to initialize all Device Management Service Providers after the initialization + * of Device Management Service component in order to avoid bundle start up order related complications */ + notifyStartupListeners(); if (log.isDebugEnabled()) { log.debug("Device management core bundle has been successfully initialized"); } @@ -312,4 +316,14 @@ public class DeviceManagementServiceComponent { DeviceManagementDataHolder.getInstance().setConfigurationContextService(null); } + public static void registerStartupListener(DeviceManagerStartupListener startupListener) { + startupListeners.add(startupListener); + } + + public static void notifyStartupListeners() { + for (DeviceManagerStartupListener startupListener : startupListeners) { + startupListener.notifyObserver(); + } + } + } diff --git a/components/device-mgt/org.wso2.carbon.device.mgt.extensions/src/main/java/org/wso2/carbon/device/mgt/extensions/license/mgt/LicenseManagementUtil.java b/components/device-mgt/org.wso2.carbon.device.mgt.core/src/main/java/org/wso2/carbon/device/mgt/core/internal/DeviceManagerStartupListener.java similarity index 84% rename from components/device-mgt/org.wso2.carbon.device.mgt.extensions/src/main/java/org/wso2/carbon/device/mgt/extensions/license/mgt/LicenseManagementUtil.java rename to components/device-mgt/org.wso2.carbon.device.mgt.core/src/main/java/org/wso2/carbon/device/mgt/core/internal/DeviceManagerStartupListener.java index 79e69864064..29f9069ffcf 100644 --- a/components/device-mgt/org.wso2.carbon.device.mgt.extensions/src/main/java/org/wso2/carbon/device/mgt/extensions/license/mgt/LicenseManagementUtil.java +++ b/components/device-mgt/org.wso2.carbon.device.mgt.core/src/main/java/org/wso2/carbon/device/mgt/core/internal/DeviceManagerStartupListener.java @@ -16,10 +16,10 @@ * under the License. * */ -package org.wso2.carbon.device.mgt.extensions.license.mgt; - -public class LicenseManagementUtil { +package org.wso2.carbon.device.mgt.core.internal; +public interface DeviceManagerStartupListener { + void notifyObserver(); } diff --git a/components/device-mgt/org.wso2.carbon.device.mgt.extensions/pom.xml b/components/device-mgt/org.wso2.carbon.device.mgt.extensions/pom.xml index 94f880c99cd..03ebbf73856 100644 --- a/components/device-mgt/org.wso2.carbon.device.mgt.extensions/pom.xml +++ b/components/device-mgt/org.wso2.carbon.device.mgt.extensions/pom.xml @@ -56,6 +56,16 @@ org.wso2.carbon.device.mgt.extensions.* + + org.wso2.carbon.governance.api.*, + javax.xml.namespace, + org.wso2.carbon.context, + org.wso2.carbon.device.mgt.common.license.mgt, + org.wso2.carbon.registry.api, + org.wso2.carbon.registry.core, + org.wso2.carbon.registry.core.exceptions, + org.wso2.carbon.registry.core.session + diff --git a/components/device-mgt/org.wso2.carbon.device.mgt.extensions/src/main/java/org/wso2/carbon/device/mgt/extensions/license/mgt/GenericArtifactManagerFactory.java b/components/device-mgt/org.wso2.carbon.device.mgt.extensions/src/main/java/org/wso2/carbon/device/mgt/extensions/license/mgt/GenericArtifactManagerFactory.java index a5bea93668e..1b52502a361 100644 --- a/components/device-mgt/org.wso2.carbon.device.mgt.extensions/src/main/java/org/wso2/carbon/device/mgt/extensions/license/mgt/GenericArtifactManagerFactory.java +++ b/components/device-mgt/org.wso2.carbon.device.mgt.extensions/src/main/java/org/wso2/carbon/device/mgt/extensions/license/mgt/GenericArtifactManagerFactory.java @@ -22,8 +22,10 @@ import org.wso2.carbon.context.CarbonContext; import org.wso2.carbon.device.mgt.common.DeviceManagementConstants; import org.wso2.carbon.device.mgt.common.license.mgt.LicenseManagementException; import org.wso2.carbon.governance.api.generic.GenericArtifactManager; +import org.wso2.carbon.governance.api.util.GovernanceUtils; import org.wso2.carbon.registry.api.Registry; import org.wso2.carbon.registry.core.exceptions.RegistryException; +import org.wso2.carbon.registry.core.session.UserRegistry; import java.util.HashMap; import java.util.Map; @@ -32,14 +34,14 @@ public class GenericArtifactManagerFactory { private static Map tenantArtifactManagers = new HashMap(); - private static final Object lock = new Object(); + private static final Object LOCK = new Object(); public static GenericArtifactManager getTenantAwareGovernanceArtifactManager( Registry registry) throws LicenseManagementException { try { int tenantId = CarbonContext.getThreadLocalCarbonContext().getTenantId(); GenericArtifactManager artifactManager; - synchronized (lock) { + synchronized (LOCK) { artifactManager = tenantArtifactManagers.get(tenantId); if (artifactManager == null) { @@ -54,7 +56,7 @@ public class GenericArtifactManagerFactory { return artifactManager; } catch (RegistryException e) { throw new LicenseManagementException("Error occurred while initializing GovernanceArtifactManager " + - "associated with tenant '" + CarbonContext.getThreadLocalCarbonContext().getTenantDomain() + "'"); + "associated with tenant '" + CarbonContext.getThreadLocalCarbonContext().getTenantDomain() + "'", e); } } diff --git a/components/device-mgt/org.wso2.carbon.device.mgt.extensions/src/main/java/org/wso2/carbon/device/mgt/extensions/license/mgt/LicenseManagementService.java b/components/device-mgt/org.wso2.carbon.device.mgt.extensions/src/main/java/org/wso2/carbon/device/mgt/extensions/license/mgt/LicenseManagementService.java deleted file mode 100644 index 2a417cf7998..00000000000 --- a/components/device-mgt/org.wso2.carbon.device.mgt.extensions/src/main/java/org/wso2/carbon/device/mgt/extensions/license/mgt/LicenseManagementService.java +++ /dev/null @@ -1,38 +0,0 @@ -/* - * 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.extensions.license.mgt; - -import org.wso2.carbon.device.mgt.common.license.mgt.License; -import org.wso2.carbon.device.mgt.common.license.mgt.LicenseManagementException; -import org.wso2.carbon.device.mgt.common.license.mgt.LicenseManager; - -public class LicenseManagementService implements LicenseManager { - - @Override - public License getLicense(String deviceType, String languageCode) throws LicenseManagementException { - //return DeviceManagementDataHolder.getInstance().getLicenseManager().getLicense(deviceType, languageCode); - return null; - } - - @Override - public void addLicense(String deviceType, License license) throws LicenseManagementException { - //return DeviceManagementDataHolder.getInstance().getLicenseManager().addLicense(deviceType, license); - } - -} diff --git a/components/device-mgt/org.wso2.carbon.device.mgt.extensions/src/main/java/org/wso2/carbon/device/mgt/extensions/license/mgt/RegistryBasedLicenseManager.java b/components/device-mgt/org.wso2.carbon.device.mgt.extensions/src/main/java/org/wso2/carbon/device/mgt/extensions/license/mgt/RegistryBasedLicenseManager.java index 019259d5324..b5b092ddb29 100644 --- a/components/device-mgt/org.wso2.carbon.device.mgt.extensions/src/main/java/org/wso2/carbon/device/mgt/extensions/license/mgt/RegistryBasedLicenseManager.java +++ b/components/device-mgt/org.wso2.carbon.device.mgt.extensions/src/main/java/org/wso2/carbon/device/mgt/extensions/license/mgt/RegistryBasedLicenseManager.java @@ -19,6 +19,8 @@ package org.wso2.carbon.device.mgt.extensions.license.mgt; +import org.wso2.carbon.context.CarbonContext; +import org.wso2.carbon.context.RegistryType; import org.wso2.carbon.device.mgt.common.DeviceManagementConstants; import org.wso2.carbon.device.mgt.common.license.mgt.License; import org.wso2.carbon.device.mgt.common.license.mgt.LicenseManagementException; @@ -41,9 +43,10 @@ public class RegistryBasedLicenseManager implements LicenseManager { private Registry registry; private GenericArtifactManager artifactManager; - public RegistryBasedLicenseManager(Registry registry) { + public RegistryBasedLicenseManager() { + Registry registry = CarbonContext.getThreadLocalCarbonContext().getRegistry(RegistryType.SYSTEM_GOVERNANCE); if (registry == null) { - throw new IllegalArgumentException("Registry instance provided is null. Hence, " + + throw new IllegalArgumentException("Registry instance retrieved is null. Hence, " + "'Registry based license manager cannot be initialized'"); } this.registry = registry;