| 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 | 6 | final public class BrokerTemplateProviderHelper |
| 53 | |
implements ResourceLoader |
| 54 | |
{ |
| 55 | |
|
| 56 | 2 | static Logger _log = LoggerFactory.getLogger(BrokerTemplateProviderHelper.class); |
| 57 | |
|
| 58 | |
|
| 59 | |
|
| 60 | |
private Broker _broker; |
| 61 | |
|
| 62 | |
|
| 63 | |
|
| 64 | 6 | private boolean _cacheSupportsReload = true; |
| 65 | |
private ReloadDelayDecorator reloadDelay; |
| 66 | |
|
| 67 | 6 | 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 | 6 | _broker = b; |
| 94 | |
|
| 95 | 6 | reloadDelay = new ReloadDelayDecorator(); |
| 96 | 6 | reloadDelay.init(b, config); |
| 97 | 6 | } |
| 98 | |
|
| 99 | |
|
| 100 | |
|
| 101 | |
|
| 102 | |
final public Object load (String name, CacheElement ce) |
| 103 | |
throws ResourceException |
| 104 | |
{ |
| 105 | 544 | Template t = null; |
| 106 | |
URL tUrl; |
| 107 | 544 | Object ret = null; |
| 108 | |
|
| 109 | 544 | tUrl = findTemplate(name); |
| 110 | |
try |
| 111 | |
{ |
| 112 | 544 | String protocol = tUrl.getProtocol(); |
| 113 | |
|
| 114 | 228 | if (protocol.equals("file")) |
| 115 | |
{ |
| 116 | 228 | File f = new File(tUrl.getFile()); |
| 117 | 228 | long lastMod = f.lastModified(); |
| 118 | |
|
| 119 | 228 | t = new FileTemplate(_broker, f); |
| 120 | 228 | t.parse(); |
| 121 | 224 | ret = t; |
| 122 | 224 | if (_cacheSupportsReload) |
| 123 | |
{ |
| 124 | 224 | CacheReloadContext reloadContext |
| 125 | |
= new TemplateProvider.FTReloadContext(f, lastMod); |
| 126 | 224 | ce.setReloadContext(reloadDelay.decorate("file", reloadContext)); |
| 127 | |
} |
| 128 | 224 | } |
| 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 | 316 | catch (NullPointerException npe) |
| 149 | |
{ |
| 150 | 316 | _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 | 4 | catch (Exception e) |
| 167 | |
{ |
| 168 | 4 | _log.warn("BrokerTemplateProvider: Error occured while fetching " |
| 169 | |
+ name, e); |
| 170 | 4 | throw new ResourceException("Error parsing template " + name, e); |
| 171 | 540 | } |
| 172 | 540 | if (ret == null) |
| 173 | 316 | throw new NotFoundException(this + " could not locate " + name); |
| 174 | |
|
| 175 | 224 | 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 | 6 | _cacheSupportsReload = reload; |
| 189 | 6 | } |
| 190 | |
|
| 191 | |
|
| 192 | |
|
| 193 | |
|
| 194 | |
|
| 195 | |
|
| 196 | |
|
| 197 | |
|
| 198 | |
final private URL findTemplate (String fileName) |
| 199 | |
{ |
| 200 | 544 | _log.debug("Looking for template in class path: " + fileName); |
| 201 | 544 | URL u = _broker.getTemplate(fileName); |
| 202 | 544 | if (u != null) |
| 203 | 228 | _log.debug("BrokerTemplateProvider: Found " + fileName |
| 204 | |
+ " at " + u.toString()); |
| 205 | 544 | return u; |
| 206 | |
} |
| 207 | |
} |
| 208 | |
|
| 209 | |
|