EMMA Coverage Report (generated Thu Jan 24 13:37:04 CST 2013)
[all classes][org.springframework.batch.core.repository.dao]

COVERAGE SUMMARY FOR SOURCE FILE [JdbcExecutionContextDao.java]

nameclass, %method, %block, %line, %
JdbcExecutionContextDao.java100% (3/3)95%  (18/19)89%  (326/365)93%  (71.8/77)

COVERAGE BREAKDOWN BY CLASS AND METHOD

nameclass, %method, %block, %line, %
     
class JdbcExecutionContextDao$1100% (1/1)100% (2/2)78%  (36/46)86%  (6/7)
setValues (PreparedStatement): void 100% (1/1)68%  (21/31)83%  (5/6)
JdbcExecutionContextDao$1 (JdbcExecutionContextDao, String, String, Long): void 100% (1/1)100% (15/15)100% (1/1)
     
class JdbcExecutionContextDao100% (1/1)93%  (13/14)89%  (239/268)93%  (58.8/63)
access$100 (JdbcExecutionContextDao): LobHandler 0%   (0/1)0%   (0/3)0%   (0/1)
persistSerializedContext (Long, String, String): void 100% (1/1)58%  (25/43)71%  (5/7)
getExecutionContext (JobExecution): ExecutionContext 100% (1/1)89%  (32/36)83%  (5/6)
getExecutionContext (StepExecution): ExecutionContext 100% (1/1)89%  (32/36)83%  (5/6)
JdbcExecutionContextDao (): void 100% (1/1)100% (11/11)100% (4/4)
access$200 (JdbcExecutionContextDao): ExecutionContextStringSerializer 100% (1/1)100% (3/3)100% (1/1)
afterPropertiesSet (): void 100% (1/1)100% (12/12)100% (4/4)
saveExecutionContext (JobExecution): void 100% (1/1)100% (22/22)100% (7/7)
saveExecutionContext (StepExecution): void 100% (1/1)100% (22/22)100% (7/7)
serializeContext (ExecutionContext): String 100% (1/1)100% (28/28)100% (4/4)
setLobHandler (LobHandler): void 100% (1/1)100% (4/4)100% (2/2)
setShortContextLength (int): void 100% (1/1)100% (4/4)100% (2/2)
updateExecutionContext (JobExecution): void 100% (1/1)100% (22/22)100% (7/7)
updateExecutionContext (StepExecution): void 100% (1/1)100% (22/22)100% (7/7)
     
