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.annotations.Inject;
18: import org.apache.tapestry.ioc.ObjectLocator;
19: import org.apache.tapestry.model.MutableComponentModel;
20: import org.apache.tapestry.services.ClassTransformation;
21: import org.apache.tapestry.services.ComponentClassTransformWorker;
22: import org.apache.tapestry.services.InjectionProvider;
23:
24: /**
25: * Performs injection of resources, for the cases where a field is labled with the {@link Inject}
26: * annotation, but no specific value was provided. This worker must be scheduled <em>before</em>
27: * {@link InjectWorker}.
28: * <p>
29: * The implementation of this worker mostly delegates to a chain of command of
30: * {@link InjectionProvider}s.
31: */
32: public class InjectResourcesWorker implements
33: ComponentClassTransformWorker {
34: private final ObjectLocator _locator;
35:
36: // Really, a chain of command
37:
38: private final InjectionProvider _injectionProvider;
39:
40: public InjectResourcesWorker(final ObjectLocator locator,
41: final InjectionProvider injectionProvider) {
42: _locator = locator;
43: _injectionProvider = injectionProvider;
44: }
45:
46: public void transform(ClassTransformation transformation,
47: MutableComponentModel model) {
48: for (String fieldName : transformation
49: .findFieldsWithAnnotation(Inject.class)) {
50: Inject annotation = transformation.getFieldAnnotation(
51: fieldName, Inject.class);
52:
53: String fieldType = transformation.getFieldType(fieldName);
54:
55: boolean result = _injectionProvider.provideInjection(
56: fieldName, fieldType, _locator, transformation,
57: model);
58:
59: // If true, claim the field; otherwise ignore it (it will be handled by a later
60: // worker and an exception will be thrown if it ultimately can't be satisfied.
61:
62: if (result)
63: transformation.claimField(fieldName, annotation);
64: }
65: }
66:
67: }
|