Update of /var/cvs/src/org/mmbase/util
In directory james.mmbase.org:/tmp/cvs-serv1726

Modified Files:
        ResourceLoader.java 
Log Message:
fix for MMB-1598, you can now place any resource outside the war, using the 
context xml. A good example would be caches.xml but also eg. multicast.xml may 
be an intersting example


See also: http://cvs.mmbase.org/viewcvs/src/org/mmbase/util
See also: http://www.mmbase.org/jira/browse/MMB-1598


Index: ResourceLoader.java
===================================================================
RCS file: /var/cvs/src/org/mmbase/util/ResourceLoader.java,v
retrieving revision 1.53
retrieving revision 1.54
diff -u -b -r1.53 -r1.54
--- ResourceLoader.java 23 Jun 2008 12:44:50 -0000      1.53
+++ ResourceLoader.java 11 Jul 2008 16:59:38 -0000      1.54
@@ -97,7 +97,7 @@
  * <p>For property-files, the java-unicode-escaping is undone on loading, and 
applied on saving, so there is no need to think of that.</p>
  * @author Michiel Meeuwissen
  * @since  MMBase-1.8
- * @version $Id: ResourceLoader.java,v 1.53 2008/06/23 12:44:50 michiel Exp $
+ * @version $Id: ResourceLoader.java,v 1.54 2008/07/11 16:59:38 michiel Exp $
  */
 public class ResourceLoader extends ClassLoader {
 
@@ -334,6 +334,8 @@
             }
 
 
