1 package net.sourceforge.blogentis.storage;
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
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
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
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
130 lastModifiedCache.put(slidePath, new Date());
131 }
132 }
133 }