Classes in this File | Line Coverage | Branch Coverage | Complexity | ||||
ServletContextTemplateLoader |
|
| 4.666666666666667;4.667 |
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 org.slf4j.Logger; | |
25 | import org.slf4j.LoggerFactory; | |
26 | import org.webmacro.Broker; | |
27 | import org.webmacro.InitException; | |
28 | import org.webmacro.ResourceException; | |
29 | import org.webmacro.Template; | |
30 | import org.webmacro.servlet.ServletBroker; | |
31 | import org.webmacro.util.Settings; | |
32 | ||
33 | import javax.servlet.ServletContext; | |
34 | import java.net.MalformedURLException; | |
35 | import java.net.URL; | |
36 | ||
37 | /** | |
38 | * Implementation of TemplateLoader that loads template from web-apps. | |
39 | * This will require a broker that is an instance of org.webmacro.servlet.ServletBroker | |
40 | * to have access to the ServletContext. | |
41 | * <br> | |
42 | * Templates will be loaded by ServletContext.getResource(). | |
43 | * The "path" setting is used as a prefix for all request, so it should | |
44 | * end with a slash.<br> | |
45 | * <br> | |
46 | * Example: If you have <pre>webapp:/WEB-INF/templates/</pre> as a | |
47 | * TemplateLoaderPath and request the template "foo/bar.wm", it will search for | |
48 | * "WEB-INF/templates/foo/bar.wm" in your web-app directory. | |
49 | * <br> | |
50 | * This TemplateLoader will automatically add trailing and starting slashed, if they | |
51 | * are missing. The starting slash is required by the servlet specification. | |
52 | * @author Sebastian Kanthak (sebastian.kanthak@muehlheim.de) | |
53 | */ | |
54 | 0 | public class ServletContextTemplateLoader extends AbstractTemplateLoader |
55 | { | |
56 | ||
57 | 0 | static Logger _log = LoggerFactory.getLogger(ServletContextTemplateLoader.class); |
58 | ||
59 | private ServletContext loader; | |
60 | private String path; | |
61 | ||
62 | public void init (Broker broker, Settings config) throws InitException | |
63 | { | |
64 | 0 | super.init(broker, config); |
65 | 0 | if (broker instanceof ServletBroker) |
66 | { | |
67 | 0 | loader = ((ServletBroker) broker).getServletContext(); |
68 | } | |
69 | else | |
70 | { | |
71 | 0 | throw new InitException("ServletContextTemplateLoader only works with instances of " + |
72 | "org.webmacro.servlet.ServletBroker"); | |
73 | } | |
74 | 0 | } |
75 | ||
76 | public void setConfig (String config) | |
77 | { | |
78 | // as we'll later use this as a prefix, it should end with a slash | |
79 | 0 | if (config.length() > 0 && !config.endsWith("/")) |
80 | { | |
81 | 0 | _log.info("ServletContextTemplateLoader: appending \"/\" to path " + config); |
82 | 0 | config = config.concat("/"); |
83 | } | |
84 | ||
85 | // the spec says, this has to start with a slash | |
86 | 0 | if (!config.startsWith("/")) |
87 | { | |
88 | 0 | _log.info("ServletContextTemplateLoader: adding \"/\" at the beginning of path " + config); |
89 | 0 | config = "/".concat(config); |
90 | } | |
91 | 0 | this.path = config; |
92 | 0 | } |
93 | ||
94 | public Template load (String query, CacheElement ce) throws ResourceException | |
95 | { | |
96 | try | |
97 | { | |
98 | 0 | URL url = loader.getResource(path.concat(query)); |
99 | 0 | if (url != null && _log.isDebugEnabled()) |
100 | { | |
101 | 0 | _log.debug("ServletContextTemplateProvider: Found Template " + url.toString()); |
102 | } | |
103 | 0 | return (url != null) ? helper.load(url, ce) : null; |
104 | } | |
105 | 0 | catch (MalformedURLException e) |
106 | { | |
107 | 0 | throw new InvalidResourceException("ServletContextTemplateLoader: Could not load " + query, e); |
108 | } | |
109 | } | |
110 | } |