View Javadoc

1   /*
2    * Copyright 2001-2004 The Apache Software Foundation.
3    * 
4    * Licensed under the Apache License, Version 2.0 (the "License");
5    * you may not use this file except in compliance with the License.
6    * You may obtain a copy of the License at
7    * 
8    *      http://www.apache.org/licenses/LICENSE-2.0
9    * 
10   * Unless required by applicable law or agreed to in writing, software
11   * distributed under the License is distributed on an "AS IS" BASIS,
12   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13   * See the License for the specific language governing permissions and
14   * limitations under the License.
15   */
16  package org.jdiagnose.library.smtp;
17  
18  import java.io.StringWriter;
19  import java.util.HashMap;
20  import java.util.Map;
21  
22  import org.apache.velocity.VelocityContext;
23  import org.apache.velocity.app.VelocityEngine;
24  import org.apache.velocity.exception.ParseErrorException;
25  import org.apache.velocity.exception.ResourceNotFoundException;
26  import org.springframework.beans.MethodInvocationException;
27  
28  /***
29   * @author jmccrindle
30   */
31  public class VelocityMessageProvider implements MessageProvider {
32  
33      private static final org.apache.commons.logging.Log log =
34          org.apache.commons.logging.LogFactory.getLog(VelocityMessageProvider.class);
35  
36      private VelocityEngine velocityEngine = null;
37      private String template = null;
38      private Map context = new HashMap();
39  
40      public VelocityMessageProvider() {
41          super();
42      }
43  
44      /***
45       * @see com.three.diagnostics.smtp.MessageProvider#getMessage(com.three.diagnostics.runner.IDiagnosticResults)
46       */
47      public String getMessage(Object results) {
48          VelocityContext velocityContext = new VelocityContext(context);
49          velocityContext.put("content", results);
50          StringWriter writer = new StringWriter();
51          try {
52              velocityEngine.mergeTemplate(template, velocityContext, writer);
53          } catch (ResourceNotFoundException e) {
54              log.warn(e, e);
55          } catch (ParseErrorException e) {
56              log.warn(e, e);
57          } catch (MethodInvocationException e) {
58              log.warn(e, e);
59          } catch (Exception e) {
60              log.warn(e, e);
61          }
62          return writer.getBuffer().toString();
63      }
64  
65      /***
66       * @return
67       */
68      public Map getContext() {
69          return context;
70      }
71  
72      /***
73       * @return
74       */
75      public String getTemplate() {
76          return template;
77      }
78  
79      /***
80       * @return
81       */
82      public VelocityEngine getVelocityEngine() {
83          return velocityEngine;
84      }
85  
86      /***
87       * @param map
88       */
89      public void setContext(Map map) {
90          context = map;
91      }
92  
93      /***
94       * @param string
95       */
96      public void setTemplate(String string) {
97          template = string;
98      }
99  
100     /***
101      * @param engine
102      */
103     public void setVelocityEngine(VelocityEngine engine) {
104         velocityEngine = engine;
105     }
106 
107 }