KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > info > magnolia > cms > mail > templates > impl > VelocityEmail


1 package info.magnolia.cms.mail.templates.impl;
2
3 import java.io.BufferedReader JavaDoc;
4 import java.io.FileReader JavaDoc;
5 import java.io.StringWriter JavaDoc;
6 import java.net.URL JavaDoc;
7 import java.util.Map JavaDoc;
8
9 import javax.mail.Session JavaDoc;
10
11 import org.apache.velocity.VelocityContext;
12 import org.apache.velocity.app.Velocity;
13
14
15 /**
16  * Date: Mar 30, 2006 Time: 1:13:33 PM
17  * @author <a HREF="mailto:niko@macnica.com">Nicolas Modrzyk</a>
18  */

19 public class VelocityEmail extends HtmlEmail {
20
21     static {
22         try {
23             Velocity.init();
24         }
25         catch (Exception JavaDoc e) {
26             log.error(e.getMessage(), e);
27         }
28     }
29
30     public VelocityEmail(Session JavaDoc _session) throws Exception JavaDoc {
31         super(_session);
32     }
33
34     public void setBodyFromResourceFile(String JavaDoc resourceFile, Map JavaDoc _map) throws Exception JavaDoc {
35         VelocityContext context = new VelocityContext(_map);
36         URL JavaDoc url = this.getClass().getResource("/" + resourceFile);
37         log.info("This is the url:" + url);
38         BufferedReader JavaDoc br = new BufferedReader JavaDoc(new FileReader JavaDoc(url.getFile()));
39         StringWriter JavaDoc w = new StringWriter JavaDoc();
40         Velocity.evaluate(context, w, "email", br);
41         super.setBody(w.toString(), _map);
42     }
43 }
44
Popular Tags