Martin Peřina has uploaded a new change for review.

Change subject: tools: Tests config options validity during notifier startup
......................................................................

tools: Tests config options validity during notifier startup

Moves all tests of configuration options to notifier startup code.

Change-Id: I3d9f36aee42e51b4903ccf388ca4b328e1740507
Signed-off-by: Martin Perina <[email protected]>
---
M 
backend/manager/tools/src/main/java/org/ovirt/engine/core/notifier/Notifier.java
M 
backend/manager/tools/src/main/java/org/ovirt/engine/core/notifier/utils/NotificationProperties.java
M 
backend/manager/tools/src/main/java/org/ovirt/engine/core/notifier/utils/sender/mail/JavaMailSender.java
3 files changed, 70 insertions(+), 90 deletions(-)


  git pull ssh://gerrit.ovirt.org:29418/ovirt-engine refs/changes/94/22294/1

diff --git 
a/backend/manager/tools/src/main/java/org/ovirt/engine/core/notifier/Notifier.java
 
b/backend/manager/tools/src/main/java/org/ovirt/engine/core/notifier/Notifier.java
index be9cace..b21c82b 100644
--- 
a/backend/manager/tools/src/main/java/org/ovirt/engine/core/notifier/Notifier.java
+++ 
b/backend/manager/tools/src/main/java/org/ovirt/engine/core/notifier/Notifier.java
@@ -1,6 +1,5 @@
 package org.ovirt.engine.core.notifier;
 
-import java.net.InetAddress;
 import java.net.MalformedURLException;
 import java.net.URL;
 import java.util.ArrayList;
@@ -10,7 +9,6 @@
 import java.util.concurrent.ScheduledFuture;
 import java.util.concurrent.TimeUnit;
 
-import javax.mail.internet.InternetAddress;
 import javax.xml.parsers.FactoryConfigurationError;
 
 import org.apache.commons.lang.StringUtils;
@@ -60,50 +58,7 @@
         EngineMonitorService engineMonitorService = null;
         try {
             NotificationProperties prop = NotificationProperties.getInstance();
-
-            for (String property : new String[] {
-                NotificationProperties.DAYS_TO_KEEP_HISTORY,
-                NotificationProperties.ENGINE_INTERVAL_IN_SECONDS,
-                NotificationProperties.ENGINE_TIMEOUT_IN_SECONDS,
-                NotificationProperties.INTERVAL_IN_SECONDS,
-                NotificationProperties.IS_HTTPS_PROTOCOL,
-                NotificationProperties.MAIL_PORT,
-                NotificationProperties.MAIL_SERVER,
-                NotificationProperties.REPEAT_NON_RESPONSIVE_NOTIFICATION,
-            }) {
-                if (StringUtils.isEmpty(prop.getProperty(property))) {
-                    throw new IllegalArgumentException(
-                        String.format(
-                            "Check configuration file, '%s' is missing",
-                            property
-                        )
-                    );
-                }
-            }
-
-            
InetAddress.getAllByName(prop.getProperty(NotificationProperties.MAIL_SERVER));
-
-            for (String property : new String[] {
-                NotificationProperties.MAIL_USER,
-                NotificationProperties.MAIL_FROM,
-                NotificationProperties.MAIL_REPLY_TO,
-            }) {
-                String candidate = prop.getProperty(property);
-                if (!StringUtils.isEmpty(candidate)) {
-                    try {
-                        new InternetAddress(candidate);
-                    }
-                    catch(Exception e) {
-                        throw new IllegalArgumentException(
-                            String.format(
-                                "Check configuration file, invalid format in 
'%s'",
-                                property
-                            ),
-                            e
-                        );
-                    }
-                }
-            }
+            prop.validate();
 
             notificationService = new NotificationService(prop);
             engineMonitorService = new EngineMonitorService(prop);
diff --git 
a/backend/manager/tools/src/main/java/org/ovirt/engine/core/notifier/utils/NotificationProperties.java
 
b/backend/manager/tools/src/main/java/org/ovirt/engine/core/notifier/utils/NotificationProperties.java
index c8ce386..9a57df7 100644
--- 
a/backend/manager/tools/src/main/java/org/ovirt/engine/core/notifier/utils/NotificationProperties.java
+++ 
b/backend/manager/tools/src/main/java/org/ovirt/engine/core/notifier/utils/NotificationProperties.java
@@ -1,5 +1,10 @@
 package org.ovirt.engine.core.notifier.utils;
 
+import java.net.InetAddress;
+
+import javax.mail.internet.InternetAddress;
+
+import org.apache.commons.lang.StringUtils;
 import org.apache.log4j.Logger;
 import org.ovirt.engine.core.utils.LocalConfig;
 
@@ -96,4 +101,68 @@
         loadConfig(defaultsPath, varsPath);
     }
 
