Coverage Report - org.webmacro.resource.BrokerTemplateProviderHelper
 
Classes in this File Line Coverage Branch Coverage Complexity
BrokerTemplateProviderHelper
59%
32/54
40%
4/10
3
BrokerTemplateProviderHelper$UrlReloadContext
0%
0/5
0%
0/2
3
 
 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  
 
 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  
  * This class does the actual work of retrieving templates using the
 44  
  * Broker.  It is called by both BrokerTemplateProvider and
 45  
  * TemplateProvider.
 46  
  * @see org.webmacro.Provider
 47  
  * @see TemplateProvider
 48  
  * @see BrokerTemplateProvider
 49  
  * @author Brian Goetz
 50  
  * @since 0.96
 51  
  */
 52  63
 final public class BrokerTemplateProviderHelper
 53  
         implements ResourceLoader
 54  
 {
 55  
 
 56  40
     static Logger _log =  LoggerFactory.getLogger(BrokerTemplateProviderHelper.class);
 57  
 
 58  
     // INITIALIZATION
 59  
 
 60  
     private Broker _broker;
 61  
     //private int _cacheDuration;
 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  
      * Create a new TemplateProvider that uses the specified directory
 87  
      * as the source for Template objects that it will return.
 88  
      * 
 89  
      * @exception InitException provider failed to initialize
 90  
      */
 91  
     public void init (Broker b, Settings config) throws InitException
 92  
     {
 93  63
         _broker = b;
 94  
         //_cacheDuration = config.getIntegerSetting("TemplateExpireTime", 0);
 95  63
         reloadDelay = new ReloadDelayDecorator();
 96  63
         reloadDelay.init(b, config);
 97  63
     }
 98  
 
 99  
     /**
 100  
      * Grab a template based on its name.
 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  
             // Treat files as a special case
 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  
                 // encoding may be null. Will be handled by StreamTemplate
 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  
     /** We don't implement this one. */
 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  
     // IMPLEMENTATION
 192  
 
 193  
     /**
 194  
      * @param fileName the template filename to find
 195  
      * @return a File object that represents the specified template file, 
 196  
      * null if template file cannot be found.  
 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