1 /*
2 * Copyright 2009-2010 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.admin.service;
17
18 import org.springframework.context.ResourceLoaderAware;
19 import org.springframework.core.convert.converter.Converter;
20 import org.springframework.core.io.DefaultResourceLoader;
21 import org.springframework.core.io.Resource;
22 import org.springframework.core.io.ResourceLoader;
23
24 import java.beans.PropertyEditorSupport;
25
26 /**
27 * Converter for String to Resource that knows about local files managed by a
28 * {@link FileService}.
29 *
30 * @author Dave Syer
31 *
32 */
33 public class FileServiceResourceConverter extends PropertyEditorSupport implements Converter<String, Resource>, ResourceLoaderAware {
34
35 // Hack for PropertyEditor. TODO: remove static modifier.
36 static private FileService fileService;
37
38 private ResourceLoader resourceLoader = new DefaultResourceLoader();
39
40 /**
41 * Convenient constructor for declarative configuration purposes.
42 */
43 public FileServiceResourceConverter() {
44 }
45
46 /**
47 * @param fileService {@link org.springframework.batch.admin.service.FileService} to be used
48 */
49 public FileServiceResourceConverter(FileService fileService) {
50 FileServiceResourceConverter.fileService = fileService;
51 }
52
53 /**
54 * @param fileService the fileService to set
55 */
56 public void setFileService(FileService fileService) {
57 FileServiceResourceConverter.fileService = fileService;
58 }
59
60 /**
61 * Set the resource loader as a fallback for resources that are not managed
62 * by the {@link FileService}.
63 *
64 * @see ResourceLoaderAware#setResourceLoader(ResourceLoader)
65 */
66 public void setResourceLoader(ResourceLoader resourceLoader) {
67 this.resourceLoader = resourceLoader;
68 }
69
70 /**
71 * Convert the source path to a Resource. If it is a managed file from the
72 * {@link FileService} then it will be returned as a wrapper.
73 *
74 * @see Converter#convert(Object)
75 */
76 public Resource convert(String source) {
77
78 String path = source;
79
80 if (path.startsWith("files:")) {
81 Resource file = fileService.getResource(path);
82 if (file != null) {
83 return file;
84 }
85 }
86
87 return resourceLoader.getResource(source);
88
89 }
90
91 @Override
92 public void setAsText(String text) throws IllegalArgumentException {
93 // TODO remove PropertyEditor support (SPR-7079)
94 setValue(convert(text));
95 }
96
97 }