001 // Copyright 2004, 2005 The Apache Software Foundation 002 // 003 // Licensed under the Apache License, Version 2.0 (the "License"); 004 // you may not use this file except in compliance with the License. 005 // You may obtain a copy of the License at 006 // 007 // http://www.apache.org/licenses/LICENSE-2.0 008 // 009 // Unless required by applicable law or agreed to in writing, software 010 // distributed under the License is distributed on an "AS IS" BASIS, 011 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 012 // See the License for the specific language governing permissions and 013 // limitations under the License. 014 015 package org.apache.hivemind.util; 016 017 import java.net.MalformedURLException; 018 import java.net.URL; 019 import java.util.Locale; 020 021 import javax.servlet.ServletContext; 022 023 import org.apache.commons.logging.Log; 024 import org.apache.commons.logging.LogFactory; 025 import org.apache.hivemind.Resource; 026 027 /** 028 * Implementation of {@link org.apache.hivemind.Resource} for resources found within the web 029 * application context. 030 * <p> 031 * Note: moved from Tapestry. Originally part of Tapestry 3.0. 032 * 033 * @author Howard Lewis Ship 034 * @since 1.1 035 */ 036 037 public class ContextResource extends AbstractResource 038 { 039 private static final Log LOG = LogFactory.getLog(ContextResource.class); 040 041 private ServletContext _context; 042 043 public ContextResource(ServletContext context, String path) 044 { 045 this(context, path, null); 046 } 047 048 public ContextResource(ServletContext context, String path, Locale locale) 049 { 050 super(path, locale); 051 052 _context = context; 053 } 054 055 /** 056 * Locates the resource using {@link LocalizedContextResourceFinder} and 057 * {@link ServletContext#getResource(java.lang.String)}. 058 */ 059 060 public Resource getLocalization(Locale locale) 061 { 062 LocalizedContextResourceFinder finder = new LocalizedContextResourceFinder(_context); 063 064 String path = getPath(); 065 LocalizedResource localizedResource = finder.resolve(path, locale); 066 067 if (localizedResource == null) 068 return null; 069 070 String localizedPath = localizedResource.getResourcePath(); 071 Locale pathLocale = localizedResource.getResourceLocale(); 072 073 if (localizedPath == null) 074 return null; 075 076 if (path.equals(localizedPath)) 077 return this; 078 079 return new ContextResource(_context, localizedPath, pathLocale); 080 } 081 082 public URL getResourceURL() 083 { 084 try 085 { 086 return _context.getResource(getPath()); 087 } 088 catch (MalformedURLException ex) 089 { 090 LOG.warn(UtilMessages.unableToReferenceContextPath(getPath(), ex), ex); 091 092 return null; 093 } 094 } 095 096 public String toString() 097 { 098 return "context:" + getPath(); 099 } 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 }