shuber      2005/07/25 14:08:40 CEST

  Modified files:
    core/src/java/org/jahia/registries ServicesRegistry.java 
    core/src/java/org/jahia/services/mail MailService.java 
    core/src/webapp/WEB-INF/etc/spring 
                                       applicationContext-services.xml 
  Added files:
    core/src/java/org/jahia/services/mail MailServiceImpl.java 
  Log:
  Introduce abstract class for mail service.
  
  Revision  Changes    Path
  1.18      +0 -4      
jahia/core/src/java/org/jahia/registries/ServicesRegistry.java
http://jahia.mine.nu:8080/cgi-bin/cvsweb.cgi/jahia/core/src/java/org/jahia/registries/ServicesRegistry.java.diff?r1=1.17&r2=1.18&f=h
  1.3       +16 -195   
jahia/core/src/java/org/jahia/services/mail/MailService.java
http://jahia.mine.nu:8080/cgi-bin/cvsweb.cgi/jahia/core/src/java/org/jahia/services/mail/MailService.java.diff?r1=1.2&r2=1.3&f=h
  1.1       +196 -0    
jahia/core/src/java/org/jahia/services/mail/MailServiceImpl.java (new)
http://jahia.mine.nu:8080/cgi-bin/cvsweb.cgi/jahia/core/src/java/org/jahia/services/mail/MailServiceImpl.java?rev=1.1&content-type=text/plain
  1.5       +1 -1      
jahia/core/src/webapp/WEB-INF/etc/spring/applicationContext-services.xml
http://jahia.mine.nu:8080/cgi-bin/cvsweb.cgi/jahia/core/src/webapp/WEB-INF/etc/spring/applicationContext-services.xml.diff?r1=1.4&r2=1.5&f=h
  
  
  
  Index: ServicesRegistry.java
  ===================================================================
  RCS file: 
