KickJava   Java API By Example, From Geeks To Geeks.

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


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.ClasspathResource;
24 import org.apache.tapestry.IRequestCycle;
25 import org.apache.tapestry.Tapestry;
26 import org.apache.tapestry.engine.IEngineService;
27 import org.apache.tapestry.engine.ILink;
28
29 /**
30  * An implementation of {@link org.apache.tapestry.IAsset}for localizable assets within the JVM's
31  * classpath.
32  * <p>
33  * The localization code here is largely cut-and-paste from {@link ContextAsset}.
34  *
35  * @author Howard Ship
36  */

37
38 public class PrivateAsset extends AbstractAsset
39 {
40     private IEngineService _assetService;
41
42     /**
43      * @deprecated To be removed (someday). Use
44      * {@link #PrivateAsset(ClasspathResource, IEngineService, Location)}&nbsp;instead.
45      */

46     public PrivateAsset(ClasspathResource resourceLocation, Location location)
47     {
48         this(resourceLocation, null, location);
49     }
50
51     public PrivateAsset(ClasspathResource resourceLocation, IEngineService assetService,
52             Location location)
53     {
54         super(resourceLocation, location);
55
56         _assetService = assetService;
57     }
58
59     /**
60      * Gets the localized version of the resource. Build the URL for the resource. If possible, the
61      * application's {@link AssetExternalizerImpl}is located, to copy the resource to a directory
62      * visible to the web server.
63      */

64
65     public String JavaDoc buildURL(IRequestCycle cycle)
66     {
67         String JavaDoc path = getResourceLocation().getPath();
68
69         // ClasspathAssetFactory will provide the asset service as a constructor
70
// parameter, but there are a few odd uses of PrivateAsset where this
71
// is not handy.
72

73         if (_assetService == null)
74             _assetService = cycle.getEngine().getService(Tapestry.ASSET_SERVICE);
75
76         ILink link = _assetService.getLink(cycle, path);
77
78         return link.getURL();
79     }
80
81     public InputStream JavaDoc getResourceAsStream(IRequestCycle cycle)
82     {
83         Resource location = getResourceLocation();
84
85         try
86         {
87             URL JavaDoc url = location.getResourceURL();
88
89             return url.openStream();
90         }
91         catch (Exception JavaDoc ex)
92         {
93             throw new ApplicationRuntimeException(Tapestry.format(
94                     "PrivateAsset.resource-missing",
95                     location), ex);
96         }
97     }
98
99 }
Popular Tags