01: /*
02: * Copyright 2005-2006 the original author or authors.
03: *
04: * Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except
05: * in compliance with the License. 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 distributed under the License
10: * is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express
11: * or implied. See the License for the specific language governing permissions and limitations under
12: * the License.
13: */
14:
15: package org.strecks.injection.internal;
16:
17: import java.beans.PropertyDescriptor;
18: import java.util.ArrayList;
19: import java.util.List;
20:
21: import org.strecks.context.ActionContext;
22: import org.strecks.injection.handler.InjectionHandler;
23:
24: /**
25: * Wraps a set of <code>InjectionHandler</code> instances which, using annotations, have been
26: * associated with the target class's setter method. More than one injection handler can be assigned
27: * to a method. In this case, all of the injection handlers will be called until one returns a
28: * non-null object.
29: *
30: * @author Phil Zoio
31: */
32: public class InjectionWrapper {
33:
34: /**
35: * Contains the list of <code>InjectionHandler</code> instances which have been associated
36: * with a bean property using annotations
37: */
38: private List<InjectionHandler> injectionHandlers = new ArrayList<InjectionHandler>();
39:
40: /**
41: * Class responsible for binding the value returned from one of the
42: * <code>InjectionHandler</code>s to the target property
43: */
44: private InjectionSetter injectionSetter;
45:
46: private PropertyDescriptor propertyDescriptor;
47:
48: public InjectionWrapper(InjectionSetter inputSetter,
49: PropertyDescriptor propertyDescriptor) {
50: this .injectionSetter = inputSetter;
51: this .propertyDescriptor = propertyDescriptor;
52: }
53:
54: /**
55: * Retrieves the required object from the request using the configured input handling strategy,
56: * then applies it to the target object
57: *
58: * @return true if object set was not null, false otherwise
59: */
60: public boolean inject(Object action, ActionContext injectionContext) {
61:
62: Object result = null;
63: for (InjectionHandler inputHandler : injectionHandlers) {
64: result = inputHandler.getValue(injectionContext);
65: if (result != null)
66: break;
67: }
68: injectionSetter.setInput(action, result);
69: return (result != null);
70: }
71:
72: public InjectionSetter getInjectionSetter() {
73: return injectionSetter;
74: }
75:
76: public void setInjectionSetter(InjectionSetter inputSetter) {
77: this .injectionSetter = inputSetter;
78: }
79:
80: public PropertyDescriptor getPropertyDescriptor() {
81: return propertyDescriptor;
82: }
83:
84: public void addHandler(InjectionHandler handler) {
85: injectionHandlers.add(handler);
86: }
87:
88: protected List<InjectionHandler> getInjectionHandlers() {
89: return injectionHandlers;
90: }
91:
92: protected void setInjectionHandlers(
93: List<InjectionHandler> inputHandlers) {
94: this.injectionHandlers = inputHandlers;
95: }
96:
97: }
|