1 15 package org.apache.hivemind.util; 16 17 import java.net.MalformedURLException ; 18 import java.net.URL ; 19 import java.util.Locale ; 20 21 import javax.servlet.ServletContext ; 22 23 import org.apache.commons.logging.Log; 24 import org.apache.commons.logging.LogFactory; 25 import org.apache.hivemind.Resource; 26 27 36 37 public class ContextResource extends AbstractResource 38 { 39 private static final Log LOG = LogFactory.getLog(ContextResource.class); 40 41 private ServletContext _context; 42 43 public ContextResource(ServletContext context, String path) 44 { 45 this(context, path, null); 46 } 47 48 public ContextResource(ServletContext context, String path, Locale locale) 49 { 50 super(path, locale); 51 52 _context = context; 53 } 54 55 59 60 public Resource getLocalization(Locale locale) 61 { 62 LocalizedContextResourceFinder finder = new LocalizedContextResourceFinder(_context); 63 64 String path = getPath(); 65 LocalizedResource localizedResource = finder.resolve(path, locale); 66 67 if (localizedResource == null) 68 return null; 69 70 String localizedPath = localizedResource.getResourcePath(); 71 Locale pathLocale = localizedResource.getResourceLocale(); 72 73 if (localizedPath == null) 74 return null; 75 76 if (path.equals(localizedPath)) 77 return this; 78 79 return new ContextResource(_context, localizedPath, pathLocale); 80 } 81 82 public URL getResourceURL() 83 { 84 try 85 { 86 return _context.getResource(getPath()); 87 } 88 catch (MalformedURLException ex) 89 { 90 LOG.warn(UtilMessages.unableToReferenceContextPath(getPath(), ex), ex); 91 92 return null; 93 } 94 } 95 96 public String toString() 97 { 98 return "context:" + getPath(); 99 } 100 101 public int hashCode() 102 { 103 return 4197 & getPath().hashCode(); 104 } 105 106 protected Resource newResource(String path) 107 { 108 return new ContextResource(_context, path); 109 } 110 111 } | Popular Tags |