On Jun 16, 7:12 pm, Arne Vajh?j <a...@vajhoej.dk> wrote:
> There are some variations, but this is the Java way of doing it.
>
[quoted text clipped - 4 lines]
>
> will load using standard classloader.
I'm not sure you need to muck around with explicit classloader use or
reflection at all. Suppose a plugin is packaged as two files, a .class
and a .template file, with the .class the obvious -- a class file, for
a class implementing your Plugin interface. The .template file would
be a serialized instance of the class, and the interface would specify
instance methods.
The .class is in the classpath of the app (probably because a Plugins
directory is in the classpath of the app). At runtime, the app uses
File and other java.io tools to enumerate the .template files and
ObjectInputStream to get copies in RAM. Then it casts them to Plugin
and invokes instance methods. These objects can be factories for
document types the plugin supports, or they can be codecs whose
instance methods encode and decode streams, or ... the sky is the
limit.
The closest to reflection this comes, besides the reflection that goes
on under the hood with serialization, is when you cast the Object to a
Plugin and pray. :)
(Obviously you'd want to trap and recover from some exceptions during
plugin-loading, and log failed plugin loads in some way, while
ignoring only the failed ones. IOException, ClassCastException, and
ClassNotFoundException are the obvious ones, the latter caused if
someone installs only the .template file of a pair...)
Carsten Ringe - 17 Jun 2007 10:00 GMT
>> There are some variations, but this is the Java way of doing it.
>>
[quoted text clipped - 4 lines]
>>
>> will load using standard classloader.
There is another way to do plugins. Take a look at
http://java.sun.com/j2se/1.4.2/docs/guide/jar/jar.html#Service%20Provider
to see an example. It's a nice way to find plugins even on runtime, so
users can just drop another plugin into the proper directory and rerun
the plugin management algorithm to start using it.
Carsten
Twisted - 17 Jun 2007 21:57 GMT
> > On Jun 16, 7:12 pm, Arne Vajh?j <a...@vajhoej.dk> wrote:
> >> There are some variations, but this is the Java way of doing it.
[quoted text clipped - 10 lines]
> users can just drop another plugin into the proper directory and rerun
> the plugin management algorithm to start using it.
Like most of them, but unlike mine, this seems to require a zero-
argument constructor in the class. :)
(Mine actually may require more than just a single .class file and
a .template mind you -- in the specific case of an abstract factory
there's likely to be additional .class files in the bundle. I'm not
sure how you'd detect missing dependencies and reject the plugin
immediately instead of flaming out with NoClassDefFoundError later on
trying to use the plugin, in case additional .class files were
required and not all of them were present. Perhaps the Plugin
interface would specify a "selfTest()" method that would try to access
all of the dependencies and throw a nicer exception if it caught
NoClassDefFoundError in the attempt. Or an instance of each class
would be in the .template file, and the plugin loader would try to
read everything in the ObjectInputStream though only keep the first
Object it saw. Or it would try to cast them all to Plugin, keep all of
the ones that made it through that filter, and complain if the number
of these was zero...allowing a plugin bundle with multiple points of
interface.)