|
|||||||||||||||||||
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 | |||||||||||||||
ContextAsset.java | 100% | 90% | 100% | 93.3% |
|
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.tapestry.asset;
|
|
16 |
|
|
17 |
import java.io.InputStream;
|
|
18 |
import java.net.URL;
|
|
19 |
|
|
20 |
import org.apache.hivemind.ApplicationRuntimeException;
|
|
21 |
import org.apache.hivemind.Location;
|
|
22 |
import org.apache.hivemind.util.ContextResource;
|
|
23 |
import org.apache.hivemind.util.Defense;
|
|
24 |
import org.apache.tapestry.IAsset;
|
|
25 |
import org.apache.tapestry.IEngine;
|
|
26 |
import org.apache.tapestry.IRequestCycle;
|
|
27 |
import org.apache.tapestry.Tapestry;
|
|
28 |
|
|
29 |
/**
|
|
30 |
* An asset whose path is relative to the {@link javax.servlet.ServletContext}containing the
|
|
31 |
* application.
|
|
32 |
*
|
|
33 |
* @author Howard Lewis Ship
|
|
34 |
*/
|
|
35 |
|
|
36 |
public class ContextAsset extends AbstractAsset implements IAsset |
|
37 |
{ |
|
38 |
private String _contextPath;
|
|
39 |
|
|
40 |
private String _resolvedURL;
|
|
41 |
|
|
42 | 6 |
public ContextAsset(String contextPath, ContextResource resourceLocation, Location location)
|
43 |
{ |
|
44 | 6 |
super(resourceLocation, location);
|
45 |
|
|
46 | 6 |
Defense.notNull(contextPath, "contextPath");
|
47 |
|
|
48 | 6 |
_contextPath = contextPath; |
49 |
} |
|
50 |
|
|
51 |
/**
|
|
52 |
* Generates a URL for the client to retrieve the asset. The context path is prepended to the
|
|
53 |
* asset path, which means that assets deployed inside web applications will still work (if
|
|
54 |
* things are configured properly).
|
|
55 |
*/
|
|
56 |
|
|
57 | 6 |
public String buildURL(IRequestCycle cycle)
|
58 |
{ |
|
59 | 6 |
if (_resolvedURL == null) |
60 | 5 |
_resolvedURL = _contextPath + getResourceLocation().getPath(); |
61 |
|
|
62 | 6 |
return _resolvedURL;
|
63 |
} |
|
64 |
|
|
65 | 1 |
public InputStream getResourceAsStream(IRequestCycle cycle)
|
66 |
{ |
|
67 | 1 |
try
|
68 |
{ |
|
69 | 1 |
URL url = getResourceLocation().getResourceURL(); |
70 |
|
|
71 | 1 |
return url.openStream();
|
72 |
} |
|
73 |
catch (Exception ex)
|
|
74 |
{ |
|
75 | 0 |
throw new ApplicationRuntimeException(Tapestry.format( |
76 |
"ContextAsset.resource-missing",
|
|
77 |
getResourceLocation()), ex); |
|
78 |
} |
|
79 |
} |
|
80 |
} |
|