View Javadoc
1   package net.sumaris.core.util;
2   
3   /*-
4    * #%L
5    * SUMARiS :: Sumaris Core Shared
6    * $Id:$
7    * $HeadURL:$
8    * %%
9    * Copyright (C) 2018 SUMARiS Consortium
10   * %%
11   * This program is free software: you can redistribute it and/or modify
12   * it under the terms of the GNU General Public License as
13   * published by the Free Software Foundation, either version 3 of the
14   * License, or (at your option) any later version.
15   * 
16   * This program is distributed in the hope that it will be useful,
17   * but WITHOUT ANY WARRANTY; without even the implied warranty of
18   * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
19   * GNU General Public License for more details.
20   * 
21   * You should have received a copy of the GNU General Public
22   * License along with this program.  If not, see
23   * <http://www.gnu.org/licenses/gpl-3.0.html>.
24   * #L%
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   * <p>Springs class.</p>
49   */
50  public class Springs {
51  	/** Logger. */
52  	private static final Logger log = LoggerFactory.getLogger(Springs.class);
53  
54  	
55  	/**
56  	 * <p>Constructor for Springs.</p>
57  	 */
58  	protected Springs() {
59  		// Helper class : do not instanciate
60  	}
61  	
62  	/**
63  	 * <p>getResourcesFromPaths.</p>
64  	 *
65  	 * @param paths an array of {@link String} objects.
66  	 * @param appContext a {@link org.springframework.context.ApplicationContext} object.
67  	 * @param checkIfResourceExists a boolean.
68  	 * @return a {@link List} object.
69  	 * @throws IOException if any.
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  		// For each path, retrieve corresponding resources
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          // Check if all resources exists
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      * <p>getResource.</p>
101      *
102      * @param location a {@link String} object.
103      * @return a {@link org.springframework.core.io.Resource} object.
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                 // Try to parse the location as a URL...
113                 URL url = new URL(location);
114                 return new UrlResource(url);
115             }
116             catch (MalformedURLException ex) {
117                 // No URL -> resolve as resource path.
118                 return getResourceByPath(location);
119             }
120         }
121     }
122 
123     /**
124      * <p>getResourceByPath.</p>
125      *
126      * @param path a {@link String} object.
127      * @return a {@link org.springframework.core.io.Resource} object.
128      */
129     protected static Resource getResourceByPath(String path) {
130         return new ClassPathContextResource(path, getClassLoader());
131     }
132 
133     /**
134      * <p>getClassLoader.</p>
135      *
136      * @return a {@link ClassLoader} object.
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 }