Author: michiel
Date: 2010-01-21 23:25:59 +0100 (Thu, 21 Jan 2010)
New Revision: 40666

Modified:
   mmbase/trunk/core/src/main/java/org/mmbase/util/ResourceLoader.java
Log:
  MMB-1917. Moved all factory code to separate factory classes (which can now 
be moved to some config resource)

Modified: mmbase/trunk/core/src/main/java/org/mmbase/util/ResourceLoader.java
===================================================================
--- mmbase/trunk/core/src/main/java/org/mmbase/util/ResourceLoader.java 
2010-01-21 21:22:39 UTC (rev 40665)
+++ mmbase/trunk/core/src/main/java/org/mmbase/util/ResourceLoader.java 
2010-01-21 22:25:59 UTC (rev 40666)
@@ -357,78 +357,14 @@
             configRootNeedsInit = false;
             configRoot.roots.clear();
 
-            //adds a resource that can load from nodes
-            configRoot.roots.add(new NodeURLStreamHandler(configRoot, 
Type.CONFIG.ordinal()));
+            configRoot.roots.addAll(Arrays.asList(new 
NodeURLStreamHandlerFactory().createURLStreamHandler(configRoot, Type.CONFIG)));
+            configRoot.roots.addAll(Arrays.asList(new 
ApplicationContextFileURLStreamHandlerFactory().createURLStreamHandler(configRoot,
 Type.CONFIG)));
+            configRoot.roots.addAll(Arrays.asList(new 
MMBaseConfigSettingFileURLStreamHandlerFactory().createURLStreamHandler(configRoot,
 Type.CONFIG)));
+            configRoot.roots.addAll(Arrays.asList(new 
ConfigServletResourceURLStreamHandlerFactory().createURLStreamHandler(configRoot,
 Type.CONFIG)));
+            configRoot.roots.addAll(Arrays.asList(new 
ClassesFileURLStreamHandlerFactory().createURLStreamHandler(configRoot, 
Type.CONFIG)));
+            configRoot.roots.addAll(Arrays.asList(new 
ConfigClassLoaderURLStreamHandlerFactory().createURLStreamHandler(configRoot, 
Type.CONFIG)));
+            configRoot.roots.addAll(Arrays.asList(new 
FullyClassifiedClassLoaderURLStreamHandlerFactory().createURLStreamHandler(configRoot,
 Type.CONFIG)));
 
-            // mmbase.config settings
-            String configPath = null;
-            if (servletContext != null) {
-                configPath = servletContext.getInitParameter("mmbase.config");
-                log.debug("found the mmbase config path parameter using the 
mmbase.config servlet context parameter");
-            }
-            if (configPath == null) {
-                try {
-                    configPath = System.getProperty("mmbase.config");
-                } catch (SecurityException se) {
-                    log.info(se.getMessage());
-                }
-                if (configPath != null) {
-                    log.debug("found the mmbase.config path parameter using 
the mmbase.config system property");
-                }
-            } else {
-                try {
-                    if (System.getProperty("mmbase.config") != null){
-                        //if the configPath at this point was not null and the 
mmbase.config system property is defined
-                        //this deserves a warning message since the setting is 
masked
-                        log.warn("mmbase.config system property is masked by 
mmbase.config servlet context parameter");
-                    }
-                } catch (SecurityException se) {
-                    log.debug(se.getMessage());
-                }
-            }
-
-
-            try {
-                configRoot.roots.add(new 
ApplicationContextFileURLStreamHandler(configRoot));
-            } catch (NoClassDefFoundError t) {
-                // Never mind, we may be in RMMCI
-            }
-
-            if (configPath != null) {
-                if (servletContext != null) {
-                    // take into account that configpath can start at 
webrootdir
-                    if (configPath.startsWith("$WEBROOT")) {
-                        configPath = 
servletContext.getRealPath(configPath.substring(8));
-                    }
-                }
-                log.debug("Adding " + configPath);
-                configRoot.roots.add(new FileURLStreamHandler(configRoot, new 
File(configPath), true));
-            }
-
-            if (servletContext != null) {
-                String s = servletContext.getRealPath(RESOURCE_ROOT);
-                if (s != null) {
-                    PathURLStreamHandler h = new 
FileURLStreamHandler(configRoot, new File(s), true);
-                    if (! configRoot.roots.contains(h)) {
-                        configRoot.roots.add(h);
-                    }
-                } else {
-                    configRoot.roots.add(new 
ServletResourceURLStreamHandler(configRoot, RESOURCE_ROOT));
-                }
-            }
-
-            if (servletContext != null) {
-                String s = servletContext.getRealPath("/WEB-INF/classes" + 
CLASSLOADER_ROOT); // prefer opening as a files.
-                if (s != null) {
-                    configRoot.roots.add(new FileURLStreamHandler(configRoot, 
new File(s), false));
-                }
-            }
-
-            configRoot.roots.add(new ClassLoaderURLStreamHandler(configRoot, 
CLASSLOADER_ROOT));
-
-            //last fall back: fully qualified class-name
-            configRoot.roots.add(new ClassLoaderURLStreamHandler(configRoot, 
"/"));
-
         }
         return configRoot;
     }
@@ -502,6 +438,13 @@
     }
 
     /**
+     * @since MMBase-2.0
+     */
+    public static ServletContext getServletContext() {
+        return servletContext;
+    }
+
+    /**
      * The URL relative to which this class-loader resolves. Cannot be 
<code>null</code>.
      */
     private final URL context;
