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