Refactor Email functionality

revert-70aa11f8
manoj 10 years ago
parent fc80514d74
commit 03b4f41a2e

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

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

@ -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;

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

@ -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<Runnable>(1000));
DeviceManagementConfig config = DeviceConfigurationManager.getInstance().getDeviceManagementConfig();
EmailConfigurations emailConfigurations = config.getDeviceManagementConfigRepository()
.getEmailConfigurations();
threadPoolExecutor = new ThreadPoolExecutor(emailConfigurations.getMinNumOfThread(),
emailConfigurations.getMaxNumOfThread(), emailConfigurations.getKeepAliveTime(),TimeUnit.SECONDS,
new LinkedBlockingQueue<Runnable>(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;

@ -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 =

@ -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<Device> getDeviceListOfUser(String username) throws DeviceManagementException;
void sendEnrollInvitation(EmailConfig config) throws DeviceManagementException;
void sendEnrollInvitation(EmailMessageProperties config) throws DeviceManagementException;
FeatureManager getFeatureManager(String type) throws DeviceManagementException;

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

@ -24,6 +24,12 @@
<Name>jdbc/DM_DS</Name>
</JndiLookupDefinition>
</DataSourceConfiguration>
<EmailClientConfiguration>
<minimumThread>8</minimumThread>
<maximumThread>100</maximumThread>
<keepAliveTime>20</keepAliveTime>
<ThreadQueueCapacity>1000</ThreadQueueCapacity>
</EmailClientConfiguration>
</ManagementRepository>
</DeviceMgtConfiguration>

Loading…
Cancel
Save