+    /**
+     * Validates properties values.
+     * @throws IllegalArgumentException if some properties has invalid values
+     */
+    public void validate() {
+        // validate mandatory properties
+        for (String property : new String[] {
+                NotificationProperties.DAYS_TO_KEEP_HISTORY,
+                NotificationProperties.ENGINE_INTERVAL_IN_SECONDS,
+                NotificationProperties.ENGINE_TIMEOUT_IN_SECONDS,
+                NotificationProperties.INTERVAL_IN_SECONDS,
+                NotificationProperties.IS_HTTPS_PROTOCOL,
+                NotificationProperties.MAIL_PORT,
+                NotificationProperties.MAIL_SERVER,
+                NotificationProperties.REPEAT_NON_RESPONSIVE_NOTIFICATION }) {
+            if (StringUtils.isEmpty(getProperty(property))) {
+                throw new IllegalArgumentException(
+                        String.format(
+                                "Check configuration file, '%s' is missing",
+                                property));
+            }
+        }
+
+        // try to resolve MAIL_SERVER host
+        try {
+            
InetAddress.getAllByName(getProperty(NotificationProperties.MAIL_SERVER));
+        } catch (Exception ex) {
+            throw new IllegalArgumentException(
+                    String.format(
+                            "Check configuration file, cannot verify '%s' 
value",
+                            NotificationProperties.MAIL_SERVER),
+                    ex);
+        }
+
+        // validate email addresses
+        for (String property : new String[] {
+                NotificationProperties.MAIL_USER,
+                NotificationProperties.MAIL_FROM,
+                NotificationProperties.MAIL_REPLY_TO }) {
+            String candidate = getProperty(property);
+            if (!StringUtils.isEmpty(candidate)) {
+                try {
+                    new InternetAddress(candidate);
+                } catch(Exception ex) {
+                    throw new IllegalArgumentException(
+                            String.format(
+                                    "Check configuration file, invalid format 
in '%s'",
+                                    property),
+                            ex);
+                }
+            }
+        }
+
+        // validate mail user value
+        String emailUser = getProperty(NotificationProperties.MAIL_USER, true);
+        if (StringUtils.isEmpty(emailUser)
+                && (getBoolean(NotificationProperties.MAIL_ENABLE_SSL, false)
+                        || 
StringUtils.isNotEmpty(getProperty(NotificationProperties.MAIL_PASSWORD, 
true)))) {
+                throw new IllegalArgumentException(
+                        String.format(
+                                "'%s' must be set when SSL is enabled or when 
password is set",
+                                NotificationProperties.MAIL_USER));
+        }
+    }
 }
diff --git 
a/backend/manager/tools/src/main/java/org/ovirt/engine/core/notifier/utils/sender/mail/JavaMailSender.java
 
