| 1 | /* |
| 2 | * Copyright 2012 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.item.data; |
| 17 | |
| 18 | import java.lang.reflect.InvocationTargetException; |
| 19 | import java.util.List; |
| 20 | |
| 21 | import org.apache.commons.logging.Log; |
| 22 | import org.apache.commons.logging.LogFactory; |
| 23 | import org.springframework.batch.item.ItemWriter; |
| 24 | import org.springframework.batch.item.adapter.AbstractMethodInvokingDelegator.InvocationTargetThrowableWrapper; |
| 25 | import org.springframework.batch.item.adapter.DynamicMethodInvocationException; |
| 26 | import org.springframework.beans.factory.InitializingBean; |
| 27 | import org.springframework.data.repository.CrudRepository; |
| 28 | import org.springframework.util.Assert; |
| 29 | import org.springframework.util.CollectionUtils; |
| 30 | import org.springframework.util.MethodInvoker; |
| 31 | |
| 32 | /** |
| 33 | * <p> |
| 34 | * A {@link org.springframework.batch.item.ItemReader} wrapper for a |
| 35 | * {@link org.springframework.data.repository.CrudRepository} from Spring Data. |
| 36 | * </p> |
| 37 | * |
| 38 | * <p> |
| 39 | * It depends on {@link org.springframework.data.repository.CrudRepository#save(Iterable)} |
| 40 | * method to store the items for the chunk. Performance will be determined by that |
| 41 | * implementation more than this writer. |
| 42 | * </p> |
| 43 | * |
| 44 | * <p> |
| 45 | * As long as the repository provided is thread-safe, this writer is also thread-safe once |
| 46 | * properties are set (normal singleton behavior), so it can be used in multiple concurrent |
| 47 | * transactions. |
| 48 | * </p> |
| 49 | * |
| 50 | * @author Michael Minella |
| 51 | * @since 2.2 |
| 52 | */ |
| 53 | @SuppressWarnings("rawtypes") |
| 54 | public class RepositoryItemWriter implements ItemWriter, InitializingBean { |
| 55 | |
| 56 | protected static final Log logger = LogFactory.getLog(RepositoryItemWriter.class); |
| 57 | |
| 58 | private CrudRepository repository; |
| 59 | |
| 60 | private String methodName; |
| 61 | |
| 62 | /** |
| 63 | * Specifies what method on the repository to call. This method must the type of |
| 64 | * object passed to this writer as the <em>sole</em> argument. |
| 65 | * |
| 66 | * @param methodName |
| 67 | */ |
| 68 | public void setMethodName(String methodName) { |
| 69 | this.methodName = methodName; |
| 70 | } |
| 71 | |
| 72 | /** |
| 73 | * Set the {@link org.springframework.data.repository.CrudRepository} implementation |
| 74 | * for persistence |
| 75 | * |
| 76 | * @param repository the Spring Data repository to be set |
| 77 | */ |
| 78 | public void setRepository(CrudRepository repository) { |
| 79 | this.repository = repository; |
| 80 | } |
| 81 | |
| 82 | /** |
| 83 | * Write all items to the data store via a Spring Data repository. |
| 84 | * |
| 85 | * @see org.springframework.batch.item.ItemWriter#write(java.util.List) |
| 86 | */ |
| 87 | @Override |
| 88 | public void write(List items) throws Exception { |
| 89 | if(!CollectionUtils.isEmpty(items)) { |
| 90 | doWrite(items); |
| 91 | } |
| 92 | } |
| 93 | |
| 94 | /** |
| 95 | * Performs the actual write to the repository. This can be overriden by |
| 96 | * a subclass if necessary. |
| 97 | * |
| 98 | * @param items the list of items to be persisted. |
| 99 | */ |
| 100 | protected void doWrite(List items) throws Exception { |
| 101 | if (logger.isDebugEnabled()) { |
| 102 | logger.debug("Writing to the repository with " + items.size() + " items."); |
| 103 | } |
| 104 | |
| 105 | MethodInvoker invoker = createMethodInvoker(repository, methodName); |
| 106 | |
| 107 | for (Object object : items) { |
| 108 | invoker.setArguments(new Object [] {object}); |
| 109 | doInvoke(invoker); |
| 110 | } |
| 111 | } |
| 112 | |
| 113 | /** |
| 114 | * Check mandatory properties - there must be a repository. |
| 115 | */ |
| 116 | @Override |
| 117 | public void afterPropertiesSet() throws Exception { |
| 118 | Assert.state(repository != null, "A CrudRepository implementation is required"); |
| 119 | } |
| 120 | |
| 121 | |
| 122 | private Object doInvoke(MethodInvoker invoker) throws Exception{ |
| 123 | try { |
| 124 | invoker.prepare(); |
| 125 | } |
| 126 | catch (ClassNotFoundException e) { |
| 127 | throw new DynamicMethodInvocationException(e); |
| 128 | } |
| 129 | catch (NoSuchMethodException e) { |
| 130 | throw new DynamicMethodInvocationException(e); |
| 131 | } |
| 132 | |
| 133 | try { |
| 134 | return invoker.invoke(); |
| 135 | } |
| 136 | catch (InvocationTargetException e) { |
| 137 | if (e.getCause() instanceof Exception) { |
| 138 | throw (Exception) e.getCause(); |
| 139 | } |
| 140 | else { |
| 141 | throw new InvocationTargetThrowableWrapper(e.getCause()); |
| 142 | } |
| 143 | } |
| 144 | catch (IllegalAccessException e) { |
| 145 | throw new DynamicMethodInvocationException(e); |
| 146 | } |
| 147 | } |
| 148 | |
| 149 | private MethodInvoker createMethodInvoker(Object targetObject, String targetMethod) { |
| 150 | MethodInvoker invoker = new MethodInvoker(); |
| 151 | invoker.setTargetObject(targetObject); |
| 152 | invoker.setTargetMethod(targetMethod); |
| 153 | return invoker; |
| 154 | } |
| 155 | } |