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.converter; |
17 | |
18 | import java.text.DateFormat; |
19 | import java.text.DecimalFormat; |
20 | import java.text.NumberFormat; |
21 | import java.text.ParseException; |
22 | import java.text.SimpleDateFormat; |
23 | import java.util.Date; |
24 | import java.util.Iterator; |
25 | import java.util.Map; |
26 | import java.util.Properties; |
27 | import java.util.Map.Entry; |
28 | |
29 | import org.springframework.batch.core.JobParameter; |
30 | import org.springframework.batch.core.JobParameters; |
31 | import org.springframework.batch.core.JobParametersBuilder; |
32 | import org.springframework.batch.core.JobParameter.ParameterType; |
33 | import org.springframework.util.StringUtils; |
34 | |
35 | /** |
36 | * Converter for {@link JobParameters} instances using a simple naming |
37 | * convention for property keys. Key names ending with "(<type>)" where |
38 | * type is one of string, date, long are converted to the corresponding type. |
39 | * The default type is string. E.g. |
40 | * |
41 | * <pre> |
42 | * schedule.date(date)=2007/12/11 |
43 | * department.id(long)=2345 |
44 | * </pre> |
45 | * |
46 | * The literal values are converted to the correct type using the default Spring |
47 | * strategies, augmented if necessary by the custom editors provided. |
48 | * |
49 | * @author Dave Syer |
50 | * |
51 | */ |
52 | public class DefaultJobParametersConverter implements JobParametersConverter { |
53 | |
54 | public static final String DATE_TYPE = "(date)"; |
55 | |
56 | public static final String STRING_TYPE = "(string)"; |
57 | |
58 | public static final String LONG_TYPE = "(long)"; |
59 | |
60 | private static final String DOUBLE_TYPE = "(double)"; |
61 | |
62 | private DateFormat dateFormat = new SimpleDateFormat("yyyy/MM/dd"); |
63 | |
64 | private NumberFormat numberFormat = new DecimalFormat("#"); |
65 | |
66 | /** |
67 | * Check for suffix on keys and use those to decide how to convert the |
68 | * value. |
69 | * |
70 | * @throws IllegalArgumentException if a number or date is passed in that |
71 | * cannot be parsed, or cast to the correct type. |
72 | * |
73 | * @see org.springframework.batch.core.converter.JobParametersConverter#getJobParameters(java.util.Properties) |
74 | */ |
75 | public JobParameters getJobParameters(Properties props) { |
76 | |
77 | if (props == null || props.isEmpty()) { |
78 | return new JobParameters(); |
79 | } |
80 | |
81 | JobParametersBuilder propertiesBuilder = new JobParametersBuilder(); |
82 | |
83 | for (Iterator<Entry<Object, Object>> it = props.entrySet().iterator(); it.hasNext();) { |
84 | Entry<Object, Object> entry = it.next(); |
85 | String key = (String) entry.getKey(); |
86 | String value = (String) entry.getValue(); |
87 | if (key.endsWith(DATE_TYPE)) { |
88 | Date date; |
89 | try { |
90 | date = dateFormat.parse(value); |
91 | } |
92 | catch (ParseException ex) { |
93 | String suffix = (dateFormat instanceof SimpleDateFormat) ? ", use " |
94 | + ((SimpleDateFormat) dateFormat).toPattern() : ""; |
95 | throw new IllegalArgumentException("Date format is invalid: [" + value + "]" + suffix); |
96 | } |
97 | propertiesBuilder.addDate(StringUtils.replace(key, DATE_TYPE, ""), date); |
98 | } |
99 | else if (key.endsWith(LONG_TYPE)) { |
100 | Long result; |
101 | try { |
102 | result = (Long) parseNumber(value); |
103 | } |
104 | catch (ClassCastException ex) { |
105 | throw new IllegalArgumentException("Number format is invalid for long value: [" + value |
106 | + "], use a format with no decimal places"); |
107 | } |
108 | propertiesBuilder.addLong(StringUtils.replace(key, LONG_TYPE, ""), result); |
109 | } |
110 | else if (key.endsWith(DOUBLE_TYPE)) { |
111 | Double result = parseNumber(value).doubleValue(); |
112 | propertiesBuilder.addDouble(StringUtils.replace(key, DOUBLE_TYPE, ""), result); |
113 | } |
114 | else if (StringUtils.endsWithIgnoreCase(key, STRING_TYPE)) { |
115 | propertiesBuilder.addString(StringUtils.replace(key, STRING_TYPE, ""), value); |
116 | } |
117 | else { |
118 | propertiesBuilder.addString(key, value); |
119 | } |
120 | } |
121 | |
122 | return propertiesBuilder.toJobParameters(); |
123 | } |
124 | |
125 | /** |
126 | * Delegate to {@link NumberFormat} to parse the value |
127 | */ |
128 | private Number parseNumber(String value) { |
129 | try { |
130 | return numberFormat.parse(value); |
131 | } |
132 | catch (ParseException ex) { |
133 | String suffix = (numberFormat instanceof DecimalFormat) ? ", use " |
134 | + ((DecimalFormat) numberFormat).toPattern() : ""; |
135 | throw new IllegalArgumentException("Number format is invalid: [" + value + "], use " + suffix); |
136 | } |
137 | } |
138 | |
139 | /** |
140 | * Use the same suffixes to create properties (omitting the string suffix |
141 | * because it is the default). |
142 | * |
143 | * @see org.springframework.batch.core.converter.JobParametersConverter#getProperties(org.springframework.batch.core.JobParameters) |
144 | */ |
145 | public Properties getProperties(JobParameters params) { |
146 | |
147 | if (params == null || params.isEmpty()) { |
148 | return new Properties(); |
149 | } |
150 | |
151 | Map<String, JobParameter> parameters = params.getParameters(); |
152 | Properties result = new Properties(); |
153 | for (Entry<String, JobParameter> entry : parameters.entrySet()) { |
154 | |
155 | String key = entry.getKey(); |
156 | JobParameter jobParameter = entry.getValue(); |
157 | Object value = jobParameter.getValue(); |
158 | if (jobParameter.getType() == ParameterType.DATE) { |
159 | result.setProperty(key + DATE_TYPE, dateFormat.format(value)); |
160 | } |
161 | else if (jobParameter.getType() == ParameterType.LONG) { |
162 | result.setProperty(key + LONG_TYPE, numberFormat.format(value)); |
163 | } |
164 | else { |
165 | result.setProperty(key, "" + value); |
166 | } |
167 | } |
168 | return result; |
169 | } |
170 | |
171 | /** |
172 | * Public setter for injecting a date format. |
173 | * |
174 | * @param dateFormat a {@link DateFormat}, defaults to "yyyy/MM/dd" |
175 | */ |
176 | public void setDateFormat(DateFormat dateFormat) { |
177 | this.dateFormat = dateFormat; |
178 | } |
179 | |
180 | /** |
181 | * Public setter for the {@link NumberFormat}. Used to parse longs, so must |
182 | * not contain decimal place (e.g. use "#" or "#,###"). |
183 | * |
184 | * @param numberFormat the {@link NumberFormat} to set |
185 | */ |
186 | public void setNumberFormat(NumberFormat numberFormat) { |
187 | this.numberFormat = numberFormat; |
188 | } |
189 | } |