View Javadoc

1   package net.sourceforge.blogentis.storage;
2   
3   //-----------------------------------------------------------------------
4   //Blogentis - a blog publishing platform.
5   //Copyright (C) 2004 Tassos Bassoukos <abassouk@gmail.com>
6   //
7   //This library is free software; you can redistribute it and/or
8   //modify it under the terms of the GNU Lesser General Public
9   //License as published by the Free Software Foundation; either
10  //version 2.1 of the License, or (at your option) any later version.
11  //
12  //This library is distributed in the hope that it will be useful,
13  //but WITHOUT ANY WARRANTY; without even the implied warranty of
14  //MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
15  //Lesser General Public License for more details.
16  //
17  //You should have received a copy of the GNU Lesser General Public
18  //License along with this library; if not, write to the Free Software
19  //Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
20  //-----------------------------------------------------------------------
21  //
22  //$Id: TemplateResourceLoader.java,v 1.2 2004/10/28 10:45:51 tassos Exp $
23  //
24  
25  import java.io.InputStream;
26  import java.util.Date;
27  import java.util.Hashtable;
28  
29  import net.sourceforge.blogentis.om.Blog;
30  import net.sourceforge.blogentis.slide.SlideService;
31  import net.sourceforge.blogentis.turbine.BlogRunData;
32  import net.sourceforge.blogentis.turbine.BlogRunDataService;
33  
34  import org.apache.commons.collections.ExtendedProperties;
35  import org.apache.velocity.exception.ResourceNotFoundException;
36  import org.apache.velocity.runtime.resource.Resource;
37  import org.apache.velocity.runtime.resource.loader.ResourceLoader;
38  
39  public class TemplateResourceLoader
40          extends ResourceLoader {
41      private static Hashtable lastModifiedCache = new Hashtable();
42  
43      public void init(ExtendedProperties configuration) {
44          // ensure that the FileRetrieverService is initialized.
45          FileRetrieverService.getInstance();
46          SlideService.getInstance();
47      }
48  
49      public InputStream getResourceStream(String source)
50              throws ResourceNotFoundException {
51          BlogRunData data = BlogRunDataService.getCurrentRunData();
52          Blog b = null;
53          if (data != null)
54              b = data.getBlog();
55          InputStream is = null;
56          try {
57              AbstractFileResource fileInstance = FileRetrieverService
58                  .getInstance().getFile(b, source);
59              is = fileInstance.getFileAsInputStream();
60              // Notify the cache with the current timestamp.
61              if (b != null && !fileInstance.isOriginal()) {
62                  synchronized (lastModifiedCache) {
63                      String path = SlideService.getInstance()
64                          .getSlidePath(b, fileInstance.getPath(), true);
65                      lastModifiedCache.put(path, fileInstance.getLastModified());
66                  }
67              }
68          } catch (Exception e) {
69              throw new ResourceNotFoundException("Could not retrieve resource "
70                                                  + source + ":" + e.toString());
71          }
72          if (is == null)
73              throw new ResourceNotFoundException("Could not retrieve resource "
74                                                  + source);
75          return is;
76      }
77  
78      private String getResourceSlidePath(Resource resource) {
79          BlogRunData data = BlogRunDataService.getCurrentRunData();
80          Blog b = null;
81          if (data != null)
82              b = data.getBlog();
83          if (b != null)
84              return SlideService.getInstance().getSlidePath(b,
85                                                             resource.getName(),
86                                                             true);
87          return null;
88      }
89  
90      public boolean isSourceModified(Resource resource) {
91          String path = getResourceSlidePath(resource);
92          if (path != null) {
93              synchronized (lastModifiedCache) {
94                  Object o = lastModifiedCache.get(path);
95                  if ((o != null) && (o instanceof Date)
96                      && ((Date)o).getTime() > resource.getLastModified())
97                      return true;
98              }
99          }
100         return resource.getLastModified() != getLastModified(resource);
101     }
102 
103     public long getLastModified(Resource resource) {
104         BlogRunData data = BlogRunDataService.getCurrentRunData();
105         Blog b = null;
106         if (data != null)
107             b = data.getBlog();
108         String path = getResourceSlidePath(resource);
109         if (b != null && path != null) {
110             synchronized (lastModifiedCache) {
111                 Object o = lastModifiedCache.get(path);
112                 if (o != null && o instanceof Date)
113                     return ((Date)o).getTime();
114             }
115         }
116         AbstractFileResource afr = FileRetrieverService.getInstance()
117             .getFile(b, resource.getName());
118         Date d = afr.getLastModified();
119         if (b != null && !afr.isOriginal()) {
120             synchronized (lastModifiedCache) {
121                 lastModifiedCache.put(path, d);
122             }
123         }
124         return d.getTime();
125     }
126 
127     public static void resourceUpdated(String slidePath) {
128         synchronized (lastModifiedCache) {
129             /* question: update with new date or remove ? */
130             lastModifiedCache.put(slidePath, new Date());
131         }
132     }
133 }