EMMA Coverage Report (generated Tue May 06 07:29:23 PDT 2008)
[all classes][org.springframework.batch.core.resource]

COVERAGE SUMMARY FOR SOURCE FILE [StepExecutionResourceProxy.java]

nameclass, %method, %block, %line, %
StepExecutionResourceProxy.java100% (1/1)47%  (9/19)60%  (161/268)64%  (33.7/53)

COVERAGE BREAKDOWN BY CLASS AND METHOD

nameclass, %method, %block, %line, %
     
class StepExecutionResourceProxy100% (1/1)47%  (9/19)60%  (161/268)64%  (33.7/53)
createRelative (String): Resource 0%   (0/1)0%   (0/13)0%   (0/2)
getDescription (): String 0%   (0/1)0%   (0/12)0%   (0/2)
getFilename (): String 0%   (0/1)0%   (0/12)0%   (0/2)
getInputStream (): InputStream 0%   (0/1)0%   (0/12)0%   (0/2)
getURI (): URI 0%   (0/1)0%   (0/12)0%   (0/2)
getURL (): URL 0%   (0/1)0%   (0/12)0%   (0/2)
isOpen (): boolean 0%   (0/1)0%   (0/12)0%   (0/2)
isReadable (): boolean 0%   (0/1)0%   (0/12)0%   (0/2)
isSingleton (): boolean 0%   (0/1)0%   (0/2)0%   (0/1)
setJobParametersFactory (JobParametersConverter): void 0%   (0/1)0%   (0/4)0%   (0/2)
exists (): boolean 100% (1/1)92%  (11/12)96%  (1.9/2)
getFile (): File 100% (1/1)92%  (11/12)96%  (1.9/2)
createFileName (String, String, Properties): String 100% (1/1)97%  (58/60)99%  (10.9/11)
StepExecutionResourceProxy (): void 100% (1/1)100% (16/16)100% (4/4)
beforeStep (StepExecution): void 100% (1/1)100% (27/27)100% (5/5)
replacePattern (String, String, String): String 100% (1/1)100% (16/16)100% (5/5)
setFilePattern (String): void 100% (1/1)100% (8/8)100% (2/2)
setResourceLoader (ResourceLoader): void 100% (1/1)100% (4/4)100% (2/2)
toString (): String 100% (1/1)100% (10/10)100% (1/1)

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 java.io.File;
20import java.io.IOException;
21import java.io.InputStream;
22import java.net.URI;
23import java.net.URL;
24import java.util.Iterator;
25import java.util.Properties;
26import java.util.Map.Entry;
27 
28import org.springframework.batch.core.JobParameters;
29import org.springframework.batch.core.StepExecution;
30import org.springframework.batch.core.StepExecutionListener;
31import org.springframework.batch.core.converter.DefaultJobParametersConverter;
32import org.springframework.batch.core.converter.JobParametersConverter;
33import org.springframework.batch.core.listener.StepExecutionListenerSupport;
34import org.springframework.context.ResourceLoaderAware;
35import org.springframework.core.io.FileSystemResourceLoader;
36import org.springframework.core.io.Resource;
37import org.springframework.core.io.ResourceLoader;
38import org.springframework.util.Assert;
39import org.springframework.util.StringUtils;
40 
41/**
42 * Strategy for locating different resources on the file system. For each unique
43 * step execution, the same file handle will be returned. A unique step is
44 * defined as having the same job instance and step name. An external file mover
45 * (such as an EAI solution) should rename and move any input files to conform
46 * to the pattern defined here.<br/>
47 * 
48 * If no pattern is passed in, then following default is used:
49 * 
50 * <pre>
51 * data/%JOB_NAME%/%STEP_NAME%.txt
52 * </pre>
53 * 
54 * The %% variables are replaced with the corresponding bean property at run
55 * time, when the factory method is executed. To insert {@link JobParameters}
56 * use a pattern with the parameter key surrounded by %%, e.g.
57 * 
58 * <pre>
59 * //home/jobs/data/%JOB_NAME%/%STEP_NAME%-%schedule.date%.txt
60 * </pre>
61 * 
62 * Note that the default pattern does not start with a separator. Because of the
63 * implementation of the Spring Core Resource abstractions, it would need to
64 * start with a double forward slash "//" to resolve to an absolute directory
65 * (or else use a full URL with the file: prefix).<br/>
66 * 
67 * To use this resource it must be initialised with a {@link StepExecution}.
68 * The best way to do that is to register it as a listener in the step that is
69 * going to need it. For this reason the resource implements
70 * {@link StepExecutionListener}.
71 * 
72 * @author Tomas Slanina
73 * @author Lucas Ward
74 * @author Dave Syer
75 * 
76 * @see Resource
77 */
78public class StepExecutionResourceProxy extends StepExecutionListenerSupport implements Resource, ResourceLoaderAware,
79                StepExecutionListener {
80 
81        private static final String JOB_NAME_PATTERN = "%JOB_NAME%";
82 
83        private static final String STEP_NAME_PATTERN = "%STEP_NAME%";
84 
85        private static final String DEFAULT_PATTERN = "data/%JOB_NAME%/" + "%STEP_NAME%.txt";
86 
87        private String filePattern = DEFAULT_PATTERN;
88 
89        private JobParametersConverter jobParametersConverter = new DefaultJobParametersConverter();
90 
91        private ResourceLoader resourceLoader = new FileSystemResourceLoader();
92 
93        private Resource delegate;
94 
95        /**
96         * @param relativePath
97         * @throws IOException
98         * @see org.springframework.core.io.Resource#createRelative(java.lang.String)
99         */
100        public Resource createRelative(String relativePath) throws IOException {
101                Assert.state(delegate != null, "The delegate resource has not been initialised. "
102                                + "Remember to register this object as a StepListener.");
103                return delegate.createRelative(relativePath);
104        }
105 
106        /**
107         * @see org.springframework.core.io.Resource#exists()
108         */
109        public boolean exists() {
110                Assert.state(delegate != null, "The delegate resource has not been initialised. "
111                                + "Remember to register this object as a StepListener.");
112                return delegate.exists();
113        }
114 
115        /**
116         * @see org.springframework.core.io.Resource#getDescription()
117         */
118        public String getDescription() {
119                Assert.state(delegate != null, "The delegate resource has not been initialised. "
120                                + "Remember to register this object as a StepListener.");
121                return delegate.getDescription();
122        }
123 
124        /**
125         * @throws IOException
126         * @see org.springframework.core.io.Resource#getFile()
127         */
128        public File getFile() throws IOException {
129                Assert.state(delegate != null, "The delegate resource has not been initialised. "
130                                + "Remember to register this object as a StepListener.");
131                return delegate.getFile();
132        }
133 
134        /**
135         * @see org.springframework.core.io.Resource#getFilename()
136         */
137        public String getFilename() {
138                Assert.state(delegate != null, "The delegate resource has not been initialised. "
139                                + "Remember to register this object as a StepListener.");
140                return delegate.getFilename();
141        }
142 
143        /**
144         * @throws IOException
145         * @see org.springframework.core.io.InputStreamSource#getInputStream()
146         */
147        public InputStream getInputStream() throws IOException {
148                Assert.state(delegate != null, "The delegate resource has not been initialised. "
149                                + "Remember to register this object as a StepListener.");
150                return delegate.getInputStream();
151        }
152 
153        /**
154         * @throws IOException
155         * @see org.springframework.core.io.Resource#getURI()
156         */
157        public URI getURI() throws IOException {
158                Assert.state(delegate != null, "The delegate resource has not been initialised. "
159                                + "Remember to register this object as a StepListener.");
160                return delegate.getURI();
161        }
162 
163        /**
164         * @throws IOException
165         * @see org.springframework.core.io.Resource#getURL()
166         */
167        public URL getURL() throws IOException {
168                Assert.state(delegate != null, "The delegate resource has not been initialised. "
169                                + "Remember to register this object as a StepListener.");
170                return delegate.getURL();
171        }
172 
173        /**
174         * @see org.springframework.core.io.Resource#isOpen()
175         */
176        public boolean isOpen() {
177                Assert.state(delegate != null, "The delegate resource has not been initialised. "
178                                + "Remember to register this object as a StepListener.");
179                return delegate.isOpen();
180        }
181 
182        /**
183         * @see org.springframework.core.io.Resource#isReadable()
184         */
185        public boolean isReadable() {
186                Assert.state(delegate != null, "The delegate resource has not been initialised. "
187                                + "Remember to register this object as a StepListener.");
188                return delegate.isReadable();
189        }
190 
191        /**
192         * Public setter for the {@link JobParametersConverter} used to translate
193         * {@link JobParameters} into {@link Properties}. Defaults to a
194         * {@link DefaultJobParametersConverter}.
195         * @param jobParametersConverter the {@link JobParametersConverter} to set
196         */
197        public void setJobParametersFactory(JobParametersConverter jobParametersConverter) {
198                this.jobParametersConverter = jobParametersConverter;
199        }
200 
201        /**
202         * Always false because we are expecting to be step scoped.
203         * 
204         * @see org.springframework.beans.factory.config.AbstractFactoryBean#isSingleton()
205         */
206        public boolean isSingleton() {
207                return false;
208        }
209 
210        /*
211         * (non-Javadoc)
212         * 
213         * @see org.springframework.context.ResourceLoaderAware#setResourceLoader(org.springframework.core.io.ResourceLoader)
214         */
215        public void setResourceLoader(ResourceLoader resourceLoader) {
216                this.resourceLoader = resourceLoader;
217        }
218 
219        /**
220         * helper method for <code>createFileName()</code>
221         */
222        private String replacePattern(String string, String pattern, String replacement) {
223 
224                if (string == null)
225                        return null;
226 
227                // check to ensure pattern exists in string.
228                if (string.indexOf(pattern) != -1) {
229                        return StringUtils.replace(string, pattern, replacement);
230                }
231 
232                return string;
233        }
234 
235        /**
236         * Creates a filename given a pattern and step context information.
237         * 
238         * Deliberate package access, so that the method can be accessed by unit
239         * tests
240         * @param jobName
241         * @param stepName
242         * @param properties
243         */
244        private String createFileName(String jobName, String stepName, Properties properties) {
245                Assert.notNull(filePattern, "filename pattern is null");
246 
247                String fileName = filePattern;
248 
249                fileName = replacePattern(fileName, JOB_NAME_PATTERN, jobName == null ? "job" : jobName);
250                fileName = replacePattern(fileName, STEP_NAME_PATTERN, stepName);
251 
252                if (properties != null) {
253                        for (Iterator iterator = properties.entrySet().iterator(); iterator.hasNext();) {
254                                Entry entry = (Entry) iterator.next();
255                                String key = (String) entry.getKey();
256                                fileName = replacePattern(fileName, "%" + key + "%", (String) entry.getValue());
257                        }
258                }
259 
260                return fileName;
261        }
262 
263        public void setFilePattern(String filePattern) {
264                this.filePattern = replacePattern(filePattern, "\\", File.separator);
265        }
266 
267        /**
268         * Collect the properties of the enclosing {@link StepExecution} that will
269         * be needed to create a file name.
270         * 
271         * @see org.springframework.batch.core.StepExecutionListener#beforeStep(org.springframework.batch.core.StepExecution)
272         */
273        public void beforeStep(StepExecution execution) {
274                String stepName = execution.getStepName();
275                String jobName = execution.getJobExecution().getJobInstance().getJobName();
276                Properties properties = jobParametersConverter.getProperties(execution.getJobExecution().getJobInstance()
277                                .getJobParameters());
278                delegate = resourceLoader.getResource(createFileName(jobName, stepName, properties));
279        }
280 
281        /**
282         * Delegates to the proxied Resource if set, otherwise returns the value of {@link #setFilePattern(String)}.
283         */
284        public String toString() {
285                return (delegate == null) ? filePattern : delegate.toString(); 
286        }
287 
288}

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