001/* 002 * Copyright 2013 the original author or authors. 003 * 004 * Licensed under the Apache License, Version 2.0 (the "License"); 005 * you may not use this file except in compliance with the License. 006 * You may obtain a copy of the License at 007 * 008 * https://www.apache.org/licenses/LICENSE-2.0 009 * 010 * Unless required by applicable law or agreed to in writing, software 011 * distributed under the License is distributed on an "AS IS" BASIS, 012 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 013 * See the License for the specific language governing permissions and 014 * limitations under the License. 015 */ 016package org.springframework.batch.core.jsr.configuration.xml; 017 018import java.util.Properties; 019import org.springframework.beans.factory.config.BeanDefinition; 020import org.springframework.beans.factory.support.BeanDefinitionBuilder; 021import org.springframework.beans.factory.xml.XmlBeanDefinitionReader; 022import org.springframework.context.support.GenericApplicationContext; 023import org.springframework.core.io.Resource; 024 025/** 026 * <p> 027 * {@link GenericApplicationContext} implementation providing JSR-352 related context operations. 028 * </p> 029 * 030 * @author Chris Schaefer 031 * @since 3.0 032 */ 033public class JsrXmlApplicationContext extends GenericApplicationContext { 034 private static final String JOB_PARAMETERS_BEAN_DEFINITION_NAME = "jsr_jobParameters"; 035 036 private XmlBeanDefinitionReader reader = new XmlBeanDefinitionReader(this); 037 038 /** 039 * <p> 040 * Create a new context instance with no job parameters. 041 * </p> 042 */ 043 public JsrXmlApplicationContext() { 044 reader.setDocumentReaderClass(JsrBeanDefinitionDocumentReader.class); 045 reader.setEnvironment(this.getEnvironment()); 046 } 047 048 /** 049 * <p> 050 * Create a new context instance using the provided {@link Properties} representing job 051 * parameters when pre-processing the job definition document. 052 * </p> 053 * 054 * @param jobParameters the {@link Properties} representing job parameters 055 */ 056 public JsrXmlApplicationContext(Properties jobParameters) { 057 reader.setDocumentReaderClass(JsrBeanDefinitionDocumentReader.class); 058 reader.setEnvironment(this.getEnvironment()); 059 060 storeJobParameters(jobParameters); 061 } 062 063 private void storeJobParameters(Properties properties) { 064 BeanDefinition jobParameters = BeanDefinitionBuilder.genericBeanDefinition(Properties.class).getBeanDefinition(); 065 jobParameters.getConstructorArgumentValues().addGenericArgumentValue(properties != null ? properties : new Properties()); 066 067 reader.getRegistry().registerBeanDefinition(JOB_PARAMETERS_BEAN_DEFINITION_NAME, jobParameters); 068 } 069 070 protected XmlBeanDefinitionReader getReader() { 071 return reader; 072 } 073 074 /** 075 * Set whether to use XML validation. Default is <code>true</code>. 076 * 077 * @param validating true if XML should be validated. 078 */ 079 public void setValidating(boolean validating) { 080 this.reader.setValidating(validating); 081 } 082 083 /** 084 * Load bean definitions from the given XML resources. 085 * @param resources one or more resources to load from 086 */ 087 public void load(Resource... resources) { 088 this.reader.loadBeanDefinitions(resources); 089 } 090}