View Javadoc
1 package org.apache.turbine.services.webmacro; 2 3 /* ==================================================================== 4 * The Apache Software License, Version 1.1 5 * 6 * Copyright (c) 2001 The Apache Software Foundation. All rights 7 * reserved. 8 * 9 * Redistribution and use in source and binary forms, with or without 10 * modification, are permitted provided that the following conditions 11 * are met: 12 * 13 * 1. Redistributions of source code must retain the above copyright 14 * notice, this list of conditions and the following disclaimer. 15 * 16 * 2. Redistributions in binary form must reproduce the above copyright 17 * notice, this list of conditions and the following disclaimer in 18 * the documentation and/or other materials provided with the 19 * distribution. 20 * 21 * 3. The end-user documentation included with the redistribution, 22 * if any, must include the following acknowledgment: 23 * "This product includes software developed by the 24 * Apache Software Foundation (http://www.apache.org/)." 25 * Alternately, this acknowledgment may appear in the software itself, 26 * if and wherever such third-party acknowledgments normally appear. 27 * 28 * 4. The names "Apache" and "Apache Software Foundation" and 29 * "Apache Turbine" must not be used to endorse or promote products 30 * derived from this software without prior written permission. For 31 * written permission, please contact apache@apache.org. 32 * 33 * 5. Products derived from this software may not be called "Apache", 34 * "Apache Turbine", nor may "Apache" appear in their name, without 35 * prior written permission of the Apache Software Foundation. 36 * 37 * THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESSED OR IMPLIED 38 * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES 39 * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE 40 * DISCLAIMED. IN NO EVENT SHALL THE APACHE SOFTWARE FOUNDATION OR 41 * ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, 42 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT 43 * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF 44 * USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND 45 * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, 46 * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT 47 * OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 48 * SUCH DAMAGE. 49 * ==================================================================== 50 * 51 * This software consists of voluntary contributions made by many 52 * individuals on behalf of the Apache Software Foundation. For more 53 * information on the Apache Software Foundation, please see 54 * <http://www.apache.org/>;. 55 */ 56 57 import java.io.File; 58 import org.apache.commons.configuration.Configuration; 59 import org.apache.turbine.services.TurbineServices; 60 import org.apache.turbine.services.template.TurbineTemplate; 61 import org.apache.turbine.util.Log; 62 import org.webmacro.Broker; 63 import org.webmacro.NotFoundException; 64 import org.webmacro.Template; 65 import org.webmacro.broker.Config; 66 import org.webmacro.broker.CreateResourceEvent; 67 import org.webmacro.broker.RequestResourceEvent; 68 import org.webmacro.broker.ResourceBroker; 69 import org.webmacro.broker.ResourceEvent; 70 import org.webmacro.broker.ResourceInitException; 71 import org.webmacro.broker.ResourceProvider; 72 import org.webmacro.engine.FileTemplate; 73 74 /*** 75 * <p>This class provides the WebMacro engine with <code>Temlate</code> 76 * instances. 77 * 78 * <p>This class was created as a replacement of 79 * <code>org.webmacro.resource.TemplateProvider</code> that relies on 80 * the properties of <code>WebMacroService</code> for locating the templates. 81 * 82 * @author <a href="mailto:Rafal.Krzewski@e-point.pl">Rafal Krzewski</a> 83 * @author <a href="mailto:dlr@collab.net">Daniel Rall</a> 84 * @version $Id: TurbineTemplateProvider.java,v 1.4 2002/07/11 16:53:23 mpoeschl Exp $ 85 * @deprecated 86 */ 87 public class TurbineTemplateProvider 88 implements ResourceProvider 89 { 90 /*** The single resource type we provide. */ 91 public static String TYPE = "template"; 92 93 /*** An array of supported resource types. */ 94 private static String[] types = { TYPE }; 95 96 /*** Expiration time of the provided resources. */ 97 private int cacheTime; 98 99 /*** Stored paths that are searched by this provider. */ 100 private String[] paths; 101 102 /*** Resource broker that manages this provider. */ 103 private Broker broker; 104 105 /*** 106 * Returns an array of supported resource types. 107 * 108 * @return A String[] with the supported resource types. 109 */ 110 public String[] getTypes() 111 { 112 return types; 113 } 114 115 /*** 116 * Returns expiration time of the provided resources. 117 * 118 * @return An int with the expiration time of the provided 119 * resources. 120 */ 121 public int resourceExpireTime() 122 { 123 return cacheTime; 124 } 125 126 /*** 127 * Returns the number of additional worker threads to be used by 128 * the provider 129 * 130 * @return The number of additional worker threads to be used by 131 * the provider. 132 */ 133 public int resourceThreads() 134 { 135 // Provider is I/O bound, therefore spawning an extra thread 136 // may be beneficial. 137 return 1; 138 } 139 140 /*** 141 * Initializes the provider. 142 * 143 * @param broker The <code>ResourceBroker</code> that manages this 144 * provider. 145 * @exception ResourceInitException The resource couldn't be initialized. 146 */ 147 public void init(ResourceBroker broker) 148 throws ResourceInitException 149 { 150 Log.info("TurbineTemplateProvider initializing"); 151 this.broker = broker; 152 153 try 154 { 155 try 156 { 157 String tmp = (String)broker.getValue(Config.TYPE, 158 Config.TEMPLATE_CACHE); 159 cacheTime = Integer.valueOf(tmp).intValue(); 160 } 161 catch (Exception e) 162 { 163 // 10 minutes. 164 cacheTime = 10 * 60 * 1000; 165 } 166 167 /* 168 * We are specifically not getting the configuration 169 * from the service because it hasn't finished 170 * init'ing we're getting it from the service broker. 171 */ 172 Configuration config = TurbineServices.getInstance() 173 .getConfiguration(WebMacroService.SERVICE_NAME); 174 175 paths = config.getStringArray("templates"); 176 paths = TurbineTemplate.translateTemplatePaths(paths); 177 178 StringBuffer pathMsg = new StringBuffer("TurbineTemplateProvider path(s):"); 179 180 for (int i = 0; i < paths.length; i++) 181 { 182 pathMsg.append('\n').append(paths[i]); 183 } 184 Log.info(pathMsg.toString()); 185 } 186 catch(Exception e) 187 { 188 String msg = "TurbineTemplateProvider failed to initialize"; 189 Log.error(msg, e); 190 throw new ResourceInitException(msg); 191 } 192 Log.info("TurbineTemplateProvider initialized"); 193 } 194 195 /*** 196 * Shuts down the provider. 197 */ 198 public void destroy() 199 { 200 broker = null; 201 paths = null; 202 } 203 204 /*** 205 * Retrieves a resource. 206 * 207 * @param request A <code>RequestResourceEvent</code>. 208 * @exception NotFoundException. 209 * @exception InterruptedException. 210 */ 211 public void resourceRequest(RequestResourceEvent request) 212 throws NotFoundException, 213 InterruptedException 214 { 215 Template t = findTemplate(request.getName()); 216 if (t == null) 217 { 218 // Let other providers handle it. 219 return; 220 } 221 try 222 { 223 // Announce that we have it. 224 request.set(t); 225 } 226 catch (Exception e) 227 { 228 // Ignore. 229 } 230 } 231 232 /*** 233 * Creates a new resource (not implemented). 234 * 235 * @param create A <code>CreateResourceEvent</code>. 236 * @exception NotFoundException. 237 * @exception InterruptedException. 238 */ 239 public void resourceCreate(CreateResourceEvent create) 240 throws NotFoundException, 241 InterruptedException 242 { 243 // Unsupported. 244 } 245 246 /*** 247 * Deletes a resource (not implemented). 248 * 249 * @param delete A <code>ResourceEvent</code>. 250 * @return Always false. 251 */ 252 public boolean resourceDelete(ResourceEvent delete) 253 { 254 // Unsupported. 255 return false; 256 } 257 258 /*** 259 * Saves a resource to permanet storage (not implemented). 260 * 261 * @param save A <code>ResourceEvent</code>. 262 * @return Always false. 263 */ 264 public boolean resourceSave(ResourceEvent save) 265 { 266 // Unsupported. 267 return false; 268 } 269 270 /*** 271 * Locates a template file, and instantiates <code>Template</code> 272 * object. 273 * 274 * @param name the name of the template to be located 275 * @return Instantiated <code>Template</code> object. 276 */ 277 private Template findTemplate( String name ) 278 { 279 for (int i = 0; i < paths.length; i++) 280 { 281 Template template; 282 File file = new File(paths[i], name); 283 //Log.debug("Looking for WebMacro template: path=" + paths[i] + 284 // ", name=" + name); 285 if (file.canRead()) 286 { 287 try 288 { 289 template = new FileTemplate 290 (broker, file, 291 TurbineWebMacroService.DEFAULT_ENCODING); 292 template.parse(); 293 return template; 294 } 295 catch(Exception e) 296 { 297 Log.error 298 ("TurbineTemplateProvider failed to load template " + 299 file.getPath(), e); 300 } 301 } 302 } 303 return null; 304 } 305 }

This page was automatically generated by Maven