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=73f1ffde37647b9465267ef4cec4316e6dcd17c5;hb=80389feff3ab8e8743db4abfb6493285f72b24dd;hp=fc9b656867246ee5610ab7205c7e12d61864f040;hpb=badf9bb44d12177bea24679106d7122f51584ebc;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..73f1ffde3 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,10 +1,18 @@ package org.argeo.slc.spring; +import java.io.IOException; 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.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); @@ -15,6 +23,39 @@ public class SpringUtils { } } + 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('/')); + return new DefaultResourceLoader(Thread.currentThread() + .getContextClassLoader()).getResource(parentUrlStr + '/'); + } catch (IOException e) { + throw new SlcException("Cannot get parent for resource " + res, e); + } + } + + public static String extractRelativePath(Resource ancestor, Resource child) { + try { + String childPath = child.getURL().getPath(); + String ancestorPath = ancestor.getURL().getPath(); + + if (log.isTraceEnabled()) + log.trace("extractRelativePath(): childPath=" + childPath + + ", ancestorPath=" + ancestorPath); + + return childPath.substring(ancestorPath.length()); + } catch (IOException e) { + throw new SlcException("Cannot extract relative path of " + child + + " based on " + ancestor, e); + } + } + private SpringUtils() { }