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 java.lang.annotation.Annotation;
18:
19: import org.apache.tapestry.annotations.Inject;
20: import org.apache.tapestry.ioc.AnnotationProvider;
21: import org.apache.tapestry.ioc.ObjectProvider;
22: import org.apache.tapestry.ioc.ObjectLocator;
23: import org.apache.tapestry.model.MutableComponentModel;
24: import org.apache.tapestry.services.ClassTransformation;
25: import org.apache.tapestry.services.ComponentClassTransformWorker;
26:
27: /**
28: * Worker for the {@link org.apache.tapestry.annotations.Inject} annotation that delegates out to
29: * the master {@link ObjectProvider} to access the value. This worker must be scheduled after
30: * certain other workers, such as {@link InjectBlockWorker} (which is keyed off a combination of
31: * type and the Inject annotation).
32: *
33: * @see ObjectProvider
34: */
35: public class InjectWorker implements ComponentClassTransformWorker {
36: private final ObjectProvider _objectProvider;
37:
38: private final ObjectLocator _locator;
39:
40: public InjectWorker(ObjectProvider objectProvider,
41: ObjectLocator locator) {
42: _objectProvider = objectProvider;
43: _locator = locator;
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: inject(fieldName, transformation, model);
54:
55: transformation.claimField(fieldName, annotation);
56: }
57:
58: }
59:
60: @SuppressWarnings("unchecked")
61: private void inject(final String fieldName,
62: final ClassTransformation transformation,
63: MutableComponentModel model) {
64: String fieldType = transformation.getFieldType(fieldName);
65:
66: Class type = transformation.toClass(fieldType);
67:
68: AnnotationProvider annotationProvider = new AnnotationProvider() {
69: public <T extends Annotation> T getAnnotation(
70: Class<T> annotationClass) {
71: return transformation.getFieldAnnotation(fieldName,
72: annotationClass);
73: }
74: };
75:
76: Object inject = null;
77:
78: try {
79: inject = _objectProvider.provide(type, annotationProvider,
80: _locator);
81: } catch (Exception ex) {
82: throw new RuntimeException(ServicesMessages
83: .fieldInjectionError(transformation.getClassName(),
84: fieldName, ex), ex);
85: }
86:
87: transformation.injectField(fieldName, inject);
88: }
89:
90: }
|