Author Topic: [Java] JARLoader  (Read 3741 times)

0 Members and 1 Guest are viewing this topic.

Offline Camel

  • Hero Member
  • *****
  • Posts: 1703
    • View Profile
    • BNU Bot
[Java] JARLoader
« on: December 08, 2007, 04:33:12 pm »
1) Find all of the files matching "./lib/*.jar" and create a URLClassLoader on them.
2) Iterate through every "*.class" file in each JAR; transform the path to a FQ Java class name, and pass off to checkClass(String)
3) Create a <Class<?>>, and iterate through its super-classes; for each super-class:
    A) If the class is a Look and Feel, register the LaF with the UIManager
    B) Iterate through the classes interfaces; for each interface:
        I) If the interface is a java.sql.Driver, create an instance of the class. If instance.jdbcCompliant(), register the Driver with the DriverManager
        II) If the interface is a plugin for my bot, (TODO: register it with the (TODO: create a plugin management system))


http://bnubot.googlecode.com/svn/trunk/BNUBot/src/net/bnubot/JARLoader.java

Code: [Select]
/**
 * This file is distributed under the GPL
 * $Id: JARLoader.java 987 2007-12-08 21:27:30Z scotta $
 */

package net.bnubot;

import ...;

/**
 * A class for loading classes from JARs in the lib folder
 * @author scotta
 */
public class JARLoader {
private static final URLClassLoader loader;
static {
String folder = "lib";

File f = new File(folder);
if(!f.exists())
f.mkdir();
if(!f.exists() || !f.isDirectory())
Out.fatalException(new FileNotFoundException(f.getName()));

FilenameFilter fnf = new FilenameFilter() {
public boolean accept(File dir, String name) {
return name.endsWith(".jar");
}};

String[] files = f.list(fnf);
URL[] urls = new URL[files.length];
for(int i = 0; i < files.length; i++)
try {
Out.debug(JARLoader.class, "Loading " + files[i]);
urls[i] = new URL("file:" + folder + "/" + files[i]);
} catch (MalformedURLException e) {
Out.exception(e);
}

loader = new URLClassLoader(urls);

// Look at each JAR
for(URL url : urls) {
try {
File file = new File(url.toExternalForm().substring(5));
JarFile jf = new JarFile(file);
// Look at each class inside the jar
Enumeration<JarEntry> en = jf.entries();
while(en.hasMoreElements()) {
try {
JarEntry je = en.nextElement();
String name = je.getName();
if(name.endsWith(".class")) {
// Convert the filename to an FQ class name
name = name.substring(0, name.length() - 6);
name = name.replace('/', '.');

checkClass(name);
}
} catch(NoClassDefFoundError e) {
} catch(ClassNotFoundException e) {
} catch(InstantiationException e) {
}

}
} catch (Exception e) {
Out.exception(e);
}
}
}

/**
* Check if a class is a JDBC driver, a Look and Feel, or a plugin
* @param name The fully qualified class name
*/
private static void checkClass(String name) throws ClassNotFoundException, InstantiationException, IllegalAccessException, SQLException {
// Get a Class for it
Class<?> clazz = JARLoader.forName(name);

for(Class<?> superClazz = clazz; superClazz != null; superClazz = superClazz.getSuperclass()) {
// Check if it's a look and feel
if(GlobalSettings.enableGUI && superClazz.equals(LookAndFeel.class)) {
LookAndFeel laf = (LookAndFeel)clazz.newInstance();
UIManager.installLookAndFeel(laf.getName(), name);
break;
}

// Check the interfaces
for(Class<?> cif : superClazz.getInterfaces()) {
// Check if it's a JDBC driver
if(cif.equals(Driver.class)) {
Driver d = (Driver)clazz.newInstance();
if(d.jdbcCompliant())
DriverManager.registerDriver(new DriverShim(d));
break;
}

// Check if it's a plugin
if(cif.equals(EventHandler.class)) {
// TODO: Enable use of this plugin
}
}
}
}

public static Class<?> forName(String name) throws ClassNotFoundException {
try {
return loader.loadClass(name);
} catch(ClassNotFoundException e) {
Out.error(JARLoader.class, "Failed to load " + name);
throw e;
} catch(UnsupportedClassVersionError e) {
String msg = "Unsupported class version " + name;
Out.debug(JARLoader.class, msg);
throw new ClassNotFoundException(msg, e);
}
}

public static ClassLoader getClassLoader() {
return loader;
}
}

<Camel> i said what what
<Blaze> in the butt
<Camel> you want to do it in my butt?
<Blaze> in my butt
<Camel> let's do it in the butt
<Blaze> Okay!