You are viewing a plain text version of this content. The canonical link for it is here.
Posted to cvs@cocoon.apache.org by sy...@apache.org on 2005/04/14 12:02:07 UTC

svn commit: r161251 - in cocoon/trunk/src/java/org/apache/cocoon/components: classloader/DefaultClassLoaderFactory.java fam/SitemapMonitorImpl.java

Author: sylvain
Date: Thu Apr 14 03:02:05 2005
New Revision: 161251

URL: http://svn.apache.org/viewcvs?view=rev&rev=161251
Log:
add include/exclude filters on sitemap classpath declarations

Modified:
    cocoon/trunk/src/java/org/apache/cocoon/components/classloader/DefaultClassLoaderFactory.java
    cocoon/trunk/src/java/org/apache/cocoon/components/fam/SitemapMonitorImpl.java

Modified: cocoon/trunk/src/java/org/apache/cocoon/components/classloader/DefaultClassLoaderFactory.java
URL: http://svn.apache.org/viewcvs/cocoon/trunk/src/java/org/apache/cocoon/components/classloader/DefaultClassLoaderFactory.java?view=diff&r1=161250&r2=161251
==============================================================================
--- cocoon/trunk/src/java/org/apache/cocoon/components/classloader/DefaultClassLoaderFactory.java (original)
+++ cocoon/trunk/src/java/org/apache/cocoon/components/classloader/DefaultClassLoaderFactory.java Thu Apr 14 03:02:05 2005
@@ -31,6 +31,7 @@
 import org.apache.avalon.framework.service.ServiceManager;
 import org.apache.avalon.framework.service.Serviceable;
 import org.apache.avalon.framework.thread.ThreadSafe;
+import org.apache.cocoon.matching.helpers.WildcardHelper;
 import org.apache.excalibur.source.Source;
 import org.apache.excalibur.source.SourceResolver;
 import org.apache.excalibur.source.TraversableSource;
@@ -39,13 +40,19 @@
  * Default implementation of {@link ClassLoaderFactory}. It accepts both class directory and jar
  * directory configurations.
  * <p>
+ * Wildcard patterns can also be specified to include or exclude some classes to be loaded in the
+ * classloader. In such case, the class is directly loaded from the parent classloader. The default
+ * is to include all classes.
+ * <p>
  * Example:
  * <pre>
  * &lt;classpath&gt;
  *   &lt;class-dir src="BLOCK-INF/classes"/&gt;
  *   &lt;lib-dir src="BLOCK-INF/lib"/&gt;
+ *   &lt;include-classes pattern="org.apache.cocoon.**"/&gt;
+ *   &lt;exclude-classes pattern="org.apache.cocoon.transformation.**"/&gt;
  * &/lt;classpath&gt;
- *
+ * </pre>
  */
 public class DefaultClassLoaderFactory extends AbstractLogEnabled implements ClassLoaderFactory,
     Serviceable, ThreadSafe, Disposable {
@@ -69,22 +76,30 @@
         }
     }
     
