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=8ef13560cf2695a84139c91ad9d77dafe67b2d8b;hpb=751669212776bf78fb9c929a3031e27003591410;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 8ef13560c..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,11 +1,13 @@ 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; @@ -15,9 +17,18 @@ public class SpringUtils { 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; } @@ -27,24 +38,26 @@ public class SpringUtils { 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('/')); + String parentUrlStr = urlStr.substring(0, urlStr.lastIndexOf('/')) + '/'; + URI uri = new URI(parentUrlStr).normalize(); return new DefaultResourceLoader(Thread.currentThread() - .getContextClassLoader()).getResource(parentUrlStr + '/'); - } catch (IOException e) { + .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.getURI().relativize(child.getURI()).toString(); - } catch (IOException e) { + + 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); }