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.jsr.item; 017 018import java.io.Serializable; 019import java.util.List; 020 021import javax.batch.api.chunk.ItemWriter; 022 023import org.springframework.util.Assert; 024import org.springframework.util.ClassUtils; 025 026/** 027 * Adapter that wraps an {@link ItemWriter} for use by Spring Batch. All calls are delegated as appropriate 028 * to the corresponding method on the delegate. 029 * 030 * @author Michael Minella 031 * @since 3.0 032 */ 033public class ItemWriterAdapter<T> extends CheckpointSupport implements org.springframework.batch.item.ItemWriter<T> { 034 035 private static final String CHECKPOINT_KEY = "writer.checkpoint"; 036 037 private ItemWriter delegate; 038 039 /** 040 * @param writer a {@link ItemWriter} to delegate calls to 041 */ 042 public ItemWriterAdapter(ItemWriter writer) { 043 super(CHECKPOINT_KEY); 044 Assert.notNull(writer, "An ItemWriter implementation is required"); 045 this.delegate = writer; 046 super.setExecutionContextName(ClassUtils.getShortName(delegate.getClass())); 047 } 048 049 /* (non-Javadoc) 050 * @see org.springframework.batch.item.ItemWriter#write(java.util.List) 051 */ 052 @SuppressWarnings("unchecked") 053 @Override 054 public void write(List<? extends T> items) throws Exception { 055 delegate.writeItems((List<Object>) items); 056 } 057 058 /* (non-Javadoc) 059 * @see org.springframework.batch.jsr.item.CheckpointSupport#doOpen(java.io.Serializable) 060 */ 061 @Override 062 protected void doOpen(Serializable checkpoint) throws Exception { 063 delegate.open(checkpoint); 064 } 065 066 /* (non-Javadoc) 067 * @see org.springframework.batch.jsr.item.CheckpointSupport#doCheckpoint() 068 */ 069 @Override 070 protected Serializable doCheckpoint() throws Exception { 071 Serializable checkpointInfo = delegate.checkpointInfo(); 072 return checkpointInfo; 073 } 074 075 /* (non-Javadoc) 076 * @see org.springframework.batch.jsr.item.CheckpointSupport#doClose() 077 */ 078 @Override 079 protected void doClose() throws Exception{ 080 delegate.close(); 081 } 082}