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