/home/cvs/repository/jahia/core/src/java/org/jahia/registries/ServicesRegistry.java,v
  retrieving revision 1.17
  retrieving revision 1.18
  diff -u -r1.17 -r1.18
  --- ServicesRegistry.java     21 Jul 2005 15:07:27 -0000      1.17
  +++ ServicesRegistry.java     25 Jul 2005 12:08:39 -0000      1.18
  @@ -43,10 +43,6 @@
   
   package org.jahia.registries;
   
  -import java.util.HashMap;
  -import java.util.Properties;
  -import java.util.Vector;
  -
   import org.apache.jetspeed.Jetspeed;
   import org.apache.jetspeed.engine.Engine;
   import org.jahia.bin.Jahia;
  
  
  
  Index: MailServiceImpl.java
  ====================================================================
  //
  //                                   ____.
  //                       __/\ ______|    |__/\.     _______
  //            __   .____|    |       \   |    +----+       \
  //    _______|  /--|    |    |    -   \  _    |    :    -   \_________
  //   \\______: :---|    :    :           |    :    |         \________>
  //           |__\---\_____________:______:    :____|____:_____\
  //                                      /_____|
  //
  //                 . . . i n   j a h i a   w e   t r u s t . . .
  //
  
  package org.jahia.services.mail;
  
  import java.io.PrintWriter;
  import java.io.StringWriter;
  import java.util.Date;
  import java.util.Properties;
  
  import javax.mail.Message;
  import javax.mail.Session;
  import javax.mail.Transport;
  import javax.mail.internet.InternetAddress;
  import javax.mail.internet.MimeMessage;
  
  import org.jahia.bin.JahiaInterface;
  import org.jahia.exceptions.JahiaException;
  import org.jahia.services.JahiaInitializableService;
  import org.jahia.settings.SettingsBean;
  
  /**
   * <p>Title: </p>
   * <p>Description:
   * This service define method to send e-mails.
   * </p>
   *
   * <p>Copyright: MAP (Jahia Solutions Sàrl 2003)</p>
   * <p>Company: Jahia Solutions Sàrl</p>
   * @author MAP
   * @version 1.0
   */
  public class MailServiceImpl extends MailService {
  
      private static org.apache.log4j.Logger logger =
              org.apache.log4j.Logger.getLogger(MailServiceImpl.class);
  
      // Mail settings
      private String to;
      private String subject;
      private String from;
      private String mailhost;
      private String mailer = "Jahia Server v." + JahiaInterface.RELEASE_NUMBER 
+
                              "." + JahiaInterface.PATCH_NUMBER +
                              " build " + JahiaInterface.BUILD_NUMBER;
  
      public MailServiceImpl() {
      }
  
      /**
       * Implementation of the JahiaInitializableService. (For future purposes)
       *
       * @param jSettings Jahia Private Settings
       */
      public void init(SettingsBean jSettings) {
          logger.debug("Start Mail Service");
          this.to = jSettings.mail_administrator;
          if (this.to == null) {
              this.to = null;
          }
          this.from = jSettings.mail_from;
          if (this.from == null) {
              this.from = null;
          }
          this.mailhost = jSettings.mail_server;
          if (this.mailhost.equals("") || to.equals("") || from.equals("")) {
              logger.warn("Mail settings not valid, ignoring...");
          }
          if (this.mailhost == null || to == null || from == null) {
              logger.warn("Mail settings not valid, ignoring...");
          }
          logger.debug("Using default settings mailhost=[" + this.mailhost +
                       "] to=[" + to + "] from=[" + from + "]");
      }
  
      public boolean sendMessage(String message) {
          return sendMessage(this.from, this.to, null, null,
                             this.subject, this.mailhost, message);
      }
  
      public boolean sendMessage(String to, String message) {
          return sendMessage(this.from, to, null, null,
                             this.subject, this.mailhost, message);
      }
  
      public boolean sendMessage(String from, String to, String message) {
          return sendMessage(from, to, null, null,
                             this.subject, this.mailhost, message);
      }
  
      public boolean sendMessage(String from, String to, String cc, String bcc,
                                 String subject, String message) {
          return sendMessage(from, to, cc, bcc,
                             subject, this.mailhost, message);
      }
  
      public boolean sendMessage(String from, String to, String cc, String bcc,
                                 String subject, String mailhost, String 
message) {
          try {
              if (to == null) {
                  to = defaultRecipient();
              }
              if (from == null) {
                  from = defaultSender();
              }
  
              if (mailhost == null || to == null || from == null) {
                  logger.debug("Mail settings not valid, ignoring...");
                  return false;
              }
              if (mailhost.equals("") || to.equals("") || from.equals("")) {
                  logger.debug("Mail settings not valid, ignoring...");
                  return false;
              }
              logger.debug("Send mail using settings mailhost=[" + mailhost +
                           "] to=[" + to + "] from=[" + from + "]");
              Properties props = System.getProperties();
              props.put("mail.smtp.host", mailhost);
              // we are listing all properties here for reference for future
              // extension.
              //props.put("mail.store.protocol", mailhost);
              //props.put("mail.transport.protocol", mailhost);
              //props.put("mail.host", mailhost);
              //props.put("mail."+protocol+".host", mailhost);
              //props.put("mail."+protocol+".user", mailhost);
              //props.put("mail.from", mailhost);
              //props.put("mail.debug", mailhost);
              // Get a Session object
              Session session = Session.getDefaultInstance(props, null);
              // construct the message
              Message msg = new MimeMessage(session);
              if (from != null) {
                  msg.setFrom(new InternetAddress(from));
              } else {
                  msg.setFrom();
              }
              msg.setRecipients(Message.RecipientType.TO, 
InternetAddress.parse(to, false));
              if (cc != null) {
                  msg.setRecipients(Message.RecipientType.CC, 
InternetAddress.parse(cc, false));
              }
              if (bcc != null) {
                  msg.setRecipients(Message.RecipientType.BCC, 
InternetAddress.parse(bcc, false));
              }
              if (subject == null || "".equals(subject)) {
                  subject = "[JAHIA] Jahia Message";
              }
              msg.setSubject(subject);
  
              StringWriter msgBodyWriter = new StringWriter();
              PrintWriter strOut = new PrintWriter(msgBodyWriter);
              strOut.println(message);
  
              msg.setText(msgBodyWriter.toString());
              msg.setHeader("X-Mailer", mailer);
              msg.setSentDate(new Date());
              logger.debug("Mailing to " + to + " via " + mailhost + "...");
  
              // send the thing off
              sendMessage(msg);
              logger.debug("Mail was sent successfully.");
          } catch (Throwable th) {
              logger.debug("Error while sending mail : " + th.getMessage(), th);
              return false;
          }
          return true;
      }
  
      public boolean sendMessage(Message message) {
          try {
              Transport.send(message);
          } catch (Throwable th) {
              logger.debug("Error while sending mail : " + th.getMessage(), th);
              return false;
          }
          return true;
      }
  
      public String defaultRecipient() {
          return this.to;
      }
  
      public String defaultSender() {
          return this.from;
      }
  
  
  }
  
  
  
  Index: MailService.java
  ===================================================================
  RCS file: 
