View Javadoc

1   /*
2    * Copyright 2006-2013 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  	@Override
57  	protected Class<? extends AbstractListenerFactoryBean> getBeanClass() {
58  		return StepListenerFactoryBean.class;
59  	}
60  
61  	@Override
62  	protected ListenerMetaData[] getMetaDataValues() {
63  		return listenerMetaData;
64  	}
65  
66  	@SuppressWarnings({"unchecked", "rawtypes"})
67  	public void handleListenersElement(Element stepElement, BeanDefinition beanDefinition,
68  			ParserContext parserContext) {
69  		MutablePropertyValues propertyValues = beanDefinition.getPropertyValues();
70  		List<Element> listenersElements = DomUtils.getChildElementsByTagName(stepElement, LISTENERS_ELE);
71  		if (listenersElements.size() == 1) {
72  			Element listenersElement = listenersElements.get(0);
73  			CompositeComponentDefinition compositeDef = new CompositeComponentDefinition(listenersElement.getTagName(),
74  					parserContext.extractSource(stepElement));
75  			parserContext.pushContainingComponent(compositeDef);
76  			ManagedList listenerBeans = new ManagedList();
77  			if (propertyValues.contains("listeners")) {
78  				listenerBeans = (ManagedList) propertyValues.getPropertyValue("listeners").getValue();
79  			}
80  			listenerBeans.setMergeEnabled(listenersElement.hasAttribute(MERGE_ATTR)
81  					&& Boolean.valueOf(listenersElement.getAttribute(MERGE_ATTR)));
82  			List<Element> listenerElements = DomUtils.getChildElementsByTagName(listenersElement, "listener");
83  			if (listenerElements != null) {
84  				for (Element listenerElement : listenerElements) {
85  					listenerBeans.add(parse(listenerElement, parserContext));
86  				}
87  			}
88  			propertyValues.addPropertyValue("listeners", listenerBeans);
89  			parserContext.popAndRegisterContainingComponent();
90  		}
91  	}
92  
93  }