| 1 | /* |
| 2 | * Copyright 2006-2013 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 | |
| 17 | package org.springframework.batch.core.partition.support; |
| 18 | |
| 19 | import java.io.IOException; |
| 20 | import java.util.HashMap; |
| 21 | import java.util.Map; |
| 22 | |
| 23 | import org.springframework.batch.item.ExecutionContext; |
| 24 | import org.springframework.core.io.Resource; |
| 25 | import org.springframework.util.Assert; |
| 26 | |
| 27 | /** |
| 28 | * Implementation of {@link Partitioner} that locates multiple resources and |
| 29 | * associates their file names with execution context keys. Creates an |
| 30 | * {@link ExecutionContext} per resource, and labels them as |
| 31 | * <code>{partition0, partition1, ..., partitionN}</code>. The grid size is |
| 32 | * ignored. |
| 33 | * |
| 34 | * @author Dave Syer |
| 35 | * @since 2.0 |
| 36 | */ |
| 37 | public class MultiResourcePartitioner implements Partitioner { |
| 38 | |
| 39 | private static final String DEFAULT_KEY_NAME = "fileName"; |
| 40 | |
| 41 | private static final String PARTITION_KEY = "partition"; |
| 42 | |
| 43 | private Resource[] resources = new Resource[0]; |
| 44 | |
| 45 | private String keyName = DEFAULT_KEY_NAME; |
| 46 | |
| 47 | /** |
| 48 | * The resources to assign to each partition. In Spring configuration you |
| 49 | * can use a pattern to select multiple resources. |
| 50 | * @param resources the resources to use |
| 51 | */ |
| 52 | public void setResources(Resource[] resources) { |
| 53 | this.resources = resources; |
| 54 | } |
| 55 | |
| 56 | /** |
| 57 | * The name of the key for the file name in each {@link ExecutionContext}. |
| 58 | * Defaults to "fileName". |
| 59 | * @param keyName the value of the key |
| 60 | */ |
| 61 | public void setKeyName(String keyName) { |
| 62 | this.keyName = keyName; |
| 63 | } |
| 64 | |
| 65 | /** |
| 66 | * Assign the filename of each of the injected resources to an |
| 67 | * {@link ExecutionContext}. |
| 68 | * |
| 69 | * @see Partitioner#partition(int) |
| 70 | */ |
| 71 | @Override |
| 72 | public Map<String, ExecutionContext> partition(int gridSize) { |
| 73 | Map<String, ExecutionContext> map = new HashMap<String, ExecutionContext>(gridSize); |
| 74 | int i = 0; |
| 75 | for (Resource resource : resources) { |
| 76 | ExecutionContext context = new ExecutionContext(); |
| 77 | Assert.state(resource.exists(), "Resource does not exist: "+resource); |
| 78 | try { |
| 79 | context.putString(keyName, resource.getURL().toExternalForm()); |
| 80 | } |
| 81 | catch (IOException e) { |
| 82 | throw new IllegalArgumentException("File could not be located for: "+resource, e); |
| 83 | } |
| 84 | map.put(PARTITION_KEY + i, context); |
| 85 | i++; |
| 86 | } |
| 87 | return map; |
| 88 | } |
| 89 | |
| 90 | } |