| 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.explore.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.ExecutionContextDao; |
| 23 | import org.springframework.batch.core.repository.dao.JdbcExecutionContextDao; |
| 24 | import org.springframework.batch.core.repository.dao.JdbcJobExecutionDao; |
| 25 | import org.springframework.batch.core.repository.dao.JdbcJobInstanceDao; |
| 26 | import org.springframework.batch.core.repository.dao.JdbcStepExecutionDao; |
| 27 | import org.springframework.batch.core.repository.dao.JobExecutionDao; |
| 28 | import org.springframework.batch.core.repository.dao.JobInstanceDao; |
| 29 | import org.springframework.batch.core.repository.dao.StepExecutionDao; |
| 30 | import org.springframework.batch.item.ExecutionContext; |
| 31 | import org.springframework.beans.factory.FactoryBean; |
| 32 | import org.springframework.beans.factory.InitializingBean; |
| 33 | import org.springframework.jdbc.core.simple.SimpleJdbcOperations; |
| 34 | import org.springframework.jdbc.core.simple.SimpleJdbcTemplate; |
| 35 | import org.springframework.jdbc.support.incrementer.AbstractDataFieldMaxValueIncrementer; |
| 36 | import org.springframework.jdbc.support.incrementer.DataFieldMaxValueIncrementer; |
| 37 | import org.springframework.jdbc.support.lob.LobHandler; |
| 38 | import org.springframework.util.Assert; |
| 39 | |
| 40 | /** |
| 41 | * A {@link FactoryBean} that automates the creation of a |
| 42 | * {@link SimpleJobExplorer} using JDBC DAO implementations. Requires the user |
| 43 | * to describe what kind of database they are using. |
| 44 | * |
| 45 | * @author Dave Syer |
| 46 | * @since 2.0 |
| 47 | */ |
| 48 | public class JobExplorerFactoryBean extends AbstractJobExplorerFactoryBean |
| 49 | implements InitializingBean { |
| 50 | |
| 51 | private DataSource dataSource; |
| 52 | |
| 53 | private SimpleJdbcOperations jdbcTemplate; |
| 54 | |
| 55 | private String tablePrefix = AbstractJdbcBatchMetadataDao.DEFAULT_TABLE_PREFIX; |
| 56 | |
| 57 | private DataFieldMaxValueIncrementer incrementer = new AbstractDataFieldMaxValueIncrementer() { |
| 58 | @Override |
| 59 | protected long getNextKey() { |
| 60 | throw new IllegalStateException("JobExplorer is read only."); |
| 61 | } |
| 62 | }; |
| 63 | |
| 64 | private LobHandler lobHandler; |
| 65 | |
| 66 | /** |
| 67 | * Public setter for the {@link DataSource}. |
| 68 | * |
| 69 | * @param dataSource |
| 70 | * a {@link DataSource} |
| 71 | */ |
| 72 | public void setDataSource(DataSource dataSource) { |
| 73 | this.dataSource = dataSource; |
| 74 | } |
| 75 | |
| 76 | /** |
| 77 | * Sets the table prefix for all the batch meta-data tables. |
| 78 | * |
| 79 | * @param tablePrefix |
| 80 | */ |
| 81 | public void setTablePrefix(String tablePrefix) { |
| 82 | this.tablePrefix = tablePrefix; |
| 83 | } |
| 84 | |
| 85 | /** |
| 86 | * The lob handler to use when saving {@link ExecutionContext} instances. |
| 87 | * Defaults to null which works for most databases. |
| 88 | * |
| 89 | * @param lobHandler |
| 90 | */ |
| 91 | public void setLobHandler(LobHandler lobHandler) { |
| 92 | this.lobHandler = lobHandler; |
| 93 | } |
| 94 | |
| 95 | public void afterPropertiesSet() throws Exception { |
| 96 | |
| 97 | Assert.notNull(dataSource, "DataSource must not be null."); |
| 98 | |
| 99 | jdbcTemplate = new SimpleJdbcTemplate(dataSource); |
| 100 | |
| 101 | } |
| 102 | |
| 103 | private Object getTarget() throws Exception { |
| 104 | return new SimpleJobExplorer(createJobInstanceDao(), |
| 105 | createJobExecutionDao(), createStepExecutionDao(), |
| 106 | createExecutionContextDao()); |
| 107 | } |
| 108 | |
| 109 | @Override |
| 110 | protected ExecutionContextDao createExecutionContextDao() throws Exception { |
| 111 | JdbcExecutionContextDao dao = new JdbcExecutionContextDao(); |
| 112 | dao.setJdbcTemplate(jdbcTemplate); |
| 113 | dao.setLobHandler(lobHandler); |
| 114 | dao.setTablePrefix(tablePrefix); |
| 115 | dao.afterPropertiesSet(); |
| 116 | return dao; |
| 117 | } |
| 118 | |
| 119 | @Override |
| 120 | protected JobInstanceDao createJobInstanceDao() throws Exception { |
| 121 | JdbcJobInstanceDao dao = new JdbcJobInstanceDao(); |
| 122 | dao.setJdbcTemplate(jdbcTemplate); |
| 123 | dao.setJobIncrementer(incrementer); |
| 124 | dao.setTablePrefix(tablePrefix); |
| 125 | dao.afterPropertiesSet(); |
| 126 | return dao; |
| 127 | } |
| 128 | |
| 129 | @Override |
| 130 | protected JobExecutionDao createJobExecutionDao() throws Exception { |
| 131 | JdbcJobExecutionDao dao = new JdbcJobExecutionDao(); |
| 132 | dao.setJdbcTemplate(jdbcTemplate); |
| 133 | dao.setJobExecutionIncrementer(incrementer); |
| 134 | dao.setTablePrefix(tablePrefix); |
| 135 | dao.afterPropertiesSet(); |
| 136 | return dao; |
| 137 | } |
| 138 | |
| 139 | protected StepExecutionDao createStepExecutionDao() throws Exception { |
| 140 | JdbcStepExecutionDao dao = new JdbcStepExecutionDao(); |
| 141 | dao.setJdbcTemplate(jdbcTemplate); |
| 142 | dao.setStepExecutionIncrementer(incrementer); |
| 143 | dao.setTablePrefix(tablePrefix); |
| 144 | dao.afterPropertiesSet(); |
| 145 | return dao; |
| 146 | } |
| 147 | |
| 148 | public Object getObject() throws Exception { |
| 149 | return getTarget(); |
| 150 | } |
| 151 | } |