View Javadoc
1 /* 2 * Copyright (c) 2003 3 * Information Desire GmbH 4 * All rights reserved. 5 */ 6 package com.infodesire.infobit.render.velocity; 7 8 import org.apache.velocity.runtime.resource.loader.ResourceLoader; 9 import org.apache.velocity.runtime.resource.Resource; 10 import org.apache.velocity.exception.ResourceNotFoundException; 11 12 import org.apache.commons.collections.ExtendedProperties; 13 14 import java.net.URL; 15 import java.io.IOException; 16 import java.io.InputStream; 17 18 import org.apache.commons.logging.Log; 19 import org.apache.commons.logging.LogFactory; 20 21 /*** 22 * advanced loader for classpath to overcome various velocity bottlenecks 23 * 24 * @author konstantin 25 * @created August 28, 2003 26 * @version $Revision: 1.2 $ 27 */ 28 public class AdvancedClasspathLoader extends ResourceLoader { 29 private static Log _log = LogFactory.getLog(AdvancedClasspathLoader.class); 30 31 32 /*** 33 * Gets the Resource attribute of the AdvancedClasspathLoader class 34 * 35 * @param resourceName Description of Parameter 36 * @return The Resource value 37 */ 38 public static URL getResource(String resourceName) { 39 URL url = null; 40 url = Thread.currentThread().getContextClassLoader().getResource(resourceName); 41 42 if (url == null) { 43 url = AdvancedClasspathLoader.class.getClassLoader().getResource(resourceName); 44 } 45 46 if ((url == null) && (resourceName != null) && (resourceName.charAt(0) != '/')) { 47 return getResource('/' + resourceName); 48 } 49 50 return url; 51 } 52 53 54 /*** 55 * Gets the ResourceStream attribute of the AdvancedClasspathLoader class 56 * 57 * @param name Description of Parameter 58 * @return The ResourceStream value 59 */ 60 public static InputStream getResourceAsStream(String name) { 61 URL url = getResource(name); 62 InputStream stream = null; 63 if (url != null) { 64 try { 65 return url.openStream(); 66 } catch (IOException ex) { 67 _log.error("could notoipen stram from URL " + url, ex); 68 return null; 69 } 70 } 71 return null; 72 } 73 74 75 /*** 76 * Get an InputStream so that the Runtime can build a template with it. 77 * 78 * @param name name of template to get 79 * @return InputStream containing the template 80 * @throws ResourceNotFoundException if template not found in classpath. 81 */ 82 public synchronized InputStream getResourceStream(String name) 83 throws ResourceNotFoundException { 84 InputStream result = null; 85 86 if (name == null || name.length() == 0) { 87 throw new ResourceNotFoundException("No template name provided"); 88 } 89 90 URL url = getResource(name); 91 rsvc.info("url : " + url); 92 if (url == null) { 93 throw new ResourceNotFoundException("Template could not be found..."); 94 } 95 try { 96 return url.openStream(); 97 } catch (IOException ex) { 98 throw new ResourceNotFoundException("Template could not be found..." + ex); 99 } 100 } 101 102 103 /*** 104 * Defaults to return false. 105 * 106 * @param resource Description of Parameter 107 * @return The SourceModified value 108 */ 109 public boolean isSourceModified(Resource resource) { 110 return false; 111 } 112 113 114 /*** 115 * Defaults to return 0 116 * 117 * @param resource Description of Parameter 118 * @return The LastModified value 119 */ 120 public long getLastModified(Resource resource) { 121 return 0; 122 } 123 124 125 /*** 126 * dumb init. nothing shall be done 127 * 128 * @param configuration Description of Parameter 129 */ 130 public void init(ExtendedProperties configuration) { 131 rsvc.info("AdvancedClasspathLoader: initialization starting."); 132 rsvc.info("AdvancedClasspathLoader: initialization complete."); 133 } 134 135 }

This page was automatically generated by Maven