| 1 | /* |
| 2 | * Copyright 2006-2008 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.launch.support; |
| 17 | |
| 18 | import java.text.DateFormat; |
| 19 | import java.text.ParseException; |
| 20 | import java.text.SimpleDateFormat; |
| 21 | import java.util.Date; |
| 22 | import java.util.Iterator; |
| 23 | import java.util.Map; |
| 24 | import java.util.Properties; |
| 25 | import java.util.Map.Entry; |
| 26 | |
| 27 | import org.springframework.batch.core.JobParameters; |
| 28 | import org.springframework.batch.core.JobParametersBuilder; |
| 29 | import org.springframework.batch.core.converter.JobParametersConverter; |
| 30 | |
| 31 | /** |
| 32 | * @author Lucas Ward |
| 33 | * |
| 34 | */ |
| 35 | public class ScheduledJobParametersFactory implements JobParametersConverter { |
| 36 | |
| 37 | public static final String SCHEDULE_DATE_KEY = "schedule.date"; |
| 38 | |
| 39 | private DateFormat dateFormat = new SimpleDateFormat("yyyy/MM/dd"); |
| 40 | |
| 41 | /* |
| 42 | * (non-Javadoc) |
| 43 | * |
| 44 | * @see org.springframework.batch.core.runtime.JobParametersFactory#getJobParameters(java.util.Properties) |
| 45 | */ |
| 46 | public JobParameters getJobParameters(Properties props) { |
| 47 | |
| 48 | if (props == null || props.isEmpty()) { |
| 49 | return new JobParameters(); |
| 50 | } |
| 51 | |
| 52 | JobParametersBuilder propertiesBuilder = new JobParametersBuilder(); |
| 53 | |
| 54 | for (Iterator it = props.entrySet().iterator(); it.hasNext();) { |
| 55 | Entry entry = (Entry) it.next(); |
| 56 | if (entry.getKey().equals(SCHEDULE_DATE_KEY)) { |
| 57 | Date scheduleDate; |
| 58 | try { |
| 59 | scheduleDate = dateFormat.parse(entry.getValue().toString()); |
| 60 | } catch (ParseException ex) { |
| 61 | throw new IllegalArgumentException("Date format is invalid: [" + entry.getValue() + "]"); |
| 62 | } |
| 63 | propertiesBuilder.addDate(entry.getKey().toString(), scheduleDate); |
| 64 | } else { |
| 65 | propertiesBuilder.addString(entry.getKey().toString(), entry.getValue().toString()); |
| 66 | } |
| 67 | } |
| 68 | |
| 69 | return propertiesBuilder.toJobParameters(); |
| 70 | } |
| 71 | |
| 72 | /** |
| 73 | * Convert schedule date to Date, and assume all other parameters can be represented by their default string value. |
| 74 | * |
| 75 | * @see org.springframework.batch.core.converter.JobParametersConverter#getProperties(org.springframework.batch.core.JobParameters) |
| 76 | */ |
| 77 | public Properties getProperties(JobParameters params) { |
| 78 | |
| 79 | if (params == null || params.isEmpty()) { |
| 80 | return new Properties(); |
| 81 | } |
| 82 | |
| 83 | Map parameters = params.getParameters(); |
| 84 | Properties result = new Properties(); |
| 85 | for (Iterator iterator = parameters.entrySet().iterator(); iterator.hasNext();) { |
| 86 | Entry entry = (Entry) iterator.next(); |
| 87 | String key = (String) entry.getKey(); |
| 88 | if (key.equals(SCHEDULE_DATE_KEY)) { |
| 89 | result.setProperty(key, dateFormat.format(entry.getValue())); |
| 90 | } else { |
| 91 | result.setProperty(key, "" + entry.getValue()); |
| 92 | } |
| 93 | } |
| 94 | return result; |
| 95 | } |
| 96 | |
| 97 | /** |
| 98 | * Public setter for injecting a date format. |
| 99 | * |
| 100 | * @param dateFormat a {@link DateFormat}, defaults to "yyyy/MM/dd" |
| 101 | */ |
| 102 | public void setDateFormat(DateFormat dateFormat) { |
| 103 | this.dateFormat = dateFormat; |
| 104 | } |
| 105 | } |