-    private URL[] parseConfiguration(Configuration config) throws ConfigurationException {
+    /* (non-Javadoc)
+     * @see org.apache.cocoon.core.ClassLoaderFactory#createClassLoader(java.lang.ClassLoader)
+     */
+    public ClassLoader createClassLoader(ClassLoader parent, Configuration config) throws ConfigurationException {
         List urlList = new ArrayList();
+        List includeList = new ArrayList();
+        List excludeList = new ArrayList();
         Configuration[] children = config.getChildren();
         for (int i = 0; i < children.length; i++) {
             Configuration child = children[i];
             String name = child.getName();
             Source src = null;
             try {
-                src = resolver.resolveURI(child.getAttribute("src"));
                 // A class dir: simply add its URL
                 if ("class-dir".equals(name)) {
+                    src = resolver.resolveURI(child.getAttribute("src"));
                     ensureIsDirectory(src, child.getLocation());
                     urlList.add(new URL(src.getURI()));
+                    resolver.release(src);
+                    src = null;
                 
                 // A lib dir: scan for all jar and zip it contains
                 } else if ("lib-dir".equals(name)) {
+                    src = resolver.resolveURI(child.getAttribute("src"));
                     ensureIsDirectory(src, child.getLocation());
                     Iterator iter = ((TraversableSource)src).getChildren().iterator();
                     while (iter.hasNext()) {
@@ -95,6 +110,12 @@
                             urlList.add(new URL(childURI));
                         }
                     }
+                    resolver.release(src);
+                    src = null;
+                } else if  ("include-classes".equals(name)) {
+                    includeList.add(WildcardHelper.compilePattern(child.getAttribute("pattern")));
+                } else if ("exclude-classes".equals(name)) {
+                    excludeList.add(WildcardHelper.compilePattern(child.getAttribute("pattern")));
                 } else {
                     throw new ConfigurationException("Unexpected element " + name + " at " + child.getLocation());
                 }
@@ -107,33 +128,65 @@
             }
         }
         
-        return (URL[])urlList.toArray(new URL[urlList.size()]);
-    }
-
-    /* (non-Javadoc)
-     * @see org.apache.cocoon.core.ClassLoaderFactory#createClassLoader(java.lang.ClassLoader)
-     */
-    public ClassLoader createClassLoader(ClassLoader parent, Configuration config) throws ConfigurationException {
-        URL[] urls = parseConfiguration(config);
-        return new DefaultClassLoader(urls, parent);
+        URL[] urls = (URL[])urlList.toArray(new URL[urlList.size()]);
+        int[][] includes = includeList.isEmpty() ? null : (int[][])includeList.toArray(new int[includeList.size()][]);
+        int[][] excludes = excludeList.isEmpty() ? null : (int[][])excludeList.toArray(new int[excludeList.size()][]);
+        
+        return new DefaultClassLoader(urls, includes, excludes, parent);
     }
 
     public static class DefaultClassLoader extends URLClassLoader {
+        
+        private final int[][] includes;
+        private final int[][] excludes;
 
         /**
          * Alternate constructor to define a parent and initial <code>URL</code>
          * s.
          */
-        public DefaultClassLoader(final URL[] urls, final ClassLoader parent) {
-            this(urls, parent, null);
+        public DefaultClassLoader(URL[] urls, int[][] includes, int[][] excludes, final ClassLoader parent) {
+            this(urls, includes, excludes, parent, null);
         }
 
         /**
          * Alternate constructor to define a parent, initial <code>URL</code>s,
          * and a default <code>URLStreamHandlerFactory</code>.
          */
-        public DefaultClassLoader(final URL[] urls, final ClassLoader parent, final URLStreamHandlerFactory factory) {
+        public DefaultClassLoader(final URL[] urls, int[][] includes, int[][] excludes, ClassLoader parent, URLStreamHandlerFactory factory) {
             super(urls, parent, factory);
+            this.includes = includes;
+            this.excludes = excludes;
+        }
+        
+        private boolean tryClassHere(String name) {
+            // Scan includes, then excludes
+            boolean tryHere;
+            
+            // If no explicit includes, try here
+            if (this.includes == null) {
+                tryHere = true;
+            } else {
+                // See if it matches include patterns
+                tryHere = false;
+                for (int i = 0; i < this.includes.length; i++) {
+                    if (WildcardHelper.match(null, name, includes[i])) {
+                        tryHere = true;
+                        break;
+                    }
+                }
+            }
+            
+            // Go through the exclusion list
+            if (tryHere && excludes != null) {
+                for (int i = 0; i < this.excludes.length; i++) {
+                    if (WildcardHelper.match(null, name, excludes[i])) {
+                        tryHere = false;
+                        break;
+                    }
+                }
+            }
+            
+            return tryHere;
         }
 
         /**
@@ -153,18 +206,27 @@
             Class clazz = findLoadedClass(name);
 
             if (clazz == null) {
+                
+                ClassLoader parent = getParent();
 
-                try {
-                    clazz = findClass(name);
-                    //System.err.println("Paranoid load : " + name);
-                } catch (ClassNotFoundException cnfe) {
-                    ClassLoader parent = getParent();
-                    if (parent != null) {
-                        // Ask to parent ClassLoader (can also throw a CNFE).
-                        clazz = parent.loadClass(name);
+                if (tryClassHere(name)) {
+                    try {
+                        clazz = findClass(name);
+                        //System.err.println("Paranoid load : " + name);
+                    } catch (ClassNotFoundException cnfe) {
+                        if (parent == null) {
+                            // Propagate exception
+                            throw cnfe;                        
+                        }
+                    }
+                }
+                
+                if (clazz == null) {
+                    if (parent == null) {
+                        throw new ClassNotFoundException(name);
                     } else {
-                        // Propagate exception
-                        throw cnfe;
+                        // Will throw a CFNE if not found in parent
+                        clazz = parent.loadClass(name);
                     }
                 }
             }

Modified: cocoon/trunk/src/java/org/apache/cocoon/components/fam/SitemapMonitorImpl.java
URL: http://svn.apache.org/viewcvs/cocoon/trunk/src/java/org/apache/cocoon/components/fam/SitemapMonitorImpl.java?view=diff&r1=161250&r2=161251
==============================================================================
--- cocoon/trunk/src/java/org/apache/cocoon/components/fam/SitemapMonitorImpl.java (original)
+++ cocoon/trunk/src/java/org/apache/cocoon/components/fam/SitemapMonitorImpl.java Thu Apr 14 03:02:05 2005
@@ -53,22 +53,24 @@
             String name = child.getName();
             Source src = null;
             try {
-                src = resolver.resolveURI(child.getAttribute("src"));
-
                 if ("class-dir".equals(name)) {
+                    src = resolver.resolveURI(child.getAttribute("src"));
                     String dir = src.getURI();
+                    resolver.release(src);
                     if (getLogger().isDebugEnabled()) {
                         getLogger().debug("class-dir:" + dir);
                     }
                     urlList.add(new File(dir.substring(5)));
                 } else if ("lib-dir".equals(name)) {
+                    src = resolver.resolveURI(child.getAttribute("src"));
                     String dir = src.getURI();
+                    resolver.release(src);
                     if (getLogger().isDebugEnabled()) {
                         getLogger().debug("lib-dir:" + dir);
                     }
                     urlList.add(new File(dir.substring(5)));
                 } else {
-                    throw new ConfigurationException("Unexpected element " + name + " at " + child.getLocation());
+                    // ignore for now include and exclude patterns
                 }
             } catch(ConfigurationException ce) {
                 resolver.release(src);