KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > hivemind > util > ContextResource


1 // Copyright 2004, 2005 The Apache Software Foundation
2
//
3
// Licensed under the Apache License, Version 2.0 (the "License");
4
// you may not use this file except in compliance with the License.
5
// You may obtain a copy of the License at
6
//
7
// http://www.apache.org/licenses/LICENSE-2.0
8
//
9
// Unless required by applicable law or agreed to in writing, software
10
// distributed under the License is distributed on an "AS IS" BASIS,
11
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12
// See the License for the specific language governing permissions and
13
// limitations under the License.
14

15 package org.apache.hivemind.util;
16
17 import java.net.MalformedURLException JavaDoc;
18 import java.net.URL JavaDoc;
19 import java.util.Locale JavaDoc;
20
21 import javax.servlet.ServletContext JavaDoc;
22
23 import org.apache.commons.logging.Log;
24 import org.apache.commons.logging.LogFactory;
25 import org.apache.hivemind.Resource;
26
27 /**
28  * Implementation of {@link org.apache.hivemind.Resource} for resources found within the web
29  * application context.
30  * <p>
31  * Note: moved from Tapestry. Originally part of Tapestry 3.0.
32  *
33  * @author Howard Lewis Ship
34  * @since 1.1
35  */

36
37 public class ContextResource extends AbstractResource
38 {
39     private static final Log LOG = LogFactory.getLog(ContextResource.class);
40
41     private ServletContext JavaDoc _context;
42
43     public ContextResource(ServletContext JavaDoc context, String JavaDoc path)
44     {
45         this(context, path, null);
46     }
47
48     public ContextResource(ServletContext JavaDoc context, String JavaDoc path, Locale JavaDoc locale)
49     {
50         super(path, locale);
51
52         _context = context;
53     }
54
55     /**
56      * Locates the resource using {@link LocalizedContextResourceFinder} and
57      * {@link ServletContext#getResource(java.lang.String)}.
58      */

59
60     public Resource getLocalization(Locale JavaDoc locale)
61     {
62         LocalizedContextResourceFinder finder = new LocalizedContextResourceFinder(_context);
63
64         String JavaDoc path = getPath();
65         LocalizedResource localizedResource = finder.resolve(path, locale);
66
67         if (localizedResource == null)
68             return null;
69
70         String JavaDoc localizedPath = localizedResource.getResourcePath();
71         Locale JavaDoc 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 JavaDoc getResourceURL()
83     {
84         try
85         {
86             return _context.getResource(getPath());
87         }
88         catch (MalformedURLException JavaDoc ex)
89         {
90             LOG.warn(UtilMessages.unableToReferenceContextPath(getPath(), ex), ex);
91
92             return null;
93         }
94     }
95
96     public String JavaDoc 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 JavaDoc path)
107     {
108         return new ContextResource(_context, path);
109     }
110
111 }
Popular Tags