/home/cvs/repository/jahia/core/src/java/org/jahia/services/mail/MailService.java,v
  retrieving revision 1.2
  retrieving revision 1.3
  diff -u -r1.2 -r1.3
  --- MailService.java  9 Jun 2004 11:53:27 -0000       1.2
  +++ MailService.java  25 Jul 2005 12:08:40 -0000      1.3
  @@ -1,88 +1,16 @@
  -//
  -//                                   ____.
  -//                       __/\ ______|    |__/\.     _______
  -//            __   .____|    |       \   |    +----+       \
  -//    _______|  /--|    |    |    -   \  _    |    :    -   \_________
  -//   \\______: :---|    :    :           |    :    |         \________>
  -//           |__\---\_____________:______:    :____|____:_____\
  -//                                      /_____|
  -//
  -//                 . . . i n   j a h i a   w e   t r u s t . . .
  -//
  -
   package org.jahia.services.mail;
   
  -import java.io.PrintWriter;
  -import java.io.StringWriter;
  -import java.util.Date;
  -import java.util.Properties;
  +import org.jahia.services.JahiaInitializableService;
   
   import javax.mail.Message;
  -import javax.mail.Session;
  -import javax.mail.Transport;
  -import javax.mail.internet.InternetAddress;
  -import javax.mail.internet.MimeMessage;
  -
  -import org.jahia.bin.JahiaInterface;
  -import org.jahia.exceptions.JahiaException;
  -import org.jahia.services.JahiaInitializableService;
  -import org.jahia.settings.SettingsBean;
   
   /**
  - * <p>Title: </p>
  - * <p>Description:
  - * This service define method to send e-mails.
  - * </p>
  - *
  - * <p>Copyright: MAP (Jahia Solutions Sàrl 2003)</p>
  - * <p>Company: Jahia Solutions Sàrl</p>
  - * @author MAP
  - * @version 1.0
  + * User: Serge Huber
  + * Date: Jul 25, 2005
  + * Time: 12:22:20 PM
  + * Copyright (C) Jahia Inc.
    */
  -public class MailService extends JahiaInitializableService {
  -
  -    /**
  -     * Returns the singleton instance of the service, and creates it if there
  -     * wasn't one.
  -     * @return a MailService object that is the singleton instance of
  -     * this service.
  -     * @throws JahiaException not used for the moment but reserved for later
  -     * use.
  -     */
  -    public static synchronized MailService getInstance()
  -            throws JahiaException {
  -        if (singletonInstance == null) {
  -            singletonInstance = new MailService();
  -        }
  -        return singletonInstance;
  -    }
  -
  -    /**
  -     * Implementation of the JahiaInitializableService. (For future purposes)
  -     *
  -     * @param jSettings Jahia Private Settings
  -     */
  -    public void init(SettingsBean jSettings) {
  -        logger.debug("Start Mail Service");
  -        this.to = jSettings.mail_administrator;
  -        if (this.to == null) {
  -            this.to = null;
  -        }
  -        this.from = jSettings.mail_from;
  -        if (this.from == null) {
  -            this.from = null;
  -        }
  -        this.mailhost = jSettings.mail_server;
  -        if (this.mailhost.equals("") || to.equals("") || from.equals("")) {
  -            logger.warn("Mail settings not valid, ignoring...");
  -        }
  -        if (this.mailhost == null || to == null || from == null) {
  -            logger.warn("Mail settings not valid, ignoring...");
  -        }
  -        logger.debug("Using default settings mailhost=[" + this.mailhost +
  -                     "] to=[" + to + "] from=[" + from + "]");
  -    }
  -
  +public abstract class MailService extends JahiaInitializableService {
       /**
        * Send message to the default Jahia settings defined in the 
jahia.properties
        * file.
  @@ -90,10 +18,7 @@
        * @param message The message to send
        * @return True if message is sent successfully, false otherwise
        */
  -    public boolean sendMessage(String message) {
  -        return sendMessage(this.from, this.to, null, null,
  -                           this.subject, this.mailhost, message);
  -    }
  +    public abstract boolean sendMessage(String message);
   
       /**
        * Send message to the desired destination.
  @@ -102,10 +27,7 @@
        * @param message The message to send.
        * @return True if message is sent successfully, false otherwise
        */
  -    public boolean sendMessage(String to, String message) {
  -        return sendMessage(this.from, to, null, null,
  -                           this.subject, this.mailhost, message);
  -    }
  +    public abstract boolean sendMessage(String to, String message);
   
       /**
        * Send message to the desired destination.
  @@ -115,10 +37,7 @@
        * @param message The message to send.
        * @return True if message is sent successfully, false otherwise
        */
  -    public boolean sendMessage(String from, String to, String message) {
  -        return sendMessage(from, to, null, null,
  -                           this.subject, this.mailhost, message);
  -    }
  +    public abstract boolean sendMessage(String from, String to, String 
message);
   
       /**
        * Send message to the desired destination with cc and bcc option. The
  @@ -132,11 +51,8 @@
        * @param message The message to send.
        * @return True if message is sent successfully, false otherwise
        */
  -    public boolean sendMessage(String from, String to, String cc, String bcc,
  -                               String subject, String message) {
  -        return sendMessage(from, to, cc, bcc,
  -                           subject, this.mailhost, message);
  -    }
  +    public abstract boolean sendMessage(String from, String to, String cc, 
String bcc,
  +                               String subject, String message);
   
       /**
        * Send message to the desired destination with cc and bcc option. The
  @@ -151,76 +67,8 @@
        * @param message The message to send.
        * @return True if message is sent successfully, false otherwise
        */
  -    public boolean sendMessage(String from, String to, String cc, String bcc,
  -                               String subject, String mailhost, String 
message) {
  -        try {
  -            if (to == null) {
  -                to = defaultRecipient();
  -            }
  -            if (from == null) {
  -                from = defaultSender();
  -            }
  -
  -            if (mailhost == null || to == null || from == null) {
  -                logger.debug("Mail settings not valid, ignoring...");
  -                return false;
  -            }
  -            if (mailhost.equals("") || to.equals("") || from.equals("")) {
  -                logger.debug("Mail settings not valid, ignoring...");
  -                return false;
  -            }
  -            logger.debug("Send mail using settings mailhost=[" + mailhost +
  -                         "] to=[" + to + "] from=[" + from + "]");
  -            Properties props = System.getProperties();
  -            props.put("mail.smtp.host", mailhost);
  -            // we are listing all properties here for reference for future
  -            // extension.
  -            //props.put("mail.store.protocol", mailhost);
  -            //props.put("mail.transport.protocol", mailhost);
  -            //props.put("mail.host", mailhost);
  -            //props.put("mail."+protocol+".host", mailhost);
  -            //props.put("mail."+protocol+".user", mailhost);
  -            //props.put("mail.from", mailhost);
  -            //props.put("mail.debug", mailhost);
  -            // Get a Session object
  -            Session session = Session.getDefaultInstance(props, null);
  -            // construct the message
  -            Message msg = new MimeMessage(session);
  -            if (from != null) {
  -                msg.setFrom(new InternetAddress(from));
  -            } else {
  -                msg.setFrom();
  -            }
  -            msg.setRecipients(Message.RecipientType.TO, 
InternetAddress.parse(to, false));
  -            if (cc != null) {
  -                msg.setRecipients(Message.RecipientType.CC, 
InternetAddress.parse(cc, false));
  -            }
  -            if (bcc != null) {
  -                msg.setRecipients(Message.RecipientType.BCC, 
InternetAddress.parse(bcc, false));
  -            }
  -            if (subject == null || "".equals(subject)) {
  -                subject = "[JAHIA] Jahia Message";
  -            }
  -            msg.setSubject(subject);
  -
  -            StringWriter msgBodyWriter = new StringWriter();
  -            PrintWriter strOut = new PrintWriter(msgBodyWriter);
  -            strOut.println(message);
  -
  -            msg.setText(msgBodyWriter.toString());
  -            msg.setHeader("X-Mailer", mailer);
  -            msg.setSentDate(new Date());
  -            logger.debug("Mailing to " + to + " via " + mailhost + "...");
  -
  -            // send the thing off
  -            sendMessage(msg);
  -            logger.debug("Mail was sent successfully.");
  -        } catch (Throwable th) {
  -            logger.debug("Error while sending mail : " + th.getMessage(), 
th);
  -            return false;
  -        }
  -        return true;
  -    }
  +    public abstract boolean sendMessage(String from, String to, String cc, 
String bcc,
  +                               String subject, String mailhost, String 
message);
   
       /**
        * Send a Message type previously initialized and formated.
  @@ -228,36 +76,9 @@
        * @param msg The Message to send.
        * @return True if message is sent successfully, false otherwise
        */
  -    public boolean sendMessage(Message message) {
  -        try {
  -            Transport.send(message);
  -        } catch (Throwable th) {
  -            logger.debug("Error while sending mail : " + th.getMessage(), 
th);
  -            return false;
  -        }
  -        return true;
  -    }
  -
  -    public String defaultRecipient() {
  -        return this.to;
  -    }
  -
  -    public String defaultSender() {
  -        return this.from;
  -    }
  -
  -    // Mail settings
  -    private String to;
  -    private String subject;
  -    private String from;
  -    private String mailhost;
  -    private String mailer = "Jahia Server v." + 
JahiaInterface.RELEASE_NUMBER +
  -                            "." + JahiaInterface.PATCH_NUMBER +
  -                            " build " + JahiaInterface.BUILD_NUMBER;
  -
  -    static private MailService singletonInstance = null;
  +    public abstract boolean sendMessage(Message message);
   
  -    private static org.apache.log4j.Logger logger =
  -            org.apache.log4j.Logger.getLogger(MailService.class);
  +    public abstract String defaultRecipient();
   
  +    public abstract String defaultSender();
   }
  
  
  
  Index: applicationContext-services.xml
  ===================================================================
  RCS file: 
/home/cvs/repository/jahia/core/src/webapp/WEB-INF/etc/spring/applicationContext-services.xml,v
  retrieving revision 1.4
  retrieving revision 1.5
  diff -u -r1.4 -r1.5
  --- applicationContext-services.xml   25 Jul 2005 09:13:36 -0000      1.4
  +++ applicationContext-services.xml   25 Jul 2005 12:08:40 -0000      1.5
  @@ -313,7 +313,7 @@
       </bean>
       <bean id="MailService" parent="txProxyTemplate">
           <property name="target">
  -            <bean class="org.jahia.services.mail.MailService" 
factory-method="getInstance">
  +            <bean class="org.jahia.services.mail.MailServiceImpl">
               </bean>
           </property>
       </bean>
  

Reply via email to