You are viewing a plain text version of this content. The canonical link for it is here.
Posted to dev@turbine.apache.org by jo...@apache.org on 2001/08/10 21:57:28 UTC

cvs commit: jakarta-turbine-fulcrum/src/util/java/org/apache/fulcrum/template TemplateEmail.java

jon         01/08/10 12:57:28

  Modified:    .        build.properties build.xml
  Added:       src/util/java/org/apache/fulcrum/template TemplateEmail.java
  Log:
  added in the TemplateEmail class...
  
  the build system has been modified to conditionally compile this class
  only if the commons-email.jar file is in the lib.repo
  
  Revision  Changes    Path
  1.6       +3 -0      jakarta-turbine-fulcrum/build.properties
  
  Index: build.properties
  ===================================================================
  RCS file: /home/cvs/jakarta-turbine-fulcrum/build.properties,v
  retrieving revision 1.5
  retrieving revision 1.6
  diff -u -r1.5 -r1.6
  --- build.properties	2001/08/09 21:01:42	1.5
  +++ build.properties	2001/08/10 19:57:28	1.6
  @@ -70,6 +70,9 @@
   # Xslt
   xalan.jar = ${lib.repo}/xalan-2.1.0.jar
   
  +# Commons-Email for the src/util directory
  +commons-email.jar = ${lib.repo}/commons-email-0.1-dev.jar
  +
   # -------------------------------------------------------------------
   # 
   # T O R Q U E  C O N F I G U R A T I O N
  
  
  
  1.14      +30 -9     jakarta-turbine-fulcrum/build.xml
  
  Index: build.xml
  ===================================================================
  RCS file: /home/cvs/jakarta-turbine-fulcrum/build.xml,v
  retrieving revision 1.13
  retrieving revision 1.14
  diff -u -r1.13 -r1.14
  --- build.xml	2001/08/09 21:04:20	1.13
  +++ build.xml	2001/08/10 19:57:28	1.14
  @@ -26,6 +26,7 @@
       <pathelement location="${javamail.jar}"/>
       <pathelement location="${jaf.jar}"/>
       <pathelement location="${commons-util.jar}"/>
  +    <pathelement location="${commons-email.jar}"/>
     </path>
   
     <!-- ================================================================== -->
  @@ -46,6 +47,7 @@
       <echo message="xalan.jar = ${xalan.jar}"/>
       <echo message="xmlrpc.jar = ${xmlrpc.jar}"/>
       <echo message="commons-util.jar = ${commons-util.jar}"/>
  +    <echo message="[optional] commons-email.jar = ${commons-email.jar}"/>
     </target>
   
     <!-- ================================================================== -->
  @@ -144,6 +146,12 @@
         property="commons-util.present"
         classpathref="classpath"
       />
  +
  +    <available
  +      classname="org.apache.commons.mail.SimpleEmail" 
  +      property="commons-email.present"
  +      classpathref="classpath"
  +    />
     </target>
   
     <!-- ================================================================== -->
  @@ -227,6 +235,20 @@
       </antcall>
     </target>
   
  +<!--
  +  This is commented out because the inclusion of the .jar file is 
  +  conditional depending on whether or not it is in your path. This
  +  is kept here in the event that this conditional requirement is
  +  removed in the future.
  +  
  +  <target name="check.commons-email" unless="commons-email.present">
  +    <antcall target="property-warning">
  +      <param name="name" value="commons-email.jar"/>
  +      <param name="value" value="${commons-email.jar}"/>
  +    </antcall>
  +  </target>
  +-->
  +
     <target name="property-warning">
       <echo>
         +----------------------------------------------------------------+
  @@ -249,12 +271,17 @@
     <!-- P R E P A R E                                                      -->
     <!-- ================================================================== -->
     
  +  <target name="include-email" if="commons-email.present">
  +    <copy todir="${build.dir}/src/org" overwrite="no">
  +      <fileset dir="${src.dir}/util/java/org"/>
  +    </copy>  
  +  </target>
  +  
     <target name="prepare" 
  -    depends="init,
  +    depends="init,env,
           check.velocity,check.log4j,check.torque,check.bsf,check.servlet,
           check.javamail,check.jaf,check.xerces,check.xalan,check.xmlrpc,
  -        check.commons-util,
  -        om,env">
  +        check.commons-util,include-email,om">
       
       <mkdir dir="${build.dir}"/>
       <mkdir dir="${build.dir}/classes"/>
  @@ -263,7 +290,6 @@
       <!-- We have some static files that should overwrite
            The torque generated files so overwrite must be
            set to 'yes'. -->
  -    
       <copy todir="${build.dir}/src/org" overwrite="yes">
         <fileset dir="${src.dir}/core/java/org"/>
         <fileset dir="${src.dir}/services/java/org"/>
  @@ -283,11 +309,6 @@
         optimize="${optimize}">
         <classpath refid="classpath"/>
       </javac>
  -
  -    <!-- copy database.dtd to the right place
  -    <copy file="${src.dir}/dtd/database.dtd"
  -      todir="${build.dir}/classes/org/apache/turbine/torque/engine/database/transform"/>
  -     -->
     </target>
   
     <!-- ================================================================== -->
  
  
  
  1.1                  jakarta-turbine-fulcrum/src/util/java/org/apache/fulcrum/template/TemplateEmail.java
  
  Index: TemplateEmail.java
  ===================================================================
  package org.apache.fulcrum.template;
  
  /* ====================================================================
   * The Apache Software License, Version 1.1
   *
   * Copyright (c) 2001 The Apache Software Foundation.  All rights
   * reserved.
   *
   * Redistribution and use in source and binary forms, with or without
   * modification, are permitted provided that the following conditions
   * are met:
   *
   * 1. Redistributions of source code must retain the above copyright
   *    notice, this list of conditions and the following disclaimer.
   *
   * 2. Redistributions in binary form must reproduce the above copyright
   *    notice, this list of conditions and the following disclaimer in
   *    the documentation and/or other materials provided with the
   *    distribution.
   *
   * 3. The end-user documentation included with the redistribution,
   *    if any, must include the following acknowledgment:
   *       "This product includes software developed by the
   *        Apache Software Foundation (http://www.apache.org/)."
   *    Alternately, this acknowledgment may appear in the software itself,
   *    if and wherever such third-party acknowledgments normally appear.
   *
   * 4. The names "Apache" and "Apache Software Foundation" and 
   *    "Apache Turbine" must not be used to endorse or promote products 
   *    derived from this software without prior written permission. For 
   *    written permission, please contact apache@apache.org.
   *
   * 5. Products derived from this software may not be called "Apache",
   *    "Apache Turbine", nor may "Apache" appear in their name, without 
   *    prior written permission of the Apache Software Foundation.
   *
   * THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESSED OR IMPLIED
   * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
   * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
   * DISCLAIMED.  IN NO EVENT SHALL THE APACHE SOFTWARE FOUNDATION OR
   * ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
   * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
   * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF
   * USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
   * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
   * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT
   * OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
   * SUCH DAMAGE.
   * ====================================================================
   *
   * This software consists of voluntary contributions made by many
   * individuals on behalf of the Apache Software Foundation.  For more
   * information on the Apache Software Foundation, please see
   * <http://www.apache.org/>.
   */
  
  import org.apache.commons.mail.SimpleEmail;
  import org.apache.commons.util.StringUtils;
  
  /**
   * This is a simple class for sending email from within the TemplateService.
   * Essentially, the body of the email is processed with a 
   * TemplateContext object.
   * The beauty of this is that you can send email from within your
   * template layer or from your business logic in your Java code.
   * The body of the email is just a TemplateService template so you can use
   * all the template functionality of your TemplateService within your emails!
   *
   * <p>Example Usage (This all needs to be on one line in your
   * template):
   *
   * <p>Setup your context:
   *
   * <p>context.put ("TemplateEmail", new TemplateEmail() );
   *
   * <p>Then, in your template (Velocity Example):
   *
   * <pre>
   * $TemplateEmail.setTo("Jon Stevens", "jon@latchkey.com")
   *     .setFrom("Mom", "mom@mom.com").setSubject("Eat dinner")
   *     .setTemplate("email/momEmail.vm")
   *     .setContext($context)
   * </pre>
   *
   * The email/momEmail.vm template will then be parsed with the
   * Context that was defined with setContext().
   *
   * <p>If you want to use this class from within your Java code all you
   * have to do is something like this:
   *
   * <pre>
   * TemplateEmail ve = new TemplateEmail();
   * ve.setTo("Jon Stevens", "jon@latchkey.com");
   * ve.setFrom("Mom", "mom@mom.com").setSubject("Eat dinner");
   * ve.setContext(context);
   * ve.setTemplate("email/momEmail.vm")
   * ve.send();
   * </pre>
   *
   * <p>(Note that when used within a Velocity template, the send method
   * will be called for you when Velocity tries to convert the
   * TemplateEmail to a string by calling toString()).</p>
   *
   * <p>If you need your email to be word-wrapped, you can add the 
   * following call to those above:
   *
   * <pre>
   * ve.setWordWrap (60);
   * </pre>
   *
   * <p>This class is just a wrapper around the SimpleEmail class.
   * Thus, it uses the JavaMail API and also depends on having the
   * mail.host property set in the System.properties().
   *
   * @author <a href="mailto:jon@latchkey.com">Jon S. Stevens</a>
   * @author <a href="mailto:gcoladonato@yahoo.com">Greg Coladonato</a>
   * @version $Id: TemplateEmail.java,v 1.1 2001/08/10 19:57:28 jon Exp $
   */
  public class TemplateEmail
  {
      /** The to name field. */
      private String toName = null;
  
      /** The to email field. */
      private String toEmail = null;
  
      /** The from name field. */
      private String fromName = null;
  
      /** The from email field. */
      private String fromEmail = null;
  
      /** The subject of the message. */
      private String subject = null;
  
      /** The column to word-wrap at.  <code>0</code> indicates no wrap. */
      private int wordWrap = 0;
  
      /**
       * The template to process, relative to the TemplateService template
       * directory.
       */
      private String template = null;
  
      /**
       * A Context
       */
      private TemplateContext context = null;
  
      /**
       * Constructor
       */
      public TemplateEmail ()
      {
      }
  
      /**
       * Constructor
       */
      public TemplateEmail (TemplateContext context)
      {
          this.context = context;
      }
  
      /**
       * To: name, email
       *
       * @param to A String with the TO name.
       * @param email A String with the TO email.
       * @return A TemplateEmail (self).
       */
      public TemplateEmail setTo(String to,
                                 String email)
      {
          this.toName = to;
          this.toEmail = email;
          return (this);
      }
  
      /**
       * From: name, email.
       *
       * @param from A String with the FROM name.
       * @param email A String with the FROM email.
       * @return A TemplateEmail (self).
       */
      public TemplateEmail setFrom(String from,
                                   String email)
      {
          this.fromName = from;
          this.fromEmail = email;
          return (this);
      }
  
      /**
       * Subject.
       *
       * @param subject A String with the subject.
       * @return A TemplateEmail (self).
       */
      public TemplateEmail setSubject(String subject)
      {
          this.subject = subject;
          return (this);
      }
  
      /**
       * TemplateService template to execute. Path is relative to the TemplateService
       * templates directory.
       *
       * @param template A String with the template.
       * @return A TemplateEmail (self).
       */
      public TemplateEmail setTemplate(String template)
      {
          this.template = template;
          return (this);
      }
  
      /**
       * Set the column at which long lines of text should be word-
       * wrapped. Setting to zero turns off word-wrap (default).
       *
       * NOTE: don't use tabs in your email template document,
       * or your word-wrapping will be off for the lines with tabs
       * in them.
       *
       * @param wordWrap The column at which to wrap long lines.
       * @return A TemplateEmail (self).
       */
      public TemplateEmail setWordWrap(int wordWrap)
      {
          this.wordWrap = wordWrap;
          return (this);
      }
  
      /**
       * Set the context object that will be merged with the 
       * template.
       *
       * @param context A TemplateContext context object.
       * @return A TemplateEmail (self).
       */
      public TemplateEmail setContext(TemplateContext context)
      {
          this.context = context;
          return (this);
      }
  
      /**
       * Get the context object that will be merged with the 
       * template.
       *
       * @return A TemplateContext.
       */
      public TemplateContext getContext()
      {
          return this.context;
      }
  
      /**
       * This method sends the email.
       */
      public void send()
          throws Exception
      {
          // Process the template.
          String body = TurbineTemplate.handleRequest(context,template);
  
          // If the caller desires word-wrapping, do it here
          if (wordWrap > 0)
          {
              body = StringUtils.wrapText (body,
                                           System.getProperty("line.separator"),
                                           wordWrap);
          }
  
          SimpleEmail se = new SimpleEmail();
          se.setFrom(fromEmail, fromName);
          se.addTo(toEmail, toName);
          se.setSubject(subject);
          se.setMsg(body);
          se.send();
      }
  
      /**
       * The method toString() calls send() for ease of use within a
       * TemplateService template (see example usage above).
       *
       * @return An empty string.
       */
      public String toString()
      {
          try
          {
              send();
          }
          catch (Exception e)
          {
  //            Log.error ("TemplateEmail error", e);
          }
          return "";
      }
  }
  
  
  

---------------------------------------------------------------------
To unsubscribe, e-mail: turbine-dev-unsubscribe@jakarta.apache.org
For additional commands, e-mail: turbine-dev-help@jakarta.apache.org