From 03b4f41a2e2c0c999966a73d616fba8d5c134bd7 Mon Sep 17 00:00:00 2001 From: manoj Date: Mon, 23 Mar 2015 20:52:08 +0530 Subject: [PATCH] Refactor Email functionality --- .../mgt/common/EmailMessageProperties.java | 18 +++++++++++++ .../DeviceManagementServiceProviderImpl.java | 25 +++++++++++-------- .../config/DeviceConfigurationManager.java | 1 + .../core/config/DeviceManagementConfig.java | 10 ++++---- .../sender/EmailServiceProviderImpl.java | 19 +++++++++----- .../DeviceManagementServiceComponent.java | 2 +- .../core/service/DeviceManagementService.java | 4 +-- .../service/DeviceManagementServiceImpl.java | 11 +++----- .../src/main/resources/conf/cdm-config.xml | 6 +++++ 9 files changed, 63 insertions(+), 33 deletions(-) diff --git a/components/device-mgt/org.wso2.carbon.device.mgt.common/src/main/java/org/wso2/carbon/device/mgt/common/EmailMessageProperties.java b/components/device-mgt/org.wso2.carbon.device.mgt.common/src/main/java/org/wso2/carbon/device/mgt/common/EmailMessageProperties.java index d4c9380333..0bc87c5087 100644 --- a/components/device-mgt/org.wso2.carbon.device.mgt.common/src/main/java/org/wso2/carbon/device/mgt/common/EmailMessageProperties.java +++ b/components/device-mgt/org.wso2.carbon.device.mgt.common/src/main/java/org/wso2/carbon/device/mgt/common/EmailMessageProperties.java @@ -25,6 +25,8 @@ public class EmailMessageProperties { private String[] ccList; private String[] bccList; private String subject; + private String firstName; + private String enrolmentUrl; public String getMessageBody() { return messageBody; @@ -65,4 +67,20 @@ public class EmailMessageProperties { public void setSubject(String subject) { this.subject = subject; } + + public String getFirstName() { + return firstName; + } + + public void setFirstName(String firstName) { + this.firstName = firstName; + } + + public String getEnrolmentUrl() { + return enrolmentUrl; + } + + public void setEnrolmentUrl(String enrolmentUrl) { + this.enrolmentUrl = enrolmentUrl; + } } diff --git a/components/device-mgt/org.wso2.carbon.device.mgt.core/src/main/java/org/wso2/carbon/device/mgt/core/DeviceManagementServiceProviderImpl.java b/components/device-mgt/org.wso2.carbon.device.mgt.core/src/main/java/org/wso2/carbon/device/mgt/core/DeviceManagementServiceProviderImpl.java index 94ff277c42..d7a8745b46 100644 --- a/components/device-mgt/org.wso2.carbon.device.mgt.core/src/main/java/org/wso2/carbon/device/mgt/core/DeviceManagementServiceProviderImpl.java +++ b/components/device-mgt/org.wso2.carbon.device.mgt.core/src/main/java/org/wso2/carbon/device/mgt/core/DeviceManagementServiceProviderImpl.java @@ -28,7 +28,7 @@ import org.wso2.carbon.device.mgt.common.operation.mgt.OperationManagementExcept import org.wso2.carbon.device.mgt.common.operation.mgt.OperationManager; import org.wso2.carbon.device.mgt.common.spi.DeviceManager; import org.wso2.carbon.device.mgt.core.config.DeviceConfigurationManager; -import org.wso2.carbon.device.mgt.core.config.EnrolmentNotifications; +import org.wso2.carbon.device.mgt.core.config.email.EnrolmentNotifications; 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; @@ -36,7 +36,7 @@ import org.wso2.carbon.device.mgt.core.dao.DeviceTypeDAO; import org.wso2.carbon.device.mgt.core.dao.util.DeviceManagementDAOUtil; import org.wso2.carbon.device.mgt.core.dto.DeviceType; import org.wso2.carbon.device.mgt.core.dto.Status; -import org.wso2.carbon.device.mgt.core.email.sender.EmailConfig; +import org.wso2.carbon.device.mgt.core.email.EmailConstants; import org.wso2.carbon.device.mgt.core.internal.EmailServiceDataHolder; import org.wso2.carbon.device.mgt.core.license.mgt.LicenseManagerImpl; import org.wso2.carbon.device.mgt.core.operation.mgt.OperationManagerImpl; @@ -231,15 +231,11 @@ public class DeviceManagementServiceProviderImpl implements DeviceManagementServ } @Override - public void sendEnrollInvitation(EmailConfig config) throws DeviceManagementException { + public void sendEnrollInvitation(EmailMessageProperties emailMessageProperties) throws DeviceManagementException { - EmailMessageProperties emailMessageProperties = new EmailMessageProperties(); EnrolmentNotifications enrolmentNotifications = DeviceConfigurationManager.getInstance() .getNotificationMessagesConfig().getEnrolmentNotifications(); - emailMessageProperties.setMailTo(new String[] { config.getAddress() }); - emailMessageProperties.setSubject(enrolmentNotifications.getSubject()); - String messageHeader = enrolmentNotifications.getHeader(); String messageBody = enrolmentNotifications.getBody(); String messageFooter = enrolmentNotifications.getFooter(); @@ -247,22 +243,29 @@ public class DeviceManagementServiceProviderImpl implements DeviceManagementServ StringBuilder messageBuilder = new StringBuilder(); try { - messageHeader = messageHeader.replaceAll("\\{title\\}", URLEncoder.encode(config.getSubject(), "UTF-8")); + messageHeader = messageHeader.replaceAll("\\{" + EmailConstants.EnrolmentEmailConstants.TITLE + "\\}", + URLEncoder.encode(emailMessageProperties.getSubject(), + EmailConstants.EnrolmentEmailConstants.ENCODED_SCHEME)); messageHeader = - messageHeader.replaceAll("\\{user-name\\}", URLEncoder.encode(config.getFirstName(), "UTF-8")); + messageHeader.replaceAll("\\{" + EmailConstants.EnrolmentEmailConstants.USERNAME + "\\}", + URLEncoder.encode(emailMessageProperties.getFirstName(), + EmailConstants.EnrolmentEmailConstants.ENCODED_SCHEME)); messageBody = messageBody + System.getProperty("line.separator") + enrolmentNotifications.getUrl() - .replaceAll("\\{downloadUrl\\}", URLEncoder.encode(config.getEnrollmentUrl(), "UTF-8")); + .replaceAll("\\{" + EmailConstants.EnrolmentEmailConstants.DOwN_LOAD_URL + "\\}", + URLDecoder.decode(emailMessageProperties.getEnrolmentUrl(), + EmailConstants.EnrolmentEmailConstants.ENCODED_SCHEME)); messageBuilder.append(messageHeader).append(System.getProperty("line.separator")).append( System.getProperty("line.separator")); messageBuilder.append(messageBody).append(System.getProperty("line.separator")).append( System.getProperty("line.separator")).append(messageFooter); + } catch (IOException e) { throw new DeviceManagementException("Error replacing tags in email template '" + - config.getSubject() + "'", e); + emailMessageProperties.getSubject() + "'", e); } emailMessageProperties.setMessageBody(messageBuilder.toString()); EmailServiceDataHolder.getInstance().getEmailServiceProvider().sendEmail(emailMessageProperties); diff --git a/components/device-mgt/org.wso2.carbon.device.mgt.core/src/main/java/org/wso2/carbon/device/mgt/core/config/DeviceConfigurationManager.java b/components/device-mgt/org.wso2.carbon.device.mgt.core/src/main/java/org/wso2/carbon/device/mgt/core/config/DeviceConfigurationManager.java index 0348f8a7f8..9eb45bde55 100644 --- a/components/device-mgt/org.wso2.carbon.device.mgt.core/src/main/java/org/wso2/carbon/device/mgt/core/config/DeviceConfigurationManager.java +++ b/components/device-mgt/org.wso2.carbon.device.mgt.core/src/main/java/org/wso2/carbon/device/mgt/core/config/DeviceConfigurationManager.java @@ -21,6 +21,7 @@ package org.wso2.carbon.device.mgt.core.config; import org.w3c.dom.Document; import org.wso2.carbon.device.mgt.common.DeviceManagementConstants; import org.wso2.carbon.device.mgt.common.DeviceManagementException; +import org.wso2.carbon.device.mgt.core.config.email.NotificationMessagesConfig; import org.wso2.carbon.device.mgt.core.util.DeviceManagerUtil; import org.wso2.carbon.utils.CarbonUtils; diff --git a/components/device-mgt/org.wso2.carbon.device.mgt.core/src/main/java/org/wso2/carbon/device/mgt/core/config/DeviceManagementConfig.java b/components/device-mgt/org.wso2.carbon.device.mgt.core/src/main/java/org/wso2/carbon/device/mgt/core/config/DeviceManagementConfig.java index 678f20d8f0..bdf7e9de8c 100644 --- a/components/device-mgt/org.wso2.carbon.device.mgt.core/src/main/java/org/wso2/carbon/device/mgt/core/config/DeviceManagementConfig.java +++ b/components/device-mgt/org.wso2.carbon.device.mgt.core/src/main/java/org/wso2/carbon/device/mgt/core/config/DeviceManagementConfig.java @@ -26,15 +26,15 @@ import javax.xml.bind.annotation.XmlRootElement; @XmlRootElement(name = "DeviceMgtConfiguration") public final class DeviceManagementConfig { - private DeviceManagementRepository deviceMgtRepository; + private DeviceManagementConfigRepository deviceManagementConfigRepository; @XmlElement(name = "ManagementRepository", required = true) - public DeviceManagementRepository getDeviceMgtRepository() { - return deviceMgtRepository; + public DeviceManagementConfigRepository getDeviceManagementConfigRepository() { + return deviceManagementConfigRepository; } - public void setDeviceMgtRepository(DeviceManagementRepository deviceMgtRepository) { - this.deviceMgtRepository = deviceMgtRepository; + public void setDeviceManagementConfigRepository(DeviceManagementConfigRepository deviceManagementConfigRepository) { + this.deviceManagementConfigRepository = deviceManagementConfigRepository; } } diff --git a/components/device-mgt/org.wso2.carbon.device.mgt.core/src/main/java/org/wso2/carbon/device/mgt/core/email/sender/EmailServiceProviderImpl.java b/components/device-mgt/org.wso2.carbon.device.mgt.core/src/main/java/org/wso2/carbon/device/mgt/core/email/sender/EmailServiceProviderImpl.java index cca14d909f..e365fc9b17 100644 --- a/components/device-mgt/org.wso2.carbon.device.mgt.core/src/main/java/org/wso2/carbon/device/mgt/core/email/sender/EmailServiceProviderImpl.java +++ b/components/device-mgt/org.wso2.carbon.device.mgt.core/src/main/java/org/wso2/carbon/device/mgt/core/email/sender/EmailServiceProviderImpl.java @@ -33,6 +33,9 @@ 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.EmailMessageProperties; +import org.wso2.carbon.device.mgt.core.config.DeviceConfigurationManager; +import org.wso2.carbon.device.mgt.core.config.DeviceManagementConfig; +import org.wso2.carbon.device.mgt.core.config.email.EmailConfigurations; import org.wso2.carbon.device.mgt.core.internal.EmailServiceDataHolder; import org.wso2.carbon.device.mgt.core.service.EmailService; @@ -45,9 +48,6 @@ import java.util.concurrent.TimeUnit; public class EmailServiceProviderImpl implements EmailService { private static ThreadPoolExecutor threadPoolExecutor; - private static final int MIN_THREAD = 8; - private static final int MAX_THREAD = 100; - private static final long DEFAULT_KEEP_ALIVE_TIME = 20; private static final String EMAIL_URI_SCHEME = "mailto:"; private static Log log = LogFactory.getLog(EmailServiceProviderImpl.class); @@ -58,14 +58,20 @@ public class EmailServiceProviderImpl implements EmailService { private void init() { if (threadPoolExecutor == null) { - threadPoolExecutor = new ThreadPoolExecutor(MIN_THREAD, MAX_THREAD, DEFAULT_KEEP_ALIVE_TIME, - TimeUnit.SECONDS, new LinkedBlockingQueue(1000)); + DeviceManagementConfig config = DeviceConfigurationManager.getInstance().getDeviceManagementConfig(); + EmailConfigurations emailConfigurations = config.getDeviceManagementConfigRepository() + .getEmailConfigurations(); + + + threadPoolExecutor = new ThreadPoolExecutor(emailConfigurations.getMinNumOfThread(), + emailConfigurations.getMaxNumOfThread(), emailConfigurations.getKeepAliveTime(),TimeUnit.SECONDS, + new LinkedBlockingQueue(emailConfigurations.getThreadQueueCapacity())); } } @Override public void sendEmail(EmailMessageProperties emailMessageProperties) throws DeviceManagementException { - for(String toAddr:emailMessageProperties.getMailTo()) { + for (String toAddr : emailMessageProperties.getMailTo()) { threadPoolExecutor .submit(new EmailSender(toAddr, emailMessageProperties.getSubject(), emailMessageProperties.getMessageBody())); @@ -73,6 +79,7 @@ public class EmailServiceProviderImpl implements EmailService { } class EmailSender implements Runnable { + String to; String subject; String body; 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 e2a6f5c974..69f7f7c27d 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 @@ -80,7 +80,7 @@ public class DeviceManagementServiceComponent { DeviceManagementConfig config = DeviceConfigurationManager.getInstance().getDeviceManagementConfig(); - DataSourceConfig dsConfig = config.getDeviceMgtRepository().getDataSourceConfig(); + DataSourceConfig dsConfig = config.getDeviceManagementConfigRepository().getDataSourceConfig(); DeviceManagementDAOFactory.init(dsConfig); DeviceManagementService deviceManagementProvider = diff --git a/components/device-mgt/org.wso2.carbon.device.mgt.core/src/main/java/org/wso2/carbon/device/mgt/core/service/DeviceManagementService.java b/components/device-mgt/org.wso2.carbon.device.mgt.core/src/main/java/org/wso2/carbon/device/mgt/core/service/DeviceManagementService.java index cdd487b91d..cbb826f542 100644 --- a/components/device-mgt/org.wso2.carbon.device.mgt.core/src/main/java/org/wso2/carbon/device/mgt/core/service/DeviceManagementService.java +++ b/components/device-mgt/org.wso2.carbon.device.mgt.core/src/main/java/org/wso2/carbon/device/mgt/core/service/DeviceManagementService.java @@ -24,8 +24,6 @@ import org.wso2.carbon.device.mgt.common.FeatureManager; import org.wso2.carbon.device.mgt.common.spi.DeviceManager; import org.wso2.carbon.device.mgt.common.license.mgt.LicenseManager; import org.wso2.carbon.device.mgt.common.operation.mgt.OperationManager; -import org.wso2.carbon.device.mgt.core.email.sender.EmailConfig; - import java.util.List; /** @@ -40,7 +38,7 @@ public interface DeviceManagementService extends DeviceManager, LicenseManager, List getDeviceListOfUser(String username) throws DeviceManagementException; - void sendEnrollInvitation(EmailConfig config) throws DeviceManagementException; + void sendEnrollInvitation(EmailMessageProperties config) throws DeviceManagementException; FeatureManager getFeatureManager(String type) throws DeviceManagementException; diff --git a/components/device-mgt/org.wso2.carbon.device.mgt.core/src/main/java/org/wso2/carbon/device/mgt/core/service/DeviceManagementServiceImpl.java b/components/device-mgt/org.wso2.carbon.device.mgt.core/src/main/java/org/wso2/carbon/device/mgt/core/service/DeviceManagementServiceImpl.java index 480c2f3329..69b9610ed5 100644 --- a/components/device-mgt/org.wso2.carbon.device.mgt.core/src/main/java/org/wso2/carbon/device/mgt/core/service/DeviceManagementServiceImpl.java +++ b/components/device-mgt/org.wso2.carbon.device.mgt.core/src/main/java/org/wso2/carbon/device/mgt/core/service/DeviceManagementServiceImpl.java @@ -17,15 +17,11 @@ */ package org.wso2.carbon.device.mgt.core.service; -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.FeatureManager; +import org.wso2.carbon.device.mgt.common.*; 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.operation.mgt.Operation; import org.wso2.carbon.device.mgt.common.operation.mgt.OperationManagementException; -import org.wso2.carbon.device.mgt.core.email.sender.EmailConfig; import org.wso2.carbon.device.mgt.core.internal.DeviceManagementDataHolder; import java.util.List; @@ -140,8 +136,9 @@ public class DeviceManagementServiceImpl implements DeviceManagementService { } @Override - public void sendEnrollInvitation(EmailConfig config) throws DeviceManagementException { - DeviceManagementDataHolder.getInstance().getDeviceManagementProvider().sendEnrollInvitation(config); + public void sendEnrollInvitation(EmailMessageProperties emailMessageProperties) throws DeviceManagementException { + DeviceManagementDataHolder.getInstance().getDeviceManagementProvider() + .sendEnrollInvitation(emailMessageProperties); } } diff --git a/features/device-mgt/org.wso2.carbon.device.mgt.server.feature/src/main/resources/conf/cdm-config.xml b/features/device-mgt/org.wso2.carbon.device.mgt.server.feature/src/main/resources/conf/cdm-config.xml index 5440bceb8e..0991207a54 100644 --- a/features/device-mgt/org.wso2.carbon.device.mgt.server.feature/src/main/resources/conf/cdm-config.xml +++ b/features/device-mgt/org.wso2.carbon.device.mgt.server.feature/src/main/resources/conf/cdm-config.xml @@ -24,6 +24,12 @@ jdbc/DM_DS + + 8 + 100 + 20 + 1000 +