KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > webdocwf > util > loader > generator > GeneratorClassLoader


1 /*
2      LoaderGenerator - tool for generated xml, sql and doml file needed for Octopus.
3     Copyright (C) 2003 Together
4     This library is free software; you can redistribute it and/or
5     modify it under the terms of the GNU Lesser General Public
6     License as published by the Free Software Foundation; either
7     version 2.1 of the License, or (at your option) any later version.
8     This library is distributed in the hope that it will be useful,
9     but WITHOUT ANY WARRANTY; without even the implied warranty of
10     MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
11     Lesser General Public License for more details.
12     You should have received a copy of the GNU Lesser General Public
13     License along with this library; if not, write to the Free Software
14     Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
15  */

16
17
18 package org.webdocwf.util.loader.generator;
19
20 import java.io.*;
21 import java.net.*;
22 import java.lang.reflect.*;
23
24 /**
25  *
26  * GeneratorClassLoader class load new files into class loader
27  * @author Radoslav Dutina
28  * @version 1.0
29  */

30 public class GeneratorClassLoader {
31
32   private static final Class JavaDoc[] parameters = new Class JavaDoc[] {
33       URL.class};
34
35   /**
36    * This methdod adding the file object to class loader
37    * @param s defines the string representation of file path
38    * @throws IOException
39    */

40   public static void addFile(String JavaDoc s) throws IOException {
41
42     File f = new File(s);
43
44     addFile(f);
45
46   } //end method
47

48   /**
49    * This method adding the file object to class loader
50    * @param f defines the url of file object
51    * @throws IOException
52    */

53   public static void addFile(File f) throws IOException {
54
55     addURL(f.toURL());
56
57   } //end method
58

59   /**
60    * This method adding the file object to class loader
61    * @param u defines the url object
62    * @throws IOException
63    */

64   public static void addURL(URL u) throws IOException {
65
66     URLClassLoader sysloader = (URLClassLoader) ClassLoader.
67         getSystemClassLoader();
68
69     Class JavaDoc sysclass = URLClassLoader.class;
70
71     try {
72
73       Method method = sysclass.getDeclaredMethod("addURL", parameters);
74
75       method.setAccessible(true);
76
77       method.invoke(sysloader, new Object JavaDoc[] {u});
78
79     }
80     catch (Throwable JavaDoc t) {
81
82       t.printStackTrace();
83
84       throw new IOException("Error, could not add URL to system classloader");
85
86     } //end try catch
87

88   } //end method
89

90 } //end class
91
Popular Tags