1 package org
.argeo
.slc
.spring
;
6 import org
.apache
.commons
.logging
.Log
;
7 import org
.apache
.commons
.logging
.LogFactory
;
8 import org
.argeo
.slc
.SlcException
;
9 import org
.springframework
.beans
.factory
.BeanFactoryUtils
;
10 import org
.springframework
.beans
.factory
.ListableBeanFactory
;
11 import org
.springframework
.core
.io
.DefaultResourceLoader
;
12 import org
.springframework
.core
.io
.Resource
;
14 public class SpringUtils
{
15 private final static Log log
= LogFactory
.getLog(SpringUtils
.class);
17 @SuppressWarnings(value
= { "unchecked" })
18 public static <T
> T
loadSingleFromContext(ListableBeanFactory context
,
20 // Map<String, T> beans = context.getBeansOfType(clss);
21 Map
<String
, T
> beans
= BeanFactoryUtils
.beansOfTypeIncludingAncestors(
22 context
, clss
, false, false);
23 if (beans
.size() == 1) {
24 return beans
.values().iterator().next();
25 } else if (beans
.size() > 1) {
26 if (log
.isDebugEnabled()) {
28 .debug(("Found more that on bean for type " + clss
29 + ": " + beans
.keySet()));
37 public static Resource
getParent(Resource res
) {
39 if (res
.getURL().getPath().equals("/"))
42 String urlStr
= res
.getURL().toString();
43 if (urlStr
.charAt(urlStr
.length() - 1) == '/')
44 urlStr
= urlStr
.substring(0, urlStr
.length() - 2);
46 String parentUrlStr
= urlStr
.substring(0, urlStr
.lastIndexOf('/')) + '/';
47 URI uri
= new URI(parentUrlStr
).normalize();
48 return new DefaultResourceLoader(Thread
.currentThread()
49 .getContextClassLoader()).getResource(uri
.toString());
50 } catch (Exception e
) {
51 throw new SlcException("Cannot get parent for resource " + res
, e
);
55 public static String
extractRelativePath(Resource ancestor
, Resource child
) {
58 return ancestor
.getURL().toURI().relativize(child
.getURL().toURI())
59 .normalize().toString();
60 } catch (Exception e
) {
61 throw new SlcException("Cannot extract relative path of " + child
62 + " based on " + ancestor
, e
);
66 private SpringUtils() {