KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > roller > ui > rendering > velocity > ThemeResourceLoader


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.ui.rendering.velocity;
25
26 import java.io.ByteArrayInputStream JavaDoc;
27 import java.io.InputStream JavaDoc;
28 import java.io.UnsupportedEncodingException JavaDoc;
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.RollerException;
36 import org.apache.roller.ThemeNotFoundException;
37 import org.apache.roller.model.RollerFactory;
38 import org.apache.roller.model.ThemeManager;
39 import org.apache.roller.pojos.Theme;
40 import org.apache.roller.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  */

49 public class ThemeResourceLoader extends ResourceLoader {
50     
51     private static Log mLogger =
52         LogFactory.getFactory().getInstance(ThemeResourceLoader.class);
53         
54     
55     public void init(ExtendedProperties configuration) {
56         mLogger.debug(configuration);
57     }
58     
59     
60     public InputStream JavaDoc getResourceStream( String JavaDoc name )
61         throws ResourceNotFoundException {
62         
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 JavaDoc[] 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 = RollerFactory.getRoller().getThemeManager();
77             Theme theme = themeMgr.getTheme(split[0]);
78             ThemeTemplate template = theme.getTemplate(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 JavaDoc(template.getContents().getBytes("UTF-8"));
88             
89         } catch (UnsupportedEncodingException JavaDoc 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 JavaDoc(uex);
94            
95         } catch (ThemeNotFoundException tnfe) {
96             String JavaDoc msg = "ThemeResourceLoader Error: " + tnfe.getMessage();
97             mLogger.error(msg, tnfe);
98             throw new ResourceNotFoundException(msg);
99             
100         } catch (RollerException re) {
101             String JavaDoc 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) {
109         return (resource.getLastModified() != this.getLastModified(resource));
110     }
111     
112     
113     public long getLastModified(Resource resource) {
114         long last_mod = 0;
115         String JavaDoc 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 JavaDoc[] 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 = RollerFactory.getRoller().getThemeManager();
130             Theme theme = themeMgr.getTheme(split[0]);
131             ThemeTemplate template = theme.getTemplate(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 (RollerException re) {
141             // we don't like to see this happen, but oh well
142
}
143         
144         return last_mod;
145     }
146     
147 }
148
Popular Tags