1 | /* |
2 | * Copyright 2006-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 org.springframework.aop.framework.ProxyFactory; |
20 | import org.springframework.aop.support.DefaultPointcutAdvisor; |
21 | import org.springframework.aop.support.NameMatchMethodPointcut; |
22 | import org.springframework.batch.core.repository.JobRepository; |
23 | import org.springframework.batch.core.repository.dao.ExecutionContextDao; |
24 | import org.springframework.batch.core.repository.dao.JobExecutionDao; |
25 | import org.springframework.batch.core.repository.dao.JobInstanceDao; |
26 | import org.springframework.batch.core.repository.dao.StepExecutionDao; |
27 | import org.springframework.batch.support.PropertiesConverter; |
28 | import org.springframework.beans.factory.FactoryBean; |
29 | import org.springframework.beans.factory.InitializingBean; |
30 | import org.springframework.transaction.PlatformTransactionManager; |
31 | import org.springframework.transaction.interceptor.TransactionInterceptor; |
32 | import org.springframework.util.Assert; |
33 | |
34 | /** |
35 | * A {@link FactoryBean} that automates the creation of a |
36 | * {@link SimpleJobRepository}. Declares abstract methods for providing DAO |
37 | * object implementations. |
38 | * |
39 | * @see JobRepositoryFactoryBean |
40 | * @see MapJobRepositoryFactoryBean |
41 | * |
42 | * @author Ben Hale |
43 | * @author Lucas Ward |
44 | * @author Robert Kasanicky |
45 | */ |
46 | public abstract class AbstractJobRepositoryFactoryBean implements FactoryBean, InitializingBean { |
47 | |
48 | private PlatformTransactionManager transactionManager; |
49 | |
50 | private ProxyFactory proxyFactory; |
51 | |
52 | private String isolationLevelForCreate = DEFAULT_ISOLATION_LEVEL; |
53 | |
54 | /** |
55 | * Default value for isolation level in create* method. |
56 | */ |
57 | private static final String DEFAULT_ISOLATION_LEVEL = "ISOLATION_SERIALIZABLE"; |
58 | |
59 | /** |
60 | * @return fully configured {@link JobInstanceDao} implementation. |
61 | */ |
62 | protected abstract JobInstanceDao createJobInstanceDao() throws Exception; |
63 | |
64 | /** |
65 | * @return fully configured {@link JobExecutionDao} implementation. |
66 | */ |
67 | protected abstract JobExecutionDao createJobExecutionDao() throws Exception; |
68 | |
69 | /** |
70 | * @return fully configured {@link StepExecutionDao} implementation. |
71 | */ |
72 | protected abstract StepExecutionDao createStepExecutionDao() throws Exception; |
73 | |
74 | /** |
75 | * @return fully configured {@link ExecutionContextDao} implementation. |
76 | */ |
77 | protected abstract ExecutionContextDao createExecutionContextDao() throws Exception; |
78 | |
79 | /** |
80 | * The type of object to be returned from {@link #getObject()}. |
81 | * |
82 | * @return JobRepository.class |
83 | * @see org.springframework.beans.factory.FactoryBean#getObjectType() |
84 | */ |
85 | public Class<JobRepository> getObjectType() { |
86 | return JobRepository.class; |
87 | } |
88 | |
89 | public boolean isSingleton() { |
90 | return true; |
91 | } |
92 | |
93 | /** |
94 | * public setter for the isolation level to be used for the transaction when |
95 | * job execution entities are initially created. The default is |
96 | * ISOLATION_SERIALIZABLE, which prevents accidental concurrent execution of |
97 | * the same job (ISOLATION_REPEATABLE_READ would work as well). |
98 | * |
99 | * @param isolationLevelForCreate the isolation level name to set |
100 | * |
101 | * @see SimpleJobRepository#createJobExecution(String, |
102 | * org.springframework.batch.core.JobParameters) |
103 | */ |
104 | public void setIsolationLevelForCreate(String isolationLevelForCreate) { |
105 | this.isolationLevelForCreate = isolationLevelForCreate; |
106 | } |
107 | |
108 | /** |
109 | * Public setter for the {@link PlatformTransactionManager}. |
110 | * @param transactionManager the transactionManager to set |
111 | */ |
112 | public void setTransactionManager(PlatformTransactionManager transactionManager) { |
113 | this.transactionManager = transactionManager; |
114 | } |
115 | |
116 | private void initializeProxy() throws Exception { |
117 | proxyFactory = new ProxyFactory(); |
118 | TransactionInterceptor advice = new TransactionInterceptor(transactionManager, PropertiesConverter |
119 | .stringToProperties("create*=PROPAGATION_REQUIRES_NEW," + isolationLevelForCreate |
120 | + "\ngetLastJobExecution*=PROPAGATION_REQUIRES_NEW," + isolationLevelForCreate |
121 | + "\n*=PROPAGATION_REQUIRED")); |
122 | DefaultPointcutAdvisor advisor = new DefaultPointcutAdvisor(advice); |
123 | NameMatchMethodPointcut pointcut = new NameMatchMethodPointcut(); |
124 | pointcut.addMethodName("*"); |
125 | advisor.setPointcut(pointcut); |
126 | proxyFactory.addAdvisor(advisor); |
127 | proxyFactory.setProxyTargetClass(false); |
128 | proxyFactory.addInterface(JobRepository.class); |
129 | proxyFactory.setTarget(getTarget()); |
130 | } |
131 | |
132 | public void afterPropertiesSet() throws Exception { |
133 | Assert.notNull(transactionManager, "TransactionManager must not be null."); |
134 | |
135 | initializeProxy(); |
136 | } |
137 | |
138 | private Object getTarget() throws Exception { |
139 | return new SimpleJobRepository(createJobInstanceDao(), createJobExecutionDao(), createStepExecutionDao(), |
140 | createExecutionContextDao()); |
141 | } |
142 | |
143 | public Object getObject() throws Exception { |
144 | if (proxyFactory == null) { |
145 | afterPropertiesSet(); |
146 | } |
147 | return proxyFactory.getProxy(); |
148 | } |
149 | |
150 | } |