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.jdbc.support.lob.LobHandler; |
44 | import org.springframework.jdbc.support.lob.OracleLobHandler; |
45 | import org.springframework.util.Assert; |
46 | import org.springframework.util.StringUtils; |
47 | |
48 | /** |
49 | * A {@link FactoryBean} that automates the creation of a |
50 | * {@link SimpleJobRepository} using JDBC DAO implementations which persist |
51 | * batch metadata in database. Requires the user to describe what kind of |
52 | * database they are using. |
53 | * |
54 | * @author Ben Hale |
55 | * @author Lucas Ward |
56 | * @author Dave Syer |
57 | */ |
58 | public class JobRepositoryFactoryBean extends AbstractJobRepositoryFactoryBean implements InitializingBean { |
59 | |
60 | protected static final Log logger = LogFactory.getLog(JobRepositoryFactoryBean.class); |
61 | |
62 | private DataSource dataSource; |
63 | |
64 | private SimpleJdbcOperations jdbcTemplate; |
65 | |
66 | private String databaseType; |
67 | |
68 | private String tablePrefix = AbstractJdbcBatchMetadataDao.DEFAULT_TABLE_PREFIX; |
69 | |
70 | private DataFieldMaxValueIncrementerFactory incrementerFactory; |
71 | |
72 | private int maxVarCharLength = AbstractJdbcBatchMetadataDao.DEFAULT_EXIT_MESSAGE_LENGTH; |
73 | |
74 | private LobHandler lobHandler; |
75 | |
76 | /** |
77 | * A special handler for large objects. The default is usually fine, except |
78 | * for some (usually older) versions of Oracle. The default is determined |
79 | * from the data base type. |
80 | * |
81 | * @param lobHandler the {@link LobHandler} to set |
82 | * |
83 | * @see LobHandler |
84 | */ |
85 | public void setLobHandler(LobHandler lobHandler) { |
86 | this.lobHandler = lobHandler; |
87 | } |
88 | |
89 | /** |
90 | * Public setter for the length of long string columns in database. Do not |
91 | * set this if you haven't modified the schema. Note this value will be used |
92 | * for the exit message in both {@link JdbcJobExecutionDao} and |
93 | * {@link JdbcStepExecutionDao} and also the short version of the execution |
94 | * context in {@link JdbcExecutionContextDao} . For databases with |
95 | * multi-byte character sets this number can be smaller (by up to a factor |
96 | * of 2 for 2-byte characters) than the declaration of the column length in |
97 | * the DDL for the tables. |
98 | * |
99 | * @param maxVarCharLength the exitMessageLength to set |
100 | */ |
101 | public void setMaxVarCharLength(int maxVarCharLength) { |
102 | this.maxVarCharLength = maxVarCharLength; |
103 | } |
104 | |
105 | /** |
106 | * Public setter for the {@link DataSource}. |
107 | * @param dataSource a {@link DataSource} |
108 | */ |
109 | public void setDataSource(DataSource dataSource) { |
110 | this.dataSource = dataSource; |
111 | } |
112 | |
113 | /** |
114 | * Sets the database type. |
115 | * @param dbType as specified by |
116 | * {@link DefaultDataFieldMaxValueIncrementerFactory} |
117 | */ |
118 | public void setDatabaseType(String dbType) { |
119 | this.databaseType = dbType; |
120 | } |
121 | |
122 | /** |
123 | * Sets the table prefix for all the batch meta-data tables. |
124 | * @param tablePrefix |
125 | */ |
126 | public void setTablePrefix(String tablePrefix) { |
127 | this.tablePrefix = tablePrefix; |
128 | } |
129 | |
130 | public void setIncrementerFactory(DataFieldMaxValueIncrementerFactory incrementerFactory) { |
131 | this.incrementerFactory = incrementerFactory; |
132 | } |
133 | |
134 | public void afterPropertiesSet() throws Exception { |
135 | |
136 | Assert.notNull(dataSource, "DataSource must not be null."); |
137 | |
138 | jdbcTemplate = new SimpleJdbcTemplate(dataSource); |
139 | |
140 | if (incrementerFactory == null) { |
141 | incrementerFactory = new DefaultDataFieldMaxValueIncrementerFactory(dataSource); |
142 | } |
143 | |
144 | if (databaseType == null) { |
145 | databaseType = DatabaseType.fromMetaData(dataSource).name(); |
146 | logger.info("No database type set, using meta data indicating: " + databaseType); |
147 | } |
148 | |
149 | if (lobHandler == null && databaseType.equalsIgnoreCase(DatabaseType.ORACLE.toString())) { |
150 | lobHandler = new OracleLobHandler(); |
151 | } |
152 | |
153 | Assert.isTrue(incrementerFactory.isSupportedIncrementerType(databaseType), "'" + databaseType |
154 | + "' is an unsupported database type. The supported database types are " |
155 | + StringUtils.arrayToCommaDelimitedString(incrementerFactory.getSupportedIncrementerTypes())); |
156 | |
157 | super.afterPropertiesSet(); |
158 | |
159 | } |
160 | |
161 | @Override |
162 | protected JobInstanceDao createJobInstanceDao() throws Exception { |
163 | JdbcJobInstanceDao dao = new JdbcJobInstanceDao(); |
164 | dao.setJdbcTemplate(jdbcTemplate); |
165 | dao.setJobIncrementer(incrementerFactory.getIncrementer(databaseType, tablePrefix + "JOB_SEQ")); |
166 | dao.setTablePrefix(tablePrefix); |
167 | dao.afterPropertiesSet(); |
168 | return dao; |
169 | } |
170 | |
171 | @Override |
172 | protected JobExecutionDao createJobExecutionDao() throws Exception { |
173 | JdbcJobExecutionDao dao = new JdbcJobExecutionDao(); |
174 | dao.setJdbcTemplate(jdbcTemplate); |
175 | dao.setJobExecutionIncrementer(incrementerFactory.getIncrementer(databaseType, tablePrefix |
176 | + "JOB_EXECUTION_SEQ")); |
177 | dao.setTablePrefix(tablePrefix); |
178 | dao.setClobTypeToUse(determineClobTypeToUse(this.databaseType)); |
179 | dao.setExitMessageLength(maxVarCharLength); |
180 | dao.afterPropertiesSet(); |
181 | return dao; |
182 | } |
183 | |
184 | @Override |
185 | protected StepExecutionDao createStepExecutionDao() throws Exception { |
186 | JdbcStepExecutionDao dao = new JdbcStepExecutionDao(); |
187 | dao.setJdbcTemplate(jdbcTemplate); |
188 | dao.setStepExecutionIncrementer(incrementerFactory.getIncrementer(databaseType, tablePrefix |
189 | + "STEP_EXECUTION_SEQ")); |
190 | dao.setTablePrefix(tablePrefix); |
191 | dao.setClobTypeToUse(determineClobTypeToUse(this.databaseType)); |
192 | dao.setExitMessageLength(maxVarCharLength); |
193 | dao.afterPropertiesSet(); |
194 | return dao; |
195 | } |
196 | |
197 | @Override |
198 | protected ExecutionContextDao createExecutionContextDao() throws Exception { |
199 | JdbcExecutionContextDao dao = new JdbcExecutionContextDao(); |
200 | dao.setJdbcTemplate(jdbcTemplate); |
201 | dao.setTablePrefix(tablePrefix); |
202 | dao.setClobTypeToUse(determineClobTypeToUse(this.databaseType)); |
203 | if (lobHandler != null) { |
204 | dao.setLobHandler(lobHandler); |
205 | } |
206 | dao.afterPropertiesSet(); |
207 | // Assume the same length. |
208 | dao.setShortContextLength(maxVarCharLength); |
209 | return dao; |
210 | } |
211 | |
212 | private int determineClobTypeToUse(String databaseType) { |
213 | if (SYBASE == DatabaseType.valueOf(databaseType.toUpperCase())) { |
214 | return Types.LONGVARCHAR; |
215 | } |
216 | else { |
217 | return Types.CLOB; |
218 | } |
219 | } |
220 | |
221 | } |