tomcat-dev mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From r...@apache.org
Subject svn commit: r393434 - in /tomcat/container/tc5.5.x/catalina/src/share/org/apache/catalina/startup: Bootstrap.java ClassLoaderFactory.java
Date Wed, 12 Apr 2006 10:55:16 GMT
Author: remm
Date: Wed Apr 12 03:55:14 2006
New Revision: 393434

URL: http://svn.apache.org/viewcvs?rev=393434&view=rev
Log:
- Improve the algorithm used when constructing classloaders, in particular to respect order.
- Submitted by Rainer Jung.

Modified:
    tomcat/container/tc5.5.x/catalina/src/share/org/apache/catalina/startup/Bootstrap.java
    tomcat/container/tc5.5.x/catalina/src/share/org/apache/catalina/startup/ClassLoaderFactory.java

Modified: tomcat/container/tc5.5.x/catalina/src/share/org/apache/catalina/startup/Bootstrap.java
URL: http://svn.apache.org/viewcvs/tomcat/container/tc5.5.x/catalina/src/share/org/apache/catalina/startup/Bootstrap.java?rev=393434&r1=393433&r2=393434&view=diff
==============================================================================
--- tomcat/container/tc5.5.x/catalina/src/share/org/apache/catalina/startup/Bootstrap.java
(original)
+++ tomcat/container/tc5.5.x/catalina/src/share/org/apache/catalina/startup/Bootstrap.java
Wed Apr 12 03:55:14 2006
@@ -115,50 +115,69 @@
         if ((value == null) || (value.equals("")))
             return parent;
 
-        ArrayList unpackedList = new ArrayList();
-        ArrayList packedList = new ArrayList();
-        ArrayList urlList = new ArrayList();
-
+        ArrayList repositoryLocations = new ArrayList();
+        ArrayList repositoryTypes = new ArrayList();
+        int i;
+ 
         StringTokenizer tokenizer = new StringTokenizer(value, ",");
         while (tokenizer.hasMoreElements()) {
             String repository = tokenizer.nextToken();
 
             // Local repository
-            boolean packed = false;
-            if (repository.startsWith(CATALINA_HOME_TOKEN)) {
-                repository = getCatalinaHome()
-                    + repository.substring(CATALINA_HOME_TOKEN.length());
-            } else if (repository.startsWith(CATALINA_BASE_TOKEN)) {
-                repository = getCatalinaBase()
-                    + repository.substring(CATALINA_BASE_TOKEN.length());
+            boolean replace = false;
+            String before = repository;
+            while ((i=repository.indexOf(CATALINA_HOME_TOKEN))>=0) {
+                replace=true;
+                if (i>0) {
+                repository = repository.substring(0,i) + getCatalinaHome() 
+                    + repository.substring(i+CATALINA_HOME_TOKEN.length());
+                } else {
+                    repository = getCatalinaHome() 
+                        + repository.substring(CATALINA_HOME_TOKEN.length());
+                }
+            }
+            while ((i=repository.indexOf(CATALINA_BASE_TOKEN))>=0) {
+                replace=true;
+                if (i>0) {
+                repository = repository.substring(0,i) + getCatalinaBase() 
+                    + repository.substring(i+CATALINA_BASE_TOKEN.length());
+                } else {
+                    repository = getCatalinaBase() 
+                        + repository.substring(CATALINA_BASE_TOKEN.length());
+                }
             }
+            if (replace && log.isDebugEnabled())
+                log.debug("Expanded " + before + " to " + replace);
 
             // Check for a JAR URL repository
             try {
-                urlList.add(new URL(repository));
+                URL url=new URL(repository);
+                repositoryLocations.add(repository);
+                repositoryTypes.add(ClassLoaderFactory.IS_URL);
                 continue;
             } catch (MalformedURLException e) {
                 // Ignore
             }
 
             if (repository.endsWith("*.jar")) {
-                packed = true;
                 repository = repository.substring
                     (0, repository.length() - "*.jar".length());
-            }
-            if (packed) {
-                packedList.add(new File(repository));
+                repositoryLocations.add(repository);
+                repositoryTypes.add(ClassLoaderFactory.IS_GLOB);
+            } else if (repository.endsWith(".jar")) {
+                repositoryLocations.add(repository);
+                repositoryTypes.add(ClassLoaderFactory.IS_JAR);
             } else {
-                unpackedList.add(new File(repository));
+                repositoryLocations.add(repository);
+                repositoryTypes.add(ClassLoaderFactory.IS_DIR);
             }
         }
 
-        File[] unpacked = (File[]) unpackedList.toArray(new File[0]);
-        File[] packed = (File[]) packedList.toArray(new File[0]);
-        URL[] urls = (URL[]) urlList.toArray(new URL[0]);
-
+        String[] locations = (String[]) repositoryLocations.toArray(new String[0]);
+        Integer[] types = (Integer[]) repositoryTypes.toArray(new Integer[0]);
+ 
         ClassLoader classLoader = ClassLoaderFactory.createClassLoader
-            (unpacked, packed, urls, parent);
+            (locations, types, parent);
 
         // Retrieving MBean server
         MBeanServer mBeanServer = null;
@@ -414,7 +433,7 @@
             } else if (command.equals("stop")) {
                 daemon.stopServer(args);
             } else {
-                log.warn("Bootsrap: command \"" + command + "\" does not exist.");
+                log.warn("Bootstrap: command \"" + command + "\" does not exist.");
             }
         } catch (Throwable t) {
             t.printStackTrace();

Modified: tomcat/container/tc5.5.x/catalina/src/share/org/apache/catalina/startup/ClassLoaderFactory.java
URL: http://svn.apache.org/viewcvs/tomcat/container/tc5.5.x/catalina/src/share/org/apache/catalina/startup/ClassLoaderFactory.java?rev=393434&r1=393433&r2=393434&view=diff
==============================================================================
--- tomcat/container/tc5.5.x/catalina/src/share/org/apache/catalina/startup/ClassLoaderFactory.java
(original)
+++ tomcat/container/tc5.5.x/catalina/src/share/org/apache/catalina/startup/ClassLoaderFactory.java
Wed Apr 12 03:55:14 2006
@@ -51,6 +51,11 @@
 
     private static Log log = LogFactory.getLog(ClassLoaderFactory.class);
 
+    protected static final Integer IS_DIR = new Integer(0);
+    protected static final Integer IS_JAR = new Integer(1);
+    protected static final Integer IS_GLOB = new Integer(2);
+    protected static final Integer IS_URL = new Integer(3);
+
     // --------------------------------------------------------- Public Methods
 
 
@@ -143,17 +148,104 @@
             }
         }
 
