1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17 package org.springframework.ws.support;
18
19 import java.io.IOException;
20 import java.util.ArrayList;
21 import java.util.Collections;
22 import java.util.List;
23 import java.util.Properties;
24 import javax.servlet.ServletContext;
25
26 import org.springframework.beans.BeanUtils;
27 import org.springframework.beans.BeansException;
28 import org.springframework.beans.factory.BeanClassLoaderAware;
29 import org.springframework.beans.factory.BeanCreationException;
30 import org.springframework.beans.factory.BeanFactory;
31 import org.springframework.beans.factory.BeanFactoryAware;
32 import org.springframework.beans.factory.BeanInitializationException;
33 import org.springframework.beans.factory.BeanNameAware;
34 import org.springframework.beans.factory.InitializingBean;
35 import org.springframework.context.ApplicationContext;
36 import org.springframework.context.ApplicationContextAware;
37 import org.springframework.context.ApplicationEventPublisherAware;
38 import org.springframework.context.MessageSourceAware;
39 import org.springframework.context.ResourceLoaderAware;
40 import org.springframework.core.OrderComparator;
41 import org.springframework.core.io.Resource;
42 import org.springframework.core.io.support.PropertiesLoaderUtils;
43 import org.springframework.util.Assert;
44 import org.springframework.util.ClassUtils;
45 import org.springframework.util.StringUtils;
46 import org.springframework.web.context.ServletContextAware;
47 import org.springframework.web.context.WebApplicationContext;
48
49
50
51
52
53
54
55
56
57
58
59
60 public class DefaultStrategiesHelper {
61
62
63 private Properties defaultStrategies;
64
65
66 public DefaultStrategiesHelper(Properties defaultStrategies) {
67 Assert.notNull(defaultStrategies, "defaultStrategies must not be null");
68 this.defaultStrategies = defaultStrategies;
69 }
70
71
72 public DefaultStrategiesHelper(Resource resource) throws IllegalStateException {
73 try {
74 defaultStrategies = PropertiesLoaderUtils.loadProperties(resource);
75 }
76 catch (IOException ex) {
77 throw new IllegalStateException("Could not load '" + resource + "': " + ex.getMessage());
78 }
79 }
80
81
82
83
84
85
86
87
88
89 public List getDefaultStrategies(Class strategyInterface) throws BeanInitializationException {
90 return getDefaultStrategies(strategyInterface, null);
91 }
92
93
94
95
96
97
98
99
100
101
102
103
104 public List getDefaultStrategies(Class strategyInterface, ApplicationContext applicationContext)
105 throws BeanInitializationException {
106 String key = strategyInterface.getName();
107 try {
108 List result;
109 String value = defaultStrategies.getProperty(key);
110 if (value != null) {
111 String[] classNames = StringUtils.commaDelimitedListToStringArray(value);
112 result = new ArrayList(classNames.length);
113 for (int i = 0; i < classNames.length; i++) {
114 Class clazz = ClassUtils.forName(classNames[i]);
115 Object strategy = instantiateBean(clazz, applicationContext);
116 result.add(strategy);
117 }
118 }
119 else {
120 result = Collections.EMPTY_LIST;
121 }
122 Collections.sort(result, new OrderComparator());
123 return result;
124 }
125 catch (ClassNotFoundException ex) {
126 throw new BeanInitializationException("Could not find default strategy class for interface [" + key + "]",
127 ex);
128 }
129 }
130
131
132 private Object instantiateBean(Class clazz, ApplicationContext applicationContext) {
133 Object strategy = BeanUtils.instantiateClass(clazz);
134 if (strategy instanceof BeanNameAware) {
135 BeanNameAware beanNameAware = (BeanNameAware) strategy;
136 beanNameAware.setBeanName(clazz.getName());
137 }
138 if (applicationContext != null) {
139 if (strategy instanceof BeanClassLoaderAware) {
140 ((BeanClassLoaderAware) strategy).setBeanClassLoader(applicationContext.getClassLoader());
141 }
142 if (strategy instanceof BeanFactoryAware) {
143 ((BeanFactoryAware) strategy).setBeanFactory(applicationContext);
144 }
145 if (strategy instanceof ResourceLoaderAware) {
146 ((ResourceLoaderAware) strategy).setResourceLoader(applicationContext);
147 }
148 if (strategy instanceof ApplicationEventPublisherAware) {
149 ((ApplicationEventPublisherAware) strategy).setApplicationEventPublisher(applicationContext);
150 }
151 if (strategy instanceof MessageSourceAware) {
152 ((MessageSourceAware) strategy).setMessageSource(applicationContext);
153 }
154 if (strategy instanceof ApplicationContextAware) {
155 ApplicationContextAware applicationContextAware = (ApplicationContextAware) strategy;
156 applicationContextAware.setApplicationContext(applicationContext);
157 }
158 if (applicationContext instanceof WebApplicationContext && strategy instanceof ServletContextAware) {
159 ServletContext servletContext = ((WebApplicationContext) applicationContext).getServletContext();
160 ((ServletContextAware) strategy).setServletContext(servletContext);
161 }
162 }
163 if (strategy instanceof InitializingBean) {
164 InitializingBean initializingBean = (InitializingBean) strategy;
165 try {
166 initializingBean.afterPropertiesSet();
167 }
168 catch (Throwable ex) {
169 throw new BeanCreationException("Invocation of init method failed", ex);
170 }
171 }
172 return strategy;
173 }
174
175
176
177
178
179
180
181
182
183 public Object getDefaultStrategy(Class strategyInterface) throws BeanInitializationException {
184 return getDefaultStrategy(strategyInterface, null);
185 }
186
187
188
189
190
191
192
193
194
195
196
197
198 public Object getDefaultStrategy(Class strategyInterface, ApplicationContext applicationContext)
199 throws BeanInitializationException {
200 List result = getDefaultStrategies(strategyInterface, applicationContext);
201 if (result.size() != 1) {
202 throw new BeanInitializationException(
203 "Could not find exactly 1 strategy for interface [" + strategyInterface.getName() + "]");
204 }
205 return result.get(0);
206 }
207
208 }