KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > blandware > atleap > service > util > MailTemplateLoader


1 /*
2  * Copyright 2004 Blandware (http://www.blandware.com)
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 com.blandware.atleap.service.util;
17
18 import org.apache.commons.collections.ExtendedProperties;
19 import org.apache.commons.logging.Log;
20 import org.apache.commons.logging.LogFactory;
21 import org.apache.velocity.exception.ResourceNotFoundException;
22 import org.apache.velocity.runtime.resource.Resource;
23 import org.apache.velocity.runtime.resource.loader.ClasspathResourceLoader;
24
25 import java.io.ByteArrayInputStream JavaDoc;
26 import java.io.InputStream JavaDoc;
27 import java.io.UnsupportedEncodingException JavaDoc;
28
29 /**
30  * <p>Loads mail templates by specified identintifier from persistence storage</p>
31  * <p><a HREF="MailTemplateLoader.java.htm"><i>View Source</i></a></p>
32  *
33  * @author Sergey Zubtcovskii <a HREF="mailto:sergey.zubtcovskii@blandware.com">&lt;sergey.zubtcovskii@blandware.com&gt;</a>
34  * @version $Revision: 1.5 $ $Date: 2005/11/26 14:51:36 $
35  */

36 public class MailTemplateLoader extends ClasspathResourceLoader {
37
38     protected transient final Log log = LogFactory.getLog(MailTemplateLoader.class);
39     protected String JavaDoc encoding;
40
41     /**
42      * Creates new instance of MailTemplateLoader
43      */

44     public MailTemplateLoader() {
45     }
46
47     /**
48      * @see org.apache.velocity.runtime.resource.loader.ResourceLoader#init(org.apache.commons.collections.ExtendedProperties)
49      */

50     public void init(ExtendedProperties extendedProperties) {
51         super.init(extendedProperties);
52         encoding = rsvc.getString("content.encoding", "utf-8");
53
54     }
55
56     /**
57      * Returns template as a stream.<br />
58      * If input string starts with &quot;mailTemplate:&nbsp;&quot;, it will be returned as is, but prefix will be truncated.<br />
59      * Otherwise resource will be loaded from classpath
60      *
61      * @param templateName Name of resource to load
62      * @return Template as a stream
63      * @throws ResourceNotFoundException if specified resource could not be found
64      */

65     public InputStream JavaDoc getResourceStream(String JavaDoc templateName) throws ResourceNotFoundException {
66
67         InputStream JavaDoc result = null;
68
69         if ( templateName.startsWith("mailTemplate: ") ) {
70             templateName = templateName.substring("mailTemplate: ".length());
71             byte[] template = null;
72             try {
73                 template = templateName.getBytes(encoding);
74             } catch ( UnsupportedEncodingException JavaDoc e ) {
75                 // do nothing
76
}
77             if ( template != null ) {
78                 result = new ByteArrayInputStream JavaDoc(template);
79             }
80         } else {
81             result = super.getResourceStream(templateName);
82         }
83
84         return result;
85     }
86
87     /**
88      * Defaults to return false.
89      *
90      * @see org.apache.velocity.runtime.resource.loader.ResourceLoader#isSourceModified(org.apache.velocity.runtime.resource.Resource)
91      */

92     public boolean isSourceModified(Resource resource) {
93         return super.isSourceModified(resource);
94     }
95
96     /**
97      * Defaults to return 0
98      *
99      * @see org.apache.velocity.runtime.resource.loader.ResourceLoader#getLastModified(org.apache.velocity.runtime.resource.Resource)
100      */

101     public long getLastModified(Resource resource) {
102         return super.getLastModified(resource);
103     }
104 }
105
Popular Tags