| 1 | /* |
| 2 | * Copyright 2002-2014 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.support; |
| 18 | |
| 19 | import static org.springframework.batch.support.DatabaseType.SYBASE; |
| 20 | |
| 21 | import java.lang.reflect.Field; |
| 22 | import java.sql.Types; |
| 23 | |
| 24 | import javax.sql.DataSource; |
| 25 | |
| 26 | import org.apache.commons.logging.Log; |
| 27 | import org.apache.commons.logging.LogFactory; |
| 28 | import org.springframework.batch.core.repository.ExecutionContextSerializer; |
| 29 | import org.springframework.batch.core.repository.dao.AbstractJdbcBatchMetadataDao; |
| 30 | import org.springframework.batch.core.repository.dao.ExecutionContextDao; |
| 31 | import org.springframework.batch.core.repository.dao.JdbcExecutionContextDao; |
| 32 | import org.springframework.batch.core.repository.dao.JdbcJobExecutionDao; |
| 33 | import org.springframework.batch.core.repository.dao.JdbcJobInstanceDao; |
| 34 | import org.springframework.batch.core.repository.dao.JdbcStepExecutionDao; |
| 35 | import org.springframework.batch.core.repository.dao.JobExecutionDao; |
| 36 | import org.springframework.batch.core.repository.dao.JobInstanceDao; |
| 37 | import org.springframework.batch.core.repository.dao.StepExecutionDao; |
| 38 | import org.springframework.batch.core.repository.dao.XStreamExecutionContextStringSerializer; |
| 39 | import org.springframework.batch.item.database.support.DataFieldMaxValueIncrementerFactory; |
| 40 | import org.springframework.batch.item.database.support.DefaultDataFieldMaxValueIncrementerFactory; |
| 41 | import org.springframework.batch.support.DatabaseType; |
| 42 | import org.springframework.beans.factory.FactoryBean; |
| 43 | import org.springframework.beans.factory.InitializingBean; |
| 44 | import org.springframework.jdbc.core.JdbcOperations; |
| 45 | import org.springframework.jdbc.core.JdbcTemplate; |
| 46 | import org.springframework.jdbc.support.lob.LobHandler; |
| 47 | import org.springframework.jdbc.support.lob.OracleLobHandler; |
| 48 | import org.springframework.util.Assert; |
| 49 | import org.springframework.util.StringUtils; |
| 50 | |
| 51 | /** |
| 52 | * A {@link FactoryBean} that automates the creation of a |
| 53 | * {@link SimpleJobRepository} using JDBC DAO implementations which persist |
| 54 | * batch metadata in database. Requires the user to describe what kind of |
| 55 | * database they are using. |
| 56 | * |
| 57 | * @author Ben Hale |
| 58 | * @author Lucas Ward |
| 59 | * @author Dave Syer |
| 60 | * @author Michael Minella |
| 61 | */ |
| 62 | public class JobRepositoryFactoryBean extends AbstractJobRepositoryFactoryBean implements InitializingBean { |
| 63 | |
| 64 | protected static final Log logger = LogFactory.getLog(JobRepositoryFactoryBean.class); |
| 65 | |
| 66 | private DataSource dataSource; |
| 67 | |
| 68 | private JdbcOperations jdbcTemplate; |
| 69 | |
| 70 | private String databaseType; |
| 71 | |
| 72 | private String tablePrefix = AbstractJdbcBatchMetadataDao.DEFAULT_TABLE_PREFIX; |
| 73 | |
| 74 | private DataFieldMaxValueIncrementerFactory incrementerFactory; |
| 75 | |
| 76 | private int maxVarCharLength = AbstractJdbcBatchMetadataDao.DEFAULT_EXIT_MESSAGE_LENGTH; |
| 77 | |
| 78 | private LobHandler lobHandler; |
| 79 | |
| 80 | private ExecutionContextSerializer serializer; |
| 81 | |
| 82 | private Integer lobType; |
| 83 | |
| 84 | /** |
| 85 | * @param type a value from the {@link java.sql.Types} class to indicate the type to use for a CLOB |
| 86 | */ |
| 87 | public void setClobType(int type) { |
| 88 | this.lobType = type; |
| 89 | } |
| 90 | |
| 91 | /** |
| 92 | * A custom implementation of the {@link ExecutionContextSerializer}. |
| 93 | * The default, if not injected, is the {@link XStreamExecutionContextStringSerializer}. |
| 94 | * |
| 95 | * @param serializer |
| 96 | * @see ExecutionContextSerializer |
| 97 | */ |
| 98 | public void setSerializer(ExecutionContextSerializer serializer) { |
| 99 | this.serializer = serializer; |
| 100 | } |
| 101 | |
| 102 | /** |
| 103 | * A special handler for large objects. The default is usually fine, except |
| 104 | * for some (usually older) versions of Oracle. The default is determined |
| 105 | * from the data base type. |
| 106 | * |
| 107 | * @param lobHandler the {@link LobHandler} to set |
| 108 | * |
| 109 | * @see LobHandler |
| 110 | */ |
| 111 | public void setLobHandler(LobHandler lobHandler) { |
| 112 | this.lobHandler = lobHandler; |
| 113 | } |
| 114 | |
| 115 | /** |
| 116 | * Public setter for the length of long string columns in database. Do not |
| 117 | * set this if you haven't modified the schema. Note this value will be used |
| 118 | * for the exit message in both {@link JdbcJobExecutionDao} and |
| 119 | * {@link JdbcStepExecutionDao} and also the short version of the execution |
| 120 | * context in {@link JdbcExecutionContextDao} . For databases with |
| 121 | * multi-byte character sets this number can be smaller (by up to a factor |
| 122 | * of 2 for 2-byte characters) than the declaration of the column length in |
| 123 | * the DDL for the tables. |
| 124 | * |
| 125 | * @param maxVarCharLength the exitMessageLength to set |
| 126 | */ |
| 127 | public void setMaxVarCharLength(int maxVarCharLength) { |
| 128 | this.maxVarCharLength = maxVarCharLength; |
| 129 | } |
| 130 | |
| 131 | /** |
| 132 | * Public setter for the {@link DataSource}. |
| 133 | * @param dataSource a {@link DataSource} |
| 134 | */ |
| 135 | public void setDataSource(DataSource dataSource) { |
| 136 | this.dataSource = dataSource; |
| 137 | } |
| 138 | |
| 139 | /** |
| 140 | * Sets the database type. |
| 141 | * @param dbType as specified by |
| 142 | * {@link DefaultDataFieldMaxValueIncrementerFactory} |
| 143 | */ |
| 144 | public void setDatabaseType(String dbType) { |
| 145 | this.databaseType = dbType; |
| 146 | } |
| 147 | |
| 148 | /** |
| 149 | * Sets the table prefix for all the batch meta-data tables. |
| 150 | * @param tablePrefix |
| 151 | */ |
| 152 | public void setTablePrefix(String tablePrefix) { |
| 153 | this.tablePrefix = tablePrefix; |
| 154 | } |
| 155 | |
| 156 | public void setIncrementerFactory(DataFieldMaxValueIncrementerFactory incrementerFactory) { |
| 157 | this.incrementerFactory = incrementerFactory; |
| 158 | } |
| 159 | |
| 160 | @Override |
| 161 | public void afterPropertiesSet() throws Exception { |
| 162 | |
| 163 | Assert.notNull(dataSource, "DataSource must not be null."); |
| 164 | |
| 165 | jdbcTemplate = new JdbcTemplate(dataSource); |
| 166 | |
| 167 | if (incrementerFactory == null) { |
| 168 | incrementerFactory = new DefaultDataFieldMaxValueIncrementerFactory(dataSource); |
| 169 | } |
| 170 | |
| 171 | if (databaseType == null) { |
| 172 | databaseType = DatabaseType.fromMetaData(dataSource).name(); |
| 173 | logger.info("No database type set, using meta data indicating: " + databaseType); |
| 174 | } |
| 175 | |
| 176 | if (lobHandler == null && databaseType.equalsIgnoreCase(DatabaseType.ORACLE.toString())) { |
| 177 | lobHandler = new OracleLobHandler(); |
| 178 | } |
| 179 | |
| 180 | if(serializer == null) { |
| 181 | XStreamExecutionContextStringSerializer defaultSerializer = new XStreamExecutionContextStringSerializer(); |
| 182 | defaultSerializer.afterPropertiesSet(); |
| 183 | |
| 184 | serializer = defaultSerializer; |
| 185 | } |
| 186 | |
| 187 | Assert.isTrue(incrementerFactory.isSupportedIncrementerType(databaseType), "'" + databaseType |
| 188 | + "' is an unsupported database type. The supported database types are " |
| 189 | + StringUtils.arrayToCommaDelimitedString(incrementerFactory.getSupportedIncrementerTypes())); |
| 190 | |
| 191 | if(lobType != null) { |
| 192 | Assert.isTrue(isValidTypes(lobType), "lobType must be a value from the java.sql.Types class"); |
| 193 | } |
| 194 | |
| 195 | super.afterPropertiesSet(); |
| 196 | } |
| 197 | |
| 198 | @Override |
| 199 | protected JobInstanceDao createJobInstanceDao() throws Exception { |
| 200 | JdbcJobInstanceDao dao = new JdbcJobInstanceDao(); |
| 201 | dao.setJdbcTemplate(jdbcTemplate); |
| 202 | dao.setJobIncrementer(incrementerFactory.getIncrementer(databaseType, tablePrefix + "JOB_SEQ")); |
| 203 | dao.setTablePrefix(tablePrefix); |
| 204 | dao.afterPropertiesSet(); |
| 205 | return dao; |
| 206 | } |
| 207 | |
| 208 | @Override |
| 209 | protected JobExecutionDao createJobExecutionDao() throws Exception { |
| 210 | JdbcJobExecutionDao dao = new JdbcJobExecutionDao(); |
| 211 | dao.setJdbcTemplate(jdbcTemplate); |
| 212 | dao.setJobExecutionIncrementer(incrementerFactory.getIncrementer(databaseType, tablePrefix |
| 213 | + "JOB_EXECUTION_SEQ")); |
| 214 | dao.setTablePrefix(tablePrefix); |
| 215 | dao.setClobTypeToUse(determineClobTypeToUse(this.databaseType)); |
| 216 | dao.setExitMessageLength(maxVarCharLength); |
| 217 | dao.afterPropertiesSet(); |
| 218 | return dao; |
| 219 | } |
| 220 | |
| 221 | @Override |
| 222 | protected StepExecutionDao createStepExecutionDao() throws Exception { |
| 223 | JdbcStepExecutionDao dao = new JdbcStepExecutionDao(); |
| 224 | dao.setJdbcTemplate(jdbcTemplate); |
| 225 | dao.setStepExecutionIncrementer(incrementerFactory.getIncrementer(databaseType, tablePrefix |
| 226 | + "STEP_EXECUTION_SEQ")); |
| 227 | dao.setTablePrefix(tablePrefix); |
| 228 | dao.setClobTypeToUse(determineClobTypeToUse(this.databaseType)); |
| 229 | dao.setExitMessageLength(maxVarCharLength); |
| 230 | dao.afterPropertiesSet(); |
| 231 | return dao; |
| 232 | } |
| 233 | |
| 234 | @Override |
| 235 | protected ExecutionContextDao createExecutionContextDao() throws Exception { |
| 236 | JdbcExecutionContextDao dao = new JdbcExecutionContextDao(); |
| 237 | dao.setJdbcTemplate(jdbcTemplate); |
| 238 | dao.setTablePrefix(tablePrefix); |
| 239 | dao.setClobTypeToUse(determineClobTypeToUse(this.databaseType)); |
| 240 | dao.setSerializer(serializer); |
| 241 | |
| 242 | if (lobHandler != null) { |
| 243 | dao.setLobHandler(lobHandler); |
| 244 | } |
| 245 | |
| 246 | dao.afterPropertiesSet(); |
| 247 | // Assume the same length. |
| 248 | dao.setShortContextLength(maxVarCharLength); |
| 249 | return dao; |
| 250 | } |
| 251 | |
| 252 | private int determineClobTypeToUse(String databaseType) throws Exception { |
| 253 | if(lobType != null) { |
| 254 | return lobType; |
| 255 | } else { |
| 256 | if (SYBASE == DatabaseType.valueOf(databaseType.toUpperCase())) { |
| 257 | return Types.LONGVARCHAR; |
| 258 | } |
| 259 | else { |
| 260 | return Types.CLOB; |
| 261 | } |
| 262 | } |
| 263 | } |
| 264 | |
| 265 | private boolean isValidTypes(int value) throws Exception { |
| 266 | boolean result = false; |
| 267 | |
| 268 | for (Field field : Types.class.getFields()) { |
| 269 | int curValue = field.getInt(null); |
| 270 | if(curValue == value) { |
| 271 | result = true; |
| 272 | break; |
| 273 | } |
| 274 | } |
| 275 | |
| 276 | return result; |
| 277 | } |
| 278 | |
| 279 | } |