KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > enhydra > shark > toolagent > ToolAgentLoader


1 package org.enhydra.shark.toolagent;
2
3 import java.io.File JavaDoc;
4 import java.io.FilenameFilter JavaDoc;
5 import java.net.MalformedURLException JavaDoc;
6 import java.net.URL JavaDoc;
7 import java.net.URLClassLoader JavaDoc;
8
9 import org.enhydra.shark.api.internal.working.CallbackUtilities;
10
11 /**
12  * Use the static <code>load</code> method to load classes that implement tool
13  * agents from jars located in a plugin directory. The name of the directory
14  * defaults to "plugins". A different name may be given using the
15  * ToolAgentPluginDir property in Shark's configuration file (conf/Shark.conf).
16  * <br><br>
17  * <b>NOTE: The plugin dir must not be in the classpath if you want the tool
18  * agent class files to be reloadable. Otherwise they will be loaded by the
19  * default class loader which does not provide a way to reload classes Note also
20  * that whenever the default tool agent loads a secondary tool agent the
21  * implementing class of the latter will be loaded anew. This fact may cause an
22  * impact on the performance of your system. You may want to decide to put the
23  * jar files holding your tool agent classes into the lib subdir which is in the
24  * classpath.</b>
25  *
26  * @author Dirk Hoffmann, H+BEDV (www.antivir.de)
27  */

28 public class ToolAgentLoader {
29
30     /**
31      * Load tool agent classes from plugin directory.
32      *
33      * @param cus
34      * callback utilities used to query the ToolAgentPluginDir
35      * property
36      * @param name
37      * name of the class
38      * @return the tool agent class
39      * @throws ClassNotFoundException
40      * if tool agent class could not be found in the plugin
41      * directory
42      * @throws MalformedURLException
43      * if the value of the ToolAgentPluginDir property results in an
44      * invalid URL
45      */

46     public static Class JavaDoc load(CallbackUtilities cus, String JavaDoc name)
47             throws ClassNotFoundException JavaDoc, MalformedURLException JavaDoc {
48
49         // The place where tool agent plugins will be found
50
File JavaDoc toolAgentPluginDir = new File JavaDoc(cus.getProperty(
51                 "ToolAgentPluginDir", "plugins"));
52
53         // We will look for jar and property files.
54
FilenameFilter JavaDoc jarAndPropFiles = new FilenameFilter JavaDoc() {
55             public boolean accept(File JavaDoc dir, String JavaDoc name) {
56                 return name.endsWith(".jar") || name.endsWith(".properties");
57             }
58         };
59
60         File JavaDoc[] files = toolAgentPluginDir.listFiles(jarAndPropFiles);
61
62         // Convert filenames to urls and create an alternate class path to be
63
// used with the URLClassLoader
64
URL JavaDoc[] urls = new URL JavaDoc[files.length];
65         for (int i = 0; i < files.length; ++i) {
66             //urls[i] = files[i].toURL();
67
// ,-----------------^^^^^^^
68
// `-- depends on the user.dir property which may be corrupted.
69

70             // Therefore we construct the URL using string methods.
71
urls[i] = new URL JavaDoc("file:" + files[i].getPath());
72         }
73
74         // Create the URLClassLoader. Provide urls as search path. Provide the
75
// class loader that loaded this class as the parent class loader (the
76
// one that uses CLASSPATH). This is necessary to allow tool agents to
77
// use statically loaded classes. This requires classes that are to be
78
// loaded dynamically to be not in the CLASSPATH.
79
ClassLoader JavaDoc cl = new URLClassLoader JavaDoc(urls, ToolAgentLoader.class
80                 .getClassLoader());
81
82         // Finally load class
83
return cl.loadClass(name);
84     }
85 }
Popular Tags