From 80389feff3ab8e8743db4abfb6493285f72b24dd Mon Sep 17 00:00:00 2001 From: Mathieu Baudier Date: Fri, 20 Jun 2008 13:25:18 +0000 Subject: [PATCH] Simplify new runtime git-svn-id: https://svn.argeo.org/slc/trunk@1256 4cfe0d0a-d680-48aa-b62c-e0a02a3f76cc --- .../org/argeo/slc/spring/SpringUtils.java | 41 +++++++++++++++++++ 1 file changed, 41 insertions(+) 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() { } -- 2.39.2