Willian said:
I want to be able to give people the ability to write their own code
in a class that I can then load into my program during run-time. I
want to be able to pass in parameters to this class from my
application and this class must not be allowed to start other threads.
Would I use an interface for this or is there some pattern I should
use? How would I go about doing this?
This is a fairly easy thing to do. Here's the general idea:
You'd define an interface, and provide it to the people who will be
writing your plugin. You'll then create a ClassLoader to load their
code (probably a URLClassLoader to make it easy), and cast the result of
Class.newInstance to the interface type. The plugin code should *not*
be available in the classpath, but the interface *should*. The
ClassLoader that loads the plugin should be a child (or, perhaps, a more
distant descendant) of the ClassLoader that loads the plugin.
You should strongly consider making the plugins JAR files instead of
bare classes, with the main plugin class specified as a JAR manifest
attribute. It's not difficult to retrieve manifest attributes with the
JarFile class in java.util.jar, and that gives the author far more
flexibility to use good software engineering practices.
As far as preventing the plugin code from starting Threads, the way to
do this is with a SecurityManager. That will allow you to gain fine-
grained control over what the plugin code may or may not do (there are
other things as well; for example, you probably don't want the plugin to
call System.exit -- which a security manager can also prevent). The
Java security model is a little involved for a newsgroup post, but feel
free to start reading up, starting from the SecurityManager class, and
ask any questions that come up.
Good luck.
--
www.designacourse.com
The Easiest Way to Train Anyone... Anywhere.
Chris Smith - Lead Software Developer/Technical Trainer
MindIQ Corporation