| 1 | /* |
| 2 | * Copyright 2002-2007 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 javax.sql.DataSource; |
| 20 | |
| 21 | import org.springframework.batch.core.repository.dao.AbstractJdbcBatchMetadataDao; |
| 22 | import org.springframework.batch.core.repository.dao.JdbcJobExecutionDao; |
| 23 | import org.springframework.batch.core.repository.dao.JdbcJobInstanceDao; |
| 24 | import org.springframework.batch.core.repository.dao.JdbcStepExecutionDao; |
| 25 | import org.springframework.batch.core.repository.dao.JobExecutionDao; |
| 26 | import org.springframework.batch.core.repository.dao.JobInstanceDao; |
| 27 | import org.springframework.batch.core.repository.dao.StepExecutionDao; |
| 28 | import org.springframework.batch.item.database.support.DataFieldMaxValueIncrementerFactory; |
| 29 | import org.springframework.batch.item.database.support.DefaultDataFieldMaxValueIncrementerFactory; |
| 30 | import org.springframework.beans.factory.FactoryBean; |
| 31 | import org.springframework.beans.factory.InitializingBean; |
| 32 | import org.springframework.jdbc.core.JdbcTemplate; |
| 33 | import org.springframework.util.Assert; |
| 34 | import org.springframework.util.StringUtils; |
| 35 | |
| 36 | /** |
| 37 | * A {@link FactoryBean} that automates the creation of a {@link SimpleJobRepository}. Requires the |
| 38 | * user to describe what kind of database they are using. |
| 39 | * |
| 40 | * @author Ben Hale |
| 41 | * @author Lucas Ward |
| 42 | */ |
| 43 | public class JobRepositoryFactoryBean implements FactoryBean, InitializingBean { |
| 44 | |
| 45 | private DataSource dataSource; |
| 46 | |
| 47 | private String databaseType; |
| 48 | |
| 49 | private String tablePrefix = AbstractJdbcBatchMetadataDao.DEFAULT_TABLE_PREFIX; |
| 50 | |
| 51 | private DataFieldMaxValueIncrementerFactory incrementerFactory; |
| 52 | |
| 53 | public void setDataSource(DataSource dataSource) { |
| 54 | this.dataSource = dataSource; |
| 55 | } |
| 56 | |
| 57 | public void setDatabaseType(String dbType) { |
| 58 | this.databaseType = dbType; |
| 59 | } |
| 60 | |
| 61 | public void setTablePrefix(String tablePrefix) { |
| 62 | this.tablePrefix = tablePrefix; |
| 63 | } |
| 64 | |
| 65 | public void setIncrementerFactory(DataFieldMaxValueIncrementerFactory incrementerFactory) { |
| 66 | this.incrementerFactory = incrementerFactory; |
| 67 | } |
| 68 | |
| 69 | public void afterPropertiesSet() throws Exception { |
| 70 | Assert.notNull(dataSource, "Datasource must not be null."); |
| 71 | |
| 72 | if (incrementerFactory == null) { |
| 73 | incrementerFactory = new DefaultDataFieldMaxValueIncrementerFactory(dataSource); |
| 74 | } |
| 75 | |
| 76 | Assert.isTrue(incrementerFactory.isSupportedIncrementerType(databaseType), "'" + databaseType |
| 77 | + "' is an unsupported database type. The supported database types are " |
| 78 | + StringUtils.arrayToCommaDelimitedString(incrementerFactory.getSupportedIncrementerTypes())); |
| 79 | } |
| 80 | |
| 81 | public Object getObject() throws Exception { |
| 82 | JdbcTemplate jdbcTemplate = new JdbcTemplate(dataSource); |
| 83 | JobInstanceDao jobInstanceDao = createJobInstanceDao(jdbcTemplate); |
| 84 | JobExecutionDao jobExecutionDao = createJobExecutionDao(jdbcTemplate); |
| 85 | StepExecutionDao stepExecutionDao = createStepExecutionDao(jdbcTemplate); |
| 86 | return new SimpleJobRepository(jobInstanceDao, jobExecutionDao, stepExecutionDao); |
| 87 | } |
| 88 | |
| 89 | public Class getObjectType() { |
| 90 | return SimpleJobRepository.class; |
| 91 | } |
| 92 | |
| 93 | public boolean isSingleton() { |
| 94 | return true; |
| 95 | } |
| 96 | |
| 97 | private JobInstanceDao createJobInstanceDao(JdbcTemplate jdbcTemplate) throws Exception { |
| 98 | JdbcJobInstanceDao dao = new JdbcJobInstanceDao(); |
| 99 | dao.setJdbcTemplate(jdbcTemplate); |
| 100 | dao.setJobIncrementer(incrementerFactory.getIncrementer(databaseType, tablePrefix + "JOB_SEQ")); |
| 101 | dao.setTablePrefix(tablePrefix); |
| 102 | dao.afterPropertiesSet(); |
| 103 | return dao; |
| 104 | } |
| 105 | |
| 106 | private JobExecutionDao createJobExecutionDao(JdbcTemplate jdbcTemplate) throws Exception { |
| 107 | JdbcJobExecutionDao dao = new JdbcJobExecutionDao(); |
| 108 | dao.setJdbcTemplate(jdbcTemplate); |
| 109 | dao.setJobExecutionIncrementer(incrementerFactory.getIncrementer(databaseType, tablePrefix + "JOB_EXECUTION_SEQ")); |
| 110 | dao.setTablePrefix(tablePrefix); |
| 111 | dao.afterPropertiesSet(); |
| 112 | return dao; |
| 113 | } |
| 114 | |
| 115 | private StepExecutionDao createStepExecutionDao(JdbcTemplate jdbcTemplate) throws Exception { |
| 116 | JdbcStepExecutionDao dao = new JdbcStepExecutionDao(); |
| 117 | dao.setJdbcTemplate(jdbcTemplate); |
| 118 | dao.setStepExecutionIncrementer(incrementerFactory.getIncrementer(databaseType, tablePrefix + "STEP_EXECUTION_SEQ")); |
| 119 | dao.setTablePrefix(tablePrefix); |
| 120 | dao.afterPropertiesSet(); |
| 121 | return dao; |
| 122 | } |
| 123 | } |