I think this would be a great "refactoring" project to review the service
manager capabilities with the developer group.  The first steps would be to
write the classes and a spring configuration that would include adding the
ConfigurationService into the EmailService.

Note, I'm sure its upto debate if the send method should be part of the
service or the Email object, in this example I started to put it in the
service....


public class EmailService {

    public ConfigurationService configurationService;

    public ConfigurationService getConfigurationService() {
        return configurationService;
    }

    public void setConfigurationService(ConfigurationService
configurationService) {
        this.configurationService = configurationService;
    }

    /**
     * Get the template for an email message. The message is suitable for
     * inserting values using <code>java.text.MessageFormat</code>.
     *
     * @param emailFile full name for the email template, for example
"/dspace/config/emails/register".
     * @return the email object, with the content and subject filled out
from
     *         the template
     * @throws IOException if the template couldn't be found, or there was
some other
     *                     error reading the template
     */
    public static Email getEmail(String emailFile) throws IOException {
        String charset = null;
        String subject = "";
        StringBuffer contentBuffer = new StringBuffer();

        // Read in template
        BufferedReader reader = null;
        try {
            reader = new BufferedReader(new FileReader(emailFile));

            boolean more = true;

            while (more) {
                String line = reader.readLine();

                if (line == null) {
                    more = false;
                } else if (line.toLowerCase().startsWith("subject:")) {
                    // Extract the first subject line - everything to the
right
                    // of the colon, trimmed of whitespace
                    subject = line.substring(8).trim();
                } else if (line.toLowerCase().startsWith("charset:")) {
                    // Extract the character set from the email
                    charset = line.substring(8).trim();
                } else if (!line.startsWith("#")) {
                    // Add non-comment lines to the content
                    contentBuffer.append(line);
                    contentBuffer.append("\n");
                }
            }
        } finally {
            if (reader != null) {
                reader.close();
            }
        }

        // Create an email
        Email email = new Email();

        email.setSubject(subject);
        email.setContent(contentBuffer.toString());

        if (charset != null)
            email.setCharset(charset);

        return email;
    }

