1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17 package edu.internet2.middleware.shibboleth.common.config;
18
19 import java.util.List;
20
21 import org.opensaml.util.resource.Resource;
22 import org.opensaml.util.resource.ResourceException;
23 import org.opensaml.xml.util.DatatypeHelper;
24 import org.opensaml.xml.util.XMLHelper;
25 import org.slf4j.Logger;
26 import org.slf4j.LoggerFactory;
27 import org.springframework.beans.factory.BeanDefinitionStoreException;
28 import org.springframework.beans.factory.config.BeanDefinition;
29 import org.springframework.beans.factory.config.RuntimeBeanReference;
30 import org.springframework.beans.factory.support.BeanDefinitionRegistry;
31 import org.springframework.beans.factory.support.ManagedList;
32 import org.springframework.beans.factory.xml.BeanDefinitionParserDelegate;
33 import org.springframework.beans.factory.xml.NamespaceHandler;
34 import org.springframework.beans.factory.xml.ParserContext;
35 import org.springframework.beans.factory.xml.XmlBeanDefinitionReader;
36 import org.springframework.core.io.InputStreamResource;
37 import org.w3c.dom.Element;
38
39
40
41
42 public final class SpringConfigurationUtils {
43
44
45 private static Logger log = LoggerFactory.getLogger(SpringConfigurationUtils.class);
46
47
48 private SpringConfigurationUtils() {
49 }
50
51
52
53
54
55
56
57
58
59
60 public static void populateRegistry(BeanDefinitionRegistry beanRegistry, List<Resource> configurationResources)
61 throws ResourceException {
62 XmlBeanDefinitionReader configReader = new XmlBeanDefinitionReader(beanRegistry);
63 configReader.setValidationMode(XmlBeanDefinitionReader.VALIDATION_XSD);
64 configReader.setDocumentLoader(new SpringDocumentLoader());
65
66 int numOfResources = configurationResources.size();
67 Resource configurationResource;
68 org.springframework.core.io.Resource[] configSources = new org.springframework.core.io.Resource[numOfResources];
69 for (int i = 0; i < numOfResources; i++) {
70 configurationResource = configurationResources.get(i);
71 if (configurationResource != null && configurationResource.exists()) {
72 configSources[i] = new InputStreamResource(configurationResources.get(i).getInputStream(),
73 configurationResource.getLocation());
74 } else {
75 log.warn("Configuration resource not loaded because it does not exist: {}", configurationResource
76 .getLocation());
77 }
78 }
79
80 try {
81 configReader.loadBeanDefinitions(configSources);
82 } catch (BeanDefinitionStoreException e) {
83 throw new ResourceException("Unable to load Spring bean registry with configuration resources", e);
84 }
85 }
86
87
88
89
90
91
92
93
94
95
96 public static BeanDefinition parseInnerCustomElement(Element element, ParserContext parserContext) {
97 return createBeanDefinition(element, parserContext);
98 }
99
100
101
102
103
104
105
106
107
108
109 public static ManagedList parseInnerCustomElements(List<Element> elements, ParserContext parserContext) {
110 ManagedList beans = new ManagedList();
111 if (elements != null) {
112 for (Element element : elements) {
113 beans.add(parseInnerCustomElement(element, parserContext));
114 }
115 }
116
117 return beans;
118 }
119
120
121
122
123
124
125
126
127
128
129
130 public static RuntimeBeanReference parseCustomElement(Element element, ParserContext parserContext) {
131 return parseCustomElement(element, "id", parserContext);
132 }
133
134
135
136
137
138
139
140
141
142
143
144 public static RuntimeBeanReference parseCustomElement(Element element, String idAttribute,
145 ParserContext parserContext) {
146 createBeanDefinition(element, parserContext);
147 RuntimeBeanReference beanRef = new RuntimeBeanReference(element.getAttributeNS(null, idAttribute));
148 beanRef.setSource(element);
149 return beanRef;
150 }
151
152
153
154
155
156
157
158
159
160 private static BeanDefinition createBeanDefinition(Element element, ParserContext parserContext) {
161 BeanDefinitionParserDelegate delegate = parserContext.getDelegate();
162 String namespaceUri = element.getNamespaceURI();
163
164 if (XMLHelper.hasXSIType(element)) {
165 namespaceUri = XMLHelper.getXSIType(element).getNamespaceURI();
166 }
167
168 NamespaceHandler handler = delegate.getReaderContext().getNamespaceHandlerResolver().resolve(namespaceUri);
169 if (handler == null) {
170 log.error("Unable to locate NamespaceHandler for namespace [" + namespaceUri + "]");
171 return null;
172 }
173 return handler.parse(element, new ParserContext(delegate.getReaderContext(), delegate));
174 }
175
176
177
178
179
180
181
182
183
184
185 public static RuntimeBeanReference parseCustomElementReference(Element element, String refAttribute,
186 ParserContext parserContext) {
187 String reference = DatatypeHelper.safeTrimOrNullString(element.getAttributeNS(null, refAttribute));
188 if (reference != null) {
189 return new RuntimeBeanReference(reference);
190 }
191
192 return null;
193 }
194
195
196
197
198
199
200
201
202
203
204 public static ManagedList parseCustomElements(List<Element> elements, ParserContext parserContext) {
205 return parseCustomElements(elements, "id", parserContext);
206 }
207
208
209
210
211
212
213
214
215
216
217 public static ManagedList parseCustomElements(List<Element> elements, String idAttribute,
218 ParserContext parserContext) {
219 if (elements == null) {
220 return null;
221 }
222
223 ManagedList definitions = new ManagedList(elements.size());
224 for (Element e : elements) {
225 definitions.add(parseCustomElement(e, idAttribute, parserContext));
226 }
227
228 return definitions;
229 }
230 }