1 /*
2 * Copyright 2006-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 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 @Override
71 public void execute(JobExecution execution) {
72 delegate.execute(execution);
73 }
74
75 /**
76 * Concatenates the group name and the delegate job name (joining with a
77 * ".").
78 *
79 * @see org.springframework.batch.core.Job#getName()
80 */
81 @Override
82 public String getName() {
83 return groupName==null ? delegate.getName() : groupName + SEPARATOR + delegate.getName();
84 }
85
86 @Override
87 public boolean isRestartable() {
88 return delegate.isRestartable();
89 }
90
91 @Override
92 public JobParametersIncrementer getJobParametersIncrementer() {
93 return delegate.getJobParametersIncrementer();
94 }
95
96 @Override
97 public JobParametersValidator getJobParametersValidator() {
98 return delegate.getJobParametersValidator();
99 }
100
101 /*
102 * (non-Javadoc)
103 *
104 * @see java.lang.Object#equals(java.lang.Object)
105 */
106 @Override
107 public boolean equals(Object obj) {
108 if (obj instanceof GroupAwareJob) {
109 return ((GroupAwareJob) obj).delegate.equals(delegate);
110 }
111 return false;
112 }
113
114 /*
115 * (non-Javadoc)
116 *
117 * @see java.lang.Object#hashCode()
118 */
119 @Override
120 public int hashCode() {
121 return delegate.hashCode();
122 }
123
124 @Override
125 public String toString() {
126 return ClassUtils.getShortName(delegate.getClass()) + ": [name=" + getName() + "]";
127 }
128
129 }