| 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 | |
| 17 | package org.springframework.batch.core.repository.dao; |
| 18 | |
| 19 | import java.sql.ResultSet; |
| 20 | import java.sql.SQLException; |
| 21 | import java.sql.PreparedStatement; |
| 22 | import java.util.Map.Entry; |
| 23 | import java.util.HashMap; |
| 24 | import java.util.Map; |
| 25 | import java.util.List; |
| 26 | |
| 27 | import org.springframework.batch.core.JobExecution; |
| 28 | import org.springframework.batch.core.StepExecution; |
| 29 | import org.springframework.batch.item.ExecutionContext; |
| 30 | import org.springframework.jdbc.core.simple.ParameterizedRowMapper; |
| 31 | import org.springframework.jdbc.core.PreparedStatementSetter; |
| 32 | import org.springframework.jdbc.support.lob.DefaultLobHandler; |
| 33 | import org.springframework.jdbc.support.lob.LobHandler; |
| 34 | import org.springframework.util.Assert; |
| 35 | |
| 36 | /** |
| 37 | * JDBC DAO for {@link ExecutionContext}. |
| 38 | * |
| 39 | * Stores execution context data related to both Step and Job using |
| 40 | * discriminator column to distinguish between the two. |
| 41 | * |
| 42 | * @author Lucas Ward |
| 43 | * @author Robert Kasanicky |
| 44 | * @author Thomas Risberg |
| 45 | */ |
| 46 | public class JdbcExecutionContextDao extends AbstractJdbcBatchMetadataDao implements ExecutionContextDao { |
| 47 | |
| 48 | private static final String FIND_JOB_EXECUTION_CONTEXT = "SELECT SHORT_CONTEXT, SERIALIZED_CONTEXT " |
| 49 | + "FROM %PREFIX%JOB_EXECUTION_CONTEXT WHERE JOB_EXECUTION_ID = ?"; |
| 50 | |
| 51 | private static final String INSERT_JOB_EXECUTION_CONTEXT = "INSERT INTO %PREFIX%JOB_EXECUTION_CONTEXT " |
| 52 | + "(SHORT_CONTEXT, SERIALIZED_CONTEXT, JOB_EXECUTION_ID) " + "VALUES(?, ?, ?)"; |
| 53 | |
| 54 | private static final String UPDATE_JOB_EXECUTION_CONTEXT = "UPDATE %PREFIX%JOB_EXECUTION_CONTEXT " |
| 55 | + "SET SHORT_CONTEXT = ?, SERIALIZED_CONTEXT = ? " + "WHERE JOB_EXECUTION_ID = ?"; |
| 56 | |
| 57 | private static final String FIND_STEP_EXECUTION_CONTEXT = "SELECT SHORT_CONTEXT, SERIALIZED_CONTEXT " |
| 58 | + "FROM %PREFIX%STEP_EXECUTION_CONTEXT WHERE STEP_EXECUTION_ID = ?"; |
| 59 | |
| 60 | private static final String INSERT_STEP_EXECUTION_CONTEXT = "INSERT INTO %PREFIX%STEP_EXECUTION_CONTEXT " |
| 61 | + "(SHORT_CONTEXT, SERIALIZED_CONTEXT, STEP_EXECUTION_ID) " + "VALUES(?, ?, ?)"; |
| 62 | |
| 63 | private static final String UPDATE_STEP_EXECUTION_CONTEXT = "UPDATE %PREFIX%STEP_EXECUTION_CONTEXT " |
| 64 | + "SET SHORT_CONTEXT = ?, SERIALIZED_CONTEXT = ? " + "WHERE STEP_EXECUTION_ID = ?"; |
| 65 | |
| 66 | private static final int MAX_VARCHAR_LENGTH = 2500; |
| 67 | |
| 68 | private LobHandler lobHandler = new DefaultLobHandler(); |
| 69 | |
| 70 | private ExecutionContextStringSerializer serializer; |
| 71 | |
| 72 | public ExecutionContext getExecutionContext(JobExecution jobExecution) { |
| 73 | Long executionId = jobExecution.getId(); |
| 74 | Assert.notNull(executionId, "ExecutionId must not be null."); |
| 75 | |
| 76 | List<ExecutionContext> results = getJdbcTemplate().query(getQuery(FIND_JOB_EXECUTION_CONTEXT), |
| 77 | new ExecutionContextRowMapper(), executionId); |
| 78 | if (results.size() > 0) { |
| 79 | return results.get(0); |
| 80 | } |
| 81 | else { |
| 82 | return new ExecutionContext(); |
| 83 | } |
| 84 | } |
| 85 | |
| 86 | public ExecutionContext getExecutionContext(StepExecution stepExecution) { |
| 87 | Long executionId = stepExecution.getId(); |
| 88 | Assert.notNull(executionId, "ExecutionId must not be null."); |
| 89 | |
| 90 | List<ExecutionContext> results = getJdbcTemplate().query(getQuery(FIND_STEP_EXECUTION_CONTEXT), |
| 91 | new ExecutionContextRowMapper(), executionId); |
| 92 | if (results.size() > 0) { |
| 93 | return results.get(0); |
| 94 | } |
| 95 | else { |
| 96 | return new ExecutionContext(); |
| 97 | } |
| 98 | } |
| 99 | |
| 100 | public void updateExecutionContext(final JobExecution jobExecution) { |
| 101 | Long executionId = jobExecution.getId(); |
| 102 | ExecutionContext executionContext = jobExecution.getExecutionContext(); |
| 103 | Assert.notNull(executionId, "ExecutionId must not be null."); |
| 104 | Assert.notNull(executionContext, "The ExecutionContext must not be null."); |
| 105 | |
| 106 | String serializedContext = serializeContext(executionContext); |
| 107 | |
| 108 | persistSerializedContext(executionId, serializedContext, UPDATE_JOB_EXECUTION_CONTEXT); |
| 109 | } |
| 110 | |
| 111 | public void updateExecutionContext(final StepExecution stepExecution) { |
| 112 | |
| 113 | Long executionId = stepExecution.getId(); |
| 114 | ExecutionContext executionContext = stepExecution.getExecutionContext(); |
| 115 | Assert.notNull(executionId, "ExecutionId must not be null."); |
| 116 | Assert.notNull(executionContext, "The ExecutionContext must not be null."); |
| 117 | |
| 118 | String serializedContext = serializeContext(executionContext); |
| 119 | |
| 120 | persistSerializedContext(executionId, serializedContext, UPDATE_STEP_EXECUTION_CONTEXT); |
| 121 | } |
| 122 | |
| 123 | public void saveExecutionContext(JobExecution jobExecution) { |
| 124 | |
| 125 | Long executionId = jobExecution.getId(); |
| 126 | ExecutionContext executionContext = jobExecution.getExecutionContext(); |
| 127 | Assert.notNull(executionId, "ExecutionId must not be null."); |
| 128 | Assert.notNull(executionContext, "The ExecutionContext must not be null."); |
| 129 | |
| 130 | String serializedContext = serializeContext(executionContext); |
| 131 | |
| 132 | persistSerializedContext(executionId, serializedContext, INSERT_JOB_EXECUTION_CONTEXT); |
| 133 | } |
| 134 | |
| 135 | public void saveExecutionContext(StepExecution stepExecution) { |
| 136 | Long executionId = stepExecution.getId(); |
| 137 | ExecutionContext executionContext = stepExecution.getExecutionContext(); |
| 138 | Assert.notNull(executionId, "ExecutionId must not be null."); |
| 139 | Assert.notNull(executionContext, "The ExecutionContext must not be null."); |
| 140 | |
| 141 | String serializedContext = serializeContext(executionContext); |
| 142 | |
| 143 | persistSerializedContext(executionId, serializedContext, INSERT_STEP_EXECUTION_CONTEXT); |
| 144 | } |
| 145 | |
| 146 | public void setLobHandler(LobHandler lobHandler) { |
| 147 | this.lobHandler = lobHandler; |
| 148 | } |
| 149 | |
| 150 | @Override |
| 151 | public void afterPropertiesSet() throws Exception { |
| 152 | super.afterPropertiesSet(); |
| 153 | serializer = new XStreamExecutionContextStringSerializer(); |
| 154 | ((XStreamExecutionContextStringSerializer) serializer).afterPropertiesSet(); |
| 155 | } |
| 156 | |
| 157 | /** |
| 158 | * @param executionId |
| 159 | * @param serializedContext |
| 160 | * @param sql with parameters (shortContext, longContext, executionId) |
| 161 | */ |
| 162 | private void persistSerializedContext(final Long executionId, String serializedContext, String sql) { |
| 163 | |
| 164 | final String shortContext; |
| 165 | final String longContext; |
| 166 | if (serializedContext.length() > MAX_VARCHAR_LENGTH) { |
| 167 | shortContext = serializedContext.substring(0, MAX_VARCHAR_LENGTH - 4) + " ..."; |
| 168 | longContext = serializedContext; |
| 169 | } |
| 170 | else { |
| 171 | shortContext = serializedContext; |
| 172 | longContext = null; |
| 173 | } |
| 174 | |
| 175 | getJdbcTemplate().getJdbcOperations().update(getQuery(sql), new PreparedStatementSetter() { |
| 176 | public void setValues(PreparedStatement ps) throws SQLException { |
| 177 | ps.setString(1, shortContext); |
| 178 | if (longContext != null) { |
| 179 | lobHandler.getLobCreator().setClobAsString(ps, 2, longContext); |
| 180 | } |
| 181 | else { |
| 182 | ps.setNull(2, getClobTypeToUse()); |
| 183 | } |
| 184 | ps.setLong(3, executionId); |
| 185 | } |
| 186 | }); |
| 187 | } |
| 188 | |
| 189 | private String serializeContext(ExecutionContext ctx) { |
| 190 | Map<String, Object> m = new HashMap<String, Object>(); |
| 191 | for (Entry<String, Object> me : ctx.entrySet()) { |
| 192 | m.put(me.getKey(), me.getValue()); |
| 193 | } |
| 194 | return serializer.serialize(m); |
| 195 | } |
| 196 | |
| 197 | private class ExecutionContextRowMapper implements ParameterizedRowMapper<ExecutionContext> { |
| 198 | public ExecutionContext mapRow(ResultSet rs, int i) throws SQLException { |
| 199 | ExecutionContext executionContext = new ExecutionContext(); |
| 200 | String serializedContext = rs.getString("SERIALIZED_CONTEXT"); |
| 201 | if (serializedContext == null) { |
| 202 | serializedContext = rs.getString("SHORT_CONTEXT"); |
| 203 | } |
| 204 | Map<String, Object> map = serializer.deserialize(serializedContext); |
| 205 | for (Map.Entry<String, Object> entry : map.entrySet()) { |
| 206 | executionContext.put(entry.getKey(), entry.getValue()); |
| 207 | } |
| 208 | return executionContext; |
| 209 | } |
| 210 | } |
| 211 | |
| 212 | } |