RESOLVED: BATCH-1146 Added MD5 hashing to JobParameters in JDBC instance dao

This commit is contained in:
dsyer
2009-03-14 09:12:10 +00:00
parent bcaaf4e2b0
commit 3b1c506bd9
2 changed files with 42 additions and 3 deletions

View File

@@ -16,6 +16,10 @@
package org.springframework.batch.core.repository.dao;
import java.io.UnsupportedEncodingException;
import java.math.BigInteger;
import java.security.MessageDigest;
import java.security.NoSuchAlgorithmException;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Timestamp;
@@ -112,15 +116,29 @@ public class JdbcJobInstanceDao extends AbstractJdbcBatchMetadataDao implements
return jobInstance;
}
private String createJobKey(JobParameters jobParameters) {
protected String createJobKey(JobParameters jobParameters) {
Map<String, JobParameter> props = jobParameters.getParameters();
StringBuffer stringBuffer = new StringBuffer();
for (Entry<String, JobParameter> entry : props.entrySet()) {
stringBuffer.append(entry.toString() + ";");
}
return stringBuffer.toString();
MessageDigest digest;
try {
digest = MessageDigest.getInstance("MD5");
}
catch (NoSuchAlgorithmException e) {
throw new IllegalStateException("MD5 algorithm not available. Fatal (should be in the JDK).");
}
try {
byte[] bytes = digest.digest(stringBuffer.toString().getBytes("UTF-8"));
return String.format("%032x", new BigInteger(1, bytes));
}
catch (UnsupportedEncodingException e) {
throw new IllegalStateException("UTF-8 encoding not available. Fatal (should be in the JDK).");
}
}
/**

View File

@@ -1,11 +1,17 @@
package org.springframework.batch.core.repository.dao;
import static org.junit.Assert.*;
import java.math.BigInteger;
import java.security.MessageDigest;
import org.springframework.batch.core.JobExecution;
import org.springframework.batch.core.JobInstance;
import org.springframework.batch.core.JobParameters;
import org.springframework.test.context.ContextConfiguration;
import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;
import org.junit.Assert;
import org.junit.Test;
import org.junit.runner.RunWith;
@@ -19,6 +25,7 @@ public class JdbcJobInstanceDaoTests extends AbstractJobInstanceDaoTests {
return (JobInstanceDao) applicationContext.getBean("jobInstanceDao");
}
@Test
public void testFindJobInstanceByExecution(){
JobExecutionDao jobExecutionDao = (JobExecutionDao) applicationContext.getBean("jobExecutionDao");
@@ -30,4 +37,18 @@ public class JdbcJobInstanceDaoTests extends AbstractJobInstanceDaoTests {
JobInstance returnedInstance = dao.getJobInstance(jobExecution);
Assert.assertEquals(jobInstance, returnedInstance);
}
@Test
public void testHexing() throws Exception {
MessageDigest digest = MessageDigest.getInstance("MD5");
byte[] bytes = digest.digest("f78spx".getBytes("UTF-8"));
StringBuffer output = new StringBuffer();
for (byte bite : bytes) {
output.append(String.format("%02x", bite));
}
assertEquals("Wrong hash: "+output,32, output.length());
String value = String.format("%032x", new BigInteger(1, bytes));
assertEquals("Wrong hash: "+value,32, value.length());
assertEquals(value, output.toString());
}
}