+            configRoot.roots.add(configRoot.new 
ApplicationContextFileURLStreamHandler());
+
             if (configPath != null) {
                 if (servletContext != null) {
                     // take into account that configpath can start at 
webrootdir
@@ -477,6 +479,8 @@
         // hmm, don't like this code, but don't know how else to copy the 
inner object.
             if (o instanceof FileURLStreamHandler) {
                 roots.add(new FileURLStreamHandler((FileURLStreamHandler) o));
+            } else if (o instanceof ApplicationContextFileURLStreamHandler) {
+                roots.add(new ApplicationContextFileURLStreamHandler());
             } else if (o instanceof NodeURLStreamHandler) {
                 roots.add(new NodeURLStreamHandler((NodeURLStreamHandler) o));
             } else if (o instanceof ServletResourceURLStreamHandler) {
@@ -909,8 +913,11 @@
             }
         }
         for (PathURLStreamHandler o : roots) {
-            if (o instanceof FileURLStreamHandler) {
-                result.add(((FileURLStreamHandler) o).getFile(name));
+            if (o instanceof AbstractFileURLStreamHandler) {
+                File f = ((AbstractFileURLStreamHandler) o).getFile(name);
+                if (f != null) {
+                    result.add(((AbstractFileURLStreamHandler) 
o).getFile(name));
+                }
             }
         }
         return result;
@@ -1079,45 +1086,24 @@
     }
 
 
-    protected  class FileURLStreamHandler extends PathURLStreamHandler {
-        private final File fileRoot;
-        private final boolean writeable;
-        FileURLStreamHandler(File root, boolean w) {
-            fileRoot = root;
-            writeable = w;
+    // 
================================================================================
+    // Files
 
-        }
-        FileURLStreamHandler(FileURLStreamHandler f) {
-            fileRoot  = f.fileRoot;
-            writeable = f.writeable;
+    protected abstract class  AbstractFileURLStreamHandler extends 
PathURLStreamHandler {
+        protected final boolean writeable;
+        AbstractFileURLStreamHandler(boolean w) {
+            writeable = w;
         }
 
-        public File getFile(String name) {
-            if (name != null && name.startsWith("file:")) {
-                try {
-                    return new File(new URI(name)); // hff, how cumbersome, to 
translate an URL to a File
-                } catch (URISyntaxException use) {
-                    log.warn(use);
-                }
-            }
-            String fileName = fileRoot + ResourceLoader.this.context.getPath() 
+ (name == null ? "" : name);
-            if (! File.separator.equals("/")) { // windows compatibility
-                fileName = fileName.replace('/', File.separator.charAt(0)); // 
er
-            }
-            return new File(fileName);
-        }
-        public String getName(URL u) {
-            int l = (fileRoot + 
ResourceLoader.this.context.getPath()).length();
-            String path = u.getPath();
-            return l < path.length() ? path.substring(l) : path;
-        }
         public URLConnection openConnection(String name)  {
             URL u;
             try {
                 if (name.startsWith("file:")) {
                     u = new URL(null, name, this);
                 } else {
-                    u = new URL(null, "file:" + getFile(name), this);
+                    File file = getFile(name);
+                    if (file == null) return 
NOT_AVAILABLE_URLSTREAM_HANDLER.openConnection(name);
+                    u = new URL(null, "file:" + file, this);
                 }
             } catch (MalformedURLException mfue) {
                 throw new AssertionError(mfue.getMessage());
@@ -1136,7 +1122,7 @@
                 };
             File f = getFile(recursive);
 
-            if (f.isDirectory()) { // should always be true
+            if (f != null && f.isDirectory()) { // should always be true
                 File [] files = f.listFiles(filter);
                 if (files == null) return results;
                 for (File element : files) {
@@ -1153,6 +1139,41 @@
 
             return results;
         }
+        abstract public File getFile(String name);
+    }
+
+    protected  class FileURLStreamHandler extends AbstractFileURLStreamHandler 
{
+        private final File fileRoot;
+        FileURLStreamHandler(File root, boolean w) {
+            super(w);
+            fileRoot = root;
+
+        }
+        FileURLStreamHandler(FileURLStreamHandler f) {
+            super(f.writeable);
+            fileRoot  = f.fileRoot;
+        }
+
+        public File getFile(String name) {
+            if (name != null && name.startsWith("file:")) {
+                try {
+                    return new File(new URI(name)); // hff, how cumbersome, to 
translate an URL to a File
+                } catch (URISyntaxException use) {
+                    log.warn(use);
+                }
+            }
+            String fileName = fileRoot + ResourceLoader.this.context.getPath() 
+ (name == null ? "" : name);
+            if (! File.separator.equals("/")) { // windows compatibility
+                fileName = fileName.replace('/', File.separator.charAt(0)); // 
er
+            }
+            return new File(fileName);
+        }
+        public String getName(URL u) {
+            int l = (fileRoot + 
ResourceLoader.this.context.getPath()).length();
+            String path = u.getPath();
+            return l < path.length() ? path.substring(l) : path;
+        }
+
         public String toString() {
             return fileRoot.toString();
         }
@@ -1267,6 +1288,42 @@
 
     }
 
+    // 
================================================================================
+    // ApplicationContext
+
+    protected class ApplicationContextFileURLStreamHandler extends 
AbstractFileURLStreamHandler {
+        private Map<String, String> FILES;
+        ApplicationContextFileURLStreamHandler() {
+            super(true);
+            try {
+                FILES = 
ApplicationContextReader.getProperties("mmbase-resources");
+            } catch (javax.naming.NamingException ne) {
+                log.error(ne);
+                FILES = new HashMap<String, String>();
+            }
+        }
+
+        public File getFile(final String in) {
+            String name = FILES.get(in);
+
+            if (name != null && name.startsWith("file:")) {
+                try {
+                    return new File(new URI(name)); // hff, how cumbersome, to 
translate an URL to a File
+                } catch (URISyntaxException use) {
+                    log.warn("" + name + " : " + use.getMessage() , use);
+                }
+            }
+            return null;
+        }
+        protected String getName(URL u) {
+            return null;
+        }
+    }
+
+
+    // 
================================================================================
+    // Nodes
+
 
     /**
      * URLStreamHandler for NodeConnections.
@@ -1464,6 +1521,11 @@
 
     }
 
+
+
+    // 
================================================================================
+    // ServletContext
+
     /**
      * If running in a servlet 2.3 environment the 
ServletResourceURLStreamHandler is not fully
      * functional. A warning about that is logged, but only once.
@@ -1566,6 +1628,10 @@
     }
 
 
+
+    // 
================================================================================
+    // ClassLoader
+
     protected class ClassLoaderURLStreamHandler extends PathURLStreamHandler {
         private String root;
 
@@ -1723,6 +1789,11 @@
     }
 
 
+
+    // 
================================================================================
+    // 'NOT AVAILABLE'
+
+
     private static String NOT_FOUND = "/localhost/NOTFOUND/";
 
 
@@ -1776,6 +1847,10 @@
     };
 
 
+
+    // 
================================================================================
+    // mm:
+
     /**
      * The MMURLStreamHandler is a StreamHandler for the protocol 'mm' (which 
is only for internal
      * use). It combines the Connection types implented here above.
_______________________________________________
Cvs mailing list
[email protected]
http://lists.mmbase.org/mailman/listinfo/cvs

Reply via email to