| 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 | package org.springframework.batch.core; |
| 17 | |
| 18 | import java.io.UnsupportedEncodingException; |
| 19 | import java.math.BigInteger; |
| 20 | import java.security.MessageDigest; |
| 21 | import java.security.NoSuchAlgorithmException; |
| 22 | import java.util.ArrayList; |
| 23 | import java.util.Collections; |
| 24 | import java.util.List; |
| 25 | import java.util.Map; |
| 26 | |
| 27 | /** |
| 28 | * Default implementation of the {@link JobKeyGenerator} interface. |
| 29 | * This implementation provides a single hash value based on the JobParameters |
| 30 | * passed in. Only identifying parameters (per {@link JobParameter#isIdentifying()}) |
| 31 | * are used in the calculation of the key. |
| 32 | * |
| 33 | * @author Michael Minella |
| 34 | * @since 2.2 |
| 35 | */ |
| 36 | public class DefaultJobKeyGenerator implements JobKeyGenerator<JobParameters> { |
| 37 | |
| 38 | /** |
| 39 | * Generates the job key to be used based on the {@link JobParameters} instance |
| 40 | * provided. |
| 41 | */ |
| 42 | @Override |
| 43 | public String generateKey(JobParameters source) { |
| 44 | |
| 45 | Map<String, JobParameter> props = source.getParameters(); |
| 46 | StringBuffer stringBuffer = new StringBuffer(); |
| 47 | List<String> keys = new ArrayList<String>(props.keySet()); |
| 48 | Collections.sort(keys); |
| 49 | for (String key : keys) { |
| 50 | JobParameter jobParameter = props.get(key); |
| 51 | if(jobParameter.isIdentifying()) { |
| 52 | String value = jobParameter.getValue()==null ? "" : jobParameter.toString(); |
| 53 | stringBuffer.append(key + "=" + value + ";"); |
| 54 | } |
| 55 | } |
| 56 | |
| 57 | MessageDigest digest; |
| 58 | try { |
| 59 | digest = MessageDigest.getInstance("MD5"); |
| 60 | } catch (NoSuchAlgorithmException e) { |
| 61 | throw new IllegalStateException( |
| 62 | "MD5 algorithm not available. Fatal (should be in the JDK)."); |
| 63 | } |
| 64 | |
| 65 | try { |
| 66 | byte[] bytes = digest.digest(stringBuffer.toString().getBytes( |
| 67 | "UTF-8")); |
| 68 | return String.format("%032x", new BigInteger(1, bytes)); |
| 69 | } catch (UnsupportedEncodingException e) { |
| 70 | throw new IllegalStateException( |
| 71 | "UTF-8 encoding not available. Fatal (should be in the JDK)."); |
| 72 | } |
| 73 | } |
| 74 | } |