cocoon-cvs mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From cziege...@apache.org
Subject svn commit: r407826 - /cocoon/trunk/core/cocoon-core/src/main/java/org/apache/cocoon/components/classloader/AbstractClassLoaderFactory.java
Date Fri, 19 May 2006 15:37:41 GMT
Author: cziegeler
Date: Fri May 19 08:37:41 2006
New Revision: 407826

URL: http://svn.apache.org/viewvc?rev=407826&view=rev
Log:
Start adding code for an avalon free class loader component

Modified:
    cocoon/trunk/core/cocoon-core/src/main/java/org/apache/cocoon/components/classloader/AbstractClassLoaderFactory.java

Modified: cocoon/trunk/core/cocoon-core/src/main/java/org/apache/cocoon/components/classloader/AbstractClassLoaderFactory.java
URL: http://svn.apache.org/viewvc/cocoon/trunk/core/cocoon-core/src/main/java/org/apache/cocoon/components/classloader/AbstractClassLoaderFactory.java?rev=407826&r1=407825&r2=407826&view=diff
==============================================================================
--- cocoon/trunk/core/cocoon-core/src/main/java/org/apache/cocoon/components/classloader/AbstractClassLoaderFactory.java
(original)
+++ cocoon/trunk/core/cocoon-core/src/main/java/org/apache/cocoon/components/classloader/AbstractClassLoaderFactory.java
Fri May 19 08:37:41 2006
@@ -16,10 +16,16 @@
  */
 package org.apache.cocoon.components.classloader;
 
+import java.io.File;
+import java.io.FilenameFilter;
+import java.net.MalformedURLException;
 import java.net.URL;
 import java.util.ArrayList;
 import java.util.Iterator;
 import java.util.List;
+import java.util.Set;
+
+import javax.servlet.ServletContext;
 
 import org.apache.avalon.framework.activity.Disposable;
 import org.apache.avalon.framework.configuration.Configuration;
@@ -153,7 +159,113 @@
         return this.createClassLoader(urls, config.getIncludes(), config.getExcludes(), parent);
     }
 
+    protected URL getUrl(ServletContext servletContext, String rootPath, String path) 
+    throws MalformedURLException {
+        // check for files
+        final File file = this.getFile(path);
+        if ( file != null ) {
+            return file.toURL();
+        }
+        // check for absolut url
+        if ( path.indexOf(":/") > 0 ) {
+            return new URL(path);
+        }
+        return servletContext.getResource(this.getContextPath(rootPath, path));
+    }
+
+    protected File getFile(String path) 
+    throws MalformedURLException {
+        // check for file url
+        if ( path.startsWith("file:") ) {
+            return new File(path.substring(5));
+        }
+        // check for absolut files on unix/windows
+        if ( path.startsWith("/") || (path.length() > 2 && path.charAt(1) == ':'
)) {
+            return new File(path);
+        }
+        return null;
+    }
+
+    protected String getContextPath(String rootPath, String path) {
+        if ( path.startsWith("context://") ) {
+            return path.substring(9);
+        }
+        return rootPath + path;        
+    }
+
+    protected ClassLoader createClassLoader(ClassLoader              parent,
+                                            ClassLoaderConfiguration config,
+                                            ServletContext           servletContext,
+                                            String                   rootPath)
+    throws Exception {
+        if ( rootPath == null ) {
+            rootPath = "/";
+        } else if ( !rootPath.endsWith("/") ) {
+            rootPath = rootPath + "/";
+        }
+        final List urlList = new ArrayList();
+        Iterator i;
+        // process class directories
+        i = config.getClassDirectories().iterator();
+        while ( i.hasNext() ) {
+            // A class dir: simply add its URL
+            final String directory = (String)i.next();
+            final URL url = this.getUrl(servletContext, rootPath, directory);
+            // TODO Should we somehow check if this is a dir?
+            urlList.add(url);
+        }
+
+        // process lib directories
+        i = config.getLibDirectories().iterator();
+        while ( i.hasNext() ) {
+            // A lib dir: scan for all jar and zip it contains
+            final String directory = (String)i.next();
+            // Test for file
+            final File libDir = this.getFile(directory);
+            if ( libDir != null ) {
+                if ( !libDir.exists() ) {
+                    throw new Exception("Directory for lib class path does not exists: "
+ libDir);
+                }
+                if ( !libDir.isDirectory() ) {
+                    throw new Exception("Configuration for lib class path is not a directory:
" + libDir);
+                }
+                File[] libraries = libDir.listFiles(new JarFileFilter());
+
+                for (int m = 0; m < libraries.length; m++) {
+                    final URL lib = libraries[m].toURL();
+                    urlList.add(lib);
+                }                
+            } else {
+                // if this is an absolut url we can't handle it!
+                if ( directory.indexOf(":/") > 0 ) {
+                    throw new Exception("Can't handle absolute url as lib class path: " +
directory);
+                }
+                final String contextPath = this.getContextPath(rootPath, directory);
+                final Set resources = servletContext.getResourcePaths(contextPath + '/');
+                if ( resources != null ) {
+                    final Iterator iter = resources.iterator();
+                    while ( iter.hasNext() ) {
+                        final String path = (String)iter.next();
+                        if (path.endsWith(".jar") || path.endsWith(".zip")) {
+                            urlList.add(servletContext.getResource(path));
+                        }
+                    }
+                }
+            }
+        }
+
+        URL[] urls = (URL[])urlList.toArray(new URL[urlList.size()]);
+        
+        return this.createClassLoader(urls, config.getIncludes(), config.getExcludes(), parent);
+    }
+
     protected abstract ClassLoader createClassLoader(URL[] urls, List includePatterns, List
excludePatterns, ClassLoader parent);
+
+    private class JarFileFilter implements FilenameFilter {
+        public boolean accept(File dir, String name) {
+            return name.endsWith(".zip") || name.endsWith(".jar");
+        }
+    }
 
     /**
      * @see org.apache.avalon.framework.activity.Disposable#dispose()



Mime
View raw message