package in.notwork.notify.server.util;

import in.notwork.notify.client.util.PropertiesUtil;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import static in.notwork.notify.client.util.NotifyConstants.*;

/**
 * @author rishabh.
 */
public class ServerConfigurationValidator {

    private static final Logger LOG = LoggerFactory.getLogger(ServerConfigurationValidator.class);

    private static ServerConfigurationValidator ourInstance = new ServerConfigurationValidator();

    public static ServerConfigurationValidator getInstance() {
        return ourInstance;
    }

    private static String[] keysToCheck = new String[]{
            QUEUE_IMPL, QUEUE_NAME, QUEUE_USERNAME, QUEUE_PASSWORD, QUEUE_HOST,
            RMQ_DURABLE, RMQ_EXCLUSIVE, RMQ_AUTO_DELETE,
            EMAIL_SENDER_IMPL, SMS_SENDER_IMPL, NOTIF_SENDER_IMPL,
            MAIL_USERNAME, MAIL_PASSWORD, MAIL_DEBUG,
            MAIL_SMTP_OVER_SSL, MAIL_SMTP_OVER_TLS,
            MAIL_SMTP_AUTH, MAIL_SMTP_HOST, MAIL_SMTP_PORT,
            POOL_SIZE_EMAIL, POOL_SIZE_SMS, POOL_SIZE_PUSH
    };

    private ServerConfigurationValidator() {
    }

    public void validate() throws IllegalStateException {
        LOG.debug("Validating the server properties...");
        validateForEmptyOrMultipleValues();
        ValidateSMTPProperties();
    }

    private void ValidateSMTPProperties() {
        boolean tlsFlag = PropertiesUtil.getBooleanProperty(MAIL_SMTP_OVER_TLS);
        boolean sslFlag = PropertiesUtil.getBooleanProperty(MAIL_SMTP_OVER_SSL);
        if (tlsFlag && sslFlag) {
            LOG.error("Both SMTP-TLS and SMTP-SSL are enabled");
            throw new IllegalStateException("Cannot enable both SMTP-TLS and SMTP-SSL");
        }
        if (!tlsFlag && !sslFlag) {
            LOG.error("Both SMTP-TLS and SMTP-SSL are disabled. Please enable one.");
            throw new IllegalStateException("Cannot disable both SMTP-TLS and SMTP-SSL. Please enable one.");
        }
        if (tlsFlag) {
            checkProperty(MAIL_SMTP_STARTTTLS_ENABLE);
        }
        if (sslFlag) {
            checkProperty(MAIL_SMTP_SOCK_FACTORY_PORT);
            checkProperty(MAIL_SMTP_SOCK_FACTORY_CLASS);
        }
    }

    private void validateForEmptyOrMultipleValues() {
        String key = "";
        try {
            for (int i = 0; i < keysToCheck.length; i++) {
                key = keysToCheck[i];
                checkProperty(key);
            }
        } catch (ClassCastException e) {
            LOG.error("It seems more than one value has been configured for property: {}", key);
            throw new IllegalStateException("Multiple values configured for property: " + key);
        }
    }

    private void checkProperty(String key) {
        if (!isValidProperty(key)) {
            LOG.error("Property {} cannot be empty.", key);
            throw new IllegalStateException("Property " + key + " cannot be empty");
        }
    }

    private boolean isValidProperty(String key) {
        boolean flag = true;
        String property = PropertiesUtil.getProperty(key);
        if (null == property || property.isEmpty()) {
            flag = false;
        }
        return flag;
    }
}

@@ -243,16 +243,22 @@
 %0Apublic 
+final 
 class Se
@@ -410,204 +410,13 @@
 tic 
-ServerConfigurationValidator ourInstance = new ServerConfigurationValidator();%0A%0A    public static ServerConfigurationValidator getInstance() %7B%0A        return ourInstance;%0A    %7D%0A%0A    private static
+final
  Str
@@ -886,16 +886,213 @@
     %7D;%0A%0A
+    private static ServerConfigurationValidator ourInstance = new ServerConfigurationValidator();%0A%0A    public static ServerConfigurationValidator getInstance() %7B%0A        return ourInstance;%0A    %7D%0A%0A
     priv
@@ -1303,17 +1303,17 @@
         
-V
+v
 alidateS
@@ -1353,17 +1353,17 @@
 te void 
-V
+v
 alidateS
@@ -1380,32 +1380,38 @@
 ies() %7B%0A        
+final 
 boolean tlsFlag 
@@ -1467,32 +1467,38 @@
 R_TLS);%0A        
+final 
 boolean sslFlag 
@@ -2293,51 +2293,8 @@
 ) %7B%0A
-        String key = %22%22;%0A        try %7B%0A    
     
@@ -2360,33 +2360,13 @@
     
-    key = keysToCheck%5Bi%5D;
+try %7B
 %0A   
@@ -2399,25 +2399,26 @@
 (key
+sToCheck%5Bi%5D
 );%0A    
- 
-       %7D%0A
     
@@ -2450,24 +2450,28 @@
 eption e) %7B%0A
+    
             
@@ -2548,27 +2548,41 @@
 ty: %7B%7D%22, key
-);%0A
+sToCheck%5Bi%5D);%0A   
             
@@ -2573,32 +2573,33 @@
 %0A               
+ 
 throw new Illega
@@ -2659,26 +2659,54 @@
 rty: %22 + key
-);
+sToCheck%5Bi%5D, e);%0A            %7D
 %0A        %7D%0A 
@@ -2734,32 +2734,38 @@
 d checkProperty(
+final 
 String key) %7B%0A  
@@ -2997,16 +2997,22 @@
 roperty(
+final 
 String k
@@ -3054,16 +3054,22 @@
         
+final 
 String p

Fix some PMD violations which make sense.