KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > tapestry > web > WebContextResource


1 // Copyright 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.tapestry.web;
16
17 import java.net.URL JavaDoc;
18 import java.util.Locale JavaDoc;
19
20 import javax.servlet.ServletContext JavaDoc;
21
22 import org.apache.commons.logging.Log;
23 import org.apache.commons.logging.LogFactory;
24 import org.apache.hivemind.Resource;
25 import org.apache.hivemind.util.AbstractResource;
26 import org.apache.hivemind.util.LocalizedResource;
27
28 /**
29  * Implementation of {@link org.apache.hivemind.Resource}for resources found within a
30  * {@link org.apache.tapestry.web.WebContext}.
31  *
32  * @author Howard Lewis Ship
33  * @since 4.0
34  */

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

58
59     public Resource getLocalization(Locale JavaDoc locale)
60     {
61         LocalizedWebContextResourceFinder finder = new LocalizedWebContextResourceFinder(
62                 _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 WebContextResource(_context, localizedPath, pathLocale);
80     }
81
82     public URL JavaDoc getResourceURL()
83     {
84         return _context.getResource(getPath());
85     }
86
87     public String JavaDoc toString()
88     {
89         return "context:" + getPath();
90     }
91
92     public int hashCode()
93     {
94         return 2387 & getPath().hashCode();
95     }
96
97     protected Resource newResource(String JavaDoc path)
98     {
99         return new WebContextResource(_context, path);
100     }
101
102 }
Popular Tags