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[] ccList;
private String[] bccList; private String[] bccList;
private String subject; private String subject;
private String firstName;
private String enrolmentUrl;
public String getMessageBody() { public String getMessageBody() {
return messageBody; return messageBody;
@ -65,4 +67,20 @@ public class EmailMessageProperties {
public void setSubject(String subject) { public void setSubject(String subject) {
this.subject = 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.operation.mgt.OperationManager;
import org.wso2.carbon.device.mgt.common.spi.DeviceManager; 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.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.DeviceDAO;
import org.wso2.carbon.device.mgt.core.dao.DeviceManagementDAOException; 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.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.dao.util.DeviceManagementDAOUtil;
import org.wso2.carbon.device.mgt.core.dto.DeviceType; 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.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.internal.EmailServiceDataHolder;
import org.wso2.carbon.device.mgt.core.license.mgt.LicenseManagerImpl; import org.wso2.carbon.device.mgt.core.license.mgt.LicenseManagerImpl;
import org.wso2.carbon.device.mgt.core.operation.mgt.OperationManagerImpl; import org.wso2.carbon.device.mgt.core.operation.mgt.OperationManagerImpl;
@ -231,15 +231,11 @@ public class DeviceManagementServiceProviderImpl implements DeviceManagementServ
} }
@Override @Override
public void sendEnrollInvitation(EmailConfig config) throws DeviceManagementException { public void sendEnrollInvitation(EmailMessageProperties emailMessageProperties) throws DeviceManagementException {
EmailMessageProperties emailMessageProperties = new EmailMessageProperties();
EnrolmentNotifications enrolmentNotifications = DeviceConfigurationManager.getInstance() EnrolmentNotifications enrolmentNotifications = DeviceConfigurationManager.getInstance()
.getNotificationMessagesConfig().getEnrolmentNotifications(); .getNotificationMessagesConfig().getEnrolmentNotifications();
emailMessageProperties.setMailTo(new String[] { config.getAddress() });
emailMessageProperties.setSubject(enrolmentNotifications.getSubject());
String messageHeader = enrolmentNotifications.getHeader(); String messageHeader = enrolmentNotifications.getHeader();
String messageBody = enrolmentNotifications.getBody(); String messageBody = enrolmentNotifications.getBody();
String messageFooter = enrolmentNotifications.getFooter(); String messageFooter = enrolmentNotifications.getFooter();
@ -247,22 +243,29 @@ public class DeviceManagementServiceProviderImpl implements DeviceManagementServ
StringBuilder messageBuilder = new StringBuilder(); StringBuilder messageBuilder = new StringBuilder();
try { 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 =
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() 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( messageBuilder.append(messageHeader).append(System.getProperty("line.separator")).append(
System.getProperty("line.separator")); System.getProperty("line.separator"));
messageBuilder.append(messageBody).append(System.getProperty("line.separator")).append( messageBuilder.append(messageBody).append(System.getProperty("line.separator")).append(
System.getProperty("line.separator")).append(messageFooter); System.getProperty("line.separator")).append(messageFooter);
} catch (IOException e) { } catch (IOException e) {
throw new DeviceManagementException("Error replacing tags in email template '" + throw new DeviceManagementException("Error replacing tags in email template '" +
config.getSubject() + "'", e); emailMessageProperties.getSubject() + "'", e);
} }
emailMessageProperties.setMessageBody(messageBuilder.toString()); emailMessageProperties.setMessageBody(messageBuilder.toString());
EmailServiceDataHolder.getInstance().getEmailServiceProvider().sendEmail(emailMessageProperties); EmailServiceDataHolder.getInstance().getEmailServiceProvider().sendEmail(emailMessageProperties);

@ -21,6 +21,7 @@ package org.wso2.carbon.device.mgt.core.config;
import org.w3c.dom.Document; import org.w3c.dom.Document;
import org.wso2.carbon.device.mgt.common.DeviceManagementConstants; import org.wso2.carbon.device.mgt.common.DeviceManagementConstants;
import org.wso2.carbon.device.mgt.common.DeviceManagementException; 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.device.mgt.core.util.DeviceManagerUtil;
import org.wso2.carbon.utils.CarbonUtils; import org.wso2.carbon.utils.CarbonUtils;

@ -26,15 +26,15 @@ import javax.xml.bind.annotation.XmlRootElement;
@XmlRootElement(name = "DeviceMgtConfiguration") @XmlRootElement(name = "DeviceMgtConfiguration")
public final class DeviceManagementConfig { public final class DeviceManagementConfig {
private DeviceManagementRepository deviceMgtRepository; private DeviceManagementConfigRepository deviceManagementConfigRepository;
@XmlElement(name = "ManagementRepository", required = true) @XmlElement(name = "ManagementRepository", required = true)
public DeviceManagementRepository getDeviceMgtRepository() { public DeviceManagementConfigRepository getDeviceManagementConfigRepository() {
return deviceMgtRepository; return deviceManagementConfigRepository;
} }
public void setDeviceMgtRepository(DeviceManagementRepository deviceMgtRepository) { public void setDeviceManagementConfigRepository(DeviceManagementConfigRepository deviceManagementConfigRepository) {
this.deviceMgtRepository = deviceMgtRepository; this.deviceManagementConfigRepository = deviceManagementConfigRepository;
} }
} }

@ -33,6 +33,9 @@ import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory; import org.apache.commons.logging.LogFactory;
import org.wso2.carbon.device.mgt.common.DeviceManagementException; import org.wso2.carbon.device.mgt.common.DeviceManagementException;
import org.wso2.carbon.device.mgt.common.EmailMessageProperties; 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.internal.EmailServiceDataHolder;
import org.wso2.carbon.device.mgt.core.service.EmailService; import org.wso2.carbon.device.mgt.core.service.EmailService;
@ -45,9 +48,6 @@ import java.util.concurrent.TimeUnit;
public class EmailServiceProviderImpl implements EmailService { public class EmailServiceProviderImpl implements EmailService {
private static ThreadPoolExecutor threadPoolExecutor; 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 final String EMAIL_URI_SCHEME = "mailto:";
private static Log log = LogFactory.getLog(EmailServiceProviderImpl.class); private static Log log = LogFactory.getLog(EmailServiceProviderImpl.class);
@ -58,8 +58,14 @@ public class EmailServiceProviderImpl implements EmailService {
private void init() { private void init() {
if (threadPoolExecutor == null) { if (threadPoolExecutor == null) {
threadPoolExecutor = new ThreadPoolExecutor(MIN_THREAD, MAX_THREAD, DEFAULT_KEEP_ALIVE_TIME, DeviceManagementConfig config = DeviceConfigurationManager.getInstance().getDeviceManagementConfig();
TimeUnit.SECONDS, new LinkedBlockingQueue<Runnable>(1000)); EmailConfigurations emailConfigurations = config.getDeviceManagementConfigRepository()
.getEmailConfigurations();
threadPoolExecutor = new ThreadPoolExecutor(emailConfigurations.getMinNumOfThread(),
emailConfigurations.getMaxNumOfThread(), emailConfigurations.getKeepAliveTime(),TimeUnit.SECONDS,
new LinkedBlockingQueue<Runnable>(emailConfigurations.getThreadQueueCapacity()));
} }
} }
@ -73,6 +79,7 @@ public class EmailServiceProviderImpl implements EmailService {
} }
class EmailSender implements Runnable { class EmailSender implements Runnable {
String to; String to;
String subject; String subject;
String body; String body;

@ -80,7 +80,7 @@ public class DeviceManagementServiceComponent {
DeviceManagementConfig config = DeviceManagementConfig config =
DeviceConfigurationManager.getInstance().getDeviceManagementConfig(); DeviceConfigurationManager.getInstance().getDeviceManagementConfig();
DataSourceConfig dsConfig = config.getDeviceMgtRepository().getDataSourceConfig(); DataSourceConfig dsConfig = config.getDeviceManagementConfigRepository().getDataSourceConfig();
DeviceManagementDAOFactory.init(dsConfig); DeviceManagementDAOFactory.init(dsConfig);
DeviceManagementService deviceManagementProvider = 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.spi.DeviceManager;
import org.wso2.carbon.device.mgt.common.license.mgt.LicenseManager; 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.common.operation.mgt.OperationManager;
import org.wso2.carbon.device.mgt.core.email.sender.EmailConfig;
import java.util.List; import java.util.List;
/** /**
@ -40,7 +38,7 @@ public interface DeviceManagementService extends DeviceManager, LicenseManager,
List<Device> getDeviceListOfUser(String username) throws DeviceManagementException; 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; FeatureManager getFeatureManager(String type) throws DeviceManagementException;

@ -17,15 +17,11 @@
*/ */
package org.wso2.carbon.device.mgt.core.service; package org.wso2.carbon.device.mgt.core.service;
import org.wso2.carbon.device.mgt.common.Device; import org.wso2.carbon.device.mgt.common.*;
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.license.mgt.License; 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.LicenseManagementException;
import org.wso2.carbon.device.mgt.common.operation.mgt.Operation; 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.OperationManagementException;
import org.wso2.carbon.device.mgt.core.email.sender.EmailConfig;
import org.wso2.carbon.device.mgt.core.internal.DeviceManagementDataHolder; import org.wso2.carbon.device.mgt.core.internal.DeviceManagementDataHolder;
import java.util.List; import java.util.List;
@ -140,8 +136,9 @@ public class DeviceManagementServiceImpl implements DeviceManagementService {
} }
@Override @Override
public void sendEnrollInvitation(EmailConfig config) throws DeviceManagementException { public void sendEnrollInvitation(EmailMessageProperties emailMessageProperties) throws DeviceManagementException {
DeviceManagementDataHolder.getInstance().getDeviceManagementProvider().sendEnrollInvitation(config); DeviceManagementDataHolder.getInstance().getDeviceManagementProvider()
.sendEnrollInvitation(emailMessageProperties);
} }
} }

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

Loading…
Cancel
Save