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.jdbc.core.JdbcOperations; |
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 |
38 | * {@link SimpleJobRepository} using JDBC DAO implementations which persist |
39 | * batch metadata in database. Requires the user to describe what kind of |
40 | * database they are using. |
41 | * |
42 | * @author Ben Hale |
43 | * @author Lucas Ward |
44 | */ |
45 | public class JobRepositoryFactoryBean extends AbstractJobRepositoryFactoryBean { |
46 | |
47 | private DataSource dataSource; |
48 | |
49 | private JdbcOperations jdbcTemplate; |
50 | |
51 | private String databaseType; |
52 | |
53 | private String tablePrefix = AbstractJdbcBatchMetadataDao.DEFAULT_TABLE_PREFIX; |
54 | |
55 | private DataFieldMaxValueIncrementerFactory incrementerFactory; |
56 | |
57 | private int exitMessageLength = AbstractJdbcBatchMetadataDao.DEFAULT_EXIT_MESSAGE_LENGTH; |
58 | |
59 | /** |
60 | * Public setter for the exit message length in database. Do not set this if |
61 | * you haven't modified the schema. Note this value will be used for both |
62 | * {@link JdbcJobExecutionDao} and {@link JdbcStepExecutionDao}. |
63 | * |
64 | * @param exitMessageLength the exitMessageLength to set |
65 | */ |
66 | public void setExitMessageLength(int exitMessageLength) { |
67 | this.exitMessageLength = exitMessageLength; |
68 | } |
69 | |
70 | /** |
71 | * Public setter for the {@link DataSource}. |
72 | * @param dataSource a {@link DataSource} |
73 | */ |
74 | public void setDataSource(DataSource dataSource) { |
75 | this.dataSource = dataSource; |
76 | } |
77 | |
78 | /** |
79 | * Sets the database type. |
80 | * @param dbType as specified by |
81 | * {@link DefaultDataFieldMaxValueIncrementerFactory} |
82 | */ |
83 | public void setDatabaseType(String dbType) { |
84 | this.databaseType = dbType; |
85 | } |
86 | |
87 | /** |
88 | * Sets the table prefix for all the batch meta-data tables. |
89 | * @param tablePrefix |
90 | */ |
91 | public void setTablePrefix(String tablePrefix) { |
92 | this.tablePrefix = tablePrefix; |
93 | } |
94 | |
95 | public void setIncrementerFactory(DataFieldMaxValueIncrementerFactory incrementerFactory) { |
96 | this.incrementerFactory = incrementerFactory; |
97 | } |
98 | |
99 | public void afterPropertiesSet() throws Exception { |
100 | |
101 | Assert.notNull(dataSource, "DataSource must not be null."); |
102 | jdbcTemplate = new JdbcTemplate(dataSource); |
103 | |
104 | if (incrementerFactory == null) { |
105 | incrementerFactory = new DefaultDataFieldMaxValueIncrementerFactory(dataSource); |
106 | } |
107 | |
108 | Assert.isTrue(incrementerFactory.isSupportedIncrementerType(databaseType), "'" + databaseType |
109 | + "' is an unsupported database type. The supported database types are " |
110 | + StringUtils.arrayToCommaDelimitedString(incrementerFactory.getSupportedIncrementerTypes())); |
111 | |
112 | super.afterPropertiesSet(); |
113 | } |
114 | |
115 | protected JobInstanceDao createJobInstanceDao() throws Exception { |
116 | JdbcJobInstanceDao dao = new JdbcJobInstanceDao(); |
117 | dao.setJdbcTemplate(jdbcTemplate); |
118 | dao.setJobIncrementer(incrementerFactory.getIncrementer(databaseType, tablePrefix + "JOB_SEQ")); |
119 | dao.setTablePrefix(tablePrefix); |
120 | dao.afterPropertiesSet(); |
121 | return dao; |
122 | } |
123 | |
124 | protected JobExecutionDao createJobExecutionDao() throws Exception { |
125 | JdbcJobExecutionDao dao = new JdbcJobExecutionDao(); |
126 | dao.setJdbcTemplate(jdbcTemplate); |
127 | dao.setJobExecutionIncrementer(incrementerFactory.getIncrementer(databaseType, tablePrefix |
128 | + "JOB_EXECUTION_SEQ")); |
129 | dao.setTablePrefix(tablePrefix); |
130 | dao.setExitMessageLength(exitMessageLength); |
131 | dao.afterPropertiesSet(); |
132 | return dao; |
133 | } |
134 | |
135 | protected StepExecutionDao createStepExecutionDao() throws Exception { |
136 | JdbcStepExecutionDao dao = new JdbcStepExecutionDao(); |
137 | dao.setJdbcTemplate(jdbcTemplate); |
138 | dao.setStepExecutionIncrementer(incrementerFactory.getIncrementer(databaseType, tablePrefix |
139 | + "STEP_EXECUTION_SEQ")); |
140 | dao.setTablePrefix(tablePrefix); |
141 | dao.setExitMessageLength(exitMessageLength); |
142 | dao.afterPropertiesSet(); |
143 | return dao; |
144 | } |
145 | } |