KickJava   Java API By Example, From Geeks To Geeks.

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


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.tapestry.IRequestCycle;
23 import org.apache.tapestry.Tapestry;
24
25 /**
26  * A reference to an external URL. {@link ExternalAsset}s are not
27  * localizable.
28  *
29  * @author Howard Lewis Ship
30  *
31  **/

32
33 public class ExternalAsset extends AbstractAsset
34 {
35     private String JavaDoc _URL;
36
37     public ExternalAsset(String JavaDoc URL, Location location)
38     {
39         super(null, location);
40         
41         _URL = URL;
42     }
43
44     /**
45      * Simply returns the URL of the external asset.
46      *
47      **/

48
49     public String JavaDoc buildURL(IRequestCycle cycle)
50     {
51         return _URL;
52     }
53
54     public InputStream JavaDoc getResourceAsStream(IRequestCycle cycle)
55     {
56         URL JavaDoc url;
57
58         try
59         {
60             url = new URL JavaDoc(_URL);
61
62             return url.openStream();
63         }
64         catch (Exception JavaDoc ex)
65         {
66             // MalrformedURLException or IOException
67

68             throw new ApplicationRuntimeException(Tapestry.format("ExternalAsset.resource-missing", _URL), ex);
69         }
70
71     }
72
73     public String JavaDoc toString()
74     {
75         return "ExternalAsset[" + _URL + "]";
76     }
77 }
Popular Tags