1 | package org.springframework.batch.core.configuration.xml; |
2 | |
3 | import java.util.ArrayList; |
4 | import java.util.List; |
5 | |
6 | import org.springframework.batch.core.listener.AbstractListenerFactoryBean; |
7 | import org.springframework.batch.core.listener.ListenerMetaData; |
8 | import org.springframework.beans.factory.support.AbstractBeanDefinition; |
9 | import org.springframework.beans.factory.support.BeanDefinitionBuilder; |
10 | import org.springframework.beans.factory.support.ManagedMap; |
11 | import org.springframework.beans.factory.support.RootBeanDefinition; |
12 | import org.springframework.beans.factory.xml.ParserContext; |
13 | import org.springframework.util.StringUtils; |
14 | import org.w3c.dom.Element; |
15 | import org.w3c.dom.NamedNodeMap; |
16 | |
17 | /** |
18 | * @author Dan Garrette |
19 | * @since 2.0 |
20 | * @see StepListenerParser |
21 | * @see JobExecutionListenerParser |
22 | */ |
23 | public abstract class AbstractListenerParser { |
24 | |
25 | private static final String ID_ATTR = "id"; |
26 | |
27 | private static final String REF_ATTR = "ref"; |
28 | |
29 | private static final String CLASS_ATTR = "class"; |
30 | |
31 | public AbstractBeanDefinition parse(Element element, ParserContext parserContext) { |
32 | BeanDefinitionBuilder builder = BeanDefinitionBuilder.genericBeanDefinition(getBeanClass()); |
33 | doParse(element, parserContext, builder); |
34 | return builder.getBeanDefinition(); |
35 | } |
36 | |
37 | @SuppressWarnings("unchecked") |
38 | public void doParse(Element element, ParserContext parserContext, BeanDefinitionBuilder builder) { |
39 | String id = element.getAttribute(ID_ATTR); |
40 | String listenerRef = element.getAttribute(REF_ATTR); |
41 | String className = element.getAttribute(CLASS_ATTR); |
42 | checkListenerElementAttributes(parserContext, element, id, listenerRef, className); |
43 | |
44 | if (StringUtils.hasText(listenerRef)) { |
45 | builder.addPropertyReference("delegate", listenerRef); |
46 | } |
47 | else if (StringUtils.hasText(className)) { |
48 | RootBeanDefinition beanDef = new RootBeanDefinition(className, null, null); |
49 | builder.addPropertyValue("delegate", beanDef); |
50 | } |
51 | else { |
52 | parserContext.getReaderContext().error( |
53 | "Neither '" + REF_ATTR + "' or '" + CLASS_ATTR + "' specified for <" + element.getTagName() |
54 | + "> element", element); |
55 | } |
56 | |
57 | ManagedMap metaDataMap = new ManagedMap(); |
58 | for (String metaDataPropertyName : getMethodNameAttributes()) { |
59 | String listenerMethod = element.getAttribute(metaDataPropertyName); |
60 | if (StringUtils.hasText(listenerMethod)) { |
61 | metaDataMap.put(metaDataPropertyName, listenerMethod); |
62 | } |
63 | } |
64 | builder.addPropertyValue("metaDataMap", metaDataMap); |
65 | } |
66 | |
67 | private void checkListenerElementAttributes(ParserContext parserContext, Element element, String id, |
68 | String listenerRef, String className) { |
69 | if (StringUtils.hasText(className) && StringUtils.hasText(listenerRef)) { |
70 | NamedNodeMap attributeNodes = element.getAttributes(); |
71 | StringBuilder attributes = new StringBuilder(); |
72 | for (int i = 0; i < attributeNodes.getLength(); i++) { |
73 | if (i > 0) { |
74 | attributes.append(" "); |
75 | } |
76 | attributes.append(attributeNodes.item(i)); |
77 | } |
78 | parserContext.getReaderContext().error( |
79 | "Either '" + REF_ATTR + "' or '" + CLASS_ATTR + "' may be specified, but not both; <" |
80 | + element.getTagName() + "> element specified with attributes: " + attributes, element); |
81 | } |
82 | } |
83 | |
84 | private List<String> getMethodNameAttributes() { |
85 | List<String> methodNameAttributes = new ArrayList<String>(); |
86 | for (ListenerMetaData metaData : getMetaDataValues()) { |
87 | methodNameAttributes.add(metaData.getMethodName()); |
88 | } |
89 | return methodNameAttributes; |
90 | } |
91 | |
92 | protected abstract Class<? extends AbstractListenerFactoryBean> getBeanClass(); |
93 | |
94 | protected abstract ListenerMetaData[] getMetaDataValues(); |
95 | |
96 | } |