From d71ea209c2d1a7d71cc3985ca89b2346e81865ad Mon Sep 17 00:00:00 2001 From: prabathabey Date: Mon, 23 Mar 2015 12:16:34 +0530 Subject: [PATCH] Code cleanup --- .../org.wso2.carbon.device.mgt.core/pom.xml | 1 - .../DeviceManagementServiceProviderImpl.java | 33 +++++---- .../mgt/core/email/sender/EmailConfig.java | 67 +++++++++++++++++++ .../sender}/EmailServiceProviderImpl.java | 12 +--- .../core/internal/EmailServiceComponent.java | 10 ++- .../core/service/DeviceManagementService.java | 4 +- .../service/DeviceManagementServiceImpl.java | 8 +-- 7 files changed, 94 insertions(+), 41 deletions(-) create mode 100644 components/device-mgt/org.wso2.carbon.device.mgt.core/src/main/java/org/wso2/carbon/device/mgt/core/email/sender/EmailConfig.java rename components/device-mgt/org.wso2.carbon.device.mgt.core/src/main/java/org/wso2/carbon/device/mgt/core/{ => email/sender}/EmailServiceProviderImpl.java (91%) 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 393d5aa2ed..ea493d6fa7 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 @@ -164,7 +164,6 @@ org.apache.axis2.transport axis2-transport-mail - org.apache.ws.commons.axiom axiom-api 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 96ef8378e1..ef71d3eb95 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 @@ -36,6 +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.internal.EmailServiceDataHolder; import org.wso2.carbon.device.mgt.core.license.mgt.LicenseManagerImpl; import org.wso2.carbon.device.mgt.core.operation.mgt.OperationManagerImpl; @@ -229,14 +230,13 @@ public class DeviceManagementServiceProviderImpl implements DeviceManagementServ } @Override - public void sendEnrollInvitation(String title, String userName, String emailAddress, String enrolUrl) throws - DeviceManagementException { + public void sendEnrollInvitation(EmailConfig config) throws DeviceManagementException { EmailMessageProperties emailMessageProperties = new EmailMessageProperties(); EnrolmentNotifications enrolmentNotifications = DeviceConfigurationManager.getInstance() .getNotificationMessagesConfig().getEnrolmentNotifications(); - emailMessageProperties.setMailTo(new String[] { emailAddress }); + emailMessageProperties.setMailTo(new String[] { config.getAddress() }); emailMessageProperties.setSubject(enrolmentNotifications.getSubject()); String messageHeader = enrolmentNotifications.getHeader(); @@ -246,28 +246,25 @@ public class DeviceManagementServiceProviderImpl implements DeviceManagementServ StringBuilder messageBuilder = new StringBuilder(); try { - messageHeader = messageHeader.replaceAll("\\{title\\}", - URLEncoder.encode(title, "UTF-8")); + messageHeader = messageHeader.replaceAll("\\{title\\}", URLEncoder.encode(config.getSubject(), "UTF-8")); - messageHeader = messageHeader.replaceAll("\\{user-name\\}", - URLEncoder.encode(userName, "UTF-8")); + messageHeader = + messageHeader.replaceAll("\\{user-name\\}", URLEncoder.encode(config.getFirstName(), "UTF-8")); - messageBody = messageBody+ System.getProperty("line.separator") + enrolmentNotifications.getUrl() - .replaceAll("\\{downloadUrl\\}", URLEncoder.encode(enrolUrl, "UTF-8")); + messageBody = messageBody + System.getProperty("line.separator") + enrolmentNotifications.getUrl() + .replaceAll("\\{downloadUrl\\}", URLEncoder.encode(config.getEnrollmentUrl(), "UTF-8")); - messageBuilder.append(messageHeader).append(System.getProperty("line.separator")) - .append(System.getProperty("line.separator")); + 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 ioEx) { - String errorMsg = "Error replacing tags in email template" + title; - log.error(errorMsg, ioEx); - throw new DeviceManagementException(errorMsg, ioEx); + 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.setMessageBody(messageBuilder.toString()); EmailServiceDataHolder.getInstance().getEmailServiceProvider().sendEmail(emailMessageProperties); - } @Override diff --git a/components/device-mgt/org.wso2.carbon.device.mgt.core/src/main/java/org/wso2/carbon/device/mgt/core/email/sender/EmailConfig.java b/components/device-mgt/org.wso2.carbon.device.mgt.core/src/main/java/org/wso2/carbon/device/mgt/core/email/sender/EmailConfig.java new file mode 100644 index 0000000000..3351cad16f --- /dev/null +++ b/components/device-mgt/org.wso2.carbon.device.mgt.core/src/main/java/org/wso2/carbon/device/mgt/core/email/sender/EmailConfig.java @@ -0,0 +1,67 @@ +/* + * 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.email.sender; + +public class EmailConfig { + + private String subject; + private String firstName; + private String address; + private String enrollmentUrl; + + public EmailConfig(String subject, String firstName, String address, String enrollmentUrl) { + this.subject = subject; + this.firstName = firstName; + this.address = address; + this.enrollmentUrl = enrollmentUrl; + } + + public String getSubject() { + return subject; + } + + public void setSubject(String subject) { + this.subject = subject; + } + + public String getFirstName() { + return firstName; + } + + public void setFirstName(String firstName) { + this.firstName = firstName; + } + + public String getAddress() { + return address; + } + + public void setAddress(String address) { + this.address = address; + } + + public String getEnrollmentUrl() { + return enrollmentUrl; + } + + public void setEnrollmentUrl(String enrollmentUrl) { + this.enrollmentUrl = enrollmentUrl; + } + +} diff --git a/components/device-mgt/org.wso2.carbon.device.mgt.core/src/main/java/org/wso2/carbon/device/mgt/core/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 similarity index 91% rename from components/device-mgt/org.wso2.carbon.device.mgt.core/src/main/java/org/wso2/carbon/device/mgt/core/EmailServiceProviderImpl.java rename to components/device-mgt/org.wso2.carbon.device.mgt.core/src/main/java/org/wso2/carbon/device/mgt/core/email/sender/EmailServiceProviderImpl.java index a52706753c..cca14d909f 100644 --- a/components/device-mgt/org.wso2.carbon.device.mgt.core/src/main/java/org/wso2/carbon/device/mgt/core/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 @@ -16,7 +16,7 @@ * under the License. */ -package org.wso2.carbon.device.mgt.core; +package org.wso2.carbon.device.mgt.core.email.sender; import org.apache.axiom.om.OMAbstractFactory; import org.apache.axiom.om.OMElement; @@ -73,7 +73,6 @@ public class EmailServiceProviderImpl implements EmailService { } class EmailSender implements Runnable { - String to; String subject; String body; @@ -110,14 +109,7 @@ public class EmailServiceProviderImpl implements EmailService { serviceClient.fireAndForget(payload); log.debug("Sending confirmation mail to " + to); } catch (AxisFault e) { - String msg = "Error in delivering the message, " + - "subject: " + subject + ", to: " + to + "."; - log.error(msg); - } catch (Throwable t) { - String msg = "Error in delivering the message, " + - "subject: " + subject + ", to: " + to + "."; - log.error(msg); - log.error(t); + log.error("Error in delivering the message, subject: '" + subject + "', to: '" + to + "'", e); } } } diff --git a/components/device-mgt/org.wso2.carbon.device.mgt.core/src/main/java/org/wso2/carbon/device/mgt/core/internal/EmailServiceComponent.java b/components/device-mgt/org.wso2.carbon.device.mgt.core/src/main/java/org/wso2/carbon/device/mgt/core/internal/EmailServiceComponent.java index 3e100d50e9..dfad072f8c 100644 --- a/components/device-mgt/org.wso2.carbon.device.mgt.core/src/main/java/org/wso2/carbon/device/mgt/core/internal/EmailServiceComponent.java +++ b/components/device-mgt/org.wso2.carbon.device.mgt.core/src/main/java/org/wso2/carbon/device/mgt/core/internal/EmailServiceComponent.java @@ -22,8 +22,7 @@ import org.apache.commons.logging.Log; import org.apache.commons.logging.LogFactory; import org.osgi.framework.BundleContext; import org.osgi.service.component.ComponentContext; -import org.wso2.carbon.device.mgt.core.DeviceManagementRepository; -import org.wso2.carbon.device.mgt.core.EmailServiceProviderImpl; +import org.wso2.carbon.device.mgt.core.email.sender.EmailServiceProviderImpl; import org.wso2.carbon.device.mgt.core.service.EmailService; import org.wso2.carbon.device.mgt.core.service.EmailServiceImpl; import org.wso2.carbon.utils.ConfigurationContextService; @@ -60,8 +59,7 @@ public class EmailServiceComponent { log.debug("Email management core bundle has been successfully initialized"); } } catch (Throwable e) { - String msg = "Error occurred while initializing device management core bundle"; - log.error(msg, e); + log.error("Error occurred while initializing device management core bundle", e); } } protected void setConfigurationContextService(ConfigurationContextService configurationContextService) { @@ -77,7 +75,7 @@ public class EmailServiceComponent { } /* Registering Email Service */ BundleContext bundleContext = componentContext.getBundleContext(); - bundleContext.registerService(EmailService.class.getName(), - new EmailServiceImpl(), null); + bundleContext.registerService(EmailService.class.getName(), new EmailServiceImpl(), null); } + } 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 225525bc61..cdd487b91d 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,6 +24,7 @@ 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; @@ -39,8 +40,7 @@ public interface DeviceManagementService extends DeviceManager, LicenseManager, List getDeviceListOfUser(String username) throws DeviceManagementException; - void sendEnrollInvitation(String title, String firstName, String emailAddress, - String enrolUrl) throws DeviceManagementException; + void sendEnrollInvitation(EmailConfig 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 f40240f6c1..480c2f3329 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 @@ -25,6 +25,7 @@ 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; @@ -139,9 +140,8 @@ public class DeviceManagementServiceImpl implements DeviceManagementService { } @Override - public void sendEnrollInvitation(String title, String userName, String emailAddress, String enrolUrl) - throws DeviceManagementException { - DeviceManagementDataHolder.getInstance().getDeviceManagementProvider() - .sendEnrollInvitation(title, userName, emailAddress, enrolUrl); + public void sendEnrollInvitation(EmailConfig config) throws DeviceManagementException { + DeviceManagementDataHolder.getInstance().getDeviceManagementProvider().sendEnrollInvitation(config); } + }