File D:\maven-qcr-plugin-testsunitaires\qcr4java\jenkins-master\target\site\qc\core\src\main\java\hudson\util\spring\DefaultBeanConfiguration.java.html

1 /*
2 * Copyright 2004-2005 the original author or authors.
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16 package hudson.util.spring;
17
18 import groovy.lang.GroovyObjectSupport;
19 import org.apache.commons.lang.StringUtils;
20 import org.springframework.beans.BeanWrapper;
21 import org.springframework.beans.BeanWrapperImpl;
22 import org.springframework.beans.factory.config.AutowireCapableBeanFactory;
23 import org.springframework.beans.factory.config.ConstructorArgumentValues;
24 import org.springframework.beans.factory.config.RuntimeBeanReference;
25 import org.springframework.beans.factory.support.AbstractBeanDefinition;
26 import org.springframework.beans.factory.support.ChildBeanDefinition;
27 import org.springframework.beans.factory.support.RootBeanDefinition;
28
29 import java.util.*;
30
31 /**
32 * Default implementation of the BeanConfiguration interface
33 *
34 * Credit must go to Solomon Duskis and the
35 * article: http://jroller.com/page/Solomon?entry=programmatic_configuration_in_spring
36 *
37 * @author Graeme
38 * @since 0.3
39 *
40 */
41 class DefaultBeanConfiguration extends GroovyObjectSupport implements BeanConfiguration {
42
43 private static final String AUTOWIRE = "autowire";
44 private static final String CONSTRUCTOR_ARGS = "constructorArgs";
45 private static final String DESTROY_METHOD = "destroyMethod";
46 private static final String FACTORY_BEAN = "factoryBean";
47 private static final String FACTORY_METHOD = "factoryMethod";
48 private static final String INIT_METHOD = "initMethod";
49 private static final String BY_NAME = "byName";
50 private static final String PARENT = "parent";
51 private static final String BY_TYPE = "byType";
52 private static final String BY_CONSTRUCTOR = "constructor";
53 private static final Set<String> DYNAMIC_PROPS = new HashSet<String>(Arrays.asList(AUTOWIRE, CONSTRUCTOR_ARGS, DESTROY_METHOD, FACTORY_BEAN, FACTORY_METHOD, INIT_METHOD, BY_NAME, BY_TYPE, BY_CONSTRUCTOR));
54 private String parentName;
55
56 @Override
57 public Object getProperty(String property) {
58 getBeanDefinition();
59 if(wrapper.isReadableProperty(property)) {
60 return wrapper.getPropertyValue(property);
61 }
62 else if(DYNAMIC_PROPS.contains(property)) {
63 return null;
64 }
65 return super.getProperty(property);
66 }
67
68 @Override
69 public void setProperty(String property, Object newValue) {
70 if(PARENT.equals(property)) {
71 setParent(newValue);
72 }
73 else {
74 AbstractBeanDefinition bd = getBeanDefinition();
75 if(AUTOWIRE.equals(property)) {
76 if(BY_NAME.equals(newValue)) {
77 bd.setAutowireMode(AutowireCapableBeanFactory.AUTOWIRE_BY_NAME);
78 }
79 else if(BY_TYPE.equals(newValue)) {
80 bd.setAutowireMode(AutowireCapableBeanFactory.AUTOWIRE_BY_TYPE);
81 }
82 else if(Boolean.TRUE.equals(newValue)) {
83 bd.setAutowireMode(AutowireCapableBeanFactory.AUTOWIRE_BY_NAME);
84 }
85 else if(BY_CONSTRUCTOR.equals(newValue)) {
86 bd.setAutowireMode(AutowireCapableBeanFactory.AUTOWIRE_CONSTRUCTOR);
87 }
88 }
89 // constructorArgs
90 else if(CONSTRUCTOR_ARGS.equals(property) && newValue instanceof List) {
91 ConstructorArgumentValues cav = new ConstructorArgumentValues();
92 List args = (List)newValue;
93 for (Object e : args) {
94 cav.addGenericArgumentValue(e);
95 }
96 bd.setConstructorArgumentValues(cav);
97 }
98 // destroyMethod
99 else if(DESTROY_METHOD.equals(property)) {
100 if(newValue != null)
101 bd.setDestroyMethodName(newValue.toString());
102 }
103 // factoryBean
104 else if(FACTORY_BEAN.equals(property)) {
105 if(newValue != null)
106 bd.setFactoryBeanName(newValue.toString());
107 }
108 // factoryMethod
109 else if(FACTORY_METHOD.equals(property)) {
110 if(newValue != null)
111 bd.setFactoryMethodName(newValue.toString());
112 }
113 // initMethod
114 else if(INIT_METHOD.equals(property)) {
115 if(newValue != null)
116 bd.setInitMethodName(newValue.toString());
117 }
118 else if(wrapper.isWritableProperty(property)) {
119
120 wrapper.setPropertyValue(property, newValue);
121 }
122 // autowire
123 else {
124 super.setProperty(property, newValue);
125 }
126 }
127 }
128
129 private Class clazz;
130 private String name;
131 private boolean singleton = true;
132 private AbstractBeanDefinition definition;
133 private Collection constructorArgs = Collections.EMPTY_LIST;
134 private BeanWrapper wrapper;
135
136 public DefaultBeanConfiguration(String name, Class clazz) {
137 this.name = name;
138 this.clazz = clazz;
139 }
140
141 public DefaultBeanConfiguration(String name, Class clazz, boolean prototype) {
142 this(name,clazz,Collections.EMPTY_LIST);
143 this.singleton = !prototype;
144 }
145
146 public DefaultBeanConfiguration(String name) {
147 this(name,null,Collections.EMPTY_LIST);
148 }
149
150 public DefaultBeanConfiguration(Class clazz2) {
151 this.clazz = clazz2;
152 }
153
154 public DefaultBeanConfiguration(String name2, Class clazz2, Collection args) {
155 this.name = name2;
156 this.clazz = clazz2;
157 this.constructorArgs = args;
158 }
159
160 public DefaultBeanConfiguration(String name2, boolean prototype) {
161 this(name2,null,Collections.EMPTY_LIST);
162 this.singleton = !prototype;
163 }
164
165 public DefaultBeanConfiguration(Class clazz2, Collection constructorArguments) {
166 this.clazz = clazz2;
167 this.constructorArgs = constructorArguments;
168 }
169
170 public String getName() {
171 return this.name;
172 }
173
174 public boolean isSingleton() {
175 return this.singleton ;
176 }
177
178 public AbstractBeanDefinition getBeanDefinition() {
179 if (definition == null)
180 definition = createBeanDefinition();
181 return definition;
182 }
183
184 protected AbstractBeanDefinition createBeanDefinition() {
185 AbstractBeanDefinition bd;
186 if(constructorArgs.size() > 0) {
187 ConstructorArgumentValues cav = new ConstructorArgumentValues();
188 for (Object constructorArg : constructorArgs) {
189 cav.addGenericArgumentValue(constructorArg);
190 }
191 if(StringUtils.isBlank(parentName)) {
192 bd = new RootBeanDefinition(clazz,cav,null);
193 }
194 else {
195 bd = new ChildBeanDefinition(parentName,clazz,cav, null);
196 }
197 bd.setSingleton(singleton);
198 }
199 else {
200 if(StringUtils.isBlank(parentName)) {
201 bd = new RootBeanDefinition(clazz,singleton);
202 }
203 else {
204 bd = new ChildBeanDefinition(parentName,clazz, null,null);
205 bd.setSingleton(singleton);
206 }
207
208 }
209 wrapper = new BeanWrapperImpl(bd);
210 return bd;
211 }
212
213 public BeanConfiguration addProperty(String propertyName, Object propertyValue) {
214 if(propertyValue instanceof BeanConfiguration) {
215 propertyValue = ((BeanConfiguration)propertyValue).getBeanDefinition();
216 }
217 getBeanDefinition()
218 .getPropertyValues()
219 .addPropertyValue(propertyName,propertyValue);
220
221 return this;
222 }
223
224 public BeanConfiguration setDestroyMethod(String methodName) {
225 getBeanDefinition().setDestroyMethodName(methodName);
226 return this;
227 }
228
229 public BeanConfiguration setDependsOn(String[] dependsOn) {
230 getBeanDefinition().setDependsOn(dependsOn);
231 return this;
232 }
233
234 public BeanConfiguration setFactoryBean(String beanName) {
235 getBeanDefinition().setFactoryBeanName(beanName);
236
237 return this;
238 }
239
240 public BeanConfiguration setFactoryMethod(String methodName) {
241 getBeanDefinition().setFactoryMethodName(methodName);
242 return this;
243 }
244
245 public BeanConfiguration setAutowire(String type) {
246 if("byName".equals(type)) {
247 getBeanDefinition().setAutowireMode(AbstractBeanDefinition.AUTOWIRE_BY_NAME);
248 }
249 else if("byType".equals(type)){
250 getBeanDefinition().setAutowireMode(AbstractBeanDefinition.AUTOWIRE_BY_TYPE);
251 }
252 return this;
253 }
254
255 public void setName(String beanName) {
256 this.name = beanName;
257 }
258
259 public Object getPropertyValue(String name) {
260 return getBeanDefinition()
261 .getPropertyValues()
262 .getPropertyValue(name)
263 .getValue();
264 }
265
266 public boolean hasProperty(String name) {
267 return getBeanDefinition().getPropertyValues().contains(name);
268 }
269
270 public void setPropertyValue(String property, Object newValue) {
271 getBeanDefinition().getPropertyValues().addPropertyValue(property, newValue);
272 }
273
274 public BeanConfiguration setAbstract(boolean isAbstract) {
275 getBeanDefinition().setAbstract(isAbstract);
276 return this;
277 }
278
279 public void setParent(Object obj) {
280 if(obj == null) throw new IllegalArgumentException("Parent bean cannot be set to a null runtime bean reference!");
281 if(obj instanceof String)
282 this.parentName = (String)obj;
283 else if(obj instanceof RuntimeBeanReference) {
284 this.parentName = ((RuntimeBeanReference)obj).getBeanName();
285 }
286 else if(obj instanceof BeanConfiguration) {
287 this.parentName = ((BeanConfiguration)obj).getName();
288 }
289 }
290
291
292 }
293