never worked on pluggable architectures before .. do you have any idea
where I can find some simple examples as browsing through the src of
ant, eclipse,netbeans will take some time .
Off the top of my head, no I can't think of one. Googling for something like
"pluggable architecture example" (or for some of the class/method names
mentioned below) may help. The problem with the examples that have been
mentioned so far, is that they are large, and the "pluggable" bit will only be
a small part of the overall structure -- making it difficult for you to find
the bits you are interested in, especially if you don't know what you are
looking for in the first place.
Here's a suggestion. Put your real application aside for a day or so, and put
together a very simple pluggable "hello world" type program. That should give
you some idea of what the concepts are and how they interrelate.
You could start by defining a my.stuff.Plugin interface, with maybe a
saySomething() method. Then you could create one or more classes with
instances that implement that interface. Then you could figure out how to load
those classes dynamically (see java.lang.Class.forName()) and create instances
of dynamically loaded classes (e.g. via java.lang.Class.newInstance()). Cast
the resulting objects to my.stuff.Plugin and invoke their saySomething()
methods. Perhaps you could identify classes to load by command-line arguments.
Then you can elaborate it a bit. Maybe scan a directory looking for JAR files
(as someone has suggested). Maybe exploring using a factory object (an object
whose job it is to make /other/ objects). Maybe look at defining what classes
to load in a config file of some sort. Try whatever it is that you don't feel
sure about how to approach (that's relevant ;-)
It would probably pay you to look at classloaders as part of the same
excursive. See java.lang.Classloader. Google for more info. BTW, if you are
using a classloader, then you /must/ make sure that Java's default classloader
won't get involved -- just make /damn sure/ that the .classfiles (or JAR files)
are not on your classpath. You can be sure that, whether or not you do /now/,
by the time you've finished this project you will know rather a lot about how
Java finds and loads classes as runtime ;-)
The more you get clear about how the basics work (in this way, or in whatever
way of learning suits you) the better chance you have of understanding, and
learning more from, the other examples of pluggable architectures that have
been mentioned.
BTW, the other posters (or I myself) may have given you the idea that this is
desperately difficult stuff. If so, then please don't worry -- it's not really
very difficult, /if/ you take it step-by-step and keep everything simple. Don't
try change your main app until you understand (most of) what you are doing, try
everything out in a very small test program first.
One other word of advice -- if you are currently unsure of how classloading,
etc, works, and you normally prefer to code/debug in one of the big IDEs, then
I'd advise you /not/ to use the IDE for the little test programs. It can be
very difficult to work out what is really going on with classpaths, etc, when
the IDE is doing things for you. In particular it will naturally assume, when
you write and compile some Java code, that you want Java to be able to find it
automatically -- but that's exactly what you /don't/ want to happen in a
pluggable architecture, you want to be in control of all that stuff explicitly.
You Said :
"You might also need to think about security -- do you need to be able
to load
"entrusted" plugins ? " -- HOW ?? is there some way to enforce digital
signatures with plugins ??
Before getting involved in security thinking, I'd go back and check just
exactly what your requirements are. It may be (for instance) that the controls
applied by the database itself are all you need for safety, or it may be that
only trusted people will be allowed to put JAR files onto your filesystem, or
there may be some other reason why you don't need to implement any security
yourself. If you do need it then I can't give much advice (it's not something
I've ever had to do). Still, there are two "levels" at which you can apply
some sort of control. You can restrict your application to loading signed (and
perhaps sealed) JAR files (Sun provide tools to help with this stuff, and I
know there are examples that can be found with Google -- 'cos I remember
reading some ;-) If you need to go beyond that then Java has ways of
controlling what a plugin is allowed to do -- look up SecurityManager and go on
from there. (But I'd guess that you /don't/ need to go that far -- we can hope
so, anyway ;-)
Good luck !
-- chris