| 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.util.StringTokenizer; |
| 28 | |
|
| 29 | |
import org.slf4j.Logger; |
| 30 | |
import org.slf4j.LoggerFactory; |
| 31 | |
|
| 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.TemplateException; |
| 38 | |
import org.webmacro.engine.FileTemplate; |
| 39 | |
import org.webmacro.util.Settings; |
| 40 | |
|
| 41 | |
|
| 42 | |
|
| 43 | |
|
| 44 | |
|
| 45 | |
|
| 46 | |
|
| 47 | |
|
| 48 | |
|
| 49 | |
|
| 50 | |
|
| 51 | |
|
| 52 | |
|
| 53 | 6 | final public class TemplateProvider extends CachingProvider |
| 54 | |
{ |
| 55 | |
|
| 56 | 2 | static Logger _log = LoggerFactory.getLogger(TemplateProvider.class); |
| 57 | |
|
| 58 | |
|
| 59 | 2 | private static String _pathSeparator = ";"; |
| 60 | 6 | private String _templateDirectory[] = null; |
| 61 | 6 | private Broker _broker = null; |
| 62 | |
private String _templatePath; |
| 63 | |
private BrokerTemplateProviderHelper _btpHelper; |
| 64 | |
|
| 65 | |
|
| 66 | |
|
| 67 | |
|
| 68 | |
|
| 69 | |
private ReloadDelayDecorator reloadDelay; |
| 70 | |
|
| 71 | |
static |
| 72 | |
{ |
| 73 | |
try |
| 74 | |
{ |
| 75 | 2 | _pathSeparator = System.getProperty("path.separator"); |
| 76 | |
} |
| 77 | 0 | catch (Throwable t) |
| 78 | |
{ |
| 79 | |
|
| 80 | 2 | } |
| 81 | 2 | } |
| 82 | |
|
| 83 | |
|
| 84 | |
|
| 85 | |
|
| 86 | |
|
| 87 | 6 | public static class FTReloadContext extends CacheReloadContext |
| 88 | |
{ |
| 89 | |
|
| 90 | |
private File file; |
| 91 | |
private long lastModified; |
| 92 | |
|
| 93 | |
public FTReloadContext (File f, long lastModified) |
| 94 | 224 | { |
| 95 | 224 | this.file = f; |
| 96 | 224 | this.lastModified = lastModified; |
| 97 | 224 | } |
| 98 | |
|
| 99 | |
public boolean shouldReload () |
| 100 | |
{ |
| 101 | 998 | return (lastModified != file.lastModified()); |
| 102 | |
} |
| 103 | |
} |
| 104 | |
|
| 105 | |
|
| 106 | |
|
| 107 | |
|
| 108 | |
|
| 109 | |
|
| 110 | |
|
| 111 | |
public void init (Broker b, Settings config) throws InitException |
| 112 | |
{ |
| 113 | 6 | super.init(b, config); |
| 114 | 6 | _broker = b; |
| 115 | |
|
| 116 | |
try |
| 117 | |
{ |
| 118 | 6 | _templatePath = config.getSetting("TemplatePath", ""); |
| 119 | 6 | if (_templatePath.equals("")) |
| 120 | 6 | _log.info("Template path is empty; will load from class path"); |
| 121 | |
else |
| 122 | |
{ |
| 123 | 0 | StringTokenizer st = |
| 124 | |
new StringTokenizer(_templatePath, _pathSeparator); |
| 125 | 0 | _templateDirectory = new String[st.countTokens()]; |
| 126 | 0 | for (int i = 0; i < _templateDirectory.length; i++) |
| 127 | |
{ |
| 128 | 0 | String dir = st.nextToken(); |
| 129 | 0 | _templateDirectory[i] = dir; |
| 130 | |
} |
| 131 | |
} |
| 132 | 6 | reloadDelay = new ReloadDelayDecorator(); |
| 133 | 6 | reloadDelay.init(b, config); |
| 134 | |
|
| 135 | 6 | _btpHelper = new BrokerTemplateProviderHelper(); |
| 136 | 6 | _btpHelper.init(b, config); |
| 137 | 6 | _btpHelper.setReload(_cacheSupportsReload); |
| 138 | |
} |
| 139 | 0 | catch (Exception e) |
| 140 | |
{ |
| 141 | 0 | throw new InitException("Could not initialize", e); |
| 142 | 6 | } |
| 143 | 6 | } |
| 144 | |
|
| 145 | |
|
| 146 | |
|
| 147 | |
|
| 148 | |
final public String getType () |
| 149 | |
{ |
| 150 | 24 | return "template"; |
| 151 | |
} |
| 152 | |
|
| 153 | |
|
| 154 | |
|
| 155 | |
|
| 156 | |
final public Object load (String name, CacheElement ce) |
| 157 | |
throws ResourceException |
| 158 | |
{ |
| 159 | 544 | Object ret = null; |
| 160 | |
|
| 161 | 544 | _log.info("Loading template: " + name); |
| 162 | |
|
| 163 | 544 | File tFile = findFileTemplate(name); |
| 164 | 544 | if (tFile != null) |
| 165 | |
{ |
| 166 | |
try |
| 167 | |
{ |
| 168 | 0 | Template t = new FileTemplate(_broker, tFile); |
| 169 | 0 | t.parse(); |
| 170 | 0 | ret = t; |
| 171 | 0 | if (_cacheSupportsReload) |
| 172 | |
{ |
| 173 | 0 | CacheReloadContext reloadContext = |
| 174 | |
new FTReloadContext(tFile, tFile.lastModified()); |
| 175 | 0 | ce.setReloadContext(reloadDelay.decorate("file", |
| 176 | |
reloadContext)); |
| 177 | |
} |
| 178 | |
} |
| 179 | 0 | catch (NullPointerException npe) |
| 180 | |
{ |
| 181 | 0 | _log.warn("TemplateProvider: Template not found: " + name, |
| 182 | |
npe); |
| 183 | 0 | throw new ResourceException("Error fetching template " + name, npe); |
| 184 | |
} |
| 185 | 0 | catch (TemplateException e) |
| 186 | |
{ |
| 187 | |
|
| 188 | 0 | _log.warn("TemplateProvider: Error occured while parsing " |
| 189 | |
+ name, e); |
| 190 | 0 | throw new InvalidResourceException("Error parsing template " |
| 191 | |
+ name, e); |
| 192 | |
} |
| 193 | 0 | catch (Exception e) |
| 194 | |
{ |
| 195 | 0 | _log.warn("TemplateProvider: Error occured while fetching " |
| 196 | |
+ name, e); |
| 197 | 0 | throw new ResourceException("Error fetching template " + name, e); |
| 198 | 0 | } |
| 199 | |
} |
| 200 | |
else |
| 201 | |
{ |
| 202 | |
|
| 203 | 544 | ret = _btpHelper.load(name, ce); |
| 204 | |
} |
| 205 | |
|
| 206 | 224 | if (ret == null) |
| 207 | |
{ |
| 208 | 0 | throw new NotFoundException( |
| 209 | |
this + " could not locate " + name + " on path " + _templatePath); |
| 210 | |
} |
| 211 | 224 | return ret; |
| 212 | |
} |
| 213 | |
|
| 214 | |
|
| 215 | |
|
| 216 | |
|
| 217 | |
|
| 218 | |
|
| 219 | |
|
| 220 | |
|
| 221 | |
|
| 222 | |
final private File findFileTemplate (String fileName) |
| 223 | |
{ |
| 224 | 544 | if (_templateDirectory != null) |
| 225 | |
{ |
| 226 | 0 | _log.debug("Looking for template in TemplatePath: " + fileName); |
| 227 | 0 | for (int i = 0; i < _templateDirectory.length; i++) |
| 228 | |
{ |
| 229 | 0 | String dir = _templateDirectory[i]; |
| 230 | 0 | File tFile = new File(dir, fileName); |
| 231 | 0 | if (tFile.canRead()) |
| 232 | |
{ |
| 233 | 0 | _log.debug("TemplateProvider: Found " + fileName + " in " + dir); |
| 234 | 0 | return tFile; |
| 235 | |
} |
| 236 | |
} |
| 237 | |
} |
| 238 | 544 | return null; |
| 239 | |
} |
| 240 | |
} |
| 241 | |
|
| 242 | |
|