File D:\maven-qcr-plugin-testsunitaires\qcr4java\jenkins-master\target\site\qc\core\src\main\java\hudson\util\spring\DefaultRuntimeSpringConfiguration.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 org.springframework.beans.PropertyValue;
19 import org.springframework.beans.factory.config.BeanDefinition;
20 import org.springframework.beans.factory.config.BeanFactoryPostProcessor;
21 import org.springframework.beans.factory.support.AbstractBeanDefinition;
22 import org.springframework.context.ApplicationContext;
23 import org.springframework.context.support.StaticApplicationContext;
24 import org.springframework.web.context.WebApplicationContext;
25 import org.springframework.web.context.support.StaticWebApplicationContext;
26
27 import javax.servlet.ServletContext;
28 import java.util.ArrayList;
29 import java.util.Collection;
30 import java.util.HashMap;
31 import java.util.List;
32 import java.util.Map;
33 import java.util.logging.Level;
34 import java.util.logging.Logger;
35
36 /**
37 * A programmable runtime Spring configuration that allows a spring ApplicationContext
38 * to be constructed at runtime
39 *
40 * Credit must go to Solomon Duskis and the
41 * article: http://jroller.com/page/Solomon?entry=programmatic_configuration_in_spring
42 *
43 * @author Graeme
44 * @since 0.3
45 *
46 */
47 class DefaultRuntimeSpringConfiguration implements RuntimeSpringConfiguration {
48 private static final Logger LOGGER = Logger.getLogger(DefaultRuntimeSpringConfiguration.class.getName());
49 private StaticWebApplicationContext context;
50 private Map<String,BeanConfiguration> beanConfigs = new HashMap<String,BeanConfiguration>();
51 private Map<String,BeanDefinition> beanDefinitions = new HashMap<String,BeanDefinition>();
52 private List<String> beanNames = new ArrayList<String>();
53
54 public DefaultRuntimeSpringConfiguration() {
55 super();
56 this.context = new StaticWebApplicationContext();
57 }
58
59 public DefaultRuntimeSpringConfiguration(ApplicationContext parent) {
60 super();
61 this.context = new StaticWebApplicationContext();
62 context.setParent(parent);
63 // if(parent != null){
64 // trySettingClassLoaderOnContextIfFoundInParent(parent);
65 // }
66 }
67
68 // private void trySettingClassLoaderOnContextIfFoundInParent(ApplicationContext parent) {
69 // try{
70 // Object classLoader = parent.getBean(GrailsRuntimeConfigurator.CLASS_LOADER_BEAN);
71 // if(classLoader instanceof ClassLoader){
72 // // this.context.setClassLoader((ClassLoader) classLoader);
73 // }
74 // }catch(NoSuchBeanDefinitionException nsbde){
75 // //ignore, we tried our best
76 // }
77 // }
78
79
80 public BeanConfiguration addSingletonBean(String name, Class clazz) {
81 BeanConfiguration bc = new DefaultBeanConfiguration(name,clazz);
82 registerBeanConfiguration(name, bc);
83 return bc;
84 }
85
86 public BeanConfiguration addPrototypeBean(String name, Class clazz) {
87 BeanConfiguration bc = new DefaultBeanConfiguration(name,clazz,true);
88 registerBeanConfiguration(name, bc);
89 return bc;
90 }
91
92 public WebApplicationContext getApplicationContext() {
93 registerBeansWithContext(context);
94 context.refresh();
95 return context;
96 }
97
98 public WebApplicationContext getUnrefreshedApplicationContext() {
99 return context;
100 }
101
102 public BeanConfiguration addSingletonBean(String name) {
103 BeanConfiguration bc = new DefaultBeanConfiguration(name);
104 registerBeanConfiguration(name, bc);
105 return bc;
106 }
107
108 public BeanConfiguration createSingletonBean(Class clazz) {
109 return new DefaultBeanConfiguration(clazz);
110 }
111
112 public BeanConfiguration addSingletonBean(String name, Class clazz, Collection args) {
113 BeanConfiguration bc = new DefaultBeanConfiguration(name,clazz,args);
114 registerBeanConfiguration(name, bc);
115 return bc;
116 }
117
118 public BeanConfiguration addPrototypeBean(String name) {
119 BeanConfiguration bc = new DefaultBeanConfiguration(name,true);
120 registerBeanConfiguration(name, bc);
121 return bc;
122 }
123
124 private void registerBeanConfiguration(String name, BeanConfiguration bc) {
125 beanConfigs.put(name,bc);
126 beanNames.add(name);
127 }
128
129 public BeanConfiguration createSingletonBean(Class clazz, Collection constructorArguments) {
130 return new DefaultBeanConfiguration(clazz, constructorArguments);
131 }
132
133 public void setServletContext(ServletContext context) {
134 this.context.setServletContext(context);
135 }
136
137 public BeanConfiguration createPrototypeBean(String name) {
138 return new DefaultBeanConfiguration(name,true);
139 }
140
141 public BeanConfiguration createSingletonBean(String name) {
142 return new DefaultBeanConfiguration(name);
143 }
144
145 public void addBeanConfiguration(String beanName, BeanConfiguration beanConfiguration) {
146 beanConfiguration.setName(beanName);
147 registerBeanConfiguration(beanName, beanConfiguration);
148 }
149
150 public void addBeanDefinition(String name, BeanDefinition bd) {
151 beanDefinitions.put(name,bd);
152 beanNames.add(name);
153 }
154
155 public boolean containsBean(String name) {
156 return beanNames .contains(name);
157 }
158
159 public BeanConfiguration getBeanConfig(String name) {
160 return beanConfigs.get(name);
161 }
162
163 public AbstractBeanDefinition createBeanDefinition(String name) {
164 if(containsBean(name)) {
165 if(beanDefinitions.containsKey(name))
166 return (AbstractBeanDefinition)beanDefinitions.get(name);
167 else if(beanConfigs.containsKey(name))
168 return beanConfigs.get(name).getBeanDefinition();
169 }
170 return null;
171 }
172
173 public void registerPostProcessor(BeanFactoryPostProcessor processor) {
174 this.context.addBeanFactoryPostProcessor(processor);
175 }
176
177
178
179 public List<String> getBeanNames() {
180 return beanNames;
181 }
182
183 public void registerBeansWithContext(StaticApplicationContext applicationContext) {
184 for (BeanConfiguration bc : beanConfigs.values()) {
185 if (LOGGER.isLoggable(Level.FINER)) {
186 LOGGER.finer("[RuntimeConfiguration] Registering bean [" + bc.getName() + "]");
187 if (LOGGER.isLoggable(Level.FINEST)) {
188 PropertyValue[] pvs = bc.getBeanDefinition()
189 .getPropertyValues()
190 .getPropertyValues();
191 for (PropertyValue pv : pvs) {
192 LOGGER.finest("[RuntimeConfiguration] With property [" + pv.getName() + "] set to [" + pv.getValue() + "]");
193 }
194 }
195 }
196
197
198 if (applicationContext.containsBeanDefinition(bc.getName()))
199 applicationContext.removeBeanDefinition(bc.getName());
200
201 applicationContext.registerBeanDefinition(bc.getName(),
202 bc.getBeanDefinition());
203 }
204 for (String key : beanDefinitions.keySet()) {
205 BeanDefinition bd = beanDefinitions.get(key);
206 if (LOGGER.isLoggable(Level.FINER)) {
207 LOGGER.finer("[RuntimeConfiguration] Registering bean [" + key + "]");
208 if (LOGGER.isLoggable(Level.FINEST)) {
209 for (PropertyValue pv : bd.getPropertyValues().getPropertyValues()) {
210 LOGGER.finest("[RuntimeConfiguration] With property [" + pv.getName() + "] set to [" + pv.getValue() + "]");
211 }
212 }
213 }
214 if (applicationContext.containsBean(key)) {
215 applicationContext.removeBeanDefinition(key);
216 }
217
218 applicationContext.registerBeanDefinition(key, bd);
219
220 }
221 }
222
223 /**
224 * Adds an abstract bean and returns the BeanConfiguration instance
225 *
226 * @param name The name of the bean
227 * @return The BeanConfiguration object
228 */
229 public BeanConfiguration addAbstractBean(String name) {
230 BeanConfiguration bc = new DefaultBeanConfiguration(name);
231 bc.setAbstract(true);
232 registerBeanConfiguration(name, bc);
233
234 return bc;
235 }
236 }
237