01: // Copyright 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.annotations.Path;
19: import org.apache.tapestry.ioc.AnnotationProvider;
20: import org.apache.tapestry.ioc.ObjectProvider;
21: import org.apache.tapestry.ioc.Resource;
22: import org.apache.tapestry.ioc.ObjectLocator;
23: import org.apache.tapestry.ioc.annotations.InjectService;
24: import org.apache.tapestry.ioc.services.SymbolSource;
25: import org.apache.tapestry.ioc.services.TypeCoercer;
26: import org.apache.tapestry.services.AssetSource;
27:
28: /**
29: * Exposes assets (in the current locale). The Inject annotation must be supplemented by a
30: * {@link Path} annotation, to identify what asset to be injected.
31: */
32: public class AssetObjectProvider implements ObjectProvider {
33: private final AssetSource _source;
34:
35: private final TypeCoercer _typeCoercer;
36:
37: private final SymbolSource _symbolSource;
38:
39: public AssetObjectProvider(@InjectService("AssetSource")
40: AssetSource source,
41:
42: @InjectService("TypeCoercer")
43: TypeCoercer typeCoercer,
44:
45: @InjectService("SymbolSource")
46: SymbolSource symbolSource) {
47: _source = source;
48: _typeCoercer = typeCoercer;
49: _symbolSource = symbolSource;
50: }
51:
52: /**
53: * Provides the asset. If the expression does not identify an asset domain, with a prefix, it is
54: * assumed to be a path on the classpath, relative to the root of the classpath.
55: *
56: * @param expression
57: * expression used to find the asset, passed to
58: * {@link AssetSource#findAsset(Resource, String, java.util.Locale)
59: * @param objectType
60: * the type of object (which must be Object or Asset)
61: * @param locator
62: * not used
63: */
64: public <T> T provide(Class<T> objectType,
65: AnnotationProvider annotationProvider, ObjectLocator locator) {
66: Path path = annotationProvider.getAnnotation(Path.class);
67:
68: if (path == null)
69: return null;
70:
71: String expanded = _symbolSource.expandSymbols(path.value());
72:
73: Asset asset = _source.findAsset(null, expanded, null);
74:
75: return _typeCoercer.coerce(asset, objectType);
76: }
77: }
|