You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@isis.apache.org by da...@apache.org on 2019/09/30 07:32:21 UTC

[isis] 01/04: ISIS-2086: refactors 'isis.service.email.*' (about a dozen of them) to type-safe config

This is an automated email from the ASF dual-hosted git repository.

danhaywood pushed a commit to branch ISIS-2086
in repository https://gitbox.apache.org/repos/asf/isis.git

commit bf14d5f91c3323a27360d9918c04e23f0b492f01
Author: danhaywood <da...@haywood-associates.co.uk>
AuthorDate: Mon Sep 30 08:26:10 2019 +0100

    ISIS-2086: refactors 'isis.service.email.*' (about a dozen of them) to type-safe config
---
 .../org/apache/isis/config/IsisConfiguration.java  | 44 +++++++++++++++++
 .../services/email/EmailServiceDefault.java        | 56 ++++++----------------
 2 files changed, 59 insertions(+), 41 deletions(-)

diff --git a/core/config/src/main/java/org/apache/isis/config/IsisConfiguration.java b/core/config/src/main/java/org/apache/isis/config/IsisConfiguration.java
index 935ba60..14d3dd6 100644
--- a/core/config/src/main/java/org/apache/isis/config/IsisConfiguration.java
+++ b/core/config/src/main/java/org/apache/isis/config/IsisConfiguration.java
@@ -295,6 +295,50 @@ public class IsisConfiguration {
         }
     }
 
+    private final Service service = new Service();
+    @Data
+    public static class Service {
+        // isis.service.email.sender.username
+        // isis.service.email.sender.address
+        // isis.service.email.sender.password
+        // isis.service.email.sender.hostname
+        // isis.service.email.port
+        // isis.service.email.socketTimeout
+        // isis.service.email.socketConnectionTimeout
+        // isis.service.email.tls.enabled
+        // isis.service.email.throwExceptionOnFail
+        // isis.service.email.override.to
+        // isis.service.email.override.cc
+        // isis.service.email.override.bcc
+        private final Email email = new Email();
+        @Data
+        public static class Email {
+            private int port = 587;
+            private int socketConnectionTimeout = 2000;
+            private int socketTimeout = 2000;
+            private boolean throwExceptionOnFail = true;
+            private final Override override = new Override();
+            @Data
+            public static class Override {
+                private String to;
+                private String cc;
+                private String bcc;
+            }
+            private final Sender sender = new Sender();
+            @Data
+            public static class Sender {
+                private String username;
+                private String address;
+                private String password;
+                private String hostname;
+            }
+            private final Tls tls = new Tls();
+            @Data
+            public static class Tls {
+                private boolean enabled = true;
+            }
+        }
+    }
     private final Services services = new Services();
     @Data
     public static class Services {
diff --git a/core/runtime-services/src/main/java/org/apache/isis/runtime/services/email/EmailServiceDefault.java b/core/runtime-services/src/main/java/org/apache/isis/runtime/services/email/EmailServiceDefault.java
index 5cf403a..449ee2d 100644
--- a/core/runtime-services/src/main/java/org/apache/isis/runtime/services/email/EmailServiceDefault.java
+++ b/core/runtime-services/src/main/java/org/apache/isis/runtime/services/email/EmailServiceDefault.java
@@ -35,6 +35,7 @@ import org.apache.isis.applib.annotation.DomainService;
 import org.apache.isis.applib.annotation.NatureOfService;
 import org.apache.isis.applib.services.email.EmailService;
 import org.apache.isis.commons.internal.base._Strings;
+import org.apache.isis.config.IsisConfiguration;
 import org.apache.isis.config.IsisConfigurationLegacy;
 
 import lombok.extern.log4j.Log4j2;
@@ -56,34 +57,6 @@ public class EmailServiceDefault implements EmailService {
         }
     }
 
