1 | /* |
2 | * Copyright 2006-2009 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 org.springframework.batch.core.configuration.xml; |
17 | |
18 | import java.util.List; |
19 | |
20 | import org.springframework.batch.core.listener.AbstractListenerFactoryBean; |
21 | import org.springframework.batch.core.listener.ListenerMetaData; |
22 | import org.springframework.batch.core.listener.StepListenerFactoryBean; |
23 | import org.springframework.batch.core.listener.StepListenerMetaData; |
24 | import org.springframework.beans.MutablePropertyValues; |
25 | import org.springframework.beans.factory.config.BeanDefinition; |
26 | import org.springframework.beans.factory.parsing.CompositeComponentDefinition; |
27 | import org.springframework.beans.factory.support.ManagedList; |
28 | import org.springframework.beans.factory.xml.ParserContext; |
29 | import org.springframework.util.xml.DomUtils; |
30 | import org.w3c.dom.Element; |
31 | |
32 | /** |
33 | * Parser for a step listener element. Builds a {@link StepListenerFactoryBean} |
34 | * using attributes from the configuration. |
35 | * |
36 | * @author Dan Garrette |
37 | * @since 2.0 |
38 | * @see AbstractListenerParser |
39 | */ |
40 | public class StepListenerParser extends AbstractListenerParser { |
41 | |
42 | private static final String LISTENERS_ELE = "listeners"; |
43 | |
44 | private static final String MERGE_ATTR = "merge"; |
45 | |
46 | private final ListenerMetaData[] listenerMetaData; |
47 | |
48 | public StepListenerParser() { |
49 | this(StepListenerMetaData.values()); |
50 | } |
51 | |
52 | public StepListenerParser(ListenerMetaData[] listenerMetaData) { |
53 | this.listenerMetaData = listenerMetaData; |
54 | } |
55 | |
56 | protected Class<? extends AbstractListenerFactoryBean> getBeanClass() { |
57 | return StepListenerFactoryBean.class; |
58 | } |
59 | |
60 | protected ListenerMetaData[] getMetaDataValues() { |
61 | return listenerMetaData; |
62 | } |
63 | |
64 | @SuppressWarnings("unchecked") |
65 | public void handleListenersElement(Element stepElement, BeanDefinition beanDefinition, |
66 | ParserContext parserContext) { |
67 | MutablePropertyValues propertyValues = beanDefinition.getPropertyValues(); |
68 | List<Element> listenersElements = DomUtils.getChildElementsByTagName(stepElement, LISTENERS_ELE); |
69 | if (listenersElements.size() == 1) { |
70 | Element listenersElement = listenersElements.get(0); |
71 | CompositeComponentDefinition compositeDef = new CompositeComponentDefinition(listenersElement.getTagName(), |
72 | parserContext.extractSource(stepElement)); |
73 | parserContext.pushContainingComponent(compositeDef); |
74 | ManagedList listenerBeans = new ManagedList(); |
75 | if (propertyValues.contains("listeners")) { |
76 | listenerBeans = (ManagedList) propertyValues.getPropertyValue("listeners").getValue(); |
77 | } |
78 | listenerBeans.setMergeEnabled(listenersElement.hasAttribute(MERGE_ATTR) |
79 | && Boolean.valueOf(listenersElement.getAttribute(MERGE_ATTR))); |
80 | List<Element> listenerElements = DomUtils.getChildElementsByTagName(listenersElement, "listener"); |
81 | if (listenerElements != null) { |
82 | for (Element listenerElement : listenerElements) { |
83 | listenerBeans.add(parse(listenerElement, parserContext)); |
84 | } |
85 | } |
86 | propertyValues.addPropertyValue("listeners", listenerBeans); |
87 | parserContext.popAndRegisterContainingComponent(); |
88 | } |
89 | } |
90 | |
91 | } |