org.springframework.batch.core.jsr.configuration.xml.PropertyParser Maven / Gradle / Ivy
/*
* Copyright 2013-2014 the original author or authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* https://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.springframework.batch.core.jsr.configuration.xml;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Properties;
import org.springframework.batch.core.jsr.configuration.support.BatchArtifactType;
import org.springframework.beans.factory.config.BeanDefinition;
import org.springframework.beans.factory.support.ManagedMap;
import org.springframework.beans.factory.xml.ParserContext;
import org.springframework.util.xml.DomUtils;
import org.w3c.dom.Element;
/**
*
* Parser for the <properties /> element defined by JSR-352.
*
*
* @author Chris Schaefer
* @since 3.0
*/
public class PropertyParser {
private static final String PROPERTY_ELEMENT = "property";
private static final String PROPERTIES_ELEMENT = "properties";
private static final String PROPERTY_NAME_ATTRIBUTE = "name";
private static final String PROPERTY_VALUE_ATTRIBUTE = "value";
private static final String JOB_PROPERTIES_BEAN_NAME = "jobProperties";
private static final String BATCH_PROPERTY_CONTEXT_BEAN_NAME = "batchPropertyContext";
private static final String JOB_PROPERTIES_PROPERTY_NAME = "jobProperties";
private static final String STEP_PROPERTIES_PROPERTY_NAME = "stepProperties";
private static final String ARTIFACT_PROPERTIES_PROPERTY_NAME = "artifactProperties";
private static final String STEP_ARTIFACT_PROPERTIES_PROPERTY_NAME = "stepArtifactProperties";
private String beanName;
private String stepName;
private ParserContext parserContext;
private BatchArtifactType batchArtifactType;
public PropertyParser(String beanName, ParserContext parserContext, BatchArtifactType batchArtifactType) {
this.beanName = beanName;
this.parserContext = parserContext;
this.batchArtifactType = batchArtifactType;
}
public PropertyParser(String beanName, ParserContext parserContext, BatchArtifactType batchArtifactType, String stepName) {
this(beanName, parserContext, batchArtifactType);
this.stepName = stepName;
}
/**
*
* Parses <property> tag values from the provided {@link Element} if it contains a <properties /> element.
* Only one <properties /> element may be present. <property> elements have a name and value attribute
* which represent the property entries key and value.
*
*
* @param element the element to parse looking for <properties />
*/
public void parseProperties(Element element) {
List propertiesElements = DomUtils.getChildElementsByTagName(element, PROPERTIES_ELEMENT);
if (propertiesElements.size() == 1) {
parsePropertyElement(propertiesElements.get(0));
} else if (propertiesElements.size() > 1) {
parserContext.getReaderContext().error("The element may not appear more than once.", element);
}
}
/**
*
* Parses a <property> tag value from the provided {@link Element}. <property> elements have a name and
* value attribute which represent the property entries key and value.
*
*
* @param element the element to parse looking for <property/>
*/
public void parseProperty(Element element) {
parsePropertyElement(element);
}
private void parsePropertyElement(Element propertyElement) {
Properties properties = new Properties();
for (Element element : DomUtils.getChildElementsByTagName(propertyElement, PROPERTY_ELEMENT)) {
properties.put(element.getAttribute(PROPERTY_NAME_ATTRIBUTE), element.getAttribute(PROPERTY_VALUE_ATTRIBUTE));
}
setProperties(properties);
setJobPropertiesBean(properties);
}
private void setProperties(Properties properties) {
Object propertyValue;
BeanDefinition beanDefinition = parserContext.getRegistry().getBeanDefinition(BATCH_PROPERTY_CONTEXT_BEAN_NAME);
if(batchArtifactType.equals(BatchArtifactType.JOB)) {
propertyValue = getJobProperties(properties);
} else if (batchArtifactType.equals(BatchArtifactType.STEP)) {
propertyValue = getProperties(stepName, properties);
} else if (batchArtifactType.equals(BatchArtifactType.ARTIFACT)) {
propertyValue = getProperties(beanName, properties);
} else if (batchArtifactType.equals(BatchArtifactType.STEP_ARTIFACT)) {
propertyValue = getStepArtifactProperties(beanDefinition, properties);
} else {
throw new IllegalStateException("Unhandled BatchArtifactType of: " + batchArtifactType);
}
beanDefinition.getPropertyValues().addPropertyValue(getPropertyName(batchArtifactType), propertyValue);
}
private Map getProperties(String keyName, Properties properties) {
ManagedMap stepProperties = new ManagedMap<>();
stepProperties.setMergeEnabled(true);
stepProperties.put(keyName, properties);
return stepProperties;
}
private Properties getJobProperties(Properties properties) {
return properties;
}
@SuppressWarnings("unchecked")
private Map> getStepArtifactProperties(BeanDefinition beanDefinition, Properties properties) {
ManagedMap> stepArtifacts = new ManagedMap<>();
stepArtifacts.setMergeEnabled(true);
Map> existingArtifacts
= (Map>) beanDefinition.getPropertyValues().get(getPropertyName(batchArtifactType));
ManagedMap artifactProperties = new ManagedMap<>();
artifactProperties.setMergeEnabled(true);
if(existingArtifacts != null && existingArtifacts.containsKey(stepName)) {
Map existingArtifactsMap = existingArtifacts.get(stepName);
for(Map.Entry existingArtifactEntry : existingArtifactsMap.entrySet()) {
artifactProperties.put(existingArtifactEntry.getKey(), existingArtifactEntry.getValue());
}
}
artifactProperties.put(beanName, properties);
stepArtifacts.put(stepName, artifactProperties);
return stepArtifacts;
}
private void setJobPropertiesBean(Properties properties) {
if (batchArtifactType.equals(BatchArtifactType.JOB)) {
Map jobProperties = new HashMap<>();
if (properties != null && !properties.isEmpty()) {
for (String param : properties.stringPropertyNames()) {
jobProperties.put(param, properties.getProperty(param));
}
}
BeanDefinition jobPropertiesBeanDefinition = parserContext.getRegistry().getBeanDefinition(JOB_PROPERTIES_BEAN_NAME);
jobPropertiesBeanDefinition.getConstructorArgumentValues().addGenericArgumentValue(jobProperties);
}
}
private String getPropertyName(BatchArtifactType batchArtifactType) {
if(batchArtifactType.equals(BatchArtifactType.JOB)) {
return JOB_PROPERTIES_PROPERTY_NAME;
} else if (batchArtifactType.equals(BatchArtifactType.STEP)) {
return STEP_PROPERTIES_PROPERTY_NAME;
} else if (batchArtifactType.equals(BatchArtifactType.ARTIFACT)) {
return ARTIFACT_PROPERTIES_PROPERTY_NAME;
} else if (batchArtifactType.equals(BatchArtifactType.STEP_ARTIFACT)) {
return STEP_ARTIFACT_PROPERTIES_PROPERTY_NAME;
} else {
throw new IllegalStateException("Unhandled BatchArtifactType of: " + batchArtifactType);
}
}
}