001/*
002 * Copyright 2006-2018 the original author or authors.
003 *
004 * Licensed under the Apache License, Version 2.0 (the "License");
005 * you may not use this file except in compliance with the License.
006 * You may obtain a copy of the License at
007 *
008 *      https://www.apache.org/licenses/LICENSE-2.0
009 *
010 * Unless required by applicable law or agreed to in writing, software
011 * distributed under the License is distributed on an "AS IS" BASIS,
012 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
013 * See the License for the specific language governing permissions and
014 * limitations under the License.
015 */
016package org.springframework.batch.core.configuration.support;
017
018import org.springframework.batch.core.Job;
019import org.springframework.batch.core.JobExecution;
020import org.springframework.batch.core.JobParametersIncrementer;
021import org.springframework.batch.core.JobParametersValidator;
022import org.springframework.lang.Nullable;
023import org.springframework.util.ClassUtils;
024
025/**
026 * A {@link Job} that can optionally prepend a group name to another job's name,
027 * to make it fit a naming convention for type or origin. E.g. the source job
028 * might be <code>overnightJob</code> and the group
029 * <code>financeDepartment</code>, which would result in a {@link Job} with
030 * identical functionality but named <code>financeDepartment.overnightJob</code>
031 * . The use of a "." separator for elements is deliberate, since it is a "safe"
032 * character in a <a href="https://www.w3.org/Addressing/URL">URL</a>.
033 *
034 *
035 * @author Dave Syer
036 * @author Mahmoud Ben Hassine
037 *
038 */
039public class GroupAwareJob implements Job {
040
041        /**
042         * The separator between group and delegate job names in the final name
043         * given to this job.
044         */
045        private static final String SEPARATOR = ".";
046
047        private final Job delegate;
048
049        private final String groupName;
050
051        /**
052         * Create a new {@link Job} with the delegate and no group name.
053         *
054         * @param delegate a delegate for the features of a regular Job
055         */
056        public GroupAwareJob(Job delegate) {
057                this(null, delegate);
058        }
059
060        /**
061         * Create a new {@link Job} with the given group name and delegate.
062         *
063         * @param groupName the group name to prepend (can be {@code null})
064         * @param delegate a delegate for the features of a regular Job
065         */
066        public GroupAwareJob(@Nullable String groupName, Job delegate) {
067                super();
068                this.groupName = groupName;
069                this.delegate = delegate;
070        }
071
072        @Override
073        public void execute(JobExecution execution) {
074                delegate.execute(execution);
075        }
076
077        /**
078         * Concatenates the group name and the delegate job name (joining with a
079         * ".").
080         *
081         * @see org.springframework.batch.core.Job#getName()
082         */
083        @Override
084        public String getName() {
085                return groupName==null ? delegate.getName() : groupName + SEPARATOR + delegate.getName();
086        }
087
088        @Override
089        public boolean isRestartable() {
090                return delegate.isRestartable();
091        }
092
093        @Override
094        @Nullable
095        public JobParametersIncrementer getJobParametersIncrementer() {
096                return delegate.getJobParametersIncrementer();
097        }
098
099        @Override
100        public JobParametersValidator getJobParametersValidator() {
101                return delegate.getJobParametersValidator();
102        }
103
104        /*
105         * (non-Javadoc)
106         *
107         * @see java.lang.Object#equals(java.lang.Object)
108         */
109        @Override
110        public boolean equals(Object obj) {
111                if (obj instanceof GroupAwareJob) {
112                        return ((GroupAwareJob) obj).delegate.equals(delegate);
113                }
114                return false;
115        }
116
117        /*
118         * (non-Javadoc)
119         *
120         * @see java.lang.Object#hashCode()
121         */
122        @Override
123        public int hashCode() {
124                return delegate.hashCode();
125        }
126
127        @Override
128        public String toString() {
129                return ClassUtils.getShortName(delegate.getClass()) + ": [name=" + getName() + "]";
130        }
131
132}