1 56 package org.objectstyle.cayenne.util; 57 58 import java.net.MalformedURLException ; 59 import java.net.URL ; 60 import java.util.ArrayList ; 61 import java.util.Iterator ; 62 import java.util.List ; 63 64 import javax.servlet.ServletContext ; 65 66 import org.apache.commons.collections.Predicate; 67 import org.apache.log4j.Logger; 68 import org.objectstyle.cayenne.conf.Configuration; 69 70 75 public class WebApplicationResourceLocator extends ResourceLocator { 76 77 private static Logger logObj; 78 79 static { 83 Predicate p = new Predicate() { 84 85 public boolean evaluate(Object o) { 86 return Configuration.isLoggingConfigured(); 87 } 88 }; 89 90 logObj = new PredicateLogger(WebApplicationResourceLocator.class, p); 91 } 92 93 protected ServletContext context; 94 protected List additionalContextPaths; 95 96 99 public WebApplicationResourceLocator() { 100 this.additionalContextPaths = new ArrayList (); 101 this.addFilesystemPath("/WEB-INF/"); 102 } 103 104 108 public WebApplicationResourceLocator(ServletContext context) { 109 this(); 110 setServletContext(context); 111 } 112 113 116 public void setServletContext(ServletContext servletContext) { 117 this.context = servletContext; 118 } 119 120 123 public ServletContext getServletContext() { 124 return this.context; 125 } 126 127 131 public URL findResource(String location) { 132 if (!additionalContextPaths.isEmpty() && getServletContext() != null) { 133 134 Iterator cpi = this.additionalContextPaths.iterator(); 135 while (cpi.hasNext()) { 136 String fullName = cpi.next() + "/" + location; 137 logObj.debug("searching for: " + fullName); 138 try { 139 URL url = getServletContext().getResource(fullName); 140 if (url != null) { 141 return url; 142 } 143 } 144 catch (MalformedURLException ex) { 145 logObj.debug("Malformed URL, ignoring.", ex); 147 } 148 } 149 } 150 151 return super.findResource(location); 152 } 153 154 158 public void addFilesystemPath(String path) { 159 if (path != null) { 160 if (path.startsWith("/WEB-INF/")) { 161 this.additionalContextPaths.add(path); 162 } 163 else { 164 super.addFilesystemPath(path); 165 } 166 } 167 else { 168 throw new IllegalArgumentException ("Path must not be null."); 169 } 170 } 171 } | Popular Tags |