View Javadoc
1   package fr.ifremer.quadrige3.core.dao.technical.spring;
2   
3   /*-
4    * #%L
5    * Quadrige3 Core :: Quadrige3 Core Shared
6    * $Id:$
7    * $HeadURL:$
8    * %%
9    * Copyright (C) 2017 Ifremer
10   * %%
11   * This program is free software: you can redistribute it and/or modify
12   * it under the terms of the GNU Affero General Public License as published by
13   * the Free Software Foundation, either version 3 of the License, or
14   * (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 Affero General Public License
22   * along with this program.  If not, see <http://www.gnu.org/licenses/>.
23   * #L%
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   * <p>Springs class.</p>
44   */
45  public class Springs {
46  	/** Logger. */
47  	private static final Log log = LogFactory.getLog(Springs.class);
48  
49  	
50  	/**
51  	 * <p>Constructor for Springs.</p>
52  	 */
53  	protected Springs() {
54  		// Helper class : do not instanciate
55  	}
56  	
57  	/**
58  	 * <p>getResourcesFromPaths.</p>
59  	 *
60  	 * @param paths an array of {@link java.lang.String} objects.
61  	 * @param appContext a {@link org.springframework.context.ApplicationContext} object.
62  	 * @param checkIfResourceExists a boolean.
63  	 * @return a {@link java.util.List} object.
64  	 * @throws java.io.IOException if any.
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  		// For each path, retrieve corresponding resources
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          // Check if all resources exists
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       * <p>getResource.</p>
96       *
97       * @param location a {@link java.lang.String} object.
98       * @return a {@link org.springframework.core.io.Resource} object.
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                 // Try to parse the location as a URL...
108                 URL url = new URL(location);
109                 return new UrlResource(url);
110             }
111             catch (MalformedURLException ex) {
112                 // No URL -> resolve as resource path.
113                 return getResourceByPath(location);
114             }
115         }
116     }
117     
118     /**
119      * <p>getResourceByPath.</p>
120      *
121      * @param path a {@link java.lang.String} object.
122      * @return a {@link org.springframework.core.io.Resource} object.
123      */
124     protected static Resource getResourceByPath(String path) {
125         return new ClassPathContextResource(path, getClassLoader());
126     }
127     
128     /**
129      * <p>getClassLoader.</p>
130      *
131      * @return a {@link java.lang.ClassLoader} object.
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 }