KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > alfresco > repo > template > ClassPathRepoTemplateLoader


1 /*
2  * Copyright (C) 2005 Alfresco, Inc.
3  *
4  * Licensed under the Mozilla Public License version 1.1
5  * with a permitted attribution clause. You may obtain a
6  * copy of the License at
7  *
8  * http://www.alfresco.org/legal/license.txt
9  *
10  * Unless required by applicable law or agreed to in writing,
11  * software distributed under the License is distributed on an
12  * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND,
13  * either express or implied. See the License for the specific
14  * language governing permissions and limitations under the
15  * License.
16  */

17 package org.alfresco.repo.template;
18
19 import java.io.IOException JavaDoc;
20 import java.io.InputStream JavaDoc;
21 import java.io.InputStreamReader JavaDoc;
22 import java.io.Reader JavaDoc;
23 import java.net.URL JavaDoc;
24 import java.net.URLConnection JavaDoc;
25
26 import org.alfresco.model.ContentModel;
27 import org.alfresco.service.ServiceRegistry;
28 import org.alfresco.service.cmr.repository.ContentReader;
29 import org.alfresco.service.cmr.repository.ContentService;
30 import org.alfresco.service.cmr.repository.NodeRef;
31 import org.alfresco.service.cmr.repository.NodeService;
32 import org.alfresco.service.cmr.repository.StoreRef;
33 import org.alfresco.util.ApplicationContextHelper;
34
35 import freemarker.cache.TemplateLoader;
36
37 /**
38  * Custom FreeMarker template loader to locate templates stored either from the ClassPath
39  * or in a Alfresco Repository.
40  * <p>
41  * The template name should be supplied either as a NodeRef String or a ClassPath path String.
42  *
43  * @author Kevin Roast
44  */

45 public class ClassPathRepoTemplateLoader implements TemplateLoader
46 {
47     private NodeService nodeService;
48     private ContentService contentService;
49     
50     public ClassPathRepoTemplateLoader(NodeService nodeService, ContentService contentService)
51     {
52         if (nodeService == null)
53         {
54             throw new IllegalArgumentException JavaDoc("NodeService is mandatory.");
55         }
56         if (contentService == null)
57         {
58             throw new IllegalArgumentException JavaDoc("ContentService is mandatory.");
59         }
60         this.nodeService = nodeService;
61         this.contentService = contentService;
62     }
63     
64     /**
65      * Return an object wrapping a source for a template
66      */

67     public Object JavaDoc findTemplateSource(String JavaDoc name)
68         throws IOException JavaDoc
69     {
70         if (name.indexOf(StoreRef.URI_FILLER) != -1)
71         {
72             NodeRef ref = new NodeRef(name);
73             if (this.nodeService.exists(ref) == true)
74             {
75                 return new RepoTemplateSource(ref);
76             }
77             else
78             {
79                 return null;
80             }
81         }
82         else
83         {
84             URL JavaDoc url = this.getClass().getClassLoader().getResource(name);
85             return url == null ? null : new ClassPathTemplateSource(url);
86         }
87     }
88     
89     public long getLastModified(Object JavaDoc templateSource)
90     {
91         return ((BaseTemplateSource)templateSource).lastModified();
92     }
93     
94     public Reader JavaDoc getReader(Object JavaDoc templateSource, String JavaDoc encoding) throws IOException JavaDoc
95     {
96         return ((BaseTemplateSource)templateSource).getReader();
97     }
98     
99     public void closeTemplateSource(Object JavaDoc templateSource) throws IOException JavaDoc
100     {
101         ((BaseTemplateSource)templateSource).close();
102     }
103     
104     
105     /**
106      * Class used as a base for custom Template Source objects
107      */

108     abstract class BaseTemplateSource
109     {
110         public abstract Reader JavaDoc getReader() throws IOException JavaDoc;
111         
112         public abstract void close() throws IOException JavaDoc;
113         
114         public abstract long lastModified();
115     }
116     
117     
118     /**
119      * Class providing a ClassPath based Template Source
120      */

121     class ClassPathTemplateSource extends BaseTemplateSource
122     {
123         private final URL JavaDoc url;
124         private URLConnection JavaDoc conn;
125         private InputStream JavaDoc inputStream;
126         
127         ClassPathTemplateSource(URL JavaDoc url) throws IOException JavaDoc
128         {
129             this.url = url;
130             this.conn = url.openConnection();
131         }
132         
133         public boolean equals(Object JavaDoc o)
134         {
135             if (o instanceof ClassPathTemplateSource)
136             {
137                 return url.equals(((ClassPathTemplateSource)o).url);
138             }
139             else
140             {
141                 return false;
142             }
143         }
144         
145         public int hashCode()
146         {
147             return url.hashCode();
148         }
149         
150         public String JavaDoc toString()
151         {
152             return url.toString();
153         }
154         
155         public long lastModified()
156         {
157             return conn.getLastModified();
158         }
159         
160         public Reader JavaDoc getReader() throws IOException JavaDoc
161         {
162             inputStream = conn.getInputStream();
163             return new InputStreamReader JavaDoc(inputStream);
164         }
165         
166         public void close() throws IOException JavaDoc
167         {
168             try
169             {
170                 if (inputStream != null)
171                 {
172                     inputStream.close();
173                 }
174             }
175             finally
176             {
177                 inputStream = null;
178                 conn = null;
179             }
180         }
181     }
182     
183     /**
184      * Class providing a Repository based Template Source
185      */

186     class RepoTemplateSource extends BaseTemplateSource
187     {
188         private final NodeRef nodeRef;
189         private InputStream JavaDoc inputStream;
190         private ContentReader conn;
191         
192         RepoTemplateSource(NodeRef ref) throws IOException JavaDoc
193         {
194             this.nodeRef = ref;
195             this.conn = contentService.getReader(nodeRef, ContentModel.PROP_CONTENT);
196         }
197         
198         public boolean equals(Object JavaDoc o)
199         {
200             if (o instanceof RepoTemplateSource)
201             {
202                 return nodeRef.equals(((RepoTemplateSource)o).nodeRef);
203             }
204             else
205             {
206                 return false;
207             }
208         }
209         
210         public int hashCode()
211         {
212             return nodeRef.hashCode();
213         }
214         
215         public String JavaDoc toString()
216         {
217             return nodeRef.toString();
218         }
219         
220         public long lastModified()
221         {
222             return conn.getLastModified();
223         }
224         
225         public Reader JavaDoc getReader() throws IOException JavaDoc
226         {
227             inputStream = conn.getContentInputStream();
228             return new InputStreamReader JavaDoc(inputStream);
229         }
230         
231         public void close() throws IOException JavaDoc
232         {
233             try
234             {
235                 if (inputStream != null)
236                 {
237                     inputStream.close();
238                 }
239             }
240             finally
241             {
242                 inputStream = null;
243                 conn = null;
244             }
245         }
246     }
247 }
248
Popular Tags