]> git.argeo.org Git - gpl/argeo-slc.git/commitdiff
Simplify new runtime
authorMathieu Baudier <mbaudier@argeo.org>
Fri, 20 Jun 2008 13:25:18 +0000 (13:25 +0000)
committerMathieu Baudier <mbaudier@argeo.org>
Fri, 20 Jun 2008 13:25:18 +0000 (13:25 +0000)
git-svn-id: https://svn.argeo.org/slc/trunk@1256 4cfe0d0a-d680-48aa-b62c-e0a02a3f76cc

org.argeo.slc.core/src/main/java/org/argeo/slc/spring/SpringUtils.java

index fc9b656867246ee5610ab7205c7e12d61864f040..73f1ffde37647b9465267ef4cec4316e6dcd17c5 100644 (file)
@@ -1,10 +1,18 @@
 package org.argeo.slc.spring;\r
 \r
+import java.io.IOException;\r
 import java.util.Map;\r
 \r
+import org.apache.commons.logging.Log;\r
+import org.apache.commons.logging.LogFactory;\r
+import org.argeo.slc.core.SlcException;\r
 import org.springframework.beans.factory.ListableBeanFactory;\r
+import org.springframework.core.io.DefaultResourceLoader;\r
+import org.springframework.core.io.Resource;\r
 \r
 public class SpringUtils {\r
+       private final static Log log = LogFactory.getLog(SpringUtils.class);\r
+\r
        public static <T> T loadSingleFromContext(ListableBeanFactory context,\r
                        Class<T> clss) {\r
                Map<String, T> beans = context.getBeansOfType(clss);\r
@@ -15,6 +23,39 @@ public class SpringUtils {
                }\r
        }\r
 \r
+       public static Resource getParent(Resource res) {\r
+               try {\r
+                       if (res.getURL().getPath().equals("/"))\r
+                               return null;\r
+\r
+                       String urlStr = res.getURL().toString();\r
+                       if (urlStr.charAt(urlStr.length() - 1) == '/')\r
+                               urlStr = urlStr.substring(0, urlStr.length() - 2);\r
+\r
+                       String parentUrlStr = urlStr.substring(0, urlStr.lastIndexOf('/'));\r
+                       return new DefaultResourceLoader(Thread.currentThread()\r
+                                       .getContextClassLoader()).getResource(parentUrlStr + '/');\r
+               } catch (IOException e) {\r
+                       throw new SlcException("Cannot get parent for resource " + res, e);\r
+               }\r
+       }\r
+\r
+       public static String extractRelativePath(Resource ancestor, Resource child) {\r
+               try {\r
+                       String childPath = child.getURL().getPath();\r
+                       String ancestorPath = ancestor.getURL().getPath();\r
+\r
+                       if (log.isTraceEnabled())\r
+                               log.trace("extractRelativePath(): childPath=" + childPath\r
+                                               + ", ancestorPath=" + ancestorPath);\r
+\r
+                       return childPath.substring(ancestorPath.length());\r
+               } catch (IOException e) {\r
+                       throw new SlcException("Cannot extract relative path of " + child\r
+                                       + " based on " + ancestor, e);\r
+               }\r
+       }\r
+\r
        private SpringUtils() {\r
 \r
        }\r