1 package org
.argeo
.slc
.spring
;
3 import java
.io
.IOException
;
8 import org
.apache
.commons
.logging
.Log
;
9 import org
.apache
.commons
.logging
.LogFactory
;
10 import org
.argeo
.slc
.core
.SlcException
;
11 import org
.springframework
.beans
.factory
.ListableBeanFactory
;
12 import org
.springframework
.core
.io
.DefaultResourceLoader
;
13 import org
.springframework
.core
.io
.Resource
;
15 public class SpringUtils
{
16 private final static Log log
= LogFactory
.getLog(SpringUtils
.class);
18 public static <T
> T
loadSingleFromContext(ListableBeanFactory context
,
20 Map
<String
, T
> beans
= context
.getBeansOfType(clss
);
21 if (beans
.size() == 1) {
22 return beans
.values().iterator().next();
28 public static Resource
getParent(Resource res
) {
30 if (res
.getURL().getPath().equals("/"))
33 String urlStr
= res
.getURL().toString();
34 if (urlStr
.charAt(urlStr
.length() - 1) == '/')
35 urlStr
= urlStr
.substring(0, urlStr
.length() - 2);
37 String parentUrlStr
= urlStr
.substring(0, urlStr
.lastIndexOf('/')) + '/';
38 URI uri
= new URI(parentUrlStr
).normalize();
39 return new DefaultResourceLoader(Thread
.currentThread()
40 .getContextClassLoader()).getResource(uri
.toString());
41 } catch (Exception e
) {
42 throw new SlcException("Cannot get parent for resource " + res
, e
);
46 public static String
extractRelativePath(Resource ancestor
, Resource child
) {
49 return ancestor
.getURI().relativize(child
.getURI()).normalize()
51 } catch (IOException e
) {
52 throw new SlcException("Cannot extract relative path of " + child
53 + " based on " + ancestor
, e
);
57 private SpringUtils() {