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