1 | |
|
2 | |
|
3 | |
|
4 | |
|
5 | |
|
6 | |
|
7 | |
|
8 | |
|
9 | |
|
10 | |
|
11 | |
|
12 | |
|
13 | |
|
14 | |
|
15 | |
|
16 | |
package org.seasar.cubby.plugins.spring.spi; |
17 | |
|
18 | |
import java.util.Collection; |
19 | |
import java.util.Collections; |
20 | |
import java.util.LinkedHashSet; |
21 | |
import java.util.Set; |
22 | |
|
23 | |
import org.seasar.cubby.converter.Converter; |
24 | |
import org.seasar.cubby.spi.impl.AbstractCachedConverterProvider; |
25 | |
import org.springframework.beans.factory.BeanFactoryUtils; |
26 | |
import org.springframework.context.ApplicationContext; |
27 | |
import org.springframework.context.ApplicationContextAware; |
28 | |
|
29 | |
|
30 | |
|
31 | |
|
32 | |
|
33 | |
|
34 | |
|
35 | 6 | public class SpringConverterProvider extends AbstractCachedConverterProvider |
36 | |
implements ApplicationContextAware { |
37 | |
|
38 | |
private ApplicationContext applicationContext; |
39 | |
|
40 | 6 | private boolean initialized = false; |
41 | |
|
42 | 6 | private final Class<Converter> converterClass = Converter.class; |
43 | |
|
44 | 6 | private final Set<Converter> converters = new LinkedHashSet<Converter>(); |
45 | |
|
46 | |
|
47 | |
|
48 | |
|
49 | |
|
50 | |
|
51 | |
|
52 | |
public void setApplicationContext( |
53 | |
final ApplicationContext applicationContext) { |
54 | 6 | this.applicationContext = applicationContext; |
55 | 6 | } |
56 | |
|
57 | |
|
58 | |
|
59 | |
|
60 | |
public void initialize() { |
61 | 6 | if (initialized) { |
62 | 0 | return; |
63 | |
} |
64 | 6 | String[] names = BeanFactoryUtils.beanNamesForTypeIncludingAncestors( |
65 | |
applicationContext, converterClass); |
66 | 23 | for (String name : names) { |
67 | 17 | Converter converter = converterClass.cast(applicationContext |
68 | |
.getBean(name, converterClass)); |
69 | 17 | converters.add(converter); |
70 | |
} |
71 | 6 | initialized = true; |
72 | 6 | } |
73 | |
|
74 | |
@Override |
75 | |
protected Collection<Converter> getConverters() { |
76 | 1 | return Collections.unmodifiableCollection(this.converters); |
77 | |
} |
78 | |
|
79 | |
} |