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.resource; |
17 | |
18 | import java.sql.PreparedStatement; |
19 | import java.sql.SQLException; |
20 | import java.util.List; |
21 | import java.util.Map; |
22 | |
23 | import org.springframework.batch.core.JobParameters; |
24 | import org.springframework.batch.core.StepExecution; |
25 | import org.springframework.batch.core.StepExecutionListener; |
26 | import org.springframework.batch.core.listener.StepExecutionListenerSupport; |
27 | import org.springframework.beans.factory.InitializingBean; |
28 | import org.springframework.jdbc.core.PreparedStatementSetter; |
29 | import org.springframework.jdbc.core.SqlTypeValue; |
30 | import org.springframework.jdbc.core.StatementCreatorUtils; |
31 | import org.springframework.util.Assert; |
32 | |
33 | /** |
34 | * Implementation of the {@link PreparedStatementSetter} interface that also implements |
35 | * {@link StepExecutionListener} and uses {@link JobParameters} to set the parameters on a |
36 | * PreparedStatement. |
37 | * |
38 | * @author Lucas Ward |
39 | * |
40 | */ |
41 | public class StepExecutionPreparedStatementSetter extends StepExecutionListenerSupport implements |
42 | PreparedStatementSetter, InitializingBean { |
43 | |
44 | private List parameterKeys; |
45 | private JobParameters jobParameters; |
46 | |
47 | public void setValues(PreparedStatement ps) throws SQLException { |
48 | Map parameters = jobParameters.getParameters(); |
49 | for(int i = 0; i < parameterKeys.size(); i++){ |
50 | Object arg = parameters.get(parameterKeys.get(i)); |
51 | if(arg == null){ |
52 | throw new IllegalStateException("No job parameter found for with key of: [" + parameterKeys.get(i) + "]"); |
53 | } |
54 | StatementCreatorUtils.setParameterValue(ps, i + 1, SqlTypeValue.TYPE_UNKNOWN, arg); |
55 | } |
56 | } |
57 | |
58 | public void beforeStep(StepExecution stepExecution) { |
59 | this.jobParameters = stepExecution.getJobParameters(); |
60 | } |
61 | |
62 | /** |
63 | * The parameter names that will be pulled from the {@link JobParameters}. It is |
64 | * assumed that their order in the List is the order of the parameters in the |
65 | * PreparedStatement. |
66 | */ |
67 | public void setParameterKeys(List parameterKeys) { |
68 | this.parameterKeys = parameterKeys; |
69 | } |
70 | |
71 | public void afterPropertiesSet() throws Exception { |
72 | Assert.notNull(parameterKeys, "Parameters names must be provided"); |
73 | } |
74 | } |