I was wondering if someone could point to (a) resourse(s) dealing with structuring your program to allow for plugins. (i'm thinking of a text editor right now).
I tried a search, but there doesn't seem to be a great ubundance,(or i suck at searching).
I don't know about Java in particular but usually for plug-ins in windows you have a directory where dlls are put in. The app loads every library in this directory and looks for a few key functions in there, which it then uses to initialize the plug-in and communicate with it. It will pass a few function pointers or similar paradigms to the DLL to allow talk-back.
COM is used much now.
Of course for Java this won't work. I have to admit that I have no idea what to do there.
All the buzzt CornedBee
"Writing specifications is like writing a novel. Writing code is like writing poetry."
- Anonymous, published by Raymond Chen
Don't PM me with your problems, I scan most of the forums daily. If you do PM me, I will not answer your question.
You could write an interface that all extension classes have to implement. Then read in the names of all extension classes from some registration file. Use system class loader to load the classes you find in this file. Like this:
Code:
package your.package;
import java.lang.reflect.*;
import java.util.*;
public interface Extension
{
// blabla...
}
// in the main class
Vector plugins;
void loadPlugins(BufferedReader regFile)
{
ClassLoader cl = ClassLoader.getSystemClassLoader();
String className;
while((className = regFile.readLine()) != null) {
try {
Class ext = cl.loadClass(className);
Class[] ints = ext.getInterfaces();
boolean valid = false;
for(int i=0; i < ints.length; ++i) {
if(ints[i].getName().equals("Lyour.package.Extension"))
valid = true;
}
if(valid)
plugins.add(ext);
} catch(ClassNotFoundException e) {
log("Invalid entry in reg file.");
}
}
}
void initPlugins()
{
for(int i=0; i < plugins.size(); ++i) {
Extension e = (Extension)plugins[i].newInstance();
e.init(this, blabla);
// ...
}
}
All the buzzt CornedBee
"Writing specifications is like writing a novel. Writing code is like writing poetry."
- Anonymous, published by Raymond Chen
Don't PM me with your problems, I scan most of the forums daily. If you do PM me, I will not answer your question.