1 /* Copyright (C) 2005  Free Software Foundation
2 
3    This file is part of libgcj.
4 
5 This software is copyrighted work licensed under the terms of the
6 Libgcj License.  Please consult the file "LIBGCJ_LICENSE" for
7 details.  */
8 
9 package gnu.gcj.runtime;
10 
11 import java.io.File;
12 import java.io.FilenameFilter;
13 import java.io.IOException;
14 import java.net.URL;
15 import java.net.URLClassLoader;
16 import java.util.StringTokenizer;
17 
18 /**
19  * This is a URLClassLoader that has an extra helper method for
20  * handling things like java.ext.dirs.
21  */
22 class HelperClassLoader extends URLClassLoader
23 {
HelperClassLoader()24   HelperClassLoader()
25   {
26     super(new URL[0]);
27   }
28 
HelperClassLoader(ClassLoader parent)29   HelperClassLoader(ClassLoader parent)
30   {
31     super(new URL[0], parent);
32   }
33 
34   /**
35    * This is a helper method that adds all the jar and zip files from
36    * a given list of paths to this class loader.  The paths are taken
37    * from a system property whose name is provided as an argument.
38    */
addDirectoriesFromProperty(String propName)39   final void addDirectoriesFromProperty(String propName)
40   {
41     StringTokenizer st
42       = new StringTokenizer (System.getProperty (propName, ""),
43 			     File.pathSeparator);
44     try
45       {
46 	while (st.hasMoreElements ())
47 	  {
48 	    String dirname = st.nextToken ();
49 	    File dir = new File (dirname);
50             if (dir.exists ())
51 	      {
52 		if (! dirname.endsWith (File.separator))
53 		  dirname = dirname + File.separator;
54 		String files[] = dir.list (new FilenameFilter ()
55 		  {
56 		    public boolean accept (File dir, String name)
57 		    {
58 		      return name.endsWith (".jar") || name.endsWith (".zip");
59 		    }
60 		  });
61 		for (int i = files.length - 1; i >= 0; i--)
62 		  addURL(new URL("file", "", -1, dirname + files[i]));
63 	      }
64 	  }
65       }
66     catch (java.net.MalformedURLException x)
67       {
68 	// This should never happen.
69 	throw new RuntimeException(x);
70       }
71   }
72 }
73