BATCH-311:Added factory for JobInstanceProperties.

This commit is contained in:
lucasward
2008-01-23 23:17:24 +00:00
parent 38e58f8c45
commit 50a9f0d84d
3 changed files with 175 additions and 2 deletions

View File

@@ -0,0 +1,94 @@
/*
* Copyright 2006-2008 the original author or authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.springframework.batch.execution.bootstrap.support;
import java.text.DateFormat;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.Iterator;
import java.util.Properties;
import java.util.Map.Entry;
import org.springframework.batch.core.domain.JobInstanceProperties;
import org.springframework.batch.core.domain.JobInstancePropertiesBuilder;
import org.springframework.batch.core.domain.JobInstancePropertiesFactory;
import org.springframework.util.Assert;
/**
* @author Lucas Ward
*
*/
public class ScheduledJobInstancePropertiesFactory implements
JobInstancePropertiesFactory {
public static String SCHEDULE_DATE_KEY = "schedule.date";
public static String JOB_KEY = "job.key";
private DateFormat dateFormat = new SimpleDateFormat("MM/dd/yyyy");
/* (non-Javadoc)
* @see org.springframework.batch.core.domain.JobInstancePropertiesFactory#getProperties(java.lang.String[])
*/
public JobInstanceProperties getProperties(String[] args) {
Assert.notNull(args, "Factory arguments must not be null.");
JobInstancePropertiesBuilder propertiesBuilder = new JobInstancePropertiesBuilder();
Properties props = parseArgs(args);
for(Iterator it = props.entrySet().iterator(); it.hasNext();){
Entry entry = (Entry)it.next();
if(entry.getKey().equals(SCHEDULE_DATE_KEY)){
Date scheduleDate;
try{
scheduleDate = dateFormat.parse(entry.getValue().toString());
}
catch(ParseException ex){
throw new IllegalArgumentException("Schedule date format is invalid: [" + entry.getValue() + "]", ex);
}
propertiesBuilder.addDate(entry.getKey().toString(), scheduleDate);
}
else{
propertiesBuilder.addString(entry.getKey().toString(), entry.getValue().toString());
}
}
return propertiesBuilder.toJobParameters();
}
private Properties parseArgs(String[] args){
Properties props = new Properties();
for(int i = 0; i < args.length; i++){
String property = args[i];
int equalsIndex = property.indexOf('=');
if(equalsIndex == -1){
throw new IllegalArgumentException("JobInstacePropertes argument invalid: [" + property + "]");
}
String key = property.substring(0, equalsIndex);
props.put(key, property.substring(equalsIndex + 1));
}
return props;
}
public void setDateFormat(DateFormat dateFormat) {
this.dateFormat = dateFormat;
}
}

View File

@@ -17,7 +17,7 @@ package org.springframework.batch.execution.launch;
import org.springframework.batch.core.domain.Job;
import org.springframework.batch.core.domain.JobExecution;
import org.springframework.batch.core.domain.JobParameters;
import org.springframework.batch.core.domain.JobInstanceProperties;
import org.springframework.batch.core.repository.JobExecutionAlreadyRunningException;
/**
@@ -44,7 +44,7 @@ public interface JobLauncher {
* by the properties already has an execution running. Throws
* IllegalArgumentException if the job or jobInstanceProperties are null.
*/
public JobExecution run(Job job, JobParameters jobParameters)
public JobExecution run(Job job, JobInstanceProperties jobParameters)
throws JobExecutionAlreadyRunningException;
}

View File

@@ -0,0 +1,79 @@
/*
* Copyright 2006-2008 the original author or authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.springframework.batch.execution.bootstrap.support;
import java.text.DateFormat;
import java.text.SimpleDateFormat;
import java.util.Date;
import org.springframework.batch.core.domain.JobInstanceProperties;
import junit.framework.TestCase;
/**
* @author Lucas Ward
*
*/
public class ScheduledJobInstancePropertiesFactoryTests extends TestCase {
ScheduledJobInstancePropertiesFactory factory;
DateFormat dateFormat = new SimpleDateFormat("MM/dd/yyyy");
protected void setUp() throws Exception {
super.setUp();
factory = new ScheduledJobInstancePropertiesFactory();
}
public void testGetProperties() throws Exception{
String jobKey = "job.key=myKey";
String scheduleDate = "schedule.date=01/23/2008";
String vendorId = "vendor.id=33243243";
String[] args = new String[]{jobKey, scheduleDate, vendorId};
JobInstanceProperties props = factory.getProperties(args);
assertNotNull(props);
assertEquals("myKey", props.getString("job.key"));
assertEquals("33243243", props.getString("vendor.id"));
Date date = dateFormat.parse("01/23/2008");
assertEquals(date, props.getDate("schedule.date"));
}
public void testInvalidFormat(){
String jobKey = "job.key-myKey";
String[] args = new String[]{jobKey};
try{
factory.getProperties(args);
fail();
}
catch(IllegalArgumentException ex){
//expected
}
}
public void testEmptyArgs(){
String[] args = new String[]{};
JobInstanceProperties props = factory.getProperties(args);
assertTrue(props.isEmtpy());
}
}