001/* 002 * Copyright 2017 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 */ 016 017package org.springframework.batch.item.support.builder; 018 019import org.springframework.batch.item.support.ScriptItemProcessor; 020import org.springframework.core.io.Resource; 021import org.springframework.util.Assert; 022import org.springframework.util.StringUtils; 023 024/** 025 * Creates a fully qualified ScriptItemProcessor. 026 * 027 * @author Glenn Renfro 028 * 029 * @since 4.0 030 */ 031public class ScriptItemProcessorBuilder<I, O> { 032 033 private String language; 034 035 private Resource scriptResource; 036 037 private String scriptSource; 038 039 private String itemBindingVariableName; 040 041 /** 042 * Sets the {@link org.springframework.core.io.Resource} location of the script to 043 * use. The script language will be deduced from the filename extension. 044 * 045 * @param resource the {@link org.springframework.core.io.Resource} location of the 046 * script to use. 047 * @return this instance for method chaining 048 * @see ScriptItemProcessor#setScript(Resource) 049 * 050 */ 051 public ScriptItemProcessorBuilder<I, O> scriptResource(Resource resource) { 052 this.scriptResource = resource; 053 054 return this; 055 } 056 057 /** 058 * Establishes the language of the script. 059 * 060 * @param language the language of the script. 061 * @return this instance for method chaining 062 * @see ScriptItemProcessor#setScriptSource(String, String) 063 */ 064 public ScriptItemProcessorBuilder<I, O> language(String language) { 065 this.language = language; 066 067 return this; 068 } 069 070 /** 071 * Sets the provided {@link String} as the script source code to use. Language must 072 * not be null nor empty when using script. 073 * 074 * @param scriptSource the {@link String} form of the script source code to use. 075 * @return this instance for method chaining 076 * @see ScriptItemProcessor#setScriptSource(String, String) 077 */ 078 public ScriptItemProcessorBuilder<I, O> scriptSource(String scriptSource) { 079 this.scriptSource = scriptSource; 080 081 return this; 082 } 083 084 /** 085 * Provides the ability to change the key name that scripts use to obtain the current 086 * item to process if the variable represented by: 087 * {@link ScriptItemProcessor#ITEM_BINDING_VARIABLE_NAME} 088 * is not suitable ("item"). 089 * 090 * @param itemBindingVariableName the desired binding variable name 091 * @return this instance for method chaining 092 * @see ScriptItemProcessor#setItemBindingVariableName(String) 093 */ 094 public ScriptItemProcessorBuilder<I, O> itemBindingVariableName(String itemBindingVariableName) { 095 this.itemBindingVariableName = itemBindingVariableName; 096 097 return this; 098 } 099 100 /** 101 * Returns a fully constructed {@link ScriptItemProcessor}. 102 * 103 * @return a new {@link ScriptItemProcessor} 104 */ 105 public ScriptItemProcessor<I, O> build() { 106 if (this.scriptResource == null && !StringUtils.hasText(this.scriptSource)) { 107 throw new IllegalArgumentException("scriptResource or scriptSource is required."); 108 } 109 110 if (StringUtils.hasText(this.scriptSource)) { 111 Assert.hasText(this.language, "language is required when using scriptSource."); 112 } 113 114 ScriptItemProcessor<I, O> processor = new ScriptItemProcessor<>(); 115 if (StringUtils.hasText(this.itemBindingVariableName)) { 116 processor.setItemBindingVariableName(this.itemBindingVariableName); 117 } 118 119 if (this.scriptResource != null) { 120 processor.setScript(this.scriptResource); 121 } 122 123 if (this.scriptSource != null) { 124 processor.setScriptSource(this.scriptSource, this.language); 125 } 126 127 return processor; 128 } 129}