]> git.argeo.org Git - gpl/argeo-slc.git/blob - runtime/org.argeo.slc.support.simple/src/main/java/org/argeo/slc/spring/SpringUtils.java
Change the generated XML for the execution message
[gpl/argeo-slc.git] / runtime / org.argeo.slc.support.simple / src / main / java / org / argeo / slc / spring / SpringUtils.java
1 package org.argeo.slc.spring;
2
3 import java.io.IOException;
4 import java.net.URI;
5 import java.util.Map;
6
7 import org.apache.commons.logging.Log;
8 import org.apache.commons.logging.LogFactory;
9 import org.argeo.slc.SlcException;
10 import org.springframework.beans.factory.BeanFactoryUtils;
11 import org.springframework.beans.factory.ListableBeanFactory;
12 import org.springframework.core.io.DefaultResourceLoader;
13 import org.springframework.core.io.Resource;
14
15 public class SpringUtils {
16 private final static Log log = LogFactory.getLog(SpringUtils.class);
17
18 public static <T> T loadSingleFromContext(ListableBeanFactory context,
19 Class<T> clss) {
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()) {
27 log
28 .debug(("Found more that on bean for type " + clss
29 + ": " + beans.keySet()));
30 }
31 return null;
32 } else {
33 return null;
34 }
35 }
36
37 public static Resource getParent(Resource res) {
38 try {
39 if (res.getURL().getPath().equals("/"))
40 return null;
41
42 String urlStr = res.getURL().toString();
43 if (urlStr.charAt(urlStr.length() - 1) == '/')
44 urlStr = urlStr.substring(0, urlStr.length() - 2);
45
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);
52 }
53 }
54
55 public static String extractRelativePath(Resource ancestor, Resource child) {
56 try {
57
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);
63 }
64 }
65
66 private SpringUtils() {
67
68 }
69 }