-    // -- CONSTANTS
-
-    private static final String ISIS_SERVICE_EMAIL_SENDER_USERNAME = "isis.service.email.sender.username";
-    private static final String ISIS_SERVICE_EMAIL_SENDER_ADDRESS = "isis.service.email.sender.address";
-    private static final String ISIS_SERVICE_EMAIL_SENDER_PASSWORD = "isis.service.email.sender.password";
-
-    private static final String ISIS_SERVICE_EMAIL_SENDER_HOSTNAME = "isis.service.email.sender.hostname";
-    private static final String ISIS_SERVICE_EMAIL_SENDER_HOSTNAME_DEFAULT = "smtp.gmail.com";
-
-    private static final String ISIS_SERVICE_EMAIL_PORT = "isis.service.email.port";
-    private static final int ISIS_SERVICE_EMAIL_PORT_DEFAULT = 587;
-
-    private static final String ISIS_SERVICE_EMAIL_TLS_ENABLED = "isis.service.email.tls.enabled";
-    private static final boolean ISIS_SERVICE_EMAIL_TLS_ENABLED_DEFAULT = true;
-
-    private static final String ISIS_SERVICE_EMAIL_THROW_EXCEPTION_ON_FAIL = "isis.service.email.throwExceptionOnFail";
-    private static final boolean ISIS_SERVICE_EMAIL_THROW_EXCEPTION_ON_FAIL_DEFAULT = true;
-
-    private static final String ISIS_SERVICE_EMAIL_SOCKET_TIMEOUT = "isis.service.email.socketTimeout";
-    private static final int ISIS_SERVICE_EMAIL_SOCKET_TIMEOUT_DEFAULT = 2000;
-
-    private static final String ISIS_SERVICE_EMAIL_SOCKET_CONNECTION_TIMEOUT = "isis.service.email.socketConnectionTimeout";
-    private static final int ISIS_SERVICE_EMAIL_SOCKET_CONNECTION_TIMEOUT_DEFAULT = 2000;
-
-    private static final String ISIS_SERVICE_EMAIL_OVERRIDE_TO = "isis.service.email.override.to";
-    private static final String ISIS_SERVICE_EMAIL_OVERRIDE_CC = "isis.service.email.override.cc";
-    private static final String ISIS_SERVICE_EMAIL_OVERRIDE_BCC = "isis.service.email.override.bcc";
-
     // -- INIT
 
     private boolean initialized;
@@ -109,51 +82,51 @@ public class EmailServiceDefault implements EmailService {
     }
 
     protected String getSenderEmailUsername() {
-        return configuration.getString(ISIS_SERVICE_EMAIL_SENDER_USERNAME);
+        return configuration.getService().getEmail().getSender().getUsername();
     }
 
     protected String getSenderEmailAddress() {
-        return configuration.getString(ISIS_SERVICE_EMAIL_SENDER_ADDRESS);
+        return configuration.getService().getEmail().getSender().getAddress();
     }
 
     protected String getSenderEmailPassword() {
-        return configuration.getString(ISIS_SERVICE_EMAIL_SENDER_PASSWORD);
+        return configuration.getService().getEmail().getSender().getPassword();
     }
 
     protected String getSenderEmailHostName() {
-        return configuration.getString(ISIS_SERVICE_EMAIL_SENDER_HOSTNAME, ISIS_SERVICE_EMAIL_SENDER_HOSTNAME_DEFAULT);
+        return configuration.getService().getEmail().getSender().getHostname();
     }
 
     protected Integer getSenderEmailPort() {
-        return configuration.getInteger(ISIS_SERVICE_EMAIL_PORT, ISIS_SERVICE_EMAIL_PORT_DEFAULT);
+        return configuration.getService().getEmail().getPort();
     }
 
     protected Boolean getSenderEmailTlsEnabled() {
-        return configuration.getBoolean(ISIS_SERVICE_EMAIL_TLS_ENABLED, ISIS_SERVICE_EMAIL_TLS_ENABLED_DEFAULT);
+        return configuration.getService().getEmail().getTls().isEnabled();
     }
 
     protected Boolean isThrowExceptionOnFail() {
-        return configuration.getBoolean(ISIS_SERVICE_EMAIL_THROW_EXCEPTION_ON_FAIL, ISIS_SERVICE_EMAIL_THROW_EXCEPTION_ON_FAIL_DEFAULT);
+        return configuration.getService().getEmail().isThrowExceptionOnFail();
     }
 
     protected int getSocketTimeout() {
-        return configuration.getInteger(ISIS_SERVICE_EMAIL_SOCKET_TIMEOUT, ISIS_SERVICE_EMAIL_SOCKET_TIMEOUT_DEFAULT);
+        return configuration.getService().getEmail().getSocketTimeout();
     }
 
     protected int getSocketConnectionTimeout() {
-        return configuration.getInteger(ISIS_SERVICE_EMAIL_SOCKET_CONNECTION_TIMEOUT, ISIS_SERVICE_EMAIL_SOCKET_CONNECTION_TIMEOUT_DEFAULT);
+        return configuration.getService().getEmail().getSocketConnectionTimeout();
     }
 
     protected String getEmailOverrideTo() {
-        return configuration.getString(ISIS_SERVICE_EMAIL_OVERRIDE_TO);
+        return configuration.getService().getEmail().getOverride().getTo();
     }
 
     protected String getEmailOverrideCc() {
-        return configuration.getString(ISIS_SERVICE_EMAIL_OVERRIDE_CC);
+        return configuration.getService().getEmail().getOverride().getCc();
     }
 
     protected String getEmailOverrideBcc() {
-        return configuration.getString(ISIS_SERVICE_EMAIL_OVERRIDE_BCC);
+        return configuration.getService().getEmail().getOverride().getBcc();
     }
 
     @Override
@@ -260,6 +233,7 @@ public class EmailServiceDefault implements EmailService {
         return addresses != null && addresses.length > 0;
     }
 
-    @Inject IsisConfigurationLegacy configuration;
+    @Inject IsisConfigurationLegacy configurationLegacy;
+    @Inject IsisConfiguration configuration;
 
 }
\ No newline at end of file