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 static org.springframework.batch.support.DatabaseType.SYBASE; |
20 | |
21 | import java.sql.Types; |
22 | |
23 | import javax.sql.DataSource; |
24 | |
25 | import org.apache.commons.logging.Log; |
26 | import org.apache.commons.logging.LogFactory; |
27 | import org.springframework.batch.core.repository.dao.AbstractJdbcBatchMetadataDao; |
28 | import org.springframework.batch.core.repository.dao.ExecutionContextDao; |
29 | import org.springframework.batch.core.repository.dao.JdbcExecutionContextDao; |
30 | import org.springframework.batch.core.repository.dao.JdbcJobExecutionDao; |
31 | import org.springframework.batch.core.repository.dao.JdbcJobInstanceDao; |
32 | import org.springframework.batch.core.repository.dao.JdbcStepExecutionDao; |
33 | import org.springframework.batch.core.repository.dao.JobExecutionDao; |
34 | import org.springframework.batch.core.repository.dao.JobInstanceDao; |
35 | import org.springframework.batch.core.repository.dao.StepExecutionDao; |
36 | import org.springframework.batch.item.database.support.DataFieldMaxValueIncrementerFactory; |
37 | import org.springframework.batch.item.database.support.DefaultDataFieldMaxValueIncrementerFactory; |
38 | import org.springframework.batch.support.DatabaseType; |
39 | import org.springframework.beans.factory.FactoryBean; |
40 | import org.springframework.beans.factory.InitializingBean; |
41 | import org.springframework.jdbc.core.simple.SimpleJdbcOperations; |
42 | import org.springframework.jdbc.core.simple.SimpleJdbcTemplate; |
43 | import org.springframework.util.Assert; |
44 | import org.springframework.util.StringUtils; |
45 | |
46 | /** |
47 | * A {@link FactoryBean} that automates the creation of a |
48 | * {@link SimpleJobRepository} using JDBC DAO implementations which persist |
49 | * batch metadata in database. Requires the user to describe what kind of |
50 | * database they are using. |
51 | * |
52 | * @author Ben Hale |
53 | * @author Lucas Ward |
54 | */ |
55 | public class JobRepositoryFactoryBean extends AbstractJobRepositoryFactoryBean implements InitializingBean { |
56 | |
57 | protected static final Log logger = LogFactory.getLog(JobRepositoryFactoryBean.class); |
58 | |
59 | private DataSource dataSource; |
60 | |
61 | private SimpleJdbcOperations jdbcTemplate; |
62 | |
63 | private String databaseType; |
64 | |
65 | private String tablePrefix = AbstractJdbcBatchMetadataDao.DEFAULT_TABLE_PREFIX; |
66 | |
67 | private DataFieldMaxValueIncrementerFactory incrementerFactory; |
68 | |
69 | private int exitMessageLength = AbstractJdbcBatchMetadataDao.DEFAULT_EXIT_MESSAGE_LENGTH; |
70 | |
71 | /** |
72 | * Public setter for the exit message length in database. Do not set this if |
73 | * you haven't modified the schema. Note this value will be used for both |
74 | * {@link JdbcJobExecutionDao} and {@link JdbcStepExecutionDao}. |
75 | * |
76 | * @param exitMessageLength the exitMessageLength to set |
77 | */ |
78 | public void setExitMessageLength(int exitMessageLength) { |
79 | this.exitMessageLength = exitMessageLength; |
80 | } |
81 | |
82 | /** |
83 | * Public setter for the {@link DataSource}. |
84 | * @param dataSource a {@link DataSource} |
85 | */ |
86 | public void setDataSource(DataSource dataSource) { |
87 | this.dataSource = dataSource; |
88 | } |
89 | |
90 | /** |
91 | * Sets the database type. |
92 | * @param dbType as specified by |
93 | * {@link DefaultDataFieldMaxValueIncrementerFactory} |
94 | */ |
95 | public void setDatabaseType(String dbType) { |
96 | this.databaseType = dbType; |
97 | } |
98 | |
99 | /** |
100 | * Sets the table prefix for all the batch meta-data tables. |
101 | * @param tablePrefix |
102 | */ |
103 | public void setTablePrefix(String tablePrefix) { |
104 | this.tablePrefix = tablePrefix; |
105 | } |
106 | |
107 | public void setIncrementerFactory(DataFieldMaxValueIncrementerFactory incrementerFactory) { |
108 | this.incrementerFactory = incrementerFactory; |
109 | } |
110 | |
111 | public void afterPropertiesSet() throws Exception { |
112 | |
113 | Assert.notNull(dataSource, "DataSource must not be null."); |
114 | |
115 | jdbcTemplate = new SimpleJdbcTemplate(dataSource); |
116 | |
117 | if (incrementerFactory == null) { |
118 | incrementerFactory = new DefaultDataFieldMaxValueIncrementerFactory(dataSource); |
119 | } |
120 | |
121 | if (databaseType == null) { |
122 | databaseType = DatabaseType.fromMetaData(dataSource).name(); |
123 | logger.info("No database type set, using meta data indicating: " + databaseType); |
124 | } |
125 | |
126 | Assert.isTrue(incrementerFactory.isSupportedIncrementerType(databaseType), "'" + databaseType |
127 | + "' is an unsupported database type. The supported database types are " |
128 | + StringUtils.arrayToCommaDelimitedString(incrementerFactory.getSupportedIncrementerTypes())); |
129 | |
130 | super.afterPropertiesSet(); |
131 | |
132 | } |
133 | |
134 | @Override |
135 | protected JobInstanceDao createJobInstanceDao() throws Exception { |
136 | JdbcJobInstanceDao dao = new JdbcJobInstanceDao(); |
137 | dao.setJdbcTemplate(jdbcTemplate); |
138 | dao.setJobIncrementer(incrementerFactory.getIncrementer(databaseType, tablePrefix + "JOB_SEQ")); |
139 | dao.setTablePrefix(tablePrefix); |
140 | dao.afterPropertiesSet(); |
141 | return dao; |
142 | } |
143 | |
144 | @Override |
145 | protected JobExecutionDao createJobExecutionDao() throws Exception { |
146 | JdbcJobExecutionDao dao = new JdbcJobExecutionDao(); |
147 | dao.setJdbcTemplate(jdbcTemplate); |
148 | dao.setJobExecutionIncrementer(incrementerFactory.getIncrementer(databaseType, tablePrefix |
149 | + "JOB_EXECUTION_SEQ")); |
150 | dao.setTablePrefix(tablePrefix); |
151 | dao.setClobTypeToUse(determineClobTypeToUse(this.databaseType)); |
152 | dao.setExitMessageLength(exitMessageLength); |
153 | dao.afterPropertiesSet(); |
154 | return dao; |
155 | } |
156 | |
157 | @Override |
158 | protected StepExecutionDao createStepExecutionDao() throws Exception { |
159 | JdbcStepExecutionDao dao = new JdbcStepExecutionDao(); |
160 | dao.setJdbcTemplate(jdbcTemplate); |
161 | dao.setStepExecutionIncrementer(incrementerFactory.getIncrementer(databaseType, tablePrefix |
162 | + "STEP_EXECUTION_SEQ")); |
163 | dao.setTablePrefix(tablePrefix); |
164 | dao.setClobTypeToUse(determineClobTypeToUse(this.databaseType)); |
165 | dao.setExitMessageLength(exitMessageLength); |
166 | dao.afterPropertiesSet(); |
167 | return dao; |
168 | } |
169 | |
170 | @Override |
171 | protected ExecutionContextDao createExecutionContextDao() throws Exception { |
172 | JdbcExecutionContextDao dao = new JdbcExecutionContextDao(); |
173 | dao.setJdbcTemplate(jdbcTemplate); |
174 | dao.setTablePrefix(tablePrefix); |
175 | dao.setClobTypeToUse(determineClobTypeToUse(this.databaseType)); |
176 | dao.afterPropertiesSet(); |
177 | return dao; |
178 | } |
179 | |
180 | private int determineClobTypeToUse(String databaseType) { |
181 | if (SYBASE == DatabaseType.valueOf(databaseType.toUpperCase())) { |
182 | return Types.LONGVARCHAR; |
183 | } |
184 | else { |
185 | return Types.CLOB; |
186 | } |
187 | } |
188 | |
189 | } |