X-Git-Url: http://git.argeo.org/?a=blobdiff_plain;f=org.argeo.slc.core%2Fsrc%2Fmain%2Fjava%2Forg%2Fargeo%2Fslc%2Fspring%2FSpringUtils.java;h=798b5648e28e1f35bfe6433866f17f0ba8384e3a;hb=c95d1a745fc431cdf7455a0e679516d537593a21;hp=fc9b656867246ee5610ab7205c7e12d61864f040;hpb=62e442adb36b006627b17061864dfa4edde0a99a;p=gpl%2Fargeo-slc.git diff --git a/org.argeo.slc.core/src/main/java/org/argeo/slc/spring/SpringUtils.java b/org.argeo.slc.core/src/main/java/org/argeo/slc/spring/SpringUtils.java index fc9b65686..798b5648e 100644 --- a/org.argeo.slc.core/src/main/java/org/argeo/slc/spring/SpringUtils.java +++ b/org.argeo.slc.core/src/main/java/org/argeo/slc/spring/SpringUtils.java @@ -1,20 +1,68 @@ package org.argeo.slc.spring; +import java.io.IOException; +import java.net.URI; import java.util.Map; +import org.apache.commons.logging.Log; +import org.apache.commons.logging.LogFactory; +import org.argeo.slc.core.SlcException; +import org.springframework.beans.factory.BeanFactoryUtils; import org.springframework.beans.factory.ListableBeanFactory; +import org.springframework.core.io.DefaultResourceLoader; +import org.springframework.core.io.Resource; public class SpringUtils { + private final static Log log = LogFactory.getLog(SpringUtils.class); + public static T loadSingleFromContext(ListableBeanFactory context, Class clss) { - Map beans = context.getBeansOfType(clss); + // Map beans = context.getBeansOfType(clss); + Map beans = BeanFactoryUtils.beansOfTypeIncludingAncestors( + context, clss, false, false); if (beans.size() == 1) { return beans.values().iterator().next(); + } else if (beans.size() > 1) { + if (log.isDebugEnabled()) { + log + .debug(("Found more that on bean for type " + clss + + ": " + beans.keySet())); + } + return null; } else { return null; } } + public static Resource getParent(Resource res) { + try { + if (res.getURL().getPath().equals("/")) + return null; + + String urlStr = res.getURL().toString(); + if (urlStr.charAt(urlStr.length() - 1) == '/') + urlStr = urlStr.substring(0, urlStr.length() - 2); + + String parentUrlStr = urlStr.substring(0, urlStr.lastIndexOf('/')) + '/'; + URI uri = new URI(parentUrlStr).normalize(); + return new DefaultResourceLoader(Thread.currentThread() + .getContextClassLoader()).getResource(uri.toString()); + } catch (Exception e) { + throw new SlcException("Cannot get parent for resource " + res, e); + } + } + + public static String extractRelativePath(Resource ancestor, Resource child) { + try { + + return ancestor.getURL().toURI().relativize(child.getURL().toURI()) + .normalize().toString(); + } catch (Exception e) { + throw new SlcException("Cannot extract relative path of " + child + + " based on " + ancestor, e); + } + } + private SpringUtils() { }