KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > tapestry > asset > ContextAsset


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 JavaDoc;
18 import java.net.URL JavaDoc;
19
20 import org.apache.hivemind.ApplicationRuntimeException;
21 import org.apache.hivemind.Location;
22 import org.apache.hivemind.Resource;
23 import org.apache.hivemind.util.Defense;
24 import org.apache.tapestry.IAsset;
25 import org.apache.tapestry.IRequestCycle;
26 import org.apache.tapestry.Tapestry;
27
28 /**
29  * An asset whose path is relative to the {@link javax.servlet.ServletContext}containing the
30  * application.
31  *
32  * @author Howard Lewis Ship
33  */

34
35 public class ContextAsset extends AbstractAsset implements IAsset
36 {
37     private String JavaDoc _contextPath;
38
39     private String JavaDoc _resolvedURL;
40
41     public ContextAsset(String JavaDoc contextPath, Resource resource, Location location)
42     {
43         super(resource, location);
44
45         Defense.notNull(contextPath, "contextPath");
46
47         _contextPath = contextPath;
48     }
49
50     /**
51      * Generates a URL for the client to retrieve the asset. The context path is prepended to the
52      * asset path, which means that assets deployed inside web applications will still work (if
53      * things are configured properly).
54      */

55
56     public String JavaDoc buildURL(IRequestCycle cycle)
57     {
58         if (_resolvedURL == null)
59             _resolvedURL = _contextPath + getResourceLocation().getPath();
60
61         return _resolvedURL;
62     }
63
64     public InputStream JavaDoc getResourceAsStream(IRequestCycle cycle)
65     {
66         try
67         {
68             URL JavaDoc url = getResourceLocation().getResourceURL();
69
70             return url.openStream();
71         }
72         catch (Exception JavaDoc ex)
73         {
74             throw new ApplicationRuntimeException(Tapestry.format(
75                     "ContextAsset.resource-missing",
76                     getResourceLocation()), ex);
77         }
78     }
79 }
Popular Tags