1 package org.apache.archiva.redback.components.springutils;
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22 import org.apache.commons.lang.StringUtils;
23 import org.slf4j.Logger;
24 import org.slf4j.LoggerFactory;
25 import org.springframework.context.ApplicationContext;
26 import org.springframework.stereotype.Service;
27
28 import javax.inject.Inject;
29 import java.util.Collections;
30 import java.util.HashMap;
31 import java.util.Map;
32 import java.util.concurrent.ConcurrentHashMap;
33
34
35
36
37
38 @Service( "componentContainer" )
39 public class ComponentContainer
40 {
41
42 private Logger log = LoggerFactory.getLogger( getClass() );
43
44 private static final String DEFAULT_ROLE_HINT = "default";
45
46 public static final String BEAN_NAME_ROLEHINTSEPARATOR = "#";
47
48
49
50
51
52
53 private Map<String, Map<String, ?>> classBeansOfType = new ConcurrentHashMap<String, Map<String, ?>>();
54
55 public String getDefaultRoleHint()
56 {
57 return DEFAULT_ROLE_HINT;
58 }
59
60 @Inject
61 protected ApplicationContext applicationContext;
62
63
64
65
66
67
68
69
70
71
72
73 public <T> T getComponent( Class<T> clazz )
74 {
75 Map<String, T> beansOfType = applicationContext.getBeansOfType( clazz );
76 if ( beansOfType == null || beansOfType.isEmpty() )
77 {
78 throw new RuntimeException( "no beans of Type " + clazz.getName() );
79 }
80 if ( beansOfType.size() > 1 )
81 {
82 throw new RuntimeException( "not only one beans of Type " + clazz.getName() );
83 }
84 return beansOfType.values().iterator().next();
85 }
86
87
88
89
90
91
92
93
94
95
96
97 public <T> T getComponent( Class<T> clazz, String hint )
98 {
99 Map<String, T> beansOfType = buildMapWithRole( clazz );
100 if ( beansOfType == null || beansOfType.isEmpty() )
101 {
102 throw new RuntimeException( "no beans of Type " + clazz.getName() );
103 }
104 T bean = beansOfType.get( hint );
105 if ( bean == null )
106 {
107 throw new RuntimeException( "no beans of Type " + clazz.getName() + " with hint " + hint );
108 }
109 return bean;
110 }
111
112
113
114
115
116
117
118
119 public <T> boolean hasComponent( Class<T> clazz )
120 {
121 Map<String, T> beansOfType = applicationContext.getBeansOfType( clazz );
122 if ( beansOfType == null || beansOfType.isEmpty() )
123 {
124 return false;
125 }
126 return beansOfType.size() == 1;
127 }
128
129
130
131
132
133
134
135
136
137 public <T> boolean hasComponent( Class<T> clazz, String hint )
138 {
139 Map<String, T> beansOfType = buildMapWithRole( clazz );
140 if ( beansOfType == null || beansOfType.isEmpty() )
141 {
142 return false;
143 }
144 return beansOfType.containsKey( hint );
145 }
146
147
148
149
150
151
152
153
154
155
156
157 @SuppressWarnings( "unchecked" )
158 public <T> Map<String, T> buildMapWithRole( Class<T> clazz )
159 {
160 try
161 {
162 Map<String, T> beansOfType = (Map<String, T>) classBeansOfType.get( clazz.getName() );
163 if ( beansOfType == null )
164 {
165 Map<String, T> map = this.applicationContext.getBeansOfType( clazz );
166 beansOfType = buildMapWithRole( map );
167 classBeansOfType.put( clazz.getName(), beansOfType );
168 }
169 return beansOfType;
170 }
171 catch ( Throwable e )
172 {
173 log.error( e.getMessage(), e );
174 throw new RuntimeException( e.getMessage(), e );
175 }
176 }
177
178
179
180
181
182
183
184
185
186
187
188 public static <T> Map<String, T> buildMapWithRole( Map<String, T> beansOfType )
189 {
190 if ( beansOfType == null || beansOfType.isEmpty() )
191 {
192 return Collections.emptyMap();
193 }
194 Map<String, T> beansOfHint = new HashMap<String, T>();
195 for ( Map.Entry<String, T> entry : beansOfType.entrySet() )
196 {
197 int separatorIndex = StringUtils.indexOf( entry.getKey(), '#' );
198 if ( separatorIndex >= 0 )
199 {
200 String hint = entry.getKey().substring( separatorIndex + 1, entry.getKey().length() );
201 beansOfHint.put( hint, beansOfType.get( entry.getKey() ) );
202 }
203 else
204 {
205 beansOfHint.put( entry.getKey(), beansOfType.get( entry.getKey() ) );
206 }
207 }
208 return beansOfHint;
209 }
210 }