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.launch.support; 017 018import java.text.DateFormat; 019import java.text.ParseException; 020import java.text.SimpleDateFormat; 021import java.util.Date; 022import java.util.Map; 023import java.util.Map.Entry; 024import java.util.Properties; 025 026import org.springframework.batch.core.JobParameter; 027import org.springframework.batch.core.JobParameters; 028import org.springframework.batch.core.JobParametersBuilder; 029import org.springframework.batch.core.converter.JobParametersConverter; 030import org.springframework.lang.Nullable; 031 032/** 033 * @author Lucas Ward 034 * @author Mahmoud Ben Hassine 035 * 036 */ 037public class ScheduledJobParametersFactory implements JobParametersConverter { 038 039 public static final String SCHEDULE_DATE_KEY = "schedule.date"; 040 041 private DateFormat dateFormat = new SimpleDateFormat("yyyy/MM/dd"); 042 043 /* 044 * (non-Javadoc) 045 * 046 * @see org.springframework.batch.core.runtime.JobParametersFactory#getJobParameters(java.util.Properties) 047 */ 048 @Override 049 public JobParameters getJobParameters(@Nullable Properties props) { 050 051 if (props == null || props.isEmpty()) { 052 return new JobParameters(); 053 } 054 055 JobParametersBuilder propertiesBuilder = new JobParametersBuilder(); 056 057 for (Entry<Object, Object> entry : props.entrySet()) { 058 if (entry.getKey().equals(SCHEDULE_DATE_KEY)) { 059 Date scheduleDate; 060 try { 061 scheduleDate = dateFormat.parse(entry.getValue().toString()); 062 } catch (ParseException ex) { 063 throw new IllegalArgumentException("Date format is invalid: [" + entry.getValue() + "]"); 064 } 065 propertiesBuilder.addDate(entry.getKey().toString(), scheduleDate); 066 } else { 067 propertiesBuilder.addString(entry.getKey().toString(), entry.getValue().toString()); 068 } 069 } 070 071 return propertiesBuilder.toJobParameters(); 072 } 073 074 /** 075 * Convert schedule date to Date, and assume all other parameters can be represented by their default string value. 076 * 077 * @see org.springframework.batch.core.converter.JobParametersConverter#getProperties(org.springframework.batch.core.JobParameters) 078 */ 079 @Override 080 public Properties getProperties(@Nullable JobParameters params) { 081 082 if (params == null || params.isEmpty()) { 083 return new Properties(); 084 } 085 086 Map<String, JobParameter> parameters = params.getParameters(); 087 Properties result = new Properties(); 088 for (Entry<String, JobParameter> entry : parameters.entrySet()) { 089 String key = entry.getKey(); 090 JobParameter jobParameter = entry.getValue(); 091 if (key.equals(SCHEDULE_DATE_KEY)) { 092 result.setProperty(key, dateFormat.format(jobParameter.getValue())); 093 } else { 094 result.setProperty(key, "" + jobParameter.getValue()); 095 } 096 } 097 return result; 098 } 099 100 /** 101 * Public setter for injecting a date format. 102 * 103 * @param dateFormat a {@link DateFormat}, defaults to "yyyy/MM/dd" 104 */ 105 public void setDateFormat(DateFormat dateFormat) { 106 this.dateFormat = dateFormat; 107 } 108}