class JdbcExecutionContextDao$ExecutionContextRowMapper100% (1/1)100% (3/3)100% (51/51)100% (9/9)
JdbcExecutionContextDao$ExecutionContextRowMapper (JdbcExecutionContextDao): ... 100% (1/1)100% (6/6)100% (1/1)
JdbcExecutionContextDao$ExecutionContextRowMapper (JdbcExecutionContextDao, J... 100% (1/1)100% (4/4)100% (1/1)
mapRow (ResultSet, int): ExecutionContext 100% (1/1)100% (41/41)100% (8/8)

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 
17package org.springframework.batch.core.repository.dao;
18 
19import java.sql.PreparedStatement;
20import java.sql.ResultSet;
21import java.sql.SQLException;
22import java.util.HashMap;
23import java.util.List;
24import java.util.Map;
25import java.util.Map.Entry;
26 
27import org.springframework.batch.core.JobExecution;
28import org.springframework.batch.core.StepExecution;
29import org.springframework.batch.item.ExecutionContext;
30import org.springframework.jdbc.core.PreparedStatementSetter;
31import org.springframework.jdbc.core.simple.ParameterizedRowMapper;
32import org.springframework.jdbc.support.lob.DefaultLobHandler;
33import org.springframework.jdbc.support.lob.LobHandler;
34import 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 * a different table for each.
41 * 
42 * @author Lucas Ward
43 * @author Robert Kasanicky
44 * @author Thomas Risberg
45 */
46public 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 DEFAULT_MAX_VARCHAR_LENGTH = 2500;
67 
68        private int shortContextLength = DEFAULT_MAX_VARCHAR_LENGTH;
69 
70        private LobHandler lobHandler = new DefaultLobHandler();
71 
72        private ExecutionContextStringSerializer serializer;
73 
74        /**
75         * The maximum size that an execution context can have and still be stored
76         * completely in short form in the column <code>SHORT_CONTEXT</code>.
77         * Anything longer than this will overflow into large-object storage, and
78         * the first part only will be retained in the short form for readability.
79         * Default value is 2500. Clients using multi-bytes charsets on the database
80         * server may need to reduce this value to as little as half the value of
81         * the column size.
82         * @param shortContextLength
83         */
84        public void setShortContextLength(int shortContextLength) {
85                this.shortContextLength = shortContextLength;
86        }
87 
88        public ExecutionContext getExecutionContext(JobExecution jobExecution) {
89                Long executionId = jobExecution.getId();
90                Assert.notNull(executionId, "ExecutionId must not be null.");
91 
92                List<ExecutionContext> results = getJdbcTemplate().query(getQuery(FIND_JOB_EXECUTION_CONTEXT),
93                                new ExecutionContextRowMapper(), executionId);
94                if (results.size() > 0) {
95                        return results.get(0);
96                }
97                else {
98                        return new ExecutionContext();
99                }
100        }
101 
102        public ExecutionContext getExecutionContext(StepExecution stepExecution) {
103                Long executionId = stepExecution.getId();
104                Assert.notNull(executionId, "ExecutionId must not be null.");
105 
106                List<ExecutionContext> results = getJdbcTemplate().query(getQuery(FIND_STEP_EXECUTION_CONTEXT),
107                                new ExecutionContextRowMapper(), executionId);
108                if (results.size() > 0) {
109                        return results.get(0);
110                }
111                else {
112                        return new ExecutionContext();
113                }
114        }
115 
116        public void updateExecutionContext(final JobExecution jobExecution) {
117                Long executionId = jobExecution.getId();
118                ExecutionContext executionContext = jobExecution.getExecutionContext();
119                Assert.notNull(executionId, "ExecutionId must not be null.");
120                Assert.notNull(executionContext, "The ExecutionContext must not be null.");
121 
122                String serializedContext = serializeContext(executionContext);
123 
124                persistSerializedContext(executionId, serializedContext, UPDATE_JOB_EXECUTION_CONTEXT);
125        }
126 
127        public void updateExecutionContext(final StepExecution stepExecution) {
128 
129                Long executionId = stepExecution.getId();
130                ExecutionContext executionContext = stepExecution.getExecutionContext();
131                Assert.notNull(executionId, "ExecutionId must not be null.");
132                Assert.notNull(executionContext, "The ExecutionContext must not be null.");
133 
134                String serializedContext = serializeContext(executionContext);
135 
136                persistSerializedContext(executionId, serializedContext, UPDATE_STEP_EXECUTION_CONTEXT);
137        }
138 
139        public void saveExecutionContext(JobExecution jobExecution) {
140 
141                Long executionId = jobExecution.getId();
142                ExecutionContext executionContext = jobExecution.getExecutionContext();
143                Assert.notNull(executionId, "ExecutionId must not be null.");
144                Assert.notNull(executionContext, "The ExecutionContext must not be null.");
145 
146                String serializedContext = serializeContext(executionContext);
147 
148                persistSerializedContext(executionId, serializedContext, INSERT_JOB_EXECUTION_CONTEXT);
149        }
150 
151        public void saveExecutionContext(StepExecution stepExecution) {
152                Long executionId = stepExecution.getId();
153                ExecutionContext executionContext = stepExecution.getExecutionContext();
154                Assert.notNull(executionId, "ExecutionId must not be null.");
155                Assert.notNull(executionContext, "The ExecutionContext must not be null.");
156 
157                String serializedContext = serializeContext(executionContext);
158 
159                persistSerializedContext(executionId, serializedContext, INSERT_STEP_EXECUTION_CONTEXT);
160        }
161 
162        public void setLobHandler(LobHandler lobHandler) {
163                this.lobHandler = lobHandler;
164        }
165 
166        @Override
167        public void afterPropertiesSet() throws Exception {
168                super.afterPropertiesSet();
169                serializer = new XStreamExecutionContextStringSerializer();
170                ((XStreamExecutionContextStringSerializer) serializer).afterPropertiesSet();
171        }
172 
173        /**
174         * @param executionId
175         * @param serializedContext
176         * @param sql with parameters (shortContext, longContext, executionId)
177         */
178        private void persistSerializedContext(final Long executionId, String serializedContext, String sql) {
179 
180                final String shortContext;
181                final String longContext;
182                if (serializedContext.length() > shortContextLength) {
183                        // Overestimate length of ellipsis to be on the safe side with
184                        // 2-byte chars
185                        shortContext = serializedContext.substring(0, shortContextLength - 8) + " ...";
186                        longContext = serializedContext;
187                }
188                else {
189                        shortContext = serializedContext;
190                        longContext = null;
191                }
192 
193                getJdbcTemplate().getJdbcOperations().update(getQuery(sql), new PreparedStatementSetter() {
194                        public void setValues(PreparedStatement ps) throws SQLException {
195                                ps.setString(1, shortContext);
196                                if (longContext != null) {
197                                        lobHandler.getLobCreator().setClobAsString(ps, 2, longContext);
198                                }
199                                else {
200                                        ps.setNull(2, getClobTypeToUse());
201                                }
202                                ps.setLong(3, executionId);
203                        }
204                });
205        }
206 
207        private String serializeContext(ExecutionContext ctx) {
208                Map<String, Object> m = new HashMap<String, Object>();
209                for (Entry<String, Object> me : ctx.entrySet()) {
210                        m.put(me.getKey(), me.getValue());
211                }
212                return serializer.serialize(m);
213        }
214 
215        private class ExecutionContextRowMapper implements ParameterizedRowMapper<ExecutionContext> {
216                public ExecutionContext mapRow(ResultSet rs, int i) throws SQLException {
217                        ExecutionContext executionContext = new ExecutionContext();
218                        String serializedContext = rs.getString("SERIALIZED_CONTEXT");
219                        if (serializedContext == null) {
220                                serializedContext = rs.getString("SHORT_CONTEXT");
221                        }
222                        Map<String, Object> map = serializer.deserialize(serializedContext);
223                        for (Map.Entry<String, Object> entry : map.entrySet()) {
224                                executionContext.put(entry.getKey(), entry.getValue());
225                        }
226                        return executionContext;
227                }
228        }
229 
230}

[all classes][org.springframework.batch.core.repository.dao]
EMMA 2.0.5312 (C) Vladimir Roubtsov