001/*
002 * Copyright 2006-2018 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.core;
017
018import java.io.UnsupportedEncodingException;
019import java.math.BigInteger;
020import java.security.MessageDigest;
021import java.security.NoSuchAlgorithmException;
022import java.util.ArrayList;
023import java.util.Collections;
024import java.util.List;
025import java.util.Map;
026
027import org.springframework.util.Assert;
028
029/**
030 * Default implementation of the {@link JobKeyGenerator} interface.
031 * This implementation provides a single hash value based on the JobParameters
032 * passed in.  Only identifying parameters (per {@link JobParameter#isIdentifying()})
033 * are used in the calculation of the key.
034 *
035 * @author Michael Minella
036 * @author Mahmoud Ben Hassine
037 * @since 2.2
038 */
039public class DefaultJobKeyGenerator implements JobKeyGenerator<JobParameters> {
040
041        /**
042         * Generates the job key to be used based on the {@link JobParameters} instance
043         * provided.
044         */
045        @Override
046        public String generateKey(JobParameters source) {
047
048                Assert.notNull(source, "source must not be null");
049                Map<String, JobParameter> props = source.getParameters();
050                StringBuilder stringBuffer = new StringBuilder();
051                List<String> keys = new ArrayList<String>(props.keySet());
052                Collections.sort(keys);
053                for (String key : keys) {
054                        JobParameter jobParameter = props.get(key);
055                        if(jobParameter.isIdentifying()) {
056                                String value = jobParameter.getValue()==null ? "" : jobParameter.toString();
057                                stringBuffer.append(key).append("=").append(value).append(";");
058                        }
059                }
060
061                MessageDigest digest;
062                try {
063                        digest = MessageDigest.getInstance("MD5");
064                } catch (NoSuchAlgorithmException e) {
065                        throw new IllegalStateException(
066                                        "MD5 algorithm not available.  Fatal (should be in the JDK).");
067                }
068
069                try {
070                        byte[] bytes = digest.digest(stringBuffer.toString().getBytes(
071                                        "UTF-8"));
072                        return String.format("%032x", new BigInteger(1, bytes));
073                } catch (UnsupportedEncodingException e) {
074                        throw new IllegalStateException(
075                                        "UTF-8 encoding not available.  Fatal (should be in the JDK).");
076                }
077        }
078}