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 | package org.springframework.batch.core.configuration.support; |
17 | |
18 | import org.springframework.batch.core.Job; |
19 | import org.springframework.batch.core.JobExecution; |
20 | import org.springframework.batch.core.JobParametersIncrementer; |
21 | import org.springframework.batch.core.JobParametersValidator; |
22 | import org.springframework.util.ClassUtils; |
23 | |
24 | /** |
25 | * A {@link Job} that can optionally prepend a group name to another job's name, |
26 | * to make it fit a naming convention for type or origin. E.g. the source job |
27 | * might be <code>overnightJob</code> and the group |
28 | * <code>financeDepartment</code>, which would result in a {@link Job} with |
29 | * identical functionality but named <code>financeDepartment.overnightJob</code> |
30 | * . The use of a "." separator for elements is deliberate, since it is a "safe" |
31 | * character in a <a href="http://www.w3.org/Addressing/URL">URL</a>. |
32 | * |
33 | * |
34 | * @author Dave Syer |
35 | * |
36 | */ |
37 | public class GroupAwareJob implements Job { |
38 | |
39 | /** |
40 | * The separator between group and delegate job names in the final name |
41 | * given to this job. |
42 | */ |
43 | private static final String SEPARATOR = "."; |
44 | |
45 | private final Job delegate; |
46 | |
47 | private final String groupName; |
48 | |
49 | /** |
50 | * Create a new {@link Job} with the delegate and no group name. |
51 | * |
52 | * @param delegate a delegate for the features of a regular Job |
53 | */ |
54 | public GroupAwareJob(Job delegate) { |
55 | this(null, delegate); |
56 | } |
57 | |
58 | /** |
59 | * Create a new {@link Job} with the given group name and delegate. |
60 | * |
61 | * @param groupName the group name to prepend |
62 | * @param delegate a delegate for the features of a regular Job |
63 | */ |
64 | public GroupAwareJob(String groupName, Job delegate) { |
65 | super(); |
66 | this.groupName = groupName; |
67 | this.delegate = delegate; |
68 | } |
69 | |
70 | public void execute(JobExecution execution) { |
71 | delegate.execute(execution); |
72 | } |
73 | |
74 | /** |
75 | * Concatenates the group name and the delegate job name (joining with a |
76 | * "."). |
77 | * |
78 | * @see org.springframework.batch.core.Job#getName() |
79 | */ |
80 | public String getName() { |
81 | return groupName==null ? delegate.getName() : groupName + SEPARATOR + delegate.getName(); |
82 | } |
83 | |
84 | public boolean isRestartable() { |
85 | return delegate.isRestartable(); |
86 | } |
87 | |
88 | public JobParametersIncrementer getJobParametersIncrementer() { |
89 | return delegate.getJobParametersIncrementer(); |
90 | } |
91 | |
92 | public JobParametersValidator getJobParametersValidator() { |
93 | return delegate.getJobParametersValidator(); |
94 | } |
95 | |
96 | /* |
97 | * (non-Javadoc) |
98 | * |
99 | * @see java.lang.Object#equals(java.lang.Object) |
100 | */ |
101 | @Override |
102 | public boolean equals(Object obj) { |
103 | if (obj instanceof GroupAwareJob) { |
104 | return ((GroupAwareJob) obj).delegate.equals(delegate); |
105 | } |
106 | return false; |
107 | } |
108 | |
109 | /* |
110 | * (non-Javadoc) |
111 | * |
112 | * @see java.lang.Object#hashCode() |
113 | */ |
114 | @Override |
115 | public int hashCode() { |
116 | return delegate.hashCode(); |
117 | } |
118 | |
119 | @Override |
120 | public String toString() { |
121 | return ClassUtils.getShortName(delegate.getClass()) + ": [name=" + getName() + "]"; |
122 | } |
123 | |
124 | } |