b/backend/manager/tools/src/main/java/org/ovirt/engine/core/notifier/utils/sender/mail/JavaMailSender.java
index 60675ef..fd705a7 100644
--- 
a/backend/manager/tools/src/main/java/org/ovirt/engine/core/notifier/utils/sender/mail/JavaMailSender.java
+++ 
b/backend/manager/tools/src/main/java/org/ovirt/engine/core/notifier/utils/sender/mail/JavaMailSender.java
@@ -1,6 +1,5 @@
 package org.ovirt.engine.core.notifier.utils.sender.mail;
 
-import java.text.MessageFormat;
 import java.util.Date;
 import java.util.Properties;
 
@@ -11,7 +10,6 @@
 import javax.mail.PasswordAuthentication;
 import javax.mail.Session;
 import javax.mail.Transport;
-import javax.mail.internet.AddressException;
 import javax.mail.internet.InternetAddress;
 import javax.mail.internet.MimeMessage;
 
@@ -71,48 +69,6 @@
         Properties mailSessionProps = new Properties();
         if (log.isTraceEnabled()) {
             mailSessionProps.put("mail.debug", "true");
-        }
-
-        String emailUser = 
aMailProps.getProperty(NotificationProperties.MAIL_USER, true);
-        if (StringUtils.isEmpty(emailUser)) {
-            if (aMailProps.getBoolean(NotificationProperties.MAIL_ENABLE_SSL, 
false) ||
-                    
StringUtils.isNotEmpty(aMailProps.getProperty(NotificationProperties.MAIL_PASSWORD,
 true))) {
-                throw new 
IllegalArgumentException(NotificationProperties.MAIL_USER
-                        + " must be set when SSL is enabled or when password 
is set");
-            } else {
-                log.warn(NotificationProperties.MAIL_USER
-                        + " property is empty in notification service 
configuration file");
-            }
-        }
-
-        if 
(StringUtils.isNotEmpty(aMailProps.getProperty(NotificationProperties.MAIL_FROM,
 true))) {
-            try {
-                from = new 
InternetAddress(aMailProps.getProperty(NotificationProperties.MAIL_FROM));
-            } catch (AddressException e) {
-                log.error(MessageFormat.format("Failed to parse 'from' user 
{0} provided by property {1}",
-                        
aMailProps.getProperty(NotificationProperties.MAIL_FROM),
-                        NotificationProperties.MAIL_FROM), e);
-            }
-        } else {
-            try {
-                if (StringUtils.isNotEmpty(emailUser)) {
-                    from = new InternetAddress(emailUser);
-                }
-            } catch (AddressException e) {
-                log.error(MessageFormat.format("Failed to parse 'email user' 
{0} provided by property {1}",
-                        emailUser,
-                        NotificationProperties.MAIL_USER), e);
-            }
-        }
-
-        if 
(StringUtils.isNotEmpty(aMailProps.getProperty(NotificationProperties.MAIL_REPLY_TO,
 true))) {
-            try {
-                replyTo = new 
InternetAddress(aMailProps.getProperty(NotificationProperties.MAIL_REPLY_TO));
-            } catch (AddressException e) {
-                log.error(MessageFormat.format("Failed to parse 'replyTo' 
email {0} provided by property {1}",
-                        
aMailProps.getProperty(NotificationProperties.MAIL_REPLY_TO),
-                        NotificationProperties.MAIL_REPLY_TO), e);
-            }
         }
 
         isBodyHtml = 
aMailProps.getBoolean(NotificationProperties.HTML_MESSAGE_FORMAT, false);


-- 
To view, visit http://gerrit.ovirt.org/22294
To unsubscribe, visit http://gerrit.ovirt.org/settings

Gerrit-MessageType: newchange
Gerrit-Change-Id: I3d9f36aee42e51b4903ccf388ca4b328e1740507
Gerrit-PatchSet: 1
Gerrit-Project: ovirt-engine
Gerrit-Branch: master
Gerrit-Owner: Martin Peřina <[email protected]>
_______________________________________________
Engine-patches mailing list
[email protected]
http://lists.ovirt.org/mailman/listinfo/engine-patches

Reply via email to