File Source: ThemeResourceLoader.java
1 /*
2 * Licensed to the Apache Software Foundation (ASF) under one or more
3 * contributor license agreements. The ASF licenses this file to You
4 * under the Apache License, Version 2.0 (the "License"); you may not
5 * use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License. For additional information regarding
15 * copyright in this work, please see the NOTICE file in the top level
16 * directory of this distribution.
17 */
18 /*
19 * ThemeResourceLoader.java
20 *
21 * Created on June 28, 2005, 12:25 PM
22 */
23
24 package org.apache.roller.weblogger.ui.rendering.velocity;
25
26 import java.io.ByteArrayInputStream;
27 import java.io.InputStream;
28 import java.io.UnsupportedEncodingException;
29 import org.apache.commons.collections.ExtendedProperties;
30 import org.apache.commons.logging.Log;
31 import org.apache.commons.logging.LogFactory;
32 import org.apache.velocity.exception.ResourceNotFoundException;
33 import org.apache.velocity.runtime.resource.Resource;
34 import org.apache.velocity.runtime.resource.loader.ResourceLoader;
35 import org.apache.roller.weblogger.WebloggerException;
36 import org.apache.roller.weblogger.business.themes.ThemeNotFoundException;
37 import org.apache.roller.weblogger.business.WebloggerFactory;
38 import org.apache.roller.weblogger.business.themes.ThemeManager;
39 import org.apache.roller.weblogger.pojos.Theme;
40 import org.apache.roller.weblogger.pojos.ThemeTemplate;
41
42
43 /**
44 * The ThemeResourceLoader is a Velocity template loader which loads
45 * templates from shared themes.
46 *
47 * @author Allen Gilliland
48 */
/*
P/P * Method: void org.apache.roller.weblogger.ui.rendering.velocity.ThemeResourceLoader()
*/
49 public class ThemeResourceLoader extends ResourceLoader {
50
/*
P/P * Method: org.apache.roller.weblogger.ui.rendering.velocity.ThemeResourceLoader__static_init
*
* Presumptions:
* org.apache.commons.logging.LogFactory:getFactory(...)@51 != null
*
* Postconditions:
* init'ed(mLogger)
*/
51 private static Log mLogger =
52 LogFactory.getFactory().getInstance(ThemeResourceLoader.class);
53
54
55 public void init(ExtendedProperties configuration) {
/*
P/P * Method: void init(ExtendedProperties)
*
* Preconditions:
* mLogger != null
*/
56 mLogger.debug(configuration);
57 }
58
59
60 public InputStream getResourceStream( String name )
61 throws ResourceNotFoundException {
62
/*
P/P * Method: InputStream getResourceStream(String)
* getResourceStream fails for all possible inputs
*
* Preconditions:
* (soft) mLogger != null
* (soft) name != null
*
* Presumptions:
* java.lang.String:length(...)@65 >= 1
*/
63 mLogger.debug("Looking up resource named ... "+name);
64
65 if (name == null || name.length() < 1) {
66 throw new ResourceNotFoundException("Need to specify a template name!");
67 }
68
69 try {
70 // parse the name ... theme templates name are <theme>:<template>
71 String[] split = name.split(":", 2);
+ 72 if(split.length < 2)
+ 73 throw new ResourceNotFoundException("Invalid ThemeRL key "+name);
74
75 // lookup the template from the proper theme
+ 76 ThemeManager themeMgr = WebloggerFactory.getWeblogger().getThemeManager();
77 Theme theme = themeMgr.getTheme(split[0]);
78 ThemeTemplate template = theme.getTemplateByName(split[1]);
79
80 if(template == null)
81 throw new ResourceNotFoundException("Template ["+split[1]+
82 "] doesn't seem to be part of theme ["+split[0]+"]");
83
84 mLogger.debug("Resource found!");
85
86 // return the input stream
87 return new ByteArrayInputStream(template.getContents().getBytes("UTF-8"));
88
89 } catch (UnsupportedEncodingException uex) {
90 // We expect UTF-8 in all JRE installation.
91 // This rethrows as a Runtime exception after logging.
92 mLogger.error(uex);
93 throw new RuntimeException(uex);
94
95 } catch (ThemeNotFoundException tnfe) {
96 String msg = "ThemeResourceLoader Error: " + tnfe.getMessage();
97 mLogger.error(msg, tnfe);
98 throw new ResourceNotFoundException(msg);
99
100 } catch (WebloggerException re) {
101 String msg = "RollerResourceLoader Error: " + re.getMessage();
102 mLogger.error( msg, re );
103 throw new ResourceNotFoundException(msg);
104 }
105 }
106
107
108 public boolean isSourceModified(Resource resource) {
/*
P/P * Method: bool isSourceModified(Resource)
*
* Preconditions:
* mLogger != null
* resource != null
*
* Postconditions:
* init'ed(return_value)
*/
109 return (resource.getLastModified() != this.getLastModified(resource));
110 }
111
112
113 public long getLastModified(Resource resource) {
/*
P/P * Method: long getLastModified(Resource)
*
* Preconditions:
* mLogger != null
* resource != null
*
* Postconditions:
* return_value == 0
*
* Test Vectors:
* java.lang.String:length(...)@119: {1..232-1}, {0}
* org.apache.velocity.runtime.resource.Resource:getName(...)@115: Addr_Set{null}, Inverse{null}
*/
114 long last_mod = 0;
115 String name = resource.getName();
116
117 mLogger.debug("Checking last modified time for resource named ... "+name);
118
119 if (name == null || name.length() < 1)
120 return last_mod;
121
122 try {
123 // parse the name ... theme templates name are <theme>:<template>
124 String[] split = name.split(":", 2);
+ 125 if(split.length < 2)
126 return last_mod;
127
128 // lookup the template from the proper theme
+ 129 ThemeManager themeMgr = WebloggerFactory.getWeblogger().getThemeManager();
130 Theme theme = themeMgr.getTheme(split[0]);
131 ThemeTemplate template = theme.getTemplateByName(split[1]);
132
133 if(template == null)
134 return last_mod;
135
136 last_mod = template.getLastModified().getTime();
137
138 } catch (ThemeNotFoundException tnfe) {
139 // ignore
140 } catch (WebloggerException re) {
141 // we don't like to see this happen, but oh well
142 }
143
144 return last_mod;
145 }
146
147 }
SofCheck Inspector Build Version : 2.18479
| ThemeResourceLoader.java |
2009-Jan-02 14:25:02 |
| ThemeResourceLoader.class |
2009-Sep-04 03:12:45 |