| 1 18 23 24 package org.apache.roller.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.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 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 getResourceStream( String 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 String [] split = name.split(":", 2); 72 if(split.length < 2) 73 throw new ResourceNotFoundException("Invalid ThemeRL key "+name); 74 75 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 new ByteArrayInputStream (template.getContents().getBytes("UTF-8")); 88 89 } catch (UnsupportedEncodingException uex) { 90 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 (RollerException 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) { 109 return (resource.getLastModified() != this.getLastModified(resource)); 110 } 111 112 113 public long getLastModified(Resource resource) { 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 String [] split = name.split(":", 2); 125 if(split.length < 2) 126 return last_mod; 127 128 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 } catch (RollerException re) { 141 } 143 144 return last_mod; 145 } 146 147 } 148 | Popular Tags |