|
|||||||||||||||||||
30 day Evaluation Version distributed via the Maven Jar Repository. Clover is not free. You have 30 days to evaluate it. Please visit http://www.thecortex.net/clover to obtain a licensed version of Clover | |||||||||||||||||||
Source file | Conditionals | Statements | Methods | TOTAL | |||||||||||||||
ContextResource.java | 83.3% | 81.8% | 85.7% | 82.9% |
|
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;
|
|
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 |
/**
|
|
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 _context;
|
|
42 |
|
|
43 | 9 |
public ContextResource(ServletContext context, String path)
|
44 |
{ |
|
45 | 9 |
this(context, path, null); |
46 |
} |
|
47 |
|
|
48 | 11 |
public ContextResource(ServletContext context, String path, Locale locale)
|
49 |
{ |
|
50 | 11 |
super(path, locale);
|
51 |
|
|
52 | 11 |
_context = context; |
53 |
} |
|
54 |
|
|
55 |
/**
|
|
56 |
* Locates the resource using {@link LocalizedContextResourceFinder}and
|
|
57 |
* {@link ServletContext#getResource(java.lang.String)}.
|
|
58 |
*/
|
|
59 |
|
|
60 | 3 |
public Resource getLocalization(Locale locale)
|
61 |
{ |
|
62 | 3 |
LocalizedContextResourceFinder finder = new LocalizedContextResourceFinder(_context);
|
63 |
|
|
64 | 3 |
String path = getPath(); |
65 | 3 |
LocalizedResource localizedResource = finder.resolve(path, locale); |
66 |
|
|
67 | 3 |
if (localizedResource == null) |
68 | 1 |
return null; |
69 |
|
|
70 | 2 |
String localizedPath = localizedResource.getResourcePath(); |
71 | 2 |
Locale pathLocale = localizedResource.getResourceLocale(); |
72 |
|
|
73 | 2 |
if (localizedPath == null) |
74 | 0 |
return null; |
75 |
|
|
76 | 2 |
if (path.equals(localizedPath))
|
77 | 1 |
return this; |
78 |
|
|
79 | 1 |
return new ContextResource(_context, localizedPath, pathLocale); |
80 |
} |
|
81 |
|
|
82 | 5 |
public URL getResourceURL()
|
83 |
{ |
|
84 | 5 |
try
|
85 |
{ |
|
86 | 5 |
return _context.getResource(getPath());
|
87 |
} |
|
88 |
catch (MalformedURLException ex)
|
|
89 |
{ |
|
90 | 0 |
LOG.warn(UtilMessages.unableToReferenceContextPath(getPath(), ex), ex); |
91 |
|
|
92 | 0 |
return null; |
93 |
} |
|
94 |
} |
|
95 |
|
|
96 | 1 |
public String toString()
|
97 |
{ |
|
98 | 1 |
return "context:" + getPath(); |
99 |
} |
|
100 |
|
|
101 | 0 |
public int hashCode() |
102 |
{ |
|
103 | 0 |
return 4197 & getPath().hashCode();
|
104 |
} |
|
105 |
|
|
106 | 1 |
protected Resource newResource(String path)
|
107 |
{ |
|
108 | 1 |
return new ContextResource(_context, path); |
109 |
} |
|
110 |
|
|
111 |
} |
|