-        // Add URLs
-        if (urls != null) {
-            for (int i = 0; i < urls.length; i++) {
-                if (log.isDebugEnabled())
-                    log.debug("  Including URL " + urls[i]);
-                list.add(urls[i]);
+        // Construct the class loader itself
+        URL[] array = (URL[]) list.toArray(new URL[list.size()]);
+        StandardClassLoader classLoader = null;
+        if (parent == null)
+            classLoader = new StandardClassLoader(array);
+        else
+            classLoader = new StandardClassLoader(array, parent);
+        return (classLoader);
+
+    }
+
+
+    /**
+     * Create and return a new class loader, based on the configuration
+     * defaults and the specified directory paths:
+     *
+     * @param locations Array of strings containing class directories, jar files,
+     *  jar directories or URLS that should be added to the repositories of
+     *  the class loader. The type is given by the member of param types.
+     * @param types Array of types for the members of param locations.
+     *  Possible values are IS_DIR (class directory), IS_JAR (single jar file),
+     *  IS_GLOB (directory of jar files) and IS_URL (URL).
+     * @param parent Parent class loader for the new class loader, or
+     *  <code>null</code> for the system class loader.
+     *
+     * @exception Exception if an error occurs constructing the class loader
+     */
+    public static ClassLoader createClassLoader(String locations[],
+                                                Integer types[],
+                                                ClassLoader parent)
+        throws Exception {
+
+        if (log.isDebugEnabled())
+            log.debug("Creating new class loader");
+
+        // Construct the "class path" for this class loader
+        ArrayList list = new ArrayList();
+
+        if (locations != null && types != null && locations.length == types.length)
{
+            for (int i = 0; i < locations.length; i++)  {
+                String location = locations[i];
+                if ( types[i] == IS_URL ) {
+                    URL url = new URL(location);
+                    if (log.isDebugEnabled())
+                        log.debug("  Including URL " + url);
+                    list.add(url);
+                } else if ( types[i] == IS_DIR ) {
+                    File directory = new File(location);
+                    directory = new File(directory.getCanonicalPath());
+                    if (!directory.exists() || !directory.isDirectory() ||
+                        !directory.canRead())
+                         continue;
+                    URL url = directory.toURL();
+                    if (log.isDebugEnabled())
+                        log.debug("  Including directory " + url);
+                    list.add(url);
+                } else if ( types[i] == IS_JAR ) {
+                    File file=new File(location);
+                    file = new File(file.getCanonicalPath());
+                    if (!file.exists() || !file.canRead())
+                        continue;
+                    URL url = file.toURL();
+                    if (log.isDebugEnabled())
+                        log.debug("  Including jar file " + url);
+                    list.add(url);
+                } else if ( types[i] == IS_GLOB ) {
+                    File directory=new File(location);
+                    if (!directory.exists() || !directory.isDirectory() ||
+                        !directory.canRead())
+                        continue;
+                    if (log.isDebugEnabled())
+                        log.debug("  Including directory glob "
+                            + directory.getAbsolutePath());
+                    String filenames[] = directory.list();
+                    for (int j = 0; j < filenames.length; j++) {
+                        String filename = filenames[j].toLowerCase();
+                        if (!filename.endsWith(".jar"))
+                            continue;
+                        File file = new File(directory, filenames[j]);
+                        file = new File(file.getCanonicalPath());
+                        if (!file.exists() || !file.canRead())
+                            continue;
+                        if (log.isDebugEnabled())
+                            log.debug("    Including glob jar file "
+                                + file.getAbsolutePath());
+                        URL url = file.toURL();
+                        list.add(url);
+                    }
+                }
             }
         }
 
         // Construct the class loader itself
         URL[] array = (URL[]) list.toArray(new URL[list.size()]);
+        if (log.isDebugEnabled())
+            for (int i = 0; i < array.length; i++) {
+                log.debug("  location " + i + " is " + array[i]);
+            }
         StandardClassLoader classLoader = null;
         if (parent == null)
             classLoader = new StandardClassLoader(array);



---------------------------------------------------------------------
To unsubscribe, e-mail: dev-unsubscribe@tomcat.apache.org
For additional commands, e-mail: dev-help@tomcat.apache.org


Mime
View raw message