1 | |
|
2 | |
|
3 | |
|
4 | |
|
5 | |
|
6 | |
|
7 | |
|
8 | |
|
9 | |
|
10 | |
|
11 | |
|
12 | |
|
13 | |
|
14 | |
|
15 | |
|
16 | |
|
17 | |
|
18 | |
|
19 | |
|
20 | |
|
21 | |
|
22 | |
|
23 | |
|
24 | |
package org.webmacro.resource; |
25 | |
|
26 | |
import java.io.File; |
27 | |
import java.io.IOException; |
28 | |
import java.net.URL; |
29 | |
|
30 | |
import org.slf4j.Logger; |
31 | |
import org.slf4j.LoggerFactory; |
32 | |
import org.webmacro.Broker; |
33 | |
import org.webmacro.InitException; |
34 | |
import org.webmacro.NotFoundException; |
35 | |
import org.webmacro.ResourceException; |
36 | |
import org.webmacro.Template; |
37 | |
import org.webmacro.engine.FileTemplate; |
38 | |
import org.webmacro.engine.ParseException; |
39 | |
import org.webmacro.engine.StreamTemplate; |
40 | |
import org.webmacro.util.Settings; |
41 | |
|
42 | |
|
43 | |
|
44 | |
|
45 | |
|
46 | |
|
47 | |
|
48 | |
|
49 | |
|
50 | |
|
51 | |
|
52 | 63 | final public class BrokerTemplateProviderHelper |
53 | |
implements ResourceLoader |
54 | |
{ |
55 | |
|
56 | 40 | static Logger _log = LoggerFactory.getLogger(BrokerTemplateProviderHelper.class); |
57 | |
|
58 | |
|
59 | |
|
60 | |
private Broker _broker; |
61 | |
|
62 | |
|
63 | |
|
64 | 63 | private boolean _cacheSupportsReload = true; |
65 | |
private ReloadDelayDecorator reloadDelay; |
66 | |
|
67 | 63 | private static class UrlReloadContext extends CacheReloadContext |
68 | |
{ |
69 | |
|
70 | |
private long lastModified; |
71 | |
private URL url; |
72 | |
|
73 | |
public UrlReloadContext (URL url, long lastModified) |
74 | 0 | { |
75 | 0 | this.url = url; |
76 | 0 | this.lastModified = lastModified; |
77 | 0 | } |
78 | |
|
79 | |
public boolean shouldReload () |
80 | |
{ |
81 | 0 | return (lastModified != UrlProvider.getUrlLastModified(url)); |
82 | |
} |
83 | |
} |
84 | |
|
85 | |
|
86 | |
|
87 | |
|
88 | |
|
89 | |
|
90 | |
|
91 | |
public void init (Broker b, Settings config) throws InitException |
92 | |
{ |
93 | 63 | _broker = b; |
94 | |
|
95 | 63 | reloadDelay = new ReloadDelayDecorator(); |
96 | 63 | reloadDelay.init(b, config); |
97 | 63 | } |
98 | |
|
99 | |
|
100 | |
|
101 | |
|
102 | |
final public Object load (String name, CacheElement ce) |
103 | |
throws ResourceException |
104 | |
{ |
105 | 57 | Template t = null; |
106 | |
URL tUrl; |
107 | 57 | Object ret = null; |
108 | |
|
109 | 57 | tUrl = findTemplate(name); |
110 | |
try |
111 | |
{ |
112 | 57 | String protocol = tUrl.getProtocol(); |
113 | |
|
114 | 57 | if (protocol.equals("file")) |
115 | |
{ |
116 | 57 | File f = new File(tUrl.getFile()); |
117 | 57 | long lastMod = f.lastModified(); |
118 | |
|
119 | 57 | t = new FileTemplate(_broker, f); |
120 | 57 | t.parse(); |
121 | 57 | ret = t; |
122 | 57 | if (_cacheSupportsReload) |
123 | |
{ |
124 | 57 | CacheReloadContext reloadContext |
125 | |
= new TemplateProvider.FTReloadContext(f, lastMod); |
126 | 57 | ce.setReloadContext(reloadDelay.decorate("file", reloadContext)); |
127 | |
} |
128 | 57 | } |
129 | |
else |
130 | |
{ |
131 | 0 | long lastMod = UrlProvider.getUrlLastModified(tUrl); |
132 | 0 | String encoding = tUrl.openConnection().getContentEncoding(); |
133 | |
|
134 | 0 | t = new StreamTemplate(_broker, |
135 | |
UrlProvider.getUrlInputStream(tUrl), |
136 | |
encoding); |
137 | 0 | t.setName(name); |
138 | 0 | t.parse(); |
139 | 0 | ret = t; |
140 | 0 | if (_cacheSupportsReload) |
141 | |
{ |
142 | 0 | CacheReloadContext reloadContext = new UrlReloadContext(tUrl, lastMod); |
143 | 0 | ce.setReloadContext(reloadDelay.decorate(tUrl.getProtocol(), |
144 | |
reloadContext)); |
145 | |
} |
146 | |
} |
147 | |
} |
148 | 0 | catch (NullPointerException npe) |
149 | |
{ |
150 | 0 | _log.warn("BrokerTemplateProvider: Template not found: " + name); |
151 | |
} |
152 | 0 | catch (ParseException e) |
153 | |
{ |
154 | 0 | _log.warn("BrokerTemplateProvider: Error occured while parsing " |
155 | |
+ name, e); |
156 | 0 | throw new InvalidResourceException("Error parsing template " + name, |
157 | |
e); |
158 | |
} |
159 | 0 | catch (IOException e) |
160 | |
{ |
161 | 0 | _log.warn("BrokerTemplateProvider: IOException while parsing " |
162 | |
+ name, e); |
163 | 0 | throw new InvalidResourceException("Error parsing template " + name, |
164 | |
e); |
165 | |
} |
166 | 0 | catch (Exception e) |
167 | |
{ |
168 | 0 | _log.warn("BrokerTemplateProvider: Error occured while fetching " |
169 | |
+ name, e); |
170 | 0 | throw new ResourceException("Error parsing template " + name, e); |
171 | 57 | } |
172 | 57 | if (ret == null) |
173 | 0 | throw new NotFoundException(this + " could not locate " + name); |
174 | |
|
175 | 57 | return ret; |
176 | |
} |
177 | |
|
178 | |
|
179 | |
|
180 | |
public Object load (Object query, CacheElement ce) |
181 | |
throws ResourceException |
182 | |
{ |
183 | 0 | throw new ResourceException("CachingProvider: load(Object) not supported, use load(String)"); |
184 | |
} |
185 | |
|
186 | |
public void setReload (boolean reload) |
187 | |
{ |
188 | 63 | _cacheSupportsReload = reload; |
189 | 63 | } |
190 | |
|
191 | |
|
192 | |
|
193 | |
|
194 | |
|
195 | |
|
196 | |
|
197 | |
|
198 | |
final private URL findTemplate (String fileName) |
199 | |
{ |
200 | 57 | _log.debug("Looking for template in class path: " + fileName); |
201 | 57 | URL u = _broker.getTemplate(fileName); |
202 | 57 | if (u != null) |
203 | 57 | _log.debug("BrokerTemplateProvider: Found " + fileName |
204 | |
+ " at " + u.toString()); |
205 | 57 | return u; |
206 | |
} |
207 | |
} |
208 | |
|
209 | |
|