EMMA Coverage Report (generated Thu May 22 12:08:10 CDT 2014)
[all classes][org.springframework.batch.core]

COVERAGE SUMMARY FOR SOURCE FILE [DefaultJobKeyGenerator.java]

nameclass, %method, %block, %line, %
DefaultJobKeyGenerator.java100% (1/1)100% (2/2)88%  (84/96)79%  (15/19)

COVERAGE BREAKDOWN BY CLASS AND METHOD

nameclass, %method, %block, %line, %
     
class DefaultJobKeyGenerator100% (1/1)100% (2/2)88%  (84/96)79%  (15/19)
generateKey (JobParameters): String 100% (1/1)87%  (81/93)78%  (14/18)
DefaultJobKeyGenerator (): void 100% (1/1)100% (3/3)100% (1/1)

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 */
16package org.springframework.batch.core;
17 
18import java.io.UnsupportedEncodingException;
19import java.math.BigInteger;
20import java.security.MessageDigest;
21import java.security.NoSuchAlgorithmException;
22import java.util.ArrayList;
23import java.util.Collections;
24import java.util.List;
25import 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 */
36public 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}

[all classes][org.springframework.batch.core]
EMMA 2.0.5312 (C) Vladimir Roubtsov