|
| 1 | +/* |
| 2 | + * Copyright 2019-2020 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 | + * https://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 | + |
| 17 | +package org.springframework.cloud.task.batch.autoconfigure; |
| 18 | + |
| 19 | +import java.util.Map; |
| 20 | + |
| 21 | +import org.springframework.batch.item.file.FlatFileItemReader; |
| 22 | +import org.springframework.batch.item.file.LineCallbackHandler; |
| 23 | +import org.springframework.batch.item.file.LineMapper; |
| 24 | +import org.springframework.batch.item.file.builder.FlatFileItemReaderBuilder; |
| 25 | +import org.springframework.batch.item.file.mapping.FieldSetMapper; |
| 26 | +import org.springframework.batch.item.file.separator.RecordSeparatorPolicy; |
| 27 | +import org.springframework.batch.item.file.transform.FieldSet; |
| 28 | +import org.springframework.batch.item.file.transform.LineTokenizer; |
| 29 | +import org.springframework.batch.item.file.transform.Range; |
| 30 | +import org.springframework.beans.factory.annotation.Autowired; |
| 31 | +import org.springframework.boot.autoconfigure.AutoConfigureAfter; |
| 32 | +import org.springframework.boot.autoconfigure.batch.BatchAutoConfiguration; |
| 33 | +import org.springframework.boot.autoconfigure.condition.ConditionalOnMissingBean; |
| 34 | +import org.springframework.boot.autoconfigure.condition.ConditionalOnProperty; |
| 35 | +import org.springframework.boot.context.properties.EnableConfigurationProperties; |
| 36 | +import org.springframework.context.annotation.Bean; |
| 37 | +import org.springframework.context.annotation.Configuration; |
| 38 | + |
| 39 | +/** |
| 40 | + * Autconfiguration for a {@code FlatFileItemReader}. |
| 41 | + * |
| 42 | + * @author Michael Minella |
| 43 | + * @since 2.3 |
| 44 | + */ |
| 45 | +@Configuration |
| 46 | +@EnableConfigurationProperties(FlatFileItemReaderProperties.class) |
| 47 | +@AutoConfigureAfter(BatchAutoConfiguration.class) |
| 48 | +public class FlatFileItemReaderAutoConfiguration { |
| 49 | + |
| 50 | + private final FlatFileItemReaderProperties properties; |
| 51 | + |
| 52 | + @Autowired(required = false) |
| 53 | + private LineTokenizer lineTokenizer; |
| 54 | + |
| 55 | + @Autowired(required = false) |
| 56 | + private FieldSetMapper<Map<Object, Object>> fieldSetMapper; |
| 57 | + |
| 58 | + @Autowired(required = false) |
| 59 | + private LineMapper<Map<Object, Object>> lineMapper; |
| 60 | + |
| 61 | + @Autowired(required = false) |
| 62 | + private LineCallbackHandler skippedLinesCallback; |
| 63 | + |
| 64 | + @Autowired(required = false) |
| 65 | + private RecordSeparatorPolicy recordSeparatorPolicy; |
| 66 | + |
| 67 | + public FlatFileItemReaderAutoConfiguration(FlatFileItemReaderProperties properties) { |
| 68 | + this.properties = properties; |
| 69 | + } |
| 70 | + |
| 71 | + @Bean |
| 72 | + @ConditionalOnMissingBean |
| 73 | + @ConditionalOnProperty(prefix = "spring.batch.job.flatfilereader", name = "name") |
| 74 | + public FlatFileItemReader<Map<Object, Object>> itemReader() { |
| 75 | + FlatFileItemReaderBuilder<Map<Object, Object>> mapFlatFileItemReaderBuilder = new FlatFileItemReaderBuilder<Map<Object, Object>>() |
| 76 | + .name(this.properties.getName()).resource(this.properties.getResource()) |
| 77 | + .saveState(this.properties.isSaveState()) |
| 78 | + .maxItemCount(this.properties.getMaxItemCount()) |
| 79 | + .currentItemCount(this.properties.getCurrentItemCount()) |
| 80 | + .strict(this.properties.isStrict()) |
| 81 | + .encoding(this.properties.getEncoding()) |
| 82 | + .linesToSkip(this.properties.getLinesToSkip()) |
| 83 | + .comments(this.properties.getComments() |
| 84 | + .toArray(new String[this.properties.getComments().size()])); |
| 85 | + |
| 86 | + if (this.lineTokenizer != null) { |
| 87 | + mapFlatFileItemReaderBuilder.lineTokenizer(this.lineTokenizer); |
| 88 | + } |
| 89 | + |
| 90 | + if (this.recordSeparatorPolicy != null) { |
| 91 | + mapFlatFileItemReaderBuilder |
| 92 | + .recordSeparatorPolicy(this.recordSeparatorPolicy); |
| 93 | + } |
| 94 | + |
| 95 | + if (this.fieldSetMapper != null) { |
| 96 | + mapFlatFileItemReaderBuilder.fieldSetMapper(this.fieldSetMapper); |
| 97 | + } |
| 98 | + |
| 99 | + if (this.lineMapper != null) { |
| 100 | + mapFlatFileItemReaderBuilder.lineMapper(this.lineMapper); |
| 101 | + } |
| 102 | + |
| 103 | + if (this.skippedLinesCallback != null) { |
| 104 | + mapFlatFileItemReaderBuilder.skippedLinesCallback(skippedLinesCallback); |
| 105 | + } |
| 106 | + |
| 107 | + if (this.properties.isDelimited()) { |
| 108 | + mapFlatFileItemReaderBuilder.delimited() |
| 109 | + .quoteCharacter(this.properties.getQuoteCharacter()) |
| 110 | + .delimiter(this.properties.getDelimiter()) |
| 111 | + .includedFields( |
| 112 | + this.properties.getIncludedFields().toArray(new Integer[0])) |
| 113 | + .names(this.properties.getNames()) |
| 114 | + .beanMapperStrict(this.properties.isParsingStrict()) |
| 115 | + .fieldSetMapper(new MapFieldSetMapper()); |
| 116 | + } |
| 117 | + else if (this.properties.isFixedLength()) { |
| 118 | + mapFlatFileItemReaderBuilder.fixedLength() |
| 119 | + .columns(this.properties.getRanges().toArray(new Range[0])) |
| 120 | + .names(this.properties.getNames()) |
| 121 | + .fieldSetMapper(new MapFieldSetMapper()) |
| 122 | + .beanMapperStrict(this.properties.isParsingStrict()); |
| 123 | + } |
| 124 | + |
| 125 | + return mapFlatFileItemReaderBuilder.build(); |
| 126 | + } |
| 127 | + |
| 128 | + /** |
| 129 | + * A {@link FieldSetMapper} that takes a {@code FieldSet} and returns the |
| 130 | + * {@code Map<Object, Object>} of its contents. |
| 131 | + */ |
| 132 | + public static class MapFieldSetMapper implements FieldSetMapper<Map<Object, Object>> { |
| 133 | + |
| 134 | + @Override |
| 135 | + public Map<Object, Object> mapFieldSet(FieldSet fieldSet) { |
| 136 | + return fieldSet.getProperties(); |
| 137 | + } |
| 138 | + |
| 139 | + } |
| 140 | + |
| 141 | +} |
0 commit comments