| Classes in this File | Line Coverage | Branch Coverage | Complexity | ||||
| DelegatingTemplateProvider |
|
| 4.2;4.2 |
| 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.util.ArrayList; | |
| 27 | import java.util.Arrays; | |
| 28 | import java.util.Collections; | |
| 29 | import java.util.List; | |
| 30 | ||
| 31 | import org.slf4j.Logger; | |
| 32 | import org.slf4j.LoggerFactory; | |
| 33 | import org.webmacro.Broker; | |
| 34 | import org.webmacro.Context; | |
| 35 | import org.webmacro.InitException; | |
| 36 | import org.webmacro.NotFoundException; | |
| 37 | import org.webmacro.ResourceException; | |
| 38 | import org.webmacro.Template; | |
| 39 | import org.webmacro.util.Settings; | |
| 40 | ||
| 41 | /** | |
| 42 | * Alternative implementation of a TemplateProvider that uses TemplateLoaders to do the actual work. | |
| 43 | * This template provider controls a list of TemplateLoaders to do the actual work of loading | |
| 44 | * a template. It asks the template loaders one by one, until a template is found or the end of | |
| 45 | * the list is reached. It is configured by a list of "TemplateLoaderPath.n" settings in | |
| 46 | * WebMacro.properties.<br> | |
| 47 | * <br> | |
| 48 | * Each template loader is described by an url like syntax with a TemplateLoaderPath.n setting, | |
| 49 | * where n should be a number starting from one. | |
| 50 | * <br> | |
| 51 | * Each template loader path is of the form "[protocol:][path]". If the protocol part in square brackets | |
| 52 | * is ommited, "default:" is assumed. | |
| 53 | * For each protocol, a "TemplateLoader.protocol" setting must give the fully qualified | |
| 54 | * classname of the template loader to be used for this protocol. | |
| 55 | * Example configuration:<br> | |
| 56 | * <pre> | |
| 57 | * TemplateLoaderPath.1=. | |
| 58 | * TemplateLoaderPath.2=classpath: | |
| 59 | * TemplateLoaderPath.3=webapp:/WEB-INF/templates/ | |
| 60 | * TemplateLoader.default=org.webmacro.resource.FileTemplateLoader | |
| 61 | * TemplateLoader.classpath=org.webmacro.resource.ClassPathTemplateLoader | |
| 62 | * TemplateLoader.webapp=org.webmacro.resource.ServletContextTemplateLoader | |
| 63 | * </pre> | |
| 64 | * This configuration will search for templates at three locations in this order: | |
| 65 | * <ol> | |
| 66 | * <li>The current directory (".") | |
| 67 | * <li>The classpath (classpath:) | |
| 68 | * <li>The directory WEB-INF/templates/ in the web-app directory ("webapp:/WEB-INF/templates/") | |
| 69 | * </ol> | |
| 70 | * NOTE This setup only makes sense in a web-app environment, because the webapp template loader | |
| 71 | * won't work otherwise. | |
| 72 | * @author Sebastian Kanthak (sebastian.kanthak@muehlheim.de) | |
| 73 | */ | |
| 74 | 0 | public class DelegatingTemplateProvider extends CachingProvider |
| 75 | { | |
| 76 | ||
| 77 | 0 | static Logger _log = LoggerFactory.getLogger(DelegatingTemplateProvider.class); |
| 78 | ||
| 79 | private TemplateLoaderFactory factory; | |
| 80 | private TemplateLoader[] templateLoaders; | |
| 81 | ||
| 82 | public void init (Broker broker, Settings config) throws InitException | |
| 83 | { | |
| 84 | 0 | super.init(broker, config); |
| 85 | ||
| 86 | 0 | String factoryClass = config.getSetting("TemplateLoaderFactory", ""); |
| 87 | 0 | _log.info("DelegatingTemplateProvider: Using TemplateLoaderFactory " + factoryClass); |
| 88 | 0 | factory = createFactory(factoryClass); |
| 89 | ||
| 90 | 0 | List loaders = new ArrayList(); |
| 91 | ||
| 92 | // for compatability reasons, check old TemplatePath setting | |
| 93 | 0 | if (config.getBooleanSetting("DelegatingTemplateProvider.EmulateTemplatePath", false)) |
| 94 | { | |
| 95 | 0 | if (config.getSetting("TemplatePath", "").length() > 0) |
| 96 | { | |
| 97 | 0 | TemplateLoader loader = new TemplatePathTemplateLoader(); |
| 98 | 0 | loader.init(broker, config); |
| 99 | 0 | loader.setConfig(""); |
| 100 | 0 | loaders.add(loader); |
| 101 | } | |
| 102 | } | |
| 103 | ||
| 104 | 0 | int i = 0; |
| 105 | 0 | String loader = config.getSetting("TemplateLoaderPath.".concat(String.valueOf(i + 1))); |
| 106 | 0 | while (loader != null) |
| 107 | { | |
| 108 | 0 | loaders.add(factory.getTemplateLoader(broker, loader)); |
| 109 | 0 | i++; |
| 110 | 0 | loader = config.getSetting("TemplateLoaderPath.".concat(String.valueOf(i + 1))); |
| 111 | } | |
| 112 | 0 | templateLoaders = new TemplateLoader[loaders.size()]; |
| 113 | 0 | loaders.toArray(templateLoaders); |
| 114 | 0 | } |
| 115 | ||
| 116 | public String getType () | |
| 117 | { | |
| 118 | 0 | return "template"; |
| 119 | } | |
| 120 | ||
| 121 | /** | |
| 122 | * Ask all template loaders to load a template from query. | |
| 123 | * Returns the template from the first provider, that returns a non-null value | |
| 124 | * or throws a NotFoundException, if all providers return null. | |
| 125 | */ | |
| 126 | public Object load (String query, CacheElement ce) throws ResourceException | |
| 127 | { | |
| 128 | 0 | for (int i = 0; i < templateLoaders.length; i++) |
| 129 | { | |
| 130 | 0 | Template t = templateLoaders[i].load(query, ce); |
| 131 | 0 | if (t != null) |
| 132 | { | |
| 133 | 0 | return t; |
| 134 | } | |
| 135 | } | |
| 136 | 0 | throw new NotFoundException("Could not locate template " + query); |
| 137 | } | |
| 138 | ||
| 139 | /** | |
| 140 | * Returns an unmodifieable list of this provider's template loaders. | |
| 141 | * The list is has the same order used for searching templates. You may | |
| 142 | * use this method to access template loaders and change their settings | |
| 143 | * at runtime if they have an appropriate method. | |
| 144 | * @return unmodifieable list of TemplateLoader objects | |
| 145 | */ | |
| 146 | public List getTemplateLoaders () | |
| 147 | { | |
| 148 | 0 | return Collections.unmodifiableList(Arrays.asList(templateLoaders)); |
| 149 | } | |
| 150 | ||
| 151 | protected TemplateLoaderFactory createFactory (String classname) throws InitException | |
| 152 | { | |
| 153 | try | |
| 154 | { | |
| 155 | 0 | return (TemplateLoaderFactory) Class.forName(classname).newInstance(); |
| 156 | } | |
| 157 | 0 | catch (ClassNotFoundException e) |
| 158 | { | |
| 159 | 0 | throw new InitException("Class " + classname + " for template loader factory not found", e); |
| 160 | } | |
| 161 | 0 | catch (InstantiationException e) |
| 162 | { | |
| 163 | 0 | throw new InitException("Could not instantiate class " + classname + " for template loader factory", e); |
| 164 | } | |
| 165 | 0 | catch (IllegalAccessException e) |
| 166 | { | |
| 167 | 0 | throw new InitException("Could not instantiate class " + classname + " for template loader facory", e); |
| 168 | } | |
| 169 | 0 | catch (ClassCastException e) |
| 170 | { | |
| 171 | 0 | throw new InitException("Class " + classname + " for template loader factory does not implement " + |
| 172 | "interface org.webmacro.resource.TemplateLoaderFactory", e); | |
| 173 | } | |
| 174 | } | |
| 175 | } |