1 package fr.ifremer.quadrige3.core.dao.technical.spring;
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26 import com.google.common.collect.Lists;
27 import fr.ifremer.quadrige3.core.dao.technical.Assert;
28 import org.apache.commons.logging.Log;
29 import org.apache.commons.logging.LogFactory;
30 import org.springframework.context.ApplicationContext;
31 import org.springframework.core.io.*;
32 import org.springframework.util.ClassUtils;
33 import org.springframework.util.StringUtils;
34
35 import java.io.FileNotFoundException;
36 import java.io.IOException;
37 import java.net.MalformedURLException;
38 import java.net.URL;
39 import java.util.Arrays;
40 import java.util.List;
41
42
43
44
45 public class Springs {
46
47 private static final Log log = LogFactory.getLog(Springs.class);
48
49
50
51
52
53 protected Springs() {
54
55 }
56
57
58
59
60
61
62
63
64
65
66 public static List<Resource> getResourcesFromPaths(String[] paths, ApplicationContext appContext, boolean checkIfResourceExists) throws IOException {
67 Assert.notEmpty(paths);
68 Assert.notNull(appContext);
69
70
71 List<Resource> resources = Lists.newArrayList();
72 for (String path : paths) {
73 try {
74 Resource[] pathResources = appContext.getResources(path);
75 resources.addAll(Arrays.asList(pathResources));
76 } catch (IOException e) {
77 throw new IOException(String.format("Error while getting files from path: %s", path), e);
78 }
79 }
80
81
82 if (checkIfResourceExists) {
83 for(Resource resource : resources) {
84 if (!resource.exists()) {
85 throw new FileNotFoundException(String.format("File not found: %s", resource.getFilename()));
86 }
87 }
88 }
89
90 return resources;
91 }
92
93
94
95
96
97
98
99
100 public static Resource getResource(String location) {
101 Assert.notNull(location, "Location must not be null");
102 if (location.startsWith(ResourceLoader.CLASSPATH_URL_PREFIX)) {
103 return new ClassPathResource(location.substring(ResourceLoader.CLASSPATH_URL_PREFIX.length()), getClassLoader());
104 }
105 else {
106 try {
107
108 URL url = new URL(location);
109 return new UrlResource(url);
110 }
111 catch (MalformedURLException ex) {
112
113 return getResourceByPath(location);
114 }
115 }
116 }
117
118
119
120
121
122
123
124 protected static Resource getResourceByPath(String path) {
125 return new ClassPathContextResource(path, getClassLoader());
126 }
127
128
129
130
131
132
133 protected static ClassLoader getClassLoader() {
134 return ClassUtils.getDefaultClassLoader();
135 }
136
137 protected static class ClassPathContextResource extends ClassPathResource implements ContextResource {
138
139 public ClassPathContextResource(String path, ClassLoader classLoader) {
140 super(path, classLoader);
141 }
142
143 @Override
144 public String getPathWithinContext() {
145 return getPath();
146 }
147
148 @Override
149 public Resource createRelative(String relativePath) {
150 String pathToUse = StringUtils.applyRelativePath(getPath(), relativePath);
151 return new ClassPathContextResource(pathToUse, getClassLoader());
152 }
153 }
154
155 }