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.job; |
18 | |
19 | import java.util.ArrayList; |
20 | import java.util.List; |
21 | |
22 | import org.springframework.batch.core.Job; |
23 | import org.springframework.batch.core.JobExecutionListener; |
24 | import org.springframework.batch.core.Step; |
25 | import org.springframework.batch.core.listener.CompositeExecutionJobListener; |
26 | import org.springframework.batch.core.repository.JobRepository; |
27 | import org.springframework.beans.factory.BeanNameAware; |
28 | import org.springframework.beans.factory.InitializingBean; |
29 | import org.springframework.util.Assert; |
30 | import org.springframework.util.ClassUtils; |
31 | |
32 | /** |
33 | * Batch domain object representing a job. Job is an explicit abstraction |
34 | * representing the configuration of a job specified by a developer. It should |
35 | * be noted that restart policy is applied to the job as a whole and not to a |
36 | * step. |
37 | * |
38 | * @author Lucas Ward |
39 | * @author Dave Syer |
40 | */ |
41 | public abstract class AbstractJob implements Job, BeanNameAware, InitializingBean { |
42 | |
43 | private List steps = new ArrayList(); |
44 | |
45 | private String name; |
46 | |
47 | private boolean restartable = false; |
48 | |
49 | private JobRepository jobRepository; |
50 | |
51 | private CompositeExecutionJobListener listener = new CompositeExecutionJobListener(); |
52 | |
53 | /** |
54 | * Default constructor. |
55 | */ |
56 | public AbstractJob() { |
57 | super(); |
58 | } |
59 | |
60 | /** |
61 | * Convenience constructor to immediately add name (which is mandatory but |
62 | * not final). |
63 | * |
64 | * @param name |
65 | */ |
66 | public AbstractJob(String name) { |
67 | super(); |
68 | this.name = name; |
69 | } |
70 | |
71 | /** |
72 | * Set the name property if it is not already set. Because of the order of |
73 | * the callbacks in a Spring container the name property will be set first |
74 | * if it is present. Care is needed with bean definition inheritance - if a |
75 | * parent bean has a name, then its children need an explicit name as well, |
76 | * otherwise they will not be unique. |
77 | * |
78 | * @see org.springframework.beans.factory.BeanNameAware#setBeanName(java.lang.String) |
79 | */ |
80 | public void setBeanName(String name) { |
81 | if (this.name == null) { |
82 | this.name = name; |
83 | } |
84 | } |
85 | |
86 | /** |
87 | * Set the name property. Always overrides the default value if this object |
88 | * is a Spring bean. |
89 | * |
90 | * @see #setBeanName(java.lang.String) |
91 | */ |
92 | public void setName(String name) { |
93 | this.name = name; |
94 | } |
95 | |
96 | /* (non-Javadoc) |
97 | * @see org.springframework.batch.core.domain.IJob#getName() |
98 | */ |
99 | public String getName() { |
100 | return name; |
101 | } |
102 | |
103 | /* (non-Javadoc) |
104 | * @see org.springframework.batch.core.domain.IJob#getSteps() |
105 | */ |
106 | public List getSteps() { |
107 | return steps; |
108 | } |
109 | |
110 | public void setSteps(List steps) { |
111 | this.steps.clear(); |
112 | this.steps.addAll(steps); |
113 | } |
114 | |
115 | public void addStep(Step step) { |
116 | this.steps.add(step); |
117 | } |
118 | |
119 | public void setRestartable(boolean restartable) { |
120 | this.restartable = restartable; |
121 | } |
122 | |
123 | /* (non-Javadoc) |
124 | * @see org.springframework.batch.core.domain.IJob#isRestartable() |
125 | */ |
126 | public boolean isRestartable() { |
127 | return restartable; |
128 | } |
129 | |
130 | public String toString() { |
131 | return ClassUtils.getShortName(getClass()) + ": [name=" + name + "]"; |
132 | } |
133 | |
134 | /** |
135 | * Public setter for injecting {@link JobExecutionListener}s. They will all |
136 | * be given the listener callbacks at the appropriate point in the job. |
137 | * |
138 | * @param listeners the listeners to set. |
139 | */ |
140 | public void setJobExecutionListeners(JobExecutionListener[] listeners) { |
141 | for (int i = 0; i < listeners.length; i++) { |
142 | this.listener.register(listeners[i]); |
143 | } |
144 | } |
145 | |
146 | /** |
147 | * Register a single listener for the {@link JobExecutionListener} |
148 | * callbacks. |
149 | * |
150 | * @param listener a {@link JobExecutionListener} |
151 | */ |
152 | public void registerJobExecutionListener(JobExecutionListener listener) { |
153 | this.listener.register(listener); |
154 | } |
155 | |
156 | /** |
157 | * Public setter for the {@link JobRepository} that is needed to manage the |
158 | * state of the batch meta domain (jobs, steps, executions) during the life |
159 | * of a job. |
160 | * |
161 | * @param jobRepository |
162 | */ |
163 | public void setJobRepository(JobRepository jobRepository) { |
164 | this.jobRepository = jobRepository; |
165 | } |
166 | |
167 | public void afterPropertiesSet() throws Exception { |
168 | Assert.notNull(getJobRepository(), "JobRepository must be set"); |
169 | } |
170 | |
171 | protected JobRepository getJobRepository() { |
172 | return jobRepository; |
173 | } |
174 | |
175 | protected CompositeExecutionJobListener getCompositeListener() { |
176 | return listener; |
177 | } |
178 | } |