Making Java Programs Pluggable??

E

ebby83

Dear all.. im making a software that needs to be extendable (plugabble
) . Any idea how this can be done ? i need specific methods to be
extendable . The extended code must be put into JAR files and added to
the software !!

Thanks
 
H

hilz

Dear all.. im making a software that needs to be extendable (plugabble
) . Any idea how this can be done ? i need specific methods to be
extendable . The extended code must be put into JAR files and added to
the software !!

Thanks

Checkout www.netbeans.org or www.eclipse.org
They both have pluggable application frameworks that you can build you
applications on top.
Netbeans uses swing, eclipse uses SWT.
I personally prefer netbeans & swing. others might disagree. The Netbeans
documentation is a little lacking, but at the end of January, they are
supposed to be publishing a new tutorial for module development. stay tuned!

good luck
hilz
 
E

ebby83

i dont get it ?? actually im making a back end application that would
need to be extensible as per the clients need .. how does swing/swt
come into the picture ??
 
E

ebby83

i dont get it ?? actually im making a back end application that would
need to be extensible as per the clients need .. how does swing/swt
come into the picture ??
thanks
 
C

Chris Uppal

Dear all.. im making a software that needs to be extendable (plugabble
) . Any idea how this can be done ? i need specific methods to be
extendable . The extended code must be put into JAR files and added to
the software !!

A fairly typical approach to creating a pluggable framework is:

1) Define some interfaces that the pluggable classes must implement, and
through which they will interact with your framework.
2) Define some way (e.g. configuration files) for plugin-writers to tell you
what classes to use, and where (which JAR/directory) to load them from.
3) Create at least one classloader to load the classes at runtime.
4) Load the classes via the classloaders.
5) Create instances of the classes, casting them to the relevant interfaces.
6) Use the instances as normal.

Depending on what you want to do, you might be able to get away without
configuration files at all.

Depending on what you want to do, you might have to use one classloader per
plugin (which will allow you to "unplug" them dynamically as well as load
them), alternatively you may be able to get away without using one at all if
you are willing to put all the jar files on the classpath, and use the default
class loader (probably not a good idea in the long run).

You might find it easier to define factory objects as the "root" of each
plugin, so that the framework creates an instance of that, and then asks it to
do the rest of the work.

You might also need to think about security -- do you need to be able to load
"entrusted" plugins ?

You /might/ also need to think about dependencies -- do some plugins require
other plugins as pre-requisites ? Do they depend on specific versions of other
plugins ?

Don't expect to be able to make your application pluggable without changing the
architecture. You /may/ be able to do it (if you had a clean architecture in
the first place, /and/ if you are lucky) but don't /assume/ that you'll be able
to do it.

-- chris
 
S

Scott Ellsworth

i dont get it ?? actually im making a back end application that would
need to be extensible as per the clients need .. how does swing/swt
come into the picture ??

Both of those apps use plugability to add interface elements in the
main. (Not completely true, but pretty close.) Thus, if you are
looking for techniques to adapt, or platforms to use entire, the UI
framework would make a difference.

ANT uses a form of plugability to add tasks, via classpaths specified in
the taskdef and the task itself. Given your domain, something like
their approach might work well.

When I have needed a plugable system, I have a single, specific,
findable directory that I can plow through. Any jar in it gets scanned,
and a config file in meta-inf gets grabbed, which tells me what else to
look for.

Scott
 
E

ebby83

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 .

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 ??

Thanks for all the help Chris!!!
 
S

Sudsy

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 .

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 ??

It's another of those issues which is neither simple nor easy to
explain. If you don't understand the complexities then asking
someone to spoon-feed you a solution isn't fair to either party.
Can it be made to work? Certainly! Is is easy? Not hardly!
You'll have to do a /huge/ amount of reading and research to
arrive at a solution which works /for you!/
Even Kerboros isn't the right approach for every situation; it
incorporates some fascinating ways of addressing the issues but
is no more the "ultimate solution" than Digest authentication
for HTTP (read the RFC to find that the authors agree).
You'll have to come up with your own solution, one which balances
security and complexity. Such has always been the way...
 
H

hilz

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 .

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 ??

Thanks for all the help Chris!!!

These are all opensource projects, and you can check out the sources from
their cvs servers.
Details about the process can be found on their websites (which you should
be able to find out easily).
But it will be a waste of time to be looking at the code to decide if that
works for you or not.
You need to know the exact requirements of your application, and then read
the documentation of such frameworks to see if they apply to you. Don't
restrict yourself to what has been suggested here. Search for solutions.
Google is your friend. For example, "ant" was suggested, so you seach for
"ant" and find out that it leads you to apache.org. Looking at apache.org,
you can find tons of other opensource projects that are platforms or
frameworks for some sort of application, be it a web application or a
desktop application or whatever. Just read their documentation (or the
FAQ's) and it should be clear if any of them is applicable to you. For
example, if you find that apache has a project called "struts", you can go
to groups.google.com and look for "struts vs" and the first hit will be
"Struts vs Java Server Faces", so now you know that Struts has a competitor
(let's assume!) called Java Server Faces. So you look for Java Server Faces,
and it leads you to another framework called "Spring" and so on.
The amount of stuff that you are going to find is overwhelming, and the best
solution depends on your requirements and your preference.
Whatever you end up choosing, don't think it is going to be a breez. There
is a lot of reading and learning waiting for you.

Good luck
hilz
 
E

ebby83

thanks hilz .. thankfully im still working on design so I can try out
different things !! well im making a sort of DB access proc. that will
be implemented by my app. (server ) .. but I want that very procedure
to be extendable ( pluggable) . now imtrying to work out howto
implement what chris suggested but he suggested a crucial aspect that I
had overlooked !!! SECURITY .. so i have a new problem before having
figured out the old one .

Besides looking at eclipse pages i found out that it uses OSGI which is
a complicated framework for making applications pluggable .. dont know
yet if netbeans uses the same framework.
 
C

Chris Uppal

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
 
M

Mark Murphy

Besides looking at eclipse pages i found out that it uses OSGI which is
a complicated framework for making applications pluggable .. dont know
yet if netbeans uses the same framework.

You could check out http://jpf.sourceforge.net/ for the Java Plugin
framework. I have never used it. But it might help simplify thing for you.

Mark
 
E

ebby83

Hi . I now need to run specific methods from particular classes . For
eg . if my class ( class A ) has methods A1 , A2 and my plugin class B
has methods B1 , B2 ; where B1 is an alternate implementation of A1 .
Now the plugin requires methods B1 , A2 to be run ... is this possible
and if yes how ( since they are from different classes ) . Im assuming
you declare only one interface in which all these gunction definitions
are put.

Thanks ALL
 
C

Chris Uppal

Hi . I now need to run specific methods from particular classes . For
eg . if my class ( class A ) has methods A1 , A2 and my plugin class B
has methods B1 , B2 ; where B1 is an alternate implementation of A1 .
Now the plugin requires methods B1 , A2 to be run ... is this possible

You need the two classes to define the /same/ method (but with different
implementations). That's what pluggable /means/. (Consider real plugs -- they
couldn't possibly work unless they were all the same shape).

If you really do need to call different methods then most likely:
A) your design is wrong
or, less likely but not impossible:
B) you will have to use reflection (see java.lang.reflect package).

-- chris
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,774
Messages
2,569,599
Members
45,169
Latest member
ArturoOlne
Top