@@ -1142,12 +1085,21 @@
 
 
     /**
+     * @since MMBase-2.0
+     */
+    public static abstract class URLStreamHandlerFactory {
+        public abstract PathURLStreamHandler[] 
createURLStreamHandler(ResourceLoader parent, Type type);
+    }
+
+    /**
      * Extension URLStreamHandler, used for the 'sub' Handlers, entries of 
'roots' in ResourceLoader are of this type.
      */
-    static abstract class PathURLStreamHandler extends URLStreamHandler { 
//implements Comparable<PathURLStreamHandler> {
+    static abstract class PathURLStreamHandler extends URLStreamHandler 
implements Comparable<PathURLStreamHandler> {
 
         protected final ResourceLoader parent;
 
+        protected final int weight = 0;
+
         PathURLStreamHandler(ResourceLoader p) {
             this.parent = p;
         }
@@ -1190,12 +1142,80 @@
         }
 
         abstract Set<String> getPaths(Set<String> results, Pattern pattern,  
boolean recursive,  boolean directories);
+
+        public int compareTo(PathURLStreamHandler o) {
+            return o.weight - weight;
+        }
     }
 
 
     // 
================================================================================
     // Files
+    /**
+     * @since MMBase-2.0
+     */
+    protected static class MMBaseConfigSettingFileURLStreamHandlerFactory 
extends URLStreamHandlerFactory {
+        public PathURLStreamHandler[] createURLStreamHandler(ResourceLoader 
parent, Type type) {
+            String configPath = null;
+            ServletContext servletContext = ResourceLoader.getServletContext();
+            if (servletContext != null) {
+                configPath = servletContext.getInitParameter("mmbase.config");
+                log.debug("found the mmbase config path parameter using the 
mmbase.config servlet context parameter");
+            }
 
+            if (configPath == null) {
+                try {
+                    configPath = System.getProperty("mmbase.config");
+                } catch (SecurityException se) {
+                    log.info(se.getMessage());
+                }
+                if (configPath != null) {
+                    log.debug("found the mmbase.config path parameter using 
the mmbase.config system property");
+                }
+            } else {
+                try {
+                    if (System.getProperty("mmbase.config") != null){
+                        //if the configPath at this point was not null and the 
mmbase.config system property is defined
+                        //this deserves a warning message since the setting is 
masked
+                        log.warn("mmbase.config system property is masked by 
mmbase.config servlet context parameter");
+                    }
+                } catch (SecurityException se) {
+                    log.debug(se.getMessage());
+                }
+            }
+
+
+
+            if (configPath != null) {
+                if (parent.servletContext != null) {
+                    // take into account that configpath can start at 
webrootdir
+                    if (configPath.startsWith("$WEBROOT")) {
+                        configPath = 
parent.servletContext.getRealPath(configPath.substring(8));
+                    }
+                }
+                log.debug("Adding " + configPath);
+                return new PathURLStreamHandler[] {new 
FileURLStreamHandler(configRoot, new File(configPath), true)};
+            }
+            return new PathURLStreamHandler[0];
+        }
+    }
+
+    /**
+     * @since MMBase-2.0
+     */
+    protected static class ClassesFileURLStreamHandlerFactory extends 
URLStreamHandlerFactory {
+        public PathURLStreamHandler[] createURLStreamHandler(ResourceLoader 
parent, Type type) {
+            if (ResourceLoader.servletContext != null) {
+                String s = servletContext.getRealPath("/WEB-INF/classes" + 
CLASSLOADER_ROOT); // prefer opening as a files.
+                if (s != null) {
+                    return new PathURLStreamHandler[] { new 
FileURLStreamHandler(parent, new File(s), false)};
+                }
+            }
+            return new PathURLStreamHandler[0];
+        }
+    }
+
+
     protected static abstract class  AbstractFileURLStreamHandler extends 
PathURLStreamHandler {
         protected final boolean writeable;
         AbstractFileURLStreamHandler(ResourceLoader parent, boolean w) {
@@ -1433,7 +1453,17 @@
     // 
================================================================================
     // ApplicationContext
 
+
     /**
+     * @since MMBase-2.0
+     */
+    protected static class ApplicationContextFileURLStreamHandlerFactory 
extends URLStreamHandlerFactory {
+        public PathURLStreamHandler[] createURLStreamHandler(ResourceLoader 
parent, Type type) {
+            return new PathURLStreamHandler[] {new 
ApplicationContextFileURLStreamHandler(parent)};
+        }
+    }
+
+    /**
      * @since MMBase-1.9
      */
     protected static class ApplicationContextFileURLStreamHandler extends 
AbstractFileURLStreamHandler {
@@ -1535,6 +1565,14 @@
 
 
     /**
+     * @since MMBase-2.0
+     */
+    protected static class NodeURLStreamHandlerFactory extends 
URLStreamHandlerFactory {
+        public PathURLStreamHandler[] createURLStreamHandler(ResourceLoader 
root, Type type) {
+            return new PathURLStreamHandler[] {new 
NodeURLStreamHandler(configRoot, type.ordinal())};
+        }
+    }
+    /**
      * URLStreamHandler for NodeConnections.
      */
     protected static class NodeURLStreamHandler extends PathURLStreamHandler {
@@ -1761,6 +1799,21 @@
     // 
================================================================================
     // ServletContext
 
+    protected static class ConfigServletResourceURLStreamHandlerFactory 
extends URLStreamHandlerFactory  {
+        public PathURLStreamHandler[] createURLStreamHandler(ResourceLoader 
parent, Type type) {
+            if (ResourceLoader.servletContext != null) {
+                String s = 
ResourceLoader.servletContext.getRealPath(RESOURCE_ROOT);
+                if (s != null) {
+                    return new PathURLStreamHandler[] {new 
FileURLStreamHandler(parent, new File(s), true)};
+                } else {
+                    return new PathURLStreamHandler[] {new 
ServletResourceURLStreamHandler(parent, RESOURCE_ROOT)};
+                }
+            }
+            return new PathURLStreamHandler[0];
+        }
+    }
+
+
     /**
      * If running in a servlet 2.3 environment the 
ServletResourceURLStreamHandler is not fully
      * functional. A warning about that is logged, but only once.
@@ -1960,7 +2013,7 @@
     }
 
 
-    private static Comparator<URL> urlComparator;
+        private static Comparator<URL> urlComparator;
     private static Comparator<URL> getUrlComparator() {
         if (urlComparator == null) {
             urlComparator = new Comparator<URL>() {
@@ -2004,8 +2057,23 @@
         return urlComparator;
     }
 
+    /**
+     * @since MMBase-2.0
+     */
+    protected static class ConfigClassLoaderURLStreamHandlerFactory extends 
URLStreamHandlerFactory {
+        public PathURLStreamHandler[] createURLStreamHandler(ResourceLoader 
parent, Type type) {
+            return new PathURLStreamHandler[] {new 
ClassLoaderURLStreamHandler(parent, CLASSLOADER_ROOT)};
+        }
+    }
+    /**
+     * @since MMBase-2.0
+     */
+    protected static class FullyClassifiedClassLoaderURLStreamHandlerFactory 
extends URLStreamHandlerFactory {
+        public PathURLStreamHandler[] createURLStreamHandler(ResourceLoader 
parent, Type type) {
+            return new PathURLStreamHandler[] { new 
ClassLoaderURLStreamHandler(parent, "/")};
+        }
+    }
 
-
     protected static class ClassLoaderURLStreamHandler extends 
PathURLStreamHandler {
         private final String root;
 

_______________________________________________
Cvs mailing list
[email protected]
http://lists.mmbase.org/mailman/listinfo/cvs

Reply via email to