    public void send() throws MessagingException
        {
            // Get the mail configuration properties
            String server = configurationService.getProperty("mail.server");
            String from =
configurationService.getProperty("mail.from.address");
            boolean disabled =
configurationService.getBooleanProperty("mail.server.disabled", false);

            if (disabled) {
                log.info("message not sent due to mail.server.disabled: " +
subject);
                return;
            }

            // Set up properties for mail session
            Properties props = System.getProperties();
            props.put("mail.smtp.host", server);

            // Set the port number for the mail server
            String portNo =
configurationService.getProperty("mail.server.port");
            if (portNo == null)
            {
                portNo = "25";
            }
            props.put("mail.smtp.port", portNo.trim());

            // If no character set specified, attempt to retrieve a default
            if (charset == null)
            {
                charset = configurationService.getProperty("mail.charset");

            }


            ....

}




test class for sending emails via the service to some configurable test
email address.

Mark

On Mon, Dec 12, 2011 at 8:15 AM, Mark Diggory <[email protected]> wrote:

> Mark,
>
> Good point, we need an "EmailService" based on the same code thats in
> ConfigurationManager,
>
>     /**
>      * Get the template for an email message. The message is suitable for
>      * inserting values using <code>java.text.MessageFormat</code>.
>      *
>      * @param emailFile
>      *            full name for the email template, for example 
> "/dspace/config/emails/register".
>      *
>      * @return the email object, with the content and subject filled out from
>      *         the template
>      *
>      * @throws IOException
>      *             if the template couldn't be found, or there was some other
>      *             error reading the template
>      */
>     public static Email getEmail(String emailFile) throws IOException
>     {
>         String charset = null;
>         String subject = "";
>         StringBuffer contentBuffer = new StringBuffer();
>
>         // Read in template
>         BufferedReader reader = null;
>         try
>         {
>             reader = new BufferedReader(new FileReader(emailFile));
>
>             boolean more = true;
>
>             while (more)
>             {
>                 String line = reader.readLine();
>
>                 if (line == null)
>                 {
>                     more = false;
>                 }
>                 else if (line.toLowerCase().startsWith("subject:"))
>                 {
>                     // Extract the first subject line - everything to the 
> right
>                     // of the colon, trimmed of whitespace
>                     subject = line.substring(8).trim();
>                 }
>                 else if (line.toLowerCase().startsWith("charset:"))
>                 {
>                     // Extract the character set from the email
>                     charset = line.substring(8).trim();
>                 }
>                 else if (!line.startsWith("#"))
>                 {
>                     // Add non-comment lines to the content
>                     contentBuffer.append(line);
>                     contentBuffer.append("\n");
>                 }
>             }
>         }
>         finally
>         {
>             if (reader != null)
>             {
>                 reader.close();
>             }
>         }
>         // Create an email
>         Email email = new Email();
>         email.setSubject(subject);
>         email.setContent(contentBuffer.toString());
>
>         if (charset != null)
>             email.setCharset(charset);
>
>         return email;
>     }
>
>
>
>
>  In general the "EmailService" should encapsulate any logic in looking up
> configuration and pre-configuring emails for the delivery. Not, if we use a
> DDD approach, our Email objects are actually pretty well behaved. However,
> the configuration would need to switch to lookups in ConfigurationService
> instead of ConfigurationManager.
>
> public void send() throws MessagingException
>     {
>         // Get the mail configuration properties
>         String server = ConfigurationManager.getProperty("mail.server");
>         String from = ConfigurationManager.getProperty("mail.from.address");
>         boolean disabled = 
> ConfigurationManager.getBooleanProperty("mail.server.disabled", false);
>
>         if (disabled) {
>             log.info("message not sent due to mail.server.disabled: " + 
> subject);
>             return;
>         }
>
>         // Set up properties for mail session
>         Properties props = System.getProperties();
>         props.put("mail.smtp.host", server);
>
>         // Set the port number for the mail server
>         String portNo = ConfigurationManager.getProperty("mail.server.port");
>         if (portNo == null)
>         {
>               portNo = "25";
>         }
>         props.put("mail.smtp.port", portNo.trim());
>
>         // If no character set specified, attempt to retrieve a default
>         if (charset == null)
>         {
>             charset = ConfigurationManager.getProperty("mail.charset");
>         }
>
>
> This will allow Email mechanics to be overridden without needing to
> override the ConfigurationManager class, for instance, the email templates
> could be stored in database fields and an Admin UI could be created to edit
> them more directly.
>
> Mark
>
> On Tue, Dec 6, 2011 at 8:15 AM, Mark H. Wood <[email protected]> wrote:
>
>> ?
>>
>> --
>> Mark H. Wood, Lead System Programmer   [email protected]
>> Asking whether markets are efficient is like asking whether people are
>> smart.
>>
>>
>> ------------------------------------------------------------------------------
>> Cloud Services Checklist: Pricing and Packaging Optimization
>> This white paper is intended to serve as a reference, checklist and point
>> of
>> discussion for anyone considering optimizing the pricing and packaging
>> model
>> of a cloud services business. Read Now!
>> http://www.accelacomm.com/jaw/sfnl/114/51491232/
>> _______________________________________________
>> Dspace-devel mailing list
>> [email protected]
>> https://lists.sourceforge.net/lists/listinfo/dspace-devel
>>
>>
>
>
> --
> [image: @mire Inc.]
> *Mark Diggory*
> *2888 Loker Avenue East, Suite 305, Carlsbad, CA. 92010*
> *Esperantolaan 4, Heverlee 3001, Belgium*
> http://www.atmire.com
>
>
>


-- 
[image: @mire Inc.]
*Mark Diggory*
*2888 Loker Avenue East, Suite 305, Carlsbad, CA. 92010*
*Esperantolaan 4, Heverlee 3001, Belgium*
http://www.atmire.com
------------------------------------------------------------------------------
Learn Windows Azure Live!  Tuesday, Dec 13, 2011
Microsoft is holding a special Learn Windows Azure training event for 
developers. It will provide a great way to learn Windows Azure and what it 
provides. You can attend the event by watching it streamed LIVE online.  
Learn more at http://p.sf.net/sfu/ms-windowsazure
_______________________________________________
Dspace-devel mailing list
[email protected]
https://lists.sourceforge.net/lists/listinfo/dspace-devel

Reply via email to