01: // Copyright 2006, 2007 The Apache Software Foundation
02: //
03: // Licensed under the Apache License, Version 2.0 (the "License");
04: // you may not use this file except in compliance with the License.
05: // You may obtain a copy of the License at
06: //
07: // http://www.apache.org/licenses/LICENSE-2.0
08: //
09: // 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.internal.services;
16:
17: import org.apache.tapestry.Asset;
18: import org.apache.tapestry.ioc.Resource;
19: import org.apache.tapestry.services.AssetFactory;
20: import org.apache.tapestry.services.Context;
21: import org.apache.tapestry.services.Request;
22:
23: /**
24: * Implementation of {@link AssetFactory} for assets that are part of the web application context.
25: *
26: * @see ContextResource
27: */
28: public class ContextAssetFactory implements AssetFactory {
29: private final Request _request;
30:
31: private final Context _context;
32:
33: public ContextAssetFactory(Request request, Context context) {
34: _request = request;
35: _context = context;
36: }
37:
38: public Asset createAsset(final Resource resource) {
39: final String contextPath = _request.getContextPath() + "/"
40: + resource.getPath();
41:
42: return new Asset() {
43: public Resource getResource() {
44: return resource;
45: }
46:
47: public String toClientURL() {
48: return contextPath;
49: }
50:
51: /**
52: * Returns the client URL, which is essiential to allow informal parameters of type
53: * Asset to generate a proper value.
54: */
55: @Override
56: public String toString() {
57: return toClientURL();
58: }
59: };
60: }
61:
62: /** Returns the root {@link ContextResource}. */
63: public Resource getRootResource() {
64: return new ContextResource(_context, "/");
65: }
66:
67: }
|