From b5a6280c8dca7daf05a7059f5045d67cd17fd323 Mon Sep 17 00:00:00 2001 From: inosh-perera Date: Thu, 3 Dec 2015 11:28:12 +0530 Subject: [PATCH 1/3] refactoring comments --- .../mgt/core/config/CertificateManagementConfig.java | 1 - .../core/config/CertificateManagementRepository.java | 1 - .../mgt/core/config/datasource/DataSourceConfig.java | 6 ------ .../certificate/mgt/core/dao/CertificateDAO.java | 10 ++++++---- .../mgt/core/dao/CertificateManagementDAOUtil.java | 1 - .../mgt/core/service/CertificateManagementService.java | 2 +- .../core/service/CertificateManagementServiceImpl.java | 7 ++++--- 7 files changed, 11 insertions(+), 17 deletions(-) diff --git a/components/certificate-mgt/org.wso2.carbon.certificate.mgt.core/src/main/java/org/wso2/carbon/certificate/mgt/core/config/CertificateManagementConfig.java b/components/certificate-mgt/org.wso2.carbon.certificate.mgt.core/src/main/java/org/wso2/carbon/certificate/mgt/core/config/CertificateManagementConfig.java index 79f67b148a..e778b1757c 100644 --- a/components/certificate-mgt/org.wso2.carbon.certificate.mgt.core/src/main/java/org/wso2/carbon/certificate/mgt/core/config/CertificateManagementConfig.java +++ b/components/certificate-mgt/org.wso2.carbon.certificate.mgt.core/src/main/java/org/wso2/carbon/certificate/mgt/core/config/CertificateManagementConfig.java @@ -16,7 +16,6 @@ * under the License. */ - package org.wso2.carbon.certificate.mgt.core.config; import javax.xml.bind.annotation.XmlElement; diff --git a/components/certificate-mgt/org.wso2.carbon.certificate.mgt.core/src/main/java/org/wso2/carbon/certificate/mgt/core/config/CertificateManagementRepository.java b/components/certificate-mgt/org.wso2.carbon.certificate.mgt.core/src/main/java/org/wso2/carbon/certificate/mgt/core/config/CertificateManagementRepository.java index 47ce800468..86cb11e29e 100644 --- a/components/certificate-mgt/org.wso2.carbon.certificate.mgt.core/src/main/java/org/wso2/carbon/certificate/mgt/core/config/CertificateManagementRepository.java +++ b/components/certificate-mgt/org.wso2.carbon.certificate.mgt.core/src/main/java/org/wso2/carbon/certificate/mgt/core/config/CertificateManagementRepository.java @@ -16,7 +16,6 @@ * under the License. */ - package org.wso2.carbon.certificate.mgt.core.config; import org.wso2.carbon.certificate.mgt.core.config.datasource.DataSourceConfig; diff --git a/components/certificate-mgt/org.wso2.carbon.certificate.mgt.core/src/main/java/org/wso2/carbon/certificate/mgt/core/config/datasource/DataSourceConfig.java b/components/certificate-mgt/org.wso2.carbon.certificate.mgt.core/src/main/java/org/wso2/carbon/certificate/mgt/core/config/datasource/DataSourceConfig.java index e9c72bb649..2c2871599f 100644 --- a/components/certificate-mgt/org.wso2.carbon.certificate.mgt.core/src/main/java/org/wso2/carbon/certificate/mgt/core/config/datasource/DataSourceConfig.java +++ b/components/certificate-mgt/org.wso2.carbon.certificate.mgt.core/src/main/java/org/wso2/carbon/certificate/mgt/core/config/datasource/DataSourceConfig.java @@ -38,9 +38,3 @@ public class DataSourceConfig { this.jndiLookupDefinition = jndiLookupDefinition; } } -// CREATE TABLE IF NOT EXISTS DM_DEVICE_CERTIFICATE ( -// ID INTEGER auto_increment NOT NULL, -// SERIAL_NUMBER VARCHAR(500) DEFAULT NULL, -// CERTIFICATE BLOB DEFAULT NULL, -// PRIMARY KEY (ID) -// ); diff --git a/components/certificate-mgt/org.wso2.carbon.certificate.mgt.core/src/main/java/org/wso2/carbon/certificate/mgt/core/dao/CertificateDAO.java b/components/certificate-mgt/org.wso2.carbon.certificate.mgt.core/src/main/java/org/wso2/carbon/certificate/mgt/core/dao/CertificateDAO.java index bffe744fb9..e6ce3ef65a 100644 --- a/components/certificate-mgt/org.wso2.carbon.certificate.mgt.core/src/main/java/org/wso2/carbon/certificate/mgt/core/dao/CertificateDAO.java +++ b/components/certificate-mgt/org.wso2.carbon.certificate.mgt.core/src/main/java/org/wso2/carbon/certificate/mgt/core/dao/CertificateDAO.java @@ -29,8 +29,9 @@ public interface CertificateDAO { /** * This can be used to store a certificate in the database, where it will be stored against the serial number * of the certificate. - * @param byteArrayInputStream Holds the certificate. - * @param serialNumber Serial number of the certificate. + * + * @param byteArrayInputStream Holds the certificate. + * @param serialNumber Serial number of the certificate. * @throws CertificateManagementDAOException */ void addCertificate(ByteArrayInputStream byteArrayInputStream, String serialNumber @@ -38,8 +39,9 @@ public interface CertificateDAO { /** * Usage is to obtain a certificate stored in the database by providing the serial number. - * @param serialNumber Serial number of the certificate. - * @return representation of the certificate. + * + * @param serialNumber Serial number of the certificate. + * @return representation of the certificate. * @throws CertificateManagementDAOException */ byte[] retrieveCertificate(String serialNumber diff --git a/components/certificate-mgt/org.wso2.carbon.certificate.mgt.core/src/main/java/org/wso2/carbon/certificate/mgt/core/dao/CertificateManagementDAOUtil.java b/components/certificate-mgt/org.wso2.carbon.certificate.mgt.core/src/main/java/org/wso2/carbon/certificate/mgt/core/dao/CertificateManagementDAOUtil.java index cbb5b1089a..4405ff8c6b 100644 --- a/components/certificate-mgt/org.wso2.carbon.certificate.mgt.core/src/main/java/org/wso2/carbon/certificate/mgt/core/dao/CertificateManagementDAOUtil.java +++ b/components/certificate-mgt/org.wso2.carbon.certificate.mgt.core/src/main/java/org/wso2/carbon/certificate/mgt/core/dao/CertificateManagementDAOUtil.java @@ -16,7 +16,6 @@ * under the License. */ - package org.wso2.carbon.certificate.mgt.core.dao; import org.apache.commons.logging.Log; diff --git a/components/certificate-mgt/org.wso2.carbon.certificate.mgt.core/src/main/java/org/wso2/carbon/certificate/mgt/core/service/CertificateManagementService.java b/components/certificate-mgt/org.wso2.carbon.certificate.mgt.core/src/main/java/org/wso2/carbon/certificate/mgt/core/service/CertificateManagementService.java index f89ab4f986..24d805c8f1 100644 --- a/components/certificate-mgt/org.wso2.carbon.certificate.mgt.core/src/main/java/org/wso2/carbon/certificate/mgt/core/service/CertificateManagementService.java +++ b/components/certificate-mgt/org.wso2.carbon.certificate.mgt.core/src/main/java/org/wso2/carbon/certificate/mgt/core/service/CertificateManagementService.java @@ -44,7 +44,7 @@ public interface CertificateManagementService { byte[] getPKIMessageSCEP(InputStream inputStream) throws KeystoreException; X509Certificate generateCertificateFromCSR(PrivateKey privateKey, PKCS10CertificationRequest request, - String issueSubject) throws KeystoreException; + String issueSubject) throws KeystoreException; Certificate getCertificateByAlias(String alias) throws KeystoreException; diff --git a/components/certificate-mgt/org.wso2.carbon.certificate.mgt.core/src/main/java/org/wso2/carbon/certificate/mgt/core/service/CertificateManagementServiceImpl.java b/components/certificate-mgt/org.wso2.carbon.certificate.mgt.core/src/main/java/org/wso2/carbon/certificate/mgt/core/service/CertificateManagementServiceImpl.java index 77dfe1686c..f3c355c6f1 100644 --- a/components/certificate-mgt/org.wso2.carbon.certificate.mgt.core/src/main/java/org/wso2/carbon/certificate/mgt/core/service/CertificateManagementServiceImpl.java +++ b/components/certificate-mgt/org.wso2.carbon.certificate.mgt.core/src/main/java/org/wso2/carbon/certificate/mgt/core/service/CertificateManagementServiceImpl.java @@ -39,7 +39,8 @@ public class CertificateManagementServiceImpl implements CertificateManagementSe private static KeyStoreReader keyStoreReader; private static CertificateGenerator certificateGenerator; - private CertificateManagementServiceImpl() {} + private CertificateManagementServiceImpl() { + } public static CertificateManagementServiceImpl getInstance() { @@ -80,8 +81,8 @@ public class CertificateManagementServiceImpl implements CertificateManagementSe } public X509Certificate generateCertificateFromCSR(PrivateKey privateKey, - PKCS10CertificationRequest request, - String issueSubject) throws KeystoreException { + PKCS10CertificationRequest request, + String issueSubject) throws KeystoreException { return certificateGenerator.generateCertificateFromCSR(privateKey, request, issueSubject); } From 9c7ebdafdb7dc0b12f4ea8e286a36bd7d881a15e Mon Sep 17 00:00:00 2001 From: pasinduj Date: Thu, 3 Dec 2015 18:30:45 +0530 Subject: [PATCH 2/3] modified PolicyDAOImpl to fix PolicyRemove error occured by a database Foriegn Key Reference --- .../wso2/carbon/policy/mgt/core/dao/impl/PolicyDAOImpl.java | 5 +++++ 1 file changed, 5 insertions(+) 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 7fd9d04d75..f78feca333 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 @@ -1282,6 +1282,11 @@ public class PolicyDAOImpl implements PolicyDAO { stmt.setInt(1, policyId); stmt.executeUpdate(); + String deleteComplianceStatus ="DELETE FROM DM_POLICY_COMPLIANCE_STATUS WHERE POLICY_ID =?"; + stmt = conn.prepareStatement(deleteComplianceStatus); + stmt.setInt(1, policyId); + stmt.executeUpdate(); + if (log.isDebugEnabled()) { log.debug("Policy (" + policyId + ") related configs deleted from database."); } From 30e8dc885c7a9550cb05b78bfb7ab2707ccffdaa Mon Sep 17 00:00:00 2001 From: harshanl Date: Thu, 3 Dec 2015 20:13:10 +0530 Subject: [PATCH 3/3] Fixed EMM-1034 --- .../operation/mgt/OperationManagerImpl.java | 1 - .../impl/PolicyAdministratorPointImpl.java | 46 ++++++++++++------- 2 files changed, 30 insertions(+), 17 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 7ef4b8f5da..71ea7adf72 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 @@ -28,7 +28,6 @@ import org.wso2.carbon.device.mgt.common.authorization.DeviceAccessAuthorization 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.common.operation.mgt.OperationManager; -import org.wso2.carbon.device.mgt.common.spi.DeviceManagementService; import org.wso2.carbon.device.mgt.core.DeviceManagementConstants; import org.wso2.carbon.device.mgt.core.dao.DeviceDAO; import org.wso2.carbon.device.mgt.core.dao.DeviceManagementDAOException; diff --git a/components/policy-mgt/org.wso2.carbon.policy.mgt.core/src/main/java/org/wso2/carbon/policy/mgt/core/impl/PolicyAdministratorPointImpl.java b/components/policy-mgt/org.wso2.carbon.policy.mgt.core/src/main/java/org/wso2/carbon/policy/mgt/core/impl/PolicyAdministratorPointImpl.java index 5e63355f95..b96e8f1cd7 100644 --- a/components/policy-mgt/org.wso2.carbon.policy.mgt.core/src/main/java/org/wso2/carbon/policy/mgt/core/impl/PolicyAdministratorPointImpl.java +++ b/components/policy-mgt/org.wso2.carbon.policy.mgt.core/src/main/java/org/wso2/carbon/policy/mgt/core/impl/PolicyAdministratorPointImpl.java @@ -41,6 +41,7 @@ import org.wso2.carbon.policy.mgt.core.util.PolicyManagementConstants; import java.util.HashMap; import java.util.List; import java.util.Map; +import java.util.Set; public class PolicyAdministratorPointImpl implements PolicyAdministratorPoint { @@ -123,36 +124,49 @@ public class PolicyAdministratorPointImpl implements PolicyAdministratorPoint { try { int tenantId = PrivilegedCarbonContext.getThreadLocalCarbonContext().getTenantId(); TaskService taskService = PolicyManagementDataHolder.getInstance().getTaskService(); - taskService.registerTaskType(PolicyManagementConstants.DELEGATION_TASK_TYPE); if (log.isDebugEnabled()) { log.debug("Policy delegations task is started for the tenant id " + tenantId); } TaskManager taskManager = taskService.getTaskManager(PolicyManagementConstants.DELEGATION_TASK_TYPE); - TaskInfo.TriggerInfo triggerInfo = new TaskInfo.TriggerInfo(); - triggerInfo.setRepeatCount(0); - Map properties = new HashMap<>(); properties.put(PolicyManagementConstants.TENANT_ID, String.valueOf(tenantId)); - String taskName = PolicyManagementConstants.DELEGATION_TASK_NAME + "_" + String.valueOf(tenantId); - if (!taskManager.isTaskScheduled(taskName)) { - - TaskInfo taskInfo = new TaskInfo(taskName, PolicyManagementConstants.DELEGATION_TASK_CLAZZ, - properties, triggerInfo); - - taskManager.registerTask(taskInfo); - taskManager.rescheduleTask(taskInfo.getName()); + Set registeredTaskTypes = taskService.getRegisteredTaskTypes(); + //Check whether the TaskType is already registered. If not we'll register it here. + if (!registeredTaskTypes.contains(PolicyManagementConstants.DELEGATION_TASK_TYPE)) { + taskService.registerTaskType(PolicyManagementConstants.DELEGATION_TASK_TYPE); + TaskInfo registeredTaskInfo = null; + // getTask method will throw a TaskException if the task is not registered. Hence we'll handle the + // exception and register the task. + try { + registeredTaskInfo = taskManager.getTask(taskName); + } catch (TaskException e) { + // No need of any specific logic to handle this exception as it is thrown if the task is not registered. + } finally { + // If registeredTaskInfo is null that means there's no registered delegation-task. + if (registeredTaskInfo == null) { + TaskInfo taskInfo = new TaskInfo(taskName, PolicyManagementConstants.DELEGATION_TASK_CLAZZ, + properties, triggerInfo); + taskManager.registerTask(taskInfo); + taskManager.scheduleTask(taskInfo.getName()); + } + } } else { - throw new PolicyManagementException("There is a task already running for policy changes. Please try " + - "to apply " + - "changes after few minutes."); + if (!taskManager.isTaskScheduled(taskName)) { + TaskInfo taskInfo = new TaskInfo(taskName, PolicyManagementConstants.DELEGATION_TASK_CLAZZ, + properties, triggerInfo); + taskManager.scheduleTask(taskInfo.getName()); + } else { + throw new PolicyManagementException("There is a task already running for policy changes. Please try " + + "to apply " + + "changes after few minutes."); + } } - } catch (TaskException e) { String msg = "Error occurred while creating the policy delegation task for tenant " + PrivilegedCarbonContext.