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: private static final Log LOG = LogFactory
039: .getLog(ContextResource.class);
040:
041: private ServletContext _context;
042:
043: public ContextResource(ServletContext context, String path) {
044: this (context, path, null);
045: }
046:
047: public ContextResource(ServletContext context, String path,
048: Locale locale) {
049: super (path, locale);
050:
051: _context = context;
052: }
053:
054: /**
055: * Locates the resource using {@link LocalizedContextResourceFinder} and
056: * {@link ServletContext#getResource(java.lang.String)}.
057: */
058:
059: public Resource getLocalization(Locale locale) {
060: LocalizedContextResourceFinder finder = new LocalizedContextResourceFinder(
061: _context);
062:
063: String path = getPath();
064: LocalizedResource localizedResource = finder.resolve(path,
065: 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: try {
084: return _context.getResource(getPath());
085: } catch (MalformedURLException ex) {
086: LOG.warn(UtilMessages.unableToReferenceContextPath(
087: getPath(), ex), ex);
088:
089: return null;
090: }
091: }
092:
093: public String toString() {
094: return "context:" + getPath();
095: }
096:
097: public int hashCode() {
098: return 4197 & getPath().hashCode();
099: }
100:
101: protected Resource newResource(String path) {
102: return new ContextResource(_context, path);
103: }
104:
105: }
|