EMMA Coverage Report (generated Thu Jan 24 13:37:04 CST 2013)
[all classes][org.springframework.batch.core.resource]

COVERAGE SUMMARY FOR SOURCE FILE [StepExecutionSimpleCompletionPolicy.java]

nameclass, %method, %block, %line, %
StepExecutionSimpleCompletionPolicy.java100% (1/1)62%  (5/8)65%  (68/104)59%  (10.5/18)

COVERAGE BREAKDOWN BY CLASS AND METHOD

nameclass, %method, %block, %line, %
     
class StepExecutionSimpleCompletionPolicy100% (1/1)62%  (5/8)65%  (68/104)59%  (10.5/18)
isComplete (RepeatContext, RepeatStatus): boolean 0%   (0/1)0%   (0/14)0%   (0/2)
setKeyName (String): void 0%   (0/1)0%   (0/4)0%   (0/2)
update (RepeatContext): void 0%   (0/1)0%   (0/13)0%   (0/3)
toString (): String 100% (1/1)70%  (7/10)70%  (0.7/1)
isComplete (RepeatContext): boolean 100% (1/1)92%  (12/13)96%  (1.9/2)
start (RepeatContext): RepeatContext 100% (1/1)92%  (12/13)96%  (1.9/2)
StepExecutionSimpleCompletionPolicy (): void 100% (1/1)100% (6/6)100% (2/2)
beforeStep (StepExecution): void 100% (1/1)100% (31/31)100% (4/4)

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 
17package org.springframework.batch.core.resource;
18 
19import org.springframework.batch.core.JobParameters;
20import org.springframework.batch.core.StepExecution;
21import org.springframework.batch.core.StepExecutionListener;
22import org.springframework.batch.core.listener.StepExecutionListenerSupport;
23import org.springframework.batch.repeat.CompletionPolicy;
24import org.springframework.batch.repeat.RepeatContext;
25import org.springframework.batch.repeat.RepeatStatus;
26import org.springframework.batch.repeat.policy.SimpleCompletionPolicy;
27import org.springframework.util.Assert;
28 
29/**
30 * <p>
31 * A {@link CompletionPolicy} that picks up a commit interval from
32 * {@link JobParameters} by listening to the start of a step. Use anywhere that
33 * a {@link CompletionPolicy} can be used (usually at the chunk level in a
34 * step), and inject as a {@link StepExecutionListener} into the surrounding
35 * step. N.B. only after the step has started will the completion policy be
36 * usable.
37 * </p>
38 * 
39 * <p>
40 * It is easier and probably preferable to simply declare the chunk with a
41 * commit-interval that is a late-binding expression (e.g.
42 * <code>#{jobParameters['commit.interval']}</code>). That feature is available
43 * from of Spring Batch 2.1.7.
44 * </p>
45 * 
46 * @author Dave Syer
47 * 
48 * @see CompletionPolicy
49 */
50public class StepExecutionSimpleCompletionPolicy extends StepExecutionListenerSupport implements CompletionPolicy {
51 
52        private CompletionPolicy delegate;
53 
54        private String keyName = "commit.interval";
55 
56        /**
57         * Public setter for the key name of a Long value in the
58         * {@link JobParameters} that will contain a commit interval. Defaults to
59         * "commit.interval".
60         * @param keyName the keyName to set
61         */
62        public void setKeyName(String keyName) {
63                this.keyName = keyName;
64        }
65 
66        /**
67         * Set up a {@link SimpleCompletionPolicy} with a commit interval taken from
68         * the {@link JobParameters}. If there is a Long parameter with the given
69         * key name, the intValue of this parameter is used. If not an exception
70         * will be thrown.
71         * 
72         * @see org.springframework.batch.core.listener.StepExecutionListenerSupport#beforeStep(org.springframework.batch.core.StepExecution)
73         */
74        public void beforeStep(StepExecution stepExecution) {
75                JobParameters jobParameters = stepExecution.getJobParameters();
76                Assert.state(jobParameters.getParameters().containsKey(keyName),
77                                "JobParameters do not contain Long parameter with key=[" + keyName + "]");
78                delegate = new SimpleCompletionPolicy((int) jobParameters.getLong(keyName));
79        }
80 
81        /**
82         * @param context
83         * @param result
84         * @return true if the commit interval has been reached or the result
85         * indicates completion
86         * @see CompletionPolicy#isComplete(RepeatContext, RepeatStatus)
87         */
88        public boolean isComplete(RepeatContext context, RepeatStatus result) {
89                Assert.state(delegate != null, "The delegate resource has not been initialised. "
90                                + "Remember to register this object as a StepListener.");
91                return delegate.isComplete(context, result);
92        }
93 
94        /**
95         * @param context
96         * @return if the commit interval has been reached
97         * @see org.springframework.batch.repeat.CompletionPolicy#isComplete(org.springframework.batch.repeat.RepeatContext)
98         */
99        public boolean isComplete(RepeatContext context) {
100                Assert.state(delegate != null, "The delegate resource has not been initialised. "
101                                + "Remember to register this object as a StepListener.");
102                return delegate.isComplete(context);
103        }
104 
105        /**
106         * @param parent
107         * @return a new {@link RepeatContext}
108         * @see org.springframework.batch.repeat.CompletionPolicy#start(org.springframework.batch.repeat.RepeatContext)
109         */
110        public RepeatContext start(RepeatContext parent) {
111                Assert.state(delegate != null, "The delegate resource has not been initialised. "
112                                + "Remember to register this object as a StepListener.");
113                return delegate.start(parent);
114        }
115 
116        /**
117         * @param context
118         * @see org.springframework.batch.repeat.CompletionPolicy#update(org.springframework.batch.repeat.RepeatContext)
119         */
120        public void update(RepeatContext context) {
121                Assert.state(delegate != null, "The delegate resource has not been initialised. "
122                                + "Remember to register this object as a StepListener.");
123                delegate.update(context);
124        }
125 
126        /**
127         * Delegates to the wrapped {@link CompletionPolicy} if set, otherwise
128         * returns the value of {@link #setKeyName(String)}.
129         */
130        public String toString() {
131                return (delegate == null) ? keyName : delegate.toString();
132        }
133 
134}

[all classes][org.springframework.batch.core.resource]
EMMA 2.0.5312 (C) Vladimir Roubtsov