| Classes in this File | Line Coverage | Branch Coverage | Complexity | ||||
| TemplateLoaderHelper |
|
| 2.2222222222222223;2.222 | ||||
| TemplateLoaderHelper$FTReloadContext |
|
| 2.2222222222222223;2.222 | ||||
| TemplateLoaderHelper$UrlReloadContext |
|
| 2.2222222222222223;2.222 |
| 1 | /* | |
| 2 | * Copyright (C) 1998-2000 Semiotek Inc. All Rights Reserved. | |
| 3 | * | |
| 4 | * Redistribution and use in source and binary forms, with or without | |
| 5 | * modification, are permitted under the terms of either of the following | |
| 6 | * Open Source licenses: | |
| 7 | * | |
| 8 | * The GNU General Public License, version 2, or any later version, as | |
| 9 | * published by the Free Software Foundation | |
| 10 | * (http://www.fsf.org/copyleft/gpl.html); | |
| 11 | * | |
| 12 | * or | |
| 13 | * | |
| 14 | * The Semiotek Public License (http://webmacro.org/LICENSE.) | |
| 15 | * | |
| 16 | * This software is provided "as is", with NO WARRANTY, not even the | |
| 17 | * implied warranties of fitness to purpose, or merchantability. You | |
| 18 | * assume all risks and liabilities associated with its use. | |
| 19 | * | |
| 20 | * See www.webmacro.org for more information on the WebMacro project. | |
| 21 | */ | |
| 22 | package org.webmacro.resource; | |
| 23 | ||
| 24 | import java.io.File; | |
| 25 | import java.io.IOException; | |
| 26 | import java.net.URL; | |
| 27 | import java.net.URLConnection; | |
| 28 | ||
| 29 | import org.webmacro.Broker; | |
| 30 | import org.webmacro.InitException; | |
| 31 | import org.webmacro.ResourceException; | |
| 32 | import org.webmacro.Template; | |
| 33 | import org.webmacro.TemplateException; | |
| 34 | import org.webmacro.engine.FileTemplate; | |
| 35 | import org.webmacro.engine.StreamTemplate; | |
| 36 | import org.webmacro.util.Settings; | |
| 37 | ||
| 38 | /** | |
| 39 | * Helper class for template loaders to actuall load a Template. | |
| 40 | * This class should be used for loading templates from URLs | |
| 41 | * or files. Thus, the template loader only has to care for | |
| 42 | * locating the template. | |
| 43 | * <br> | |
| 44 | * This class will take care of loading the template and setting | |
| 45 | * up a correct reload context, depending on the protocol used. | |
| 46 | * @author Sebastian Kanthak (sebastian.kanthak@muehlheim.de) | |
| 47 | */ | |
| 48 | public class TemplateLoaderHelper | |
| 49 | { | |
| 50 | ||
| 51 | private Broker broker; | |
| 52 | private ReloadDelayDecorator reloadDelay; | |
| 53 | ||
| 54 | /** | |
| 55 | * Construct a new TemplateLoaderHelper object. | |
| 56 | */ | |
| 57 | public TemplateLoaderHelper () | |
| 58 | { | |
| 59 | 0 | super(); |
| 60 | 0 | } |
| 61 | ||
| 62 | /** | |
| 63 | * Initialize this object. | |
| 64 | * @param b broker to use | |
| 65 | * @param config configuration to initialize from | |
| 66 | */ | |
| 67 | public void init (Broker b, Settings config) throws InitException | |
| 68 | { | |
| 69 | 0 | this.broker = b; |
| 70 | 0 | reloadDelay = new ReloadDelayDecorator(); |
| 71 | 0 | reloadDelay.init(b, config); |
| 72 | 0 | } |
| 73 | ||
| 74 | /** | |
| 75 | * Load a template from a file. | |
| 76 | * This will load the template from the specified file and | |
| 77 | * set up an appropriate reload context, if ce is not null. | |
| 78 | * It will use the check-for-reload delay specified for protocol | |
| 79 | * file. | |
| 80 | * @param file file to load template from | |
| 81 | * @param ce cache element for reload context or null, if no caching is used. | |
| 82 | * @return loaded template | |
| 83 | * @exception ResourceException if template could not be loaded or parsed. | |
| 84 | */ | |
| 85 | public Template load (File file, CacheElement ce) throws ResourceException | |
| 86 | { | |
| 87 | 0 | Template t = new FileTemplate(broker, file); |
| 88 | 0 | parseTemplate(t); |
| 89 | 0 | if (ce != null) |
| 90 | { | |
| 91 | 0 | CacheReloadContext reloadContext = new FTReloadContext(file, file.lastModified()); |
| 92 | 0 | ce.setReloadContext(reloadDelay.decorate("file", reloadContext)); |
| 93 | } | |
| 94 | 0 | return t; |
| 95 | } | |
| 96 | ||
| 97 | /** | |
| 98 | * Load a template from an url. | |
| 99 | * This will load the template from the specified url and | |
| 100 | * set up an appropriate reload context, if ce is not null. | |
| 101 | * It will use the check-for-reload delay specified for the url's | |
| 102 | * protocol. If protocl is "file", the more efficient file operations | |
| 103 | * of java are used. | |
| 104 | * @param url url to load template from | |
| 105 | * @param ce cache element for reload context or null, if no caching is used. | |
| 106 | * @return loaded template | |
| 107 | * @exception ResourceException if template could not be loaded or parsed. | |
| 108 | */ | |
| 109 | public Template load (URL url, CacheElement ce) throws ResourceException | |
| 110 | { | |
| 111 | 0 | if (url.getProtocol().equals("file")) |
| 112 | { | |
| 113 | // handle files directly, because it is more efficient | |
| 114 | 0 | File file = new File(url.getFile()); |
| 115 | 0 | return load(file, ce); |
| 116 | } | |
| 117 | else | |
| 118 | { | |
| 119 | try | |
| 120 | { | |
| 121 | 0 | URLConnection conn = url.openConnection(); |
| 122 | 0 | long lastMod = conn.getLastModified(); |
| 123 | 0 | String encoding = conn.getContentEncoding(); |
| 124 | // encoding may be null. Will be handled by StreamTemplate | |
| 125 | 0 | Template t = new StreamTemplate(broker, conn.getInputStream(), encoding); |
| 126 | 0 | t.setName(url.toExternalForm()); |
| 127 | 0 | parseTemplate(t); |
| 128 | 0 | if (ce != null) |
| 129 | { | |
| 130 | 0 | CacheReloadContext reloadContext = new UrlReloadContext(url, lastMod); |
| 131 | 0 | ce.setReloadContext(reloadDelay.decorate(url.getProtocol(), reloadContext)); |
| 132 | } | |
| 133 | 0 | return t; |
| 134 | } | |
| 135 | 0 | catch (IOException e) |
| 136 | { | |
| 137 | 0 | throw new InvalidResourceException("IOException while reading template from " + url, e); |
| 138 | } | |
| 139 | } | |
| 140 | } | |
| 141 | ||
| 142 | /** | |
| 143 | * Encapsulates calls to Template.parse and wraps | |
| 144 | * checked exceptions into ResourceExceptions. | |
| 145 | */ | |
| 146 | private void parseTemplate (Template template) throws ResourceException | |
| 147 | { | |
| 148 | try | |
| 149 | { | |
| 150 | 0 | template.parse(); |
| 151 | } | |
| 152 | 0 | catch (IOException e) |
| 153 | { | |
| 154 | 0 | throw new InvalidResourceException("IOException while reading template " + template.getName(), e); |
| 155 | } | |
| 156 | 0 | catch (TemplateException e) |
| 157 | { | |
| 158 | 0 | throw new InvalidResourceException("Error while parsing template: " + template.getName(), e); |
| 159 | 0 | } |
| 160 | 0 | } |
| 161 | ||
| 162 | /** | |
| 163 | * ReloadContext for file templates. Uses last-modified to determine | |
| 164 | * if resource should be reloaded. | |
| 165 | */ | |
| 166 | private static class FTReloadContext extends CacheReloadContext | |
| 167 | { | |
| 168 | ||
| 169 | private File file; | |
| 170 | private long lastModified; | |
| 171 | ||
| 172 | public FTReloadContext (File f, long lastModified) | |
| 173 | 0 | { |
| 174 | 0 | this.file = f; |
| 175 | 0 | this.lastModified = lastModified; |
| 176 | 0 | } |
| 177 | ||
| 178 | public boolean shouldReload () | |
| 179 | { | |
| 180 | 0 | return (lastModified != file.lastModified()); |
| 181 | } | |
| 182 | } | |
| 183 | ||
| 184 | /** | |
| 185 | * ReloadContext for url templates. Uses last-modified to determine | |
| 186 | * if resource should be reloaded. | |
| 187 | */ | |
| 188 | private static class UrlReloadContext extends CacheReloadContext | |
| 189 | { | |
| 190 | ||
| 191 | private long lastModified; | |
| 192 | private URL url; | |
| 193 | ||
| 194 | public UrlReloadContext (URL url, long lastModified) | |
| 195 | 0 | { |
| 196 | 0 | this.url = url; |
| 197 | 0 | this.lastModified = lastModified; |
| 198 | 0 | } |
| 199 | ||
| 200 | public boolean shouldReload () | |
| 201 | { | |
| 202 | 0 | return (lastModified != UrlProvider.getUrlLastModified(url)); |
| 203 | } | |
| 204 | } | |
| 205 | } | |
| 206 |