View Javadoc

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