The Artima Developer Community
Sponsored Link

Java Answers Forum
plug ins

2 replies on 1 page. Most recent reply: Nov 21, 2005 2:11 AM by Matthias Neumair

Welcome Guest
  Sign In

Go back to the topic listing  Back to Topic List Click to reply to this topic  Reply to this Topic Click to search messages in this forum  Search Forum Click for a threaded view of the topic  Threaded View   
Previous Topic   Next Topic
Flat View: This topic has 2 replies on 1 page
Matthias Neumair

Posts: 660
Nickname: neumi
Registered: Sep, 2003

plug ins Posted: Nov 16, 2005 10:50 PM
Reply to this message Reply
Advertisement
I would like to add import/export plugins to my project.

What would be the best way to do this?

I know I have to create an interface which all plug in classes must implement.

But my problem is to find the classes.

I wanted to have a folder in which I can put the jar files containing the plug ins.
But how can I get a listing of all the plugins?

Do you know a tutorial I can look into?


Slager .

Posts: 16
Nickname: slager
Registered: May, 2003

Re: plug ins Posted: Nov 21, 2005 2:02 AM
Reply to this message Reply
Hi,

This should get you going. It's a classloader loading a specific jar (you would extend it by listing the plugin directory for jar files)

Each jarfile is examined for .class files and classes are loaded to see which interfaces they implement. Note that you may want to check recursively for each interface found because an interface may be extending your plugin interface.

Of course you can also try using instanceof (since the interface class is definately loadable in your case)

Anyway loads of options ;)

Good luck.


import java.io.File;
import java.io.IOException;
import java.net.MalformedURLException;
import java.net.URL;
import java.net.URLClassLoader;
import java.util.Enumeration;
import java.util.LinkedList;
import java.util.List;
import java.util.jar.JarFile;
import java.util.zip.ZipEntry;

public class CheckJar {

public static class MyClassLoader extends URLClassLoader {
private File jarFile;

public MyClassLoader(String _sJar) throws MalformedURLException {
super(new URL[0]);
jarFile = new File(_sJar);
addURL(jarFile.toURL());
}

public List findClasses(String ifName) throws IOException, ClassNotFoundException {
List classList = new LinkedList();
JarFile jf = new JarFile(jarFile);
Enumeration e = jf.entries();
while(e.hasMoreElements()) {
ZipEntry z = (ZipEntry)e.nextElement();

if(z.getName().endsWith(".class")) {
String className = z.getName().replace('\\', '.').replace('/','.');
try {
// load the class (problably needs catching because dependencies may be unresolvable
Class c = loadClass(className.substring(0, className.length() - ".class".length()));
Class interfaces[] = c.getInterfaces();
for(int i = 0 ; i < interfaces.length; i++) {
// you may want to do this recursively (in case interfaces were extended)
if(interfaces[i].getName().equals(ifName)) {
// jay we got a plugin
System.out.println("Usable class : " + className);
classList.add(className);
}
}
} catch(Throwable t) {
System.out.println("problem with : " + className);
}
}
}
return classList;
}
}

public static void main(String[] args) throws Exception {
MyClassLoader cl = new MyClassLoader(args[0]);
Thread.currentThread().setContextClassLoader( cl );
List classList = cl.findClasses(args[1]);
}

}

Matthias Neumair

Posts: 660
Nickname: neumi
Registered: Sep, 2003

Re: plug ins Posted: Nov 21, 2005 2:11 AM
Reply to this message Reply
Thanks, I'll try that.

Flat View: This topic has 2 replies on 1 page
Topic: xmlToFile(document, filepath) problem Previous Topic   Next Topic Topic: Action Listeners

Sponsored Links



Google
  Web Artima.com   

Copyright © 1996-2019 Artima, Inc. All Rights Reserved. - Privacy Policy - Terms of Use