KickJava   Java API By Example, From Geeks To Geeks.

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


1 // Copyright 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.util.Locale JavaDoc;
18
19 import org.apache.hivemind.ApplicationRuntimeException;
20 import org.apache.hivemind.Location;
21 import org.apache.hivemind.Resource;
22 import org.apache.tapestry.IAsset;
23 import org.apache.tapestry.web.WebContext;
24 import org.apache.tapestry.web.WebContextResource;
25
26 /**
27  * For the moment, all "context:" prefixed asset paths are interpreted relative to the web context
28  * (the web application's root folder).
29  *
30  * @author Howard M. Lewis Ship
31  * @since 4.0
32  */

33 public class ContextAssetFactory implements AssetFactory
34 {
35     private WebContext _context;
36
37     private String JavaDoc _contextPath;
38
39     private Resource _servletRoot;
40
41     public void initializeService()
42     {
43         _servletRoot = new WebContextResource(_context, "/");
44     }
45
46     public IAsset createAsset(Resource baseResource, String JavaDoc path, Locale JavaDoc locale, Location location)
47     {
48         Resource assetResource = _servletRoot.getRelativeResource(path);
49         Resource localized = assetResource.getLocalization(locale);
50
51         if (localized == null)
52             throw new ApplicationRuntimeException(AssetMessages.missingAsset(path, _servletRoot),
53                     location, null);
54
55         return new ContextAsset(_contextPath, localized, location);
56     }
57
58     public IAsset createAsset(Resource resource, Location location)
59     {
60         return new ContextAsset(resource.getPath(), resource, location);
61     }
62
63     public void setContext(WebContext context)
64     {
65         _context = context;
66     }
67
68     public void setContextPath(String JavaDoc contextPath)
69     {
70         _contextPath = contextPath;
71     }
72 }
Popular Tags