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