Classes in this File | Line Coverage | Branch Coverage | Complexity | ||||
PrivateAsset |
|
| 1.75;1.75 |
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 org.apache.hivemind.ApplicationRuntimeException; | |
18 | import org.apache.hivemind.Location; | |
19 | import org.apache.hivemind.Resource; | |
20 | import org.apache.hivemind.util.Defense; | |
21 | import org.apache.tapestry.engine.IEngineService; | |
22 | import org.apache.tapestry.engine.ILink; | |
23 | ||
24 | import java.io.InputStream; | |
25 | import java.net.URL; | |
26 | ||
27 | /** | |
28 | * An implementation of {@link org.apache.tapestry.IAsset} for localizable assets within the JVM's | |
29 | * classpath. | |
30 | * <p> | |
31 | * The localization code here is largely cut-and-paste from {@link ContextAsset}. | |
32 | * | |
33 | * @author Howard Ship | |
34 | */ | |
35 | ||
36 | public class PrivateAsset extends AbstractAsset | |
37 | { | |
38 | private IEngineService _assetService; | |
39 | ||
40 | /** | |
41 | * @deprecated To be removed (someday). Use | |
42 | * {@link #PrivateAsset(Resource, IEngineService, Location)} instead. | |
43 | */ | |
44 | public PrivateAsset(Resource resourceLocation, Location location) | |
45 | { | |
46 | 0 | this(resourceLocation, null, location); |
47 | 0 | } |
48 | ||
49 | public PrivateAsset(Resource resourceLocation, IEngineService assetService, | |
50 | Location location) | |
51 | { | |
52 | 0 | super(resourceLocation, location); |
53 | ||
54 | 0 | Defense.notNull(assetService, "assetService"); |
55 | ||
56 | 0 | _assetService = assetService; |
57 | 0 | } |
58 | ||
59 | /** | |
60 | * Gets the localized version of the resource. Build the URL for the resource. If possible, the | |
61 | * application's {@link ExternalAsset}is located, to copy the resource to a directory | |
62 | * visible to the web server. | |
63 | */ | |
64 | ||
65 | public String buildURL() | |
66 | { | |
67 | 0 | String path = getResourceLocation().getPath(); |
68 | ||
69 | 0 | ILink link = _assetService.getLink(false, path); |
70 | ||
71 | 0 | return link.getURL(); |
72 | } | |
73 | ||
74 | public InputStream getResourceAsStream() | |
75 | { | |
76 | 0 | Resource location = getResourceLocation(); |
77 | ||
78 | try | |
79 | { | |
80 | 0 | URL url = location.getResourceURL(); |
81 | ||
82 | 0 | return url.openStream(); |
83 | } | |
84 | 0 | catch (Exception ex) |
85 | { | |
86 | 0 | throw new ApplicationRuntimeException(AssetMessages.noSuchResource(location.getPath())); |
87 | } | |
88 | } | |
89 | ||
90 | } |