| 1 | /* |
| 2 | * Copyright 2006-2007 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 | * http://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 | package org.springframework.batch.core.step.item; |
| 17 | |
| 18 | import org.springframework.batch.core.StepContribution; |
| 19 | import org.springframework.batch.item.ClearFailedException; |
| 20 | import org.springframework.batch.item.FlushFailedException; |
| 21 | import org.springframework.batch.item.ItemReader; |
| 22 | import org.springframework.batch.item.ItemWriter; |
| 23 | import org.springframework.batch.item.MarkFailedException; |
| 24 | import org.springframework.batch.item.ResetFailedException; |
| 25 | import org.springframework.batch.repeat.ExitStatus; |
| 26 | |
| 27 | /** |
| 28 | * Simplest possible implementation of {@link ItemHandler} with no skipping or |
| 29 | * recovering. Just delegates all calls to the provided {@link ItemReader} and |
| 30 | * {@link ItemWriter}. |
| 31 | * |
| 32 | * Provides extension points by protected {@link #read(StepContribution)} and |
| 33 | * {@link #write(Object, StepContribution)} methods that can be overriden to |
| 34 | * provide more sophisticated behavior (e.g. skipping). |
| 35 | * |
| 36 | * @author Dave Syer |
| 37 | * @author Robert Kasanicky |
| 38 | */ |
| 39 | public class SimpleItemHandler implements ItemHandler { |
| 40 | |
| 41 | private ItemReader itemReader; |
| 42 | |
| 43 | private ItemWriter itemWriter; |
| 44 | |
| 45 | /** |
| 46 | * @param itemReader |
| 47 | * @param itemWriter |
| 48 | */ |
| 49 | public SimpleItemHandler(ItemReader itemReader, ItemWriter itemWriter) { |
| 50 | super(); |
| 51 | this.itemReader = itemReader; |
| 52 | this.itemWriter = itemWriter; |
| 53 | } |
| 54 | |
| 55 | /** |
| 56 | * Get the next item from {@link #read(StepContribution)} and if not null |
| 57 | * pass the item to {@link #write(Object, StepContribution)}. |
| 58 | * |
| 59 | * @see org.springframework.batch.core.step.item.ItemHandler#handle(org.springframework.batch.core.StepContribution) |
| 60 | */ |
| 61 | public ExitStatus handle(StepContribution contribution) throws Exception { |
| 62 | Object item = read(contribution); |
| 63 | if (item == null) { |
| 64 | return ExitStatus.FINISHED; |
| 65 | } |
| 66 | write(item, contribution); |
| 67 | return ExitStatus.CONTINUABLE; |
| 68 | } |
| 69 | |
| 70 | /** |
| 71 | * @param contribution current context |
| 72 | * @return next item for writing |
| 73 | */ |
| 74 | protected Object read(StepContribution contribution) throws Exception { |
| 75 | return doRead(); |
| 76 | } |
| 77 | |
| 78 | /** |
| 79 | * @return item |
| 80 | * @throws Exception |
| 81 | */ |
| 82 | protected final Object doRead() throws Exception { |
| 83 | return itemReader.read(); |
| 84 | } |
| 85 | |
| 86 | /** |
| 87 | * |
| 88 | * @param item the item to write |
| 89 | * @param contribution current context |
| 90 | */ |
| 91 | protected void write(Object item, StepContribution contribution) throws Exception { |
| 92 | contribution.incrementItemCount(); |
| 93 | doWrite(item); |
| 94 | } |
| 95 | |
| 96 | /** |
| 97 | * @param item |
| 98 | * @throws Exception |
| 99 | */ |
| 100 | protected final void doWrite(Object item) throws Exception { |
| 101 | itemWriter.write(item); |
| 102 | } |
| 103 | |
| 104 | /** |
| 105 | * @throws MarkFailedException |
| 106 | * @see org.springframework.batch.item.ItemReader#mark() |
| 107 | */ |
| 108 | public void mark() throws MarkFailedException { |
| 109 | itemReader.mark(); |
| 110 | } |
| 111 | |
| 112 | /** |
| 113 | * @throws ResetFailedException |
| 114 | * @see org.springframework.batch.item.ItemReader#reset() |
| 115 | */ |
| 116 | public void reset() throws ResetFailedException { |
| 117 | itemReader.reset(); |
| 118 | } |
| 119 | |
| 120 | /** |
| 121 | * @throws ClearFailedException |
| 122 | * @see org.springframework.batch.item.ItemWriter#clear() |
| 123 | */ |
| 124 | public void clear() throws ClearFailedException { |
| 125 | itemWriter.clear(); |
| 126 | } |
| 127 | |
| 128 | /** |
| 129 | * @throws FlushFailedException |
| 130 | * @see org.springframework.batch.item.ItemWriter#flush() |
| 131 | */ |
| 132 | public void flush() throws FlushFailedException { |
| 133 | itemWriter.flush(); |
| 134 | } |
| 135 | |
| 136 | } |